Added RERR_VANISHED.
[rsync/rsync.git] / popt / popt.c
CommitLineData
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
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
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
47void 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
57static 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
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. */
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
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;
62402cb1 144 }
62402cb1
MP
145 }
146}
147
b348deae 148poptContext 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
195static 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
205void 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. */
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;
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
297static 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
349static 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 *
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 */
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
500static 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
537static /*@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 @*/
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
586static 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
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;
b348deae
MP
656}
657
62402cb1 658/* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
b348deae
MP
659int 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 {
75fb17b8
WD
907#ifndef DBL_EPSILON
908#define DBL_EPSILON 2.2204460492503131e-16
909#endif
cc248aae
WD
910#define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
911 if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
912 return POPT_ERROR_OVERFLOW;
913 if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
914 return POPT_ERROR_OVERFLOW;
915 *((float *) opt->arg) = aDouble;
916 }
917 } /*@switchbreak@*/ break;
918 default:
919 fprintf(stdout,
920 POPT_("option type (%d) not implemented in popt\n"),
921 (opt->argInfo & POPT_ARG_MASK));
b348deae 922 exit(EXIT_FAILURE);
cc248aae 923 /*@notreached@*/ /*@switchbreak@*/ break;
62402cb1
MP
924 }
925 }
926 }
927
cc248aae
WD
928 if (cb) {
929 /*@-internalglobs@*/
930 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
931 /*@=internalglobs@*/
932 } else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
62402cb1
MP
933 done = 1;
934
935 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
936 con->finalArgvAlloced += 10;
937 con->finalArgv = realloc(con->finalArgv,
938 sizeof(*con->finalArgv) * con->finalArgvAlloced);
939 }
940
cc248aae
WD
941 if (con->finalArgv != NULL)
942 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
943 if (s != NULL) { /* XXX can't happen */
944 if (opt->longName)
945 sprintf(s, "%s%s",
946 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
947 opt->longName);
948 else
949 sprintf(s, "-%c", opt->shortName);
950 con->finalArgv[con->finalArgvCount++] = s;
951 } else
952 con->finalArgv[con->finalArgvCount++] = NULL;
b348deae 953 }
62402cb1 954
cc248aae
WD
955 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
956 /*@-ifempty@*/ ; /*@=ifempty@*/
957 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
958 /*@-ifempty@*/ ; /*@=ifempty@*/
959 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
960 if (con->finalArgv != NULL && con->os->nextArg)
961 con->finalArgv[con->finalArgvCount++] =
962 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
963 xstrdup(con->os->nextArg);
964 /*@=nullpass@*/
b348deae 965 }
62402cb1
MP
966 }
967
cc248aae 968 return (opt ? opt->val : -1); /* XXX can't happen */
62402cb1
MP
969}
970
cc248aae
WD
971const char * poptGetOptArg(poptContext con)
972{
973 const char * ret = NULL;
974 /*@-branchstate@*/
975 if (con) {
976 ret = con->os->nextArg;
977 con->os->nextArg = NULL;
978 }
979 /*@=branchstate@*/
62402cb1
MP
980 return ret;
981}
982
cc248aae
WD
983const char * poptGetArg(poptContext con)
984{
985 const char * ret = NULL;
986 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
987 ret = con->leftovers[con->nextLeftover++];
988 return ret;
62402cb1
MP
989}
990
cc248aae
WD
991const char * poptPeekArg(poptContext con)
992{
993 const char * ret = NULL;
994 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
995 ret = con->leftovers[con->nextLeftover];
996 return ret;
62402cb1
MP
997}
998
cc248aae
WD
999const char ** poptGetArgs(poptContext con)
1000{
1001 if (con == NULL ||
1002 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1003 return NULL;
62402cb1
MP
1004
1005 /* some apps like [like RPM ;-) ] need this NULL terminated */
1006 con->leftovers[con->numLeftovers] = NULL;
1007
cc248aae 1008 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
62402cb1 1009 return (con->leftovers + con->nextLeftover);
cc248aae 1010 /*@=nullret =nullstate @*/
62402cb1
MP
1011}
1012
cc248aae
WD
1013poptContext poptFreeContext(poptContext con)
1014{
1015 poptItem item;
62402cb1
MP
1016 int i;
1017
cc248aae 1018 if (con == NULL) return con;
b348deae 1019 poptResetContext(con);
cc248aae 1020 con->os->argb = _free(con->os->argb);
b348deae 1021
cc248aae 1022 if (con->aliases != NULL)
62402cb1 1023 for (i = 0; i < con->numAliases; i++) {
cc248aae
WD
1024 item = con->aliases + i;
1025 /*@-modobserver -observertrans -dependenttrans@*/
1026 item->option.longName = _free(item->option.longName);
1027 item->option.descrip = _free(item->option.descrip);
1028 item->option.argDescrip = _free(item->option.argDescrip);
1029 /*@=modobserver =observertrans =dependenttrans@*/
1030 item->argv = _free(item->argv);
62402cb1 1031 }
cc248aae 1032 con->aliases = _free(con->aliases);
62402cb1 1033
cc248aae 1034 if (con->execs != NULL)
62402cb1 1035 for (i = 0; i < con->numExecs; i++) {
cc248aae
WD
1036 item = con->execs + i;
1037 /*@-modobserver -observertrans -dependenttrans@*/
1038 item->option.longName = _free(item->option.longName);
1039 item->option.descrip = _free(item->option.descrip);
1040 item->option.argDescrip = _free(item->option.argDescrip);
1041 /*@=modobserver =observertrans =dependenttrans@*/
1042 item->argv = _free(item->argv);
62402cb1 1043 }
cc248aae
WD
1044 con->execs = _free(con->execs);
1045
1046 con->leftovers = _free(con->leftovers);
1047 con->finalArgv = _free(con->finalArgv);
1048 con->appName = _free(con->appName);
1049 con->otherHelp = _free(con->otherHelp);
1050 con->execPath = _free(con->execPath);
1051 con->arg_strip = PBM_FREE(con->arg_strip);
b348deae 1052
cc248aae
WD
1053 con = _free(con);
1054 return con;
62402cb1
MP
1055}
1056
cc248aae 1057int poptAddAlias(poptContext con, struct poptAlias alias,
b348deae
MP
1058 /*@unused@*/ int flags)
1059{
451b5fc9 1060 poptItem item = (poptItem) alloca(sizeof(*item));
cc248aae
WD
1061 memset(item, 0, sizeof(*item));
1062 item->option.longName = alias.longName;
1063 item->option.shortName = alias.shortName;
1064 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1065 item->option.arg = 0;
1066 item->option.val = 0;
1067 item->option.descrip = NULL;
1068 item->option.argDescrip = NULL;
1069 item->argc = alias.argc;
1070 item->argv = alias.argv;
1071 return poptAddItem(con, item, 0);
1072}
62402cb1 1073
cc248aae
WD
1074/*@-mustmod@*/ /* LCL: con not modified? */
1075int poptAddItem(poptContext con, poptItem newItem, int flags)
1076{
1077 poptItem * items, item;
1078 int * nitems;
1079
1080 switch (flags) {
1081 case 1:
1082 items = &con->execs;
1083 nitems = &con->numExecs;
1084 break;
1085 case 0:
1086 items = &con->aliases;
1087 nitems = &con->numAliases;
1088 break;
1089 default:
1090 return 1;
1091 /*@notreached@*/ break;
1092 }
1093
1094 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1095 if ((*items) == NULL)
1096 return 1;
1097
1098 item = (*items) + (*nitems);
b348deae 1099
cc248aae
WD
1100 item->option.longName =
1101 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1102 item->option.shortName = newItem->option.shortName;
1103 item->option.argInfo = newItem->option.argInfo;
1104 item->option.arg = newItem->option.arg;
1105 item->option.val = newItem->option.val;
1106 item->option.descrip =
1107 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1108 item->option.argDescrip =
1109 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1110 item->argc = newItem->argc;
1111 item->argv = newItem->argv;
1112
1113 (*nitems)++;
62402cb1
MP
1114
1115 return 0;
1116}
cc248aae 1117/*@=mustmod@*/
62402cb1 1118
cc248aae
WD
1119const char * poptBadOption(poptContext con, int flags)
1120{
1121 struct optionStackEntry * os = NULL;
62402cb1 1122
cc248aae
WD
1123 if (con != NULL)
1124 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
62402cb1 1125
cc248aae
WD
1126 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1127 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1128 /*@=nullderef@*/
62402cb1
MP
1129}
1130
cc248aae
WD
1131const char *const poptStrerror(const int error)
1132{
62402cb1
MP
1133 switch (error) {
1134 case POPT_ERROR_NOARG:
1135 return POPT_("missing argument");
1136 case POPT_ERROR_BADOPT:
1137 return POPT_("unknown option");
cc248aae
WD
1138 case POPT_ERROR_BADOPERATION:
1139 return POPT_("mutually exclusive logical operations requested");
1140 case POPT_ERROR_NULLARG:
1141 return POPT_("opt->arg should not be NULL");
62402cb1
MP
1142 case POPT_ERROR_OPTSTOODEEP:
1143 return POPT_("aliases nested too deeply");
1144 case POPT_ERROR_BADQUOTE:
cc248aae 1145 return POPT_("error in parameter quoting");
62402cb1
MP
1146 case POPT_ERROR_BADNUMBER:
1147 return POPT_("invalid numeric value");
1148 case POPT_ERROR_OVERFLOW:
1149 return POPT_("number too large or too small");
cc248aae
WD
1150 case POPT_ERROR_MALLOC:
1151 return POPT_("memory allocation failed");
62402cb1
MP
1152 case POPT_ERROR_ERRNO:
1153 return strerror(errno);
1154 default:
1155 return POPT_("unknown error");
1156 }
1157}
1158
cc248aae
WD
1159int poptStuffArgs(poptContext con, const char ** argv)
1160{
b348deae 1161 int argc;
cc248aae 1162 int rc;
62402cb1
MP
1163
1164 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1165 return POPT_ERROR_OPTSTOODEEP;
1166
b348deae 1167 for (argc = 0; argv[argc]; argc++)
cc248aae 1168 {};
62402cb1
MP
1169
1170 con->os++;
1171 con->os->next = 0;
b348deae
MP
1172 con->os->nextArg = NULL;
1173 con->os->nextCharArg = NULL;
62402cb1 1174 con->os->currAlias = NULL;
cc248aae 1175 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
b348deae 1176 con->os->argb = NULL;
62402cb1
MP
1177 con->os->stuffed = 1;
1178
cc248aae 1179 return rc;
62402cb1
MP
1180}
1181
cc248aae
WD
1182const char * poptGetInvocationName(poptContext con)
1183{
1184 return (con->os->argv ? con->os->argv[0] : "");
62402cb1 1185}
b348deae 1186
cc248aae 1187int poptStrippedArgv(poptContext con, int argc, char ** argv)
b348deae 1188{
cc248aae
WD
1189 int numargs = argc;
1190 int j = 1;
1191 int i;
b348deae 1192
cc248aae
WD
1193 /*@-sizeoftype@*/
1194 if (con->arg_strip)
1195 for (i = 1; i < argc; i++) {
1196 if (PBM_ISSET(i, con->arg_strip))
b348deae 1197 numargs--;
b348deae
MP
1198 }
1199
cc248aae
WD
1200 for (i = 1; i < argc; i++) {
1201 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
b348deae 1202 continue;
cc248aae
WD
1203 argv[j] = (j < numargs) ? argv[i] : NULL;
1204 j++;
b348deae 1205 }
cc248aae 1206 /*@=sizeoftype@*/
b348deae 1207
cc248aae 1208 return numargs;
b348deae 1209}