Fixed a bug where SmartUnPack would be called with Immediate=true (to resolve dependa...
[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:
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 <apt-pkg/cmndline.h>
20#include <apt-pkg/error.h>
21#include <apt-pkg/init.h>
b2e465d6 22#include <apt-pkg/strutl.h>
aa833344
DK
23#include <apt-pkg/configuration.h>
24#include <apt-pkg/aptconfiguration.h>
7a1b1f8b 25
b2e465d6
AL
26#include <config.h>
27#include <apti18n.h>
233c2b66
AL
28
29#include <locale.h>
7a1b1f8b 30#include <iostream>
b2e465d6 31#include <string>
aa833344 32#include <vector>
7a1b1f8b 33 /*}}}*/
8f312f45 34using namespace std;
7a1b1f8b
AL
35
36// DoShell - Handle the shell command /*{{{*/
37// ---------------------------------------------------------------------
38/* */
39bool DoShell(CommandLine &CmdL)
40{
41 for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
42 {
e42eb508 43 if (I[1] == 0 || strlen(I[1]) == 0)
b2e465d6 44 return _error->Error(_("Arguments not in pairs"));
e42eb508 45
b2e465d6
AL
46 string key = I[1];
47 if (key.end()[-1] == '/') // old directory format
48 key.append("d");
49
50 if (_config->ExistsAny(key.c_str()))
51 cout << *I << "='" <<
52 SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
e42eb508 53
7a1b1f8b
AL
54 }
55
314037ba
AL
56 return true;
57}
58 /*}}}*/
59// DoDump - Dump the configuration space /*{{{*/
60// ---------------------------------------------------------------------
61/* */
62bool DoDump(CommandLine &CmdL)
63{
ff2a211a 64 _config->Dump(cout);
7a1b1f8b
AL
65 return true;
66}
67 /*}}}*/
68// ShowHelp - Show the help screen /*{{{*/
69// ---------------------------------------------------------------------
70/* */
71int ShowHelp()
72{
5b28c804
OS
73 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
74 COMMON_ARCH,__DATE__,__TIME__);
04aa15a8 75 if (_config->FindB("version") == true)
b2e465d6 76 return 0;
7a1b1f8b 77
b2e465d6
AL
78 cout <<
79 _("Usage: apt-config [options] command\n"
80 "\n"
81 "apt-config is a simple tool to read the APT config file\n"
82 "\n"
83 "Commands:\n"
84 " shell - Shell mode\n"
85 " dump - Show the configuration\n"
86 "\n"
87 "Options:\n"
88 " -h This help text.\n"
89 " -c=? Read this configuration file\n"
a2884e32 90 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
b2e465d6 91 return 0;
7a1b1f8b
AL
92}
93 /*}}}*/
92fcbfc1 94int main(int argc,const char *argv[]) /*{{{*/
7a1b1f8b
AL
95{
96 CommandLine::Args Args[] = {
97 {'h',"help","help",0},
04aa15a8 98 {'v',"version","version",0},
7a1b1f8b
AL
99 {'c',"config-file",0,CommandLine::ConfigFile},
100 {'o',"option",0,CommandLine::ArbItem},
101 {0,0,0,0}};
83d89a9f 102 CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
314037ba 103 {"dump",&DoDump},
83d89a9f 104 {0,0}};
67111687
AL
105
106 // Set up gettext support
107 setlocale(LC_ALL,"");
108 textdomain(PACKAGE);
109
7a1b1f8b
AL
110 // Parse the command line and initialize the package library
111 CommandLine CmdL(Args,_config);
b2e465d6
AL
112 if (pkgInitConfig(*_config) == false ||
113 CmdL.Parse(argc,argv) == false ||
114 pkgInitSystem(*_config,_system) == false)
7a1b1f8b
AL
115 {
116 _error->DumpErrors();
117 return 100;
118 }
119
120 // See if the help should be shown
121 if (_config->FindB("help") == true ||
122 CmdL.FileSize() == 0)
123 return ShowHelp();
7a1b1f8b 124
aa833344
DK
125 std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
126 _config->Clear("Acquire::Languages");
127 for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
128 _config->Set("Acquire::Languages::", *l);
129
130 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
131 _config->Clear("APT::Architectures");
132 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
133 _config->Set("APT::Architectures::", *a);
134
83d89a9f
AL
135 // Match the operation
136 CmdL.DispatchArg(Cmds);
137
7a1b1f8b
AL
138 // Print any errors or warnings found during parsing
139 if (_error->empty() == false)
140 {
141 bool Errors = _error->PendingError();
142 _error->DumpErrors();
143 return Errors == true?100:0;
144 }
145
146 return 0;
147}
92fcbfc1 148 /*}}}*/