bumped version
[gnutls.git] / src / libopts / sort.c
bloba05588dd1b42c796769c902bc36360d755d74c32
2 /*
3 * \file sort.c
5 * Time-stamp: "2011-05-24 18:07:14 bkorb"
7 * This module implements argument sorting.
9 * This file is part of AutoOpts, a companion to AutoGen.
10 * AutoOpts is free software.
11 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
13 * AutoOpts is available under any one of two licenses. The license
14 * in use must be one of these two and the choice is under the control
15 * of the user of the license.
17 * The GNU Lesser General Public License, version 3 or later
18 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
20 * The Modified Berkeley Software Distribution License
21 * See the file "COPYING.mbsd"
23 * These files have the following md5sums:
25 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
26 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
27 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
30 /* = = = START-STATIC-FORWARD = = = */
31 static tSuccess
32 mustHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
33 char** ppzOpts, int* pOptsIdx);
35 static tSuccess
36 mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
37 char** ppzOpts, int* pOptsIdx);
39 static tSuccess
40 checkShortOpts(tOptions* pOpts, char* pzArg, tOptState* pOS,
41 char** ppzOpts, int* pOptsIdx);
42 /* = = = END-STATIC-FORWARD = = = */
45 * "mustHandleArg" and "mayHandleArg" are really similar. The biggest
46 * difference is that "may" will consume the next argument only if it
47 * does not start with a hyphen and "must" will consume it, hyphen or not.
49 static tSuccess
50 mustHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
51 char** ppzOpts, int* pOptsIdx)
54 * An option argument is required. Long options can either have
55 * a separate command line argument, or an argument attached by
56 * the '=' character. Figure out which.
58 switch (pOS->optType) {
59 case TOPT_SHORT:
61 * See if an arg string follows the flag character. If not,
62 * the next arg must be the option argument.
64 if (*pzArg != NUL)
65 return SUCCESS;
66 break;
68 case TOPT_LONG:
70 * See if an arg string has already been assigned (glued on
71 * with an `=' character). If not, the next is the opt arg.
73 if (pOS->pzOptArg != NULL)
74 return SUCCESS;
75 break;
77 default:
78 return FAILURE;
80 if (pOpts->curOptIdx >= pOpts->origArgCt)
81 return FAILURE;
83 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
84 return SUCCESS;
87 static tSuccess
88 mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
89 char** ppzOpts, int* pOptsIdx)
92 * An option argument is optional.
94 switch (pOS->optType) {
95 case TOPT_SHORT:
97 * IF nothing is glued on after the current flag character,
98 * THEN see if there is another argument. If so and if it
99 * does *NOT* start with a hyphen, then it is the option arg.
101 if (*pzArg != NUL)
102 return SUCCESS;
103 break;
105 case TOPT_LONG:
107 * Look for an argument if we don't already have one (glued on
108 * with a `=' character)
110 if (pOS->pzOptArg != NULL)
111 return SUCCESS;
112 break;
114 default:
115 return FAILURE;
117 if (pOpts->curOptIdx >= pOpts->origArgCt)
118 return PROBLEM;
120 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
121 if (*pzArg != '-')
122 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
123 return SUCCESS;
127 * Process a string of short options glued together. If the last one
128 * does or may take an argument, the do the argument processing and leave.
130 static tSuccess
131 checkShortOpts(tOptions* pOpts, char* pzArg, tOptState* pOS,
132 char** ppzOpts, int* pOptsIdx)
134 while (*pzArg != NUL) {
135 if (FAILED(opt_find_short(pOpts, (tAoUC)*pzArg, pOS)))
136 return FAILURE;
139 * See if we can have an arg.
141 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
142 pzArg++;
144 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
146 * Take an argument if it is not attached and it does not
147 * start with a hyphen.
149 if (pzArg[1] != NUL)
150 return SUCCESS;
152 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
153 if (*pzArg != '-')
154 ppzOpts[ (*pOptsIdx)++ ] =
155 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
156 return SUCCESS;
158 } else {
160 * IF we need another argument, be sure it is there and
161 * take it.
163 if (pzArg[1] == NUL) {
164 if (pOpts->curOptIdx >= pOpts->origArgCt)
165 return FAILURE;
166 ppzOpts[ (*pOptsIdx)++ ] =
167 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
169 return SUCCESS;
172 return SUCCESS;
176 * If the program wants sorted options (separated operands and options),
177 * then this routine will to the trick.
179 LOCAL void
180 optionSort(tOptions* pOpts)
182 char** ppzOpts;
183 char** ppzOpds;
184 int optsIdx = 0;
185 int opdsIdx = 0;
187 tOptState os = OPTSTATE_INITIALIZER(DEFINED);
190 * Disable for POSIX conformance, or if there are no operands.
192 if ( (getenv("POSIXLY_CORRECT") != NULL)
193 || NAMED_OPTS(pOpts))
194 return;
197 * Make sure we can allocate two full-sized arg vectors.
199 ppzOpts = malloc(pOpts->origArgCt * sizeof(char*));
200 if (ppzOpts == NULL)
201 goto exit_no_mem;
203 ppzOpds = malloc(pOpts->origArgCt * sizeof(char*));
204 if (ppzOpds == NULL) {
205 free(ppzOpts);
206 goto exit_no_mem;
209 pOpts->curOptIdx = 1;
210 pOpts->pzCurOpt = NULL;
213 * Now, process all the options from our current position onward.
214 * (This allows interspersed options and arguments for the few
215 * non-standard programs that require it.)
217 for (;;) {
218 char* pzArg;
219 tSuccess res;
222 * If we're out of arguments, we're done. Join the option and
223 * operand lists into the original argument vector.
225 if (pOpts->curOptIdx >= pOpts->origArgCt) {
226 errno = 0;
227 goto joinLists;
230 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
231 if (*pzArg != '-') {
232 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
233 continue;
236 switch (pzArg[1]) {
237 case NUL:
239 * A single hyphen is an operand.
241 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
242 continue;
244 case '-':
246 * Two consecutive hypens. Put them on the options list and then
247 * _always_ force the remainder of the arguments to be operands.
249 if (pzArg[2] == NUL) {
250 ppzOpts[ optsIdx++ ] =
251 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
252 goto restOperands;
254 res = opt_find_long(pOpts, pzArg+2, &os);
255 break;
257 default:
259 * If short options are not allowed, then do long
260 * option processing. Otherwise the character must be a
261 * short (i.e. single character) option.
263 if ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) {
264 res = opt_find_long(pOpts, pzArg+1, &os);
265 } else {
266 res = opt_find_short(pOpts, (tAoUC)pzArg[1], &os);
268 break;
270 if (FAILED(res)) {
271 errno = EINVAL;
272 goto freeTemps;
276 * We've found an option. Add the argument to the option list.
277 * Next, we have to see if we need to pull another argument to be
278 * used as the option argument.
280 ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
282 if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
284 * No option argument. If we have a short option here,
285 * then scan for short options until we get to the end
286 * of the argument string.
288 if ( (os.optType == TOPT_SHORT)
289 && FAILED(checkShortOpts(pOpts, pzArg+2, &os, ppzOpts,
290 &optsIdx)) ) {
291 errno = EINVAL;
292 goto freeTemps;
295 } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
296 switch (mayHandleArg(pOpts, pzArg+2, &os, ppzOpts, &optsIdx)) {
297 case FAILURE: errno = EIO; goto freeTemps;
298 case PROBLEM: errno = 0; goto joinLists;
301 } else {
302 switch (mustHandleArg(pOpts, pzArg+2, &os, ppzOpts, &optsIdx)) {
303 case PROBLEM:
304 case FAILURE: errno = EIO; goto freeTemps;
307 } /* for (;;) */
309 restOperands:
310 while (pOpts->curOptIdx < pOpts->origArgCt)
311 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
313 joinLists:
314 if (optsIdx > 0)
315 memcpy(pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof(char*));
316 if (opdsIdx > 0)
317 memcpy(pOpts->origArgVect + 1 + optsIdx, ppzOpds,
318 opdsIdx * sizeof(char*));
320 freeTemps:
321 free(ppzOpts);
322 free(ppzOpds);
323 return;
325 exit_no_mem:
326 errno = ENOMEM;
327 return;
331 * Local Variables:
332 * mode: C
333 * c-file-style: "stroustrup"
334 * indent-tabs-mode: nil
335 * End:
336 * end of autoopts/sort.c */