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 #include <float.h>
14 #include <math.h>
15 #include <unistd.h>
16 #include <limits.h>
17 #include <errno.h>
18
19 #include "poptint.h"
20
21 #ifdef HAVE_STDALIGN_H
22 #include <stdalign.h>
23 #define ALIGNOF(x) alignof(x)
24 #elif defined __GNUC__
25 #define ALIGNOF(x) __alignof__(x)
26 #else
27 #define ALIGNOF(x) sizeof(x)
28 #endif
29
30 #ifdef MYDEBUG
31 int _popt_debug = 0;
32 #endif
33
34 unsigned int _poptArgMask = POPT_ARG_MASK;
35 unsigned int _poptGroupMask = POPT_GROUP_MASK;
36
37 #if !defined(HAVE_STRERROR)
strerror(int errno)38 static char * strerror(int errno)
39 {
40 extern int sys_nerr;
41 extern char * sys_errlist[];
42
43 if ((0 <= errno) && (errno < sys_nerr))
44 return sys_errlist[errno];
45 else
46 return POPT_("unknown errno");
47 }
48 #endif
49
50 #ifdef MYDEBUG
prtcon(const char * msg,poptContext con)51 static void prtcon(const char *msg, poptContext con)
52 {
53 if (msg) fprintf(stderr, "%s", msg);
54 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
55 con, con->os,
56 (con->os->nextCharArg ? con->os->nextCharArg : ""),
57 (con->os->nextArg ? con->os->nextArg : ""),
58 con->os->next,
59 (con->os->argv && con->os->argv[con->os->next]
60 ? con->os->argv[con->os->next] : ""));
61 }
62 #endif
63
poptSetExecPath(poptContext con,const char * path,int allowAbsolute)64 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
65 {
66 con->execPath = _free(con->execPath);
67 con->execPath = xstrdup(path);
68 con->execAbsolute = allowAbsolute;
69 return;
70 }
71
invokeCallbacksPRE(poptContext con,const struct poptOption * opt)72 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
73 {
74 if (opt != NULL)
75 for (; opt->longName || opt->shortName || opt->arg; opt++) {
76 poptArg arg = { .ptr = opt->arg };
77 if (arg.ptr)
78 switch (poptArgType(opt)) {
79 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
80 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
81 invokeCallbacksPRE(con, arg.opt);
82 break;
83 case POPT_ARG_CALLBACK: /* Perform callback. */
84 if (!CBF_ISSET(opt, PRE))
85 break;
86 arg.cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
87 break;
88 }
89 }
90 }
91
invokeCallbacksPOST(poptContext con,const struct poptOption * opt)92 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
93 {
94 if (opt != NULL)
95 for (; opt->longName || opt->shortName || opt->arg; opt++) {
96 poptArg arg = { .ptr = opt->arg };
97 if (arg.ptr)
98 switch (poptArgType(opt)) {
99 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
100 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
101 invokeCallbacksPOST(con, arg.opt);
102 break;
103 case POPT_ARG_CALLBACK: /* Perform callback. */
104 if (!CBF_ISSET(opt, POST))
105 break;
106 arg.cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
107 break;
108 }
109 }
110 }
111
invokeCallbacksOPTION(poptContext con,const struct poptOption * opt,const struct poptOption * myOpt,const void * myData,int shorty)112 static void invokeCallbacksOPTION(poptContext con,
113 const struct poptOption * opt,
114 const struct poptOption * myOpt,
115 const void * myData, int shorty)
116 {
117 const struct poptOption * cbopt = NULL;
118 poptArg cbarg = { .ptr = NULL };
119
120 if (opt != NULL)
121 for (; opt->longName || opt->shortName || opt->arg; opt++) {
122 poptArg arg = { .ptr = opt->arg };
123 switch (poptArgType(opt)) {
124 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
125 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
126 if (opt->arg != NULL)
127 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
128 break;
129 case POPT_ARG_CALLBACK: /* Save callback info. */
130 if (CBF_ISSET(opt, SKIPOPTION))
131 break;
132 cbopt = opt;
133 cbarg.ptr = opt->arg;
134 break;
135 default: /* Perform callback on matching option. */
136 if (cbopt == NULL || cbarg.cb == NULL)
137 break;
138 if ((myOpt->shortName && opt->shortName && shorty &&
139 myOpt->shortName == opt->shortName)
140 || (myOpt->longName != NULL && opt->longName != NULL &&
141 !strcmp(myOpt->longName, opt->longName)))
142 { const void *cbData = (cbopt->descrip ? cbopt->descrip : myData);
143 cbarg.cb(con, POPT_CALLBACK_REASON_OPTION,
144 myOpt, con->os->nextArg, cbData);
145 /* Terminate (unless explcitly continuing). */
146 if (!CBF_ISSET(cbopt, CONTINUE))
147 return;
148 }
149 break;
150 }
151 }
152 }
153
poptGetContext(const char * name,int argc,const char ** argv,const struct poptOption * options,unsigned int flags)154 poptContext poptGetContext(const char * name, int argc, const char ** argv,
155 const struct poptOption * options, unsigned int flags)
156 {
157 poptContext con = malloc(sizeof(*con));
158
159 if (con == NULL) return NULL; /* XXX can't happen */
160 memset(con, 0, sizeof(*con));
161
162 con->os = con->optionStack;
163 con->os->argc = argc;
164 con->os->argv = argv;
165 con->os->argb = NULL;
166
167 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
168 con->os->next = 1; /* skip argv[0] */
169
170 con->leftovers = calloc( (size_t)(argc + 1), sizeof(*con->leftovers) );
171 con->options = options;
172 con->aliases = NULL;
173 con->numAliases = 0;
174 con->flags = flags;
175 con->execs = NULL;
176 con->numExecs = 0;
177 con->execFail = NULL;
178 con->finalArgvAlloced = argc * 2;
179 con->finalArgv = calloc( (size_t)con->finalArgvAlloced, sizeof(*con->finalArgv) );
180 con->execAbsolute = 1;
181 con->arg_strip = NULL;
182
183 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
184 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
185
186 if (name)
187 con->appName = xstrdup(name);
188
189 invokeCallbacksPRE(con, con->options);
190
191 return con;
192 }
193
cleanOSE(struct optionStackEntry * os)194 static void cleanOSE(struct optionStackEntry *os)
195 {
196 os->nextArg = _free(os->nextArg);
197 os->argv = _free(os->argv);
198 os->argb = PBM_FREE(os->argb);
199 }
200
poptResetContext(poptContext con)201 void poptResetContext(poptContext con)
202 {
203 int i;
204
205 if (con == NULL) return;
206 while (con->os > con->optionStack) {
207 cleanOSE(con->os--);
208 }
209 con->os->argb = PBM_FREE(con->os->argb);
210 con->os->currAlias = NULL;
211 con->os->nextCharArg = NULL;
212 con->os->nextArg = _free(con->os->nextArg);
213 con->os->next = 1; /* skip argv[0] */
214
215 con->numLeftovers = 0;
216 con->nextLeftover = 0;
217 con->restLeftover = 0;
218 con->doExec = NULL;
219 con->execFail = _free(con->execFail);
220
221 if (con->finalArgv != NULL)
222 for (i = 0; i < con->finalArgvCount; i++) {
223 con->finalArgv[i] = _free(con->finalArgv[i]);
224 }
225
226 con->finalArgvCount = 0;
227 con->arg_strip = PBM_FREE(con->arg_strip);
228 return;
229 }
230
231 /* Only one of longName, shortName should be set, not both. */
handleExec(poptContext con,const char * longName,char shortName)232 static int handleExec(poptContext con,
233 const char * longName, char shortName)
234 {
235 poptItem item;
236 int i;
237
238 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
239 return 0;
240
241 for (i = con->numExecs - 1; i >= 0; i--) {
242 item = con->execs + i;
243 if (longName && !(item->option.longName &&
244 !strcmp(longName, item->option.longName)))
245 continue;
246 else if (shortName != item->option.shortName)
247 continue;
248 break;
249 }
250 if (i < 0) return 0;
251
252
253 if (con->flags & POPT_CONTEXT_NO_EXEC)
254 return 1;
255
256 if (con->doExec == NULL) {
257 con->doExec = con->execs + i;
258 return 1;
259 }
260
261 /* We already have an exec to do; remember this option for next
262 time 'round */
263 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
264 con->finalArgvAlloced += 10;
265 con->finalArgv = realloc(con->finalArgv,
266 sizeof(*con->finalArgv) * con->finalArgvAlloced);
267 }
268
269 i = con->finalArgvCount++;
270 if (con->finalArgv != NULL) /* XXX can't happen */
271 { char *s = malloc((longName ? strlen(longName) : 0) + sizeof("--"));
272 if (s != NULL) { /* XXX can't happen */
273 con->finalArgv[i] = s;
274 *s++ = '-';
275 if (longName)
276 s = stpcpy( stpcpy(s, "-"), longName);
277 else
278 *s++ = shortName;
279 *s = '\0';
280 } else
281 con->finalArgv[i] = NULL;
282 }
283
284 return 1;
285 }
286
287 /**
288 * Compare long option for equality, adjusting for POPT_ARGFLAG_TOGGLE.
289 * @param opt option
290 * @param longName arg option
291 * @param longNameLen arg option length
292 * @return does long option match?
293 */
294 static int
longOptionStrcmp(const struct poptOption * opt,const char * longName,size_t longNameLen)295 longOptionStrcmp(const struct poptOption * opt,
296 const char * longName, size_t longNameLen)
297 {
298 const char * optLongName = opt->longName;
299 int rc;
300
301 if (optLongName == NULL || longName == NULL) /* XXX can't heppen */
302 return 0;
303
304 if (F_ISSET(opt, TOGGLE)) {
305 if (optLongName[0] == 'n' && optLongName[1] == 'o') {
306 optLongName += sizeof("no") - 1;
307 if (optLongName[0] == '-')
308 optLongName++;
309 }
310 if (longName[0] == 'n' && longName[1] == 'o') {
311 longName += sizeof("no") - 1;
312 longNameLen -= sizeof("no") - 1;
313 if (longName[0] == '-') {
314 longName++;
315 longNameLen--;
316 }
317 }
318 }
319 rc = (int)(strlen(optLongName) == longNameLen);
320 if (rc)
321 rc = (int)(strncmp(optLongName, longName, longNameLen) == 0);
322 return rc;
323 }
324
325 /* Only one of longName, shortName may be set at a time */
handleAlias(poptContext con,const char * longName,size_t longNameLen,char shortName,const char * nextArg)326 static int handleAlias(poptContext con,
327 const char * longName, size_t longNameLen,
328 char shortName,
329 const char * nextArg)
330 {
331 poptItem item = con->os->currAlias;
332 int rc;
333 int i;
334
335 if (item) {
336 if (longName && item->option.longName != NULL
337 && longOptionStrcmp(&item->option, longName, longNameLen))
338 return 0;
339 else
340 if (shortName && shortName == item->option.shortName)
341 return 0;
342 }
343
344 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
345 return 0;
346
347 for (i = con->numAliases - 1; i >= 0; i--) {
348 item = con->aliases + i;
349 if (longName) {
350 if (item->option.longName == NULL)
351 continue;
352 if (!longOptionStrcmp(&item->option, longName, longNameLen))
353 continue;
354 } else if (shortName != item->option.shortName)
355 continue;
356 break;
357 }
358 if (i < 0) return 0;
359
360 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
361 return POPT_ERROR_OPTSTOODEEP;
362
363 if (longName == NULL && nextArg != NULL && *nextArg != '\0')
364 con->os->nextCharArg = nextArg;
365
366 con->os++;
367 con->os->next = 0;
368 con->os->stuffed = 0;
369 con->os->nextArg = NULL;
370 con->os->nextCharArg = NULL;
371 con->os->currAlias = con->aliases + i;
372 { const char ** av;
373 int ac = con->os->currAlias->argc;
374 /* Append --foo=bar arg to alias argv array (if present). */
375 if (longName && nextArg != NULL && *nextArg != '\0') {
376 av = malloc((ac + 1 + 1) * sizeof(*av));
377 if (av != NULL) { /* XXX won't happen. */
378 for (i = 0; i < ac; i++) {
379 av[i] = con->os->currAlias->argv[i];
380 }
381 av[ac++] = nextArg;
382 av[ac] = NULL;
383 } else /* XXX revert to old popt behavior if malloc fails. */
384 av = con->os->currAlias->argv;
385 } else
386 av = con->os->currAlias->argv;
387 rc = poptDupArgv(ac, av, &con->os->argc, &con->os->argv);
388 if (av != NULL && av != con->os->currAlias->argv)
389 free(av);
390 }
391 con->os->argb = NULL;
392
393 return (rc ? rc : 1);
394 }
395
396 /**
397 * Return absolute path to executable by searching PATH.
398 * @param argv0 name of executable
399 * @return (malloc'd) absolute path to executable (or NULL)
400 */
401 static
findProgramPath(const char * argv0)402 const char * findProgramPath(const char * argv0)
403 {
404 char *path = NULL, *s = NULL, *se;
405 char *t = NULL;
406
407 if (argv0 == NULL) return NULL; /* XXX can't happen */
408
409 /* If there is a / in argv[0], it has to be an absolute path. */
410 /* XXX Hmmm, why not if (argv0[0] == '/') ... instead? */
411 if (strchr(argv0, '/'))
412 return xstrdup(argv0);
413
414 if ((path = getenv("PATH")) == NULL || (path = xstrdup(path)) == NULL)
415 return NULL;
416
417 /* The return buffer in t is big enough for any path. */
418 if ((t = malloc(strlen(path) + strlen(argv0) + sizeof("/"))) != NULL)
419 for (s = path; s && *s; s = se) {
420
421 /* Snip PATH element into [s,se). */
422 if ((se = strchr(s, ':')))
423 *se++ = '\0';
424
425 /* Append argv0 to PATH element. */
426 (void) stpcpy(stpcpy(stpcpy(t, s), "/"), argv0);
427
428 /* If file is executable, bingo! */
429 if (!access(t, X_OK))
430 break;
431 }
432
433 /* If no executable was found in PATH, return NULL. */
434 if (!(s && *s) && t != NULL)
435 t = _free(t);
436 path = _free(path);
437
438 return t;
439 }
440
execCommand(poptContext con)441 static int execCommand(poptContext con)
442 {
443 poptItem item = con->doExec;
444 poptArgv argv = NULL;
445 int argc = 0;
446 int rc;
447 int ec = POPT_ERROR_ERRNO;
448
449 if (item == NULL) /*XXX can't happen*/
450 return POPT_ERROR_NOARG;
451
452 if (item->argv == NULL || item->argc < 1 ||
453 (!con->execAbsolute && strchr(item->argv[0], '/')))
454 return POPT_ERROR_NOARG;
455
456 argv = malloc(sizeof(*argv) *
457 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
458 if (argv == NULL) return POPT_ERROR_MALLOC;
459
460 if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
461 char *s = malloc(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
462 if (s)
463 (void)stpcpy(stpcpy(stpcpy(s, con->execPath), "/"), item->argv[0]);
464
465 argv[argc] = s;
466 } else
467 argv[argc] = findProgramPath(item->argv[0]);
468 if (argv[argc++] == NULL) {
469 ec = POPT_ERROR_NOARG;
470 goto exit;
471 }
472
473 if (item->argc > 1) {
474 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
475 argc += (item->argc - 1);
476 }
477
478 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
479 memcpy(argv + argc, con->finalArgv,
480 sizeof(*argv) * con->finalArgvCount);
481 argc += con->finalArgvCount;
482 }
483
484 if (con->leftovers != NULL && con->numLeftovers > 0) {
485 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
486 argc += con->numLeftovers;
487 }
488
489 argv[argc] = NULL;
490
491 #if defined(hpux) || defined(__hpux)
492 rc = setresgid(getgid(), getgid(),-1);
493 if (rc) goto exit;
494 rc = setresuid(getuid(), getuid(),-1);
495 if (rc) goto exit;
496 #else
497 /*
498 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
499 * XXX sez' Timur Bakeyev <mc@bat.ru>
500 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
501 */
502 #if defined(HAVE_SETUID)
503 rc = setgid(getgid());
504 if (rc) goto exit;
505 rc = setuid(getuid());
506 if (rc) goto exit;
507 #elif defined (HAVE_SETREUID)
508 rc = setregid(getgid(), getgid());
509 if (rc) goto exit;
510 rc = setreuid(getuid(), getuid());
511 if (rc) goto exit;
512 #else
513 /* refuse to exec if we cannot drop suid/sgid privileges */
514 if (getuid() != geteuid() || getgid() != getegid()) {
515 errno = ENOTSUP;
516 goto exit;
517 }
518 #endif
519 #endif
520
521 #ifdef MYDEBUG
522 if (_popt_debug)
523 { poptArgv avp;
524 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
525 for (avp = argv; *avp; avp++)
526 fprintf(stderr, " '%s'", *avp);
527 fprintf(stderr, "\n");
528 }
529 #endif
530
531 rc = execvp(argv[0], (char *const *)argv);
532
533 /* only reached on execvp() failure */
534 con->execFail = xstrdup(argv[0]);
535
536 exit:
537 if (argv) {
538 if (argv[0])
539 free((void *)argv[0]);
540 free(argv);
541 }
542 return ec;
543 }
544
545 static const struct poptOption *
findOption(const struct poptOption * opt,const char * longName,size_t longNameLen,char shortName,poptCallbackType * callback,const void ** callbackData,unsigned int argInfo)546 findOption(const struct poptOption * opt,
547 const char * longName, size_t longNameLen,
548 char shortName,
549 poptCallbackType * callback,
550 const void ** callbackData,
551 unsigned int argInfo)
552 {
553 const struct poptOption * cb = NULL;
554 poptArg cbarg = { .ptr = NULL };
555
556 /* This happens when a single - is given */
557 if (LF_ISSET(ONEDASH) && !shortName && (longName && *longName == '\0'))
558 shortName = '-';
559
560 for (; opt->longName || opt->shortName || opt->arg; opt++) {
561 poptArg arg = { .ptr = opt->arg };
562
563 switch (poptArgType(opt)) {
564 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
565 { const struct poptOption * opt2;
566
567 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
568 if (arg.ptr == NULL) continue; /* XXX program error */
569 opt2 = findOption(arg.opt, longName, longNameLen, shortName, callback,
570 callbackData, argInfo);
571 if (opt2 == NULL) continue;
572 /* Sub-table data will be inheirited if no data yet. */
573 if (callback && *callback
574 && callbackData && *callbackData == NULL)
575 *callbackData = opt->descrip;
576 return opt2;
577 } break;
578 case POPT_ARG_CALLBACK:
579 cb = opt;
580 cbarg.ptr = opt->arg;
581 continue;
582 break;
583 default:
584 break;
585 }
586
587 if (longName != NULL && opt->longName != NULL &&
588 (!LF_ISSET(ONEDASH) || F_ISSET(opt, ONEDASH)) &&
589 longOptionStrcmp(opt, longName, longNameLen))
590 {
591 break;
592 } else if (shortName && shortName == opt->shortName) {
593 break;
594 }
595 }
596
597 if (opt->longName == NULL && !opt->shortName)
598 return NULL;
599
600 if (callback)
601 *callback = (cb ? cbarg.cb : NULL);
602 if (callbackData)
603 *callbackData = (cb && !CBF_ISSET(cb, INC_DATA) ? cb->descrip : NULL);
604
605 return opt;
606 }
607
findNextArg(poptContext con,unsigned argx,int delete_arg)608 static const char * findNextArg(poptContext con,
609 unsigned argx, int delete_arg)
610 {
611 struct optionStackEntry * os = con->os;
612 const char * arg;
613
614 do {
615 int i;
616 arg = NULL;
617 while (os->next == os->argc && os > con->optionStack) os--;
618 if (os->next == os->argc && os == con->optionStack) break;
619 if (os->argv != NULL)
620 for (i = os->next; i < os->argc; i++) {
621 if (os->argb && PBM_ISSET(i, os->argb))
622 continue;
623 if (*os->argv[i] == '-')
624 continue;
625 if (--argx > 0)
626 continue;
627 arg = os->argv[i];
628 if (delete_arg) {
629 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
630 if (os->argb != NULL) /* XXX can't happen */
631 PBM_SET(i, os->argb);
632 }
633 break;
634 }
635 if (os > con->optionStack) os--;
636 } while (arg == NULL);
637 return arg;
638 }
639
640 static const char *
expandNextArg(poptContext con,const char * s)641 expandNextArg(poptContext con, const char * s)
642 {
643 const char * a = NULL;
644 char *t, *te;
645 size_t tn = strlen(s) + 1;
646 char c;
647
648 te = t = malloc(tn);
649 if (t == NULL) return NULL; /* XXX can't happen */
650 *t = '\0';
651 while ((c = *s++) != '\0') {
652 switch (c) {
653 #if 0 /* XXX can't do this */
654 case '\\': /* escape */
655 c = *s++;
656 break;
657 #endif
658 case '!':
659 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
660 break;
661 /* XXX Make sure that findNextArg deletes only next arg. */
662 if (a == NULL) {
663 if ((a = findNextArg(con, 1U, 1)) == NULL)
664 break;
665 }
666 s += sizeof("#:+") - 1;
667
668 tn += strlen(a);
669 { size_t pos = (size_t) (te - t);
670 if ((t = realloc(t, tn)) == NULL) /* XXX can't happen */
671 return NULL;
672 te = stpcpy(t + pos, a);
673 }
674 continue;
675 break;
676 default:
677 break;
678 }
679 *te++ = c;
680 }
681 *te++ = '\0';
682 /* If the new string is longer than needed, shorten. */
683 if ((t + tn) > te) {
684 if ((te = realloc(t, (size_t)(te - t))) == NULL)
685 free(t);
686 t = te;
687 }
688 return t;
689 }
690
poptStripArg(poptContext con,int which)691 static void poptStripArg(poptContext con, int which)
692 {
693 if (con->arg_strip == NULL)
694 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
695 if (con->arg_strip != NULL) /* XXX can't happen */
696 PBM_SET(which, con->arg_strip);
697 return;
698 }
699
700 unsigned int _poptBitsN = _POPT_BITS_N;
701 unsigned int _poptBitsM = _POPT_BITS_M;
702 unsigned int _poptBitsK = _POPT_BITS_K;
703
_poptBitsNew(poptBits * bitsp)704 static int _poptBitsNew(poptBits *bitsp)
705 {
706 if (bitsp == NULL)
707 return POPT_ERROR_NULLARG;
708
709 /* XXX handle negated initialization. */
710 if (*bitsp == NULL) {
711 if (_poptBitsN == 0) {
712 _poptBitsN = _POPT_BITS_N;
713 _poptBitsM = _POPT_BITS_M;
714 }
715 if (_poptBitsM == 0U) _poptBitsM = (3 * _poptBitsN) / 2;
716 if (_poptBitsK == 0U || _poptBitsK > 32U) _poptBitsK = _POPT_BITS_K;
717 *bitsp = PBM_ALLOC(_poptBitsM-1);
718 }
719 return 0;
720 }
721
poptBitsAdd(poptBits bits,const char * s)722 int poptBitsAdd(poptBits bits, const char * s)
723 {
724 size_t ns = (s ? strlen(s) : 0);
725 uint32_t h0 = 0;
726 uint32_t h1 = 0;
727
728 if (bits == NULL || ns == 0)
729 return POPT_ERROR_NULLARG;
730
731 poptJlu32lpair(s, ns, &h0, &h1);
732
733 for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
734 uint32_t h = h0 + ns * h1;
735 uint32_t ix = (h % _poptBitsM);
736 PBM_SET(ix, bits);
737 }
738 return 0;
739 }
740
poptBitsChk(poptBits bits,const char * s)741 int poptBitsChk(poptBits bits, const char * s)
742 {
743 size_t ns = (s ? strlen(s) : 0);
744 uint32_t h0 = 0;
745 uint32_t h1 = 0;
746 int rc = 1;
747
748 if (bits == NULL || ns == 0)
749 return POPT_ERROR_NULLARG;
750
751 poptJlu32lpair(s, ns, &h0, &h1);
752
753 for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
754 uint32_t h = h0 + ns * h1;
755 uint32_t ix = (h % _poptBitsM);
756 if (PBM_ISSET(ix, bits))
757 continue;
758 rc = 0;
759 break;
760 }
761 return rc;
762 }
763
poptBitsClr(poptBits bits)764 int poptBitsClr(poptBits bits)
765 {
766 static size_t nbw = (__PBM_NBITS/8);
767 size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
768
769 if (bits == NULL)
770 return POPT_ERROR_NULLARG;
771 memset(bits, 0, nw * nbw);
772 return 0;
773 }
774
poptBitsDel(poptBits bits,const char * s)775 int poptBitsDel(poptBits bits, const char * s)
776 {
777 size_t ns = (s ? strlen(s) : 0);
778 uint32_t h0 = 0;
779 uint32_t h1 = 0;
780
781 if (bits == NULL || ns == 0)
782 return POPT_ERROR_NULLARG;
783
784 poptJlu32lpair(s, ns, &h0, &h1);
785
786 for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
787 uint32_t h = h0 + ns * h1;
788 uint32_t ix = (h % _poptBitsM);
789 PBM_CLR(ix, bits);
790 }
791 return 0;
792 }
793
poptBitsIntersect(poptBits * ap,const poptBits b)794 int poptBitsIntersect(poptBits *ap, const poptBits b)
795 {
796 __pbm_bits *abits;
797 __pbm_bits *bbits;
798 __pbm_bits rc = 0;
799 size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
800 size_t i;
801
802 if (ap == NULL || b == NULL || _poptBitsNew(ap))
803 return POPT_ERROR_NULLARG;
804 abits = __PBM_BITS(*ap);
805 bbits = __PBM_BITS(b);
806
807 for (i = 0; i < nw; i++) {
808 abits[i] &= bbits[i];
809 rc |= abits[i];
810 }
811 return (rc ? 1 : 0);
812 }
813
poptBitsUnion(poptBits * ap,const poptBits b)814 int poptBitsUnion(poptBits *ap, const poptBits b)
815 {
816 __pbm_bits *abits;
817 __pbm_bits *bbits;
818 __pbm_bits rc = 0;
819 size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
820 size_t i;
821
822 if (ap == NULL || b == NULL || _poptBitsNew(ap))
823 return POPT_ERROR_NULLARG;
824 abits = __PBM_BITS(*ap);
825 bbits = __PBM_BITS(b);
826
827 for (i = 0; i < nw; i++) {
828 abits[i] |= bbits[i];
829 rc |= abits[i];
830 }
831 return (rc ? 1 : 0);
832 }
833
poptBitsArgs(poptContext con,poptBits * ap)834 int poptBitsArgs(poptContext con, poptBits *ap)
835 {
836 const char ** av;
837 int rc = 0;
838
839 if (con == NULL || ap == NULL || _poptBitsNew(ap) ||
840 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
841 return POPT_ERROR_NULLARG;
842
843 /* some apps like [like RPM ;-) ] need this NULL terminated */
844 con->leftovers[con->numLeftovers] = NULL;
845
846 for (av = con->leftovers + con->nextLeftover; *av != NULL; av++) {
847 if ((rc = poptBitsAdd(*ap, *av)) != 0)
848 break;
849 }
850 return rc;
851 }
852
poptSaveBits(poptBits * bitsp,UNUSED (unsigned int argInfo),const char * s)853 int poptSaveBits(poptBits * bitsp,
854 UNUSED(unsigned int argInfo), const char * s)
855 {
856 char *tbuf = NULL;
857 char *t, *te;
858 int rc = 0;
859
860 if (bitsp == NULL || s == NULL || *s == '\0' || _poptBitsNew(bitsp))
861 return POPT_ERROR_NULLARG;
862
863 /* Parse comma separated attributes. */
864 te = tbuf = xstrdup(s);
865 while ((t = te) != NULL && *t) {
866 while (*te != '\0' && *te != ',')
867 te++;
868 if (*te != '\0')
869 *te++ = '\0';
870 /* XXX Ignore empty strings. */
871 if (*t == '\0')
872 continue;
873 /* XXX Permit negated attributes. caveat emptor: false negatives. */
874 if (*t == '!') {
875 t++;
876 if ((rc = poptBitsChk(*bitsp, t)) > 0)
877 rc = poptBitsDel(*bitsp, t);
878 } else
879 rc = poptBitsAdd(*bitsp, t);
880 if (rc)
881 break;
882 }
883 tbuf = _free(tbuf);
884 return rc;
885 }
886
poptSaveString(const char *** argvp,UNUSED (unsigned int argInfo),const char * val)887 int poptSaveString(const char *** argvp,
888 UNUSED(unsigned int argInfo), const char * val)
889 {
890 int argc = 0;
891
892 if (argvp == NULL || val == NULL)
893 return POPT_ERROR_NULLARG;
894
895 /* XXX likely needs an upper bound on argc. */
896 if (*argvp != NULL)
897 while ((*argvp)[argc] != NULL)
898 argc++;
899
900 if ((*argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp))) != NULL) {
901 (*argvp)[argc++] = xstrdup(val);
902 (*argvp)[argc ] = NULL;
903 }
904 return 0;
905 }
906
907 static unsigned int seed = 0;
908
poptSaveLongLong(long long * arg,unsigned int argInfo,long long aLongLong)909 int poptSaveLongLong(long long * arg, unsigned int argInfo, long long aLongLong)
910 {
911 /* XXX Check alignment, may fail on funky platforms. */
912 if (arg == NULL || (((unsigned long)arg) & (ALIGNOF(*arg)-1)))
913 return POPT_ERROR_NULLARG;
914
915 if (aLongLong != 0 && LF_ISSET(RANDOM)) {
916 #if defined(HAVE_SRANDOM)
917 if (!seed) {
918 srandom((unsigned)getpid());
919 srandom((unsigned)random());
920 }
921 aLongLong = (long long)(random() % (aLongLong > 0 ? aLongLong : -aLongLong));
922 aLongLong++;
923 #else
924 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
925 return POPT_ERROR_BADOPERATION;
926 #endif
927 }
928 if (LF_ISSET(NOT))
929 aLongLong = ~aLongLong;
930 switch (LF_ISSET(LOGICALOPS)) {
931 case 0:
932 *arg = aLongLong;
933 break;
934 case POPT_ARGFLAG_OR:
935 *(unsigned long long *)arg |= (unsigned long long)aLongLong;
936 break;
937 case POPT_ARGFLAG_AND:
938 *(unsigned long long *)arg &= (unsigned long long)aLongLong;
939 break;
940 case POPT_ARGFLAG_XOR:
941 *(unsigned long long *)arg ^= (unsigned long long)aLongLong;
942 break;
943 default:
944 return POPT_ERROR_BADOPERATION;
945 break;
946 }
947 return 0;
948 }
949
poptSaveLong(long * arg,unsigned int argInfo,long aLong)950 int poptSaveLong(long * arg, unsigned int argInfo, long aLong)
951 {
952 /* XXX Check alignment, may fail on funky platforms. */
953 if (arg == NULL || (((unsigned long)arg) & (ALIGNOF(*arg)-1)))
954 return POPT_ERROR_NULLARG;
955
956 if (aLong != 0 && LF_ISSET(RANDOM)) {
957 #if defined(HAVE_SRANDOM)
958 if (!seed) {
959 srandom((unsigned)getpid());
960 srandom((unsigned)random());
961 }
962 aLong = random() % (aLong > 0 ? aLong : -aLong);
963 aLong++;
964 #else
965 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
966 return POPT_ERROR_BADOPERATION;
967 #endif
968 }
969 if (LF_ISSET(NOT))
970 aLong = ~aLong;
971 switch (LF_ISSET(LOGICALOPS)) {
972 case 0: *arg = aLong; break;
973 case POPT_ARGFLAG_OR: *(unsigned long *)arg |= (unsigned long)aLong; break;
974 case POPT_ARGFLAG_AND: *(unsigned long *)arg &= (unsigned long)aLong; break;
975 case POPT_ARGFLAG_XOR: *(unsigned long *)arg ^= (unsigned long)aLong; break;
976 default:
977 return POPT_ERROR_BADOPERATION;
978 break;
979 }
980 return 0;
981 }
982
poptSaveInt(int * arg,unsigned int argInfo,long aLong)983 int poptSaveInt(int * arg, unsigned int argInfo, long aLong)
984 {
985 /* XXX Check alignment, may fail on funky platforms. */
986 if (arg == NULL || (((unsigned long)arg) & (ALIGNOF(*arg)-1)))
987 return POPT_ERROR_NULLARG;
988
989 if (aLong != 0 && LF_ISSET(RANDOM)) {
990 #if defined(HAVE_SRANDOM)
991 if (!seed) {
992 srandom((unsigned)getpid());
993 srandom((unsigned)random());
994 }
995 aLong = random() % (aLong > 0 ? aLong : -aLong);
996 aLong++;
997 #else
998 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
999 return POPT_ERROR_BADOPERATION;
1000 #endif
1001 }
1002 if (LF_ISSET(NOT))
1003 aLong = ~aLong;
1004 switch (LF_ISSET(LOGICALOPS)) {
1005 case 0: *arg = (int) aLong; break;
1006 case POPT_ARGFLAG_OR: *(unsigned int *)arg |= (unsigned int) aLong; break;
1007 case POPT_ARGFLAG_AND: *(unsigned int *)arg &= (unsigned int) aLong; break;
1008 case POPT_ARGFLAG_XOR: *(unsigned int *)arg ^= (unsigned int) aLong; break;
1009 default:
1010 return POPT_ERROR_BADOPERATION;
1011 break;
1012 }
1013 return 0;
1014 }
1015
poptSaveShort(short * arg,unsigned int argInfo,long aLong)1016 int poptSaveShort(short * arg, unsigned int argInfo, long aLong)
1017 {
1018 /* XXX Check alignment, may fail on funky platforms. */
1019 if (arg == NULL || (((unsigned long)arg) & (ALIGNOF(*arg)-1)))
1020 return POPT_ERROR_NULLARG;
1021
1022 if (aLong != 0 && LF_ISSET(RANDOM)) {
1023 #if defined(HAVE_SRANDOM)
1024 if (!seed) {
1025 srandom((unsigned)getpid());
1026 srandom((unsigned)random());
1027 }
1028 aLong = random() % (aLong > 0 ? aLong : -aLong);
1029 aLong++;
1030 #else
1031 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1032 return POPT_ERROR_BADOPERATION;
1033 #endif
1034 }
1035 if (LF_ISSET(NOT))
1036 aLong = ~aLong;
1037 switch (LF_ISSET(LOGICALOPS)) {
1038 case 0: *arg = (short) aLong;
1039 break;
1040 case POPT_ARGFLAG_OR: *(unsigned short *)arg |= (unsigned short) aLong;
1041 break;
1042 case POPT_ARGFLAG_AND: *(unsigned short *)arg &= (unsigned short) aLong;
1043 break;
1044 case POPT_ARGFLAG_XOR: *(unsigned short *)arg ^= (unsigned short) aLong;
1045 break;
1046 default: return POPT_ERROR_BADOPERATION;
1047 break;
1048 }
1049 return 0;
1050 }
1051
1052 /**
1053 * Return argInfo field, handling POPT_ARGFLAG_TOGGLE overrides.
1054 * @param con context
1055 * @param opt option
1056 * @return argInfo
1057 */
poptArgInfo(poptContext con,const struct poptOption * opt)1058 static unsigned int poptArgInfo(poptContext con, const struct poptOption * opt)
1059 {
1060 unsigned int argInfo = opt->argInfo;
1061
1062 if (con->os->argv != NULL && con->os->next > 0 && opt->longName != NULL)
1063 if (LF_ISSET(TOGGLE)) {
1064 const char * longName = con->os->argv[con->os->next-1];
1065 while (*longName == '-') longName++;
1066 /* XXX almost good enough but consider --[no]nofoo corner cases. */
1067 if (longName[0] != opt->longName[0] || longName[1] != opt->longName[1])
1068 {
1069 if (!LF_ISSET(XOR)) { /* XXX dont toggle with XOR */
1070 /* Toggle POPT_BIT_SET <=> POPT_BIT_CLR. */
1071 if (LF_ISSET(LOGICALOPS))
1072 argInfo ^= (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND);
1073 argInfo ^= POPT_ARGFLAG_NOT;
1074 }
1075 }
1076 }
1077 return argInfo;
1078 }
1079
1080 /**
1081 * Parse an integer expression.
1082 * @retval *llp integer expression value
1083 * @param argInfo integer expression type
1084 * @param val integer expression string
1085 * @return 0 on success, otherwise POPT_* error.
1086 */
poptParseInteger(long long * llp,UNUSED (unsigned int argInfo),const char * val)1087 static int poptParseInteger(long long * llp,
1088 UNUSED(unsigned int argInfo),
1089 const char * val)
1090 {
1091 if (val) {
1092 char *end = NULL;
1093 *llp = strtoll(val, &end, 0);
1094
1095 /* XXX parse scaling suffixes here. */
1096
1097 if (!(end && *end == '\0'))
1098 return POPT_ERROR_BADNUMBER;
1099 } else
1100 *llp = 0;
1101 return 0;
1102 }
1103
1104 /**
1105 * Save the option argument through the (*opt->arg) pointer.
1106 * @param con context
1107 * @param opt option
1108 * @return 0 on success, otherwise POPT_* error.
1109 */
poptSaveArg(poptContext con,const struct poptOption * opt)1110 static int poptSaveArg(poptContext con, const struct poptOption * opt)
1111 {
1112 poptArg arg = { .ptr = opt->arg };
1113 int rc = 0; /* assume success */
1114
1115 switch (poptArgType(opt)) {
1116 case POPT_ARG_BITSET:
1117 /* XXX memory leak, application is responsible for free. */
1118 rc = poptSaveBits(arg.ptr, opt->argInfo, con->os->nextArg);
1119 break;
1120 case POPT_ARG_ARGV:
1121 /* XXX memory leak, application is responsible for free. */
1122 rc = poptSaveString(arg.ptr, opt->argInfo, con->os->nextArg);
1123 break;
1124 case POPT_ARG_STRING:
1125 /* XXX memory leak, application is responsible for free. */
1126 arg.argv[0] = (con->os->nextArg) ? xstrdup(con->os->nextArg) : NULL;
1127 break;
1128
1129 case POPT_ARG_INT:
1130 case POPT_ARG_SHORT:
1131 case POPT_ARG_LONG:
1132 case POPT_ARG_LONGLONG:
1133 { unsigned int argInfo = poptArgInfo(con, opt);
1134 long long aNUM = 0;
1135
1136 if ((rc = poptParseInteger(&aNUM, argInfo, con->os->nextArg)) != 0)
1137 break;
1138
1139 switch (poptArgType(opt)) {
1140 case POPT_ARG_LONGLONG:
1141 /* XXX let's not demand C99 compiler flags for <limits.h> quite yet. */
1142 #if !defined(LLONG_MAX)
1143 # define LLONG_MAX 9223372036854775807LL
1144 # define LLONG_MIN (-LLONG_MAX - 1LL)
1145 #endif
1146 rc = !(aNUM == LLONG_MIN || aNUM == LLONG_MAX)
1147 ? poptSaveLongLong(arg.longlongp, argInfo, aNUM)
1148 : POPT_ERROR_OVERFLOW;
1149 break;
1150 case POPT_ARG_LONG:
1151 rc = !(aNUM < (long long)LONG_MIN || aNUM > (long long)LONG_MAX)
1152 ? poptSaveLong(arg.longp, argInfo, (long)aNUM)
1153 : POPT_ERROR_OVERFLOW;
1154 break;
1155 case POPT_ARG_INT:
1156 rc = !(aNUM < (long long)INT_MIN || aNUM > (long long)INT_MAX)
1157 ? poptSaveInt(arg.intp, argInfo, (long)aNUM)
1158 : POPT_ERROR_OVERFLOW;
1159 break;
1160 case POPT_ARG_SHORT:
1161 rc = !(aNUM < (long long)SHRT_MIN || aNUM > (long long)SHRT_MAX)
1162 ? poptSaveShort(arg.shortp, argInfo, (long)aNUM)
1163 : POPT_ERROR_OVERFLOW;
1164 break;
1165 }
1166 } break;
1167
1168 case POPT_ARG_FLOAT:
1169 case POPT_ARG_DOUBLE:
1170 { char *end = NULL;
1171 double aDouble = 0.0;
1172
1173 if (con->os->nextArg) {
1174 int saveerrno = errno;
1175 errno = 0;
1176 aDouble = strtod(con->os->nextArg, &end);
1177 if (errno == ERANGE) {
1178 rc = POPT_ERROR_OVERFLOW;
1179 break;
1180 }
1181 errno = saveerrno;
1182 if (*end != '\0') {
1183 rc = POPT_ERROR_BADNUMBER;
1184 break;
1185 }
1186 }
1187
1188 switch (poptArgType(opt)) {
1189 case POPT_ARG_DOUBLE:
1190 arg.doublep[0] = aDouble;
1191 break;
1192 case POPT_ARG_FLOAT:
1193 #define POPT_ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
1194 if ((FLT_MIN - POPT_ABS(aDouble)) > DBL_EPSILON
1195 || (POPT_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
1196 rc = POPT_ERROR_OVERFLOW;
1197 else
1198 arg.floatp[0] = (float) aDouble;
1199 break;
1200 }
1201 } break;
1202 case POPT_ARG_MAINCALL:
1203 con->maincall = opt->arg;
1204 break;
1205 default:
1206 fprintf(stdout, POPT_("option type (%u) not implemented in popt\n"),
1207 poptArgType(opt));
1208 exit(EXIT_FAILURE);
1209 break;
1210 }
1211 return rc;
1212 }
1213
1214 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
poptGetNextOpt(poptContext con)1215 int poptGetNextOpt(poptContext con)
1216 {
1217 const struct poptOption * opt = NULL;
1218 int done = 0;
1219
1220 if (con == NULL)
1221 return -1;
1222 while (!done) {
1223 const char * origOptString = NULL;
1224 poptCallbackType cb = NULL;
1225 const void * cbData = NULL;
1226 const char * longArg = NULL;
1227 int canstrip = 0;
1228 int shorty = 0;
1229
1230 while (!con->os->nextCharArg && con->os->next == con->os->argc
1231 && con->os > con->optionStack) {
1232 cleanOSE(con->os--);
1233 }
1234 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
1235 invokeCallbacksPOST(con, con->options);
1236
1237 if (con->maincall) {
1238 (void) (*con->maincall) (con->finalArgvCount, con->finalArgv);
1239 return -1;
1240 }
1241
1242 if (con->doExec) return execCommand(con);
1243 return -1;
1244 }
1245
1246 /* Process next long option */
1247 if (!con->os->nextCharArg) {
1248 const char * optString;
1249 size_t optStringLen;
1250 int thisopt;
1251
1252 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
1253 con->os->next++;
1254 continue;
1255 }
1256 thisopt = con->os->next;
1257 if (con->os->argv != NULL) /* XXX can't happen */
1258 origOptString = con->os->argv[con->os->next++];
1259
1260 if (origOptString == NULL) /* XXX can't happen */
1261 return POPT_ERROR_BADOPT;
1262
1263 if (con->restLeftover || *origOptString != '-' ||
1264 (*origOptString == '-' && origOptString[1] == '\0'))
1265 {
1266 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
1267 con->restLeftover = 1;
1268 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
1269 con->os->nextArg = xstrdup(origOptString);
1270 return 0;
1271 }
1272 if (con->leftovers != NULL) /* XXX can't happen */
1273 con->leftovers[con->numLeftovers++] = origOptString;
1274 continue;
1275 }
1276
1277 /* Make a copy we can hack at */
1278 optString = origOptString;
1279
1280 if (optString[0] == '\0')
1281 return POPT_ERROR_BADOPT;
1282
1283 if (optString[1] == '-' && !optString[2]) {
1284 con->restLeftover = 1;
1285 continue;
1286 } else {
1287 const char *oe;
1288 unsigned int argInfo = 0;
1289
1290 optString++;
1291 if (*optString == '-')
1292 optString++;
1293 else
1294 argInfo |= POPT_ARGFLAG_ONEDASH;
1295
1296 /* Check for "--long=arg" option. */
1297 for (oe = optString; *oe && *oe != '='; oe++)
1298 {};
1299 optStringLen = (size_t)(oe - optString);
1300 if (*oe == '=')
1301 longArg = oe + 1;
1302
1303 /* XXX aliases with arg substitution need "--alias=arg" */
1304 if (handleAlias(con, optString, optStringLen, '\0', longArg)) {
1305 longArg = NULL;
1306 continue;
1307 }
1308
1309 if (handleExec(con, optString, '\0'))
1310 continue;
1311
1312 opt = findOption(con->options, optString, optStringLen, '\0', &cb, &cbData,
1313 argInfo);
1314 if (!opt && !LF_ISSET(ONEDASH))
1315 return POPT_ERROR_BADOPT;
1316 }
1317
1318 if (!opt) {
1319 con->os->nextCharArg = origOptString + 1;
1320 longArg = NULL;
1321 } else {
1322 if (con->os == con->optionStack && F_ISSET(opt, STRIP))
1323 {
1324 canstrip = 1;
1325 poptStripArg(con, thisopt);
1326 }
1327 shorty = 0;
1328 }
1329 }
1330
1331 /* Process next short option */
1332 if (con->os->nextCharArg) {
1333 const char * nextCharArg = con->os->nextCharArg;
1334
1335 con->os->nextCharArg = NULL;
1336
1337 if (handleAlias(con, NULL, 0, *nextCharArg, nextCharArg + 1))
1338 continue;
1339
1340 if (handleExec(con, NULL, *nextCharArg)) {
1341 /* Restore rest of short options for further processing */
1342 nextCharArg++;
1343 if (*nextCharArg != '\0')
1344 con->os->nextCharArg = nextCharArg;
1345 continue;
1346 }
1347
1348 opt = findOption(con->options, NULL, 0, *nextCharArg, &cb,
1349 &cbData, 0);
1350 if (!opt)
1351 return POPT_ERROR_BADOPT;
1352 shorty = 1;
1353
1354 nextCharArg++;
1355 if (*nextCharArg != '\0')
1356 con->os->nextCharArg = nextCharArg + (int)(*nextCharArg == '=');
1357 }
1358
1359 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
1360 if (opt->arg && poptArgType(opt) == POPT_ARG_NONE) {
1361 unsigned int argInfo = poptArgInfo(con, opt);
1362 if (poptSaveInt((int *)opt->arg, argInfo, 1L))
1363 return POPT_ERROR_BADOPERATION;
1364 } else if (poptArgType(opt) == POPT_ARG_VAL) {
1365 if (opt->arg) {
1366 unsigned int argInfo = poptArgInfo(con, opt);
1367 if (poptSaveInt((int *)opt->arg, argInfo, (long)opt->val))
1368 return POPT_ERROR_BADOPERATION;
1369 }
1370 } else if (poptArgType(opt) != POPT_ARG_NONE) {
1371 int rc;
1372
1373 con->os->nextArg = _free(con->os->nextArg);
1374 if (longArg) {
1375 longArg = expandNextArg(con, longArg);
1376 con->os->nextArg = (char *) longArg;
1377 } else if (con->os->nextCharArg) {
1378 longArg = expandNextArg(con, con->os->nextCharArg);
1379 con->os->nextArg = (char *) longArg;
1380 con->os->nextCharArg = NULL;
1381 } else {
1382 while (con->os->next == con->os->argc &&
1383 con->os > con->optionStack)
1384 {
1385 cleanOSE(con->os--);
1386 }
1387 if (con->os->next == con->os->argc) {
1388 if (!F_ISSET(opt, OPTIONAL))
1389 return POPT_ERROR_NOARG;
1390 con->os->nextArg = NULL;
1391 } else {
1392
1393 /*
1394 * Make sure this isn't part of a short arg or the
1395 * result of an alias expansion.
1396 */
1397 if (con->os == con->optionStack
1398 && F_ISSET(opt, STRIP) && canstrip)
1399 {
1400 poptStripArg(con, con->os->next);
1401 }
1402
1403 if (con->os->argv != NULL) { /* XXX can't happen */
1404 if (F_ISSET(opt, OPTIONAL) &&
1405 con->os->argv[con->os->next][0] == '-') {
1406 con->os->nextArg = NULL;
1407 } else {
1408 /* XXX watchout: subtle side-effects live here. */
1409 longArg = con->os->argv[con->os->next++];
1410 longArg = expandNextArg(con, longArg);
1411 con->os->nextArg = (char *) longArg;
1412 }
1413 }
1414 }
1415 }
1416 longArg = NULL;
1417
1418 /* Save the option argument through a (*opt->arg) pointer. */
1419 if (opt->arg != NULL && (rc = poptSaveArg(con, opt)) != 0)
1420 return rc;
1421 }
1422
1423 if (cb)
1424 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
1425 else if (opt->val && (poptArgType(opt) != POPT_ARG_VAL))
1426 done = 1;
1427
1428 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
1429 con->finalArgvAlloced += 10;
1430 con->finalArgv = realloc(con->finalArgv,
1431 sizeof(*con->finalArgv) * con->finalArgvAlloced);
1432 }
1433
1434 if (con->finalArgv != NULL)
1435 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + sizeof("--"));
1436 if (s != NULL) { /* XXX can't happen */
1437 con->finalArgv[con->finalArgvCount++] = s;
1438 *s++ = '-';
1439 if (opt->longName) {
1440 if (!F_ISSET(opt, ONEDASH))
1441 *s++ = '-';
1442 s = stpcpy(s, opt->longName);
1443 } else {
1444 *s++ = opt->shortName;
1445 *s = '\0';
1446 }
1447 } else
1448 con->finalArgv[con->finalArgvCount++] = NULL;
1449 }
1450
1451 if (opt->arg && poptArgType(opt) == POPT_ARG_NONE)
1452 ;
1453 else if (poptArgType(opt) == POPT_ARG_VAL)
1454 ;
1455 else if (poptArgType(opt) != POPT_ARG_NONE) {
1456 if (con->finalArgv != NULL && con->os->nextArg != NULL)
1457 con->finalArgv[con->finalArgvCount++] =
1458 xstrdup(con->os->nextArg);
1459 }
1460 }
1461
1462 return (opt ? opt->val : -1); /* XXX can't happen */
1463 }
1464
poptGetOptArg(poptContext con)1465 char * poptGetOptArg(poptContext con)
1466 {
1467 char * ret = NULL;
1468 if (con) {
1469 ret = con->os->nextArg;
1470 con->os->nextArg = NULL;
1471 }
1472 return ret;
1473 }
1474
poptGetArg(poptContext con)1475 const char * poptGetArg(poptContext con)
1476 {
1477 const char * ret = NULL;
1478 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1479 ret = con->leftovers[con->nextLeftover++];
1480 return ret;
1481 }
1482
poptPeekArg(poptContext con)1483 const char * poptPeekArg(poptContext con)
1484 {
1485 const char * ret = NULL;
1486 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1487 ret = con->leftovers[con->nextLeftover];
1488 return ret;
1489 }
1490
poptGetArgs(poptContext con)1491 const char ** poptGetArgs(poptContext con)
1492 {
1493 if (con == NULL ||
1494 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1495 return NULL;
1496
1497 /* some apps like [like RPM ;-) ] need this NULL terminated */
1498 con->leftovers[con->numLeftovers] = NULL;
1499
1500 return (con->leftovers + con->nextLeftover);
1501 }
1502
1503 static
poptFreeItems(poptItem items,int nitems)1504 poptItem poptFreeItems(poptItem items, int nitems)
1505 {
1506 if (items != NULL) {
1507 poptItem item = items;
1508 while (--nitems >= 0) {
1509 item->option.longName = _free(item->option.longName);
1510 item->option.descrip = _free(item->option.descrip);
1511 item->option.argDescrip = _free(item->option.argDescrip);
1512 item->argv = _free(item->argv);
1513 item++;
1514 }
1515 items = _free(items);
1516 }
1517 return NULL;
1518 }
1519
poptFreeContext(poptContext con)1520 poptContext poptFreeContext(poptContext con)
1521 {
1522 if (con == NULL) return con;
1523 poptResetContext(con);
1524
1525 con->aliases = poptFreeItems(con->aliases, con->numAliases);
1526 con->numAliases = 0;
1527
1528 con->execs = poptFreeItems(con->execs, con->numExecs);
1529 con->numExecs = 0;
1530
1531 con->leftovers = _free(con->leftovers);
1532 con->finalArgv = _free(con->finalArgv);
1533 con->appName = _free(con->appName);
1534 con->otherHelp = _free(con->otherHelp);
1535 con->execPath = _free(con->execPath);
1536 con->arg_strip = PBM_FREE(con->arg_strip);
1537
1538 con = _free(con);
1539 return con;
1540 }
1541
poptAddAlias(poptContext con,struct poptAlias alias,UNUSED (int flags))1542 int poptAddAlias(poptContext con, struct poptAlias alias,
1543 UNUSED(int flags))
1544 {
1545 struct poptItem_s item_buf;
1546 poptItem item = &item_buf;
1547 memset(item, 0, sizeof(*item));
1548 item->option.longName = alias.longName;
1549 item->option.shortName = alias.shortName;
1550 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1551 item->option.arg = 0;
1552 item->option.val = 0;
1553 item->option.descrip = NULL;
1554 item->option.argDescrip = NULL;
1555 item->argc = alias.argc;
1556 item->argv = alias.argv;
1557 return poptAddItem(con, item, 0);
1558 }
1559
poptAddItem(poptContext con,poptItem newItem,int flags)1560 int poptAddItem(poptContext con, poptItem newItem, int flags)
1561 {
1562 poptItem * items, item;
1563 int * nitems;
1564
1565 switch (flags) {
1566 case 1:
1567 items = &con->execs;
1568 nitems = &con->numExecs;
1569 break;
1570 case 0:
1571 items = &con->aliases;
1572 nitems = &con->numAliases;
1573 break;
1574 default:
1575 return 1;
1576 break;
1577 }
1578
1579 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1580 if ((*items) == NULL)
1581 return 1;
1582
1583 item = (*items) + (*nitems);
1584
1585 item->option.longName =
1586 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1587 item->option.shortName = newItem->option.shortName;
1588 item->option.argInfo = newItem->option.argInfo;
1589 item->option.arg = newItem->option.arg;
1590 item->option.val = newItem->option.val;
1591 item->option.descrip =
1592 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1593 item->option.argDescrip =
1594 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1595 item->argc = newItem->argc;
1596 item->argv = newItem->argv;
1597
1598 (*nitems)++;
1599
1600 return 0;
1601 }
1602
poptBadOption(poptContext con,unsigned int flags)1603 const char * poptBadOption(poptContext con, unsigned int flags)
1604 {
1605 struct optionStackEntry * os = NULL;
1606 const char *badOpt = NULL;
1607
1608 if (con != NULL) {
1609 /* Stupid hack to return something semi-meaningful from exec failure */
1610 if (con->execFail) {
1611 badOpt = con->execFail;
1612 } else {
1613 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1614 badOpt = os->argv[os->next - 1];
1615 }
1616 }
1617
1618 return badOpt;
1619 }
1620
poptStrerror(const int error)1621 const char * poptStrerror(const int error)
1622 {
1623 switch (error) {
1624 case POPT_ERROR_NOARG:
1625 return POPT_("missing argument");
1626 case POPT_ERROR_BADOPT:
1627 return POPT_("unknown option");
1628 case POPT_ERROR_BADOPERATION:
1629 return POPT_("mutually exclusive logical operations requested");
1630 case POPT_ERROR_NULLARG:
1631 return POPT_("opt->arg should not be NULL");
1632 case POPT_ERROR_OPTSTOODEEP:
1633 return POPT_("aliases nested too deeply");
1634 case POPT_ERROR_BADQUOTE:
1635 return POPT_("error in parameter quoting");
1636 case POPT_ERROR_BADNUMBER:
1637 return POPT_("invalid numeric value");
1638 case POPT_ERROR_OVERFLOW:
1639 return POPT_("number too large or too small");
1640 case POPT_ERROR_MALLOC:
1641 return POPT_("memory allocation failed");
1642 case POPT_ERROR_BADCONFIG:
1643 return POPT_("config file failed sanity test");
1644 case POPT_ERROR_ERRNO:
1645 return strerror(errno);
1646 default:
1647 return POPT_("unknown error");
1648 }
1649 }
1650
poptStuffArgs(poptContext con,const char ** argv)1651 int poptStuffArgs(poptContext con, const char ** argv)
1652 {
1653 int argc;
1654 int rc;
1655
1656 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1657 return POPT_ERROR_OPTSTOODEEP;
1658
1659 for (argc = 0; argv[argc]; argc++)
1660 {};
1661
1662 con->os++;
1663 con->os->next = 0;
1664 con->os->nextArg = NULL;
1665 con->os->nextCharArg = NULL;
1666 con->os->currAlias = NULL;
1667 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1668 con->os->argb = NULL;
1669 con->os->stuffed = 1;
1670
1671 return rc;
1672 }
1673
poptGetInvocationName(poptContext con)1674 const char * poptGetInvocationName(poptContext con)
1675 {
1676 return (con->os->argv ? con->os->argv[0] : "");
1677 }
1678
poptStrippedArgv(poptContext con,int argc,char ** argv)1679 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1680 {
1681 int numargs = argc;
1682 int j = 1;
1683 int i;
1684
1685 if (con->arg_strip)
1686 for (i = 1; i < argc; i++) {
1687 if (PBM_ISSET(i, con->arg_strip))
1688 numargs--;
1689 }
1690
1691 for (i = 1; i < argc; i++) {
1692 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1693 continue;
1694 argv[j] = (j < numargs) ? argv[i] : NULL;
1695 j++;
1696 }
1697
1698 return numargs;
1699 }
1700