]> git.saurik.com Git - apt.git/blame - cmdline/apt-config.cc
use FileFd instead of forking the compression childs by hand
[apt.git] / cmdline / apt-config.cc
CommitLineData
7a1b1f8b
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
8f312f45 3// $Id: apt-config.cc,v 1.11 2003/01/11 07:18:44 jgg Exp $
7a1b1f8b
AL
4/* ######################################################################
5
6 APT Config - Program to manipulate APT configuration files
7
8 This program will parse a config file and then do something with it.
9
10 Commands:
11 shell - Shell mode. After this a series of word pairs should occure.
12 The first is the environment var to set and the second is
13 the key to set it from. Use like:
14 eval `apt-config shell QMode apt::QMode`
15
16 ##################################################################### */
17 /*}}}*/
18// Include Files /*{{{*/
ea542140
DK
19#include<config.h>
20
7a1b1f8b
AL
21#include <apt-pkg/cmndline.h>
22#include <apt-pkg/error.h>
23#include <apt-pkg/init.h>
b2e465d6 24#include <apt-pkg/strutl.h>
aa833344
DK
25#include <apt-pkg/configuration.h>
26#include <apt-pkg/aptconfiguration.h>
472ff00e 27#include <apt-pkg/pkgsystem.h>
7a1b1f8b 28
233c2b66 29#include <locale.h>
7a1b1f8b 30#include <iostream>
b2e465d6 31#include <string>
aa833344 32#include <vector>
ea542140
DK
33
34#include <apti18n.h>
7a1b1f8b 35 /*}}}*/
8f312f45 36using namespace std;
7a1b1f8b
AL
37
38// DoShell - Handle the shell command /*{{{*/
39// ---------------------------------------------------------------------
40/* */
41bool DoShell(CommandLine &CmdL)
42{
43 for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
44 {
e42eb508 45 if (I[1] == 0 || strlen(I[1]) == 0)
b2e465d6 46 return _error->Error(_("Arguments not in pairs"));
e42eb508 47
b2e465d6
AL
48 string key = I[1];
49 if (key.end()[-1] == '/') // old directory format
50 key.append("d");
51
52 if (_config->ExistsAny(key.c_str()))
53 cout << *I << "='" <<
54 SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
e42eb508 55
7a1b1f8b
AL
56 }
57
314037ba
AL
58 return true;
59}
60 /*}}}*/
61// DoDump - Dump the configuration space /*{{{*/
62// ---------------------------------------------------------------------
63/* */
64bool DoDump(CommandLine &CmdL)
65{
ff2a211a 66 _config->Dump(cout);
7a1b1f8b
AL
67 return true;
68}
69 /*}}}*/
70// ShowHelp - Show the help screen /*{{{*/
71// ---------------------------------------------------------------------
72/* */
73int ShowHelp()
74{
5b28c804
OS
75 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
76 COMMON_ARCH,__DATE__,__TIME__);
04aa15a8 77 if (_config->FindB("version") == true)
b2e465d6 78 return 0;
7a1b1f8b 79
b2e465d6
AL
80 cout <<
81 _("Usage: apt-config [options] command\n"
82 "\n"
83 "apt-config is a simple tool to read the APT config file\n"
84 "\n"
85 "Commands:\n"
86 " shell - Shell mode\n"
87 " dump - Show the configuration\n"
88 "\n"
89 "Options:\n"
90 " -h This help text.\n"
91 " -c=? Read this configuration file\n"
a2884e32 92 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
b2e465d6 93 return 0;
7a1b1f8b
AL
94}
95 /*}}}*/
92fcbfc1 96int main(int argc,const char *argv[]) /*{{{*/
7a1b1f8b
AL
97{
98 CommandLine::Args Args[] = {
99 {'h',"help","help",0},
04aa15a8 100 {'v',"version","version",0},
7a1b1f8b
AL
101 {'c',"config-file",0,CommandLine::ConfigFile},
102 {'o',"option",0,CommandLine::ArbItem},
103 {0,0,0,0}};
83d89a9f 104 CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
314037ba 105 {"dump",&DoDump},
83d89a9f 106 {0,0}};
67111687
AL
107
108 // Set up gettext support
109 setlocale(LC_ALL,"");
110 textdomain(PACKAGE);
111
7a1b1f8b
AL
112 // Parse the command line and initialize the package library
113 CommandLine CmdL(Args,_config);
b2e465d6
AL
114 if (pkgInitConfig(*_config) == false ||
115 CmdL.Parse(argc,argv) == false ||
116 pkgInitSystem(*_config,_system) == false)
7a1b1f8b
AL
117 {
118 _error->DumpErrors();
119 return 100;
120 }
121
122 // See if the help should be shown
123 if (_config->FindB("help") == true ||
124 CmdL.FileSize() == 0)
125 return ShowHelp();
7a1b1f8b 126
aa833344
DK
127 std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
128 _config->Clear("Acquire::Languages");
129 for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
130 _config->Set("Acquire::Languages::", *l);
131
132 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
133 _config->Clear("APT::Architectures");
134 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
135 _config->Set("APT::Architectures::", *a);
136
8e16d8c3
DK
137 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
138 _config->Clear("APT::Compressor");
139 string conf = "APT::Compressor::";
140 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
141 {
142 string comp = conf + c->Name + "::";
143 _config->Set(comp + "Name", c->Name);
144 _config->Set(comp + "Extension", c->Extension);
145 _config->Set(comp + "Binary", c->Binary);
146 _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
147 for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
148 _config->Set(comp + "CompressArg::", *a);
149 for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
150 _config->Set(comp + "UncompressArg::", *a);
151 }
152
83d89a9f
AL
153 // Match the operation
154 CmdL.DispatchArg(Cmds);
155
7a1b1f8b
AL
156 // Print any errors or warnings found during parsing
157 if (_error->empty() == false)
158 {
159 bool Errors = _error->PendingError();
160 _error->DumpErrors();
161 return Errors == true?100:0;
162 }
163
164 return 0;
165}
92fcbfc1 166 /*}}}*/