Home | History | Annotate | Download | only in libpopt

Lines Matching full:con

41 static void prtcon(const char *msg, poptContext con)
45 con, con->os,
46 (con->os->nextCharArg ? con->os->nextCharArg : ""),
47 (con->os->nextArg ? con->os->nextArg : ""),
48 con->os->next,
49 (con->os->argv && con->os->argv[con->os->next]
50 ? con->os->argv[con->os->next] : ""));
54 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
56 con->execPath = _free(con->execPath);
57 con->execPath = xstrdup(path);
58 con->execAbsolute = allowAbsolute;
59 /*@-nullstate@*/ /* LCL: con->execPath not NULL */
64 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
78 invokeCallbacksPRE(con, arg);
86 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
92 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
106 invokeCallbacksPOST(con, arg);
114 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
120 static void invokeCallbacksOPTION(poptContext con,
139 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
159 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
160 con->os->nextArg, cbData);
173 poptContext con = malloc(sizeof(*con));
175 if (con == NULL) return NULL; /* XXX can't happen */
176 memset(con, 0, sizeof(*con));
178 con->os = con->optionStack;
179 con->os->argc = argc;
181 con->os->argv = argv;
183 con->os->argb = NULL;
186 con->os->next = 1; /* skip argv[0] */
188 con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
190 con->options = options;
192 con->aliases = NULL;
193 con->numAliases = 0;
194 con->flags = flags;
195 con->execs = NULL;
196 con->numExecs = 0;
197 con->finalArgvAlloced = argc * 2;
198 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
199 con->execAbsolute = 1;
200 con->arg_strip = NULL;
203 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
207 if (t) con->appName = strcpy(t, name);
211 invokeCallbacksPRE(con, con->options);
214 return con;
228 void poptResetContext(poptContext con)
232 if (con == NULL) return;
233 while (con->os > con->optionStack) {
234 cleanOSE(con->os--);
236 con->os->argb = PBM_FREE(con->os->argb);
237 con->os->currAlias = NULL;
238 con->os->nextCharArg = NULL;
239 con->os->nextArg = NULL;
240 con->os->next = 1; /* skip argv[0] */
242 con->numLeftovers = 0;
243 con->nextLeftover = 0;
244 con->restLeftover = 0;
245 con->doExec = NULL;
247 if (con->finalArgv != NULL)
248 for (i = 0; i < con->finalArgvCount; i++) {
250 con->finalArgv[i] = _free(con->finalArgv[i]);
254 con->finalArgvCount = 0;
255 con->arg_strip = PBM_FREE(con->arg_strip);
256 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
264 static int handleExec(/*@special@*/ poptContext con,
266 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
267 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
268 /*@modifies con @*/
273 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
276 for (i = con->numExecs - 1; i >= 0; i--) {
277 item = con->execs + i;
288 if (con->flags & POPT_CONTEXT_NO_EXEC)
291 if (con->doExec == NULL) {
292 con->doExec = con->execs + i;
298 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
299 con->finalArgvAlloced += 10;
300 con->finalArgv = realloc(con->finalArgv,
301 sizeof(*con->finalArgv) * con->finalArgvAlloced);
304 i = con->finalArgvCount++;
305 if (con->finalArgv != NULL) /* XXX can't happen */
312 con->finalArgv[i] = s;
314 con->finalArgv[i] = NULL;
317 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
324 static int handleAlias(/*@special@*/ poptContext con,
327 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
328 con->os->currAlias, con->os->currAlias->option.longName @*/
329 /*@modifies con @*/
331 poptItem item = con->os->currAlias;
343 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
346 for (i = con->numAliases - 1; i >= 0; i--) {
347 item = con->aliases + i;
357 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
362 con->os->nextCharArg = nextCharArg;
365 con->os++;
366 con->os->next = 0;
367 con->os->stuffed = 0;
368 con->os->nextArg = NULL;
369 con->os->nextCharArg = NULL;
370 con->os->currAlias = con->aliases + i;
371 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
372 &con->os->argc, &con->os->argv);
373 con->os->argb = NULL;
379 static int execCommand(poptContext con)
383 poptItem item = con->doExec;
392 (!con->execAbsolute && strchr(item->argv[0], '/')))
396 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
399 if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
400 char *s = alloca(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
401 sprintf(s, "%s/%s", con->execPath, item->argv[0]);
412 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
413 memcpy(argv + argc, con->finalArgv,
414 sizeof(*argv) * con->finalArgvCount);
415 argc += con->finalArgvCount;
418 if (con->leftovers != NULL && con->numLeftovers > 0) {
419 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
420 argc += con->numLeftovers;
544 static const char * findNextArg(/*@special@*/ poptContext con,
546 /*@uses con->optionStack, con->os,
547 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
548 /*@modifies con @*/
550 struct optionStackEntry * os = con->os;
556 while (os->next == os->argc && os > con->optionStack) os--;
557 if (os->next == os->argc && os == con->optionStack) break;
576 if (os > con->optionStack) os--;
583 expandNextArg(/*@special@*/ poptContext con, const char * s)
584 /*@uses con->optionStack, con->os,
585 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
586 /*@modifies con @*/
608 if ((a = findNextArg(con, 1, 1)) == NULL)
632 static void poptStripArg(/*@special@*/ poptContext con, int which)
633 /*@uses con->arg_strip, con->optionStack @*/
634 /*@defines con->arg_strip @*/
635 /*@modifies con @*/
638 if (con->arg_strip == NULL)
639 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
640 if (con->arg_strip != NULL) /* XXX can't happen */
641 PBM_SET(which, con->arg_strip);
643 /*@-compdef@*/ /* LCL: con->arg_strip udefined? */
706 int poptGetNextOpt(poptContext con)
711 if (con == NULL)
721 while (!con->os->nextCharArg && con->os->next == con->os->argc
722 && con->os > con->optionStack) {
723 cleanOSE(con->os--);
725 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
727 invokeCallbacksPOST(con, con->options);
729 if (con->doExec) return execCommand(con);
734 if (!con->os->nextCharArg) {
739 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
740 con->os->next++;
744 thisopt = con->os->next;
745 if (con->os->argv != NULL) /* XXX can't happen */
746 origOptString = con->os->argv[con->os->next++];
751 if (con->restLeftover || *origOptString != '-' ||
754 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
755 con->restLeftover = 1;
756 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
757 con->os->nextArg = xstrdup(origOptString);
760 if (con->leftovers != NULL) /* XXX can't happen */
761 con->leftovers[con->numLeftovers++] = origOptString;
773 con->restLeftover = 1;
786 if (handleAlias(con, optString, '\0', NULL))
789 if (handleExec(con, optString, '\0'))
801 opt = findOption(con->options, optString, '\0', &cb, &cbData,
808 con->os->nextCharArg = origOptString + 1;
810 if (con->os == con->optionStack &&
814 poptStripArg(con, thisopt);
822 if (con->os->nextCharArg) {
823 origOptString = con->os->nextCharArg;
825 con->os->nextCharArg = NULL;
827 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
830 if (handleExec(con, NULL, *origOptString)) {
834 con->os->nextCharArg = origOptString;
838 opt = findOption(con->options, NULL, *origOptString, &cb,
846 con->os->nextCharArg = origOptString;
860 con->os->nextArg = _free(con->os->nextArg);
864 longArg = expandNextArg(con, longArg);
865 con->os->nextArg = longArg;
866 } else if (con->os->nextCharArg) {
867 longArg = expandNextArg(con, con->os->nextCharArg);
868 con->os->nextArg = longArg;
869 con->os->nextCharArg = NULL;
871 while (con->os->next == con->os->argc &&
872 con->os > con->optionStack) {
873 cleanOSE(con->os--);
875 if (con->os->next == con->os->argc) {
877 /*@-compdef@*/ /* FIX: con->os->argv not defined */
880 con->os->nextArg = NULL;
887 if (con->os == con->optionStack &&
890 poptStripArg(con, con->os->next);
893 if (con->os->argv != NULL) { /* XXX can't happen */
895 longArg = con->os->argv[con->os->next++];
896 longArg = expandNextArg(con, longArg);
897 con->os->nextArg = longArg;
907 *((const char **) opt->arg) = (con->os->nextArg)
908 ? xstrdup(con->os->nextArg) : NULL;
916 if (con->os->nextArg) {
917 aLong = strtol(con->os->nextArg, &end, 0);
940 if (con->os->nextArg) {
944 aDouble = strtod(con->os->nextArg, &end);
976 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
981 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
982 con->finalArgvAlloced += 10;
983 con->finalArgv = realloc(con->finalArgv,
984 sizeof(*con->finalArgv) * con->finalArgvAlloced);
987 if (con->finalArgv != NULL)
996 con->finalArgv[con->finalArgvCount++] = s;
998 con->finalArgv[con->finalArgvCount++] = NULL;
1006 if (con->finalArgv != NULL && con->os->nextArg)
1007 con->finalArgv[con->finalArgvCount++] =
1008 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
1009 xstrdup(con->os->nextArg);
1018 const char * poptGetOptArg(poptContext con)
1022 if (con) {
1023 ret = con->os->nextArg;
1024 con->os->nextArg = NULL;
1030 const char * poptGetArg(poptContext con)
1033 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1034 ret = con->leftovers[con->nextLeftover++];
1038 const char * poptPeekArg(poptContext con)
1041 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1042 ret = con->leftovers[con->nextLeftover];
1047 const char ** poptGetArgs(poptContext con)
1049 if (con == NULL ||
1050 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1054 con->leftovers[con->numLeftovers] = NULL;
1057 return (con->leftovers + con->nextLeftover);
1062 poptContext poptFreeContext(poptContext con)
1067 if (con == NULL) return con;
1068 poptResetContext(con);
1069 con->os->argb = _free(con->os->argb);
1071 if (con->aliases != NULL)
1072 for (i = 0; i < con->numAliases; i++) {
1073 item = con->aliases + i;
1081 con->aliases = _free(con->aliases);
1083 if (con->execs != NULL)
1084 for (i = 0; i < con->numExecs; i++) {
1085 item = con->execs + i;
1093 con->execs = _free(con->execs);
1095 con->leftovers = _free(con->leftovers);
1096 con->finalArgv = _free(con->finalArgv);
1097 con->appName = _free(con->appName);
1098 con->otherHelp = _free(con->otherHelp);
1099 con->execPath = _free(con->execPath);
1100 con->arg_strip = PBM_FREE(con->arg_strip);
1102 con = _free(con);
1103 return con;
1106 int poptAddAlias(poptContext con, struct poptAlias alias,
1120 return poptAddItem(con, item, 0);
1124 /*@-mustmod@*/ /* LCL: con not modified? */
1125 int poptAddItem(poptContext con, poptItem newItem, int flags)
1132 items = &con->execs;
1133 nitems = &con->numExecs;
1136 items = &con->aliases;
1137 nitems = &con->numAliases;
1170 const char * poptBadOption(poptContext con, int flags)
1174 if (con != NULL)
1175 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1210 int poptStuffArgs(poptContext con, const char ** argv)
1215 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1221 con->os++;
1222 con->os->next = 0;
1223 con->os->nextArg = NULL;
1224 con->os->nextCharArg = NULL;
1225 con->os->currAlias = NULL;
1226 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1227 con->os->argb = NULL;
1228 con->os->stuffed = 1;
1233 const char * poptGetInvocationName(poptContext con)
1235 return (con->os->argv ? con->os->argv[0] : "");
1239 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1246 if (con->arg_strip)
1248 if (PBM_ISSET(i, con->arg_strip))
1253 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))