1 ////////////////////////////////////////////////////////////////////////
2 // FILE: argparser.cpp
3 // AUTHOR: Johannes Winkelmann, jw@tks6.net
4 // COPYRIGHT: (c) 2002 by Johannes Winkelmann
5 // ---------------------------------------------------------------------
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 ////////////////////////////////////////////////////////////////////////
11
12 using namespace std;
13
14 #include "argparser.h"
15
16 /*!
17 Construct a ArgParser object
18 \param argc argument count
19 \param argv argument vector
20 */
21 ArgParser::ArgParser( int argc, char** argv )
22 : m_isCommandGiven( false ),
23 m_isForced( false ),
24 m_isTest( false ),
25 m_isAlternateConfigGiven( false ),
26 m_useCache( false ),
27 m_calledAsPrtCache( false ),
28 m_alternateConfigFile( "" ),
29 m_pkgmkArgs( "" ),
30 m_pkgaddArgs( "" ),
31 m_pkgrmArgs( "" ),
32 m_installRoot( "" ),
33 m_ignore( "" ),
34 m_argc( argc ),
35 m_argv( argv ),
36 m_verbose( 0 ),
37 m_writeLog( false ),
38 m_hasFilter( false ),
39 m_noStdConfig( false ),
40 m_nodeps( false ),
41 m_all( false ),
42 m_printPath( false ),
43 m_execPreInstall( false ),
44 m_execPostInstall( false ),
45 m_preferHigher( false ),
46 m_strictDiff( false ),
47 m_useRegex(false),
48 m_fullPath(false),
49 m_recursive(false),
50 m_printTree(false)
51 {
52 }
53
54
55 /*!
56 \return true if an alternate configuration file is given
57 */
58 bool ArgParser::isAlternateConfigGiven() const
59 {
60 return m_isAlternateConfigGiven;
61 }
62
63
64 /*!
65 \return true if a command is given
66 */
67 bool ArgParser::isCommandGiven() const
68 {
69 return m_isCommandGiven;
70 }
71
72
73 /*!
74 \return a list of arguments not processed by ArgParser
75 */
76 const list<char*>& ArgParser::otherArgs() const
77 {
78 return m_otherArgs;
79 }
80
81
82 /*!
83 \return what command was given
84 */
85 ArgParser::Type ArgParser::commandType() const
86 {
87 return m_commandType;
88 }
89
90
91 /*!
92 \return addtional arguments to pkgmk
93 */
94 const string& ArgParser::pkgmkArgs() const
95 {
96 return m_pkgmkArgs;
97 }
98
99
100 /*!
101 \return addtional arguments to pkgadd
102 */
103 const string& ArgParser::pkgaddArgs() const
104 {
105 return m_pkgaddArgs;
106 }
107
108
109 /*!
110 \return the name of the alternative configuration file
111 */
112 const string& ArgParser::alternateConfigFile() const
113 {
114 return m_alternateConfigFile;
115 }
116
117
118 /*!
119 parse the arguments
120 \return true on success
121 */
122 bool ArgParser::parse()
123 {
124 const int commandCount = 35;
125 string commands[commandCount] = { "list", "search", "dsearch",
126 "info",
127 "depends", "install", "depinst",
128 "help", "isinst", "dup", "update",
129 "quickdep", "diff", "quickdiff",
130 "grpinst", "version", "cache",
131 "path", "listinst", "printf", "readme",
132 "dependent", "sysup", "current",
133 "fsearch", "lock", "unlock",
134 "listlocked", "cat", "ls", "edit",
135 "remove", "deptree", "dumpconfig",
136 "listorphans" };
137
138 Type commandID[commandCount] = { LIST, SEARCH, DSEARCH, INFO,
139 DEPENDS, INSTALL, DEPINST,
140 HELP, ISINST, DUP, UPDATE,
141 QUICKDEP, DIFF, QUICKDIFF,
142 GRPINST, SHOW_VERSION, CREATE_CACHE,
143 PATH, LISTINST, PRINTF, README,
144 DEPENDENT, SYSUP, CURRENT,
145 FSEARCH, LOCK, UNLOCK, LISTLOCKED,
146 CAT, LS, EDIT, REMOVE, DEPTREE,
147 DUMPCONFIG, LISTORPHANS };
148 if ( m_argc < 2 ) {
149 return false;
150 }
151
152 // if called from a symlink ending on prt-cache, use cached
153 // access
154 string app = m_argv[0];
155 string::size_type pos = app.rfind( "/" );
156 if ( pos != string::npos ) {
157 app = app.substr( pos );
158 }
159 if ( app.find( "prt-cache" ) != string::npos ) {
160 m_useCache = true;
161 m_calledAsPrtCache = true;
162 }
163
164 for ( int i = 1; i < m_argc; ++i ) {
165 if ( m_argv[i][0] == '-' ) {
166 string s = m_argv[i];
167 if ( s == "-v" ) {
168 m_verbose += 1;
169 } else if ( s == "-vv" ) {
170 m_verbose += 2;
171 } else if ( s == "--force" ) {
172 m_isForced = true;
173 } else if ( s == "--test" ) {
174 m_isTest = true;
175 } else if ( s == "--cache" ) {
176 m_useCache = true;
177 } else if ( s == "--nodeps" ) {
178 m_nodeps = true;
179 } else if ( s == "--all" ) {
180 m_all = true;
181 } else if ( s == "--path" ) {
182 m_printPath = true;
183 } else if ( s == "--log" ) {
184 m_writeLog = true;
185 } else if ( s == "--pre-install" ) {
186 m_execPreInstall = true;
187 } else if ( s == "--post-install" ) {
188 m_execPostInstall = true;
189 } else if ( s == "--install-scripts" ) {
190 m_execPreInstall = true;
191 m_execPostInstall = true;
192 } else if ( s == "--no-std-config" ) {
193 m_noStdConfig = true;
194 } else if ( s == "--prefer-higher" || s == "-ph" ) {
195 m_preferHigher = true;
196 } else if ( s == "--strict-diff" || s == "-sd" ) {
197 m_strictDiff = true;
198 } else if ( s == "--regex" ) {
199 m_useRegex = true;
200 } else if ( s == "--full" ) {
201 m_fullPath = true;
202 } else if ( s == "--recursive" ) {
203 m_recursive = true;
204 } else if ( s == "--tree" ) {
205 m_printTree = true;
206
207 } else if ( s == "-f" ) {
208 m_pkgaddArgs += " " + s;
209 } else if ( s == "-fr" ) {
210 m_pkgmkArgs += " -f";
211 } else if ( s == "-if" ) {
212 m_pkgmkArgs += " " + s;
213 } else if ( s == "-uf" ) {
214 m_pkgmkArgs += " " + s;
215 } else if ( s == "-im" ) {
216 m_pkgmkArgs += " " + s;
217 } else if ( s == "-um" ) {
218 m_pkgmkArgs += " " + s;
219 } else if ( s == "-kw" ) {
220 m_pkgmkArgs += " " + s;
221 } else if ( s == "-ns" ) {
222 m_pkgmkArgs += " " + s;
223 } else if ( s == "-fi" ) {
224 m_pkgaddArgs += " -f";
225 }
226
227 // substrings
228 else if ( s.substr( 0, 8 ) == "--margs=" ) {
229 m_pkgmkArgs += " " + s.substr( 8 );
230 } else if ( s.substr( 0, 8 ) == "--aargs=" ) {
231 m_pkgaddArgs += " " + s.substr( 8 );
232 } else if ( s.substr( 0, 8 ) == "--rargs=" ) {
233 m_pkgrmArgs = s.substr( 8 );
234 } else if ( s.substr( 0, 7 ) == "--sort=" ) {
235 m_sortArgs = s.substr( 7 );
236 } else if ( s.substr( 0, 9 ) == "--filter=" ) {
237 m_filter = s.substr( 9 );
238 m_hasFilter = true;
239 } else if ( s.substr( 0, 9 ) == "--config=" ) {
240 m_alternateConfigFile = s.substr( 9 );
241 m_isAlternateConfigGiven = true;
242 } else if ( s.substr( 0, 16 ) == "--config-append=" ) {
243 m_configData.push_back(make_pair(m_argv[i]+16,
244 CONFIG_APPEND ) );
245 } else if ( s.substr( 0, 17 ) == "--config-prepend=" ) {
246 m_configData.push_back(make_pair(m_argv[i]+17,
247 CONFIG_PREPEND ) );
248 } else if ( s.substr( 0, 13 ) == "--config-set=" ) {
249 m_configData.push_back(make_pair(m_argv[i]+13, CONFIG_SET ) );
250 } else if ( s.substr( 0, 15 ) == "--install-root=" ) {
251 m_installRoot = s.substr(15);
252 } else if ( s.substr( 0, 9 ) == "--ignore=" ) {
253 m_ignore = s.substr(9);
254 } else {
255 m_unknownOption = s;
256 return false;
257 }
258 } else {
259 if (!m_isCommandGiven) {
260 string s = m_argv[i];
261 m_commandName = s;
262 for ( int i = 0; i < commandCount; ++i ) {
263 if ( s == commands[i] ) {
264 m_isCommandGiven = true;
265 m_commandType = commandID[i];
266 break;
267 }
268 }
269 // first argument must be command
270 if ( !m_isCommandGiven ) {
271 return false;
272 }
273 } else {
274 m_otherArgs.push_back( m_argv[i] );
275 }
276 }
277 }
278
279
280
281 return m_isCommandGiven;
282 }
283
284
285 /*!
286 \return true whether --force has been specified
287 */
288 bool ArgParser::isForced() const
289 {
290 return m_isForced;
291 }
292
293
294 /*!
295 \return true whether --test has been specified
296 */
297 bool ArgParser::isTest() const
298 {
299 return m_isTest;
300 }
301
302
303 /*!
304 \return the level of verbose: -v -> 1, -vv -> 2
305 */
306 int ArgParser::verbose() const
307 {
308 return m_verbose;
309 }
310
311
312 /*!
313 \return whether --cache has been specified
314 */
315 bool ArgParser::useCache() const
316 {
317 return m_useCache;
318 }
319
320
321 /*!
322 \return whether prt-get was called as 'prt-cache' or not
323 */
324 bool ArgParser::wasCalledAsPrtCached() const
325 {
326 return m_calledAsPrtCache;
327 }
328
329 /*!
330 \return whether prt-get should write to a log file or not
331 */
332 bool ArgParser::writeLog() const
333 {
334 return m_writeLog;
335 }
336
337 /*!
338 \return the --sort="..." string
339 */
340 const string& ArgParser::sortArgs() const
341 {
342 return m_sortArgs;
343 }
344
345 /*!
346 \return whether there was a --filter argument
347 */
348 bool ArgParser::hasFilter() const
349 {
350 return m_hasFilter;
351 }
352
353
354 /*!
355 \return whether there was a --no-std-config argument
356 */
357 bool ArgParser::noStdConfig() const
358 {
359 return m_noStdConfig;
360 }
361
362
363 /*!
364 \return the --filter="..." string
365 */
366 const string& ArgParser::filter() const
367 {
368 return m_filter;
369 }
370
371 /*!
372 \return whether there was a --nodeps argument
373 */
374 bool ArgParser::nodeps() const
375 {
376 return m_nodeps;
377 }
378
379 /*!
380 \return whether there was a --all argument
381 */
382 bool ArgParser::all() const
383 {
384 return m_all;
385 }
386
387 bool ArgParser::printPath() const
388 {
389 return m_printPath;
390 }
391
392 bool ArgParser::recursive() const
393 {
394 return m_recursive;
395 }
396
397 bool ArgParser::printTree() const
398 {
399 return m_printTree;
400 }
401
402 const string& ArgParser::commandName() const
403 {
404 return m_commandName;
405 }
406
407 const string& ArgParser::unknownOption() const
408 {
409 return m_unknownOption;
410 }
411
412 bool ArgParser::execPreInstall() const
413 {
414 return m_execPreInstall;
415 }
416
417 bool ArgParser::execPostInstall() const
418 {
419 return m_execPostInstall;
420 }
421
422 const list< pair<char*, ArgParser::ConfigArgType> >
423 ArgParser::configData() const
424 {
425 return m_configData;
426 }
427
428 const string& ArgParser::installRoot() const
429 {
430 return m_installRoot;
431 }
432
433 const string& ArgParser::pkgrmArgs() const
434 {
435 return m_pkgrmArgs;
436 }
437
438 bool ArgParser::preferHigher() const
439 {
440 return m_preferHigher;
441 }
442
443 bool ArgParser::strictDiff() const
444 {
445 return m_strictDiff;
446 }
447
448 bool ArgParser::useRegex() const
449 {
450 return m_useRegex;
451 }
452
453 bool ArgParser::fullPath() const
454 {
455 return m_fullPath;
456 }
457
458
459 const string& ArgParser::ignore() const
460 {
461 return m_ignore;
462 }
|