autoopts.c 34 KB

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