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