blob: fcf671caf941bd4fcd6a9be02317e7ee9b80157f [file] [log] [blame]
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001/** \ingroup popt
2 * \file popt/popt.c
3 */
4
Wayne Davisonbc93ee82006-11-09 00:57:55 +00005/* (C) 1998-2002 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
Wayne Davison684576f2006-11-09 01:51:57 +000021#ifndef DBL_EPSILON
22#define DBL_EPSILON 2.2204460492503131e-16
23#endif
24
Wayne Davisonbc93ee82006-11-09 00:57:55 +000025#ifdef MYDEBUG
26/*@unchecked@*/
27int _popt_debug = 0;
28#endif
29
30#if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
31static char * strerror(int errno)
32{
Martin Poolb348dea2001-02-24 01:32:22 +000033 extern int sys_nerr;
34 extern char * sys_errlist[];
35
36 if ((0 <= errno) && (errno < sys_nerr))
37 return sys_errlist[errno];
38 else
39 return POPT_("unknown errno");
40}
41#endif
42
Wayne Davisoncc248aa2002-07-27 18:32:25 +000043#ifdef MYDEBUG
Wayne Davisonbc93ee82006-11-09 00:57:55 +000044/*@unused@*/
45static void prtcon(const char *msg, poptContext con)
Wayne Davisoncc248aa2002-07-27 18:32:25 +000046{
47 if (msg) fprintf(stderr, "%s", msg);
48 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
49 con, con->os,
50 (con->os->nextCharArg ? con->os->nextCharArg : ""),
51 (con->os->nextArg ? con->os->nextArg : ""),
52 con->os->next,
53 (con->os->argv && con->os->argv[con->os->next]
54 ? con->os->argv[con->os->next] : ""));
55}
56#endif
57
58void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
59{
60 con->execPath = _free(con->execPath);
Martin Poolb348dea2001-02-24 01:32:22 +000061 con->execPath = xstrdup(path);
62 con->execAbsolute = allowAbsolute;
Wayne Davisonbc93ee82006-11-09 00:57:55 +000063 /*@-nullstate@*/ /* LCL: con->execPath not NULL */
Wayne Davisoncc248aa2002-07-27 18:32:25 +000064 return;
65 /*@=nullstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +000066}
67
Wayne Davisoncc248aa2002-07-27 18:32:25 +000068static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
69 /*@globals internalState@*/
70 /*@modifies internalState@*/
71{
72 if (opt != NULL)
73 for (; opt->longName || opt->shortName || opt->arg; opt++) {
74 if (opt->arg == NULL) continue; /* XXX program error. */
Martin Poolb348dea2001-02-24 01:32:22 +000075 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
Wayne Davisonbc93ee82006-11-09 00:57:55 +000076 void * arg = opt->arg;
77/*@-branchstate@*/
78 /* XXX sick hack to preserve pretense of ABI. */
79 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
80/*@=branchstate@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +000081 /* Recurse on included sub-tables. */
Wayne Davisonbc93ee82006-11-09 00:57:55 +000082 invokeCallbacksPRE(con, arg);
Wayne Davisoncc248aa2002-07-27 18:32:25 +000083 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
84 (opt->argInfo & POPT_CBFLAG_PRE))
85 { /*@-castfcnptr@*/
86 poptCallbackType cb = (poptCallbackType)opt->arg;
87 /*@=castfcnptr@*/
88 /* Perform callback. */
Wayne Davisonbc93ee82006-11-09 00:57:55 +000089 /*@-noeffectuncon @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +000090 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
Wayne Davisonbc93ee82006-11-09 00:57:55 +000091 /*@=noeffectuncon @*/
Martin Poolb348dea2001-02-24 01:32:22 +000092 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +000093 }
94}
95
96static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
97 /*@globals internalState@*/
98 /*@modifies internalState@*/
99{
100 if (opt != NULL)
101 for (; opt->longName || opt->shortName || opt->arg; opt++) {
102 if (opt->arg == NULL) continue; /* XXX program error. */
103 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000104 void * arg = opt->arg;
105/*@-branchstate@*/
106 /* XXX sick hack to preserve pretense of ABI. */
107 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
108/*@=branchstate@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000109 /* Recurse on included sub-tables. */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000110 invokeCallbacksPOST(con, arg);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000111 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
112 (opt->argInfo & POPT_CBFLAG_POST))
113 { /*@-castfcnptr@*/
114 poptCallbackType cb = (poptCallbackType)opt->arg;
115 /*@=castfcnptr@*/
116 /* Perform callback. */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000117 /*@-noeffectuncon @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000118 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000119 /*@=noeffectuncon @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000120 }
121 }
122}
123
124static void invokeCallbacksOPTION(poptContext con,
125 const struct poptOption * opt,
126 const struct poptOption * myOpt,
127 /*@null@*/ const void * myData, int shorty)
128 /*@globals internalState@*/
129 /*@modifies internalState@*/
130{
131 const struct poptOption * cbopt = NULL;
132
133 if (opt != NULL)
134 for (; opt->longName || opt->shortName || opt->arg; opt++) {
135 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000136 void * arg = opt->arg;
137/*@-branchstate@*/
138 /* XXX sick hack to preserve pretense of ABI. */
139 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
140/*@=branchstate@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000141 /* Recurse on included sub-tables. */
142 if (opt->arg != NULL) /* XXX program error */
143 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
144 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
145 !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
146 /* Save callback info. */
147 cbopt = opt;
148 } else if (cbopt != NULL &&
149 ((myOpt->shortName && opt->shortName && shorty &&
150 myOpt->shortName == opt->shortName) ||
151 (myOpt->longName && opt->longName &&
152 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
153 !strcmp(myOpt->longName, opt->longName)))
154 /*@=nullpass@*/
155 )
156 { /*@-castfcnptr@*/
157 poptCallbackType cb = (poptCallbackType)cbopt->arg;
158 /*@=castfcnptr@*/
159 const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
160 /* Perform callback. */
161 if (cb != NULL) { /* XXX program error */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000162 /*@-noeffectuncon @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000163 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
164 con->os->nextArg, cbData);
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000165 /*@=noeffectuncon @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000166 }
167 /* Terminate (unless explcitly continuing). */
168 if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
169 return;
170 }
Martin Poolb348dea2001-02-24 01:32:22 +0000171 }
172}
173
174poptContext poptGetContext(const char * name, int argc, const char ** argv,
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000175 const struct poptOption * options, int flags)
176{
Martin Poolb348dea2001-02-24 01:32:22 +0000177 poptContext con = malloc(sizeof(*con));
178
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000179 if (con == NULL) return NULL; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000180 memset(con, 0, sizeof(*con));
181
182 con->os = con->optionStack;
183 con->os->argc = argc;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000184 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000185 con->os->argv = argv;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000186 /*@=dependenttrans =assignexpose@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000187 con->os->argb = NULL;
188
189 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
190 con->os->next = 1; /* skip argv[0] */
191
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000192 con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
193 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000194 con->options = options;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000195 /*@=dependenttrans =assignexpose@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000196 con->aliases = NULL;
197 con->numAliases = 0;
198 con->flags = flags;
199 con->execs = NULL;
200 con->numExecs = 0;
201 con->finalArgvAlloced = argc * 2;
202 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
203 con->execAbsolute = 1;
204 con->arg_strip = NULL;
205
206 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
207 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
208
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000209 if (name) {
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000210 size_t bufsize = strlen(name) + 1;
211 char * t = malloc(bufsize);
212 if (t) {
213 strlcpy(t, name, bufsize);
214 con->appName = t;
215 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000216 }
Martin Poolb348dea2001-02-24 01:32:22 +0000217
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000218 /*@-internalglobs@*/
219 invokeCallbacksPRE(con, con->options);
220 /*@=internalglobs@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000221
222 return con;
223}
224
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000225static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
226 /*@uses os @*/
227 /*@releases os->nextArg, os->argv, os->argb @*/
228 /*@modifies os @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000229{
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000230 os->nextArg = _free(os->nextArg);
231 os->argv = _free(os->argv);
232 os->argb = PBM_FREE(os->argb);
Martin Poolb348dea2001-02-24 01:32:22 +0000233}
234
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000235/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000236void poptResetContext(poptContext con)
237{
Martin Poolb348dea2001-02-24 01:32:22 +0000238 int i;
239
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000240 if (con == NULL) return;
Martin Poolb348dea2001-02-24 01:32:22 +0000241 while (con->os > con->optionStack) {
242 cleanOSE(con->os--);
243 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000244 con->os->argb = PBM_FREE(con->os->argb);
Martin Poolb348dea2001-02-24 01:32:22 +0000245 con->os->currAlias = NULL;
246 con->os->nextCharArg = NULL;
247 con->os->nextArg = NULL;
248 con->os->next = 1; /* skip argv[0] */
249
250 con->numLeftovers = 0;
251 con->nextLeftover = 0;
252 con->restLeftover = 0;
253 con->doExec = NULL;
254
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000255 if (con->finalArgv != NULL)
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000256 for (i = 0; i < con->finalArgvCount; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000257 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
258 con->finalArgv[i] = _free(con->finalArgv[i]);
259 /*@=unqualifiedtrans@*/
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000260 }
Martin Poolb348dea2001-02-24 01:32:22 +0000261
262 con->finalArgvCount = 0;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000263 con->arg_strip = PBM_FREE(con->arg_strip);
264 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
265 return;
266 /*@=nullstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000267}
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000268/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000269
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000270/* Only one of longName, shortName should be set, not both. */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000271/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000272static int handleExec(/*@special@*/ poptContext con,
273 /*@null@*/ const char * longName, char shortName)
274 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
275 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
276 /*@modifies con @*/
277{
278 poptItem item;
Martin Poolb348dea2001-02-24 01:32:22 +0000279 int i;
280
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000281 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
282 return 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000283
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000284 for (i = con->numExecs - 1; i >= 0; i--) {
285 item = con->execs + i;
286 if (longName && !(item->option.longName &&
287 !strcmp(longName, item->option.longName)))
288 continue;
289 else if (shortName != item->option.shortName)
290 continue;
291 break;
292 }
Martin Poolb348dea2001-02-24 01:32:22 +0000293 if (i < 0) return 0;
294
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000295
Martin Poolb348dea2001-02-24 01:32:22 +0000296 if (con->flags & POPT_CONTEXT_NO_EXEC)
297 return 1;
298
299 if (con->doExec == NULL) {
300 con->doExec = con->execs + i;
301 return 1;
302 }
303
304 /* We already have an exec to do; remember this option for next
305 time 'round */
306 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
307 con->finalArgvAlloced += 10;
308 con->finalArgv = realloc(con->finalArgv,
309 sizeof(*con->finalArgv) * con->finalArgvAlloced);
310 }
311
312 i = con->finalArgvCount++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000313 if (con->finalArgv != NULL) /* XXX can't happen */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000314 { size_t bufsize = (longName ? strlen(longName) : 0) + 3;
315 char *s = malloc(bufsize);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000316 if (s != NULL) { /* XXX can't happen */
317 if (longName)
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000318 snprintf(s, bufsize, "--%s", longName);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000319 else
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000320 snprintf(s, bufsize, "-%c", shortName);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000321 con->finalArgv[i] = s;
322 } else
323 con->finalArgv[i] = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000324 }
325
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000326 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
Martin Poolb348dea2001-02-24 01:32:22 +0000327 return 1;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000328 /*@=nullstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000329}
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000330/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000331
332/* Only one of longName, shortName may be set at a time */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000333static int handleAlias(/*@special@*/ poptContext con,
334 /*@null@*/ const char * longName, char shortName,
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000335 /*@exposed@*/ /*@null@*/ const char * nextCharArg)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000336 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
337 con->os->currAlias, con->os->currAlias->option.longName @*/
338 /*@modifies con @*/
339{
340 poptItem item = con->os->currAlias;
341 int rc;
Martin Poolb348dea2001-02-24 01:32:22 +0000342 int i;
343
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000344 if (item) {
345 if (longName && (item->option.longName &&
346 !strcmp(longName, item->option.longName)))
347 return 0;
348 if (shortName && shortName == item->option.shortName)
349 return 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000350 }
351
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000352 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
353 return 0;
354
355 for (i = con->numAliases - 1; i >= 0; i--) {
356 item = con->aliases + i;
357 if (longName && !(item->option.longName &&
358 !strcmp(longName, item->option.longName)))
359 continue;
360 else if (shortName != item->option.shortName)
361 continue;
362 break;
363 }
Martin Poolb348dea2001-02-24 01:32:22 +0000364 if (i < 0) return 0;
365
366 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
367 return POPT_ERROR_OPTSTOODEEP;
368
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000369/*@-boundsread@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000370 if (nextCharArg && *nextCharArg)
371 con->os->nextCharArg = nextCharArg;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000372/*@=boundsread@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000373
374 con->os++;
375 con->os->next = 0;
376 con->os->stuffed = 0;
377 con->os->nextArg = NULL;
378 con->os->nextCharArg = NULL;
379 con->os->currAlias = con->aliases + i;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000380 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
Martin Poolb348dea2001-02-24 01:32:22 +0000381 &con->os->argc, &con->os->argv);
382 con->os->argb = NULL;
383
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000384 return (rc ? rc : 1);
Martin Poolb348dea2001-02-24 01:32:22 +0000385}
386
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000387/*@-bounds -boundswrite @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000388static int execCommand(poptContext con)
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000389 /*@globals internalState @*/
390 /*@modifies internalState @*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000391{
392 poptItem item = con->doExec;
Martin Poolb348dea2001-02-24 01:32:22 +0000393 const char ** argv;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000394 int argc = 0;
395 int rc;
396
397 if (item == NULL) /*XXX can't happen*/
398 return POPT_ERROR_NOARG;
399
400 if (item->argv == NULL || item->argc < 1 ||
401 (!con->execAbsolute && strchr(item->argv[0], '/')))
402 return POPT_ERROR_NOARG;
Martin Poolb348dea2001-02-24 01:32:22 +0000403
404 argv = malloc(sizeof(*argv) *
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000405 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000406 if (argv == NULL) return POPT_ERROR_MALLOC;
Martin Poolb348dea2001-02-24 01:32:22 +0000407
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000408 if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
409 size_t bufsize = strlen(con->execPath) + strlen(item->argv[0]) + sizeof "/";
410 char *s = alloca(bufsize);
411 snprintf(s, bufsize, "%s/%s", con->execPath, item->argv[0]);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000412 argv[argc] = s;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000413 } else
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000414 argv[argc] = findProgramPath(item->argv[0]);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000415 if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
Martin Poolb348dea2001-02-24 01:32:22 +0000416
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000417 if (item->argc > 1) {
418 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
419 argc += (item->argc - 1);
Martin Poolb348dea2001-02-24 01:32:22 +0000420 }
421
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000422 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
423 memcpy(argv + argc, con->finalArgv,
424 sizeof(*argv) * con->finalArgvCount);
425 argc += con->finalArgvCount;
426 }
427
428 if (con->leftovers != NULL && con->numLeftovers > 0) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000429 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
430 argc += con->numLeftovers;
431 }
432
433 argv[argc] = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000434
435#ifdef __hpux
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000436 rc = setresgid(getgid(), getgid(),-1);
437 if (rc) return POPT_ERROR_ERRNO;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000438 rc = setresuid(getuid(), getuid(),-1);
439 if (rc) return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000440#else
441/*
442 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
443 * XXX sez' Timur Bakeyev <mc@bat.ru>
444 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
445 */
446#if defined(HAVE_SETUID)
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000447 rc = setgid(getgid());
448 if (rc) return POPT_ERROR_ERRNO;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000449 rc = setuid(getuid());
450 if (rc) return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000451#elif defined (HAVE_SETREUID)
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000452 rc = setregid(getgid(), getgid());
453 if (rc) return POPT_ERROR_ERRNO;
454 rc = setreuid(getuid(), getuid());
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000455 if (rc) return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000456#else
457 ; /* Can't drop privileges */
458#endif
459#endif
460
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000461 if (argv[0] == NULL)
462 return POPT_ERROR_NOARG;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000463
464#ifdef MYDEBUG
465if (_popt_debug)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000466 { const char ** avp;
467 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
468 for (avp = argv; *avp; avp++)
469 fprintf(stderr, " '%s'", *avp);
470 fprintf(stderr, "\n");
471 }
472#endif
473
474 rc = execvp(argv[0], (char *const *)argv);
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000475
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000476 return POPT_ERROR_ERRNO;
Martin Poolb348dea2001-02-24 01:32:22 +0000477}
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000478/*@=bounds =boundswrite @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000479
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000480/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000481/*@observer@*/ /*@null@*/ static const struct poptOption *
482findOption(const struct poptOption * opt, /*@null@*/ const char * longName,
483 char shortName,
484 /*@null@*/ /*@out@*/ poptCallbackType * callback,
485 /*@null@*/ /*@out@*/ const void ** callbackData,
486 int singleDash)
487 /*@modifies *callback, *callbackData */
Martin Poolb348dea2001-02-24 01:32:22 +0000488{
Martin Poolb348dea2001-02-24 01:32:22 +0000489 const struct poptOption * cb = NULL;
490
491 /* This happens when a single - is given */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000492 if (singleDash && !shortName && (longName && *longName == '\0'))
Martin Poolb348dea2001-02-24 01:32:22 +0000493 shortName = '-';
494
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000495 for (; opt->longName || opt->shortName || opt->arg; opt++) {
496
Martin Poolb348dea2001-02-24 01:32:22 +0000497 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000498 const struct poptOption * opt2;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000499 void * arg = opt->arg;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000500
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000501/*@-branchstate@*/
502 /* XXX sick hack to preserve pretense of ABI. */
503 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
504/*@=branchstate@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000505 /* Recurse on included sub-tables. */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000506 if (arg == NULL) continue; /* XXX program error */
507 opt2 = findOption(arg, longName, shortName, callback,
Martin Poolb348dea2001-02-24 01:32:22 +0000508 callbackData, singleDash);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000509 if (opt2 == NULL) continue;
510 /* Sub-table data will be inheirited if no data yet. */
511 if (!(callback && *callback)) return opt2;
512 if (!(callbackData && *callbackData == NULL)) return opt2;
513 /*@-observertrans -dependenttrans @*/
514 *callbackData = opt->descrip;
515 /*@=observertrans =dependenttrans @*/
516 return opt2;
Martin Poolb348dea2001-02-24 01:32:22 +0000517 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
518 cb = opt;
519 } else if (longName && opt->longName &&
520 (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000521 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
522 !strcmp(longName, opt->longName))
523 /*@=nullpass@*/
524 {
Martin Poolb348dea2001-02-24 01:32:22 +0000525 break;
526 } else if (shortName && shortName == opt->shortName) {
527 break;
528 }
Martin Poolb348dea2001-02-24 01:32:22 +0000529 }
530
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000531 if (!opt->longName && !opt->shortName)
532 return NULL;
533 /*@-modobserver -mods @*/
534 if (callback) *callback = NULL;
535 if (callbackData) *callbackData = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000536 if (cb) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000537 if (callback)
538 /*@-castfcnptr@*/
539 *callback = (poptCallbackType)cb->arg;
540 /*@=castfcnptr@*/
541 if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
542 if (callbackData)
543 /*@-observertrans@*/ /* FIX: typedef double indirection. */
544 *callbackData = cb->descrip;
545 /*@=observertrans@*/
546 }
Martin Poolb348dea2001-02-24 01:32:22 +0000547 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000548 /*@=modobserver =mods @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000549
550 return opt;
551}
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000552/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000553
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000554static const char * findNextArg(/*@special@*/ poptContext con,
555 unsigned argx, int delete_arg)
556 /*@uses con->optionStack, con->os,
557 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
558 /*@modifies con @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000559{
560 struct optionStackEntry * os = con->os;
561 const char * arg;
562
563 do {
564 int i;
565 arg = NULL;
566 while (os->next == os->argc && os > con->optionStack) os--;
567 if (os->next == os->argc && os == con->optionStack) break;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000568 if (os->argv != NULL)
Martin Poolb348dea2001-02-24 01:32:22 +0000569 for (i = os->next; i < os->argc; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000570 /*@-sizeoftype@*/
571 if (os->argb && PBM_ISSET(i, os->argb))
572 /*@innercontinue@*/ continue;
573 if (*os->argv[i] == '-')
574 /*@innercontinue@*/ continue;
575 if (--argx > 0)
576 /*@innercontinue@*/ continue;
Martin Poolb348dea2001-02-24 01:32:22 +0000577 arg = os->argv[i];
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000578 if (delete_arg) {
Martin Poolb348dea2001-02-24 01:32:22 +0000579 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000580 if (os->argb != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000581 PBM_SET(i, os->argb);
582 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000583 /*@innerbreak@*/ break;
584 /*@=sizeoftype@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000585 }
586 if (os > con->optionStack) os--;
587 } while (arg == NULL);
588 return arg;
589}
590
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000591/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000592static /*@only@*/ /*@null@*/ const char *
593expandNextArg(/*@special@*/ poptContext con, const char * s)
594 /*@uses con->optionStack, con->os,
595 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
596 /*@modifies con @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000597{
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000598 const char * a = NULL;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000599 size_t alen, pos;
Martin Poolb348dea2001-02-24 01:32:22 +0000600 char *t, *te;
601 size_t tn = strlen(s) + 1;
602 char c;
603
604 te = t = malloc(tn);;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000605 if (t == NULL) return NULL; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000606 while ((c = *s++) != '\0') {
607 switch (c) {
608#if 0 /* XXX can't do this */
609 case '\\': /* escape */
610 c = *s++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000611 /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000612#endif
613 case '!':
614 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000615 /*@switchbreak@*/ break;
616 /* XXX Make sure that findNextArg deletes only next arg. */
617 if (a == NULL) {
618 if ((a = findNextArg(con, 1, 1)) == NULL)
619 /*@switchbreak@*/ break;
620 }
Martin Poolb348dea2001-02-24 01:32:22 +0000621 s += 3;
622
623 alen = strlen(a);
624 tn += alen;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000625 pos = te - t;
Martin Poolb348dea2001-02-24 01:32:22 +0000626 t = realloc(t, tn);
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000627 te = t + pos;
Martin Poolb348dea2001-02-24 01:32:22 +0000628 strncpy(te, a, alen); te += alen;
629 continue;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000630 /*@notreached@*/ /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000631 default:
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000632 /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000633 }
634 *te++ = c;
635 }
636 *te = '\0';
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000637 t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
Martin Poolb348dea2001-02-24 01:32:22 +0000638 return t;
639}
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000640/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000641
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000642static void poptStripArg(/*@special@*/ poptContext con, int which)
643 /*@uses con->arg_strip, con->optionStack @*/
644 /*@defines con->arg_strip @*/
645 /*@modifies con @*/
Martin Poolb348dea2001-02-24 01:32:22 +0000646{
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000647 /*@-sizeoftype@*/
648 if (con->arg_strip == NULL)
Martin Poolb348dea2001-02-24 01:32:22 +0000649 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000650 if (con->arg_strip != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000651 PBM_SET(which, con->arg_strip);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000652 /*@=sizeoftype@*/
653 /*@-compdef@*/ /* LCL: con->arg_strip udefined? */
654 return;
655 /*@=compdef@*/
656}
657
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000658int poptSaveLong(long * arg, int argInfo, long aLong)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000659{
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000660 /* XXX Check alignment, may fail on funky platforms. */
661 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000662 return POPT_ERROR_NULLARG;
663
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000664 if (argInfo & POPT_ARGFLAG_NOT)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000665 aLong = ~aLong;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000666 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000667 case 0:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000668 *arg = aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000669 break;
670 case POPT_ARGFLAG_OR:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000671 *arg |= aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000672 break;
673 case POPT_ARGFLAG_AND:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000674 *arg &= aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000675 break;
676 case POPT_ARGFLAG_XOR:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000677 *arg ^= aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000678 break;
679 default:
680 return POPT_ERROR_BADOPERATION;
681 /*@notreached@*/ break;
682 }
683 return 0;
684}
685
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000686int poptSaveInt(/*@null@*/ int * arg, int argInfo, long aLong)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000687{
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000688 /* XXX Check alignment, may fail on funky platforms. */
689 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000690 return POPT_ERROR_NULLARG;
691
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000692 if (argInfo & POPT_ARGFLAG_NOT)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000693 aLong = ~aLong;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000694 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000695 case 0:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000696 *arg = aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000697 break;
698 case POPT_ARGFLAG_OR:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000699 *arg |= aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000700 break;
701 case POPT_ARGFLAG_AND:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000702 *arg &= aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000703 break;
704 case POPT_ARGFLAG_XOR:
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000705 *arg ^= aLong;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000706 break;
707 default:
708 return POPT_ERROR_BADOPERATION;
709 /*@notreached@*/ break;
710 }
711 return 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000712}
713
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000714/*@-boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000715/* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
716int poptGetNextOpt(poptContext con)
717{
718 const struct poptOption * opt = NULL;
719 int done = 0;
720
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000721 if (con == NULL)
722 return -1;
Martin Poolb348dea2001-02-24 01:32:22 +0000723 while (!done) {
724 const char * origOptString = NULL;
725 poptCallbackType cb = NULL;
726 const void * cbData = NULL;
727 const char * longArg = NULL;
728 int canstrip = 0;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000729 int shorty = 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000730
731 while (!con->os->nextCharArg && con->os->next == con->os->argc
732 && con->os > con->optionStack) {
733 cleanOSE(con->os--);
734 }
735 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000736 /*@-internalglobs@*/
737 invokeCallbacksPOST(con, con->options);
738 /*@=internalglobs@*/
739 if (con->doExec) return execCommand(con);
Martin Poolb348dea2001-02-24 01:32:22 +0000740 return -1;
741 }
742
743 /* Process next long option */
744 if (!con->os->nextCharArg) {
745 char * localOptString, * optString;
746 int thisopt;
747
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000748 /*@-sizeoftype@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000749 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
750 con->os->next++;
751 continue;
752 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000753 /*@=sizeoftype@*/
754 thisopt = con->os->next;
755 if (con->os->argv != NULL) /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000756 origOptString = con->os->argv[con->os->next++];
757
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000758 if (origOptString == NULL) /* XXX can't happen */
759 return POPT_ERROR_BADOPT;
760
Martin Poolb348dea2001-02-24 01:32:22 +0000761 if (con->restLeftover || *origOptString != '-') {
Martin Poolb348dea2001-02-24 01:32:22 +0000762 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
763 con->restLeftover = 1;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000764 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
765 con->os->nextArg = xstrdup(origOptString);
766 return 0;
767 }
768 if (con->leftovers != NULL) /* XXX can't happen */
769 con->leftovers[con->numLeftovers++] = origOptString;
Martin Poolb348dea2001-02-24 01:32:22 +0000770 continue;
771 }
772
773 /* Make a copy we can hack at */
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000774 { size_t bufsize = strlen(origOptString) + 1;
775 localOptString = optString = alloca(bufsize);
776 if (optString == NULL) /* XXX can't happen */
777 return POPT_ERROR_BADOPT;
778 strlcpy(optString, origOptString, bufsize);
779 }
Martin Poolb348dea2001-02-24 01:32:22 +0000780
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000781 if (optString[0] == '\0')
Martin Poolb348dea2001-02-24 01:32:22 +0000782 return POPT_ERROR_BADOPT;
783
784 if (optString[1] == '-' && !optString[2]) {
785 con->restLeftover = 1;
786 continue;
787 } else {
788 char *oe;
789 int singleDash;
790
791 optString++;
792 if (*optString == '-')
793 singleDash = 0, optString++;
794 else
795 singleDash = 1;
796
797 /* XXX aliases with arg substitution need "--alias=arg" */
798 if (handleAlias(con, optString, '\0', NULL))
799 continue;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000800
Martin Poolb348dea2001-02-24 01:32:22 +0000801 if (handleExec(con, optString, '\0'))
802 continue;
803
804 /* Check for "--long=arg" option. */
805 for (oe = optString; *oe && *oe != '='; oe++)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000806 {};
Martin Poolb348dea2001-02-24 01:32:22 +0000807 if (*oe == '=') {
808 *oe++ = '\0';
809 /* XXX longArg is mapped back to persistent storage. */
810 longArg = origOptString + (oe - localOptString);
811 }
812
813 opt = findOption(con->options, optString, '\0', &cb, &cbData,
814 singleDash);
815 if (!opt && !singleDash)
816 return POPT_ERROR_BADOPT;
817 }
818
819 if (!opt) {
820 con->os->nextCharArg = origOptString + 1;
821 } else {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000822 if (con->os == con->optionStack &&
823 opt->argInfo & POPT_ARGFLAG_STRIP)
824 {
Martin Poolb348dea2001-02-24 01:32:22 +0000825 canstrip = 1;
826 poptStripArg(con, thisopt);
827 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000828 shorty = 0;
Martin Poolb348dea2001-02-24 01:32:22 +0000829 }
830 }
831
832 /* Process next short option */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000833 /*@-branchstate@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000834 if (con->os->nextCharArg) {
835 origOptString = con->os->nextCharArg;
836
837 con->os->nextCharArg = NULL;
838
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000839 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
840 continue;
841
842 if (handleExec(con, NULL, *origOptString)) {
843 /* Restore rest of short options for further processing */
Martin Poolb348dea2001-02-24 01:32:22 +0000844 origOptString++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000845 if (*origOptString != '\0')
846 con->os->nextCharArg = origOptString;
Martin Poolb348dea2001-02-24 01:32:22 +0000847 continue;
848 }
Martin Poolb348dea2001-02-24 01:32:22 +0000849
850 opt = findOption(con->options, NULL, *origOptString, &cb,
851 &cbData, 0);
852 if (!opt)
853 return POPT_ERROR_BADOPT;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000854 shorty = 1;
Martin Poolb348dea2001-02-24 01:32:22 +0000855
856 origOptString++;
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000857 if (*origOptString != '\0')
Martin Poolb348dea2001-02-24 01:32:22 +0000858 con->os->nextCharArg = origOptString;
859 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000860 /*@=branchstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +0000861
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000862 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +0000863 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000864 if (poptSaveInt((int *)opt->arg, opt->argInfo, 1L))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000865 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000866 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000867 if (opt->arg) {
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000868 if (poptSaveInt((int *)opt->arg, opt->argInfo, (long)opt->val))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000869 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000870 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000871 } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
872 con->os->nextArg = _free(con->os->nextArg);
873 /*@-usedef@*/ /* FIX: W2DO? */
Martin Poolb348dea2001-02-24 01:32:22 +0000874 if (longArg) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000875 /*@=usedef@*/
876 longArg = expandNextArg(con, longArg);
877 con->os->nextArg = longArg;
Martin Poolb348dea2001-02-24 01:32:22 +0000878 } else if (con->os->nextCharArg) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000879 longArg = expandNextArg(con, con->os->nextCharArg);
880 con->os->nextArg = longArg;
Martin Poolb348dea2001-02-24 01:32:22 +0000881 con->os->nextCharArg = NULL;
882 } else {
883 while (con->os->next == con->os->argc &&
884 con->os > con->optionStack) {
885 cleanOSE(con->os--);
886 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000887 if (con->os->next == con->os->argc) {
888 if (!(opt->argInfo & POPT_ARGFLAG_OPTIONAL))
889 /*@-compdef@*/ /* FIX: con->os->argv not defined */
890 return POPT_ERROR_NOARG;
891 /*@=compdef@*/
892 con->os->nextArg = NULL;
893 } else {
Martin Poolb348dea2001-02-24 01:32:22 +0000894
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000895 /*
896 * Make sure this isn't part of a short arg or the
897 * result of an alias expansion.
898 */
899 if (con->os == con->optionStack &&
900 (opt->argInfo & POPT_ARGFLAG_STRIP) &&
901 canstrip) {
902 poptStripArg(con, con->os->next);
903 }
Martin Poolb348dea2001-02-24 01:32:22 +0000904
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000905 if (con->os->argv != NULL) { /* XXX can't happen */
906 /* XXX watchout: subtle side-effects live here. */
907 longArg = con->os->argv[con->os->next++];
908 longArg = expandNextArg(con, longArg);
909 con->os->nextArg = longArg;
910 }
911 }
Martin Poolb348dea2001-02-24 01:32:22 +0000912 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000913 longArg = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +0000914
915 if (opt->arg) {
Martin Poolb348dea2001-02-24 01:32:22 +0000916 switch (opt->argInfo & POPT_ARG_MASK) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000917 case POPT_ARG_STRING:
Martin Poolb348dea2001-02-24 01:32:22 +0000918 /* XXX memory leak, hard to plug */
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000919 *((const char **) opt->arg) = (con->os->nextArg)
920 ? xstrdup(con->os->nextArg) : NULL;
921 /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000922
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000923 case POPT_ARG_INT:
924 case POPT_ARG_LONG:
925 { long aLong = 0;
926 char *end;
Martin Poolb348dea2001-02-24 01:32:22 +0000927
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000928 if (con->os->nextArg) {
929 aLong = strtol(con->os->nextArg, &end, 0);
930 if (!(end && *end == '\0'))
931 return POPT_ERROR_BADNUMBER;
932 }
933
Martin Poolb348dea2001-02-24 01:32:22 +0000934 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000935 if (aLong == LONG_MIN || aLong == LONG_MAX)
936 return POPT_ERROR_OVERFLOW;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000937 if (poptSaveLong((long *)opt->arg, opt->argInfo, aLong))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000938 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000939 } else {
940 if (aLong > INT_MAX || aLong < INT_MIN)
941 return POPT_ERROR_OVERFLOW;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000942 if (poptSaveInt((int *)opt->arg, opt->argInfo, aLong))
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000943 return POPT_ERROR_BADOPERATION;
Martin Poolb348dea2001-02-24 01:32:22 +0000944 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000945 } /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000946
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000947 case POPT_ARG_FLOAT:
948 case POPT_ARG_DOUBLE:
949 { double aDouble = 0.0;
950 char *end;
951
952 if (con->os->nextArg) {
953 /*@-mods@*/
954 int saveerrno = errno;
955 errno = 0;
956 aDouble = strtod(con->os->nextArg, &end);
957 if (errno == ERANGE)
958 return POPT_ERROR_OVERFLOW;
959 errno = saveerrno;
960 /*@=mods@*/
961 if (*end != '\0')
962 return POPT_ERROR_BADNUMBER;
963 }
964
965 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
966 *((double *) opt->arg) = aDouble;
967 } else {
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000968#define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
969 if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000970 return POPT_ERROR_OVERFLOW;
Wayne Davisonbc93ee82006-11-09 00:57:55 +0000971 if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000972 return POPT_ERROR_OVERFLOW;
973 *((float *) opt->arg) = aDouble;
974 }
975 } /*@switchbreak@*/ break;
976 default:
977 fprintf(stdout,
978 POPT_("option type (%d) not implemented in popt\n"),
979 (opt->argInfo & POPT_ARG_MASK));
Martin Poolb348dea2001-02-24 01:32:22 +0000980 exit(EXIT_FAILURE);
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000981 /*@notreached@*/ /*@switchbreak@*/ break;
Martin Poolb348dea2001-02-24 01:32:22 +0000982 }
983 }
984 }
985
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000986 if (cb) {
987 /*@-internalglobs@*/
988 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
989 /*@=internalglobs@*/
990 } else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
Martin Poolb348dea2001-02-24 01:32:22 +0000991 done = 1;
992
993 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
994 con->finalArgvAlloced += 10;
995 con->finalArgv = realloc(con->finalArgv,
996 sizeof(*con->finalArgv) * con->finalArgvAlloced);
997 }
998
Wayne Davisoncc248aa2002-07-27 18:32:25 +0000999 if (con->finalArgv != NULL)
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001000 { ssize_t bufsize = (opt->longName ? strlen(opt->longName) : 0) + 3;
1001 char *s = malloc(bufsize);
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001002 if (s != NULL) { /* XXX can't happen */
1003 if (opt->longName)
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001004 snprintf(s, bufsize, "%s%s",
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001005 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
1006 opt->longName);
1007 else
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001008 snprintf(s, bufsize, "-%c", opt->shortName);
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001009 con->finalArgv[con->finalArgvCount++] = s;
1010 } else
1011 con->finalArgv[con->finalArgvCount++] = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +00001012 }
1013
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001014 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
1015 /*@-ifempty@*/ ; /*@=ifempty@*/
1016 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
1017 /*@-ifempty@*/ ; /*@=ifempty@*/
1018 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
1019 if (con->finalArgv != NULL && con->os->nextArg)
1020 con->finalArgv[con->finalArgvCount++] =
1021 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
1022 xstrdup(con->os->nextArg);
1023 /*@=nullpass@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001024 }
1025 }
1026
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001027 return (opt ? opt->val : -1); /* XXX can't happen */
Martin Poolb348dea2001-02-24 01:32:22 +00001028}
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001029/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001030
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001031const char * poptGetOptArg(poptContext con)
1032{
1033 const char * ret = NULL;
1034 /*@-branchstate@*/
1035 if (con) {
1036 ret = con->os->nextArg;
1037 con->os->nextArg = NULL;
1038 }
1039 /*@=branchstate@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001040 return ret;
1041}
1042
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001043const char * poptGetArg(poptContext con)
1044{
1045 const char * ret = NULL;
1046 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1047 ret = con->leftovers[con->nextLeftover++];
1048 return ret;
Martin Poolb348dea2001-02-24 01:32:22 +00001049}
1050
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001051const char * poptPeekArg(poptContext con)
1052{
1053 const char * ret = NULL;
1054 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1055 ret = con->leftovers[con->nextLeftover];
1056 return ret;
Martin Poolb348dea2001-02-24 01:32:22 +00001057}
1058
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001059/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001060const char ** poptGetArgs(poptContext con)
1061{
1062 if (con == NULL ||
1063 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1064 return NULL;
Martin Poolb348dea2001-02-24 01:32:22 +00001065
1066 /* some apps like [like RPM ;-) ] need this NULL terminated */
1067 con->leftovers[con->numLeftovers] = NULL;
1068
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001069 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
Martin Poolb348dea2001-02-24 01:32:22 +00001070 return (con->leftovers + con->nextLeftover);
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001071 /*@=nullret =nullstate @*/
Martin Poolb348dea2001-02-24 01:32:22 +00001072}
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001073/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001074
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001075poptContext poptFreeContext(poptContext con)
1076{
1077 poptItem item;
Martin Poolb348dea2001-02-24 01:32:22 +00001078 int i;
1079
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001080 if (con == NULL) return con;
Martin Poolb348dea2001-02-24 01:32:22 +00001081 poptResetContext(con);
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001082 con->os->argb = _free(con->os->argb);
Martin Poolb348dea2001-02-24 01:32:22 +00001083
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001084 if (con->aliases != NULL)
Martin Poolb348dea2001-02-24 01:32:22 +00001085 for (i = 0; i < con->numAliases; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001086 item = con->aliases + i;
1087 /*@-modobserver -observertrans -dependenttrans@*/
1088 item->option.longName = _free(item->option.longName);
1089 item->option.descrip = _free(item->option.descrip);
1090 item->option.argDescrip = _free(item->option.argDescrip);
1091 /*@=modobserver =observertrans =dependenttrans@*/
1092 item->argv = _free(item->argv);
Martin Poolb348dea2001-02-24 01:32:22 +00001093 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001094 con->aliases = _free(con->aliases);
Martin Poolb348dea2001-02-24 01:32:22 +00001095
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001096 if (con->execs != NULL)
Martin Poolb348dea2001-02-24 01:32:22 +00001097 for (i = 0; i < con->numExecs; i++) {
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001098 item = con->execs + i;
1099 /*@-modobserver -observertrans -dependenttrans@*/
1100 item->option.longName = _free(item->option.longName);
1101 item->option.descrip = _free(item->option.descrip);
1102 item->option.argDescrip = _free(item->option.argDescrip);
1103 /*@=modobserver =observertrans =dependenttrans@*/
1104 item->argv = _free(item->argv);
Martin Poolb348dea2001-02-24 01:32:22 +00001105 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001106 con->execs = _free(con->execs);
Martin Poolb348dea2001-02-24 01:32:22 +00001107
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001108 con->leftovers = _free(con->leftovers);
1109 con->finalArgv = _free(con->finalArgv);
1110 con->appName = _free(con->appName);
1111 con->otherHelp = _free(con->otherHelp);
1112 con->execPath = _free(con->execPath);
1113 con->arg_strip = PBM_FREE(con->arg_strip);
Martin Poolb348dea2001-02-24 01:32:22 +00001114
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001115 con = _free(con);
1116 return con;
Martin Poolb348dea2001-02-24 01:32:22 +00001117}
1118
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001119int poptAddAlias(poptContext con, struct poptAlias alias,
Wayne Davison73042aa2004-06-09 21:41:21 +00001120 /*@unused@*/ UNUSED(int flags))
Martin Poolb348dea2001-02-24 01:32:22 +00001121{
David Dykstra451b5fc2003-01-11 14:39:41 +00001122 poptItem item = (poptItem) alloca(sizeof(*item));
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001123 memset(item, 0, sizeof(*item));
1124 item->option.longName = alias.longName;
1125 item->option.shortName = alias.shortName;
1126 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1127 item->option.arg = 0;
1128 item->option.val = 0;
1129 item->option.descrip = NULL;
1130 item->option.argDescrip = NULL;
1131 item->argc = alias.argc;
1132 item->argv = alias.argv;
1133 return poptAddItem(con, item, 0);
1134}
Martin Poolb348dea2001-02-24 01:32:22 +00001135
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001136/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001137/*@-mustmod@*/ /* LCL: con not modified? */
1138int poptAddItem(poptContext con, poptItem newItem, int flags)
1139{
1140 poptItem * items, item;
1141 int * nitems;
Martin Poolb348dea2001-02-24 01:32:22 +00001142
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001143 switch (flags) {
1144 case 1:
1145 items = &con->execs;
1146 nitems = &con->numExecs;
1147 break;
1148 case 0:
1149 items = &con->aliases;
1150 nitems = &con->numAliases;
1151 break;
1152 default:
1153 return 1;
1154 /*@notreached@*/ break;
1155 }
1156
1157 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1158 if ((*items) == NULL)
1159 return 1;
1160
1161 item = (*items) + (*nitems);
1162
1163 item->option.longName =
1164 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1165 item->option.shortName = newItem->option.shortName;
1166 item->option.argInfo = newItem->option.argInfo;
1167 item->option.arg = newItem->option.arg;
1168 item->option.val = newItem->option.val;
1169 item->option.descrip =
1170 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1171 item->option.argDescrip =
1172 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1173 item->argc = newItem->argc;
1174 item->argv = newItem->argv;
1175
1176 (*nitems)++;
Martin Poolb348dea2001-02-24 01:32:22 +00001177
1178 return 0;
1179}
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001180/*@=mustmod@*/
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001181/*@=boundswrite@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001182
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001183const char * poptBadOption(poptContext con, int flags)
1184{
1185 struct optionStackEntry * os = NULL;
Martin Poolb348dea2001-02-24 01:32:22 +00001186
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001187 if (con != NULL)
1188 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
Martin Poolb348dea2001-02-24 01:32:22 +00001189
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001190 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1191 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1192 /*@=nullderef@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001193}
1194
Wayne Davison4fd4b3d2005-02-20 17:21:13 +00001195const char * poptStrerror(const int error)
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001196{
Martin Poolb348dea2001-02-24 01:32:22 +00001197 switch (error) {
1198 case POPT_ERROR_NOARG:
1199 return POPT_("missing argument");
1200 case POPT_ERROR_BADOPT:
1201 return POPT_("unknown option");
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001202 case POPT_ERROR_BADOPERATION:
1203 return POPT_("mutually exclusive logical operations requested");
1204 case POPT_ERROR_NULLARG:
1205 return POPT_("opt->arg should not be NULL");
Martin Poolb348dea2001-02-24 01:32:22 +00001206 case POPT_ERROR_OPTSTOODEEP:
1207 return POPT_("aliases nested too deeply");
1208 case POPT_ERROR_BADQUOTE:
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001209 return POPT_("error in parameter quoting");
Martin Poolb348dea2001-02-24 01:32:22 +00001210 case POPT_ERROR_BADNUMBER:
1211 return POPT_("invalid numeric value");
1212 case POPT_ERROR_OVERFLOW:
1213 return POPT_("number too large or too small");
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001214 case POPT_ERROR_MALLOC:
1215 return POPT_("memory allocation failed");
Martin Poolb348dea2001-02-24 01:32:22 +00001216 case POPT_ERROR_ERRNO:
1217 return strerror(errno);
1218 default:
1219 return POPT_("unknown error");
1220 }
1221}
1222
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001223int poptStuffArgs(poptContext con, const char ** argv)
1224{
Martin Poolb348dea2001-02-24 01:32:22 +00001225 int argc;
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001226 int rc;
Martin Poolb348dea2001-02-24 01:32:22 +00001227
1228 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1229 return POPT_ERROR_OPTSTOODEEP;
1230
1231 for (argc = 0; argv[argc]; argc++)
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001232 {};
Martin Poolb348dea2001-02-24 01:32:22 +00001233
1234 con->os++;
1235 con->os->next = 0;
1236 con->os->nextArg = NULL;
1237 con->os->nextCharArg = NULL;
1238 con->os->currAlias = NULL;
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001239 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
Martin Poolb348dea2001-02-24 01:32:22 +00001240 con->os->argb = NULL;
1241 con->os->stuffed = 1;
1242
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001243 return rc;
Martin Poolb348dea2001-02-24 01:32:22 +00001244}
1245
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001246const char * poptGetInvocationName(poptContext con)
Martin Poolb348dea2001-02-24 01:32:22 +00001247{
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001248 return (con->os->argv ? con->os->argv[0] : "");
1249}
1250
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001251/*@-boundswrite@*/
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001252int poptStrippedArgv(poptContext con, int argc, char ** argv)
1253{
1254 int numargs = argc;
1255 int j = 1;
1256 int i;
Martin Poolb348dea2001-02-24 01:32:22 +00001257
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001258 /*@-sizeoftype@*/
1259 if (con->arg_strip)
1260 for (i = 1; i < argc; i++) {
1261 if (PBM_ISSET(i, con->arg_strip))
Martin Poolb348dea2001-02-24 01:32:22 +00001262 numargs--;
Martin Poolb348dea2001-02-24 01:32:22 +00001263 }
1264
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001265 for (i = 1; i < argc; i++) {
1266 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
Martin Poolb348dea2001-02-24 01:32:22 +00001267 continue;
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001268 argv[j] = (j < numargs) ? argv[i] : NULL;
1269 j++;
Martin Poolb348dea2001-02-24 01:32:22 +00001270 }
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001271 /*@=sizeoftype@*/
Martin Poolb348dea2001-02-24 01:32:22 +00001272
Wayne Davisoncc248aa2002-07-27 18:32:25 +00001273 return numargs;
Martin Poolb348dea2001-02-24 01:32:22 +00001274}
Wayne Davisonbc93ee82006-11-09 00:57:55 +00001275/*@=boundswrite@*/