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