Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / sntp / libopts / sort.c
blobeec09c6de995a3a7603509e2d2236c2848254013
1 /* $NetBSD$ */
4 /*
5 * sort.c Id: aac1bf81481f4bb149a72129fbd11fe54db7fa35
6 * Time-stamp: "2007-07-04 11:34:52 bkorb"
8 * This module implements argument sorting.
10 * This file is part of AutoOpts, a companion to AutoGen.
11 * AutoOpts is free software.
12 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
14 * AutoOpts is available under any one of two licenses. The license
15 * in use must be one of these two and the choice is under the control
16 * of the user of the license.
18 * The GNU Lesser General Public License, version 3 or later
19 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
21 * The Modified Berkeley Software Distribution License
22 * See the file "COPYING.mbsd"
24 * These files have the following md5sums:
26 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
27 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
28 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
31 /* = = = START-STATIC-FORWARD = = = */
32 /* static forward declarations maintained by mk-fwd */
33 static tSuccess
34 mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
35 char** ppzOpts, int* pOptsIdx );
37 static tSuccess
38 mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
39 char** ppzOpts, int* pOptsIdx );
41 static tSuccess
42 checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS,
43 char** ppzOpts, int* pOptsIdx );
44 /* = = = END-STATIC-FORWARD = = = */
47 * "mustHandleArg" and "mayHandleArg" 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.
51 static tSuccess
52 mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
53 char** ppzOpts, int* pOptsIdx )
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) {
61 case TOPT_SHORT:
63 * See if an arg string follows the flag character. If not,
64 * the next arg must be the option argument.
66 if (*pzArg != NUL)
67 return SUCCESS;
68 break;
70 case TOPT_LONG:
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)
76 return SUCCESS;
77 break;
79 default:
80 return FAILURE;
82 if (pOpts->curOptIdx >= pOpts->origArgCt)
83 return FAILURE;
85 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
86 return SUCCESS;
89 static tSuccess
90 mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
91 char** ppzOpts, int* pOptsIdx )
94 * An option argument is optional.
96 switch (pOS->optType) {
97 case TOPT_SHORT:
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.
103 if (*pzArg != NUL)
104 return SUCCESS;
105 break;
107 case TOPT_LONG:
109 * Look for an argument if we don't already have one (glued on
110 * with a `=' character)
112 if (pOS->pzOptArg != NULL)
113 return SUCCESS;
114 break;
116 default:
117 return FAILURE;
119 if (pOpts->curOptIdx >= pOpts->origArgCt)
120 return PROBLEM;
122 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
123 if (*pzArg != '-')
124 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
125 return SUCCESS;
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.
132 static tSuccess
133 checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS,
134 char** ppzOpts, int* pOptsIdx )
136 while (*pzArg != NUL) {
137 if (FAILED( shortOptionFind( pOpts, (tAoUC)*pzArg, pOS )))
138 return FAILURE;
141 * See if we can have an arg.
143 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
144 pzArg++;
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 (pzArg[1] != NUL)
152 return SUCCESS;
154 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
155 if (*pzArg != '-')
156 ppzOpts[ (*pOptsIdx)++ ] =
157 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
158 return SUCCESS;
160 } else {
162 * IF we need another argument, be sure it is there and
163 * take it.
165 if (pzArg[1] == NUL) {
166 if (pOpts->curOptIdx >= pOpts->origArgCt)
167 return FAILURE;
168 ppzOpts[ (*pOptsIdx)++ ] =
169 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
171 return SUCCESS;
174 return SUCCESS;
178 * If the program wants sorted options (separated operands and options),
179 * then this routine will to the trick.
181 LOCAL void
182 optionSort( tOptions* pOpts )
184 char** ppzOpts;
185 char** ppzOpds;
186 int optsIdx = 0;
187 int 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)
195 || NAMED_OPTS(pOpts))
196 return;
199 * Make sure we can allocate two full-sized arg vectors.
201 ppzOpts = malloc( pOpts->origArgCt * sizeof( char* ));
202 if (ppzOpts == NULL)
203 goto exit_no_mem;
205 ppzOpds = malloc( pOpts->origArgCt * sizeof( char* ));
206 if (ppzOpds == NULL) {
207 free( ppzOpts );
208 goto exit_no_mem;
211 pOpts->curOptIdx = 1;
212 pOpts->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.)
219 for (;;) {
220 char* pzArg;
221 tSuccess res;
224 * If we're out of arguments, we're done. Join the option and
225 * operand lists into the original argument vector.
227 if (pOpts->curOptIdx >= pOpts->origArgCt) {
228 errno = 0;
229 goto joinLists;
232 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
233 if (*pzArg != '-') {
234 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
235 continue;
238 switch (pzArg[1]) {
239 case NUL:
241 * A single hyphen is an operand.
243 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
244 continue;
246 case '-':
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 (pzArg[2] == NUL) {
252 ppzOpts[ optsIdx++ ] =
253 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
254 goto restOperands;
256 res = longOptionFind( pOpts, pzArg+2, &os );
257 break;
259 default:
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 ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) {
266 res = longOptionFind( pOpts, pzArg+1, &os );
267 } else {
268 res = shortOptionFind( pOpts, (tAoUC)pzArg[1], &os );
270 break;
272 if (FAILED( res )) {
273 errno = EINVAL;
274 goto freeTemps;
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 ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->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( checkShortOpts( pOpts, pzArg+2, &os,
292 ppzOpts, &optsIdx )) ) {
293 errno = EINVAL;
294 goto freeTemps;
297 } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
298 switch (mayHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
299 case FAILURE: errno = EIO; goto freeTemps;
300 case PROBLEM: errno = 0; goto joinLists;
303 } else {
304 switch (mustHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
305 case PROBLEM:
306 case FAILURE: errno = EIO; goto freeTemps;
309 } /* for (;;) */
311 restOperands:
312 while (pOpts->curOptIdx < pOpts->origArgCt)
313 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
315 joinLists:
316 if (optsIdx > 0)
317 memcpy( pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof( char* ));
318 if (opdsIdx > 0)
319 memcpy( pOpts->origArgVect + 1 + optsIdx,
320 ppzOpds, opdsIdx * sizeof( char* ));
322 freeTemps:
323 free( ppzOpts );
324 free( ppzOpds );
325 return;
327 exit_no_mem:
328 errno = ENOMEM;
329 return;
333 * Local Variables:
334 * mode: C
335 * c-file-style: "stroustrup"
336 * indent-tabs-mode: nil
337 * End:
338 * end of autoopts/sort.c */