enumeration.c 14 KB

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