autoopts.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * $Id: autoopts.c,v 4.25 2007/04/15 19:01:18 bkorb Exp $
  3. * Time-stamp: "2007-04-15 11:10:40 bkorb"
  4. *
  5. * This file contains all of the routines that must be linked into
  6. * an executable to use the generated option processing. The optional
  7. * routines are in separately compiled modules so that they will not
  8. * necessarily be linked in.
  9. */
  10. /*
  11. * Automated Options copyright 1992-2007 Bruce Korb
  12. *
  13. * Automated Options is free software.
  14. * You may redistribute it and/or modify it under the terms of the
  15. * GNU General Public License, as published by the Free Software
  16. * Foundation; either version 2, or (at your option) any later version.
  17. *
  18. * Automated Options is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with Automated Options. See the file "COPYING". If not,
  25. * write to: The Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor,
  27. * Boston, MA 02110-1301, USA.
  28. *
  29. * As a special exception, Bruce Korb gives permission for additional
  30. * uses of the text contained in his release of AutoOpts.
  31. *
  32. * The exception is that, if you link the AutoOpts library with other
  33. * files to produce an executable, this does not by itself cause the
  34. * resulting executable to be covered by the GNU General Public License.
  35. * Your use of that executable is in no way restricted on account of
  36. * linking the AutoOpts library code into it.
  37. *
  38. * This exception does not however invalidate any other reasons why
  39. * the executable file might be covered by the GNU General Public License.
  40. *
  41. * This exception applies only to the code released by Bruce Korb under
  42. * the name AutoOpts. If you copy code from other sources under the
  43. * General Public License into a copy of AutoOpts, as the General Public
  44. * License permits, the exception does not apply to the code that you add
  45. * in this way. To avoid misleading anyone as to the status of such
  46. * modified files, you must delete this exception notice from them.
  47. *
  48. * If you write modifications of your own for AutoOpts, it is your choice
  49. * whether to permit this exception to apply to your modifications.
  50. * If you do not wish that, delete this exception notice.
  51. */
  52. static char const zNil[] = "";
  53. /* = = = START-STATIC-FORWARD = = = */
  54. /* static forward declarations maintained by :mkfwd */
  55. static tSuccess
  56. findOptDesc( tOptions* pOpts, tOptState* pOptState );
  57. static tSuccess
  58. nextOption( tOptions* pOpts, tOptState* pOptState );
  59. static tSuccess
  60. doPresets( tOptions* pOpts );
  61. static int
  62. checkConsistency( tOptions* pOpts );
  63. /* = = = END-STATIC-FORWARD = = = */
  64. LOCAL void *
  65. ao_malloc( size_t sz )
  66. {
  67. void * res = malloc(sz);
  68. if (res == NULL) {
  69. fprintf( stderr, "malloc of %d bytes failed\n", (int)sz );
  70. exit( EXIT_FAILURE );
  71. }
  72. return res;
  73. }
  74. #undef malloc
  75. #define malloc(_s) ao_malloc(_s)
  76. LOCAL void *
  77. ao_realloc( void *p, size_t sz )
  78. {
  79. void * res = realloc(p, sz);
  80. if (res == NULL) {
  81. fprintf( stderr, "realloc of %d bytes at 0x%p failed\n", (int)sz, p );
  82. exit( EXIT_FAILURE );
  83. }
  84. return res;
  85. }
  86. #undef realloc
  87. #define realloc(_p,_s) ao_realloc(_p,_s)
  88. LOCAL void
  89. ao_free( void *p )
  90. {
  91. if (p != NULL)
  92. free(p);
  93. }
  94. #undef free
  95. #define free(_p) ao_free(_p)
  96. LOCAL char *
  97. ao_strdup( char const *str )
  98. {
  99. char * res = strdup(str);
  100. if (res == NULL) {
  101. fprintf( stderr, "strdup of %d byte string failed\n", (int)strlen(str) );
  102. exit( EXIT_FAILURE );
  103. }
  104. return res;
  105. }
  106. #undef strdup
  107. #define strdup(_p) ao_strdup(_p)
  108. #ifndef HAVE_PATHFIND
  109. # include "compat/pathfind.c"
  110. #endif
  111. #ifndef HAVE_SNPRINTF
  112. # include "compat/snprintf.c"
  113. #endif
  114. #ifndef HAVE_STRDUP
  115. # include "compat/strdup.c"
  116. #endif
  117. #ifndef HAVE_STRCHR
  118. # include "compat/strchr.c"
  119. #endif
  120. /*
  121. * handleOption
  122. *
  123. * This routine handles equivalencing, sets the option state flags and
  124. * invokes the handler procedure, if any.
  125. */
  126. LOCAL tSuccess
  127. handleOption( tOptions* pOpts, tOptState* pOptState )
  128. {
  129. /*
  130. * Save a copy of the option procedure pointer.
  131. * If this is an equivalence class option, we still want this proc.
  132. */
  133. tOptDesc* pOD = pOptState->pOD;
  134. tOptProc* pOP = pOD->pOptProc;
  135. if (pOD->fOptState & OPTST_ALLOC_ARG)
  136. AGFREE(pOD->optArg.argString);
  137. pOD->optArg.argString = pOptState->pzOptArg;
  138. /*
  139. * IF we are presetting options, then we will ignore any un-presettable
  140. * options. They are the ones either marked as such.
  141. */
  142. if ( ((pOpts->fOptSet & OPTPROC_PRESETTING) != 0)
  143. && ((pOD->fOptState & OPTST_NO_INIT) != 0)
  144. )
  145. return PROBLEM;
  146. /*
  147. * IF this is an equivalence class option,
  148. * THEN
  149. * Save the option value that got us to this option
  150. * entry. (It may not be pOD->optChar[0], if this is an
  151. * equivalence entry.)
  152. * set the pointer to the equivalence class base
  153. */
  154. if (pOD->optEquivIndex != NO_EQUIVALENT) {
  155. tOptDesc* p = pOpts->pOptDesc + pOD->optEquivIndex;
  156. /*
  157. * IF the current option state has not been defined (set on the
  158. * command line), THEN we will allow continued resetting of
  159. * the value. Once "defined", then it must not change.
  160. */
  161. if ((pOD->fOptState & OPTST_DEFINED) != 0) {
  162. /*
  163. * The equivalenced-to option has been found on the command
  164. * line before. Make sure new occurrences are the same type.
  165. *
  166. * IF this option has been previously equivalenced and
  167. * it was not the same equivalenced-to option,
  168. * THEN we have a usage problem.
  169. */
  170. if (p->optActualIndex != pOD->optIndex) {
  171. fprintf( stderr, (char*)zMultiEquiv, p->pz_Name, pOD->pz_Name,
  172. (pOpts->pOptDesc + p->optActualIndex)->pz_Name);
  173. return FAILURE;
  174. }
  175. } else {
  176. /*
  177. * Set the equivalenced-to actual option index to no-equivalent
  178. * so that we set all the entries below. This option may either
  179. * never have been selected before, or else it was selected by
  180. * some sort of "presetting" mechanism.
  181. */
  182. p->optActualIndex = NO_EQUIVALENT;
  183. }
  184. if (p->optActualIndex != pOD->optIndex) {
  185. /*
  186. * First time through, copy over the state
  187. * and add in the equivalence flag
  188. */
  189. p->optActualValue = pOD->optValue;
  190. p->optActualIndex = pOD->optIndex;
  191. pOptState->flags |= OPTST_EQUIVALENCE;
  192. }
  193. /*
  194. * Copy the most recent option argument. set membership state
  195. * is kept in ``p->optCookie''. Do not overwrite.
  196. */
  197. p->optArg.argString = pOD->optArg.argString;
  198. pOD = p;
  199. } else {
  200. pOD->optActualValue = pOD->optValue;
  201. pOD->optActualIndex = pOD->optIndex;
  202. }
  203. pOD->fOptState &= OPTST_PERSISTENT_MASK;
  204. pOD->fOptState |= (pOptState->flags & ~OPTST_PERSISTENT_MASK);
  205. /*
  206. * Keep track of count only for DEFINED (command line) options.
  207. * IF we have too many, build up an error message and bail.
  208. */
  209. if ( (pOD->fOptState & OPTST_DEFINED)
  210. && (++pOD->optOccCt > pOD->optMaxCt) ) {
  211. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
  212. char const * pzEqv =
  213. (pOD->optEquivIndex != NO_EQUIVALENT) ? zEquiv : zNil;
  214. fputs( zErrOnly, stderr );
  215. if (pOD->optMaxCt > 1)
  216. fprintf(stderr, zAtMost, pOD->optMaxCt, pOD->pz_Name, pzEqv);
  217. else
  218. fprintf(stderr, zOnlyOne, pOD->pz_Name, pzEqv);
  219. }
  220. return FAILURE;
  221. }
  222. /*
  223. * If provided a procedure to call, call it
  224. */
  225. if (pOP != (tpOptProc)NULL)
  226. (*pOP)( pOpts, pOD );
  227. return SUCCESS;
  228. }
  229. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  230. *
  231. * HUNT FOR OPTIONS IN THE ARGUMENT LIST
  232. *
  233. * The next four procedures are "private" to nextOption().
  234. * nextOption() uses findOptDesc() to find the next descriptor and it, in
  235. * turn, uses longOptionFind() and shortOptionFind() to actually do the hunt.
  236. *
  237. * longOptionFind
  238. *
  239. * Find the long option descriptor for the current option
  240. */
  241. LOCAL tSuccess
  242. longOptionFind( tOptions* pOpts, char* pzOptName, tOptState* pOptState )
  243. {
  244. ag_bool disable = AG_FALSE;
  245. char* pzEq = strchr( pzOptName, '=' );
  246. tOptDesc* pOD = pOpts->pOptDesc;
  247. int idx = 0;
  248. int idxLim = pOpts->optCt;
  249. int matchCt = 0;
  250. int matchIdx = 0;
  251. int nameLen;
  252. /*
  253. * IF the value is attached to the name,
  254. * THEN clip it off.
  255. * Either way, figure out how long our name is
  256. */
  257. if (pzEq != NULL) {
  258. nameLen = (int)(pzEq - pzOptName);
  259. *pzEq = NUL;
  260. } else nameLen = strlen( pzOptName );
  261. do {
  262. if (SKIP_OPT(pOD))
  263. continue;
  264. if (strneqvcmp( pzOptName, pOD->pz_Name, nameLen ) == 0) {
  265. /*
  266. * IF we have a complete match
  267. * THEN it takes priority over any already located partial
  268. */
  269. if (pOD->pz_Name[ nameLen ] == NUL) {
  270. matchCt = 1;
  271. matchIdx = idx;
  272. break;
  273. }
  274. }
  275. /*
  276. * IF there is a disable name
  277. * *AND* no argument value has been supplied
  278. * (disabled options may have no argument)
  279. * *AND* the option name matches the disable name
  280. * THEN ...
  281. */
  282. else if ( (pOD->pz_DisableName != NULL)
  283. && (strneqvcmp(pzOptName, pOD->pz_DisableName, nameLen) == 0)
  284. ) {
  285. disable = AG_TRUE;
  286. /*
  287. * IF we have a complete match
  288. * THEN it takes priority over any already located partial
  289. */
  290. if (pOD->pz_DisableName[ nameLen ] == NUL) {
  291. matchCt = 1;
  292. matchIdx = idx;
  293. break;
  294. }
  295. }
  296. else
  297. continue;
  298. /*
  299. * We found a partial match, either regular or disabling.
  300. * Remember the index for later.
  301. */
  302. matchIdx = idx;
  303. if (++matchCt > 1)
  304. break;
  305. } while (pOD++, (++idx < idxLim));
  306. if (pzEq != NULL)
  307. *(pzEq++) = '=';
  308. /*
  309. * Make sure we either found an exact match or found only one partial
  310. */
  311. if (matchCt == 1) {
  312. /*
  313. * IF we found a disablement name,
  314. * THEN set the bit in the callers' flag word
  315. */
  316. if (disable)
  317. pOptState->flags |= OPTST_DISABLED;
  318. pOptState->pOD = pOpts->pOptDesc + matchIdx;
  319. pOptState->pzOptArg = pzEq;
  320. pOptState->optType = TOPT_LONG;
  321. return SUCCESS;
  322. }
  323. /*
  324. * IF there is no equal sign
  325. * *AND* we are using named arguments
  326. * *AND* there is a default named option,
  327. * THEN return that option.
  328. */
  329. if ( (pzEq == NULL)
  330. && NAMED_OPTS(pOpts)
  331. && (pOpts->specOptIdx.default_opt != NO_EQUIVALENT)) {
  332. pOptState->pOD = pOpts->pOptDesc + pOpts->specOptIdx.default_opt;
  333. pOptState->pzOptArg = pzOptName;
  334. pOptState->optType = TOPT_DEFAULT;
  335. return SUCCESS;
  336. }
  337. /*
  338. * IF we are to stop on errors (the default, actually)
  339. * THEN call the usage procedure.
  340. */
  341. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
  342. fprintf( stderr, zIllOptStr, pOpts->pzProgPath,
  343. (matchCt == 0) ? zIllegal : zAmbiguous, pzOptName );
  344. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  345. }
  346. return FAILURE;
  347. }
  348. /*
  349. * shortOptionFind
  350. *
  351. * Find the short option descriptor for the current option
  352. */
  353. LOCAL tSuccess
  354. shortOptionFind( tOptions* pOpts, uint_t optValue, tOptState* pOptState )
  355. {
  356. tOptDesc* pRes = pOpts->pOptDesc;
  357. int ct = pOpts->optCt;
  358. /*
  359. * Search the option list
  360. */
  361. for (;;) {
  362. /*
  363. * IF the values match,
  364. * THEN we stop here
  365. */
  366. if ((! SKIP_OPT(pRes)) && (optValue == pRes->optValue)) {
  367. pOptState->pOD = pRes;
  368. pOptState->optType = TOPT_SHORT;
  369. return SUCCESS;
  370. }
  371. /*
  372. * Advance to next option description
  373. */
  374. pRes++;
  375. /*
  376. * IF we have searched everything, ...
  377. */
  378. if (--ct <= 0)
  379. break;
  380. }
  381. /*
  382. * IF the character value is a digit
  383. * AND there is a special number option ("-n")
  384. * THEN the result is the "option" itself and the
  385. * option is the specially marked "number" option.
  386. */
  387. if ( isdigit( optValue )
  388. && (pOpts->specOptIdx.number_option != NO_EQUIVALENT) ) {
  389. pOptState->pOD = \
  390. pRes = pOpts->pOptDesc + pOpts->specOptIdx.number_option;
  391. (pOpts->pzCurOpt)--;
  392. pOptState->optType = TOPT_SHORT;
  393. return SUCCESS;
  394. }
  395. /*
  396. * IF we are to stop on errors (the default, actually)
  397. * THEN call the usage procedure.
  398. */
  399. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
  400. fprintf( stderr, zIllOptChr, pOpts->pzProgPath, optValue );
  401. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  402. }
  403. return FAILURE;
  404. }
  405. /*
  406. * findOptDesc
  407. *
  408. * Find the option descriptor for the current option
  409. */
  410. static tSuccess
  411. findOptDesc( tOptions* pOpts, tOptState* pOptState )
  412. {
  413. /*
  414. * IF we are continuing a short option list (e.g. -xyz...)
  415. * THEN continue a single flag option.
  416. * OTHERWISE see if there is room to advance and then do so.
  417. */
  418. if ((pOpts->pzCurOpt != NULL) && (*pOpts->pzCurOpt != NUL))
  419. return shortOptionFind( pOpts, (tAoUC)*(pOpts->pzCurOpt), pOptState );
  420. if (pOpts->curOptIdx >= pOpts->origArgCt)
  421. return PROBLEM; /* NORMAL COMPLETION */
  422. pOpts->pzCurOpt = pOpts->origArgVect[ pOpts->curOptIdx ];
  423. /*
  424. * IF all arguments must be named options, ...
  425. */
  426. if (NAMED_OPTS(pOpts)) {
  427. char* pz = pOpts->pzCurOpt;
  428. pOpts->curOptIdx++;
  429. /*
  430. * Skip over any flag/option markers.
  431. * In this mode, they are not required.
  432. */
  433. while (*pz == '-') pz++;
  434. return longOptionFind( pOpts, pz, pOptState );
  435. }
  436. /*
  437. * Note the kind of flag/option marker
  438. */
  439. if (*((pOpts->pzCurOpt)++) != '-')
  440. return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */
  441. /*
  442. * Special hack for a hyphen by itself
  443. */
  444. if (*(pOpts->pzCurOpt) == NUL)
  445. return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */
  446. /*
  447. * The current argument is to be processed as an option argument
  448. */
  449. pOpts->curOptIdx++;
  450. /*
  451. * We have an option marker.
  452. * Test the next character for long option indication
  453. */
  454. if (pOpts->pzCurOpt[0] == '-') {
  455. if (*++(pOpts->pzCurOpt) == NUL)
  456. /*
  457. * NORMAL COMPLETION - NOT this arg, but rest are operands
  458. */
  459. return PROBLEM;
  460. /*
  461. * We do not allow the hyphen to be used as a flag value.
  462. * Therefore, if long options are not to be accepted, we punt.
  463. */
  464. if ((pOpts->fOptSet & OPTPROC_LONGOPT) == 0) {
  465. fprintf( stderr, zIllOptStr, pOpts->pzProgPath,
  466. zIllegal, pOpts->pzCurOpt-2 );
  467. return FAILURE;
  468. }
  469. return longOptionFind( pOpts, pOpts->pzCurOpt, pOptState );
  470. }
  471. /*
  472. * If short options are not allowed, then do long
  473. * option processing. Otherwise the character must be a
  474. * short (i.e. single character) option.
  475. */
  476. if ((pOpts->fOptSet & OPTPROC_SHORTOPT) != 0)
  477. return shortOptionFind( pOpts, (tAoUC)*(pOpts->pzCurOpt), pOptState );
  478. return longOptionFind( pOpts, pOpts->pzCurOpt, pOptState );
  479. }
  480. /*
  481. * nextOption
  482. *
  483. * Find the option descriptor and option argument (if any) for the
  484. * next command line argument. DO NOT modify the descriptor. Put
  485. * all the state in the state argument so that the option can be skipped
  486. * without consequence (side effect).
  487. */
  488. static tSuccess
  489. nextOption( tOptions* pOpts, tOptState* pOptState )
  490. {
  491. tSuccess res;
  492. enum { ARG_NONE, ARG_MAY, ARG_MUST } arg_type = ARG_NONE;
  493. teOptArgType at;
  494. res = findOptDesc( pOpts, pOptState );
  495. if (! SUCCESSFUL( res ))
  496. return res;
  497. pOptState->flags |= (pOptState->pOD->fOptState & OPTST_PERSISTENT_MASK);
  498. at = OPTST_GET_ARGTYPE(pOptState->flags);
  499. /*
  500. * Figure out what to do about option arguments. An argument may be
  501. * required, not associated with the option, or be optional. We detect the
  502. * latter by examining for an option marker on the next possible argument.
  503. * Disabled mode option selection also disables option arguments.
  504. */
  505. if ((pOptState->flags & OPTST_DISABLED) != 0)
  506. arg_type = ARG_NONE;
  507. else if (at == OPARG_TYPE_NONE)
  508. arg_type = ARG_NONE;
  509. else if (pOptState->flags & OPTST_ARG_OPTIONAL)
  510. arg_type = ARG_MAY;
  511. else
  512. arg_type = ARG_MUST;
  513. switch (arg_type) {
  514. case ARG_MUST:
  515. /*
  516. * An option argument is required. Long options can either have
  517. * a separate command line argument, or an argument attached by
  518. * the '=' character. Figure out which.
  519. */
  520. switch (pOptState->optType) {
  521. case TOPT_SHORT:
  522. /*
  523. * See if an arg string follows the flag character
  524. */
  525. if (*++(pOpts->pzCurOpt) == NUL)
  526. pOpts->pzCurOpt = pOpts->origArgVect[ pOpts->curOptIdx++ ];
  527. pOptState->pzOptArg = pOpts->pzCurOpt;
  528. break;
  529. case TOPT_LONG:
  530. /*
  531. * See if an arg string has already been assigned (glued on
  532. * with an `=' character)
  533. */
  534. if (pOptState->pzOptArg == NULL)
  535. pOptState->pzOptArg = pOpts->origArgVect[ pOpts->curOptIdx++ ];
  536. break;
  537. default:
  538. #ifdef DEBUG
  539. fputs( "AutoOpts lib error: option type not selected\n",
  540. stderr );
  541. exit( EXIT_FAILURE );
  542. #endif
  543. case TOPT_DEFAULT:
  544. /*
  545. * The option was selected by default. The current token is
  546. * the option argument.
  547. */
  548. break;
  549. }
  550. /*
  551. * Make sure we did not overflow the argument list.
  552. */
  553. if (pOpts->curOptIdx > pOpts->origArgCt) {
  554. fprintf( stderr, zMisArg, pOpts->pzProgPath,
  555. pOptState->pOD->pz_Name );
  556. return FAILURE;
  557. }
  558. pOpts->pzCurOpt = NULL; /* next time advance to next arg */
  559. break;
  560. case ARG_MAY:
  561. /*
  562. * An option argument is optional.
  563. */
  564. switch (pOptState->optType) {
  565. case TOPT_SHORT:
  566. if (*++pOpts->pzCurOpt != NUL)
  567. pOptState->pzOptArg = pOpts->pzCurOpt;
  568. else {
  569. char* pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
  570. /*
  571. * BECAUSE it is optional, we must make sure
  572. * we did not find another flag and that there
  573. * is such an argument.
  574. */
  575. if ((pzLA == NULL) || (*pzLA == '-'))
  576. pOptState->pzOptArg = NULL;
  577. else {
  578. pOpts->curOptIdx++; /* argument found */
  579. pOptState->pzOptArg = pzLA;
  580. }
  581. }
  582. break;
  583. case TOPT_LONG:
  584. /*
  585. * Look for an argument if we don't already have one (glued on
  586. * with a `=' character) *AND* we are not in named argument mode
  587. */
  588. if ( (pOptState->pzOptArg == NULL)
  589. && (! NAMED_OPTS(pOpts))) {
  590. char* pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
  591. /*
  592. * BECAUSE it is optional, we must make sure
  593. * we did not find another flag and that there
  594. * is such an argument.
  595. */
  596. if ((pzLA == NULL) || (*pzLA == '-'))
  597. pOptState->pzOptArg = NULL;
  598. else {
  599. pOpts->curOptIdx++; /* argument found */
  600. pOptState->pzOptArg = pzLA;
  601. }
  602. }
  603. break;
  604. default:
  605. case TOPT_DEFAULT:
  606. fputs( "AutoOpts lib error: defaulted to option with optional arg\n",
  607. stderr );
  608. exit( EX_SOFTWARE );
  609. }
  610. /*
  611. * After an option with an optional argument, we will
  612. * *always* start with the next option because if there
  613. * were any characters following the option name/flag,
  614. * they would be interpreted as the argument.
  615. */
  616. pOpts->pzCurOpt = NULL;
  617. break;
  618. default: /* CANNOT */
  619. /*
  620. * No option argument. Make sure next time around we find
  621. * the correct option flag character for short options
  622. */
  623. if (pOptState->optType == TOPT_SHORT)
  624. (pOpts->pzCurOpt)++;
  625. /*
  626. * It is a long option. Make sure there was no ``=xxx'' argument
  627. */
  628. else if (pOptState->pzOptArg != NULL) {
  629. fprintf( stderr, zNoArg, pOpts->pzProgPath,
  630. pOptState->pOD->pz_Name );
  631. return FAILURE;
  632. }
  633. /*
  634. * It is a long option. Advance to next command line argument.
  635. */
  636. else
  637. pOpts->pzCurOpt = NULL;
  638. }
  639. return SUCCESS;
  640. }
  641. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  642. *
  643. * DO PRESETS
  644. *
  645. * The next several routines do the immediate action pass on the command
  646. * line options, then the environment variables, then the config files in
  647. * reverse order. Once done with that, the order is reversed and all
  648. * the config files and environment variables are processed again, this
  649. * time only processing the non-immediate action options. doPresets()
  650. * will then return for optionProcess() to do the final pass on the command
  651. * line arguments.
  652. */
  653. /*
  654. * doImmediateOpts - scan the command line for immediate action options
  655. */
  656. LOCAL tSuccess
  657. doImmediateOpts( tOptions* pOpts )
  658. {
  659. pOpts->curOptIdx = 1; /* start by skipping program name */
  660. pOpts->pzCurOpt = NULL;
  661. /*
  662. * Examine all the options from the start. We process any options that
  663. * are marked for immediate processing.
  664. */
  665. for (;;) {
  666. tOptState optState = OPTSTATE_INITIALIZER(PRESET);
  667. switch (nextOption( pOpts, &optState )) {
  668. case FAILURE: goto optionsDone;
  669. case PROBLEM: return SUCCESS; /* no more args */
  670. case SUCCESS: break;
  671. }
  672. /*
  673. * IF this *is* an immediate-attribute option, then do it.
  674. */
  675. if (! DO_IMMEDIATELY(optState.flags))
  676. continue;
  677. if (! SUCCESSFUL( handleOption( pOpts, &optState )))
  678. break;
  679. } optionsDone:;
  680. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
  681. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  682. return FAILURE;
  683. }
  684. LOCAL tSuccess
  685. doRegularOpts( tOptions* pOpts )
  686. {
  687. /*
  688. * Now, process all the options from our current position onward.
  689. * (This allows interspersed options and arguments for the few
  690. * non-standard programs that require it.)
  691. */
  692. for (;;) {
  693. tOptState optState = OPTSTATE_INITIALIZER(DEFINED);
  694. switch (nextOption( pOpts, &optState )) {
  695. case FAILURE: goto optionsDone;
  696. case PROBLEM: return SUCCESS; /* no more args */
  697. case SUCCESS: break;
  698. }
  699. /*
  700. * IF this is not being processed normally (i.e. is immediate action)
  701. * THEN skip it (unless we are supposed to do it a second time).
  702. */
  703. if (! DO_NORMALLY(optState.flags)) {
  704. if (! DO_SECOND_TIME(optState.flags))
  705. continue;
  706. optState.pOD->optOccCt--; /* don't count last time */
  707. }
  708. if (! SUCCESSFUL( handleOption( pOpts, &optState )))
  709. break;
  710. } optionsDone:;
  711. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
  712. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  713. return FAILURE;
  714. }
  715. /*
  716. * doPresets - check for preset values from a config file or the envrionment
  717. */
  718. static tSuccess
  719. doPresets( tOptions* pOpts )
  720. {
  721. tOptDesc * pOD = NULL;
  722. if (! SUCCESSFUL( doImmediateOpts( pOpts )))
  723. return FAILURE;
  724. /*
  725. * IF this option set has a --save-opts option, then it also
  726. * has a --load-opts option. See if a command line option has disabled
  727. * option presetting.
  728. */
  729. if (pOpts->specOptIdx.save_opts != 0) {
  730. pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts + 1;
  731. if (DISABLED_OPT(pOD))
  732. return SUCCESS;
  733. }
  734. /*
  735. * Until we return from this procedure, disable non-presettable opts
  736. */
  737. pOpts->fOptSet |= OPTPROC_PRESETTING;
  738. /*
  739. * IF there are no config files,
  740. * THEN do any environment presets and leave.
  741. */
  742. if (pOpts->papzHomeList == NULL) {
  743. doEnvPresets( pOpts, ENV_ALL );
  744. }
  745. else {
  746. doEnvPresets( pOpts, ENV_IMM );
  747. /*
  748. * Check to see if environment variables have disabled presetting.
  749. */
  750. if ((pOD != NULL) && ! DISABLED_OPT(pOD))
  751. internalFileLoad( pOpts );
  752. /*
  753. * ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
  754. * variable options. Only the loading of .rc files.
  755. */
  756. doEnvPresets( pOpts, ENV_NON_IMM );
  757. }
  758. pOpts->fOptSet &= ~OPTPROC_PRESETTING;
  759. return SUCCESS;
  760. }
  761. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  762. *
  763. * VERIFY OPTION CONSISTENCY
  764. *
  765. * Make sure that the argument list passes our consistency tests.
  766. */
  767. static int
  768. checkConsistency( tOptions* pOpts )
  769. {
  770. int errCt = 0;
  771. tOptDesc* pOD = pOpts->pOptDesc;
  772. int oCt = pOpts->presetOptCt;
  773. /*
  774. * FOR each of "oCt" options, ...
  775. */
  776. for (;;) {
  777. const int* pMust = pOD->pOptMust;
  778. const int* pCant = pOD->pOptCant;
  779. /*
  780. * IF the current option was provided on the command line
  781. * THEN ensure that any "MUST" requirements are not
  782. * "DEFAULT" (unspecified) *AND* ensure that any
  783. * "CANT" options have not been SET or DEFINED.
  784. */
  785. if (SELECTED_OPT(pOD)) {
  786. if (pMust != NULL) for (;;) {
  787. tOptDesc* p = pOpts->pOptDesc + *(pMust++);
  788. if (UNUSED_OPT(p)) {
  789. const tOptDesc* pN = pOpts->pOptDesc + pMust[-1];
  790. errCt++;
  791. fprintf( stderr, zReqFmt, pOD->pz_Name, pN->pz_Name );
  792. }
  793. if (*pMust == NO_EQUIVALENT)
  794. break;
  795. }
  796. if (pCant != NULL) for (;;) {
  797. tOptDesc* p = pOpts->pOptDesc + *(pCant++);
  798. if (SELECTED_OPT(p)) {
  799. const tOptDesc* pN = pOpts->pOptDesc + pCant[-1];
  800. errCt++;
  801. fprintf( stderr, zCantFmt, pOD->pz_Name, pN->pz_Name );
  802. }
  803. if (*pCant == NO_EQUIVALENT)
  804. break;
  805. }
  806. }
  807. /*
  808. * IF this option is not equivalenced to another,
  809. * OR it is equivalenced to itself (is the equiv. root)
  810. * THEN we need to make sure it occurs often enough.
  811. */
  812. if ( (pOD->optEquivIndex == NO_EQUIVALENT)
  813. || (pOD->optEquivIndex == pOD->optIndex) ) do {
  814. /*
  815. * IF the occurrence counts have been satisfied,
  816. * THEN there is no problem.
  817. */
  818. if (pOD->optOccCt >= pOD->optMinCt)
  819. break;
  820. /*
  821. * IF MUST_SET means SET and PRESET are okay,
  822. * so min occurrence count doesn't count
  823. */
  824. if ( (pOD->fOptState & OPTST_MUST_SET)
  825. && (pOD->fOptState & (OPTST_PRESET | OPTST_SET)) )
  826. break;
  827. errCt++;
  828. if (pOD->optMinCt > 1)
  829. fprintf( stderr, zNotEnough, pOD->pz_Name, pOD->optMinCt );
  830. else fprintf( stderr, zNeedOne, pOD->pz_Name );
  831. } while (0);
  832. if (--oCt <= 0)
  833. break;
  834. pOD++;
  835. }
  836. /*
  837. * IF we are stopping on errors, check to see if any remaining
  838. * arguments are required to be there or prohibited from being there.
  839. */
  840. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
  841. /*
  842. * Check for prohibition
  843. */
  844. if ((pOpts->fOptSet & OPTPROC_NO_ARGS) != 0) {
  845. if (pOpts->origArgCt > pOpts->curOptIdx) {
  846. fprintf( stderr, zNoArgs, pOpts->pzProgName );
  847. ++errCt;
  848. }
  849. }
  850. /*
  851. * ELSE not prohibited, check for being required
  852. */
  853. else if ((pOpts->fOptSet & OPTPROC_ARGS_REQ) != 0) {
  854. if (pOpts->origArgCt <= pOpts->curOptIdx) {
  855. fprintf( stderr, zArgsMust, pOpts->pzProgName );
  856. ++errCt;
  857. }
  858. }
  859. }
  860. return errCt;
  861. }
  862. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  863. *
  864. * THESE ROUTINES ARE CALLABLE FROM THE GENERATED OPTION PROCESSING CODE
  865. */
  866. /*=--subblock=arg=arg_type,arg_name,arg_desc =*/
  867. /*=*
  868. * library: opts
  869. * header: your-opts.h
  870. *
  871. * lib_description:
  872. *
  873. * These are the routines that libopts users may call directly from their
  874. * code. There are several other routines that can be called by code
  875. * generated by the libopts option templates, but they are not to be
  876. * called from any other user code. The @file{options.h} header is
  877. * fairly clear about this, too.
  878. =*/
  879. /*=export_func optionProcess
  880. *
  881. * what: this is the main option processing routine
  882. *
  883. * arg: + tOptions* + pOpts + program options descriptor +
  884. * arg: + int + argc + program arg count +
  885. * arg: + char** + argv + program arg vector +
  886. *
  887. * ret_type: int
  888. * ret_desc: the count of the arguments processed
  889. *
  890. * doc:
  891. *
  892. * This is the main entry point for processing options. It is intended
  893. * that this procedure be called once at the beginning of the execution of
  894. * a program. Depending on options selected earlier, it is sometimes
  895. * necessary to stop and restart option processing, or to select completely
  896. * different sets of options. This can be done easily, but you generally
  897. * do not want to do this.
  898. *
  899. * The number of arguments processed always includes the program name.
  900. * If one of the arguments is "--", then it is counted and the processing
  901. * stops. If an error was encountered and errors are to be tolerated, then
  902. * the returned value is the index of the argument causing the error.
  903. * A hyphen by itself ("-") will also cause processing to stop and will
  904. * @emph{not} be counted among the processed arguments. A hyphen by itself
  905. * is treated as an operand. Encountering an operand stops option
  906. * processing.
  907. *
  908. * err: Errors will cause diagnostics to be printed. @code{exit(3)} may
  909. * or may not be called. It depends upon whether or not the options
  910. * were generated with the "allow-errors" attribute, or if the
  911. * ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked.
  912. =*/
  913. int
  914. optionProcess(
  915. tOptions* pOpts,
  916. int argCt,
  917. char** argVect )
  918. {
  919. if (! SUCCESSFUL( validateOptionsStruct( pOpts, argVect[0] )))
  920. exit( EX_SOFTWARE );
  921. /*
  922. * Establish the real program name, the program full path,
  923. * and do all the presetting the first time thru only.
  924. */
  925. if ((pOpts->fOptSet & OPTPROC_INITDONE) == 0) {
  926. pOpts->origArgCt = argCt;
  927. pOpts->origArgVect = argVect;
  928. pOpts->fOptSet |= OPTPROC_INITDONE;
  929. if (! SUCCESSFUL( doPresets( pOpts )))
  930. return 0;
  931. if ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
  932. optionSort( pOpts );
  933. pOpts->curOptIdx = 1;
  934. pOpts->pzCurOpt = NULL;
  935. }
  936. /*
  937. * IF we are (re)starting,
  938. * THEN reset option location
  939. */
  940. else if (pOpts->curOptIdx <= 0) {
  941. pOpts->curOptIdx = 1;
  942. pOpts->pzCurOpt = NULL;
  943. }
  944. if (! SUCCESSFUL( doRegularOpts( pOpts )))
  945. return pOpts->origArgCt;
  946. /*
  947. * IF there were no errors
  948. * AND we have RC/INI files
  949. * AND there is a request to save the files
  950. * THEN do that now before testing for conflicts.
  951. * (conflicts are ignored in preset options)
  952. */
  953. if (pOpts->specOptIdx.save_opts != 0) {
  954. tOptDesc* pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts;
  955. if (SELECTED_OPT( pOD )) {
  956. optionSaveFile( pOpts );
  957. exit( EXIT_SUCCESS );
  958. }
  959. }
  960. /*
  961. * IF we are checking for errors,
  962. * THEN look for too few occurrences of required options
  963. */
  964. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
  965. if (checkConsistency( pOpts ) != 0)
  966. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  967. }
  968. return pOpts->curOptIdx;
  969. }
  970. /*
  971. * Local Variables:
  972. * mode: C
  973. * c-file-style: "stroustrup"
  974. * indent-tabs-mode: nil
  975. * End:
  976. * end of autoopts/autoopts.c */