Commit | Line | Data |
---|---|---|
7a1b1f8b AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
e42eb508 | 3 | // $Id: apt-config.cc,v 1.5 1999/05/23 05:45:12 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> | |
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 | { | |
e42eb508 | 34 | if (I[1] == 0 || strlen(I[1]) == 0) |
7a1b1f8b | 35 | return _error->Error("Arguments not in pairs"); |
e42eb508 AL |
36 | |
37 | // Check if the caller has requested a directory path | |
38 | if (I[1][strlen(I[1])-1] == '/') | |
39 | { | |
40 | char S[300]; | |
41 | strcpy(S,I[1]); | |
42 | S[strlen(S)-1] = 0; | |
43 | if (_config->Exists(S) == true) | |
44 | cout << *I << "=\"" << _config->FindDir(S) << '"' << endl; | |
45 | } | |
46 | ||
7a1b1f8b AL |
47 | if (_config->Exists(I[1]) == true) |
48 | cout << *I << "=\"" << _config->Find(I[1]) << '"' << endl; | |
49 | } | |
50 | ||
51 | return true; | |
52 | } | |
53 | /*}}}*/ | |
54 | // ShowHelp - Show the help screen /*{{{*/ | |
55 | // --------------------------------------------------------------------- | |
56 | /* */ | |
57 | int ShowHelp() | |
58 | { | |
59 | cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE << | |
60 | " compiled on " << __DATE__ << " " << __TIME__ << endl; | |
04aa15a8 AL |
61 | if (_config->FindB("version") == true) |
62 | return 100; | |
7a1b1f8b AL |
63 | |
64 | cout << "Usage: apt-config [options] command" << endl; | |
65 | cout << endl; | |
66 | cout << "apt-config is a simple tool to read the APT config file" << endl; | |
67 | cout << endl; | |
68 | cout << "Commands:" << endl; | |
69 | cout << " shell - Shell mode" << endl; | |
70 | cout << endl; | |
71 | cout << "Options:" << endl; | |
72 | cout << " -h This help text." << endl; | |
73 | cout << " -c=? Read this configuration file" << endl; | |
7974b907 | 74 | cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl; |
7a1b1f8b AL |
75 | return 100; |
76 | } | |
77 | /*}}}*/ | |
78 | ||
79 | int main(int argc,const char *argv[]) | |
80 | { | |
81 | CommandLine::Args Args[] = { | |
82 | {'h',"help","help",0}, | |
04aa15a8 | 83 | {'v',"version","version",0}, |
7a1b1f8b AL |
84 | {'c',"config-file",0,CommandLine::ConfigFile}, |
85 | {'o',"option",0,CommandLine::ArbItem}, | |
86 | {0,0,0,0}}; | |
83d89a9f AL |
87 | CommandLine::Dispatch Cmds[] = {{"shell",&DoShell}, |
88 | {0,0}}; | |
7a1b1f8b AL |
89 | |
90 | // Parse the command line and initialize the package library | |
91 | CommandLine CmdL(Args,_config); | |
92 | if (pkgInitialize(*_config) == false || | |
93 | CmdL.Parse(argc,argv) == false) | |
94 | { | |
95 | _error->DumpErrors(); | |
96 | return 100; | |
97 | } | |
98 | ||
99 | // See if the help should be shown | |
100 | if (_config->FindB("help") == true || | |
101 | CmdL.FileSize() == 0) | |
102 | return ShowHelp(); | |
7a1b1f8b | 103 | |
83d89a9f AL |
104 | // Match the operation |
105 | CmdL.DispatchArg(Cmds); | |
106 | ||
7a1b1f8b AL |
107 | // Print any errors or warnings found during parsing |
108 | if (_error->empty() == false) | |
109 | { | |
110 | bool Errors = _error->PendingError(); | |
111 | _error->DumpErrors(); | |
112 | return Errors == true?100:0; | |
113 | } | |
114 | ||
115 | return 0; | |
116 | } |