blob: c6672b7ad0381f0cb04a3d3433349a465a977871 [file] [log] [blame]
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001/** \ingroup popt
2 * \file popt/popt.c
3 */
4
5/* (C) 19982000 Red Hat, Inc. -- Licensing details are in the COPYING
Martin Poolb348dea2001-02-24 01:32:22 +00006 file accompanying popt source distributions, available from
Wayne Davisoncc248aa2002-07-27 18:32:25 +00007 ftp://ftp.rpm.org/pub/rpm/dist */
8
9#undef MYDEBUG
Martin Poolb348dea2001-02-24 01:32:22 +000010
11#include "system.h"
Wayne Davisoncc248aa2002-07-27 18:32:25 +000012
13#if HAVE_FLOAT_H
14#include <float.h>
15#endif
16#include <math.h>
17
Martin Poolb348dea2001-02-24 01:32:22 +000018#include "findme.h"
19#include "poptint.h"
20
21#ifndef HAVE_STRERROR
22static char * strerror(int errno) {
23 extern int sys_nerr;
24 extern char * sys_errlist[];
25
26 if ((0 <= errno) && (errno < sys_nerr))
27 return sys_errlist[errno];
28 else
29 return POPT_("unknown errno");
30}
31#endif
32
Wayne Davisoncc248aa2002-07-27 18:32:25 +000033#ifdef MYDEBUG
34/*@unused@*/ static void prtcon(const char *msg, poptContext con)
35{
36 if (msg) fprintf(stderr, "%s", msg);
37 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
38 con, con->os,
39 (con->os->nextCharArg ? con->os->nextCharArg : ""),
40 (con->os->nextArg ? con->os->nextArg : ""),
41 con->os->next,
42 (con->os->argv && con->os->argv[con->os->next]
43 ? con->os->argv[con->os->next] : ""));
44}
45#endif
46
47void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
48{
49 con->execPath = _free(con->execPath);
Martin Poolb348dea2001-02-24 01:32:22 +000050 con->execPath = xstrdup(path);
51 con->execAbsolute = allowAbsolute;
Wayne Davisoncc248aa2002-07-27 18:32:25 +000052 /*@-nullstate@*/ /* LCL: con->execPath can be NULL? */
53 return;
54 /*@=nullstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +000055}
56
Wayne Davisoncc248aa2002-07-27 18:32:25 +000057static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
58 /*@globals internalState@*/
59 /*@modifies internalState@*/
60{
61 if (opt != NULL)
62 for (; opt->longName || opt->shortName || opt->arg; opt++) {
63 if (opt->arg == NULL) continue; /* XXX program error. */
Martin Poolb348dea2001-02-24 01:32:22 +000064 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +000065 /* Recurse on included sub-tables. */
66 invokeCallbacksPRE(con, opt->arg);
67 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
68 (opt->argInfo & POPT_CBFLAG_PRE))
69 { /*@-castfcnptr@*/
70 poptCallbackType cb = (poptCallbackType)opt->arg;
71 /*@=castfcnptr@*/
72 /* Perform callback. */
73 /*@-moduncon -noeffectuncon @*/
74 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
75 /*@=moduncon =noeffectuncon @*/
Martin Poolb348dea2001-02-24 01:32:22 +000076 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +000077 }
78}
79
80static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
81 /*@globals internalState@*/
82 /*@modifies internalState@*/
83{
84 if (opt != NULL)
85 for (; opt->longName || opt->shortName || opt->arg; opt++) {
86 if (opt->arg == NULL) continue; /* XXX program error. */
87 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
88 /* Recurse on included sub-tables. */
89 invokeCallbacksPOST(con, opt->arg);
90 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
91 (opt->argInfo & POPT_CBFLAG_POST))
92 { /*@-castfcnptr@*/
93 poptCallbackType cb = (poptCallbackType)opt->arg;
94 /*@=castfcnptr@*/
95 /* Perform callback. */
96 /*@-moduncon -noeffectuncon @*/
97 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
98 /*@=moduncon =noeffectuncon @*/
99 }
100 }
101}
102
103static void invokeCallbacksOPTION(poptContext con,
104 const struct poptOption * opt,
105 const struct poptOption * myOpt,
106 /*@null@*/ const void * myData, int shorty)
107 /*@globals internalState@*/
108 /*@modifies internalState@*/
109{
110 const struct poptOption * cbopt = NULL;
111
112 if (opt != NULL)
113 for (; opt->longName || opt->shortName || opt->arg; opt++) {
114 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
115 /* Recurse on included sub-tables. */
116 if (opt->arg != NULL) /* XXX program error */
117 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
118 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
119 !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
120 /* Save callback info. */
121 cbopt = opt;
122 } else if (cbopt != NULL &&
123 ((myOpt->shortName && opt->shortName && shorty &&
124 myOpt->shortName == opt->shortName) ||
125 (myOpt->longName && opt->longName &&
126 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
127 !strcmp(myOpt->longName, opt->longName)))
128 /*@=nullpass@*/
129 )
130 { /*@-castfcnptr@*/
131 poptCallbackType cb = (poptCallbackType)cbopt->arg;
132 /*@=castfcnptr@*/
133 const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
134 /* Perform callback. */
135 if (cb != NULL) { /* XXX program error */
136 /*@-moduncon -noeffectuncon @*/
137 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
138 con->os->nextArg, cbData);
139 /*@=moduncon =noeffectuncon @*/
140 }
141 /* Terminate (unless explcitly continuing). */
142 if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
143 return;
144 }
Martin Poolb348dea2001-02-24 01:32:22 +0000145 }
146}
147
148poptContext poptGetContext(const char * name, int argc, const char ** argv,
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000149 const struct poptOption * options, int flags)
150{
Martin Poolb348dea2001-02-24 01:32:22 +0000151 poptContext con = malloc(sizeof(*con));
152
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000153 if (con == NULL) return NULL; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000154 memset(con, 0, sizeof(*con));
155
156 con->os = con->optionStack;
157 con->os->argc = argc;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000158 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000159 con->os->argv = argv;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000160 /*@=dependenttrans =assignexpose@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000161 con->os->argb = NULL;
162
163 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
164 con->os->next = 1; /* skip argv[0] */
165
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000166 con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
167 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000168 con->options = options;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000169 /*@=dependenttrans =assignexpose@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000170 con->aliases = NULL;
171 con->numAliases = 0;
172 con->flags = flags;
173 con->execs = NULL;
174 con->numExecs = 0;
175 con->finalArgvAlloced = argc * 2;
176 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
177 con->execAbsolute = 1;
178 con->arg_strip = NULL;
179
180 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
181 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
182
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000183 if (name) {
184 char * t = malloc(strlen(name) + 1);
185 if (t) con->appName = strcpy(t, name);
186 }
Martin Poolb348dea2001-02-24 01:32:22 +0000187
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000188 /*@-internalglobs@*/
189 invokeCallbacksPRE(con, con->options);
190 /*@=internalglobs@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000191
192 return con;
193}
194
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000195static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
196 /*@uses os @*/
197 /*@releases os->nextArg, os->argv, os->argb @*/
198 /*@modifies os @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000199{
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000200 os->nextArg = _free(os->nextArg);
201 os->argv = _free(os->argv);
202 os->argb = PBM_FREE(os->argb);
Martin Poolb348dea2001-02-24 01:32:22 +0000203}
204
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000205void poptResetContext(poptContext con)
206{
Martin Poolb348dea2001-02-24 01:32:22 +0000207 int i;
208
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000209 if (con == NULL) return;
Martin Poolb348dea2001-02-24 01:32:22 +0000210 while (con->os > con->optionStack) {
211 cleanOSE(con->os--);
212 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000213 con->os->argb = PBM_FREE(con->os->argb);
Martin Poolb348dea2001-02-24 01:32:22 +0000214 con->os->currAlias = NULL;
215 con->os->nextCharArg = NULL;
216 con->os->nextArg = NULL;
217 con->os->next = 1; /* skip argv[0] */
218
219 con->numLeftovers = 0;
220 con->nextLeftover = 0;
221 con->restLeftover = 0;
222 con->doExec = NULL;
223
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000224 if (con->finalArgv != NULL)
225 for (i = 0; i < con->finalArgvCount; i++)
226 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
227 con->finalArgv[i] = _free(con->finalArgv[i]);
228 /*@=unqualifiedtrans@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000229
230 con->finalArgvCount = 0;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000231 con->arg_strip = PBM_FREE(con->arg_strip);
232 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
233 return;
234 /*@=nullstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000235}
236
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000237/* Only one of longName, shortName should be set, not both. */
238static int handleExec(/*@special@*/ poptContext con,
239 /*@null@*/ const char * longName, char shortName)
240 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
241 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
242 /*@modifies con @*/
243{
244 poptItem item;
Martin Poolb348dea2001-02-24 01:32:22 +0000245 int i;
246
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000247 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
248 return 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000249
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000250 for (i = con->numExecs - 1; i >= 0; i--) {
251 item = con->execs + i;
252 if (longName && !(item->option.longName &&
253 !strcmp(longName, item->option.longName)))
254 continue;
255 else if (shortName != item->option.shortName)
256 continue;
257 break;
258 }
Martin Poolb348dea2001-02-24 01:32:22 +0000259 if (i < 0) return 0;
260
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000261
Martin Poolb348dea2001-02-24 01:32:22 +0000262 if (con->flags & POPT_CONTEXT_NO_EXEC)
263 return 1;
264
265 if (con->doExec == NULL) {
266 con->doExec = con->execs + i;
267 return 1;
268 }
269
270 /* We already have an exec to do; remember this option for next
271 time 'round */
272 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
273 con->finalArgvAlloced += 10;
274 con->finalArgv = realloc(con->finalArgv,
275 sizeof(*con->finalArgv) * con->finalArgvAlloced);
276 }
277
278 i = con->finalArgvCount++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000279 if (con->finalArgv != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000280 { char *s = malloc((longName ? strlen(longName) : 0) + 3);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000281 if (s != NULL) { /* XXX can't happen */
282 if (longName)
283 sprintf(s, "--%s", longName);
284 else
285 sprintf(s, "-%c", shortName);
286 con->finalArgv[i] = s;
287 } else
288 con->finalArgv[i] = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000289 }
290
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000291 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
Martin Poolb348dea2001-02-24 01:32:22 +0000292 return 1;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000293 /*@=nullstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000294}
295
296/* Only one of longName, shortName may be set at a time */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000297static int handleAlias(/*@special@*/ poptContext con,
298 /*@null@*/ const char * longName, char shortName,
299 /*@keep@*/ /*@null@*/ const char * nextCharArg)
300 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
301 con->os->currAlias, con->os->currAlias->option.longName @*/
302 /*@modifies con @*/
303{
304 poptItem item = con->os->currAlias;
305 int rc;
Martin Poolb348dea2001-02-24 01:32:22 +0000306 int i;
307
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000308 if (item) {
309 if (longName && (item->option.longName &&
310 !strcmp(longName, item->option.longName)))
311 return 0;
312 if (shortName && shortName == item->option.shortName)
313 return 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000314 }
315
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000316 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
317 return 0;
318
319 for (i = con->numAliases - 1; i >= 0; i--) {
320 item = con->aliases + i;
321 if (longName && !(item->option.longName &&
322 !strcmp(longName, item->option.longName)))
323 continue;
324 else if (shortName != item->option.shortName)
325 continue;
326 break;
327 }
Martin Poolb348dea2001-02-24 01:32:22 +0000328 if (i < 0) return 0;
329
330 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
331 return POPT_ERROR_OPTSTOODEEP;
332
333 if (nextCharArg && *nextCharArg)
334 con->os->nextCharArg = nextCharArg;
335
336 con->os++;
337 con->os->next = 0;
338 con->os->stuffed = 0;
339 con->os->nextArg = NULL;
340 con->os->nextCharArg = NULL;
341 con->os->currAlias = con->aliases + i;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000342 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
Martin Poolb348dea2001-02-24 01:32:22 +0000343 &con->os->argc, &con->os->argv);
344 con->os->argb = NULL;
345
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000346 return (rc ? rc : 1);
Martin Poolb348dea2001-02-24 01:32:22 +0000347}
348
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000349static int execCommand(poptContext con)
350 /*@*/
351{
352 poptItem item = con->doExec;
Martin Poolb348dea2001-02-24 01:32:22 +0000353 const char ** argv;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000354 int argc = 0;
355 int rc;
356
357 if (item == NULL) /*XXX can't happen*/
358 return POPT_ERROR_NOARG;
359
360 if (item->argv == NULL || item->argc < 1 ||
361 (!con->execAbsolute && strchr(item->argv[0], '/')))
362 return POPT_ERROR_NOARG;
Martin Poolb348dea2001-02-24 01:32:22 +0000363
364 argv = malloc(sizeof(*argv) *
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000365 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
366 if (argv == NULL) return POPT_ERROR_MALLOC; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000367
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000368 if (!strchr(item->argv[0], '/') && con->execPath) {
369 char *s = alloca(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
370 sprintf(s, "%s/%s", con->execPath, item->argv[0]);
371 argv[argc] = s;
Martin Poolb348dea2001-02-24 01:32:22 +0000372 } else {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000373 argv[argc] = findProgramPath(item->argv[0]);
Martin Poolb348dea2001-02-24 01:32:22 +0000374 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000375 if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
Martin Poolb348dea2001-02-24 01:32:22 +0000376
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000377 if (item->argc > 1) {
378 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
379 argc += (item->argc - 1);
Martin Poolb348dea2001-02-24 01:32:22 +0000380 }
381
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000382 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
383 memcpy(argv + argc, con->finalArgv,
384 sizeof(*argv) * con->finalArgvCount);
385 argc += con->finalArgvCount;
386 }
387
388 if (con->leftovers != NULL && con->numLeftovers > 0) {
389#if 0
390 argv[argc++] = "--";
391#endif
392 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
393 argc += con->numLeftovers;
394 }
395
396 argv[argc] = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000397
398#ifdef __hpux
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000399 rc = setresuid(getuid(), getuid(),-1);
400 if (rc) return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000401#else
402/*
403 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
404 * XXX sez' Timur Bakeyev <mc@bat.ru>
405 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
406 */
407#if defined(HAVE_SETUID)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000408 rc = setuid(getuid());
409 if (rc) return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000410#elif defined (HAVE_SETREUID)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000411 rc = setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */
412 if (rc) return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000413#else
414 ; /* Can't drop privileges */
415#endif
416#endif
417
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000418 if (argv[0] == NULL)
419 return POPT_ERROR_NOARG;
420#ifdef MYDEBUG
421 { const char ** avp;
422 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
423 for (avp = argv; *avp; avp++)
424 fprintf(stderr, " '%s'", *avp);
425 fprintf(stderr, "\n");
426 }
427#endif
428
429 rc = execvp(argv[0], (char *const *)argv);
430 return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000431}
432
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000433/*@observer@*/ /*@null@*/ static const struct poptOption *
434findOption(const struct poptOption * opt, /*@null@*/ const char * longName,
435 char shortName,
436 /*@null@*/ /*@out@*/ poptCallbackType * callback,
437 /*@null@*/ /*@out@*/ const void ** callbackData,
438 int singleDash)
439 /*@modifies *callback, *callbackData */
Martin Poolb348dea2001-02-24 01:32:22 +0000440{
Martin Poolb348dea2001-02-24 01:32:22 +0000441 const struct poptOption * cb = NULL;
442
443 /* This happens when a single - is given */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000444 if (singleDash && !shortName && (longName && *longName == '\0'))
Martin Poolb348dea2001-02-24 01:32:22 +0000445 shortName = '-';
446
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000447 for (; opt->longName || opt->shortName || opt->arg; opt++) {
448
Martin Poolb348dea2001-02-24 01:32:22 +0000449 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000450 const struct poptOption * opt2;
451
452 /* Recurse on included sub-tables. */
453 if (opt->arg == NULL) continue; /* XXX program error */
Martin Poolb348dea2001-02-24 01:32:22 +0000454 opt2 = findOption(opt->arg, longName, shortName, callback,
455 callbackData, singleDash);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000456 if (opt2 == NULL) continue;
457 /* Sub-table data will be inheirited if no data yet. */
458 if (!(callback && *callback)) return opt2;
459 if (!(callbackData && *callbackData == NULL)) return opt2;
460 /*@-observertrans -dependenttrans @*/
461 *callbackData = opt->descrip;
462 /*@=observertrans =dependenttrans @*/
463 return opt2;
Martin Poolb348dea2001-02-24 01:32:22 +0000464 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
465 cb = opt;
466 } else if (longName && opt->longName &&
467 (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000468 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
469 !strcmp(longName, opt->longName))
470 /*@=nullpass@*/
471 {
Martin Poolb348dea2001-02-24 01:32:22 +0000472 break;
473 } else if (shortName && shortName == opt->shortName) {
474 break;
475 }
Martin Poolb348dea2001-02-24 01:32:22 +0000476 }
477
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000478 if (!opt->longName && !opt->shortName)
479 return NULL;
480 /*@-modobserver -mods @*/
481 if (callback) *callback = NULL;
482 if (callbackData) *callbackData = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000483 if (cb) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000484 if (callback)
485 /*@-castfcnptr@*/
486 *callback = (poptCallbackType)cb->arg;
487 /*@=castfcnptr@*/
488 if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
489 if (callbackData)
490 /*@-observertrans@*/ /* FIX: typedef double indirection. */
491 *callbackData = cb->descrip;
492 /*@=observertrans@*/
493 }
Martin Poolb348dea2001-02-24 01:32:22 +0000494 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000495 /*@=modobserver =mods @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000496
497 return opt;
498}
499
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000500static const char * findNextArg(/*@special@*/ poptContext con,
501 unsigned argx, int delete_arg)
502 /*@uses con->optionStack, con->os,
503 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
504 /*@modifies con @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000505{
506 struct optionStackEntry * os = con->os;
507 const char * arg;
508
509 do {
510 int i;
511 arg = NULL;
512 while (os->next == os->argc && os > con->optionStack) os--;
513 if (os->next == os->argc && os == con->optionStack) break;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000514 if (os->argv != NULL)
Martin Poolb348dea2001-02-24 01:32:22 +0000515 for (i = os->next; i < os->argc; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000516 /*@-sizeoftype@*/
517 if (os->argb && PBM_ISSET(i, os->argb))
518 /*@innercontinue@*/ continue;
519 if (*os->argv[i] == '-')
520 /*@innercontinue@*/ continue;
521 if (--argx > 0)
522 /*@innercontinue@*/ continue;
Martin Poolb348dea2001-02-24 01:32:22 +0000523 arg = os->argv[i];
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000524 if (delete_arg) {
Martin Poolb348dea2001-02-24 01:32:22 +0000525 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000526 if (os->argb != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000527 PBM_SET(i, os->argb);
528 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000529 /*@innerbreak@*/ break;
530 /*@=sizeoftype@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000531 }
532 if (os > con->optionStack) os--;
533 } while (arg == NULL);
534 return arg;
535}
536
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000537static /*@only@*/ /*@null@*/ const char *
538expandNextArg(/*@special@*/ poptContext con, const char * s)
539 /*@uses con->optionStack, con->os,
540 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
541 /*@modifies con @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000542{
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000543 const char * a = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000544 size_t alen;
545 char *t, *te;
546 size_t tn = strlen(s) + 1;
547 char c;
548
549 te = t = malloc(tn);;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000550 if (t == NULL) return NULL; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000551 while ((c = *s++) != '\0') {
552 switch (c) {
553#if 0 /* XXX can't do this */
554 case '\\': /* escape */
555 c = *s++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000556 /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000557#endif
558 case '!':
559 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000560 /*@switchbreak@*/ break;
561 /* XXX Make sure that findNextArg deletes only next arg. */
562 if (a == NULL) {
563 if ((a = findNextArg(con, 1, 1)) == NULL)
564 /*@switchbreak@*/ break;
565 }
Martin Poolb348dea2001-02-24 01:32:22 +0000566 s += 3;
567
568 alen = strlen(a);
569 tn += alen;
570 *te = '\0';
571 t = realloc(t, tn);
572 te = t + strlen(t);
573 strncpy(te, a, alen); te += alen;
574 continue;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000575 /*@notreached@*/ /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000576 default:
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000577 /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000578 }
579 *te++ = c;
580 }
581 *te = '\0';
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000582 t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
Martin Poolb348dea2001-02-24 01:32:22 +0000583 return t;
584}
585
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000586static void poptStripArg(/*@special@*/ poptContext con, int which)
587 /*@uses con->arg_strip, con->optionStack @*/
588 /*@defines con->arg_strip @*/
589 /*@modifies con @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000590{
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000591 /*@-sizeoftype@*/
592 if (con->arg_strip == NULL)
Martin Poolb348dea2001-02-24 01:32:22 +0000593 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000594 if (con->arg_strip != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000595 PBM_SET(which, con->arg_strip);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000596 /*@=sizeoftype@*/
597 /*@-compdef@*/ /* LCL: con->arg_strip udefined? */
598 return;
599 /*@=compdef@*/
600}
601
602static int poptSaveLong(const struct poptOption * opt, long aLong)
603 /*@modifies opt->arg @*/
604{
605 if (opt->arg == NULL)
606 return POPT_ERROR_NULLARG;
607
608 if (opt->argInfo & POPT_ARGFLAG_NOT)
609 aLong = ~aLong;
610 switch (opt->argInfo & POPT_ARGFLAG_LOGICALOPS) {
611 case 0:
612 *((long *) opt->arg) = aLong;
613 break;
614 case POPT_ARGFLAG_OR:
615 *((long *) opt->arg) |= aLong;
616 break;
617 case POPT_ARGFLAG_AND:
618 *((long *) opt->arg) &= aLong;
619 break;
620 case POPT_ARGFLAG_XOR:
621 *((long *) opt->arg) ^= aLong;
622 break;
623 default:
624 return POPT_ERROR_BADOPERATION;
625 /*@notreached@*/ break;
626 }
627 return 0;
628}
629
630static int poptSaveInt(const struct poptOption * opt, long aLong)
631 /*@modifies opt->arg @*/
632{
633 if (opt->arg == NULL)
634 return POPT_ERROR_NULLARG;
635
636 if (opt->argInfo & POPT_ARGFLAG_NOT)
637 aLong = ~aLong;
638 switch (opt->argInfo & POPT_ARGFLAG_LOGICALOPS) {
639 case 0:
640 *((int *) opt->arg) = aLong;
641 break;
642 case POPT_ARGFLAG_OR:
643 *((int *) opt->arg) |= aLong;
644 break;
645 case POPT_ARGFLAG_AND:
646 *((int *) opt->arg) &= aLong;
647 break;
648 case POPT_ARGFLAG_XOR:
649 *((int *) opt->arg) ^= aLong;
650 break;
651 default:
652 return POPT_ERROR_BADOPERATION;
653 /*@notreached@*/ break;
654 }
655 return 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000656}
657
658/* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
659int poptGetNextOpt(poptContext con)
660{
661 const struct poptOption * opt = NULL;
662 int done = 0;
663
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000664 if (con == NULL)
665 return -1;
Martin Poolb348dea2001-02-24 01:32:22 +0000666 while (!done) {
667 const char * origOptString = NULL;
668 poptCallbackType cb = NULL;
669 const void * cbData = NULL;
670 const char * longArg = NULL;
671 int canstrip = 0;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000672 int shorty = 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000673
674 while (!con->os->nextCharArg && con->os->next == con->os->argc
675 && con->os > con->optionStack) {
676 cleanOSE(con->os--);
677 }
678 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000679 /*@-internalglobs@*/
680 invokeCallbacksPOST(con, con->options);
681 /*@=internalglobs@*/
682 if (con->doExec) return execCommand(con);
Martin Poolb348dea2001-02-24 01:32:22 +0000683 return -1;
684 }
685
686 /* Process next long option */
687 if (!con->os->nextCharArg) {
688 char * localOptString, * optString;
689 int thisopt;
690
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000691 /*@-sizeoftype@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000692 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
693 con->os->next++;
694 continue;
695 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000696 /*@=sizeoftype@*/
697 thisopt = con->os->next;
698 if (con->os->argv != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000699 origOptString = con->os->argv[con->os->next++];
700
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000701 if (origOptString == NULL) /* XXX can't happen */
702 return POPT_ERROR_BADOPT;
703
Martin Poolb348dea2001-02-24 01:32:22 +0000704 if (con->restLeftover || *origOptString != '-') {
Martin Poolb348dea2001-02-24 01:32:22 +0000705 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
706 con->restLeftover = 1;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000707 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
708 con->os->nextArg = xstrdup(origOptString);
709 return 0;
710 }
711 if (con->leftovers != NULL) /* XXX can't happen */
712 con->leftovers[con->numLeftovers++] = origOptString;
Martin Poolb348dea2001-02-24 01:32:22 +0000713 continue;
714 }
715
716 /* Make a copy we can hack at */
717 localOptString = optString =
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000718 strcpy(alloca(strlen(origOptString) + 1), origOptString);
Martin Poolb348dea2001-02-24 01:32:22 +0000719
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000720 if (optString[0] == '\0')
Martin Poolb348dea2001-02-24 01:32:22 +0000721 return POPT_ERROR_BADOPT;
722
723 if (optString[1] == '-' && !optString[2]) {
724 con->restLeftover = 1;
725 continue;
726 } else {
727 char *oe;
728 int singleDash;
729
730 optString++;
731 if (*optString == '-')
732 singleDash = 0, optString++;
733 else
734 singleDash = 1;
735
736 /* XXX aliases with arg substitution need "--alias=arg" */
737 if (handleAlias(con, optString, '\0', NULL))
738 continue;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000739
Martin Poolb348dea2001-02-24 01:32:22 +0000740 if (handleExec(con, optString, '\0'))
741 continue;
742
743 /* Check for "--long=arg" option. */
744 for (oe = optString; *oe && *oe != '='; oe++)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000745 {};
Martin Poolb348dea2001-02-24 01:32:22 +0000746 if (*oe == '=') {
747 *oe++ = '\0';
748 /* XXX longArg is mapped back to persistent storage. */
749 longArg = origOptString + (oe - localOptString);
750 }
751
752 opt = findOption(con->options, optString, '\0', &cb, &cbData,
753 singleDash);
754 if (!opt && !singleDash)
755 return POPT_ERROR_BADOPT;
756 }
757
758 if (!opt) {
759 con->os->nextCharArg = origOptString + 1;
760 } else {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000761 if (con->os == con->optionStack &&
762 opt->argInfo & POPT_ARGFLAG_STRIP)
763 {
Martin Poolb348dea2001-02-24 01:32:22 +0000764 canstrip = 1;
765 poptStripArg(con, thisopt);
766 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000767 shorty = 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000768 }
769 }
770
771 /* Process next short option */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000772 /*@-branchstate@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000773 if (con->os->nextCharArg) {
774 origOptString = con->os->nextCharArg;
775
776 con->os->nextCharArg = NULL;
777
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000778 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
779 continue;
780
781 if (handleExec(con, NULL, *origOptString)) {
782 /* Restore rest of short options for further processing */
Martin Poolb348dea2001-02-24 01:32:22 +0000783 origOptString++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000784 if (*origOptString != '\0')
785 con->os->nextCharArg = origOptString;
Martin Poolb348dea2001-02-24 01:32:22 +0000786 continue;
787 }
Martin Poolb348dea2001-02-24 01:32:22 +0000788
789 opt = findOption(con->options, NULL, *origOptString, &cb,
790 &cbData, 0);
791 if (!opt)
792 return POPT_ERROR_BADOPT;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000793 shorty = 1;
Martin Poolb348dea2001-02-24 01:32:22 +0000794
795 origOptString++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000796 if (*origOptString != '\0')
Martin Poolb348dea2001-02-24 01:32:22 +0000797 con->os->nextCharArg = origOptString;
798 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000799 /*@=branchstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000800
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000801 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000802 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000803 if (poptSaveInt(opt, 1L))
804 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000805 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000806 if (opt->arg) {
807 if (poptSaveInt(opt, (long)opt->val))
808 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000809 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000810 } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
811 con->os->nextArg = _free(con->os->nextArg);
812 /*@-usedef@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000813 if (longArg) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000814 /*@=usedef@*/
815 longArg = expandNextArg(con, longArg);
816 con->os->nextArg = longArg;
Martin Poolb348dea2001-02-24 01:32:22 +0000817 } else if (con->os->nextCharArg) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000818 longArg = expandNextArg(con, con->os->nextCharArg);
819 con->os->nextArg = longArg;
Martin Poolb348dea2001-02-24 01:32:22 +0000820 con->os->nextCharArg = NULL;
821 } else {
822 while (con->os->next == con->os->argc &&
823 con->os > con->optionStack) {
824 cleanOSE(con->os--);
825 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000826 if (con->os->next == con->os->argc) {
827 if (!(opt->argInfo & POPT_ARGFLAG_OPTIONAL))
828 /*@-compdef@*/ /* FIX: con->os->argv not defined */
829 return POPT_ERROR_NOARG;
830 /*@=compdef@*/
831 con->os->nextArg = NULL;
832 } else {
Martin Poolb348dea2001-02-24 01:32:22 +0000833
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000834 /*
835 * Make sure this isn't part of a short arg or the
836 * result of an alias expansion.
837 */
838 if (con->os == con->optionStack &&
839 (opt->argInfo & POPT_ARGFLAG_STRIP) &&
840 canstrip) {
841 poptStripArg(con, con->os->next);
842 }
Martin Poolb348dea2001-02-24 01:32:22 +0000843
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000844 if (con->os->argv != NULL) { /* XXX can't happen */
845 /* XXX watchout: subtle side-effects live here. */
846 longArg = con->os->argv[con->os->next++];
847 longArg = expandNextArg(con, longArg);
848 con->os->nextArg = longArg;
849 }
850 }
Martin Poolb348dea2001-02-24 01:32:22 +0000851 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000852 longArg = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000853
854 if (opt->arg) {
Martin Poolb348dea2001-02-24 01:32:22 +0000855 switch (opt->argInfo & POPT_ARG_MASK) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000856 case POPT_ARG_STRING:
Martin Poolb348dea2001-02-24 01:32:22 +0000857 /* XXX memory leak, hard to plug */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000858 *((const char **) opt->arg) = (con->os->nextArg)
859 ? xstrdup(con->os->nextArg) : NULL;
860 /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000861
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000862 case POPT_ARG_INT:
863 case POPT_ARG_LONG:
864 { long aLong = 0;
865 char *end;
Martin Poolb348dea2001-02-24 01:32:22 +0000866
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000867 if (con->os->nextArg) {
868 aLong = strtol(con->os->nextArg, &end, 0);
869 if (!(end && *end == '\0'))
870 return POPT_ERROR_BADNUMBER;
871 }
872
Martin Poolb348dea2001-02-24 01:32:22 +0000873 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000874 if (aLong == LONG_MIN || aLong == LONG_MAX)
875 return POPT_ERROR_OVERFLOW;
876 if (poptSaveLong(opt, aLong))
877 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000878 } else {
879 if (aLong > INT_MAX || aLong < INT_MIN)
880 return POPT_ERROR_OVERFLOW;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000881 if (poptSaveInt(opt, aLong))
882 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000883 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000884 } /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000885
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000886 case POPT_ARG_FLOAT:
887 case POPT_ARG_DOUBLE:
888 { double aDouble = 0.0;
889 char *end;
890
891 if (con->os->nextArg) {
892 /*@-mods@*/
893 int saveerrno = errno;
894 errno = 0;
895 aDouble = strtod(con->os->nextArg, &end);
896 if (errno == ERANGE)
897 return POPT_ERROR_OVERFLOW;
898 errno = saveerrno;
899 /*@=mods@*/
900 if (*end != '\0')
901 return POPT_ERROR_BADNUMBER;
902 }
903
904 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
905 *((double *) opt->arg) = aDouble;
906 } else {
907#define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
908 if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
909 return POPT_ERROR_OVERFLOW;
910 if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
911 return POPT_ERROR_OVERFLOW;
912 *((float *) opt->arg) = aDouble;
913 }
914 } /*@switchbreak@*/ break;
915 default:
916 fprintf(stdout,
917 POPT_("option type (%d) not implemented in popt\n"),
918 (opt->argInfo & POPT_ARG_MASK));
Martin Poolb348dea2001-02-24 01:32:22 +0000919 exit(EXIT_FAILURE);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000920 /*@notreached@*/ /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000921 }
922 }
923 }
924
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000925 if (cb) {
926 /*@-internalglobs@*/
927 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
928 /*@=internalglobs@*/
929 } else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
Martin Poolb348dea2001-02-24 01:32:22 +0000930 done = 1;
931
932 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
933 con->finalArgvAlloced += 10;
934 con->finalArgv = realloc(con->finalArgv,
935 sizeof(*con->finalArgv) * con->finalArgvAlloced);
936 }
937
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000938 if (con->finalArgv != NULL)
939 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
940 if (s != NULL) { /* XXX can't happen */
941 if (opt->longName)
942 sprintf(s, "%s%s",
943 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
944 opt->longName);
945 else
946 sprintf(s, "-%c", opt->shortName);
947 con->finalArgv[con->finalArgvCount++] = s;
948 } else
949 con->finalArgv[con->finalArgvCount++] = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000950 }
951
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000952 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
953 /*@-ifempty@*/ ; /*@=ifempty@*/
954 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
955 /*@-ifempty@*/ ; /*@=ifempty@*/
956 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
957 if (con->finalArgv != NULL && con->os->nextArg)
958 con->finalArgv[con->finalArgvCount++] =
959 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
960 xstrdup(con->os->nextArg);
961 /*@=nullpass@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000962 }
963 }
964
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000965 return (opt ? opt->val : -1); /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000966}
967
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000968const char * poptGetOptArg(poptContext con)
969{
970 const char * ret = NULL;
971 /*@-branchstate@*/
972 if (con) {
973 ret = con->os->nextArg;
974 con->os->nextArg = NULL;
975 }
976 /*@=branchstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000977 return ret;
978}
979
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000980const char * poptGetArg(poptContext con)
981{
982 const char * ret = NULL;
983 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
984 ret = con->leftovers[con->nextLeftover++];
985 return ret;
Martin Poolb348dea2001-02-24 01:32:22 +0000986}
987
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000988const char * poptPeekArg(poptContext con)
989{
990 const char * ret = NULL;
991 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
992 ret = con->leftovers[con->nextLeftover];
993 return ret;
Martin Poolb348dea2001-02-24 01:32:22 +0000994}
995
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000996const char ** poptGetArgs(poptContext con)
997{
998 if (con == NULL ||
999 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1000 return NULL;
Martin Poolb348dea2001-02-24 01:32:22 +00001001
1002 /* some apps like [like RPM ;-) ] need this NULL terminated */
1003 con->leftovers[con->numLeftovers] = NULL;
1004
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001005 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
Martin Poolb348dea2001-02-24 01:32:22 +00001006 return (con->leftovers + con->nextLeftover);
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001007 /*@=nullret =nullstate @*/
Martin Poolb348dea2001-02-24 01:32:22 +00001008}
1009
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001010poptContext poptFreeContext(poptContext con)
1011{
1012 poptItem item;
Martin Poolb348dea2001-02-24 01:32:22 +00001013 int i;
1014
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001015 if (con == NULL) return con;
Martin Poolb348dea2001-02-24 01:32:22 +00001016 poptResetContext(con);
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001017 con->os->argb = _free(con->os->argb);
Martin Poolb348dea2001-02-24 01:32:22 +00001018
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001019 if (con->aliases != NULL)
Martin Poolb348dea2001-02-24 01:32:22 +00001020 for (i = 0; i < con->numAliases; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001021 item = con->aliases + i;
1022 /*@-modobserver -observertrans -dependenttrans@*/
1023 item->option.longName = _free(item->option.longName);
1024 item->option.descrip = _free(item->option.descrip);
1025 item->option.argDescrip = _free(item->option.argDescrip);
1026 /*@=modobserver =observertrans =dependenttrans@*/
1027 item->argv = _free(item->argv);
Martin Poolb348dea2001-02-24 01:32:22 +00001028 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001029 con->aliases = _free(con->aliases);
Martin Poolb348dea2001-02-24 01:32:22 +00001030
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001031 if (con->execs != NULL)
Martin Poolb348dea2001-02-24 01:32:22 +00001032 for (i = 0; i < con->numExecs; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001033 item = con->execs + i;
1034 /*@-modobserver -observertrans -dependenttrans@*/
1035 item->option.longName = _free(item->option.longName);
1036 item->option.descrip = _free(item->option.descrip);
1037 item->option.argDescrip = _free(item->option.argDescrip);
1038 /*@=modobserver =observertrans =dependenttrans@*/
1039 item->argv = _free(item->argv);
Martin Poolb348dea2001-02-24 01:32:22 +00001040 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001041 con->execs = _free(con->execs);
Martin Poolb348dea2001-02-24 01:32:22 +00001042
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001043 con->leftovers = _free(con->leftovers);
1044 con->finalArgv = _free(con->finalArgv);
1045 con->appName = _free(con->appName);
1046 con->otherHelp = _free(con->otherHelp);
1047 con->execPath = _free(con->execPath);
1048 con->arg_strip = PBM_FREE(con->arg_strip);
Martin Poolb348dea2001-02-24 01:32:22 +00001049
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001050 con = _free(con);
1051 return con;
Martin Poolb348dea2001-02-24 01:32:22 +00001052}
1053
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001054int poptAddAlias(poptContext con, struct poptAlias alias,
Martin Poolb348dea2001-02-24 01:32:22 +00001055 /*@unused@*/ int flags)
1056{
David Dykstra451b5fc2003-01-11 14:39:41 +00001057 poptItem item = (poptItem) alloca(sizeof(*item));
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001058 memset(item, 0, sizeof(*item));
1059 item->option.longName = alias.longName;
1060 item->option.shortName = alias.shortName;
1061 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1062 item->option.arg = 0;
1063 item->option.val = 0;
1064 item->option.descrip = NULL;
1065 item->option.argDescrip = NULL;
1066 item->argc = alias.argc;
1067 item->argv = alias.argv;
1068 return poptAddItem(con, item, 0);
1069}
Martin Poolb348dea2001-02-24 01:32:22 +00001070
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001071/*@-mustmod@*/ /* LCL: con not modified? */
1072int poptAddItem(poptContext con, poptItem newItem, int flags)
1073{
1074 poptItem * items, item;
1075 int * nitems;
Martin Poolb348dea2001-02-24 01:32:22 +00001076
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001077 switch (flags) {
1078 case 1:
1079 items = &con->execs;
1080 nitems = &con->numExecs;
1081 break;
1082 case 0:
1083 items = &con->aliases;
1084 nitems = &con->numAliases;
1085 break;
1086 default:
1087 return 1;
1088 /*@notreached@*/ break;
1089 }
1090
1091 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1092 if ((*items) == NULL)
1093 return 1;
1094
1095 item = (*items) + (*nitems);
1096
1097 item->option.longName =
1098 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1099 item->option.shortName = newItem->option.shortName;
1100 item->option.argInfo = newItem->option.argInfo;
1101 item->option.arg = newItem->option.arg;
1102 item->option.val = newItem->option.val;
1103 item->option.descrip =
1104 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1105 item->option.argDescrip =
1106 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1107 item->argc = newItem->argc;
1108 item->argv = newItem->argv;
1109
1110 (*nitems)++;
Martin Poolb348dea2001-02-24 01:32:22 +00001111
1112 return 0;
1113}
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001114/*@=mustmod@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001115
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001116const char * poptBadOption(poptContext con, int flags)
1117{
1118 struct optionStackEntry * os = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +00001119
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001120 if (con != NULL)
1121 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
Martin Poolb348dea2001-02-24 01:32:22 +00001122
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001123 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1124 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1125 /*@=nullderef@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001126}
1127
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001128const char *const poptStrerror(const int error)
1129{
Martin Poolb348dea2001-02-24 01:32:22 +00001130 switch (error) {
1131 case POPT_ERROR_NOARG:
1132 return POPT_("missing argument");
1133 case POPT_ERROR_BADOPT:
1134 return POPT_("unknown option");
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001135 case POPT_ERROR_BADOPERATION:
1136 return POPT_("mutually exclusive logical operations requested");
1137 case POPT_ERROR_NULLARG:
1138 return POPT_("opt->arg should not be NULL");
Martin Poolb348dea2001-02-24 01:32:22 +00001139 case POPT_ERROR_OPTSTOODEEP:
1140 return POPT_("aliases nested too deeply");
1141 case POPT_ERROR_BADQUOTE:
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001142 return POPT_("error in parameter quoting");
Martin Poolb348dea2001-02-24 01:32:22 +00001143 case POPT_ERROR_BADNUMBER:
1144 return POPT_("invalid numeric value");
1145 case POPT_ERROR_OVERFLOW:
1146 return POPT_("number too large or too small");
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001147 case POPT_ERROR_MALLOC:
1148 return POPT_("memory allocation failed");
Martin Poolb348dea2001-02-24 01:32:22 +00001149 case POPT_ERROR_ERRNO:
1150 return strerror(errno);
1151 default:
1152 return POPT_("unknown error");
1153 }
1154}
1155
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001156int poptStuffArgs(poptContext con, const char ** argv)
1157{
Martin Poolb348dea2001-02-24 01:32:22 +00001158 int argc;
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001159 int rc;
Martin Poolb348dea2001-02-24 01:32:22 +00001160
1161 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1162 return POPT_ERROR_OPTSTOODEEP;
1163
1164 for (argc = 0; argv[argc]; argc++)
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001165 {};
Martin Poolb348dea2001-02-24 01:32:22 +00001166
1167 con->os++;
1168 con->os->next = 0;
1169 con->os->nextArg = NULL;
1170 con->os->nextCharArg = NULL;
1171 con->os->currAlias = NULL;
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001172 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
Martin Poolb348dea2001-02-24 01:32:22 +00001173 con->os->argb = NULL;
1174 con->os->stuffed = 1;
1175
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001176 return rc;
Martin Poolb348dea2001-02-24 01:32:22 +00001177}
1178
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001179const char * poptGetInvocationName(poptContext con)
Martin Poolb348dea2001-02-24 01:32:22 +00001180{
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001181 return (con->os->argv ? con->os->argv[0] : "");
1182}
1183
1184int poptStrippedArgv(poptContext con, int argc, char ** argv)
1185{
1186 int numargs = argc;
1187 int j = 1;
1188 int i;
Martin Poolb348dea2001-02-24 01:32:22 +00001189
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001190 /*@-sizeoftype@*/
1191 if (con->arg_strip)
1192 for (i = 1; i < argc; i++) {
1193 if (PBM_ISSET(i, con->arg_strip))
Martin Poolb348dea2001-02-24 01:32:22 +00001194 numargs--;
Martin Poolb348dea2001-02-24 01:32:22 +00001195 }
1196
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001197 for (i = 1; i < argc; i++) {
1198 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
Martin Poolb348dea2001-02-24 01:32:22 +00001199 continue;
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001200 argv[j] = (j < numargs) ? argv[i] : NULL;
1201 j++;
Martin Poolb348dea2001-02-24 01:32:22 +00001202 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001203 /*@=sizeoftype@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001204
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001205 return numargs;
Martin Poolb348dea2001-02-24 01:32:22 +00001206}