releasing package apt version 1.0.9.2
[ntk/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:
1e3f4083 11 shell - Shell mode. After this a series of word pairs should occur.
7a1b1f8b
AL
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
AL
28
29#include <iostream>
b2e465d6 30#include <string>
aa833344 31#include <vector>
453b82a3 32#include <string.h>
ea542140 33
b9179170
MV
34#include <apt-private/private-cmndline.h>
35
ea542140 36#include <apti18n.h>
7a1b1f8b 37 /*}}}*/
8f312f45 38using namespace std;
7a1b1f8b
AL
39
40// DoShell - Handle the shell command /*{{{*/
41// ---------------------------------------------------------------------
42/* */
c3ccac92 43static bool DoShell(CommandLine &CmdL)
7a1b1f8b
AL
44{
45 for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
46 {
e42eb508 47 if (I[1] == 0 || strlen(I[1]) == 0)
b2e465d6 48 return _error->Error(_("Arguments not in pairs"));
e42eb508 49
b2e465d6
AL
50 string key = I[1];
51 if (key.end()[-1] == '/') // old directory format
52 key.append("d");
53
54 if (_config->ExistsAny(key.c_str()))
55 cout << *I << "='" <<
56 SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
e42eb508 57
7a1b1f8b
AL
58 }
59
314037ba
AL
60 return true;
61}
62 /*}}}*/
63// DoDump - Dump the configuration space /*{{{*/
64// ---------------------------------------------------------------------
65/* */
c3ccac92 66static bool DoDump(CommandLine &CmdL)
314037ba 67{
a8813606
DK
68 bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
69 std::string const format = _config->Find("APT::Config::Dump::Format", "%f \"%v\";\n");
70 if (CmdL.FileSize() == 1)
71 _config->Dump(cout, NULL, format.c_str(), empty);
72 else
73 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
74 _config->Dump(cout, *I, format.c_str(), empty);
7a1b1f8b
AL
75 return true;
76}
77 /*}}}*/
78// ShowHelp - Show the help screen /*{{{*/
79// ---------------------------------------------------------------------
80/* */
65512241 81static bool ShowHelp(CommandLine &)
7a1b1f8b 82{
9179f697 83 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
5b28c804 84 COMMON_ARCH,__DATE__,__TIME__);
04aa15a8 85 if (_config->FindB("version") == true)
b9179170 86 return true;
7a1b1f8b 87
b2e465d6
AL
88 cout <<
89 _("Usage: apt-config [options] command\n"
90 "\n"
91 "apt-config is a simple tool to read the APT config file\n"
92 "\n"
93 "Commands:\n"
94 " shell - Shell mode\n"
95 " dump - Show the configuration\n"
96 "\n"
97 "Options:\n"
98 " -h This help text.\n"
99 " -c=? Read this configuration file\n"
a2884e32 100 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
b9179170 101 return true;
7a1b1f8b
AL
102}
103 /*}}}*/
92fcbfc1 104int main(int argc,const char *argv[]) /*{{{*/
7a1b1f8b 105{
83d89a9f 106 CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
314037ba 107 {"dump",&DoDump},
b9179170 108 {"help",&ShowHelp},
83d89a9f 109 {0,0}};
67111687 110
52922e2a 111 std::vector<CommandLine::Args> Args = getCommandArgs("apt-config", CommandLine::GetCommand(Cmds, argc, argv));
b9179170 112
67111687
AL
113 // Set up gettext support
114 setlocale(LC_ALL,"");
115 textdomain(PACKAGE);
116
7a1b1f8b 117 // Parse the command line and initialize the package library
b9179170 118 CommandLine CmdL(Args.data(),_config);
b2e465d6
AL
119 if (pkgInitConfig(*_config) == false ||
120 CmdL.Parse(argc,argv) == false ||
121 pkgInitSystem(*_config,_system) == false)
7a1b1f8b
AL
122 {
123 _error->DumpErrors();
124 return 100;
125 }
126
127 // See if the help should be shown
128 if (_config->FindB("help") == true ||
129 CmdL.FileSize() == 0)
b9179170 130 return ShowHelp(CmdL);
7a1b1f8b 131
aa833344
DK
132 std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
133 _config->Clear("Acquire::Languages");
134 for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
135 _config->Set("Acquire::Languages::", *l);
136
137 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
138 _config->Clear("APT::Architectures");
139 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
140 _config->Set("APT::Architectures::", *a);
141
8e16d8c3
DK
142 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
143 _config->Clear("APT::Compressor");
144 string conf = "APT::Compressor::";
145 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
146 {
147 string comp = conf + c->Name + "::";
148 _config->Set(comp + "Name", c->Name);
149 _config->Set(comp + "Extension", c->Extension);
150 _config->Set(comp + "Binary", c->Binary);
151 _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
152 for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
153 _config->Set(comp + "CompressArg::", *a);
154 for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
155 _config->Set(comp + "UncompressArg::", *a);
156 }
157
ce7f128c
DK
158 std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
159 _config->Clear("APT::Build-Profiles");
160 for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
161 _config->Set("APT::Build-Profiles::", *p);
162
83d89a9f
AL
163 // Match the operation
164 CmdL.DispatchArg(Cmds);
165
7a1b1f8b
AL
166 // Print any errors or warnings found during parsing
167 if (_error->empty() == false)
168 {
169 bool Errors = _error->PendingError();
170 _error->DumpErrors();
171 return Errors == true?100:0;
172 }
173
174 return 0;
175}
92fcbfc1 176 /*}}}*/