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