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