autoopts.c 33 KB

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