Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libopts / sort.c
blob60c44f3eb872f90ed326a77cea176872616f7cfe
1 /* $NetBSD$ */
4 /*
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.
9 */
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 */
56 static tSuccess
57 mustHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
58 char** ppzOpts, int* pOptsIdx );
60 static tSuccess
61 mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
62 char** ppzOpts, int* pOptsIdx );
64 static tSuccess
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.
74 static tSuccess
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) {
84 case TOPT_SHORT:
86 * See if an arg string follows the flag character. If not,
87 * the next arg must be the option argument.
89 if (*pzArg != NUL)
90 return SUCCESS;
91 break;
93 case TOPT_LONG:
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)
99 return SUCCESS;
100 break;
102 default:
103 return FAILURE;
105 if (pOpts->curOptIdx >= pOpts->origArgCt)
106 return FAILURE;
108 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
109 return SUCCESS;
112 static tSuccess
113 mayHandleArg( tOptions* pOpts, char* pzArg, tOptState* pOS,
114 char** ppzOpts, int* pOptsIdx )
117 * An option argument is optional.
119 switch (pOS->optType) {
120 case TOPT_SHORT:
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.
126 if (*pzArg != NUL)
127 return SUCCESS;
128 break;
130 case TOPT_LONG:
132 * Look for an argument if we don't already have one (glued on
133 * with a `=' character)
135 if (pOS->pzOptArg != NULL)
136 return SUCCESS;
137 break;
139 default:
140 return FAILURE;
142 if (pOpts->curOptIdx >= pOpts->origArgCt)
143 return PROBLEM;
145 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
146 if (*pzArg != '-')
147 ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
148 return SUCCESS;
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.
155 static tSuccess
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 )))
161 return FAILURE;
164 * See if we can have an arg.
166 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
167 pzArg++;
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.
174 if (pzArg[1] != NUL)
175 return SUCCESS;
177 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
178 if (*pzArg != '-')
179 ppzOpts[ (*pOptsIdx)++ ] =
180 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
181 return SUCCESS;
183 } else {
185 * IF we need another argument, be sure it is there and
186 * take it.
188 if (pzArg[1] == NUL) {
189 if (pOpts->curOptIdx >= pOpts->origArgCt)
190 return FAILURE;
191 ppzOpts[ (*pOptsIdx)++ ] =
192 pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
194 return SUCCESS;
197 return SUCCESS;
201 * If the program wants sorted options (separated operands and options),
202 * then this routine will to the trick.
204 LOCAL void
205 optionSort( tOptions* pOpts )
207 char** ppzOpts;
208 char** ppzOpds;
209 int optsIdx = 0;
210 int opdsIdx = 0;
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))
219 return;
222 * Make sure we can allocate two full-sized arg vectors.
224 ppzOpts = malloc( pOpts->origArgCt * sizeof( char* ));
225 if (ppzOpts == NULL)
226 goto exit_no_mem;
228 ppzOpds = malloc( pOpts->origArgCt * sizeof( char* ));
229 if (ppzOpds == NULL) {
230 free( ppzOpts );
231 goto exit_no_mem;
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.)
242 for (;;) {
243 char* pzArg;
244 tSuccess res;
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) {
251 errno = 0;
252 goto joinLists;
255 pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
256 if (*pzArg != '-') {
257 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
258 continue;
261 switch (pzArg[1]) {
262 case NUL:
264 * A single hyphen is an operand.
266 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
267 continue;
269 case '-':
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)++ ];
277 goto restOperands;
279 res = longOptionFind( pOpts, pzArg+2, &os );
280 break;
282 default:
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 );
290 } else {
291 res = shortOptionFind( pOpts, (tAoUC)pzArg[1], &os );
293 break;
295 if (FAILED( res )) {
296 errno = EINVAL;
297 goto freeTemps;
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 )) ) {
316 errno = EINVAL;
317 goto freeTemps;
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;
326 } else {
327 switch (mustHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) {
328 case PROBLEM:
329 case FAILURE: errno = EIO; goto freeTemps;
332 } /* for (;;) */
334 restOperands:
335 while (pOpts->curOptIdx < pOpts->origArgCt)
336 ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
338 joinLists:
339 if (optsIdx > 0)
340 memcpy( pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof( char* ));
341 if (opdsIdx > 0)
342 memcpy( pOpts->origArgVect + 1 + optsIdx,
343 ppzOpds, opdsIdx * sizeof( char* ));
345 freeTemps:
346 free( ppzOpts );
347 free( ppzOpds );
348 return;
350 exit_no_mem:
351 errno = ENOMEM;
352 return;
356 * Local Variables:
357 * mode: C
358 * c-file-style: "stroustrup"
359 * indent-tabs-mode: nil
360 * End:
361 * end of autoopts/sort.c */