3 * sort.c $Id: sort.c,v 4.10 2007/04/28 22:19:23 bkorb Exp $
4 * Time-stamp: "2006-10-18 11:29:04 bkorb"
6 * This module implements argument sorting.
10 * Automated Options copyright 1992-2007 Bruce Korb
12 * Automated Options is free software.
13 * You may redistribute it and/or modify it under the terms of the
14 * GNU General Public License, as published by the Free Software
15 * Foundation; either version 2, or (at your option) any later version.
17 * Automated Options is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Automated Options. See the file "COPYING". If not,
24 * write to: The Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
28 * As a special exception, Bruce Korb gives permission for additional
29 * uses of the text contained in his release of AutoOpts.
31 * The exception is that, if you link the AutoOpts library with other
32 * files to produce an executable, this does not by itself cause the
33 * resulting executable to be covered by the GNU General Public License.
34 * Your use of that executable is in no way restricted on account of
35 * linking the AutoOpts library code into it.
37 * This exception does not however invalidate any other reasons why
38 * the executable file might be covered by the GNU General Public License.
40 * This exception applies only to the code released by Bruce Korb under
41 * the name AutoOpts. If you copy code from other sources under the
42 * General Public License into a copy of AutoOpts, as the General Public
43 * License permits, the exception does not apply to the code that you add
44 * in this way. To avoid misleading anyone as to the status of such
45 * modified files, you must delete this exception notice from them.
47 * If you write modifications of your own for AutoOpts, it is your choice
48 * whether to permit this exception to apply to your modifications.
49 * If you do not wish that, delete this exception notice.
52 /* = = = START-STATIC-FORWARD = = = */
53 /* static forward declarations maintained by :mkfwd */
55 mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
56 char** ppzOpts, int* pOptsIdx );
59 mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
60 char** ppzOpts, int* pOptsIdx );
63 checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS,
64 char** ppzOpts, int* pOptsIdx );
65 /* = = = END-STATIC-FORWARD = = = */
68 * "mustHandleArg" and "mayHandleArg" are really similar. The biggest
69 * difference is that "may" will consume the next argument only if it
70 * does not start with a hyphen and "must" will consume it, hyphen or not.
73 mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
74 char** ppzOpts, int* pOptsIdx )
77 * An option argument is required. Long options can either have
78 * a separate command line argument, or an argument attached by
79 * the '=' character. Figure out which.
81 switch (pOS->optType) {
84 * See if an arg string follows the flag character. If not,
85 * the next arg must be the option argument.
93 * See if an arg string has already been assigned (glued on
94 * with an `=' character). If not, the next is the opt arg.
96 if (pOS->pzOptArg != NULL)
103 if (pOpts->curOptIdx >= pOpts->origArgCt)
106 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
111 mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
112 char** ppzOpts, int* pOptsIdx )
115 * An option argument is optional.
117 switch (pOS->optType) {
120 * IF nothing is glued on after the current flag character,
121 * THEN see if there is another argument. If so and if it
122 * does *NOT* start with a hyphen, then it is the option arg.
130 * Look for an argument if we don't already have one (glued on
131 * with a `=' character)
133 if (pOS->pzOptArg != NULL)
140 if (pOpts->curOptIdx >= pOpts->origArgCt)
143 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
145 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
150 * Process a string of short options glued together. If the last one
151 * does or may take an argument, the do the argument processing and leave.
154 checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS,
155 char** ppzOpts, int* pOptsIdx )
157 while (*pzArg != NUL) {
158 if (FAILED( shortOptionFind( pOpts, (tAoUC)*pzArg, pOS )))
162 * See if we can have an arg.
164 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
167 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
169 * Take an argument if it is not attached and it does not
170 * start with a hyphen.
175 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
177 ppzOpts[ (*pOptsIdx)++ ] =
178 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
183 * IF we need another argument, be sure it is there and
186 if (pzArg[1] == NUL) {
187 if (pOpts->curOptIdx >= pOpts->origArgCt)
189 ppzOpts[ (*pOptsIdx)++ ] =
190 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
199 * If the program wants sorted options (separated operands and options),
200 * then this routine will to the trick.
203 optionSort( tOptions* pOpts )
210 tOptState os = OPTSTATE_INITIALIZER(DEFINED);
213 * Disable for POSIX conformance, or if there are no operands.
215 if ( (getenv( "POSIXLY_CORRECT" ) != NULL)
216 || NAMED_OPTS(pOpts))
220 * Make sure we can allocate two full-sized arg vectors.
222 ppzOpts = malloc( pOpts->origArgCt * sizeof( char* ));
226 ppzOpds = malloc( pOpts->origArgCt * sizeof( char* ));
227 if (ppzOpds == NULL) {
232 pOpts->curOptIdx = 1;
233 pOpts->pzCurOpt = NULL;
236 * Now, process all the options from our current position onward.
237 * (This allows interspersed options and arguments for the few
238 * non-standard programs that require it.)
245 * If we're out of arguments, we're done. Join the option and
246 * operand lists into the original argument vector.
248 if (pOpts->curOptIdx >= pOpts->origArgCt) {
253 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
255 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
262 * A single hyphen is an operand.
264 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
269 * Two consecutive hypens. Put them on the options list and then
270 * _always_ force the remainder of the arguments to be operands.
272 if (pzArg[2] == NUL) {
273 ppzOpts[ optsIdx++ ] =
274 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
277 res = longOptionFind( pOpts, pzArg+2, &os );
282 * If short options are not allowed, then do long
283 * option processing. Otherwise the character must be a
284 * short (i.e. single character) option.
286 if ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) {
287 res = longOptionFind( pOpts, pzArg+1, &os );
289 res = shortOptionFind( pOpts, (tAoUC)pzArg[1], &os );
299 * We've found an option. Add the argument to the option list.
300 * Next, we have to see if we need to pull another argument to be
301 * used as the option argument.
303 ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
305 if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
307 * No option argument. If we have a short option here,
308 * then scan for short options until we get to the end
309 * of the argument string.
311 if ( (os.optType == TOPT_SHORT)
312 && FAILED( checkShortOpts( pOpts, pzArg+2, &os,
313 ppzOpts, &optsIdx )) ) {
318 } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
319 switch (mayHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
320 case FAILURE: errno = EIO; goto freeTemps;
321 case PROBLEM: errno = 0; goto joinLists;
325 switch (mustHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
327 case FAILURE: errno = EIO; goto freeTemps;
333 while (pOpts->curOptIdx < pOpts->origArgCt)
334 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
338 memcpy( pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof( char* ));
340 memcpy( pOpts->origArgVect + 1 + optsIdx,
341 ppzOpds, opdsIdx * sizeof( char* ));
356 * c-file-style: "stroustrup"
357 * indent-tabs-mode: nil
359 * end of autoopts/sort.c */