1
0

enumeration.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * $Id: enumeration.c,v 4.9 2006/03/25 19:24:56 bkorb Exp $
  3. * Time-stamp: "2005-12-09 06:37:15 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-2006 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** paz_names,
  60. int name_ct );
  61. static uintptr_t
  62. findName(
  63. tCC* pzName,
  64. tOptions* pOpts,
  65. tOptDesc* pOD,
  66. tCC** paz_names,
  67. unsigned int name_ct );
  68. /* = = = END-STATIC-FORWARD = = = */
  69. static void
  70. enumError(
  71. tOptions* pOpts,
  72. tOptDesc* pOD,
  73. tCC** 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,
  80. pOpts->pzProgName, pOD->pzLastArg );
  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** 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", 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** 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, 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, 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: const char*
  202. * ret_desc: the enumeration name from const memory
  203. *
  204. * doc: This converts an enumeration value into the matching string.
  205. =*/
  206. const char*
  207. optionKeywordName(
  208. tOptDesc* pOD,
  209. unsigned int enum_val )
  210. {
  211. tOptDesc od;
  212. od.pzLastArg = (const char*)(uintptr_t)enum_val;
  213. (*(pOD->pOptProc))( (void*)(2UL), &od );
  214. return od.pzLastArg;
  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: const char**, paz_names, list of enumeration names
  223. * arg: unsigned int, name_ct, number of names in list
  224. *
  225. * ret_type: char*
  226. * ret_desc: the enumeration value cast as a char*
  227. *
  228. * doc: This converts the pzLastArg 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. char*
  235. optionEnumerationVal(
  236. tOptions* pOpts,
  237. tOptDesc* pOD,
  238. tCC** paz_names,
  239. unsigned int name_ct )
  240. {
  241. /*
  242. * IF the program option descriptor pointer is invalid,
  243. * then it is some sort of special request.
  244. */
  245. switch ((uintptr_t)pOpts) {
  246. case 0UL:
  247. /*
  248. * print the list of enumeration names.
  249. */
  250. enumError( pOpts, pOD, paz_names, name_ct );
  251. return (char*)0UL;
  252. case 1UL:
  253. {
  254. unsigned int ix = (uintptr_t)(pOD->pzLastArg);
  255. /*
  256. * print the name string.
  257. */
  258. if (ix >= name_ct)
  259. printf( "INVALID-%d", ix );
  260. else
  261. fputs( paz_names[ ix ], stdout );
  262. return (char*)0UL;
  263. }
  264. case 2UL:
  265. {
  266. tSCC zInval[] = "*INVALID*";
  267. unsigned int ix = (uintptr_t)(pOD->pzLastArg);
  268. /*
  269. * Replace the enumeration value with the name string.
  270. */
  271. if (ix >= name_ct)
  272. return (char*)zInval;
  273. return (char*)paz_names[ ix ];
  274. }
  275. default:
  276. break;
  277. }
  278. return (char*)findName( pOD->pzLastArg, pOpts, pOD, paz_names, name_ct );
  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: const char**, paz_names, list of enumeration names
  287. * arg: unsigned int, name_ct, number of names in list
  288. *
  289. * doc: This converts the pzLastArg string from the option description
  290. * into the index corresponding to an entry in the name list.
  291. * This will match the generated enumeration value.
  292. * Full matches are always accepted. Partial matches are accepted
  293. * if there is only one partial match.
  294. =*/
  295. void
  296. optionSetMembers(
  297. tOptions* pOpts,
  298. tOptDesc* pOD,
  299. tCC** paz_names,
  300. unsigned int name_ct )
  301. {
  302. /*
  303. * IF the program option descriptor pointer is invalid,
  304. * then it is some sort of special request.
  305. */
  306. switch ((uintptr_t)pOpts) {
  307. case 0UL:
  308. /*
  309. * print the list of enumeration names.
  310. */
  311. enumError( pOpts, pOD, paz_names, name_ct );
  312. return;
  313. case 1UL:
  314. {
  315. /*
  316. * print the name string.
  317. */
  318. uintptr_t bits = (uintptr_t)pOD->optCookie;
  319. uintptr_t res = 0;
  320. size_t len = 0;
  321. while (bits != 0) {
  322. if (bits & 1) {
  323. if (len++ > 0) fputs( " | ", stdout );
  324. fputs( paz_names[ res ], stdout );
  325. }
  326. if (++res >= name_ct) break;
  327. bits >>= 1;
  328. }
  329. return;
  330. }
  331. case 2UL:
  332. {
  333. char* pz;
  334. uintptr_t bits = (uintptr_t)pOD->optCookie;
  335. uintptr_t res = 0;
  336. size_t len = 0;
  337. /*
  338. * Replace the enumeration value with the name string.
  339. * First, determine the needed length, then allocate and fill in.
  340. */
  341. while (bits != 0) {
  342. if (bits & 1)
  343. len += strlen( paz_names[ res ]) + 8;
  344. if (++res >= name_ct) break;
  345. bits >>= 1;
  346. }
  347. pOD->pzLastArg = pz = malloc( len );
  348. /*
  349. * Start by clearing all the bits. We want to turn off any defaults
  350. * because we will be restoring to current state, not adding to
  351. * the default set of bits.
  352. */
  353. strcpy( pz, "none" );
  354. pz += 4;
  355. bits = (uintptr_t)pOD->optCookie;
  356. res = 0;
  357. while (bits != 0) {
  358. if (bits & 1) {
  359. strcpy( pz, " + " );
  360. strcpy( pz+3, paz_names[ res ]);
  361. pz += strlen( paz_names[ res ]) + 3;
  362. }
  363. if (++res >= name_ct) break;
  364. bits >>= 1;
  365. }
  366. return;
  367. }
  368. default:
  369. break;
  370. }
  371. {
  372. tCC* pzArg = pOD->pzLastArg;
  373. uintptr_t res;
  374. if ((pzArg == NULL) || (*pzArg == NUL)) {
  375. pOD->optCookie = (void*)0;
  376. return;
  377. }
  378. res = (uintptr_t)pOD->optCookie;
  379. for (;;) {
  380. tSCC zSpn[] = " ,|+\t\r\f\n";
  381. int iv, len;
  382. pzArg += strspn( pzArg, zSpn );
  383. iv = (*pzArg == '!');
  384. if (iv)
  385. pzArg += strspn( pzArg+1, zSpn ) + 1;
  386. len = strcspn( pzArg, zSpn );
  387. if (len == 0)
  388. break;
  389. if ((len == 3) && (strncmp( pzArg, zAll, 3 ) == 0)) {
  390. if (iv)
  391. res = 0;
  392. else res = ~0;
  393. }
  394. else if ((len == 4) && (strncmp( pzArg, zNone, 4 ) == 0)) {
  395. if (! iv)
  396. res = 0;
  397. }
  398. else do {
  399. char* pz;
  400. uintptr_t bit = strtoul( pzArg, &pz, 0 );
  401. if (pz != pzArg + len) {
  402. char z[ AO_NAME_SIZE ];
  403. tCC* p;
  404. if (*pz != NUL) {
  405. if (len >= AO_NAME_LIMIT)
  406. break;
  407. strncpy( z, pzArg, len );
  408. z[len] = NUL;
  409. p = z;
  410. } else {
  411. p = pzArg;
  412. }
  413. bit = 1UL << findName(p, pOpts, pOD, paz_names, name_ct);
  414. }
  415. if (iv)
  416. res &= ~bit;
  417. else res |= bit;
  418. } while (0);
  419. if (pzArg[len] == NUL)
  420. break;
  421. pzArg += len + 1;
  422. }
  423. if (name_ct < (8 * sizeof( uintptr_t ))) {
  424. res &= (1UL << name_ct) - 1UL;
  425. }
  426. pOD->optCookie = (void*)res;
  427. }
  428. }
  429. /*
  430. * Local Variables:
  431. * mode: C
  432. * c-file-style: "stroustrup"
  433. * tab-width: 4
  434. * indent-tabs-mode: nil
  435. * End:
  436. * end of autoopts/enumeration.c */