Commit | Line | Data |
---|---|---|
cc248aae WD |
1 | /** \ingroup popt |
2 | * \file popt/popt.c | |
3 | */ | |
4 | ||
5 | /* (C) 19982000 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 | ||
21 | #ifndef HAVE_STRERROR | |
22 | static 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 | ||
cc248aae WD |
33 | #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 | ||
47 | void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) | |
48 | { | |
49 | con->execPath = _free(con->execPath); | |
b348deae | 50 | con->execPath = xstrdup(path); |
62402cb1 | 51 | con->execAbsolute = allowAbsolute; |
cc248aae WD |
52 | /*@-nullstate@*/ /* LCL: con->execPath can be NULL? */ |
53 | return; | |
54 | /*@=nullstate@*/ | |
62402cb1 MP |
55 | } |
56 | ||
cc248aae WD |
57 | static 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. */ | |
64 | if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) { | |
65 | /* 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 @*/ | |
76 | } | |
77 | } | |
78 | } | |
b348deae | 79 | |
cc248aae WD |
80 | static 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. */ | |
62402cb1 | 87 | if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) { |
cc248aae WD |
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 | ||
103 | static 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; | |
62402cb1 | 144 | } |
62402cb1 MP |
145 | } |
146 | } | |
147 | ||
b348deae | 148 | poptContext poptGetContext(const char * name, int argc, const char ** argv, |
cc248aae WD |
149 | const struct poptOption * options, int flags) |
150 | { | |
62402cb1 MP |
151 | poptContext con = malloc(sizeof(*con)); |
152 | ||
cc248aae | 153 | if (con == NULL) return NULL; /* XXX can't happen */ |
62402cb1 MP |
154 | memset(con, 0, sizeof(*con)); |
155 | ||
156 | con->os = con->optionStack; | |
157 | con->os->argc = argc; | |
cc248aae | 158 | /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */ |
62402cb1 | 159 | con->os->argv = argv; |
cc248aae | 160 | /*@=dependenttrans =assignexpose@*/ |
b348deae | 161 | con->os->argb = NULL; |
62402cb1 MP |
162 | |
163 | if (!(flags & POPT_CONTEXT_KEEP_FIRST)) | |
164 | con->os->next = 1; /* skip argv[0] */ | |
165 | ||
cc248aae WD |
166 | con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) ); |
167 | /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */ | |
62402cb1 | 168 | con->options = options; |
cc248aae | 169 | /*@=dependenttrans =assignexpose@*/ |
b348deae MP |
170 | con->aliases = NULL; |
171 | con->numAliases = 0; | |
62402cb1 | 172 | con->flags = flags; |
b348deae MP |
173 | con->execs = NULL; |
174 | con->numExecs = 0; | |
175 | con->finalArgvAlloced = argc * 2; | |
176 | con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) ); | |
62402cb1 | 177 | con->execAbsolute = 1; |
b348deae | 178 | con->arg_strip = NULL; |
62402cb1 MP |
179 | |
180 | if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER")) | |
181 | con->flags |= POPT_CONTEXT_POSIXMEHARDER; | |
b348deae | 182 | |
cc248aae WD |
183 | if (name) { |
184 | char * t = malloc(strlen(name) + 1); | |
185 | if (t) con->appName = strcpy(t, name); | |
186 | } | |
62402cb1 | 187 | |
cc248aae WD |
188 | /*@-internalglobs@*/ |
189 | invokeCallbacksPRE(con, con->options); | |
190 | /*@=internalglobs@*/ | |
62402cb1 MP |
191 | |
192 | return con; | |
193 | } | |
194 | ||
cc248aae WD |
195 | static void cleanOSE(/*@special@*/ struct optionStackEntry *os) |
196 | /*@uses os @*/ | |
197 | /*@releases os->nextArg, os->argv, os->argb @*/ | |
198 | /*@modifies os @*/ | |
b348deae | 199 | { |
cc248aae WD |
200 | os->nextArg = _free(os->nextArg); |
201 | os->argv = _free(os->argv); | |
202 | os->argb = PBM_FREE(os->argb); | |
b348deae MP |
203 | } |
204 | ||
cc248aae WD |
205 | void poptResetContext(poptContext con) |
206 | { | |
b348deae MP |
207 | int i; |
208 | ||
cc248aae | 209 | if (con == NULL) return; |
b348deae MP |
210 | while (con->os > con->optionStack) { |
211 | cleanOSE(con->os--); | |
212 | } | |
cc248aae | 213 | con->os->argb = PBM_FREE(con->os->argb); |
62402cb1 MP |
214 | 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; | |
b348deae | 223 | |
cc248aae WD |
224 | 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@*/ | |
b348deae | 229 | |
62402cb1 | 230 | con->finalArgvCount = 0; |
cc248aae WD |
231 | con->arg_strip = PBM_FREE(con->arg_strip); |
232 | /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */ | |
233 | return; | |
234 | /*@=nullstate@*/ | |
62402cb1 MP |
235 | } |
236 | ||
cc248aae WD |
237 | /* Only one of longName, shortName should be set, not both. */ |
238 | static 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; | |
62402cb1 MP |
245 | int i; |
246 | ||
cc248aae WD |
247 | if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */ |
248 | return 0; | |
62402cb1 | 249 | |
cc248aae WD |
250 | 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 | } | |
62402cb1 MP |
259 | if (i < 0) return 0; |
260 | ||
cc248aae | 261 | |
62402cb1 MP |
262 | if (con->flags & POPT_CONTEXT_NO_EXEC) |
263 | return 1; | |
264 | ||
b348deae | 265 | if (con->doExec == NULL) { |
62402cb1 MP |
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++; | |
cc248aae | 279 | if (con->finalArgv != NULL) /* XXX can't happen */ |
b348deae | 280 | { char *s = malloc((longName ? strlen(longName) : 0) + 3); |
cc248aae WD |
281 | 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; | |
b348deae | 289 | } |
62402cb1 | 290 | |
cc248aae | 291 | /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */ |
62402cb1 | 292 | return 1; |
cc248aae | 293 | /*@=nullstate@*/ |
62402cb1 MP |
294 | } |
295 | ||
296 | /* Only one of longName, shortName may be set at a time */ | |
cc248aae WD |
297 | static 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; | |
62402cb1 MP |
306 | int i; |
307 | ||
cc248aae WD |
308 | 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; | |
314 | } | |
315 | ||
316 | if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */ | |
62402cb1 MP |
317 | return 0; |
318 | ||
cc248aae WD |
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; | |
62402cb1 | 327 | } |
62402cb1 MP |
328 | if (i < 0) return 0; |
329 | ||
b348deae | 330 | if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH) |
62402cb1 MP |
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; | |
b348deae MP |
339 | con->os->nextArg = NULL; |
340 | con->os->nextCharArg = NULL; | |
62402cb1 | 341 | con->os->currAlias = con->aliases + i; |
cc248aae | 342 | rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv, |
b348deae MP |
343 | &con->os->argc, &con->os->argv); |
344 | con->os->argb = NULL; | |
62402cb1 | 345 | |
cc248aae | 346 | return (rc ? rc : 1); |
62402cb1 MP |
347 | } |
348 | ||
cc248aae WD |
349 | static int execCommand(poptContext con) |
350 | /*@*/ | |
351 | { | |
352 | poptItem item = con->doExec; | |
b348deae | 353 | const char ** argv; |
cc248aae WD |
354 | int argc = 0; |
355 | int rc; | |
62402cb1 | 356 | |
cc248aae WD |
357 | if (item == NULL) /*XXX can't happen*/ |
358 | return POPT_ERROR_NOARG; | |
62402cb1 | 359 | |
cc248aae WD |
360 | if (item->argv == NULL || item->argc < 1 || |
361 | (!con->execAbsolute && strchr(item->argv[0], '/'))) | |
362 | return POPT_ERROR_NOARG; | |
363 | ||
364 | argv = malloc(sizeof(*argv) * | |
365 | (6 + item->argc + con->numLeftovers + con->finalArgvCount)); | |
366 | if (argv == NULL) return POPT_ERROR_MALLOC; /* XXX can't happen */ | |
62402cb1 | 367 | |
cc248aae WD |
368 | 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; | |
62402cb1 | 372 | } else { |
cc248aae | 373 | argv[argc] = findProgramPath(item->argv[0]); |
62402cb1 | 374 | } |
cc248aae | 375 | if (argv[argc++] == NULL) return POPT_ERROR_NOARG; |
62402cb1 | 376 | |
cc248aae WD |
377 | if (item->argc > 1) { |
378 | memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1)); | |
379 | argc += (item->argc - 1); | |
380 | } | |
62402cb1 | 381 | |
cc248aae WD |
382 | if (con->finalArgv != NULL && con->finalArgvCount > 0) { |
383 | memcpy(argv + argc, con->finalArgv, | |
384 | sizeof(*argv) * con->finalArgvCount); | |
385 | argc += con->finalArgvCount; | |
386 | } | |
62402cb1 | 387 | |
cc248aae WD |
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; | |
62402cb1 MP |
394 | } |
395 | ||
cc248aae | 396 | argv[argc] = NULL; |
62402cb1 MP |
397 | |
398 | #ifdef __hpux | |
cc248aae WD |
399 | rc = setresuid(getuid(), getuid(),-1); |
400 | if (rc) return POPT_ERROR_ERRNO; | |
62402cb1 | 401 | #else |
b348deae MP |
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) | |
cc248aae WD |
408 | rc = setuid(getuid()); |
409 | if (rc) return POPT_ERROR_ERRNO; | |
b348deae | 410 | #elif defined (HAVE_SETREUID) |
cc248aae WD |
411 | rc = setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */ |
412 | if (rc) return POPT_ERROR_ERRNO; | |
b348deae MP |
413 | #else |
414 | ; /* Can't drop privileges */ | |
415 | #endif | |
62402cb1 MP |
416 | #endif |
417 | ||
cc248aae WD |
418 | 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; | |
62402cb1 MP |
431 | } |
432 | ||
cc248aae WD |
433 | /*@observer@*/ /*@null@*/ static const struct poptOption * |
434 | findOption(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 */ | |
b348deae | 440 | { |
62402cb1 MP |
441 | const struct poptOption * cb = NULL; |
442 | ||
b348deae | 443 | /* This happens when a single - is given */ |
cc248aae | 444 | if (singleDash && !shortName && (longName && *longName == '\0')) |
b348deae MP |
445 | shortName = '-'; |
446 | ||
cc248aae WD |
447 | for (; opt->longName || opt->shortName || opt->arg; opt++) { |
448 | ||
62402cb1 | 449 | if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) { |
cc248aae WD |
450 | const struct poptOption * opt2; |
451 | ||
452 | /* Recurse on included sub-tables. */ | |
453 | if (opt->arg == NULL) continue; /* XXX program error */ | |
b348deae | 454 | opt2 = findOption(opt->arg, longName, shortName, callback, |
62402cb1 | 455 | callbackData, singleDash); |
cc248aae WD |
456 | 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; | |
62402cb1 MP |
464 | } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) { |
465 | cb = opt; | |
b348deae | 466 | } else if (longName && opt->longName && |
62402cb1 | 467 | (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) && |
cc248aae WD |
468 | /*@-nullpass@*/ /* LCL: opt->longName != NULL */ |
469 | !strcmp(longName, opt->longName)) | |
470 | /*@=nullpass@*/ | |
471 | { | |
62402cb1 MP |
472 | break; |
473 | } else if (shortName && shortName == opt->shortName) { | |
474 | break; | |
475 | } | |
62402cb1 MP |
476 | } |
477 | ||
cc248aae WD |
478 | if (!opt->longName && !opt->shortName) |
479 | return NULL; | |
480 | /*@-modobserver -mods @*/ | |
481 | if (callback) *callback = NULL; | |
482 | if (callbackData) *callbackData = NULL; | |
62402cb1 | 483 | if (cb) { |
cc248aae WD |
484 | 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 | } | |
62402cb1 | 494 | } |
cc248aae | 495 | /*@=modobserver =mods @*/ |
62402cb1 MP |
496 | |
497 | return opt; | |
498 | } | |
499 | ||
cc248aae WD |
500 | static 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 @*/ | |
b348deae MP |
505 | { |
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; | |
cc248aae | 514 | if (os->argv != NULL) |
b348deae | 515 | for (i = os->next; i < os->argc; i++) { |
cc248aae WD |
516 | /*@-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; | |
b348deae | 523 | arg = os->argv[i]; |
cc248aae | 524 | if (delete_arg) { |
b348deae | 525 | if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc); |
cc248aae | 526 | if (os->argb != NULL) /* XXX can't happen */ |
b348deae MP |
527 | PBM_SET(i, os->argb); |
528 | } | |
cc248aae WD |
529 | /*@innerbreak@*/ break; |
530 | /*@=sizeoftype@*/ | |
b348deae MP |
531 | } |
532 | if (os > con->optionStack) os--; | |
533 | } while (arg == NULL); | |
534 | return arg; | |
535 | } | |
536 | ||
cc248aae WD |
537 | static /*@only@*/ /*@null@*/ const char * |
538 | expandNextArg(/*@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 @*/ | |
b348deae | 542 | { |
cc248aae | 543 | const char * a = NULL; |
b348deae MP |
544 | size_t alen; |
545 | char *t, *te; | |
546 | size_t tn = strlen(s) + 1; | |
547 | char c; | |
548 | ||
549 | te = t = malloc(tn);; | |
cc248aae | 550 | if (t == NULL) return NULL; /* XXX can't happen */ |
b348deae MP |
551 | while ((c = *s++) != '\0') { |
552 | switch (c) { | |
553 | #if 0 /* XXX can't do this */ | |
554 | case '\\': /* escape */ | |
555 | c = *s++; | |
cc248aae | 556 | /*@switchbreak@*/ break; |
b348deae MP |
557 | #endif |
558 | case '!': | |
559 | if (!(s[0] == '#' && s[1] == ':' && s[2] == '+')) | |
cc248aae WD |
560 | /*@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 | } | |
b348deae MP |
566 | 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; | |
cc248aae | 575 | /*@notreached@*/ /*@switchbreak@*/ break; |
b348deae | 576 | default: |
cc248aae | 577 | /*@switchbreak@*/ break; |
b348deae MP |
578 | } |
579 | *te++ = c; | |
580 | } | |
581 | *te = '\0'; | |
cc248aae | 582 | t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */ |
b348deae MP |
583 | return t; |
584 | } | |
585 | ||
cc248aae WD |
586 | static void poptStripArg(/*@special@*/ poptContext con, int which) |
587 | /*@uses con->arg_strip, con->optionStack @*/ | |
588 | /*@defines con->arg_strip @*/ | |
589 | /*@modifies con @*/ | |
b348deae | 590 | { |
cc248aae WD |
591 | /*@-sizeoftype@*/ |
592 | if (con->arg_strip == NULL) | |
b348deae | 593 | con->arg_strip = PBM_ALLOC(con->optionStack[0].argc); |
cc248aae | 594 | if (con->arg_strip != NULL) /* XXX can't happen */ |
b348deae | 595 | PBM_SET(which, con->arg_strip); |
cc248aae WD |
596 | /*@=sizeoftype@*/ |
597 | /*@-compdef@*/ /* LCL: con->arg_strip udefined? */ | |
598 | return; | |
599 | /*@=compdef@*/ | |
600 | } | |
601 | ||
602 | static 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 | ||
630 | static 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; | |
b348deae MP |
656 | } |
657 | ||
62402cb1 | 658 | /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */ |
b348deae MP |
659 | int poptGetNextOpt(poptContext con) |
660 | { | |
62402cb1 MP |
661 | const struct poptOption * opt = NULL; |
662 | int done = 0; | |
62402cb1 | 663 | |
cc248aae WD |
664 | if (con == NULL) |
665 | return -1; | |
62402cb1 | 666 | while (!done) { |
b348deae MP |
667 | const char * origOptString = NULL; |
668 | poptCallbackType cb = NULL; | |
669 | const void * cbData = NULL; | |
670 | const char * longArg = NULL; | |
671 | int canstrip = 0; | |
cc248aae | 672 | int shorty = 0; |
b348deae MP |
673 | |
674 | while (!con->os->nextCharArg && con->os->next == con->os->argc | |
675 | && con->os > con->optionStack) { | |
676 | cleanOSE(con->os--); | |
677 | } | |
62402cb1 | 678 | if (!con->os->nextCharArg && con->os->next == con->os->argc) { |
cc248aae WD |
679 | /*@-internalglobs@*/ |
680 | invokeCallbacksPOST(con, con->options); | |
681 | /*@=internalglobs@*/ | |
682 | if (con->doExec) return execCommand(con); | |
62402cb1 MP |
683 | return -1; |
684 | } | |
685 | ||
b348deae | 686 | /* Process next long option */ |
62402cb1 | 687 | if (!con->os->nextCharArg) { |
b348deae MP |
688 | char * localOptString, * optString; |
689 | int thisopt; | |
690 | ||
cc248aae | 691 | /*@-sizeoftype@*/ |
b348deae MP |
692 | if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) { |
693 | con->os->next++; | |
694 | continue; | |
695 | } | |
cc248aae WD |
696 | /*@=sizeoftype@*/ |
697 | thisopt = con->os->next; | |
698 | if (con->os->argv != NULL) /* XXX can't happen */ | |
62402cb1 MP |
699 | origOptString = con->os->argv[con->os->next++]; |
700 | ||
cc248aae WD |
701 | if (origOptString == NULL) /* XXX can't happen */ |
702 | return POPT_ERROR_BADOPT; | |
703 | ||
62402cb1 | 704 | if (con->restLeftover || *origOptString != '-') { |
62402cb1 MP |
705 | if (con->flags & POPT_CONTEXT_POSIXMEHARDER) |
706 | con->restLeftover = 1; | |
cc248aae WD |
707 | 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; | |
62402cb1 MP |
713 | continue; |
714 | } | |
715 | ||
716 | /* Make a copy we can hack at */ | |
b348deae | 717 | localOptString = optString = |
cc248aae | 718 | strcpy(alloca(strlen(origOptString) + 1), origOptString); |
62402cb1 | 719 | |
cc248aae | 720 | if (optString[0] == '\0') |
62402cb1 MP |
721 | return POPT_ERROR_BADOPT; |
722 | ||
723 | if (optString[1] == '-' && !optString[2]) { | |
724 | con->restLeftover = 1; | |
725 | continue; | |
726 | } else { | |
b348deae MP |
727 | char *oe; |
728 | int singleDash; | |
729 | ||
62402cb1 MP |
730 | optString++; |
731 | if (*optString == '-') | |
732 | singleDash = 0, optString++; | |
733 | else | |
734 | singleDash = 1; | |
735 | ||
b348deae | 736 | /* XXX aliases with arg substitution need "--alias=arg" */ |
62402cb1 MP |
737 | if (handleAlias(con, optString, '\0', NULL)) |
738 | continue; | |
cc248aae | 739 | |
62402cb1 MP |
740 | if (handleExec(con, optString, '\0')) |
741 | continue; | |
742 | ||
b348deae MP |
743 | /* Check for "--long=arg" option. */ |
744 | for (oe = optString; *oe && *oe != '='; oe++) | |
cc248aae | 745 | {}; |
b348deae MP |
746 | if (*oe == '=') { |
747 | *oe++ = '\0'; | |
748 | /* XXX longArg is mapped back to persistent storage. */ | |
749 | longArg = origOptString + (oe - localOptString); | |
62402cb1 MP |
750 | } |
751 | ||
752 | opt = findOption(con->options, optString, '\0', &cb, &cbData, | |
753 | singleDash); | |
b348deae MP |
754 | if (!opt && !singleDash) |
755 | return POPT_ERROR_BADOPT; | |
62402cb1 MP |
756 | } |
757 | ||
b348deae | 758 | if (!opt) { |
62402cb1 | 759 | con->os->nextCharArg = origOptString + 1; |
b348deae | 760 | } else { |
cc248aae WD |
761 | if (con->os == con->optionStack && |
762 | opt->argInfo & POPT_ARGFLAG_STRIP) | |
763 | { | |
b348deae MP |
764 | canstrip = 1; |
765 | poptStripArg(con, thisopt); | |
766 | } | |
cc248aae | 767 | shorty = 0; |
b348deae | 768 | } |
62402cb1 MP |
769 | } |
770 | ||
b348deae | 771 | /* Process next short option */ |
cc248aae | 772 | /*@-branchstate@*/ /* FIX: W2DO? */ |
62402cb1 MP |
773 | if (con->os->nextCharArg) { |
774 | origOptString = con->os->nextCharArg; | |
775 | ||
776 | con->os->nextCharArg = NULL; | |
777 | ||
cc248aae WD |
778 | 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 */ | |
62402cb1 | 783 | origOptString++; |
cc248aae WD |
784 | if (*origOptString != '\0') |
785 | con->os->nextCharArg = origOptString; | |
62402cb1 MP |
786 | continue; |
787 | } | |
62402cb1 | 788 | |
b348deae | 789 | opt = findOption(con->options, NULL, *origOptString, &cb, |
62402cb1 | 790 | &cbData, 0); |
b348deae MP |
791 | if (!opt) |
792 | return POPT_ERROR_BADOPT; | |
cc248aae | 793 | shorty = 1; |
62402cb1 MP |
794 | |
795 | origOptString++; | |
cc248aae | 796 | if (*origOptString != '\0') |
62402cb1 MP |
797 | con->os->nextCharArg = origOptString; |
798 | } | |
cc248aae | 799 | /*@=branchstate@*/ |
62402cb1 | 800 | |
cc248aae | 801 | if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */ |
b348deae | 802 | if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) { |
cc248aae WD |
803 | if (poptSaveInt(opt, 1L)) |
804 | return POPT_ERROR_BADOPERATION; | |
b348deae | 805 | } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) { |
cc248aae WD |
806 | if (opt->arg) { |
807 | if (poptSaveInt(opt, (long)opt->val)) | |
808 | return POPT_ERROR_BADOPERATION; | |
b348deae | 809 | } |
cc248aae WD |
810 | } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) { |
811 | con->os->nextArg = _free(con->os->nextArg); | |
812 | /*@-usedef@*/ /* FIX: W2DO? */ | |
62402cb1 | 813 | if (longArg) { |
cc248aae WD |
814 | /*@=usedef@*/ |
815 | longArg = expandNextArg(con, longArg); | |
816 | con->os->nextArg = longArg; | |
62402cb1 | 817 | } else if (con->os->nextCharArg) { |
cc248aae WD |
818 | longArg = expandNextArg(con, con->os->nextCharArg); |
819 | con->os->nextArg = longArg; | |
62402cb1 | 820 | con->os->nextCharArg = NULL; |
b348deae MP |
821 | } else { |
822 | while (con->os->next == con->os->argc && | |
823 | con->os > con->optionStack) { | |
824 | cleanOSE(con->os--); | |
825 | } | |
cc248aae WD |
826 | 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 { | |
833 | ||
834 | /* | |
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 | } | |
b348deae | 843 | |
cc248aae WD |
844 | 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 | } | |
62402cb1 | 851 | } |
cc248aae | 852 | longArg = NULL; |
62402cb1 MP |
853 | |
854 | if (opt->arg) { | |
855 | switch (opt->argInfo & POPT_ARG_MASK) { | |
cc248aae | 856 | case POPT_ARG_STRING: |
b348deae | 857 | /* XXX memory leak, hard to plug */ |
cc248aae WD |
858 | *((const char **) opt->arg) = (con->os->nextArg) |
859 | ? xstrdup(con->os->nextArg) : NULL; | |
860 | /*@switchbreak@*/ break; | |
861 | ||
862 | case POPT_ARG_INT: | |
863 | case POPT_ARG_LONG: | |
864 | { long aLong = 0; | |
865 | char *end; | |
866 | ||
867 | if (con->os->nextArg) { | |
868 | aLong = strtol(con->os->nextArg, &end, 0); | |
869 | if (!(end && *end == '\0')) | |
870 | return POPT_ERROR_BADNUMBER; | |
871 | } | |
62402cb1 | 872 | |
62402cb1 | 873 | if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) { |
cc248aae WD |
874 | if (aLong == LONG_MIN || aLong == LONG_MAX) |
875 | return POPT_ERROR_OVERFLOW; | |
876 | if (poptSaveLong(opt, aLong)) | |
877 | return POPT_ERROR_BADOPERATION; | |
62402cb1 MP |
878 | } else { |
879 | if (aLong > INT_MAX || aLong < INT_MIN) | |
880 | return POPT_ERROR_OVERFLOW; | |
cc248aae WD |
881 | if (poptSaveInt(opt, aLong)) |
882 | return POPT_ERROR_BADOPERATION; | |
883 | } | |
884 | } /*@switchbreak@*/ break; | |
885 | ||
886 | 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; | |
62402cb1 | 902 | } |
62402cb1 | 903 | |
cc248aae WD |
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)); | |
b348deae | 919 | exit(EXIT_FAILURE); |
cc248aae | 920 | /*@notreached@*/ /*@switchbreak@*/ break; |
62402cb1 MP |
921 | } |
922 | } | |
923 | } | |
924 | ||
cc248aae WD |
925 | 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)) | |
62402cb1 MP |
930 | 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 | ||
cc248aae WD |
938 | 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; | |
b348deae | 950 | } |
62402cb1 | 951 | |
cc248aae WD |
952 | 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@*/ | |
b348deae | 962 | } |
62402cb1 MP |
963 | } |
964 | ||
cc248aae | 965 | return (opt ? opt->val : -1); /* XXX can't happen */ |
62402cb1 MP |
966 | } |
967 | ||
cc248aae WD |
968 | const 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@*/ | |
62402cb1 MP |
977 | return ret; |
978 | } | |
979 | ||
cc248aae WD |
980 | const 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; | |
62402cb1 MP |
986 | } |
987 | ||
cc248aae WD |
988 | const 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; | |
62402cb1 MP |
994 | } |
995 | ||
cc248aae WD |
996 | const char ** poptGetArgs(poptContext con) |
997 | { | |
998 | if (con == NULL || | |
999 | con->leftovers == NULL || con->numLeftovers == con->nextLeftover) | |
1000 | return NULL; | |
62402cb1 MP |
1001 | |
1002 | /* some apps like [like RPM ;-) ] need this NULL terminated */ | |
1003 | con->leftovers[con->numLeftovers] = NULL; | |
1004 | ||
cc248aae | 1005 | /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */ |
62402cb1 | 1006 | return (con->leftovers + con->nextLeftover); |
cc248aae | 1007 | /*@=nullret =nullstate @*/ |
62402cb1 MP |
1008 | } |
1009 | ||
cc248aae WD |
1010 | poptContext poptFreeContext(poptContext con) |
1011 | { | |
1012 | poptItem item; | |
62402cb1 MP |
1013 | int i; |
1014 | ||
cc248aae | 1015 | if (con == NULL) return con; |
b348deae | 1016 | poptResetContext(con); |
cc248aae | 1017 | con->os->argb = _free(con->os->argb); |
b348deae | 1018 | |
cc248aae | 1019 | if (con->aliases != NULL) |
62402cb1 | 1020 | for (i = 0; i < con->numAliases; i++) { |
cc248aae WD |
1021 | 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); | |
62402cb1 | 1028 | } |
cc248aae | 1029 | con->aliases = _free(con->aliases); |
62402cb1 | 1030 | |
cc248aae | 1031 | if (con->execs != NULL) |
62402cb1 | 1032 | for (i = 0; i < con->numExecs; i++) { |
cc248aae WD |
1033 | 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); | |
62402cb1 | 1040 | } |
cc248aae WD |
1041 | con->execs = _free(con->execs); |
1042 | ||
1043 | 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); | |
b348deae | 1049 | |
cc248aae WD |
1050 | con = _free(con); |
1051 | return con; | |
62402cb1 MP |
1052 | } |
1053 | ||
cc248aae | 1054 | int poptAddAlias(poptContext con, struct poptAlias alias, |
b348deae MP |
1055 | /*@unused@*/ int flags) |
1056 | { | |
cc248aae WD |
1057 | poptItem item = alloca(sizeof(*item)); |
1058 | 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 | } | |
62402cb1 | 1070 | |
cc248aae WD |
1071 | /*@-mustmod@*/ /* LCL: con not modified? */ |
1072 | int poptAddItem(poptContext con, poptItem newItem, int flags) | |
1073 | { | |
1074 | poptItem * items, item; | |
1075 | int * nitems; | |
1076 | ||
1077 | 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); | |
b348deae | 1096 | |
cc248aae WD |
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)++; | |
62402cb1 MP |
1111 | |
1112 | return 0; | |
1113 | } | |
cc248aae | 1114 | /*@=mustmod@*/ |
62402cb1 | 1115 | |
cc248aae WD |
1116 | const char * poptBadOption(poptContext con, int flags) |
1117 | { | |
1118 | struct optionStackEntry * os = NULL; | |
62402cb1 | 1119 | |
cc248aae WD |
1120 | if (con != NULL) |
1121 | os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os; | |
62402cb1 | 1122 | |
cc248aae WD |
1123 | /*@-nullderef@*/ /* LCL: os->argv != NULL */ |
1124 | return (os && os->argv ? os->argv[os->next - 1] : NULL); | |
1125 | /*@=nullderef@*/ | |
62402cb1 MP |
1126 | } |
1127 | ||
cc248aae WD |
1128 | const char *const poptStrerror(const int error) |
1129 | { | |
62402cb1 MP |
1130 | switch (error) { |
1131 | case POPT_ERROR_NOARG: | |
1132 | return POPT_("missing argument"); | |
1133 | case POPT_ERROR_BADOPT: | |
1134 | return POPT_("unknown option"); | |
cc248aae WD |
1135 | 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"); | |
62402cb1 MP |
1139 | case POPT_ERROR_OPTSTOODEEP: |
1140 | return POPT_("aliases nested too deeply"); | |
1141 | case POPT_ERROR_BADQUOTE: | |
cc248aae | 1142 | return POPT_("error in parameter quoting"); |
62402cb1 MP |
1143 | case POPT_ERROR_BADNUMBER: |
1144 | return POPT_("invalid numeric value"); | |
1145 | case POPT_ERROR_OVERFLOW: | |
1146 | return POPT_("number too large or too small"); | |
cc248aae WD |
1147 | case POPT_ERROR_MALLOC: |
1148 | return POPT_("memory allocation failed"); | |
62402cb1 MP |
1149 | case POPT_ERROR_ERRNO: |
1150 | return strerror(errno); | |
1151 | default: | |
1152 | return POPT_("unknown error"); | |
1153 | } | |
1154 | } | |
1155 | ||
cc248aae WD |
1156 | int poptStuffArgs(poptContext con, const char ** argv) |
1157 | { | |
b348deae | 1158 | int argc; |
cc248aae | 1159 | int rc; |
62402cb1 MP |
1160 | |
1161 | if ((con->os - con->optionStack) == POPT_OPTION_DEPTH) | |
1162 | return POPT_ERROR_OPTSTOODEEP; | |
1163 | ||
b348deae | 1164 | for (argc = 0; argv[argc]; argc++) |
cc248aae | 1165 | {}; |
62402cb1 MP |
1166 | |
1167 | con->os++; | |
1168 | con->os->next = 0; | |
b348deae MP |
1169 | con->os->nextArg = NULL; |
1170 | con->os->nextCharArg = NULL; | |
62402cb1 | 1171 | con->os->currAlias = NULL; |
cc248aae | 1172 | rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv); |
b348deae | 1173 | con->os->argb = NULL; |
62402cb1 MP |
1174 | con->os->stuffed = 1; |
1175 | ||
cc248aae | 1176 | return rc; |
62402cb1 MP |
1177 | } |
1178 | ||
cc248aae WD |
1179 | const char * poptGetInvocationName(poptContext con) |
1180 | { | |
1181 | return (con->os->argv ? con->os->argv[0] : ""); | |
62402cb1 | 1182 | } |
b348deae | 1183 | |
cc248aae | 1184 | int poptStrippedArgv(poptContext con, int argc, char ** argv) |
b348deae | 1185 | { |
cc248aae WD |
1186 | int numargs = argc; |
1187 | int j = 1; | |
1188 | int i; | |
b348deae | 1189 | |
cc248aae WD |
1190 | /*@-sizeoftype@*/ |
1191 | if (con->arg_strip) | |
1192 | for (i = 1; i < argc; i++) { | |
1193 | if (PBM_ISSET(i, con->arg_strip)) | |
b348deae | 1194 | numargs--; |
b348deae MP |
1195 | } |
1196 | ||
cc248aae WD |
1197 | for (i = 1; i < argc; i++) { |
1198 | if (con->arg_strip && PBM_ISSET(i, con->arg_strip)) | |
b348deae | 1199 | continue; |
cc248aae WD |
1200 | argv[j] = (j < numargs) ? argv[i] : NULL; |
1201 | j++; | |
b348deae | 1202 | } |
cc248aae | 1203 | /*@=sizeoftype@*/ |
b348deae | 1204 | |
cc248aae | 1205 | return numargs; |
b348deae | 1206 | } |