enumeration.c 14 KB

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