enumeration.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * $Id: enumeration.c,v 4.4 2005/02/15 02:12:18 bkorb Exp $
  3. * Time-stamp: "2005-02-14 14:29:55 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-2005 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. * 59 Temple Place - Suite 330,
  27. * Boston, MA 02111-1307, 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 optionEnumerationVal
  195. * what: Convert between enumeration values and strings
  196. * private:
  197. *
  198. * arg: tOptions*, pOpts, the program options descriptor
  199. * arg: tOptDesc*, pOD, enumeration option description
  200. * arg: tCC**, paz_names, list of enumeration names
  201. * arg: unsigned int, name_ct, number of names in list
  202. *
  203. * ret_type: char*
  204. * ret_desc: the enumeration value cast as a char*
  205. *
  206. * doc: This converts the pzLastArg string from the option description
  207. * into the index corresponding to an entry in the name list.
  208. * This will match the generated enumeration value.
  209. * Full matches are always accepted. Partial matches are accepted
  210. * if there is only one partial match.
  211. =*/
  212. char*
  213. optionEnumerationVal(
  214. tOptions* pOpts,
  215. tOptDesc* pOD,
  216. tCC** paz_names,
  217. unsigned int name_ct )
  218. {
  219. /*
  220. * IF the program option descriptor pointer is invalid,
  221. * then it is some sort of special request.
  222. */
  223. switch ((uintptr_t)pOpts) {
  224. case 0UL:
  225. /*
  226. * print the list of enumeration names.
  227. */
  228. enumError( pOpts, pOD, paz_names, name_ct );
  229. return (char*)0UL;
  230. case 1UL:
  231. /*
  232. * print the name string.
  233. */
  234. fputs( paz_names[ (uintptr_t)(pOD->pzLastArg) ], stdout );
  235. return (char*)0UL;
  236. case 2UL:
  237. /*
  238. * Replace the enumeration value with the name string.
  239. */
  240. return (char*)paz_names[ (uintptr_t)(pOD->pzLastArg) ];
  241. default:
  242. break;
  243. }
  244. return (char*)findName( pOD->pzLastArg, pOpts, pOD, paz_names, name_ct );
  245. }
  246. /*=export_func optionSetMembers
  247. * what: Convert between bit flag values and strings
  248. * private:
  249. *
  250. * arg: tOptions*, pOpts, the program options descriptor
  251. * arg: tOptDesc*, pOD, enumeration option description
  252. * arg: tCC**, paz_names, list of enumeration names
  253. * arg: unsigned int, name_ct, number of names in list
  254. *
  255. * doc: This converts the pzLastArg string from the option description
  256. * into the index corresponding to an entry in the name list.
  257. * This will match the generated enumeration value.
  258. * Full matches are always accepted. Partial matches are accepted
  259. * if there is only one partial match.
  260. =*/
  261. void
  262. optionSetMembers(
  263. tOptions* pOpts,
  264. tOptDesc* pOD,
  265. tCC** paz_names,
  266. unsigned int name_ct )
  267. {
  268. /*
  269. * IF the program option descriptor pointer is invalid,
  270. * then it is some sort of special request.
  271. */
  272. switch ((uintptr_t)pOpts) {
  273. case 0UL:
  274. /*
  275. * print the list of enumeration names.
  276. */
  277. enumError( pOpts, pOD, paz_names, name_ct );
  278. return;
  279. case 1UL:
  280. {
  281. /*
  282. * print the name string.
  283. */
  284. uintptr_t bits = (uintptr_t)pOD->optCookie;
  285. uintptr_t res = 0;
  286. size_t len = 0;
  287. while (bits != 0) {
  288. if (bits & 1) {
  289. if (len++ > 0) fputs( " | ", stdout );
  290. fputs( paz_names[ res ], stdout );
  291. }
  292. if (++res >= name_ct) break;
  293. bits >>= 1;
  294. }
  295. return;
  296. }
  297. case 2UL:
  298. {
  299. char* pz;
  300. uintptr_t bits = (uintptr_t)pOD->optCookie;
  301. uintptr_t res = 0;
  302. size_t len = 0;
  303. /*
  304. * Replace the enumeration value with the name string.
  305. * First, determine the needed length, then allocate and fill in.
  306. */
  307. while (bits != 0) {
  308. if (bits & 1)
  309. len += strlen( paz_names[ res ]) + 8;
  310. if (++res >= name_ct) break;
  311. bits >>= 1;
  312. }
  313. pOD->pzLastArg = pz = malloc( len );
  314. /*
  315. * Start by clearing all the bits. We want to turn off any defaults
  316. * because we will be restoring to current state, not adding to
  317. * the default set of bits.
  318. */
  319. strcpy( pz, "none" );
  320. pz += 4;
  321. bits = (uintptr_t)pOD->optCookie;
  322. res = 0;
  323. while (bits != 0) {
  324. if (bits & 1) {
  325. strcpy( pz, " + " );
  326. strcpy( pz+3, paz_names[ res ]);
  327. pz += strlen( paz_names[ res ]) + 3;
  328. }
  329. if (++res >= name_ct) break;
  330. bits >>= 1;
  331. }
  332. return;
  333. }
  334. default:
  335. break;
  336. }
  337. {
  338. tCC* pzArg = pOD->pzLastArg;
  339. uintptr_t res;
  340. if ((pzArg == NULL) || (*pzArg == NUL)) {
  341. pOD->optCookie = (void*)0;
  342. return;
  343. }
  344. res = (uintptr_t)pOD->optCookie;
  345. for (;;) {
  346. tSCC zSpn[] = " ,|+\t\r\f\n";
  347. int iv, len;
  348. pzArg += strspn( pzArg, zSpn );
  349. iv = (*pzArg == '!');
  350. if (iv)
  351. pzArg += strspn( pzArg+1, zSpn ) + 1;
  352. len = strcspn( pzArg, zSpn );
  353. if (len == 0)
  354. break;
  355. if ((len == 3) && (strncmp( pzArg, zAll, 3 ) == 0)) {
  356. if (iv)
  357. res = 0;
  358. else res = ~0;
  359. }
  360. else if ((len == 4) && (strncmp( pzArg, zNone, 4 ) == 0)) {
  361. if (! iv)
  362. res = 0;
  363. }
  364. else do {
  365. char* pz;
  366. uintptr_t bit = strtoul( pzArg, &pz, 0 );
  367. if (pz != pzArg + len) {
  368. char z[ AO_NAME_SIZE ];
  369. tCC* p;
  370. if (*pz != NUL) {
  371. if (len >= AO_NAME_LIMIT)
  372. break;
  373. strncpy( z, pzArg, len );
  374. z[len] = NUL;
  375. p = z;
  376. } else {
  377. p = pzArg;
  378. }
  379. bit = 1UL << findName(p, pOpts, pOD, paz_names, name_ct);
  380. }
  381. if (iv)
  382. res &= ~bit;
  383. else res |= bit;
  384. } while (0);
  385. if (pzArg[len] == NUL)
  386. break;
  387. pzArg += len + 1;
  388. }
  389. if (name_ct < (8 * sizeof( uintptr_t ))) {
  390. res &= (1UL << name_ct) - 1UL;
  391. }
  392. pOD->optCookie = (void*)res;
  393. }
  394. }
  395. /*
  396. * Local Variables:
  397. * mode: C
  398. * c-file-style: "stroustrup"
  399. * tab-width: 4
  400. * indent-tabs-mode: nil
  401. * End:
  402. * end of autoopts/enumeration.c */