Mixe for Privacy and Anonymity in the Internet
popt.cpp
Go to the documentation of this file.
1 /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2  file accompanying popt source distributions, available from
3  ftp://ftp.redhat.com/pub/code/popt */
4 
5 #include "../StdAfx.h"
6 //#include "system.h"
7 //#include "popt.h"
8 #include "poptint.h"
9 
10 #ifdef _WIN32
11 #define HAVE_STRERROR
12 #endif
13 #ifndef HAVE_STRERROR
14 static char * strerror(int errno) {
15  extern int sys_nerr;
16  extern char * sys_errlist[];
17 
18  if ((0 <= errno) && (errno < sys_nerr))
19  return sys_errlist[errno];
20  else
21  return POPT_("unknown errno");
22 }
23 #endif
24 
25 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) {
26  if (con->execPath) xfree(con->execPath);
27  con->execPath = xstrdup(path);
28  con->execAbsolute = allowAbsolute;
29 }
30 
31 static void invokeCallbacks(poptContext con, const struct poptOption * table,
32  int post) {
33  const struct poptOption * opt = table;
35 
36  while (opt->longName || opt->shortName || opt->arg) {
38  invokeCallbacks(con, (struct poptOption*) opt->arg, post);
39  }
40  else if (((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) &&
41  ((!post && (opt->argInfo & POPT_CBFLAG_PRE)) ||
42  (post && (opt->argInfo & POPT_CBFLAG_POST)))) {
43  cb = (poptCallbackType)opt->arg;
45  NULL, NULL, opt->descrip);
46  }
47  opt++;
48  }
49 }
50 
51 poptContext poptGetContext(const char * name, int argc, const char ** argv,
52  const struct poptOption * options, int flags) {
53  poptContext con = (poptContext)malloc(sizeof(*con));
54 
55  memset(con, 0, sizeof(*con));
56 
57  con->os = con->optionStack;
58  con->os->argc = argc;
59  con->os->argv = argv;
60  con->os->argb = NULL;
61 
63  con->os->next = 1; /* skip argv[0] */
64 
65  con->leftovers = (const char**)calloc((argc + 1), sizeof(char *));
66  con->options = options;
67  con->aliases = NULL;
68  con->numAliases = 0;
69  con->flags = flags;
70  con->execs = NULL;
71  con->numExecs = 0;
72  con->finalArgvAlloced = argc * 2;
73  con->finalArgv = (const char**)calloc(con->finalArgvAlloced, sizeof(*con->finalArgv));
74  con->execAbsolute = 1;
75  con->arg_strip = NULL;
76 
77  if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
79 
80  if (name)
81  con->appName = strcpy((char*)malloc(strlen(name) + 1), name);
82 
83  invokeCallbacks(con, con->options, 0);
84 
85  return con;
86 }
87 
88 static void cleanOSE(struct optionStackEntry *os)
89 {
90  if (os->nextArg) {
91  xfree(os->nextArg);
92  os->nextArg = NULL;
93  }
94  if (os->argv) {
95  xfree(os->argv);
96  os->argv = NULL;
97  }
98  if (os->argb) {
99  PBM_FREE(os->argb);
100  os->argb = NULL;
101  }
102 }
103 
105  int i;
106 
107  while (con->os > con->optionStack) {
108  cleanOSE(con->os--);
109  }
110  if (con->os->argb) {
111  PBM_FREE(con->os->argb);
112  con->os->argb = NULL;
113  }
114  con->os->currAlias = NULL;
115  con->os->nextCharArg = NULL;
116  con->os->nextArg = NULL;
117  con->os->next = 1; /* skip argv[0] */
118 
119  con->numLeftovers = 0;
120  con->nextLeftover = 0;
121  con->restLeftover = 0;
122  con->doExec = NULL;
123 
124  for (i = 0; i < con->finalArgvCount; i++) {
125  if (con->finalArgv[i]) {
126  xfree(con->finalArgv[i]);
127  con->finalArgv[i] = NULL;
128  }
129  }
130 
131  con->finalArgvCount = 0;
132 
133  if (con->arg_strip) {
134  PBM_FREE(con->arg_strip);
135  con->arg_strip = NULL;
136  }
137 }
138 
139 /* Only one of longName, shortName may be set at a time */
140 static int handleExec(poptContext con, char * longName, char shortName) {
141  int i;
142 
143  i = con->numExecs - 1;
144  if (longName) {
145  while (i >= 0 && (!con->execs[i].longName ||
146  strcmp(con->execs[i].longName, longName))) i--;
147  }
148  else {
149  while (i >= 0 &&
150  con->execs[i].shortName != shortName) i--;
151  }
152 
153  if (i < 0) return 0;
154 
155  if (con->flags & POPT_CONTEXT_NO_EXEC)
156  return 1;
157 
158  if (con->doExec == NULL) {
159  con->doExec = con->execs + i;
160  return 1;
161  }
162 
163  /* We already have an exec to do; remember this option for next
164  time 'round */
165  if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
166  con->finalArgvAlloced += 10;
167  con->finalArgv = (const char**)realloc((void*)con->finalArgv,
168  sizeof(*con->finalArgv) * con->finalArgvAlloced);
169  }
170 
171  i = con->finalArgvCount++;
172  { char *s = (char*)malloc((longName ? strlen(longName) : 0) + 3);
173  if (longName)
174  sprintf(s, "--%s", longName);
175  else
176  sprintf(s, "-%c", shortName);
177  con->finalArgv[i] = s;
178  }
179 
180  return 1;
181 }
182 
183 /* Only one of longName, shortName may be set at a time */
184 static int handleAlias(poptContext con, const char * longName, char shortName,
185  /*@keep@*/ const char * nextCharArg) {
186  int i;
187 
188  if (con->os->currAlias && con->os->currAlias->longName && longName &&
189  !strcmp(con->os->currAlias->longName, longName))
190  return 0;
191  if (con->os->currAlias && shortName &&
192  shortName == con->os->currAlias->shortName)
193  return 0;
194 
195  i = con->numAliases - 1;
196  if (longName) {
197  while (i >= 0 && (!con->aliases[i].longName ||
198  strcmp(con->aliases[i].longName, longName))) i--;
199  }
200  else {
201  while (i >= 0 &&
202  con->aliases[i].shortName != shortName) i--;
203  }
204 
205  if (i < 0) return 0;
206 
207  if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
208  return POPT_ERROR_OPTSTOODEEP;
209 
210  if (nextCharArg && *nextCharArg)
211  con->os->nextCharArg = nextCharArg;
212 
213  con->os++;
214  con->os->next = 0;
215  con->os->stuffed = 0;
216  con->os->nextArg = NULL;
217  con->os->nextCharArg = NULL;
218  con->os->currAlias = con->aliases + i;
219  poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
220  &con->os->argc, &con->os->argv);
221  con->os->argb = NULL;
222 
223  return 1;
224 }
225 
226 /*
227 static void execCommand(poptContext con) {
228 const char ** argv;
229 int pos = 0;
230 const char * script = con->doExec->script;
231 
232 argv = malloc(sizeof(*argv) *
233 (6 + con->numLeftovers + con->finalArgvCount));
234 
235 if (!con->execAbsolute && strchr(script, '/')) return;
236 
237 if (!strchr(script, '/') && con->execPath) {
238 char *s = alloca(strlen(con->execPath) + strlen(script) + 2);
239 sprintf(s, "%s/%s", con->execPath, script);
240 argv[pos] = s;
241 } else {
242 argv[pos] = script;
243 }
244 pos++;
245 
246 argv[pos] = findProgramPath(con->os->argv[0]);
247 if (argv[pos]) pos++;
248 argv[pos++] = ";";
249 
250 memcpy(argv + pos, con->finalArgv, sizeof(*argv) * con->finalArgvCount);
251 pos += con->finalArgvCount;
252 
253 if (con->numLeftovers) {
254 argv[pos++] = "--";
255 memcpy(argv + pos, con->leftovers, sizeof(*argv) * con->numLeftovers);
256 pos += con->numLeftovers;
257 }
258 
259 argv[pos++] = NULL;
260 
261 #ifdef __hpux
262 setresuid(getuid(), getuid(),-1);
263 #else
264 //
265 // XXX " ... on BSD systems setuid() should be preferred over setreuid()"
266 // XXX sez' Timur Bakeyev <mc@bat.ru>
267 // XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
268 //
269 #if defined(HAVE_SETUID)
270 setuid(getuid());
271 #elif defined (HAVE_SETREUID)
272 setreuid(getuid(), getuid()); //hlauer: not portable to hpux9.01
273 #else
274 ; // Can't drop privileges
275 #endif
276 #endif
277 
278 execvp(argv[0], (char *const *)argv);
279 }
280 */
281 /*@observer@*/ static const struct poptOption *
282 findOption(const struct poptOption * table, const char * longName,
283 char shortName,
284 /*@out@*/ poptCallbackType * callback, /*@out@*/ const void ** callbackData,
285 int singleDash)
286 {
287  const struct poptOption * opt = table;
288  const struct poptOption * opt2;
289  const struct poptOption * cb = NULL;
290 
291  /* This happens when a single - is given */
292  if (singleDash && !shortName && !*longName)
293  shortName = '-';
294 
295  while (opt->longName || opt->shortName || opt->arg) {
296  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
297  opt2 = findOption((struct poptOption*)opt->arg, longName, shortName, callback,
298  callbackData, singleDash);
299  if (opt2) {
300  if (*callback && !*callbackData)
301  *callbackData = opt->descrip;
302  return opt2;
303  }
304  }
305  else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
306  cb = opt;
307  }
308  else if (longName && opt->longName &&
309  (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
310  !strcmp(longName, opt->longName)) {
311  break;
312  }
313  else if (shortName && shortName == opt->shortName) {
314  break;
315  }
316  opt++;
317  }
318 
319  if (!opt->longName && !opt->shortName) return NULL;
320  *callbackData = NULL;
321  *callback = NULL;
322  if (cb) {
323  *callback = (poptCallbackType)cb->arg;
324  if (!(cb->argInfo & POPT_CBFLAG_INC_DATA))
325  *callbackData = cb->descrip;
326  }
327 
328  return opt;
329 }
330 
331 static const char *findNextArg(poptContext con, unsigned argx, int deletE)
332 {
333  struct optionStackEntry * os = con->os;
334  const char * arg;
335 
336  do {
337  int i;
338  arg = NULL;
339  while (os->next == os->argc && os > con->optionStack) os--;
340  if (os->next == os->argc && os == con->optionStack) break;
341  for (i = os->next; i < os->argc; i++) {
342  if (os->argb && PBM_ISSET(i, os->argb)) continue;
343  if (*os->argv[i] == '-') continue;
344  if (--argx > 0) continue;
345  arg = os->argv[i];
346  if (deletE) {
347  if (os->argb == NULL) os->argb = (pbm_set*)PBM_ALLOC(os->argc);
348  PBM_SET(i, os->argb);
349  }
350  break;
351  }
352  if (os > con->optionStack) os--;
353  } while (arg == NULL);
354  return arg;
355 }
356 
357 static /*@only@*/ const char * expandNextArg(poptContext con, const char * s)
358 {
359  const char *a;
360  size_t alen;
361  char *t, *te;
362  size_t tn = strlen(s) + 1;
363  char c;
364 
365  te = t = (char*)malloc(tn);;
366  while ((c = *s++) != '\0') {
367  switch (c) {
368 #if 0 /* XXX can't do this */
369  case '\\': /* escape */
370  c = *s++;
371  break;
372 #endif
373  case '!':
374  if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
375  break;
376  if ((a = findNextArg(con, 1, 1)) == NULL)
377  break;
378  s += 3;
379 
380  alen = strlen(a);
381  tn += alen;
382  *te = '\0';
383  t = (char*)realloc(t, tn);
384  te = t + strlen(t);
385  strncpy(te, a, alen); te += alen;
386  continue;
387  /*@notreached@*/ break;
388  default:
389  break;
390  }
391  *te++ = c;
392  }
393  *te = '\0';
394  t = (char*)realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
395  return t;
396 }
397 
398 static void poptStripArg(poptContext con, int which)
399 {
400  if (con->arg_strip == NULL) {
401  con->arg_strip = (pbm_set*)PBM_ALLOC(con->optionStack[0].argc);
402  }
403  PBM_SET(which, con->arg_strip);
404 }
405 
406 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
408 {
409  const struct poptOption * opt = NULL;
410  int done = 0;
411 
412  while (!done) {
413  const char * origOptString = NULL;
414  poptCallbackType cb = NULL;
415  const void * cbData = NULL;
416  const char * longArg = NULL;
417  int canstrip = 0;
418 
419  while (!con->os->nextCharArg && con->os->next == con->os->argc
420  && con->os > con->optionStack) {
421  cleanOSE(con->os--);
422  }
423  if (!con->os->nextCharArg && con->os->next == con->os->argc) {
424  invokeCallbacks(con, con->options, 1);
425  //if (con->doExec) execCommand(con);
426  return -1;
427  }
428 
429  /* Process next long option */
430  if (!con->os->nextCharArg) {
431  char * localOptString, *optString;
432  int thisopt;
433 
434  if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
435  con->os->next++;
436  continue;
437  }
438  thisopt = con->os->next;
439  origOptString = con->os->argv[con->os->next++];
440 
441  if (con->restLeftover || *origOptString != '-') {
442  con->leftovers[con->numLeftovers++] = origOptString;
444  con->restLeftover = 1;
445  continue;
446  }
447 
448  /* Make a copy we can hack at */
449  localOptString = optString =
450  strcpy((char*)alloca(strlen(origOptString) + 1),
451  origOptString);
452 
453  if (!optString[0])
454  return POPT_ERROR_BADOPT;
455 
456  if (optString[1] == '-' && !optString[2]) {
457  con->restLeftover = 1;
458  continue;
459  }
460  else {
461  char *oe;
462  int singleDash;
463 
464  optString++;
465  if (*optString == '-')
466  singleDash = 0, optString++;
467  else
468  singleDash = 1;
469 
470  /* XXX aliases with arg substitution need "--alias=arg" */
471  if (handleAlias(con, optString, '\0', NULL))
472  continue;
473  if (handleExec(con, optString, '\0'))
474  continue;
475 
476  /* Check for "--long=arg" option. */
477  for (oe = optString; *oe && *oe != '='; oe++)
478  ;
479  if (*oe == '=') {
480  *oe++ = '\0';
481  /* XXX longArg is mapped back to persistent storage. */
482  longArg = origOptString + (oe - localOptString);
483  }
484 
485  opt = findOption(con->options, optString, '\0', &cb, &cbData,
486  singleDash);
487  if (!opt && !singleDash)
488  return POPT_ERROR_BADOPT;
489  }
490 
491  if (!opt) {
492  con->os->nextCharArg = origOptString + 1;
493  }
494  else {
495  if (con->os == con->optionStack &&
496  opt->argInfo & POPT_ARGFLAG_STRIP) {
497  canstrip = 1;
498  poptStripArg(con, thisopt);
499  }
500  }
501  }
502 
503  /* Process next short option */
504  if (con->os->nextCharArg) {
505  origOptString = con->os->nextCharArg;
506 
507  con->os->nextCharArg = NULL;
508 
509  if (handleAlias(con, NULL, *origOptString,
510  origOptString + 1)) {
511  origOptString++;
512  continue;
513  }
514  if (handleExec(con, NULL, *origOptString))
515  continue;
516 
517  opt = findOption(con->options, NULL, *origOptString, &cb,
518  &cbData, 0);
519  if (!opt)
520  return POPT_ERROR_BADOPT;
521 
522  origOptString++;
523  if (*origOptString)
524  con->os->nextCharArg = origOptString;
525  }
526 
527  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
528  *((int *)opt->arg) = 1;
529  }
530  else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
531  if (opt->arg)
532  *((int *)opt->arg) = opt->val;
533  }
534  else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
535  if (con->os->nextArg) {
536  xfree(con->os->nextArg);
537  con->os->nextArg = NULL;
538  }
539  if (longArg) {
540  con->os->nextArg = expandNextArg(con, longArg);
541  }
542  else if (con->os->nextCharArg) {
543  con->os->nextArg = expandNextArg(con, con->os->nextCharArg);
544  con->os->nextCharArg = NULL;
545  }
546  else {
547  while (con->os->next == con->os->argc &&
548  con->os > con->optionStack) {
549  cleanOSE(con->os--);
550  }
551  if (con->os->next == con->os->argc)
552  return POPT_ERROR_NOARG;
553 
554  /* make sure this isn't part of a short arg or the
555  result of an alias expansion */
556  if (con->os == con->optionStack &&
557  opt->argInfo & POPT_ARGFLAG_STRIP &&
558  canstrip) {
559  poptStripArg(con, con->os->next);
560  }
561 
562  con->os->nextArg = expandNextArg(con, con->os->argv[con->os->next++]);
563  }
564 
565  if (opt->arg) {
566  long aLong;
567  char *end;
568 
569  switch (opt->argInfo & POPT_ARG_MASK) {
570  case POPT_ARG_STRING:
571  /* XXX memory leak, hard to plug */
572  *((const char **)opt->arg) = xstrdup(con->os->nextArg);
573  break;
574 
575  case POPT_ARG_INT:
576  case POPT_ARG_LONG:
577  aLong = strtol(con->os->nextArg, &end, 0);
578  if (!(end && *end == '\0'))
579  return POPT_ERROR_BADNUMBER;
580 
581  if (aLong == LONG_MIN || aLong == LONG_MAX)
582  return POPT_ERROR_OVERFLOW;
583  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
584  *((long *)opt->arg) = aLong;
585  }
586  else {
587  if (aLong > INT_MAX || aLong < INT_MIN)
588  return POPT_ERROR_OVERFLOW;
589  *((int *)opt->arg) = aLong;
590  }
591  break;
592 
593  default:
594  fprintf(stdout, POPT_("option type (%d) not implemented in popt\n"),
595  opt->argInfo & POPT_ARG_MASK);
596  exit(EXIT_FAILURE);
597  }
598  }
599  }
600 
601  if (cb)
602  cb(con, POPT_CALLBACK_REASON_OPTION, opt, con->os->nextArg, cbData);
603  else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
604  done = 1;
605 
606  if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
607  con->finalArgvAlloced += 10;
608  con->finalArgv = (const char**)realloc((void*)con->finalArgv,
609  sizeof(*con->finalArgv) * con->finalArgvAlloced);
610  }
611 
612  { char *s = (char*)malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
613  if (opt->longName)
614  sprintf(s, "--%s", opt->longName);
615  else
616  sprintf(s, "-%c", opt->shortName);
617  con->finalArgv[con->finalArgvCount++] = s;
618  }
619 
620  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE
621  && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL) {
622  con->finalArgv[con->finalArgvCount++] = xstrdup(con->os->nextArg);
623  }
624  }
625 
626  return opt->val;
627 }
628 
629 const char * poptGetOptArg(poptContext con) {
630  const char * ret = con->os->nextArg;
631  con->os->nextArg = NULL;
632  return ret;
633 }
634 
635 const char * poptGetArg(poptContext con) {
636  if (con->numLeftovers == con->nextLeftover) return NULL;
637  return con->leftovers[con->nextLeftover++];
638 }
639 
640 const char * poptPeekArg(poptContext con) {
641  if (con->numLeftovers == con->nextLeftover) return NULL;
642  return con->leftovers[con->nextLeftover];
643 }
644 
645 const char ** poptGetArgs(poptContext con) {
646  if (con->numLeftovers == con->nextLeftover) return NULL;
647 
648  /* some apps like [like RPM ;-) ] need this NULL terminated */
649  con->leftovers[con->numLeftovers] = NULL;
650 
651  return (con->leftovers + con->nextLeftover);
652 }
653 
655  int i;
656 
657  poptResetContext(con);
658  if (con->os->argb) free(con->os->argb);
659 
660  for (i = 0; i < con->numAliases; i++) {
661  if (con->aliases[i].longName) xfree(con->aliases[i].longName);
662  free((void*)con->aliases[i].argv);
663  }
664 
665  for (i = 0; i < con->numExecs; i++) {
666  if (con->execs[i].longName) xfree(con->execs[i].longName);
667  xfree(con->execs[i].script);
668  }
669  if (con->execs) xfree(con->execs);
670 
671  free((void*)con->leftovers);
672  free((void*)con->finalArgv);
673  if (con->appName) xfree(con->appName);
674  if (con->aliases) free(con->aliases);
675  if (con->otherHelp) xfree(con->otherHelp);
676  if (con->execPath) xfree(con->execPath);
677  if (con->arg_strip) PBM_FREE(con->arg_strip);
678 
679  free(con);
680 }
681 
682 int poptAddAlias(poptContext con, struct poptAlias newAlias,
683  /*@unused@*/ int flags)
684 {
685  int aliasNum = con->numAliases++;
686  struct poptAlias * alias;
687 
688  /* SunOS won't realloc(NULL, ...) */
689  if (!con->aliases)
690  con->aliases = (struct poptAlias*)malloc(sizeof(newAlias)* con->numAliases);
691  else
692  con->aliases = (struct poptAlias*)realloc(con->aliases,
693  sizeof(newAlias)* con->numAliases);
694  alias = con->aliases + aliasNum;
695 
696  alias->longName = (newAlias.longName)
697  ? strcpy((char*)malloc(strlen(newAlias.longName) + 1), newAlias.longName)
698  : NULL;
699  alias->shortName = newAlias.shortName;
700  alias->argc = newAlias.argc;
701  alias->argv = newAlias.argv;
702 
703  return 0;
704 }
705 
706 const char * poptBadOption(poptContext con, int flags) {
707  struct optionStackEntry * os;
708 
710  os = con->optionStack;
711  else
712  os = con->os;
713 
714  return os->argv[os->next - 1];
715 }
716 
717 #define POPT_ERROR_NOARG -10
718 #define POPT_ERROR_BADOPT -11
719 #define POPT_ERROR_OPTSTOODEEP -13
720 #define POPT_ERROR_BADQUOTE -15 /* only from poptParseArgString() */
721 #define POPT_ERROR_ERRNO -16 /* only from poptParseArgString() */
722 
723 const char * /*const*/ poptStrerror(const int error) {
724  switch (error) {
725  case POPT_ERROR_NOARG:
726  return POPT_("missing argument");
727  case POPT_ERROR_BADOPT:
728  return POPT_("unknown option");
730  return POPT_("aliases nested too deeply");
731  case POPT_ERROR_BADQUOTE:
732  return POPT_("error in paramter quoting");
734  return POPT_("invalid numeric value");
735  case POPT_ERROR_OVERFLOW:
736  return POPT_("number too large or too small");
737  case POPT_ERROR_ERRNO:
738  return strerror(errno);
739  default:
740  return POPT_("unknown error");
741  }
742 }
743 
744 int poptStuffArgs(poptContext con, const char ** argv) {
745  int argc;
746 
747  if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
748  return POPT_ERROR_OPTSTOODEEP;
749 
750  for (argc = 0; argv[argc]; argc++)
751  ;
752 
753  con->os++;
754  con->os->next = 0;
755  con->os->nextArg = NULL;
756  con->os->nextCharArg = NULL;
757  con->os->currAlias = NULL;
758  poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
759  con->os->argb = NULL;
760  con->os->stuffed = 1;
761 
762  return 0;
763 }
764 
766  return con->os->argv[0];
767 }
768 
769 int poptStrippedArgv(poptContext con, int argc, char **argv)
770 {
771  int i, j = 1, numargs = argc;
772 
773  for (i = 1; i < argc; i++) {
774  if (PBM_ISSET(i, con->arg_strip)) {
775  numargs--;
776  }
777  }
778 
779  for (i = 1; i < argc; i++) {
780  if (PBM_ISSET(i, con->arg_strip)) {
781  continue;
782  }
783  else {
784  if (j < numargs) {
785  argv[j++] = argv[i];
786  }
787  else {
788  argv[j++] = NULL;
789  }
790  }
791  }
792 
793  return(numargs);
794 }
int poptAddAlias(poptContext con, struct poptAlias newAlias, int flags)
Definition: popt.cpp:682
void poptSetExecPath(poptContext con, const char *path, int allowAbsolute)
Definition: popt.cpp:25
const char * poptPeekArg(poptContext con)
Definition: popt.cpp:640
#define POPT_ERROR_OPTSTOODEEP
Definition: popt.cpp:719
#define POPT_ERROR_BADOPT
Definition: popt.cpp:718
#define POPT_ERROR_NOARG
Definition: popt.cpp:717
int poptStuffArgs(poptContext con, const char **argv)
Definition: popt.cpp:744
const char * poptGetInvocationName(poptContext con)
Definition: popt.cpp:765
const char ** poptGetArgs(poptContext con)
Definition: popt.cpp:645
const char * poptGetOptArg(poptContext con)
Definition: popt.cpp:629
#define POPT_ERROR_BADQUOTE
Definition: popt.cpp:720
int poptStrippedArgv(poptContext con, int argc, char **argv)
Definition: popt.cpp:769
void poptResetContext(poptContext con)
Definition: popt.cpp:104
const char * poptStrerror(const int error)
Definition: popt.cpp:723
int poptGetNextOpt(poptContext con)
Definition: popt.cpp:407
void poptFreeContext(poptContext con)
Definition: popt.cpp:654
#define POPT_ERROR_ERRNO
Definition: popt.cpp:721
const char * poptGetArg(poptContext con)
Definition: popt.cpp:635
poptContext poptGetContext(const char *name, int argc, const char **argv, const struct poptOption *options, int flags)
Definition: popt.cpp:51
const char * poptBadOption(poptContext con, int flags)
Definition: popt.cpp:706
#define POPT_ERROR_OVERFLOW
Definition: popt.h:38
#define POPT_CONTEXT_KEEP_FIRST
Definition: popt.h:45
#define POPT_ARG_MASK
Definition: popt.h:24
void(* poptCallbackType)(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)
Definition: popt.h:77
#define POPT_CBFLAG_PRE
Definition: popt.h:28
#define POPT_CBFLAG_POST
Definition: popt.h:29
#define POPT_ARG_VAL
Definition: popt.h:23
#define POPT_ARGFLAG_STRIP
Definition: popt.h:27
#define POPT_ARGFLAG_ONEDASH
Definition: popt.h:25
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
Definition: poptparse.cpp:10
#define POPT_CONTEXT_NO_EXEC
Definition: popt.h:44
#define POPT_ARG_INT
Definition: popt.h:18
#define POPT_BADOPTION_NOALIAS
Definition: popt.h:41
#define POPT_ARG_LONG
Definition: popt.h:19
#define POPT_ARG_NONE
Definition: popt.h:16
#define POPT_ARG_STRING
Definition: popt.h:17
#define POPT_CBFLAG_INC_DATA
Definition: popt.h:30
#define POPT_ARG_INCLUDE_TABLE
Definition: popt.h:20
#define POPT_OPTION_DEPTH
Definition: popt.h:14
#define POPT_ARG_CALLBACK
Definition: popt.h:21
struct poptContext_s * poptContext
Definition: popt.h:69
#define POPT_CONTEXT_POSIXMEHARDER
Definition: popt.h:46
#define POPT_ERROR_BADNUMBER
Definition: popt.h:37
@ POPT_CALLBACK_REASON_PRE
Definition: popt.h:74
@ POPT_CALLBACK_REASON_OPTION
Definition: popt.h:76
@ POPT_CALLBACK_REASON_POST
Definition: popt.h:75
#define PBM_SET(d, s)
Definition: poptint.h:20
#define PBM_ALLOC(d)
Definition: poptint.h:18
#define POPT_(foo)
Definition: poptint.h:81
#define xfree(_a)
Definition: poptint.h:65
#define PBM_FREE(s)
Definition: poptint.h:19
#define PBM_ISSET(d, s)
Definition: poptint.h:22
const char * script
Definition: poptint.h:38
const char * longName
Definition: poptint.h:36
char shortName
Definition: poptint.h:37
Definition: poptint.h:24
int stuffed
Definition: poptint.h:32
const char * nextArg
Definition: poptint.h:29
const char * nextCharArg
Definition: poptint.h:30
struct poptAlias * currAlias
Definition: poptint.h:31
int argc
Definition: poptint.h:25
int next
Definition: poptint.h:28
pbm_set * argb
Definition: poptint.h:27
const char ** argv
Definition: poptint.h:26
Definition: popt.h:58
int argc
Definition: popt.h:61
const char ** argv
Definition: popt.h:62
const char * longName
Definition: popt.h:59
char shortName
Definition: popt.h:60
int finalArgvCount
Definition: poptint.h:56
const char ** leftovers
Definition: poptint.h:44
int execAbsolute
Definition: poptint.h:60
int nextLeftover
Definition: poptint.h:46
int finalArgvAlloced
Definition: poptint.h:57
const char * execPath
Definition: poptint.h:59
const struct poptOption * options
Definition: poptint.h:47
int numExecs
Definition: poptint.h:54
struct optionStackEntry * os
Definition: poptint.h:43
pbm_set * arg_strip
Definition: poptint.h:62
const char * otherHelp
Definition: poptint.h:61
int numLeftovers
Definition: poptint.h:45
const char * appName
Definition: poptint.h:49
int restLeftover
Definition: poptint.h:48
struct execEntry * execs
Definition: poptint.h:53
const char ** finalArgv
Definition: poptint.h:55
struct poptAlias * aliases
Definition: poptint.h:50
int numAliases
Definition: poptint.h:51
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
Definition: poptint.h:42
struct execEntry * doExec
Definition: poptint.h:58
int argInfo
Definition: popt.h:51
void * arg
Definition: popt.h:52
char shortName
Definition: popt.h:50
int val
Definition: popt.h:53
const char * longName
Definition: popt.h:49
const char * descrip
Definition: popt.h:54
char * xstrdup(const char *str)
char * alloca()
UINT16 flags
Definition: typedefs.hpp:1