]>
Commit | Line | Data |
---|---|---|
7a1b1f8b AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | // $Id: apt-config.cc,v 1.1 1998/11/22 23:37:07 jgg Exp $ | |
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> | |
22 | #include "config.h" | |
23 | ||
24 | #include <iostream> | |
25 | /*}}}*/ | |
26 | ||
27 | // DoShell - Handle the shell command /*{{{*/ | |
28 | // --------------------------------------------------------------------- | |
29 | /* */ | |
30 | bool DoShell(CommandLine &CmdL) | |
31 | { | |
32 | for (const char **I = CmdL.FileList + 1; *I != 0; I += 2) | |
33 | { | |
34 | if (I[1] == 0) | |
35 | return _error->Error("Arguments not in pairs"); | |
36 | if (_config->Exists(I[1]) == true) | |
37 | cout << *I << "=\"" << _config->Find(I[1]) << '"' << endl; | |
38 | } | |
39 | ||
40 | return true; | |
41 | } | |
42 | /*}}}*/ | |
43 | // ShowHelp - Show the help screen /*{{{*/ | |
44 | // --------------------------------------------------------------------- | |
45 | /* */ | |
46 | int ShowHelp() | |
47 | { | |
48 | cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE << | |
49 | " compiled on " << __DATE__ << " " << __TIME__ << endl; | |
50 | ||
51 | cout << "Usage: apt-config [options] command" << endl; | |
52 | cout << endl; | |
53 | cout << "apt-config is a simple tool to read the APT config file" << endl; | |
54 | cout << endl; | |
55 | cout << "Commands:" << endl; | |
56 | cout << " shell - Shell mode" << endl; | |
57 | cout << endl; | |
58 | cout << "Options:" << endl; | |
59 | cout << " -h This help text." << endl; | |
60 | cout << " -c=? Read this configuration file" << endl; | |
61 | cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl; | |
62 | return 100; | |
63 | } | |
64 | /*}}}*/ | |
65 | ||
66 | int main(int argc,const char *argv[]) | |
67 | { | |
68 | CommandLine::Args Args[] = { | |
69 | {'h',"help","help",0}, | |
70 | {'c',"config-file",0,CommandLine::ConfigFile}, | |
71 | {'o',"option",0,CommandLine::ArbItem}, | |
72 | {0,0,0,0}}; | |
73 | ||
74 | // Parse the command line and initialize the package library | |
75 | CommandLine CmdL(Args,_config); | |
76 | if (pkgInitialize(*_config) == false || | |
77 | CmdL.Parse(argc,argv) == false) | |
78 | { | |
79 | _error->DumpErrors(); | |
80 | return 100; | |
81 | } | |
82 | ||
83 | // See if the help should be shown | |
84 | if (_config->FindB("help") == true || | |
85 | CmdL.FileSize() == 0) | |
86 | return ShowHelp(); | |
87 | ||
88 | // Match the operation | |
89 | struct | |
90 | { | |
91 | const char *Match; | |
92 | bool (*Handler)(CommandLine &); | |
93 | } Map[] = {{"shell",&DoShell}, | |
94 | {0,0}}; | |
95 | int I; | |
96 | for (I = 0; Map[I].Match != 0; I++) | |
97 | { | |
98 | if (strcmp(CmdL.FileList[0],Map[I].Match) == 0) | |
99 | { | |
100 | if (Map[I].Handler(CmdL) == false && _error->PendingError() == false) | |
101 | _error->Error("Handler silently failed"); | |
102 | break; | |
103 | } | |
104 | } | |
105 | ||
106 | // No matching name | |
107 | if (Map[I].Match == 0) | |
108 | _error->Error("Invalid operation %s", CmdL.FileList[0]); | |
109 | ||
110 | // Print any errors or warnings found during parsing | |
111 | if (_error->empty() == false) | |
112 | { | |
113 | bool Errors = _error->PendingError(); | |
114 | _error->DumpErrors(); | |
115 | return Errors == true?100:0; | |
116 | } | |
117 | ||
118 | return 0; | |
119 | } |