5 * Id: autoopts.c,v 4.25 2007/04/15 19:01:18 bkorb Exp
6 * Time-stamp: "2007-04-15 11:10:40 bkorb"
8 * This file contains all of the routines that must be linked into
9 * an executable to use the generated option processing. The optional
10 * routines are in separately compiled modules so that they will not
11 * necessarily be linked in.
15 * Automated Options copyright 1992-2007 Bruce Korb
17 * Automated Options is free software.
18 * You may redistribute it and/or modify it under the terms of the
19 * GNU General Public License, as published by the Free Software
20 * Foundation; either version 2, or (at your option) any later version.
22 * Automated Options is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with Automated Options. See the file "COPYING". If not,
29 * write to: The Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor,
31 * Boston, MA 02110-1301, USA.
33 * As a special exception, Bruce Korb gives permission for additional
34 * uses of the text contained in his release of AutoOpts.
36 * The exception is that, if you link the AutoOpts library with other
37 * files to produce an executable, this does not by itself cause the
38 * resulting executable to be covered by the GNU General Public License.
39 * Your use of that executable is in no way restricted on account of
40 * linking the AutoOpts library code into it.
42 * This exception does not however invalidate any other reasons why
43 * the executable file might be covered by the GNU General Public License.
45 * This exception applies only to the code released by Bruce Korb under
46 * the name AutoOpts. If you copy code from other sources under the
47 * General Public License into a copy of AutoOpts, as the General Public
48 * License permits, the exception does not apply to the code that you add
49 * in this way. To avoid misleading anyone as to the status of such
50 * modified files, you must delete this exception notice from them.
52 * If you write modifications of your own for AutoOpts, it is your choice
53 * whether to permit this exception to apply to your modifications.
54 * If you do not wish that, delete this exception notice.
57 static char const zNil
[] = "";
59 /* = = = START-STATIC-FORWARD = = = */
60 /* static forward declarations maintained by :mkfwd */
62 findOptDesc( tOptions
* pOpts
, tOptState
* pOptState
);
65 nextOption( tOptions
* pOpts
, tOptState
* pOptState
);
68 doPresets( tOptions
* pOpts
);
71 checkConsistency( tOptions
* pOpts
);
72 /* = = = END-STATIC-FORWARD = = = */
75 ao_malloc( size_t sz
)
77 void * res
= malloc(sz
);
79 fprintf( stderr
, "malloc of %d bytes failed\n", (int)sz
);
85 #define malloc(_s) ao_malloc(_s)
88 ao_realloc( void *p
, size_t sz
)
90 void * res
= realloc(p
, sz
);
92 fprintf( stderr
, "realloc of %d bytes at 0x%p failed\n", (int)sz
, p
);
98 #define realloc(_p,_s) ao_realloc(_p,_s)
108 #define free(_p) ao_free(_p)
112 ao_strdup( char const *str
)
114 char * res
= strdup(str
);
116 fprintf( stderr
, "strdup of %d byte string failed\n", (int)strlen(str
) );
117 exit( EXIT_FAILURE
);
122 #define strdup(_p) ao_strdup(_p)
124 #ifndef HAVE_PATHFIND
125 # include "compat/pathfind.c"
128 #ifndef HAVE_SNPRINTF
129 # include "compat/snprintf.c"
133 # include "compat/strdup.c"
137 # include "compat/strchr.c"
143 * This routine handles equivalencing, sets the option state flags and
144 * invokes the handler procedure, if any.
147 handleOption( tOptions
* pOpts
, tOptState
* pOptState
)
150 * Save a copy of the option procedure pointer.
151 * If this is an equivalence class option, we still want this proc.
153 tOptDesc
* pOD
= pOptState
->pOD
;
154 tOptProc
* pOP
= pOD
->pOptProc
;
155 if (pOD
->fOptState
& OPTST_ALLOC_ARG
)
156 AGFREE(pOD
->optArg
.argString
);
158 pOD
->optArg
.argString
= pOptState
->pzOptArg
;
161 * IF we are presetting options, then we will ignore any un-presettable
162 * options. They are the ones either marked as such.
164 if ( ((pOpts
->fOptSet
& OPTPROC_PRESETTING
) != 0)
165 && ((pOD
->fOptState
& OPTST_NO_INIT
) != 0)
170 * IF this is an equivalence class option,
172 * Save the option value that got us to this option
173 * entry. (It may not be pOD->optChar[0], if this is an
174 * equivalence entry.)
175 * set the pointer to the equivalence class base
177 if (pOD
->optEquivIndex
!= NO_EQUIVALENT
) {
178 tOptDesc
* p
= pOpts
->pOptDesc
+ pOD
->optEquivIndex
;
181 * IF the current option state has not been defined (set on the
182 * command line), THEN we will allow continued resetting of
183 * the value. Once "defined", then it must not change.
185 if ((pOD
->fOptState
& OPTST_DEFINED
) != 0) {
187 * The equivalenced-to option has been found on the command
188 * line before. Make sure new occurrences are the same type.
190 * IF this option has been previously equivalenced and
191 * it was not the same equivalenced-to option,
192 * THEN we have a usage problem.
194 if (p
->optActualIndex
!= pOD
->optIndex
) {
195 fprintf( stderr
, (char*)zMultiEquiv
, p
->pz_Name
, pOD
->pz_Name
,
196 (pOpts
->pOptDesc
+ p
->optActualIndex
)->pz_Name
);
201 * Set the equivalenced-to actual option index to no-equivalent
202 * so that we set all the entries below. This option may either
203 * never have been selected before, or else it was selected by
204 * some sort of "presetting" mechanism.
206 p
->optActualIndex
= NO_EQUIVALENT
;
209 if (p
->optActualIndex
!= pOD
->optIndex
) {
211 * First time through, copy over the state
212 * and add in the equivalence flag
214 p
->optActualValue
= pOD
->optValue
;
215 p
->optActualIndex
= pOD
->optIndex
;
216 pOptState
->flags
|= OPTST_EQUIVALENCE
;
220 * Copy the most recent option argument. set membership state
221 * is kept in ``p->optCookie''. Do not overwrite.
223 p
->optArg
.argString
= pOD
->optArg
.argString
;
227 pOD
->optActualValue
= pOD
->optValue
;
228 pOD
->optActualIndex
= pOD
->optIndex
;
231 pOD
->fOptState
&= OPTST_PERSISTENT_MASK
;
232 pOD
->fOptState
|= (pOptState
->flags
& ~OPTST_PERSISTENT_MASK
);
235 * Keep track of count only for DEFINED (command line) options.
236 * IF we have too many, build up an error message and bail.
238 if ( (pOD
->fOptState
& OPTST_DEFINED
)
239 && (++pOD
->optOccCt
> pOD
->optMaxCt
) ) {
241 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0) {
243 (pOD
->optEquivIndex
!= NO_EQUIVALENT
) ? zEquiv
: zNil
;
245 fputs( zErrOnly
, stderr
);
247 if (pOD
->optMaxCt
> 1)
248 fprintf(stderr
, zAtMost
, pOD
->optMaxCt
, pOD
->pz_Name
, pzEqv
);
250 fprintf(stderr
, zOnlyOne
, pOD
->pz_Name
, pzEqv
);
257 * If provided a procedure to call, call it
259 if (pOP
!= (tpOptProc
)NULL
)
260 (*pOP
)( pOpts
, pOD
);
266 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
268 * HUNT FOR OPTIONS IN THE ARGUMENT LIST
270 * The next four procedures are "private" to nextOption().
271 * nextOption() uses findOptDesc() to find the next descriptor and it, in
272 * turn, uses longOptionFind() and shortOptionFind() to actually do the hunt.
276 * Find the long option descriptor for the current option
279 longOptionFind( tOptions
* pOpts
, char* pzOptName
, tOptState
* pOptState
)
281 ag_bool disable
= AG_FALSE
;
282 char* pzEq
= strchr( pzOptName
, '=' );
283 tOptDesc
* pOD
= pOpts
->pOptDesc
;
285 int idxLim
= pOpts
->optCt
;
291 * IF the value is attached to the name,
293 * Either way, figure out how long our name is
296 nameLen
= (int)(pzEq
- pzOptName
);
298 } else nameLen
= strlen( pzOptName
);
304 if (strneqvcmp( pzOptName
, pOD
->pz_Name
, nameLen
) == 0) {
306 * IF we have a complete match
307 * THEN it takes priority over any already located partial
309 if (pOD
->pz_Name
[ nameLen
] == NUL
) {
317 * IF there is a disable name
318 * *AND* no argument value has been supplied
319 * (disabled options may have no argument)
320 * *AND* the option name matches the disable name
323 else if ( (pOD
->pz_DisableName
!= NULL
)
324 && (strneqvcmp(pzOptName
, pOD
->pz_DisableName
, nameLen
) == 0)
329 * IF we have a complete match
330 * THEN it takes priority over any already located partial
332 if (pOD
->pz_DisableName
[ nameLen
] == NUL
) {
343 * We found a partial match, either regular or disabling.
344 * Remember the index for later.
351 } while (pOD
++, (++idx
< idxLim
));
357 * Make sure we either found an exact match or found only one partial
361 * IF we found a disablement name,
362 * THEN set the bit in the callers' flag word
365 pOptState
->flags
|= OPTST_DISABLED
;
367 pOptState
->pOD
= pOpts
->pOptDesc
+ matchIdx
;
368 pOptState
->pzOptArg
= pzEq
;
369 pOptState
->optType
= TOPT_LONG
;
374 * IF there is no equal sign
375 * *AND* we are using named arguments
376 * *AND* there is a default named option,
377 * THEN return that option.
381 && (pOpts
->specOptIdx
.default_opt
!= NO_EQUIVALENT
)) {
382 pOptState
->pOD
= pOpts
->pOptDesc
+ pOpts
->specOptIdx
.default_opt
;
384 pOptState
->pzOptArg
= pzOptName
;
385 pOptState
->optType
= TOPT_DEFAULT
;
390 * IF we are to stop on errors (the default, actually)
391 * THEN call the usage procedure.
393 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0) {
394 fprintf( stderr
, zIllOptStr
, pOpts
->pzProgPath
,
395 (matchCt
== 0) ? zIllegal
: zAmbiguous
, pzOptName
);
396 (*pOpts
->pUsageProc
)( pOpts
, EXIT_FAILURE
);
406 * Find the short option descriptor for the current option
409 shortOptionFind( tOptions
* pOpts
, uint_t optValue
, tOptState
* pOptState
)
411 tOptDesc
* pRes
= pOpts
->pOptDesc
;
412 int ct
= pOpts
->optCt
;
415 * Search the option list
419 * IF the values match,
422 if ((! SKIP_OPT(pRes
)) && (optValue
== pRes
->optValue
)) {
423 pOptState
->pOD
= pRes
;
424 pOptState
->optType
= TOPT_SHORT
;
429 * Advance to next option description
434 * IF we have searched everything, ...
441 * IF the character value is a digit
442 * AND there is a special number option ("-n")
443 * THEN the result is the "option" itself and the
444 * option is the specially marked "number" option.
446 if ( isdigit( optValue
)
447 && (pOpts
->specOptIdx
.number_option
!= NO_EQUIVALENT
) ) {
449 pRes
= pOpts
->pOptDesc
+ pOpts
->specOptIdx
.number_option
;
451 pOptState
->optType
= TOPT_SHORT
;
456 * IF we are to stop on errors (the default, actually)
457 * THEN call the usage procedure.
459 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0) {
460 fprintf( stderr
, zIllOptChr
, pOpts
->pzProgPath
, optValue
);
461 (*pOpts
->pUsageProc
)( pOpts
, EXIT_FAILURE
);
471 * Find the option descriptor for the current option
474 findOptDesc( tOptions
* pOpts
, tOptState
* pOptState
)
477 * IF we are continuing a short option list (e.g. -xyz...)
478 * THEN continue a single flag option.
479 * OTHERWISE see if there is room to advance and then do so.
481 if ((pOpts
->pzCurOpt
!= NULL
) && (*pOpts
->pzCurOpt
!= NUL
))
482 return shortOptionFind( pOpts
, (tAoUC
)*(pOpts
->pzCurOpt
), pOptState
);
484 if (pOpts
->curOptIdx
>= pOpts
->origArgCt
)
485 return PROBLEM
; /* NORMAL COMPLETION */
487 pOpts
->pzCurOpt
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
490 * IF all arguments must be named options, ...
492 if (NAMED_OPTS(pOpts
)) {
493 char* pz
= pOpts
->pzCurOpt
;
497 * Skip over any flag/option markers.
498 * In this mode, they are not required.
500 while (*pz
== '-') pz
++;
502 return longOptionFind( pOpts
, pz
, pOptState
);
506 * Note the kind of flag/option marker
508 if (*((pOpts
->pzCurOpt
)++) != '-')
509 return PROBLEM
; /* NORMAL COMPLETION - this + rest are operands */
512 * Special hack for a hyphen by itself
514 if (*(pOpts
->pzCurOpt
) == NUL
)
515 return PROBLEM
; /* NORMAL COMPLETION - this + rest are operands */
518 * The current argument is to be processed as an option argument
523 * We have an option marker.
524 * Test the next character for long option indication
526 if (pOpts
->pzCurOpt
[0] == '-') {
527 if (*++(pOpts
->pzCurOpt
) == NUL
)
529 * NORMAL COMPLETION - NOT this arg, but rest are operands
534 * We do not allow the hyphen to be used as a flag value.
535 * Therefore, if long options are not to be accepted, we punt.
537 if ((pOpts
->fOptSet
& OPTPROC_LONGOPT
) == 0) {
538 fprintf( stderr
, zIllOptStr
, pOpts
->pzProgPath
,
539 zIllegal
, pOpts
->pzCurOpt
-2 );
543 return longOptionFind( pOpts
, pOpts
->pzCurOpt
, pOptState
);
547 * If short options are not allowed, then do long
548 * option processing. Otherwise the character must be a
549 * short (i.e. single character) option.
551 if ((pOpts
->fOptSet
& OPTPROC_SHORTOPT
) != 0)
552 return shortOptionFind( pOpts
, (tAoUC
)*(pOpts
->pzCurOpt
), pOptState
);
554 return longOptionFind( pOpts
, pOpts
->pzCurOpt
, pOptState
);
561 * Find the option descriptor and option argument (if any) for the
562 * next command line argument. DO NOT modify the descriptor. Put
563 * all the state in the state argument so that the option can be skipped
564 * without consequence (side effect).
567 nextOption( tOptions
* pOpts
, tOptState
* pOptState
)
570 enum { ARG_NONE
, ARG_MAY
, ARG_MUST
} arg_type
= ARG_NONE
;
573 res
= findOptDesc( pOpts
, pOptState
);
574 if (! SUCCESSFUL( res
))
576 pOptState
->flags
|= (pOptState
->pOD
->fOptState
& OPTST_PERSISTENT_MASK
);
577 at
= OPTST_GET_ARGTYPE(pOptState
->flags
);
580 * Figure out what to do about option arguments. An argument may be
581 * required, not associated with the option, or be optional. We detect the
582 * latter by examining for an option marker on the next possible argument.
583 * Disabled mode option selection also disables option arguments.
585 if ((pOptState
->flags
& OPTST_DISABLED
) != 0)
587 else if (at
== OPARG_TYPE_NONE
)
589 else if (pOptState
->flags
& OPTST_ARG_OPTIONAL
)
597 * An option argument is required. Long options can either have
598 * a separate command line argument, or an argument attached by
599 * the '=' character. Figure out which.
601 switch (pOptState
->optType
) {
604 * See if an arg string follows the flag character
606 if (*++(pOpts
->pzCurOpt
) == NUL
)
607 pOpts
->pzCurOpt
= pOpts
->origArgVect
[ pOpts
->curOptIdx
++ ];
608 pOptState
->pzOptArg
= pOpts
->pzCurOpt
;
613 * See if an arg string has already been assigned (glued on
614 * with an `=' character)
616 if (pOptState
->pzOptArg
== NULL
)
617 pOptState
->pzOptArg
= pOpts
->origArgVect
[ pOpts
->curOptIdx
++ ];
622 fputs( "AutoOpts lib error: option type not selected\n",
624 exit( EXIT_FAILURE
);
629 * The option was selected by default. The current token is
630 * the option argument.
636 * Make sure we did not overflow the argument list.
638 if (pOpts
->curOptIdx
> pOpts
->origArgCt
) {
639 fprintf( stderr
, zMisArg
, pOpts
->pzProgPath
,
640 pOptState
->pOD
->pz_Name
);
644 pOpts
->pzCurOpt
= NULL
; /* next time advance to next arg */
649 * An option argument is optional.
651 switch (pOptState
->optType
) {
653 if (*++pOpts
->pzCurOpt
!= NUL
)
654 pOptState
->pzOptArg
= pOpts
->pzCurOpt
;
656 char* pzLA
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
659 * BECAUSE it is optional, we must make sure
660 * we did not find another flag and that there
661 * is such an argument.
663 if ((pzLA
== NULL
) || (*pzLA
== '-'))
664 pOptState
->pzOptArg
= NULL
;
666 pOpts
->curOptIdx
++; /* argument found */
667 pOptState
->pzOptArg
= pzLA
;
674 * Look for an argument if we don't already have one (glued on
675 * with a `=' character) *AND* we are not in named argument mode
677 if ( (pOptState
->pzOptArg
== NULL
)
678 && (! NAMED_OPTS(pOpts
))) {
679 char* pzLA
= pOpts
->origArgVect
[ pOpts
->curOptIdx
];
682 * BECAUSE it is optional, we must make sure
683 * we did not find another flag and that there
684 * is such an argument.
686 if ((pzLA
== NULL
) || (*pzLA
== '-'))
687 pOptState
->pzOptArg
= NULL
;
689 pOpts
->curOptIdx
++; /* argument found */
690 pOptState
->pzOptArg
= pzLA
;
697 fputs( "AutoOpts lib error: defaulted to option with optional arg\n",
703 * After an option with an optional argument, we will
704 * *always* start with the next option because if there
705 * were any characters following the option name/flag,
706 * they would be interpreted as the argument.
708 pOpts
->pzCurOpt
= NULL
;
711 default: /* CANNOT */
713 * No option argument. Make sure next time around we find
714 * the correct option flag character for short options
716 if (pOptState
->optType
== TOPT_SHORT
)
720 * It is a long option. Make sure there was no ``=xxx'' argument
722 else if (pOptState
->pzOptArg
!= NULL
) {
723 fprintf( stderr
, zNoArg
, pOpts
->pzProgPath
,
724 pOptState
->pOD
->pz_Name
);
729 * It is a long option. Advance to next command line argument.
732 pOpts
->pzCurOpt
= NULL
;
739 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
743 * The next several routines do the immediate action pass on the command
744 * line options, then the environment variables, then the config files in
745 * reverse order. Once done with that, the order is reversed and all
746 * the config files and environment variables are processed again, this
747 * time only processing the non-immediate action options. doPresets()
748 * will then return for optionProcess() to do the final pass on the command
753 * doImmediateOpts - scan the command line for immediate action options
756 doImmediateOpts( tOptions
* pOpts
)
758 pOpts
->curOptIdx
= 1; /* start by skipping program name */
759 pOpts
->pzCurOpt
= NULL
;
762 * Examine all the options from the start. We process any options that
763 * are marked for immediate processing.
766 tOptState optState
= OPTSTATE_INITIALIZER(PRESET
);
768 switch (nextOption( pOpts
, &optState
)) {
769 case FAILURE
: goto optionsDone
;
770 case PROBLEM
: return SUCCESS
; /* no more args */
775 * IF this *is* an immediate-attribute option, then do it.
777 if (! DO_IMMEDIATELY(optState
.flags
))
780 if (! SUCCESSFUL( handleOption( pOpts
, &optState
)))
784 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0)
785 (*pOpts
->pUsageProc
)( pOpts
, EXIT_FAILURE
);
791 doRegularOpts( tOptions
* pOpts
)
794 * Now, process all the options from our current position onward.
795 * (This allows interspersed options and arguments for the few
796 * non-standard programs that require it.)
799 tOptState optState
= OPTSTATE_INITIALIZER(DEFINED
);
801 switch (nextOption( pOpts
, &optState
)) {
802 case FAILURE
: goto optionsDone
;
803 case PROBLEM
: return SUCCESS
; /* no more args */
808 * IF this is not being processed normally (i.e. is immediate action)
809 * THEN skip it (unless we are supposed to do it a second time).
811 if (! DO_NORMALLY(optState
.flags
)) {
812 if (! DO_SECOND_TIME(optState
.flags
))
814 optState
.pOD
->optOccCt
--; /* don't count last time */
817 if (! SUCCESSFUL( handleOption( pOpts
, &optState
)))
820 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0)
821 (*pOpts
->pUsageProc
)( pOpts
, EXIT_FAILURE
);
827 * doPresets - check for preset values from a config file or the envrionment
830 doPresets( tOptions
* pOpts
)
832 tOptDesc
* pOD
= NULL
;
834 if (! SUCCESSFUL( doImmediateOpts( pOpts
)))
838 * IF this option set has a --save-opts option, then it also
839 * has a --load-opts option. See if a command line option has disabled
842 if (pOpts
->specOptIdx
.save_opts
!= 0) {
843 pOD
= pOpts
->pOptDesc
+ pOpts
->specOptIdx
.save_opts
+ 1;
844 if (DISABLED_OPT(pOD
))
849 * Until we return from this procedure, disable non-presettable opts
851 pOpts
->fOptSet
|= OPTPROC_PRESETTING
;
853 * IF there are no config files,
854 * THEN do any environment presets and leave.
856 if (pOpts
->papzHomeList
== NULL
) {
857 doEnvPresets( pOpts
, ENV_ALL
);
860 doEnvPresets( pOpts
, ENV_IMM
);
863 * Check to see if environment variables have disabled presetting.
865 if ((pOD
!= NULL
) && ! DISABLED_OPT(pOD
))
866 internalFileLoad( pOpts
);
869 * ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
870 * variable options. Only the loading of .rc files.
872 doEnvPresets( pOpts
, ENV_NON_IMM
);
874 pOpts
->fOptSet
&= ~OPTPROC_PRESETTING
;
880 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
882 * VERIFY OPTION CONSISTENCY
884 * Make sure that the argument list passes our consistency tests.
887 checkConsistency( tOptions
* pOpts
)
890 tOptDesc
* pOD
= pOpts
->pOptDesc
;
891 int oCt
= pOpts
->presetOptCt
;
894 * FOR each of "oCt" options, ...
897 const int* pMust
= pOD
->pOptMust
;
898 const int* pCant
= pOD
->pOptCant
;
901 * IF the current option was provided on the command line
902 * THEN ensure that any "MUST" requirements are not
903 * "DEFAULT" (unspecified) *AND* ensure that any
904 * "CANT" options have not been SET or DEFINED.
906 if (SELECTED_OPT(pOD
)) {
907 if (pMust
!= NULL
) for (;;) {
908 tOptDesc
* p
= pOpts
->pOptDesc
+ *(pMust
++);
910 const tOptDesc
* pN
= pOpts
->pOptDesc
+ pMust
[-1];
912 fprintf( stderr
, zReqFmt
, pOD
->pz_Name
, pN
->pz_Name
);
915 if (*pMust
== NO_EQUIVALENT
)
919 if (pCant
!= NULL
) for (;;) {
920 tOptDesc
* p
= pOpts
->pOptDesc
+ *(pCant
++);
921 if (SELECTED_OPT(p
)) {
922 const tOptDesc
* pN
= pOpts
->pOptDesc
+ pCant
[-1];
924 fprintf( stderr
, zCantFmt
, pOD
->pz_Name
, pN
->pz_Name
);
927 if (*pCant
== NO_EQUIVALENT
)
933 * IF this option is not equivalenced to another,
934 * OR it is equivalenced to itself (is the equiv. root)
935 * THEN we need to make sure it occurs often enough.
937 if ( (pOD
->optEquivIndex
== NO_EQUIVALENT
)
938 || (pOD
->optEquivIndex
== pOD
->optIndex
) ) do {
940 * IF the occurrence counts have been satisfied,
941 * THEN there is no problem.
943 if (pOD
->optOccCt
>= pOD
->optMinCt
)
947 * IF MUST_SET means SET and PRESET are okay,
948 * so min occurrence count doesn't count
950 if ( (pOD
->fOptState
& OPTST_MUST_SET
)
951 && (pOD
->fOptState
& (OPTST_PRESET
| OPTST_SET
)) )
955 if (pOD
->optMinCt
> 1)
956 fprintf( stderr
, zNotEnough
, pOD
->pz_Name
, pOD
->optMinCt
);
957 else fprintf( stderr
, zNeedOne
, pOD
->pz_Name
);
966 * IF we are stopping on errors, check to see if any remaining
967 * arguments are required to be there or prohibited from being there.
969 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0) {
972 * Check for prohibition
974 if ((pOpts
->fOptSet
& OPTPROC_NO_ARGS
) != 0) {
975 if (pOpts
->origArgCt
> pOpts
->curOptIdx
) {
976 fprintf( stderr
, zNoArgs
, pOpts
->pzProgName
);
982 * ELSE not prohibited, check for being required
984 else if ((pOpts
->fOptSet
& OPTPROC_ARGS_REQ
) != 0) {
985 if (pOpts
->origArgCt
<= pOpts
->curOptIdx
) {
986 fprintf( stderr
, zArgsMust
, pOpts
->pzProgName
);
996 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
998 * THESE ROUTINES ARE CALLABLE FROM THE GENERATED OPTION PROCESSING CODE
1000 /*=--subblock=arg=arg_type,arg_name,arg_desc =*/
1003 * header: your-opts.h
1007 * These are the routines that libopts users may call directly from their
1008 * code. There are several other routines that can be called by code
1009 * generated by the libopts option templates, but they are not to be
1010 * called from any other user code. The @file{options.h} header is
1011 * fairly clear about this, too.
1014 /*=export_func optionProcess
1016 * what: this is the main option processing routine
1018 * arg: + tOptions* + pOpts + program options descriptor +
1019 * arg: + int + argc + program arg count +
1020 * arg: + char** + argv + program arg vector +
1023 * ret_desc: the count of the arguments processed
1027 * This is the main entry point for processing options. It is intended
1028 * that this procedure be called once at the beginning of the execution of
1029 * a program. Depending on options selected earlier, it is sometimes
1030 * necessary to stop and restart option processing, or to select completely
1031 * different sets of options. This can be done easily, but you generally
1032 * do not want to do this.
1034 * The number of arguments processed always includes the program name.
1035 * If one of the arguments is "--", then it is counted and the processing
1036 * stops. If an error was encountered and errors are to be tolerated, then
1037 * the returned value is the index of the argument causing the error.
1038 * A hyphen by itself ("-") will also cause processing to stop and will
1039 * @emph{not} be counted among the processed arguments. A hyphen by itself
1040 * is treated as an operand. Encountering an operand stops option
1043 * err: Errors will cause diagnostics to be printed. @code{exit(3)} may
1044 * or may not be called. It depends upon whether or not the options
1045 * were generated with the "allow-errors" attribute, or if the
1046 * ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked.
1054 if (! SUCCESSFUL( validateOptionsStruct( pOpts
, argVect
[0] )))
1055 exit( EX_SOFTWARE
);
1058 * Establish the real program name, the program full path,
1059 * and do all the presetting the first time thru only.
1061 if ((pOpts
->fOptSet
& OPTPROC_INITDONE
) == 0) {
1062 pOpts
->origArgCt
= argCt
;
1063 pOpts
->origArgVect
= argVect
;
1064 pOpts
->fOptSet
|= OPTPROC_INITDONE
;
1066 if (! SUCCESSFUL( doPresets( pOpts
)))
1069 if ((pOpts
->fOptSet
& OPTPROC_REORDER
) != 0)
1070 optionSort( pOpts
);
1072 pOpts
->curOptIdx
= 1;
1073 pOpts
->pzCurOpt
= NULL
;
1077 * IF we are (re)starting,
1078 * THEN reset option location
1080 else if (pOpts
->curOptIdx
<= 0) {
1081 pOpts
->curOptIdx
= 1;
1082 pOpts
->pzCurOpt
= NULL
;
1085 if (! SUCCESSFUL( doRegularOpts( pOpts
)))
1086 return pOpts
->origArgCt
;
1089 * IF there were no errors
1090 * AND we have RC/INI files
1091 * AND there is a request to save the files
1092 * THEN do that now before testing for conflicts.
1093 * (conflicts are ignored in preset options)
1095 if (pOpts
->specOptIdx
.save_opts
!= 0) {
1096 tOptDesc
* pOD
= pOpts
->pOptDesc
+ pOpts
->specOptIdx
.save_opts
;
1098 if (SELECTED_OPT( pOD
)) {
1099 optionSaveFile( pOpts
);
1100 exit( EXIT_SUCCESS
);
1105 * IF we are checking for errors,
1106 * THEN look for too few occurrences of required options
1108 if ((pOpts
->fOptSet
& OPTPROC_ERRSTOP
) != 0) {
1109 if (checkConsistency( pOpts
) != 0)
1110 (*pOpts
->pUsageProc
)( pOpts
, EXIT_FAILURE
);
1113 return pOpts
->curOptIdx
;
1119 * c-file-style: "stroustrup"
1120 * indent-tabs-mode: nil
1122 * end of autoopts/autoopts.c */