5 * Time-stamp: "2012-03-31 13:16:41 bkorb"
7 * This is a special option processing routine that will save the
8 * argument to an option in a FIFO queue.
10 * This file is part of AutoOpts, a companion to AutoGen.
11 * AutoOpts is free software.
12 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
14 * AutoOpts is available under any one of two licenses. The license
15 * in use must be one of these two and the choice is under the control
16 * of the user of the license.
18 * The GNU Lesser General Public License, version 3 or later
19 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
21 * The Modified Berkeley Software Distribution License
22 * See the file "COPYING.mbsd"
24 * These files have the following md5sums:
26 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
27 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
28 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
32 # include REGEX_HEADER
35 /*=export_func optionUnstackArg
38 * what: Remove option args from a stack
39 * arg: + tOptions* + pOpts + program options descriptor +
40 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
43 * Invoked for options that are equivalenced to stacked options.
46 optionUnstackArg(tOptions
* pOpts
, tOptDesc
* pOptDesc
)
52 if ((pOptDesc
->fOptState
& OPTST_RESET
) != 0)
54 pAL
= (tArgList
*)pOptDesc
->optCookie
;
57 * IF we don't have any stacked options,
58 * THEN indicate that we don't have any of these options
61 pOptDesc
->fOptState
&= OPTST_PERSISTENT_MASK
;
62 if ((pOptDesc
->fOptState
& OPTST_INITENABLED
) == 0)
63 pOptDesc
->fOptState
|= OPTST_DISABLED
;
72 if (regcomp(&re
, pOptDesc
->optArg
.argString
, REG_NOSUB
) != 0)
76 * search the list for the entry(s) to remove. Entries that
77 * are removed are *not* copied into the result. The source
78 * index is incremented every time. The destination only when
79 * we are keeping a define.
81 for (i
= 0, dIdx
= 0, ct
= pAL
->useCt
; --ct
>= 0; i
++) {
82 char const * pzSrc
= pAL
->apzArgs
[ i
];
83 char * pzEq
= strchr(pzSrc
, '=');
90 res
= regexec(&re
, pzSrc
, (size_t)0, NULL
, 0);
94 * Remove this entry by reducing the in-use count
95 * and *not* putting the string pointer back into
108 * IF we have dropped an entry
109 * THEN we have to move the current one.
112 pAL
->apzArgs
[ dIdx
] = pzSrc
;
119 #else /* not WITH_LIBREGEX */
124 * search the list for the entry(s) to remove. Entries that
125 * are removed are *not* copied into the result. The source
126 * index is incremented every time. The destination only when
127 * we are keeping a define.
129 for (i
= 0, dIdx
= 0, ct
= pAL
->useCt
; --ct
>= 0; i
++) {
130 tCC
* pzSrc
= pAL
->apzArgs
[ i
];
131 char* pzEq
= strchr(pzSrc
, '=');
136 if (strcmp(pzSrc
, pOptDesc
->optArg
.argString
) == 0) {
138 * Remove this entry by reducing the in-use count
139 * and *not* putting the string pointer back into
149 * IF we have dropped an entry
150 * THEN we have to move the current one.
153 pAL
->apzArgs
[ dIdx
] = pzSrc
;
158 #endif /* WITH_LIBREGEX */
160 * IF we have unstacked everything,
161 * THEN indicate that we don't have any of these options
163 if (pAL
->useCt
== 0) {
164 pOptDesc
->fOptState
&= OPTST_PERSISTENT_MASK
;
165 if ((pOptDesc
->fOptState
& OPTST_INITENABLED
) == 0)
166 pOptDesc
->fOptState
|= OPTST_DISABLED
;
168 pOptDesc
->optCookie
= NULL
;
174 * Put an entry into an argument list. The first argument points to
175 * a pointer to the argument list structure. It gets passed around
176 * as an opaque address.
179 addArgListEntry(void** ppAL
, void* entry
)
181 tArgList
* pAL
= *(void**)ppAL
;
184 * IF we have never allocated one of these,
185 * THEN allocate one now
188 pAL
= (tArgList
*)AGALOC(sizeof(*pAL
), "new option arg stack");
192 pAL
->allocCt
= MIN_ARG_ALLOC_CT
;
197 * ELSE if we are out of room
198 * THEN make it bigger
200 else if (pAL
->useCt
>= pAL
->allocCt
) {
201 size_t sz
= sizeof(*pAL
);
202 pAL
->allocCt
+= INCR_ARG_ALLOC_CT
;
205 * The base structure contains space for MIN_ARG_ALLOC_CT
206 * pointers. We subtract it off to find our augment size.
208 sz
+= sizeof(char*) * (pAL
->allocCt
- MIN_ARG_ALLOC_CT
);
209 pAL
= (tArgList
*)AGREALOC((void*)pAL
, sz
, "expanded opt arg stack");
216 * Insert the new argument into the list
218 pAL
->apzArgs
[ (pAL
->useCt
)++ ] = entry
;
222 /*=export_func optionStackArg
225 * what: put option args on a stack
226 * arg: + tOptions* + pOpts + program options descriptor +
227 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
230 * Keep an entry-ordered list of option arguments.
233 optionStackArg(tOptions
* pOpts
, tOptDesc
* pOD
)
239 if ((pOD
->fOptState
& OPTST_RESET
) != 0) {
240 tArgList
* pAL
= (void*)pOD
->optCookie
;
247 AGFREE(pAL
->apzArgs
[ix
]);
251 if (pOD
->optArg
.argString
== NULL
)
254 AGDUPSTR(pz
, pOD
->optArg
.argString
, "stack arg");
255 addArgListEntry(&(pOD
->optCookie
), (void*)pz
);
261 * c-file-style: "stroustrup"
262 * indent-tabs-mode: nil
264 * end of autoopts/stack.c */