Updated gnulib and added hash-pjw-bare
[gnutls.git] / src / libopts / stack.c
blob094b2a1de3c4dad9d4f0aa0fc4b70654332c31da
2 /**
3 * \file stack.c
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
31 #ifdef WITH_LIBREGEX
32 # include REGEX_HEADER
33 #endif
35 /*=export_func optionUnstackArg
36 * private:
38 * what: Remove option args from a stack
39 * arg: + tOptions* + pOpts + program options descriptor +
40 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
42 * doc:
43 * Invoked for options that are equivalenced to stacked options.
44 =*/
45 void
46 optionUnstackArg(tOptions * pOpts, tOptDesc * pOptDesc)
48 tArgList * pAL;
50 (void)pOpts;
52 if ((pOptDesc->fOptState & OPTST_RESET) != 0)
53 return;
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
60 if (pAL == NULL) {
61 pOptDesc->fOptState &= OPTST_PERSISTENT_MASK;
62 if ((pOptDesc->fOptState & OPTST_INITENABLED) == 0)
63 pOptDesc->fOptState |= OPTST_DISABLED;
64 return;
67 #ifdef WITH_LIBREGEX
69 regex_t re;
70 int i, ct, dIdx;
72 if (regcomp(&re, pOptDesc->optArg.argString, REG_NOSUB) != 0)
73 return;
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, '=');
84 int res;
87 if (pzEq != NULL)
88 *pzEq = NUL;
90 res = regexec(&re, pzSrc, (size_t)0, NULL, 0);
91 switch (res) {
92 case 0:
94 * Remove this entry by reducing the in-use count
95 * and *not* putting the string pointer back into
96 * the list.
98 AGFREE(pzSrc);
99 pAL->useCt--;
100 break;
102 default:
103 case REG_NOMATCH:
104 if (pzEq != NULL)
105 *pzEq = '=';
108 * IF we have dropped an entry
109 * THEN we have to move the current one.
111 if (dIdx != i)
112 pAL->apzArgs[ dIdx ] = pzSrc;
113 dIdx++;
117 regfree(&re);
119 #else /* not WITH_LIBREGEX */
121 int i, ct, dIdx;
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, '=');
133 if (pzEq != NULL)
134 *pzEq = NUL;
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
140 * the list.
142 AGFREE(pzSrc);
143 pAL->useCt--;
144 } else {
145 if (pzEq != NULL)
146 *pzEq = '=';
149 * IF we have dropped an entry
150 * THEN we have to move the current one.
152 if (dIdx != i)
153 pAL->apzArgs[ dIdx ] = pzSrc;
154 dIdx++;
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;
167 AGFREE((void*)pAL);
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.
178 LOCAL void
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
187 if (pAL == NULL) {
188 pAL = (tArgList*)AGALOC(sizeof(*pAL), "new option arg stack");
189 if (pAL == NULL)
190 return;
191 pAL->useCt = 0;
192 pAL->allocCt = MIN_ARG_ALLOC_CT;
193 *ppAL = (void*)pAL;
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");
210 if (pAL == NULL)
211 return;
212 *ppAL = (void*)pAL;
216 * Insert the new argument into the list
218 pAL->apzArgs[ (pAL->useCt)++ ] = entry;
222 /*=export_func optionStackArg
223 * private:
225 * what: put option args on a stack
226 * arg: + tOptions* + pOpts + program options descriptor +
227 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
229 * doc:
230 * Keep an entry-ordered list of option arguments.
232 void
233 optionStackArg(tOptions * pOpts, tOptDesc * pOD)
235 char * pz;
237 (void)pOpts;
239 if ((pOD->fOptState & OPTST_RESET) != 0) {
240 tArgList* pAL = (void*)pOD->optCookie;
241 int ix;
242 if (pAL == NULL)
243 return;
245 ix = pAL->useCt;
246 while (--ix >= 0)
247 AGFREE(pAL->apzArgs[ix]);
248 AGFREE(pAL);
250 } else {
251 if (pOD->optArg.argString == NULL)
252 return;
254 AGDUPSTR(pz, pOD->optArg.argString, "stack arg");
255 addArgListEntry(&(pOD->optCookie), (void*)pz);
259 * Local Variables:
260 * mode: C
261 * c-file-style: "stroustrup"
262 * indent-tabs-mode: nil
263 * End:
264 * end of autoopts/stack.c */