5 * This is a special option processing routine that will save the
6 * argument to an option in a FIFO queue.
12 * This file is part of AutoOpts, a companion to AutoGen.
13 * AutoOpts is free software.
14 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
16 * AutoOpts is available under any one of two licenses. The license
17 * in use must be one of these two and the choice is under the control
18 * of the user of the license.
20 * The GNU Lesser General Public License, version 3 or later
21 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
23 * The Modified Berkeley Software Distribution License
24 * See the file "COPYING.mbsd"
26 * These files have the following sha256 sums:
28 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
29 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
30 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
34 # include REGEX_HEADER
37 /*=export_func optionUnstackArg
40 * what: Remove option args from a stack
41 * arg: + tOptions * + opts + program options descriptor +
42 * arg: + tOptDesc * + od + the descriptor for this arg +
45 * Invoked for options that are equivalenced to stacked options.
48 optionUnstackArg(tOptions * opts, tOptDesc * od)
52 if (INQUERY_CALL(opts, od))
55 arg_list = (tArgList *)od->optCookie;
58 * IF we don't have any stacked options,
59 * THEN indicate that we don't have any of these options
61 if (arg_list == NULL) {
62 od->fOptState &= OPTST_PERSISTENT_MASK;
63 if ((od->fOptState & OPTST_INITENABLED) == 0)
64 od->fOptState |= OPTST_DISABLED;
73 if (regcomp(&re, od->optArg.argString, REG_NOSUB) != 0)
77 * search the list for the entry(s) to remove. Entries that
78 * are removed are *not* copied into the result. The source
79 * index is incremented every time. The destination only when
80 * we are keeping a define.
82 for (i = 0, dIdx = 0, ct = arg_list->useCt; --ct >= 0; i++) {
83 char const * pzSrc = arg_list->apzArgs[ i ];
84 char * pzEq = strchr(pzSrc, '=');
91 res = regexec(&re, pzSrc, (size_t)0, NULL, 0);
95 * Remove this entry by reducing the in-use count
96 * and *not* putting the string pointer back into
109 * IF we have dropped an entry
110 * THEN we have to move the current one.
113 arg_list->apzArgs[ dIdx ] = pzSrc;
120 #else /* not WITH_LIBREGEX */
125 * search the list for the entry(s) to remove. Entries that
126 * are removed are *not* copied into the result. The source
127 * index is incremented every time. The destination only when
128 * we are keeping a define.
130 for (i = 0, dIdx = 0, ct = arg_list->useCt; --ct >= 0; i++) {
131 const char * pzSrc = arg_list->apzArgs[ i ];
132 char * pzEq = strchr(pzSrc, '=');
137 if (strcmp(pzSrc, od->optArg.argString) == 0) {
139 * Remove this entry by reducing the in-use count
140 * and *not* putting the string pointer back into
150 * IF we have dropped an entry
151 * THEN we have to move the current one.
154 arg_list->apzArgs[ dIdx ] = pzSrc;
159 #endif /* WITH_LIBREGEX */
161 * IF we have unstacked everything,
162 * THEN indicate that we don't have any of these options
164 if (arg_list->useCt == 0) {
165 od->fOptState &= OPTST_PERSISTENT_MASK;
166 if ((od->fOptState & OPTST_INITENABLED) == 0)
167 od->fOptState |= OPTST_DISABLED;
169 od->optCookie = NULL;
175 * Put an entry into an argument list. The first argument points to
176 * a pointer to the argument list structure. It gets passed around
177 * as an opaque address.
180 addArgListEntry(void ** ppAL, void * entry)
182 tArgList * pAL = *(void **)ppAL;
185 * IF we have never allocated one of these,
186 * THEN allocate one now
189 pAL = (tArgList *)AGALOC(sizeof(*pAL), "new option arg stack");
193 pAL->allocCt = MIN_ARG_ALLOC_CT;
198 * ELSE if we are out of room
199 * THEN make it bigger
201 else if (pAL->useCt >= pAL->allocCt) {
202 size_t sz = sizeof(*pAL);
203 pAL->allocCt += INCR_ARG_ALLOC_CT;
206 * The base structure contains space for MIN_ARG_ALLOC_CT
207 * pointers. We subtract it off to find our augment size.
209 sz += sizeof(char *) * ((size_t)pAL->allocCt - MIN_ARG_ALLOC_CT);
210 pAL = (tArgList *)AGREALOC(VOIDP(pAL), sz, "expanded opt arg stack");
217 * Insert the new argument into the list
219 pAL->apzArgs[ (pAL->useCt)++ ] = entry;
223 /*=export_func optionStackArg
226 * what: put option args on a stack
227 * arg: + tOptions * + opts + program options descriptor +
228 * arg: + tOptDesc * + od + the descriptor for this arg +
231 * Keep an entry-ordered list of option arguments.
234 optionStackArg(tOptions * opts, tOptDesc * od)
238 if (INQUERY_CALL(opts, od))
241 if ((od->fOptState & OPTST_RESET) != 0) {
242 tArgList * arg_list = od->optCookie;
244 if (arg_list == NULL)
247 ix = arg_list->useCt;
249 AGFREE(arg_list->apzArgs[ix]);
253 if (od->optArg.argString == NULL)
256 AGDUPSTR(pz, od->optArg.argString, "stack arg");
257 addArgListEntry(&(od->optCookie), VOIDP(pz));
264 * c-file-style: "stroustrup"
265 * indent-tabs-mode: nil
267 * end of autoopts/stack.c */