5 * This module implements argument sorting.
11 * This file is part of AutoOpts, a companion to AutoGen.
12 * AutoOpts is free software.
13 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
15 * AutoOpts is available under any one of two licenses. The license
16 * in use must be one of these two and the choice is under the control
17 * of the user of the license.
19 * The GNU Lesser General Public License, version 3 or later
20 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
22 * The Modified Berkeley Software Distribution License
23 * See the file "COPYING.mbsd"
25 * These files have the following sha256 sums:
27 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
28 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
29 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
32 /* = = = START-STATIC-FORWARD = = = */
34 must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
35 char ** opt_txt, uint32_t * opt_idx);
38 maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
39 char ** opt_txt, uint32_t * opt_idx);
42 short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
43 char ** opt_txt, uint32_t * opt_idx);
44 /* = = = END-STATIC-FORWARD = = = */
47 * "must_arg" and "maybe_arg" are really similar. The biggest
48 * difference is that "may" will consume the next argument only if it
49 * does not start with a hyphen and "must" will consume it, hyphen or not.
52 must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
53 char ** opt_txt, uint32_t * opt_idx)
56 * An option argument is required. Long options can either have
57 * a separate command line argument, or an argument attached by
58 * the '=' character. Figure out which.
60 switch (pOS->optType) {
63 * See if an arg string follows the flag character. If not,
64 * the next arg must be the option argument.
72 * See if an arg string has already been assigned (glued on
73 * with an `=' character). If not, the next is the opt arg.
75 if (pOS->pzOptArg != NULL)
82 if (opts->curOptIdx >= opts->origArgCt)
85 opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
90 maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
91 char ** opt_txt, uint32_t * opt_idx)
94 * An option argument is optional.
96 switch (pOS->optType) {
99 * IF nothing is glued on after the current flag character,
100 * THEN see if there is another argument. If so and if it
101 * does *NOT* start with a hyphen, then it is the option arg.
109 * Look for an argument if we don't already have one (glued on
110 * with a `=' character)
112 if (pOS->pzOptArg != NULL)
119 if (opts->curOptIdx >= opts->origArgCt)
122 arg_txt = opts->origArgVect[ opts->curOptIdx ];
124 opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
129 * Process a string of short options glued together. If the last one
130 * does or may take an argument, the do the argument processing and leave.
133 short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
134 char ** opt_txt, uint32_t * opt_idx)
136 while (*arg_txt != NUL) {
137 if (FAILED(opt_find_short(opts, (uint8_t)*arg_txt, pOS)))
141 * See if we can have an arg.
143 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
146 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
148 * Take an argument if it is not attached and it does not
149 * start with a hyphen.
151 if (arg_txt[1] != NUL)
154 arg_txt = opts->origArgVect[ opts->curOptIdx ];
156 opt_txt[ (*opt_idx)++ ] =
157 opts->origArgVect[ (opts->curOptIdx)++ ];
162 * IF we need another argument, be sure it is there and
165 if (arg_txt[1] == NUL) {
166 if (opts->curOptIdx >= opts->origArgCt)
168 opt_txt[ (*opt_idx)++ ] =
169 opts->origArgVect[ (opts->curOptIdx)++ ];
178 * If the program wants sorted options (separated operands and options),
179 * then this routine will to the trick.
182 optionSort(tOptions * opts)
186 uint32_t optsIdx = 0;
187 uint32_t opdsIdx = 0;
189 tOptState os = OPTSTATE_INITIALIZER(DEFINED);
192 * Disable for POSIX conformance, or if there are no operands.
194 if ( (getenv("POSIXLY_CORRECT") != NULL)
199 * Make sure we can allocate two full-sized arg vectors.
201 opt_txt = malloc(opts->origArgCt * sizeof(char *));
205 ppzOpds = malloc(opts->origArgCt * sizeof(char *));
206 if (ppzOpds == NULL) {
212 opts->pzCurOpt = NULL;
215 * Now, process all the options from our current position onward.
216 * (This allows interspersed options and arguments for the few
217 * non-standard programs that require it.)
224 * If we're out of arguments, we're done. Join the option and
225 * operand lists into the original argument vector.
227 if (opts->curOptIdx >= opts->origArgCt) {
232 arg_txt = opts->origArgVect[ opts->curOptIdx ];
233 if (*arg_txt != '-') {
234 ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
238 switch (arg_txt[1]) {
241 * A single hyphen is an operand.
243 ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
248 * Two consecutive hypens. Put them on the options list and then
249 * _always_ force the remainder of the arguments to be operands.
251 if (arg_txt[2] == NUL) {
252 opt_txt[ optsIdx++ ] =
253 opts->origArgVect[ (opts->curOptIdx)++ ];
256 res = opt_find_long(opts, arg_txt+2, &os);
261 * If short options are not allowed, then do long
262 * option processing. Otherwise the character must be a
263 * short (i.e. single character) option.
265 if ((opts->fOptSet & OPTPROC_SHORTOPT) == 0) {
266 res = opt_find_long(opts, arg_txt+1, &os);
268 res = opt_find_short(opts, (uint8_t)arg_txt[1], &os);
278 * We've found an option. Add the argument to the option list.
279 * Next, we have to see if we need to pull another argument to be
280 * used as the option argument.
282 opt_txt[ optsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
284 if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
286 * No option argument. If we have a short option here,
287 * then scan for short options until we get to the end
288 * of the argument string.
290 if ( (os.optType == TOPT_SHORT)
291 && FAILED(short_opt_ck(opts, arg_txt+2, &os, opt_txt,
297 } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
298 switch (maybe_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
299 case FAILURE: errno = EIO; goto freeTemps;
300 case PROBLEM: errno = 0; goto joinLists;
304 switch (must_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
306 case FAILURE: errno = EIO; goto freeTemps;
312 while (opts->curOptIdx < opts->origArgCt)
313 ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
317 memcpy(opts->origArgVect + 1, opt_txt,
318 (size_t)optsIdx * sizeof(char *));
320 memcpy(opts->origArgVect + 1 + optsIdx, ppzOpds,
321 (size_t)opdsIdx * sizeof(char *));
337 * c-file-style: "stroustrup"
338 * indent-tabs-mode: nil
340 * end of autoopts/sort.c */