5 * sort.c Id: sort.c,v 4.10 2007/04/28 22:19:23 bkorb Exp
6 * Time-stamp: "2006-10-18 11:29:04 bkorb"
8 * This module implements argument sorting.
12 * Automated Options copyright 1992-2007 Bruce Korb
14 * Automated Options is free software.
15 * You may redistribute it and/or modify it under the terms of the
16 * GNU General Public License, as published by the Free Software
17 * Foundation; either version 2, or (at your option) any later version.
19 * Automated Options is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Automated Options. See the file "COPYING". If not,
26 * write to: The Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
30 * As a special exception, Bruce Korb gives permission for additional
31 * uses of the text contained in his release of AutoOpts.
33 * The exception is that, if you link the AutoOpts library with other
34 * files to produce an executable, this does not by itself cause the
35 * resulting executable to be covered by the GNU General Public License.
36 * Your use of that executable is in no way restricted on account of
37 * linking the AutoOpts library code into it.
39 * This exception does not however invalidate any other reasons why
40 * the executable file might be covered by the GNU General Public License.
42 * This exception applies only to the code released by Bruce Korb under
43 * the name AutoOpts. If you copy code from other sources under the
44 * General Public License into a copy of AutoOpts, as the General Public
45 * License permits, the exception does not apply to the code that you add
46 * in this way. To avoid misleading anyone as to the status of such
47 * modified files, you must delete this exception notice from them.
49 * If you write modifications of your own for AutoOpts, it is your choice
50 * whether to permit this exception to apply to your modifications.
51 * If you do not wish that, delete this exception notice.
54 /* = = = START-STATIC-FORWARD = = = */
55 /* static forward declarations maintained by :mkfwd */
57 mustHandleArg( tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
58 char** ppzOpts
, int* pOptsIdx
);
61 mayHandleArg( tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
62 char** ppzOpts
, int* pOptsIdx
);
65 checkShortOpts( tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
66 char** ppzOpts
, int* pOptsIdx
);
67 /* = = = END-STATIC-FORWARD = = = */
70 * "mustHandleArg" and "mayHandleArg" are really similar. The biggest
71 * difference is that "may" will consume the next argument only if it
72 * does not start with a hyphen and "must" will consume it, hyphen or not.
75 mustHandleArg( tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
76 char** ppzOpts
, int* pOptsIdx
)
79 * An option argument is required. Long options can either have
80 * a separate command line argument, or an argument attached by
81 * the '=' character. Figure out which.
83 switch (pOS
->optType
) {
86 * See if an arg string follows the flag character. If not,
87 * the next arg must be the option argument.
95 * See if an arg string has already been assigned (glued on
96 * with an `=' character). If not, the next is the opt arg.
98 if (pOS
->pzOptArg
!= NULL
)
105 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
108 ppzOpts
[ (*pOptsIdx
)++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
113 mayHandleArg( tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
114 char** ppzOpts
, int* pOptsIdx
)
117 * An option argument is optional.
119 switch (pOS
->optType
) {
122 * IF nothing is glued on after the current flag character,
123 * THEN see if there is another argument. If so and if it
124 * does *NOT* start with a hyphen, then it is the option arg.
132 * Look for an argument if we don't already have one (glued on
133 * with a `=' character)
135 if (pOS
->pzOptArg
!= NULL
)
142 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
145 pzArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
147 ppzOpts
[ (*pOptsIdx
)++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
152 * Process a string of short options glued together. If the last one
153 * does or may take an argument, the do the argument processing and leave.
156 checkShortOpts( tOptions
* pOpts
, char* pzArg
, tOptState
* pOS
,
157 char** ppzOpts
, int* pOptsIdx
)
159 while (*pzArg
!= NUL
) {
160 if (FAILED( shortOptionFind( pOpts
, (tAoUC
)*pzArg
, pOS
)))
164 * See if we can have an arg.
166 if (OPTST_GET_ARGTYPE(pOS
->pOD
->fOptState
) == OPARG_TYPE_NONE
) {
169 } else if (pOS
->pOD
->fOptState
& OPTST_ARG_OPTIONAL
) {
171 * Take an argument if it is not attached and it does not
172 * start with a hyphen.
177 pzArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
179 ppzOpts
[ (*pOptsIdx
)++ ] =
180 pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
185 * IF we need another argument, be sure it is there and
188 if (pzArg
[1] == NUL
) {
189 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
191 ppzOpts
[ (*pOptsIdx
)++ ] =
192 pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
201 * If the program wants sorted options (separated operands and options),
202 * then this routine will to the trick.
205 optionSort( tOptions
* pOpts
)
212 tOptState os
= OPTSTATE_INITIALIZER(DEFINED
);
215 * Disable for POSIX conformance, or if there are no operands.
217 if ( (getenv( "POSIXLY_CORRECT" ) != NULL
)
218 || NAMED_OPTS(pOpts
))
222 * Make sure we can allocate two full-sized arg vectors.
224 ppzOpts
= malloc( pOpts
->origArgCt
* sizeof( char* ));
228 ppzOpds
= malloc( pOpts
->origArgCt
* sizeof( char* ));
229 if (ppzOpds
== NULL
) {
234 pOpts
->curOptIdx
= 1;
235 pOpts
->pzCurOpt
= NULL
;
238 * Now, process all the options from our current position onward.
239 * (This allows interspersed options and arguments for the few
240 * non-standard programs that require it.)
247 * If we're out of arguments, we're done. Join the option and
248 * operand lists into the original argument vector.
250 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
) {
255 pzArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
257 ppzOpds
[ opdsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
264 * A single hyphen is an operand.
266 ppzOpds
[ opdsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
271 * Two consecutive hypens. Put them on the options list and then
272 * _always_ force the remainder of the arguments to be operands.
274 if (pzArg
[2] == NUL
) {
275 ppzOpts
[ optsIdx
++ ] =
276 pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
279 res
= longOptionFind( pOpts
, pzArg
+2, &os
);
284 * If short options are not allowed, then do long
285 * option processing. Otherwise the character must be a
286 * short (i.e. single character) option.
288 if ((pOpts
->fOptSet
& OPTPROC_SHORTOPT
) == 0) {
289 res
= longOptionFind( pOpts
, pzArg
+1, &os
);
291 res
= shortOptionFind( pOpts
, (tAoUC
)pzArg
[1], &os
);
301 * We've found an option. Add the argument to the option list.
302 * Next, we have to see if we need to pull another argument to be
303 * used as the option argument.
305 ppzOpts
[ optsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
307 if (OPTST_GET_ARGTYPE(os
.pOD
->fOptState
) == OPARG_TYPE_NONE
) {
309 * No option argument. If we have a short option here,
310 * then scan for short options until we get to the end
311 * of the argument string.
313 if ( (os
.optType
== TOPT_SHORT
)
314 && FAILED( checkShortOpts( pOpts
, pzArg
+2, &os
,
315 ppzOpts
, &optsIdx
)) ) {
320 } else if (os
.pOD
->fOptState
& OPTST_ARG_OPTIONAL
) {
321 switch (mayHandleArg( pOpts
, pzArg
+2, &os
, ppzOpts
, &optsIdx
)) {
322 case FAILURE
: errno
= EIO
; goto freeTemps
;
323 case PROBLEM
: errno
= 0; goto joinLists
;
327 switch (mustHandleArg( pOpts
, pzArg
+2, &os
, ppzOpts
, &optsIdx
)) {
329 case FAILURE
: errno
= EIO
; goto freeTemps
;
335 while (pOpts
->curOptIdx
< pOpts
->origArgCt
)
336 ppzOpds
[ opdsIdx
++ ] = pOpts
->origArgVect
[ (pOpts
->curOptIdx
)++ ];
340 memcpy( pOpts
->origArgVect
+ 1, ppzOpts
, optsIdx
* sizeof( char* ));
342 memcpy( pOpts
->origArgVect
+ 1 + optsIdx
,
343 ppzOpds
, opdsIdx
* sizeof( char* ));
358 * c-file-style: "stroustrup"
359 * indent-tabs-mode: nil
361 * end of autoopts/sort.c */