1 /** \ingroup popt
2 * \file popt/popt.c
3 */
4
5 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist */
8
9 #undef MYDEBUG
10
11 #include "system.h"
12
13 #if HAVE_FLOAT_H
14 #include <float.h>
15 #endif
16 #include <math.h>
17
18 #include "findme.h"
19 #include "poptint.h"
20
21 #ifdef MYDEBUG
22 /*@unchecked@*/
23 int _popt_debug = 0;
24 #endif
25
26 #ifndef HAVE_STRERROR
strerror(int errno)27 static char * strerror(int errno) {
28 extern int sys_nerr;
29 extern char * sys_errlist[];
30
31 if ((0 <= errno) && (errno < sys_nerr))
32 return sys_errlist[errno];
33 else
34 return POPT_("unknown errno");
35 }
36 #endif
37
38 #ifdef MYDEBUG
prtcon(const char * msg,poptContext con)39 /*@unused@*/ static void prtcon(const char *msg, poptContext con)
40 {
41 if (msg) fprintf(stderr, "%s", msg);
42 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
43 con, con->os,
44 (con->os->nextCharArg ? con->os->nextCharArg : ""),
45 (con->os->nextArg ? con->os->nextArg : ""),
46 con->os->next,
47 (con->os->argv && con->os->argv[con->os->next]
48 ? con->os->argv[con->os->next] : ""));
49 }
50 #endif
51
poptSetExecPath(poptContext con,const char * path,int allowAbsolute)52 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
53 {
54 con->execPath = _free(con->execPath);
55 con->execPath = xstrdup(path);
56 con->execAbsolute = allowAbsolute;
57 /*@-nullstate@*/ /* LCL: con->execPath can be NULL? */
58 return;
59 /*@=nullstate@*/
60 }
61
invokeCallbacksPRE(poptContext con,const struct poptOption * opt)62 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
63 /*@globals internalState@*/
64 /*@modifies internalState@*/
65 {
66 if (opt != NULL)
67 for (; opt->longName || opt->shortName || opt->arg; opt++) {
68 if (opt->arg == NULL) continue; /* XXX program error. */
69 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
70 /* Recurse on included sub-tables. */
71 invokeCallbacksPRE(con, opt->arg);
72 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
73 (opt->argInfo & POPT_CBFLAG_PRE))
74 { /*@-castfcnptr@*/
75 poptCallbackType cb = (poptCallbackType)opt->arg;
76 /*@=castfcnptr@*/
77 /* Perform callback. */
78 /*@-moduncon -noeffectuncon @*/
79 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
80 /*@=moduncon =noeffectuncon @*/
81 }
82 }
83 }
84
invokeCallbacksPOST(poptContext con,const struct poptOption * opt)85 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
86 /*@globals internalState@*/
87 /*@modifies internalState@*/
88 {
89 if (opt != NULL)
90 for (; opt->longName || opt->shortName || opt->arg; opt++) {
91 if (opt->arg == NULL) continue; /* XXX program error. */
92 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
93 /* Recurse on included sub-tables. */
94 invokeCallbacksPOST(con, opt->arg);
95 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
96 (opt->argInfo & POPT_CBFLAG_POST))
97 { /*@-castfcnptr@*/
98 poptCallbackType cb = (poptCallbackType)opt->arg;
99 /*@=castfcnptr@*/
100 /* Perform callback. */
101 /*@-moduncon -noeffectuncon @*/
102 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
103 /*@=moduncon =noeffectuncon @*/
104 }
105 }
106 }
107
invokeCallbacksOPTION(poptContext con,const struct poptOption * opt,const struct poptOption * myOpt,const void * myData,int shorty)108 static void invokeCallbacksOPTION(poptContext con,
109 const struct poptOption * opt,
110 const struct poptOption * myOpt,
111 /*@null@*/ const void * myData, int shorty)
112 /*@globals internalState@*/
113 /*@modifies internalState@*/
114 {
115 const struct poptOption * cbopt = NULL;
116
117 if (opt != NULL)
118 for (; opt->longName || opt->shortName || opt->arg; opt++) {
119 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
120 /* Recurse on included sub-tables. */
121 if (opt->arg != NULL) /* XXX program error */
122 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
123 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
124 !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
125 /* Save callback info. */
126 cbopt = opt;
127 } else if (cbopt != NULL &&
128 ((myOpt->shortName && opt->shortName && shorty &&
129 myOpt->shortName == opt->shortName) ||
130 (myOpt->longName && opt->longName &&
131 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
132 !strcmp(myOpt->longName, opt->longName)))
133 /*@=nullpass@*/
134 )
135 { /*@-castfcnptr@*/
136 poptCallbackType cb = (poptCallbackType)cbopt->arg;
137 /*@=castfcnptr@*/
138 const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
139 /* Perform callback. */
140 if (cb != NULL) { /* XXX program error */
141 /*@-moduncon -noeffectuncon @*/
142 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
143 con->os->nextArg, cbData);
144 /*@=moduncon =noeffectuncon @*/
145 }
146 /* Terminate (unless explcitly continuing). */
147 if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
148 return;
149 }
150 }
151 }
152
poptGetContext(const char * name,int argc,const char ** argv,const struct poptOption * options,int flags)153 poptContext poptGetContext(const char * name, int argc, const char ** argv,
154 const struct poptOption * options, int flags)
155 {
156 poptContext con = malloc(sizeof(*con));
157
158 if (con == NULL) return NULL; /* XXX can't happen */
159 memset(con, 0, sizeof(*con));
160
161 con->os = con->optionStack;
162 con->os->argc = argc;
163 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
164 con->os->argv = argv;
165 /*@=dependenttrans =assignexpose@*/
166 con->os->argb = NULL;
167
168 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
169 con->os->next = 1; /* skip argv[0] */
170
171 con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
172 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
173 con->options = options;
174 /*@=dependenttrans =assignexpose@*/
175 con->aliases = NULL;
176 con->numAliases = 0;
177 con->flags = flags;
178 con->execs = NULL;
179 con->numExecs = 0;
180 con->finalArgvAlloced = argc * 2;
181 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
182 con->execAbsolute = 1;
183 con->arg_strip = NULL;
184
185 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
186 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
187
188 if (name) {
189 char * t = malloc(strlen(name) + 1);
190 if (t) con->appName = strcpy(t, name);
191 }
192
193 /*@-internalglobs@*/
194 invokeCallbacksPRE(con, con->options);
195 /*@=internalglobs@*/
196
197 return con;
198 }
199
cleanOSE(struct optionStackEntry * os)200 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
201 /*@uses os @*/
202 /*@releases os->nextArg, os->argv, os->argb @*/
203 /*@modifies os @*/
204 {
205 os->nextArg = _free(os->nextArg);
206 os->argv = _free(os->argv);
207 os->argb = PBM_FREE(os->argb);
208 }
209
210 /*@-boundswrite@*/
poptResetContext(poptContext con)211 void poptResetContext(poptContext con)
212 {
213 int i;
214
215 if (con == NULL) return;
216 while (con->os > con->optionStack) {
217 cleanOSE(con->os--);
218 }
219 con->os->argb = PBM_FREE(con->os->argb);
220 con->os->currAlias = NULL;
221 con->os->nextCharArg = NULL;
222 con->os->nextArg = NULL;
223 con->os->next = 1; /* skip argv[0] */
224
225 con->numLeftovers = 0;
226 con->nextLeftover = 0;
227 con->restLeftover = 0;
228 con->doExec = NULL;
229
230 if (con->finalArgv != NULL)
231 for (i = 0; i < con->finalArgvCount; i++) {
232 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
233 con->finalArgv[i] = _free(con->finalArgv[i]);
234 /*@=unqualifiedtrans@*/
235 }
236
237 con->finalArgvCount = 0;
238 con->arg_strip = PBM_FREE(con->arg_strip);
239 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
240 return;
241 /*@=nullstate@*/
242 }
243 /*@=boundswrite@*/
244
245 /* Only one of longName, shortName should be set, not both. */
246 /*@-boundswrite@*/
handleExec(poptContext con,const char * longName,char shortName)247 static int handleExec(/*@special@*/ poptContext con,
248 /*@null@*/ const char * longName, char shortName)
249 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
250 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
251 /*@modifies con @*/
252 {
253 poptItem item;
254 int i;
255
256 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
257 return 0;
258
259 for (i = con->numExecs - 1; i >= 0; i--) {
260 item = con->execs + i;
261 if (longName && !(item->option.longName &&
262 !strcmp(longName, item->option.longName)))
263 continue;
264 else if (shortName != item->option.shortName)
265 continue;
266 break;
267 }
268 if (i < 0) return 0;
269
270
271 if (con->flags & POPT_CONTEXT_NO_EXEC)
272 return 1;
273
274 if (con->doExec == NULL) {
275 con->doExec = con->execs + i;
276 return 1;
277 }
278
279 /* We already have an exec to do; remember this option for next
280 time 'round */
281 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
282 con->finalArgvAlloced += 10;
283 con->finalArgv = realloc(con->finalArgv,
284 sizeof(*con->finalArgv) * con->finalArgvAlloced);
285 }
286
287 i = con->finalArgvCount++;
288 if (con->finalArgv != NULL) /* XXX can't happen */
289 { char *s = malloc((longName ? strlen(longName) : 0) + 3);
290 if (s != NULL) { /* XXX can't happen */
291 if (longName)
292 sprintf(s, "--%s", longName);
293 else
294 sprintf(s, "-%c", shortName);
295 con->finalArgv[i] = s;
296 } else
297 con->finalArgv[i] = NULL;
298 }
299
300 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
301 return 1;
302 /*@=nullstate@*/
303 }
304 /*@=boundswrite@*/
305
306 /* Only one of longName, shortName may be set at a time */
handleAlias(poptContext con,const char * longName,char shortName,const char * nextCharArg)307 static int handleAlias(/*@special@*/ poptContext con,
308 /*@null@*/ const char * longName, char shortName,
309 /*@exposed@*/ /*@null@*/ const char * nextCharArg)
310 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
311 con->os->currAlias, con->os->currAlias->option.longName @*/
312 /*@modifies con @*/
313 {
314 poptItem item = con->os->currAlias;
315 int rc;
316 int i;
317
318 if (item) {
319 if (longName && (item->option.longName &&
320 !strcmp(longName, item->option.longName)))
321 return 0;
322 if (shortName && shortName == item->option.shortName)
323 return 0;
324 }
325
326 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
327 return 0;
328
329 for (i = con->numAliases - 1; i >= 0; i--) {
330 item = con->aliases + i;
331 if (longName && !(item->option.longName &&
332 !strcmp(longName, item->option.longName)))
333 continue;
334 else if (shortName != item->option.shortName)
335 continue;
336 break;
337 }
338 if (i < 0) return 0;
339
340 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
341 return POPT_ERROR_OPTSTOODEEP;
342
343 /*@-boundsread@*/
344 if (nextCharArg && *nextCharArg)
345 con->os->nextCharArg = nextCharArg;
346 /*@=boundsread@*/
347
348 con->os++;
349 con->os->next = 0;
350 con->os->stuffed = 0;
351 con->os->nextArg = NULL;
352 con->os->nextCharArg = NULL;
353 con->os->currAlias = con->aliases + i;
354 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
355 &con->os->argc, &con->os->argv);
356 con->os->argb = NULL;
357
358 return (rc ? rc : 1);
359 }
360
361 /*@-bounds -boundswrite @*/
execCommand(poptContext con)362 static int execCommand(poptContext con)
363 /*@globals internalState @*/
364 /*@modifies internalState @*/
365 {
366 poptItem item = con->doExec;
367 const char ** argv;
368 int argc = 0;
369 int rc;
370
371 if (item == NULL) /*XXX can't happen*/
372 return POPT_ERROR_NOARG;
373
374 if (item->argv == NULL || item->argc < 1 ||
375 (!con->execAbsolute && strchr(item->argv[0], '/')))
376 return POPT_ERROR_NOARG;
377
378 argv = malloc(sizeof(*argv) *
379 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
380 if (argv == NULL) return POPT_ERROR_MALLOC; /* XXX can't happen */
381
382 if (!strchr(item->argv[0], '/') && con->execPath) {
383 char *s = alloca(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
384 sprintf(s, "%s/%s", con->execPath, item->argv[0]);
385 argv[argc] = s;
386 } else {
387 argv[argc] = findProgramPath(item->argv[0]);
388 }
389 if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
390
391 if (item->argc > 1) {
392 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
393 argc += (item->argc - 1);
394 }
395
396 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
397 memcpy(argv + argc, con->finalArgv,
398 sizeof(*argv) * con->finalArgvCount);
399 argc += con->finalArgvCount;
400 }
401
402 if (con->leftovers != NULL && con->numLeftovers > 0) {
403 #if 0
404 argv[argc++] = "--";
405 #endif
406 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
407 argc += con->numLeftovers;
408 }
409
410 argv[argc] = NULL;
411
412 #ifdef __hpux
413 rc = setresuid(getuid(), getuid(),-1);
414 if (rc) return POPT_ERROR_ERRNO;
415 #else
416 /*
417 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
418 * XXX sez' Timur Bakeyev <mc@bat.ru>
419 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
420 */
421 #if defined(HAVE_SETUID)
422 rc = setuid(getuid());
423 if (rc) return POPT_ERROR_ERRNO;
424 #elif defined (HAVE_SETREUID)
425 rc = setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */
426 if (rc) return POPT_ERROR_ERRNO;
427 #else
428 ; /* Can't drop privileges */
429 #endif
430 #endif
431
432 if (argv[0] == NULL)
433 return POPT_ERROR_NOARG;
434
435 #ifdef MYDEBUG
436 if (_popt_debug)
437 { const char ** avp;
438 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
439 for (avp = argv; *avp; avp++)
440 fprintf(stderr, " '%s'", *avp);
441 fprintf(stderr, "\n");
442 }
443 #endif
444
445 rc = execvp(argv[0], (char *const *)argv);
446
447 return POPT_ERROR_ERRNO;
448 }
449 /*@=bounds =boundswrite @*/
450
451 /*@-boundswrite@*/
452 /*@observer@*/ /*@null@*/ static const struct poptOption *
findOption(const struct poptOption * opt,const char * longName,char shortName,poptCallbackType * callback,const void ** callbackData,int singleDash)453 findOption(const struct poptOption * opt, /*@null@*/ const char * longName,
454 char shortName,
455 /*@null@*/ /*@out@*/ poptCallbackType * callback,
456 /*@null@*/ /*@out@*/ const void ** callbackData,
457 int singleDash)
458 /*@modifies *callback, *callbackData */
459 {
460 const struct poptOption * cb = NULL;
461
462 /* This happens when a single - is given */
463 if (singleDash && !shortName && (longName && *longName == '\0'))
464 shortName = '-';
465
466 for (; opt->longName || opt->shortName || opt->arg; opt++) {
467
468 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
469 const struct poptOption * opt2;
470
471 /* Recurse on included sub-tables. */
472 if (opt->arg == NULL) continue; /* XXX program error */
473 opt2 = findOption(opt->arg, longName, shortName, callback,
474 callbackData, singleDash);
475 if (opt2 == NULL) continue;
476 /* Sub-table data will be inheirited if no data yet. */
477 if (!(callback && *callback)) return opt2;
478 if (!(callbackData && *callbackData == NULL)) return opt2;
479 /*@-observertrans -dependenttrans @*/
480 *callbackData = opt->descrip;
481 /*@=observertrans =dependenttrans @*/
482 return opt2;
483 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
484 cb = opt;
485 } else if (longName && opt->longName &&
486 (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
487 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
488 !strcmp(longName, opt->longName))
489 /*@=nullpass@*/
490 {
491 break;
492 } else if (shortName && shortName == opt->shortName) {
493 break;
494 }
495 }
496
497 if (!opt->longName && !opt->shortName)
498 return NULL;
499 /*@-modobserver -mods @*/
500 if (callback) *callback = NULL;
501 if (callbackData) *callbackData = NULL;
502 if (cb) {
503 if (callback)
504 /*@-castfcnptr@*/
505 *callback = (poptCallbackType)cb->arg;
506 /*@=castfcnptr@*/
507 if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
508 if (callbackData)
509 /*@-observertrans@*/ /* FIX: typedef double indirection. */
510 *callbackData = cb->descrip;
511 /*@=observertrans@*/
512 }
513 }
514 /*@=modobserver =mods @*/
515
516 return opt;
517 }
518 /*@=boundswrite@*/
519
findNextArg(poptContext con,unsigned argx,int delete_arg)520 static const char * findNextArg(/*@special@*/ poptContext con,
521 unsigned argx, int delete_arg)
522 /*@uses con->optionStack, con->os,
523 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
524 /*@modifies con @*/
525 {
526 struct optionStackEntry * os = con->os;
527 const char * arg;
528
529 do {
530 int i;
531 arg = NULL;
532 while (os->next == os->argc && os > con->optionStack) os--;
533 if (os->next == os->argc && os == con->optionStack) break;
534 if (os->argv != NULL)
535 for (i = os->next; i < os->argc; i++) {
536 /*@-sizeoftype@*/
537 if (os->argb && PBM_ISSET(i, os->argb))
538 /*@innercontinue@*/ continue;
539 if (*os->argv[i] == '-')
540 /*@innercontinue@*/ continue;
541 if (--argx > 0)
542 /*@innercontinue@*/ continue;
543 arg = os->argv[i];
544 if (delete_arg) {
545 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
546 if (os->argb != NULL) /* XXX can't happen */
547 PBM_SET(i, os->argb);
548 }
549 /*@innerbreak@*/ break;
550 /*@=sizeoftype@*/
551 }
552 if (os > con->optionStack) os--;
553 } while (arg == NULL);
554 return arg;
555 }
556
557 /*@-boundswrite@*/
558 static /*@only@*/ /*@null@*/ const char *
expandNextArg(poptContext con,const char * s)559 expandNextArg(/*@special@*/ poptContext con, const char * s)
560 /*@uses con->optionStack, con->os,
561 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
562 /*@modifies con @*/
563 {
564 const char * a = NULL;
565 size_t alen;
566 char *t, *te;
567 size_t tn = strlen(s) + 1;
568 char c;
569
570 te = t = malloc(tn);;
571 if (t == NULL) return NULL; /* XXX can't happen */
572 while ((c = *s++) != '\0') {
573 switch (c) {
574 #if 0 /* XXX can't do this */
575 case '\\': /* escape */
576 c = *s++;
577 /*@switchbreak@*/ break;
578 #endif
579 case '!':
580 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
581 /*@switchbreak@*/ break;
582 /* XXX Make sure that findNextArg deletes only next arg. */
583 if (a == NULL) {
584 if ((a = findNextArg(con, 1, 1)) == NULL)
585 /*@switchbreak@*/ break;
586 }
587 s += 3;
588
589 alen = strlen(a);
590 tn += alen;
591 *te = '\0';
592 t = realloc(t, tn);
593 te = t + strlen(t);
594 strncpy(te, a, alen); te += alen;
595 continue;
596 /*@notreached@*/ /*@switchbreak@*/ break;
597 default:
598 /*@switchbreak@*/ break;
599 }
600 *te++ = c;
601 }
602 *te = '\0';
603 t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
604 return t;
605 }
606 /*@=boundswrite@*/
607
poptStripArg(poptContext con,int which)608 static void poptStripArg(/*@special@*/ poptContext con, int which)
609 /*@uses con->arg_strip, con->optionStack @*/
610 /*@defines con->arg_strip @*/
611 /*@modifies con @*/
612 {
613 /*@-sizeoftype@*/
614 if (con->arg_strip == NULL)
615 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
616 if (con->arg_strip != NULL) /* XXX can't happen */
617 PBM_SET(which, con->arg_strip);
618 /*@=sizeoftype@*/
619 /*@-compdef@*/ /* LCL: con->arg_strip udefined? */
620 return;
621 /*@=compdef@*/
622 }
623
poptSaveLong(long * arg,int argInfo,long aLong)624 int poptSaveLong(long * arg, int argInfo, long aLong)
625 {
626 /* XXX Check alignment, may fail on funky platforms. */
627 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
628 return POPT_ERROR_NULLARG;
629
630 if (argInfo & POPT_ARGFLAG_NOT)
631 aLong = ~aLong;
632 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
633 case 0:
634 *arg = aLong;
635 break;
636 case POPT_ARGFLAG_OR:
637 *arg |= aLong;
638 break;
639 case POPT_ARGFLAG_AND:
640 *arg &= aLong;
641 break;
642 case POPT_ARGFLAG_XOR:
643 *arg ^= aLong;
644 break;
645 default:
646 return POPT_ERROR_BADOPERATION;
647 /*@notreached@*/ break;
648 }
649 return 0;
650 }
651
poptSaveInt(int * arg,int argInfo,long aLong)652 int poptSaveInt(/*@null@*/ int * arg, int argInfo, long aLong)
653 {
654 /* XXX Check alignment, may fail on funky platforms. */
655 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
656 return POPT_ERROR_NULLARG;
657
658 if (argInfo & POPT_ARGFLAG_NOT)
659 aLong = ~aLong;
660 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
661 case 0:
662 *arg = aLong;
663 break;
664 case POPT_ARGFLAG_OR:
665 *arg |= aLong;
666 break;
667 case POPT_ARGFLAG_AND:
668 *arg &= aLong;
669 break;
670 case POPT_ARGFLAG_XOR:
671 *arg ^= aLong;
672 break;
673 default:
674 return POPT_ERROR_BADOPERATION;
675 /*@notreached@*/ break;
676 }
677 return 0;
678 }
679
680 /*@-boundswrite@*/
681 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
poptGetNextOpt(poptContext con)682 int poptGetNextOpt(poptContext con)
683 {
684 const struct poptOption * opt = NULL;
685 int done = 0;
686
687 if (con == NULL)
688 return -1;
689 while (!done) {
690 const char * origOptString = NULL;
691 poptCallbackType cb = NULL;
692 const void * cbData = NULL;
693 const char * longArg = NULL;
694 int canstrip = 0;
695 int shorty = 0;
696
697 while (!con->os->nextCharArg && con->os->next == con->os->argc
698 && con->os > con->optionStack) {
699 cleanOSE(con->os--);
700 }
701 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
702 /*@-internalglobs@*/
703 invokeCallbacksPOST(con, con->options);
704 /*@=internalglobs@*/
705 if (con->doExec) return execCommand(con);
706 return -1;
707 }
708
709 /* Process next long option */
710 if (!con->os->nextCharArg) {
711 char * localOptString, * optString;
712 int thisopt;
713
714 /*@-sizeoftype@*/
715 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
716 con->os->next++;
717 continue;
718 }
719 /*@=sizeoftype@*/
720 thisopt = con->os->next;
721 if (con->os->argv != NULL) /* XXX can't happen */
722 origOptString = con->os->argv[con->os->next++];
723
724 if (origOptString == NULL) /* XXX can't happen */
725 return POPT_ERROR_BADOPT;
726
727 if (con->restLeftover || *origOptString != '-') {
728 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
729 con->restLeftover = 1;
730 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
731 con->os->nextArg = xstrdup(origOptString);
732 return 0;
733 }
734 if (con->leftovers != NULL) /* XXX can't happen */
735 con->leftovers[con->numLeftovers++] = origOptString;
736 continue;
737 }
738
739 /* Make a copy we can hack at */
740 localOptString = optString =
741 strcpy(alloca(strlen(origOptString) + 1), origOptString);
742
743 if (optString[0] == '\0')
744 return POPT_ERROR_BADOPT;
745
746 if (optString[1] == '-' && !optString[2]) {
747 con->restLeftover = 1;
748 continue;
749 } else {
750 char *oe;
751 int singleDash;
752
753 optString++;
754 if (*optString == '-')
755 singleDash = 0, optString++;
756 else
757 singleDash = 1;
758
759 /* XXX aliases with arg substitution need "--alias=arg" */
760 if (handleAlias(con, optString, '\0', NULL))
761 continue;
762
763 if (handleExec(con, optString, '\0'))
764 continue;
765
766 /* Check for "--long=arg" option. */
767 for (oe = optString; *oe && *oe != '='; oe++)
768 {};
769 if (*oe == '=') {
770 *oe++ = '\0';
771 /* XXX longArg is mapped back to persistent storage. */
772 longArg = origOptString + (oe - localOptString);
773 }
774
775 opt = findOption(con->options, optString, '\0', &cb, &cbData,
776 singleDash);
777 if (!opt && !singleDash)
778 return POPT_ERROR_BADOPT;
779 }
780
781 if (!opt) {
782 con->os->nextCharArg = origOptString + 1;
783 } else {
784 if (con->os == con->optionStack &&
785 opt->argInfo & POPT_ARGFLAG_STRIP)
786 {
787 canstrip = 1;
788 poptStripArg(con, thisopt);
789 }
790 shorty = 0;
791 }
792 }
793
794 /* Process next short option */
795 /*@-branchstate@*/ /* FIX: W2DO? */
796 if (con->os->nextCharArg) {
797 origOptString = con->os->nextCharArg;
798
799 con->os->nextCharArg = NULL;
800
801 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
802 continue;
803
804 if (handleExec(con, NULL, *origOptString)) {
805 /* Restore rest of short options for further processing */
806 origOptString++;
807 if (*origOptString != '\0')
808 con->os->nextCharArg = origOptString;
809 continue;
810 }
811
812 opt = findOption(con->options, NULL, *origOptString, &cb,
813 &cbData, 0);
814 if (!opt)
815 return POPT_ERROR_BADOPT;
816 shorty = 1;
817
818 origOptString++;
819 if (*origOptString != '\0')
820 con->os->nextCharArg = origOptString;
821 }
822 /*@=branchstate@*/
823
824 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
825 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
826 if (poptSaveInt((int *)opt->arg, opt->argInfo, 1L))
827 return POPT_ERROR_BADOPERATION;
828 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
829 if (opt->arg) {
830 if (poptSaveInt((int *)opt->arg, opt->argInfo, (long)opt->val))
831 return POPT_ERROR_BADOPERATION;
832 }
833 } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
834 con->os->nextArg = _free(con->os->nextArg);
835 /*@-usedef@*/ /* FIX: W2DO? */
836 if (longArg) {
837 /*@=usedef@*/
838 longArg = expandNextArg(con, longArg);
839 con->os->nextArg = longArg;
840 } else if (con->os->nextCharArg) {
841 longArg = expandNextArg(con, con->os->nextCharArg);
842 con->os->nextArg = longArg;
843 con->os->nextCharArg = NULL;
844 } else {
845 while (con->os->next == con->os->argc &&
846 con->os > con->optionStack) {
847 cleanOSE(con->os--);
848 }
849 if (con->os->next == con->os->argc) {
850 if (!(opt->argInfo & POPT_ARGFLAG_OPTIONAL))
851 /*@-compdef@*/ /* FIX: con->os->argv not defined */
852 return POPT_ERROR_NOARG;
853 /*@=compdef@*/
854 con->os->nextArg = NULL;
855 } else {
856
857 /*
858 * Make sure this isn't part of a short arg or the
859 * result of an alias expansion.
860 */
861 if (con->os == con->optionStack &&
862 (opt->argInfo & POPT_ARGFLAG_STRIP) &&
863 canstrip) {
864 poptStripArg(con, con->os->next);
865 }
866
867 if (con->os->argv != NULL) { /* XXX can't happen */
868 /* XXX watchout: subtle side-effects live here. */
869 longArg = con->os->argv[con->os->next++];
870 longArg = expandNextArg(con, longArg);
871 con->os->nextArg = longArg;
872 }
873 }
874 }
875 longArg = NULL;
876
877 if (opt->arg) {
878 switch (opt->argInfo & POPT_ARG_MASK) {
879 case POPT_ARG_STRING:
880 /* XXX memory leak, hard to plug */
881 *((const char **) opt->arg) = (con->os->nextArg)
882 ? xstrdup(con->os->nextArg) : NULL;
883 /*@switchbreak@*/ break;
884
885 case POPT_ARG_INT:
886 case POPT_ARG_LONG:
887 { long aLong = 0;
888 char *end;
889
890 if (con->os->nextArg) {
891 aLong = strtol(con->os->nextArg, &end, 0);
892 if (!(end && *end == '\0'))
893 return POPT_ERROR_BADNUMBER;
894 }
895
896 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
897 if (aLong == LONG_MIN || aLong == LONG_MAX)
898 return POPT_ERROR_OVERFLOW;
899 if (poptSaveLong((long *)opt->arg, opt->argInfo, aLong))
900 return POPT_ERROR_BADOPERATION;
901 } else {
902 if (aLong > INT_MAX || aLong < INT_MIN)
903 return POPT_ERROR_OVERFLOW;
904 if (poptSaveInt((int *)opt->arg, opt->argInfo, aLong))
905 return POPT_ERROR_BADOPERATION;
906 }
907 } /*@switchbreak@*/ break;
908
909 case POPT_ARG_FLOAT:
910 case POPT_ARG_DOUBLE:
911 { double aDouble = 0.0;
912 char *end;
913
914 if (con->os->nextArg) {
915 /*@-mods@*/
916 int saveerrno = errno;
917 errno = 0;
918 aDouble = strtod(con->os->nextArg, &end);
919 if (errno == ERANGE)
920 return POPT_ERROR_OVERFLOW;
921 errno = saveerrno;
922 /*@=mods@*/
923 if (*end != '\0')
924 return POPT_ERROR_BADNUMBER;
925 }
926
927 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
928 *((double *) opt->arg) = aDouble;
929 } else {
930 #define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
931 if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
932 return POPT_ERROR_OVERFLOW;
933 if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
934 return POPT_ERROR_OVERFLOW;
935 *((float *) opt->arg) = aDouble;
936 }
937 } /*@switchbreak@*/ break;
938 default:
939 fprintf(stdout,
940 POPT_("option type (%d) not implemented in popt\n"),
941 (opt->argInfo & POPT_ARG_MASK));
942 exit(EXIT_FAILURE);
943 /*@notreached@*/ /*@switchbreak@*/ break;
944 }
945 }
946 }
947
948 if (cb) {
949 /*@-internalglobs@*/
950 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
951 /*@=internalglobs@*/
952 } else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
953 done = 1;
954
955 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
956 con->finalArgvAlloced += 10;
957 con->finalArgv = realloc(con->finalArgv,
958 sizeof(*con->finalArgv) * con->finalArgvAlloced);
959 }
960
961 if (con->finalArgv != NULL)
962 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
963 if (s != NULL) { /* XXX can't happen */
964 if (opt->longName)
965 sprintf(s, "%s%s",
966 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
967 opt->longName);
968 else
969 sprintf(s, "-%c", opt->shortName);
970 con->finalArgv[con->finalArgvCount++] = s;
971 } else
972 con->finalArgv[con->finalArgvCount++] = NULL;
973 }
974
975 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
976 /*@-ifempty@*/ ; /*@=ifempty@*/
977 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
978 /*@-ifempty@*/ ; /*@=ifempty@*/
979 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
980 if (con->finalArgv != NULL && con->os->nextArg)
981 con->finalArgv[con->finalArgvCount++] =
982 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
983 xstrdup(con->os->nextArg);
984 /*@=nullpass@*/
985 }
986 }
987
988 return (opt ? opt->val : -1); /* XXX can't happen */
989 }
990 /*@=boundswrite@*/
991
poptGetOptArg(poptContext con)992 const char * poptGetOptArg(poptContext con)
993 {
994 const char * ret = NULL;
995 /*@-branchstate@*/
996 if (con) {
997 ret = con->os->nextArg;
998 con->os->nextArg = NULL;
999 }
1000 /*@=branchstate@*/
1001 return ret;
1002 }
1003
poptGetArg(poptContext con)1004 const char * poptGetArg(poptContext con)
1005 {
1006 const char * ret = NULL;
1007 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1008 ret = con->leftovers[con->nextLeftover++];
1009 return ret;
1010 }
1011
poptPeekArg(poptContext con)1012 const char * poptPeekArg(poptContext con)
1013 {
1014 const char * ret = NULL;
1015 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1016 ret = con->leftovers[con->nextLeftover];
1017 return ret;
1018 }
1019
1020 /*@-boundswrite@*/
poptGetArgs(poptContext con)1021 const char ** poptGetArgs(poptContext con)
1022 {
1023 if (con == NULL ||
1024 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1025 return NULL;
1026
1027 /* some apps like [like RPM ;-) ] need this NULL terminated */
1028 con->leftovers[con->numLeftovers] = NULL;
1029
1030 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
1031 return (con->leftovers + con->nextLeftover);
1032 /*@=nullret =nullstate @*/
1033 }
1034 /*@=boundswrite@*/
1035
poptFreeContext(poptContext con)1036 poptContext poptFreeContext(poptContext con)
1037 {
1038 poptItem item;
1039 int i;
1040
1041 if (con == NULL) return con;
1042 poptResetContext(con);
1043 con->os->argb = _free(con->os->argb);
1044
1045 if (con->aliases != NULL)
1046 for (i = 0; i < con->numAliases; i++) {
1047 item = con->aliases + i;
1048 /*@-modobserver -observertrans -dependenttrans@*/
1049 item->option.longName = _free(item->option.longName);
1050 item->option.descrip = _free(item->option.descrip);
1051 item->option.argDescrip = _free(item->option.argDescrip);
1052 /*@=modobserver =observertrans =dependenttrans@*/
1053 item->argv = _free(item->argv);
1054 }
1055 con->aliases = _free(con->aliases);
1056
1057 if (con->execs != NULL)
1058 for (i = 0; i < con->numExecs; i++) {
1059 item = con->execs + i;
1060 /*@-modobserver -observertrans -dependenttrans@*/
1061 item->option.longName = _free(item->option.longName);
1062 item->option.descrip = _free(item->option.descrip);
1063 item->option.argDescrip = _free(item->option.argDescrip);
1064 /*@=modobserver =observertrans =dependenttrans@*/
1065 item->argv = _free(item->argv);
1066 }
1067 con->execs = _free(con->execs);
1068
1069 con->leftovers = _free(con->leftovers);
1070 con->finalArgv = _free(con->finalArgv);
1071 con->appName = _free(con->appName);
1072 con->otherHelp = _free(con->otherHelp);
1073 con->execPath = _free(con->execPath);
1074 con->arg_strip = PBM_FREE(con->arg_strip);
1075
1076 con = _free(con);
1077 return con;
1078 }
1079
poptAddAlias(poptContext con,struct poptAlias alias,int flags)1080 int poptAddAlias(poptContext con, struct poptAlias alias,
1081 /*@unused@*/ int flags)
1082 {
1083 poptItem item = alloca(sizeof(*item));
1084 memset(item, 0, sizeof(*item));
1085 item->option.longName = alias.longName;
1086 item->option.shortName = alias.shortName;
1087 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1088 item->option.arg = 0;
1089 item->option.val = 0;
1090 item->option.descrip = NULL;
1091 item->option.argDescrip = NULL;
1092 item->argc = alias.argc;
1093 item->argv = alias.argv;
1094 return poptAddItem(con, item, 0);
1095 }
1096
1097 /*@-boundswrite@*/
1098 /*@-mustmod@*/ /* LCL: con not modified? */
poptAddItem(poptContext con,poptItem newItem,int flags)1099 int poptAddItem(poptContext con, poptItem newItem, int flags)
1100 {
1101 poptItem * items, item;
1102 int * nitems;
1103
1104 switch (flags) {
1105 case 1:
1106 items = &con->execs;
1107 nitems = &con->numExecs;
1108 break;
1109 case 0:
1110 items = &con->aliases;
1111 nitems = &con->numAliases;
1112 break;
1113 default:
1114 return 1;
1115 /*@notreached@*/ break;
1116 }
1117
1118 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1119 if ((*items) == NULL)
1120 return 1;
1121
1122 item = (*items) + (*nitems);
1123
1124 item->option.longName =
1125 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1126 item->option.shortName = newItem->option.shortName;
1127 item->option.argInfo = newItem->option.argInfo;
1128 item->option.arg = newItem->option.arg;
1129 item->option.val = newItem->option.val;
1130 item->option.descrip =
1131 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1132 item->option.argDescrip =
1133 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1134 item->argc = newItem->argc;
1135 item->argv = newItem->argv;
1136
1137 (*nitems)++;
1138
1139 return 0;
1140 }
1141 /*@=mustmod@*/
1142 /*@=boundswrite@*/
1143
poptBadOption(poptContext con,int flags)1144 const char * poptBadOption(poptContext con, int flags)
1145 {
1146 struct optionStackEntry * os = NULL;
1147
1148 if (con != NULL)
1149 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1150
1151 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1152 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1153 /*@=nullderef@*/
1154 }
1155
poptStrerror(const int error)1156 const char * poptStrerror(const int error)
1157 {
1158 switch (error) {
1159 case POPT_ERROR_NOARG:
1160 return POPT_("missing argument");
1161 case POPT_ERROR_BADOPT:
1162 return POPT_("unknown option");
1163 case POPT_ERROR_BADOPERATION:
1164 return POPT_("mutually exclusive logical operations requested");
1165 case POPT_ERROR_NULLARG:
1166 return POPT_("opt->arg should not be NULL");
1167 case POPT_ERROR_OPTSTOODEEP:
1168 return POPT_("aliases nested too deeply");
1169 case POPT_ERROR_BADQUOTE:
1170 return POPT_("error in parameter quoting");
1171 case POPT_ERROR_BADNUMBER:
1172 return POPT_("invalid numeric value");
1173 case POPT_ERROR_OVERFLOW:
1174 return POPT_("number too large or too small");
1175 case POPT_ERROR_MALLOC:
1176 return POPT_("memory allocation failed");
1177 case POPT_ERROR_ERRNO:
1178 return strerror(errno);
1179 default:
1180 return POPT_("unknown error");
1181 }
1182 }
1183
poptStuffArgs(poptContext con,const char ** argv)1184 int poptStuffArgs(poptContext con, const char ** argv)
1185 {
1186 int argc;
1187 int rc;
1188
1189 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1190 return POPT_ERROR_OPTSTOODEEP;
1191
1192 for (argc = 0; argv[argc]; argc++)
1193 {};
1194
1195 con->os++;
1196 con->os->next = 0;
1197 con->os->nextArg = NULL;
1198 con->os->nextCharArg = NULL;
1199 con->os->currAlias = NULL;
1200 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1201 con->os->argb = NULL;
1202 con->os->stuffed = 1;
1203
1204 return rc;
1205 }
1206
poptGetInvocationName(poptContext con)1207 const char * poptGetInvocationName(poptContext con)
1208 {
1209 return (con->os->argv ? con->os->argv[0] : "");
1210 }
1211
1212 /*@-boundswrite@*/
poptStrippedArgv(poptContext con,int argc,char ** argv)1213 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1214 {
1215 int numargs = argc;
1216 int j = 1;
1217 int i;
1218
1219 /*@-sizeoftype@*/
1220 if (con->arg_strip)
1221 for (i = 1; i < argc; i++) {
1222 if (PBM_ISSET(i, con->arg_strip))
1223 numargs--;
1224 }
1225
1226 for (i = 1; i < argc; i++) {
1227 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1228 continue;
1229 argv[j] = (j < numargs) ? argv[i] : NULL;
1230 j++;
1231 }
1232 /*@=sizeoftype@*/
1233
1234 return numargs;
1235 }
1236 /*@=boundswrite@*/
1237