]> git.saurik.com Git - apt.git/blob - cmdline/apt-config.cc
merged from the debian-sid branch
[apt.git] / cmdline / apt-config.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-config.cc,v 1.11 2003/01/11 07:18:44 jgg Exp $
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 /*{{{*/
19 #include<config.h>
20
21 #include <apt-pkg/cmndline.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/init.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/configuration.h>
26 #include <apt-pkg/aptconfiguration.h>
27 #include <apt-pkg/pkgsystem.h>
28
29 #include <locale.h>
30 #include <iostream>
31 #include <string>
32 #include <vector>
33
34 #include <apti18n.h>
35 /*}}}*/
36 using namespace std;
37
38 // DoShell - Handle the shell command /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 bool DoShell(CommandLine &CmdL)
42 {
43 for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
44 {
45 if (I[1] == 0 || strlen(I[1]) == 0)
46 return _error->Error(_("Arguments not in pairs"));
47
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;
55
56 }
57
58 return true;
59 }
60 /*}}}*/
61 // DoDump - Dump the configuration space /*{{{*/
62 // ---------------------------------------------------------------------
63 /* */
64 bool DoDump(CommandLine &CmdL)
65 {
66 bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
67 std::string const format = _config->Find("APT::Config::Dump::Format", "%f \"%v\";\n");
68 if (CmdL.FileSize() == 1)
69 _config->Dump(cout, NULL, format.c_str(), empty);
70 else
71 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
72 _config->Dump(cout, *I, format.c_str(), empty);
73 return true;
74 }
75 /*}}}*/
76 // ShowHelp - Show the help screen /*{{{*/
77 // ---------------------------------------------------------------------
78 /* */
79 int ShowHelp()
80 {
81 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
82 COMMON_ARCH,__DATE__,__TIME__);
83 if (_config->FindB("version") == true)
84 return 0;
85
86 cout <<
87 _("Usage: apt-config [options] command\n"
88 "\n"
89 "apt-config is a simple tool to read the APT config file\n"
90 "\n"
91 "Commands:\n"
92 " shell - Shell mode\n"
93 " dump - Show the configuration\n"
94 "\n"
95 "Options:\n"
96 " -h This help text.\n"
97 " -c=? Read this configuration file\n"
98 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
99 return 0;
100 }
101 /*}}}*/
102 int main(int argc,const char *argv[]) /*{{{*/
103 {
104 CommandLine::Args Args[] = {
105 {'h',"help","help",0},
106 {'v',"version","version",0},
107 {'c',"config-file",0,CommandLine::ConfigFile},
108 {'o',"option",0,CommandLine::ArbItem},
109 {0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean},
110 {0,"format","APT::Config::Dump::Format",CommandLine::HasArg},
111 {0,0,0,0}};
112 CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
113 {"dump",&DoDump},
114 {0,0}};
115
116 // Set up gettext support
117 setlocale(LC_ALL,"");
118 textdomain(PACKAGE);
119
120 // Parse the command line and initialize the package library
121 CommandLine CmdL(Args,_config);
122 if (pkgInitConfig(*_config) == false ||
123 CmdL.Parse(argc,argv) == false ||
124 pkgInitSystem(*_config,_system) == false)
125 {
126 _error->DumpErrors();
127 return 100;
128 }
129
130 // See if the help should be shown
131 if (_config->FindB("help") == true ||
132 CmdL.FileSize() == 0)
133 return ShowHelp();
134
135 std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
136 _config->Clear("Acquire::Languages");
137 for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
138 _config->Set("Acquire::Languages::", *l);
139
140 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
141 _config->Clear("APT::Architectures");
142 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
143 _config->Set("APT::Architectures::", *a);
144
145 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
146 _config->Clear("APT::Compressor");
147 string conf = "APT::Compressor::";
148 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
149 {
150 string comp = conf + c->Name + "::";
151 _config->Set(comp + "Name", c->Name);
152 _config->Set(comp + "Extension", c->Extension);
153 _config->Set(comp + "Binary", c->Binary);
154 _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
155 for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
156 _config->Set(comp + "CompressArg::", *a);
157 for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
158 _config->Set(comp + "UncompressArg::", *a);
159 }
160
161 // Match the operation
162 CmdL.DispatchArg(Cmds);
163
164 // Print any errors or warnings found during parsing
165 if (_error->empty() == false)
166 {
167 bool Errors = _error->PendingError();
168 _error->DumpErrors();
169 return Errors == true?100:0;
170 }
171
172 return 0;
173 }
174 /*}}}*/