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