enumeration.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * $Id: enumeration.c,v 4.17 2007/02/04 17:44:12 bkorb Exp $
  3. * Time-stamp: "2007-01-13 10:22:35 bkorb"
  4. *
  5. * Automated Options Paged Usage module.
  6. *
  7. * This routine will run run-on options through a pager so the
  8. * user may examine, print or edit them at their leisure.
  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. tSCC* pz_enum_err_fmt;
  53. /* = = = START-STATIC-FORWARD = = = */
  54. /* static forward declarations maintained by :mkfwd */
  55. static void
  56. enumError(
  57. tOptions* pOpts,
  58. tOptDesc* pOD,
  59. tCC* const * paz_names,
  60. int name_ct );
  61. static uintptr_t
  62. findName(
  63. tCC* pzName,
  64. tOptions* pOpts,
  65. tOptDesc* pOD,
  66. tCC* const * paz_names,
  67. unsigned int name_ct );
  68. /* = = = END-STATIC-FORWARD = = = */
  69. static void
  70. enumError(
  71. tOptions* pOpts,
  72. tOptDesc* pOD,
  73. tCC* const * paz_names,
  74. int name_ct )
  75. {
  76. size_t max_len = 0;
  77. size_t ttl_len = 0;
  78. if (pOpts != NULL)
  79. fprintf( option_usage_fp, pz_enum_err_fmt, pOpts->pzProgName,
  80. pOD->optArg.argString, pOD->pz_Name );
  81. fprintf( option_usage_fp, zValidKeys, pOD->pz_Name );
  82. if (**paz_names == 0x7F) {
  83. paz_names++;
  84. name_ct--;
  85. }
  86. /*
  87. * Figure out the maximum length of any name, plus the total length
  88. * of all the names.
  89. */
  90. {
  91. tCC * const * paz = paz_names;
  92. int ct = name_ct;
  93. do {
  94. size_t len = strlen( *(paz++) ) + 1;
  95. if (len > max_len)
  96. max_len = len;
  97. ttl_len += len;
  98. } while (--ct > 0);
  99. }
  100. /*
  101. * IF any one entry is about 1/2 line or longer, print one per line
  102. */
  103. if (max_len > 35) {
  104. do {
  105. fprintf( option_usage_fp, " %s\n", *(paz_names++) );
  106. } while (--name_ct > 0);
  107. }
  108. /*
  109. * ELSE IF they all fit on one line, then do so.
  110. */
  111. else if (ttl_len < 76) {
  112. fputc( ' ', option_usage_fp );
  113. do {
  114. fputc( ' ', option_usage_fp );
  115. fputs( *(paz_names++), option_usage_fp );
  116. } while (--name_ct > 0);
  117. fputc( '\n', option_usage_fp );
  118. }
  119. /*
  120. * Otherwise, columnize the output
  121. */
  122. else {
  123. int ent_no = 0;
  124. char zFmt[16]; /* format for all-but-last entries on a line */
  125. sprintf( zFmt, "%%-%ds", (int)max_len );
  126. max_len = 78 / max_len; /* max_len is now max entries on a line */
  127. fputs( " ", option_usage_fp );
  128. /*
  129. * Loop through all but the last entry
  130. */
  131. while (--name_ct > 0) {
  132. if (++ent_no == max_len) {
  133. /*
  134. * Last entry on a line. Start next line, too.
  135. */
  136. fprintf( option_usage_fp, "%s\n ", *(paz_names++) );
  137. ent_no = 0;
  138. }
  139. else
  140. fprintf( option_usage_fp, zFmt, *(paz_names++) );
  141. }
  142. fprintf( option_usage_fp, "%s\n", *paz_names );
  143. }
  144. /*
  145. * IF we do not have a pOpts pointer, then this output is being requested
  146. * by the usage procedure. Let's not re-invoke it recursively.
  147. */
  148. if (pOpts != NULL)
  149. (*(pOpts->pUsageProc))( pOpts, EXIT_FAILURE );
  150. if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP)
  151. fputs( zSetMemberSettings, option_usage_fp );
  152. }
  153. static uintptr_t
  154. findName(
  155. tCC* pzName,
  156. tOptions* pOpts,
  157. tOptDesc* pOD,
  158. tCC* const * paz_names,
  159. unsigned int name_ct )
  160. {
  161. uintptr_t res = name_ct;
  162. size_t len = strlen( (char*)pzName );
  163. uintptr_t idx;
  164. /*
  165. * Look for an exact match, but remember any partial matches.
  166. * Multiple partial matches means we have an ambiguous match.
  167. */
  168. for (idx = 0; idx < name_ct; idx++) {
  169. if (strncmp( (char*)paz_names[idx], (char*)pzName, len) == 0) {
  170. if (paz_names[idx][len] == NUL)
  171. return idx; /* full match */
  172. if (res != name_ct) {
  173. pz_enum_err_fmt = zAmbigKey;
  174. option_usage_fp = stderr;
  175. enumError( pOpts, pOD, paz_names, (int)name_ct );
  176. }
  177. res = idx; /* save partial match */
  178. }
  179. }
  180. /*
  181. * no partial match -> error
  182. */
  183. if (res == name_ct) {
  184. pz_enum_err_fmt = zNoKey;
  185. option_usage_fp = stderr;
  186. enumError( pOpts, pOD, paz_names, (int)name_ct );
  187. }
  188. /*
  189. * Return the matching index as a char* pointer.
  190. * The result gets stashed in a char* pointer, so it will have to fit.
  191. */
  192. return res;
  193. }
  194. /*=export_func optionKeywordName
  195. * what: Convert between enumeration values and strings
  196. * private:
  197. *
  198. * arg: tOptDesc*, pOD, enumeration option description
  199. * arg: unsigned int, enum_val, the enumeration value to map
  200. *
  201. * ret_type: char const*
  202. * ret_desc: the enumeration name from const memory
  203. *
  204. * doc: This converts an enumeration value into the matching string.
  205. =*/
  206. char const*
  207. optionKeywordName(
  208. tOptDesc* pOD,
  209. unsigned int enum_val )
  210. {
  211. tOptDesc od;
  212. od.optArg.argEnum = enum_val;
  213. (*(pOD->pOptProc))( (void*)(2UL), &od );
  214. return od.optArg.argString;
  215. }
  216. /*=export_func optionEnumerationVal
  217. * what: Convert from a string to an enumeration value
  218. * private:
  219. *
  220. * arg: tOptions*, pOpts, the program options descriptor
  221. * arg: tOptDesc*, pOD, enumeration option description
  222. * arg: char const * const *, paz_names, list of enumeration names
  223. * arg: unsigned int, name_ct, number of names in list
  224. *
  225. * ret_type: uintptr_t
  226. * ret_desc: the enumeration value
  227. *
  228. * doc: This converts the optArg.argString string from the option description
  229. * into the index corresponding to an entry in the name list.
  230. * This will match the generated enumeration value.
  231. * Full matches are always accepted. Partial matches are accepted
  232. * if there is only one partial match.
  233. =*/
  234. uintptr_t
  235. optionEnumerationVal(
  236. tOptions* pOpts,
  237. tOptDesc* pOD,
  238. tCC * const * paz_names,
  239. unsigned int name_ct )
  240. {
  241. uintptr_t res = 0UL;
  242. /*
  243. * IF the program option descriptor pointer is invalid,
  244. * then it is some sort of special request.
  245. */
  246. switch ((uintptr_t)pOpts) {
  247. case 0UL:
  248. /*
  249. * print the list of enumeration names.
  250. */
  251. enumError( pOpts, pOD, paz_names, (int)name_ct );
  252. break;
  253. case 1UL:
  254. {
  255. unsigned int ix = pOD->optArg.argEnum;
  256. /*
  257. * print the name string.
  258. */
  259. if (ix >= name_ct)
  260. printf( "INVALID-%d", ix );
  261. else
  262. fputs( paz_names[ ix ], stdout );
  263. break;
  264. }
  265. case 2UL:
  266. {
  267. tSCC zInval[] = "*INVALID*";
  268. unsigned int ix = pOD->optArg.argEnum;
  269. /*
  270. * Replace the enumeration value with the name string.
  271. */
  272. if (ix >= name_ct)
  273. return (uintptr_t)zInval;
  274. res = (uintptr_t)paz_names[ ix ];
  275. break;
  276. }
  277. default:
  278. res = findName( pOD->optArg.argString, pOpts, pOD, paz_names, name_ct );
  279. if (pOD->fOptState & OPTST_ALLOC_ARG) {
  280. AGFREE(pOD->optArg.argString);
  281. pOD->fOptState &= ~OPTST_ALLOC_ARG;
  282. pOD->optArg.argString = NULL;
  283. }
  284. }
  285. return res;
  286. }
  287. /*=export_func optionSetMembers
  288. * what: Convert between bit flag values and strings
  289. * private:
  290. *
  291. * arg: tOptions*, pOpts, the program options descriptor
  292. * arg: tOptDesc*, pOD, enumeration option description
  293. * arg: char const * const *,
  294. * paz_names, list of enumeration names
  295. * arg: unsigned int, name_ct, number of names in list
  296. *
  297. * doc: This converts the optArg.argString string from the option description
  298. * into the index corresponding to an entry in the name list.
  299. * This will match the generated enumeration value.
  300. * Full matches are always accepted. Partial matches are accepted
  301. * if there is only one partial match.
  302. =*/
  303. void
  304. optionSetMembers(
  305. tOptions* pOpts,
  306. tOptDesc* pOD,
  307. tCC* const * paz_names,
  308. unsigned int name_ct )
  309. {
  310. /*
  311. * IF the program option descriptor pointer is invalid,
  312. * then it is some sort of special request.
  313. */
  314. switch ((uintptr_t)pOpts) {
  315. case 0UL:
  316. /*
  317. * print the list of enumeration names.
  318. */
  319. enumError( pOpts, pOD, paz_names, (int)name_ct );
  320. return;
  321. case 1UL:
  322. {
  323. /*
  324. * print the name string.
  325. */
  326. uintptr_t bits = (uintptr_t)pOD->optCookie;
  327. uintptr_t res = 0;
  328. size_t len = 0;
  329. while (bits != 0) {
  330. if (bits & 1) {
  331. if (len++ > 0) fputs( " | ", stdout );
  332. fputs( paz_names[ res ], stdout );
  333. }
  334. if (++res >= name_ct) break;
  335. bits >>= 1;
  336. }
  337. return;
  338. }
  339. case 2UL:
  340. {
  341. char* pz;
  342. uintptr_t bits = (uintptr_t)pOD->optCookie;
  343. uintptr_t res = 0;
  344. size_t len = 0;
  345. /*
  346. * Replace the enumeration value with the name string.
  347. * First, determine the needed length, then allocate and fill in.
  348. */
  349. while (bits != 0) {
  350. if (bits & 1)
  351. len += strlen( paz_names[ res ]) + 8;
  352. if (++res >= name_ct) break;
  353. bits >>= 1;
  354. }
  355. pOD->optArg.argString = pz = AGALOC( len, "enum name" );
  356. /*
  357. * Start by clearing all the bits. We want to turn off any defaults
  358. * because we will be restoring to current state, not adding to
  359. * the default set of bits.
  360. */
  361. strcpy( pz, "none" );
  362. pz += 4;
  363. bits = (uintptr_t)pOD->optCookie;
  364. res = 0;
  365. while (bits != 0) {
  366. if (bits & 1) {
  367. strcpy( pz, " + " );
  368. strcpy( pz+3, paz_names[ res ]);
  369. pz += strlen( paz_names[ res ]) + 3;
  370. }
  371. if (++res >= name_ct) break;
  372. bits >>= 1;
  373. }
  374. return;
  375. }
  376. default:
  377. break;
  378. }
  379. {
  380. tCC* pzArg = pOD->optArg.argString;
  381. uintptr_t res;
  382. if ((pzArg == NULL) || (*pzArg == NUL)) {
  383. pOD->optCookie = (void*)0;
  384. return;
  385. }
  386. res = (uintptr_t)pOD->optCookie;
  387. for (;;) {
  388. tSCC zSpn[] = " ,|+\t\r\f\n";
  389. int iv, len;
  390. pzArg += strspn( pzArg, zSpn );
  391. iv = (*pzArg == '!');
  392. if (iv)
  393. pzArg += strspn( pzArg+1, zSpn ) + 1;
  394. len = strcspn( pzArg, zSpn );
  395. if (len == 0)
  396. break;
  397. if ((len == 3) && (strncmp(pzArg, zAll, (size_t)3) == 0)) {
  398. if (iv)
  399. res = 0;
  400. else res = ~0UL;
  401. }
  402. else if ((len == 4) && (strncmp(pzArg, zNone, (size_t)4) == 0)) {
  403. if (! iv)
  404. res = 0;
  405. }
  406. else do {
  407. char* pz;
  408. uintptr_t bit = strtoul( pzArg, &pz, 0 );
  409. if (pz != pzArg + len) {
  410. char z[ AO_NAME_SIZE ];
  411. tCC* p;
  412. if (*pz != NUL) {
  413. if (len >= AO_NAME_LIMIT)
  414. break;
  415. strncpy( z, pzArg, (size_t)len );
  416. z[len] = NUL;
  417. p = z;
  418. } else {
  419. p = pzArg;
  420. }
  421. bit = 1UL << findName(p, pOpts, pOD, paz_names, name_ct);
  422. }
  423. if (iv)
  424. res &= ~bit;
  425. else res |= bit;
  426. } while (0);
  427. if (pzArg[len] == NUL)
  428. break;
  429. pzArg += len + 1;
  430. }
  431. if (name_ct < (8 * sizeof( uintptr_t ))) {
  432. res &= (1UL << name_ct) - 1UL;
  433. }
  434. pOD->optCookie = (void*)res;
  435. }
  436. }
  437. /*
  438. * Local Variables:
  439. * mode: C
  440. * c-file-style: "stroustrup"
  441. * indent-tabs-mode: nil
  442. * End:
  443. * end of autoopts/enumeration.c */