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 = = = */
32 mustHandleArg(tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
33 char** ppzOpts
, int* pOptsIdx
);
36 mayHandleArg(tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
37 char** ppzOpts
, int* pOptsIdx
);
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.
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
) {
61 * See if an arg string follows the flag character. If not,
62 * the next arg must be the option argument.
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
)
80 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
83 ppzOpts
[ (*pOptsIdx
)++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
88 mayHandleArg(tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
89 char** ppzOpts
, int* pOptsIdx
)
92 * An option argument is optional.
94 switch (pOS
->optType
) {
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.
107 * Look for an argument if we don't already have one (glued on
108 * with a `=' character)
110 if (pOS
->pzOptArg
!= NULL
)
117 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
120 pzArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
122 ppzOpts
[ (*pOptsIdx
)++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
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.
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
)))
139 * See if we can have an arg.
141 if (OPTST_GET_ARGTYPE(pOS
->pOD
->fOptState
) == OPARG_TYPE_NONE
) {
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.
152 pzArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
154 ppzOpts
[ (*pOptsIdx
)++ ] =
155 pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
160 * IF we need another argument, be sure it is there and
163 if (pzArg
[1] == NUL
) {
164 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
166 ppzOpts
[ (*pOptsIdx
)++ ] =
167 pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
176 * If the program wants sorted options (separated operands and options),
177 * then this routine will to the trick.
180 optionSort(tOptions
* pOpts
)
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
))
197 * Make sure we can allocate two full-sized arg vectors.
199 ppzOpts
= malloc(pOpts
->origArgCt
* sizeof(char*));
203 ppzOpds
= malloc(pOpts
->origArgCt
* sizeof(char*));
204 if (ppzOpds
== NULL
) {
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.)
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
) {
230 pzArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
232 ppzOpds
[ opdsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
239 * A single hyphen is an operand.
241 ppzOpds
[ opdsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
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
)++ ];
254 res
= opt_find_long(pOpts
, pzArg
+2, &os
);
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
);
266 res
= opt_find_short(pOpts
, (tAoUC
)pzArg
[1], &os
);
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
,
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
;
302 switch (mustHandleArg(pOpts
, pzArg
+2, &os
, ppzOpts
, &optsIdx
)) {
304 case FAILURE
: errno
= EIO
; goto freeTemps
;
310 while (pOpts
->curOptIdx
< pOpts
->origArgCt
)
311 ppzOpds
[ opdsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
315 memcpy(pOpts
->origArgVect
+ 1, ppzOpts
, optsIdx
* sizeof(char*));
317 memcpy(pOpts
->origArgVect
+ 1 + optsIdx
, ppzOpds
,
318 opdsIdx
* sizeof(char*));
333 * c-file-style: "stroustrup"
334 * indent-tabs-mode: nil
336 * end of autoopts/sort.c */