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