putshell.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * $Id: putshell.c,v 4.27 2009/08/01 17:43:06 bkorb Exp $
  3. * Time-stamp: "2008-07-27 12:14:38 bkorb"
  4. *
  5. * This module will interpret the options set in the tOptions
  6. * structure and print them to standard out in a fashion that
  7. * will allow them to be interpreted by the Bourne or Korn shells.
  8. *
  9. * This file is part of AutoOpts, a companion to AutoGen.
  10. * AutoOpts is free software.
  11. * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
  12. *
  13. * AutoOpts is available under any one of two licenses. The license
  14. * in use must be one of these two and the choice is under the control
  15. * of the user of the license.
  16. *
  17. * The GNU Lesser General Public License, version 3 or later
  18. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  19. *
  20. * The Modified Berkeley Software Distribution License
  21. * See the file "COPYING.mbsd"
  22. *
  23. * These files have the following md5sums:
  24. *
  25. * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
  26. * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
  27. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  28. */
  29. /* = = = START-STATIC-FORWARD = = = */
  30. /* static forward declarations maintained by mk-fwd */
  31. static void
  32. putQuotedStr( tCC* pzStr );
  33. /* = = = END-STATIC-FORWARD = = = */
  34. /*
  35. * Make sure embedded single quotes come out okay. The initial quote has
  36. * been emitted and the closing quote will be upon return.
  37. */
  38. static void
  39. putQuotedStr( tCC* pzStr )
  40. {
  41. /*
  42. * Handle empty strings to make the rest of the logic simpler.
  43. */
  44. if ((pzStr == NULL) || (*pzStr == NUL)) {
  45. fputs( "''", stdout );
  46. return;
  47. }
  48. /*
  49. * Emit any single quotes/apostrophes at the start of the string and
  50. * bail if that is all we need to do.
  51. */
  52. while (*pzStr == '\'') {
  53. fputs( "\\'", stdout );
  54. pzStr++;
  55. }
  56. if (*pzStr == NUL)
  57. return;
  58. /*
  59. * Start the single quote string
  60. */
  61. fputc( '\'', stdout );
  62. for (;;) {
  63. tCC* pz = strchr( pzStr, '\'' );
  64. if (pz == NULL)
  65. break;
  66. /*
  67. * Emit the string up to the single quote (apostrophe) we just found.
  68. */
  69. (void)fwrite( pzStr, (size_t)(pz - pzStr), (size_t)1, stdout );
  70. fputc( '\'', stdout );
  71. pzStr = pz;
  72. /*
  73. * Emit an escaped apostrophe for every one we find.
  74. * If that ends the string, do not re-open the single quotes.
  75. */
  76. while (*++pzStr == '\'') fputs( "\\'", stdout );
  77. if (*pzStr == NUL)
  78. return;
  79. fputc( '\'', stdout );
  80. }
  81. /*
  82. * If we broke out of the loop, we must still emit the remaining text
  83. * and then close the single quote string.
  84. */
  85. fputs( pzStr, stdout );
  86. fputc( '\'', stdout );
  87. }
  88. /*=export_func optionPutShell
  89. * what: write a portable shell script to parse options
  90. * private:
  91. * arg: tOptions*, pOpts, the program options descriptor
  92. * doc: This routine will emit portable shell script text for parsing
  93. * the options described in the option definitions.
  94. =*/
  95. void
  96. optionPutShell( tOptions* pOpts )
  97. {
  98. int optIx = 0;
  99. tSCC zOptCtFmt[] = "OPTION_CT=%d\nexport OPTION_CT\n";
  100. tSCC zOptNumFmt[] = "%1$s_%2$s=%3$d # 0x%3$X\nexport %1$s_%2$s\n";
  101. tSCC zOptDisabl[] = "%1$s_%2$s=%3$s\nexport %1$s_%2$s\n";
  102. tSCC zOptValFmt[] = "%s_%s=";
  103. tSCC zOptEnd[] = "\nexport %s_%s\n";
  104. tSCC zFullOptFmt[]= "%1$s_%2$s='%3$s'\nexport %1$s_%2$s\n";
  105. tSCC zEquivMode[] = "%1$s_%2$s_MODE='%3$s'\nexport %1$s_%2$s_MODE\n";
  106. printf( zOptCtFmt, pOpts->curOptIdx-1 );
  107. do {
  108. tOptDesc* pOD = pOpts->pOptDesc + optIx;
  109. if (SKIP_OPT(pOD))
  110. continue;
  111. /*
  112. * Equivalence classes are hard to deal with. Where the
  113. * option data wind up kind of squishes around. For the purposes
  114. * of emitting shell state, they are not recommended, but we'll
  115. * do something. I guess we'll emit the equivalenced-to option
  116. * at the point in time when the base option is found.
  117. */
  118. if (pOD->optEquivIndex != NO_EQUIVALENT)
  119. continue; /* equivalence to a different option */
  120. /*
  121. * Equivalenced to a different option. Process the current option
  122. * as the equivalenced-to option. Keep the persistent state bits,
  123. * but copy over the set-state bits.
  124. */
  125. if (pOD->optActualIndex != optIx) {
  126. tOptDesc* p = pOpts->pOptDesc + pOD->optActualIndex;
  127. p->optArg = pOD->optArg;
  128. p->fOptState &= OPTST_PERSISTENT_MASK;
  129. p->fOptState |= pOD->fOptState & ~OPTST_PERSISTENT_MASK;
  130. printf( zEquivMode, pOpts->pzPROGNAME, pOD->pz_NAME, p->pz_NAME );
  131. pOD = p;
  132. }
  133. /*
  134. * If the argument type is a set membership bitmask, then we always
  135. * emit the thing. We do this because it will always have some sort
  136. * of bitmask value and we need to emit the bit values.
  137. */
  138. if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP) {
  139. char const * pz;
  140. uintptr_t val = 1;
  141. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  142. (int)(uintptr_t)(pOD->optCookie) );
  143. pOD->optCookie = (void*)(uintptr_t)~0UL;
  144. (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD);
  145. /*
  146. * We are building the typeset list. The list returned starts with
  147. * 'none + ' for use by option saving stuff. We must ignore that.
  148. */
  149. pz = pOD->optArg.argString + 7;
  150. while (*pz != NUL) {
  151. printf( "typeset -x -i %s_", pOD->pz_NAME );
  152. while (IS_PLUS_N_SPACE_CHAR(*pz)) pz++;
  153. for (;;) {
  154. int ch = *(pz++);
  155. if (IS_LOWER_CASE_CHAR(ch)) fputc(toupper(ch), stdout);
  156. else if (IS_UPPER_CASE_CHAR(ch)) fputc(ch, stdout);
  157. else if (IS_PLUS_N_SPACE_CHAR(ch)) goto name_done;
  158. else if (ch == NUL) { pz--; goto name_done; }
  159. else fputc( '_', stdout );
  160. } name_done:;
  161. printf( "=%1$lu # 0x%1$lX\n", (unsigned long)val );
  162. val <<= 1;
  163. }
  164. AGFREE(pOD->optArg.argString);
  165. pOD->optArg.argString = NULL;
  166. pOD->fOptState &= ~OPTST_ALLOC_ARG;
  167. continue;
  168. }
  169. /*
  170. * IF the option was either specified or it wakes up enabled,
  171. * then we will emit information. Otherwise, skip it.
  172. * The idea is that if someone defines an option to initialize
  173. * enabled, we should tell our shell script that it is enabled.
  174. */
  175. if (UNUSED_OPT( pOD ) && DISABLED_OPT( pOD ))
  176. continue;
  177. /*
  178. * Handle stacked arguments
  179. */
  180. if ( (pOD->fOptState & OPTST_STACKED)
  181. && (pOD->optCookie != NULL) ) {
  182. tSCC zOptCookieCt[] = "%1$s_%2$s_CT=%3$d\nexport %1$s_%2$s_CT\n";
  183. tArgList* pAL = (tArgList*)pOD->optCookie;
  184. tCC** ppz = pAL->apzArgs;
  185. int ct = pAL->useCt;
  186. printf( zOptCookieCt, pOpts->pzPROGNAME, pOD->pz_NAME, ct );
  187. while (--ct >= 0) {
  188. tSCC numarg_z[] = "%s_%s_%d=";
  189. tSCC end_z[] = "\nexport %s_%s_%d\n";
  190. printf( numarg_z, pOpts->pzPROGNAME, pOD->pz_NAME,
  191. pAL->useCt - ct );
  192. putQuotedStr( *(ppz++) );
  193. printf( end_z, pOpts->pzPROGNAME, pOD->pz_NAME,
  194. pAL->useCt - ct );
  195. }
  196. }
  197. /*
  198. * If the argument has been disabled,
  199. * Then set its value to the disablement string
  200. */
  201. else if ((pOD->fOptState & OPTST_DISABLED) != 0)
  202. printf( zOptDisabl, pOpts->pzPROGNAME, pOD->pz_NAME,
  203. (pOD->pz_DisablePfx != NULL)
  204. ? pOD->pz_DisablePfx : "false" );
  205. /*
  206. * If the argument type is numeric, the last arg pointer
  207. * is really the VALUE of the string that was pointed to.
  208. */
  209. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NUMERIC)
  210. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  211. (int)pOD->optArg.argInt );
  212. /*
  213. * If the argument type is an enumeration, then it is much
  214. * like a text value, except we call the callback function
  215. * to emit the value corresponding to the "optArg" number.
  216. */
  217. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_ENUMERATION) {
  218. uintptr_t e_val = pOD->optArg.argEnum;
  219. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  220. /*
  221. * Convert value to string, print that and restore numeric value.
  222. */
  223. (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD);
  224. printf("'%s'", pOD->optArg.argString);
  225. if (pOD->fOptState & OPTST_ALLOC_ARG)
  226. AGFREE(pOD->optArg.argString);
  227. pOD->optArg.argEnum = e_val;
  228. printf(zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME);
  229. }
  230. /*
  231. * If the argument type is numeric, the last arg pointer
  232. * is really the VALUE of the string that was pointed to.
  233. */
  234. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_BOOLEAN)
  235. printf( zFullOptFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  236. (pOD->optArg.argBool == 0) ? "false" : "true" );
  237. /*
  238. * IF the option has an empty value,
  239. * THEN we set the argument to the occurrence count.
  240. */
  241. else if ( (pOD->optArg.argString == NULL)
  242. || (pOD->optArg.argString[0] == NUL) )
  243. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  244. pOD->optOccCt );
  245. /*
  246. * This option has a text value
  247. */
  248. else {
  249. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  250. putQuotedStr( pOD->optArg.argString );
  251. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  252. }
  253. } while (++optIx < pOpts->presetOptCt );
  254. if ( ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
  255. && (pOpts->curOptIdx < pOpts->origArgCt)) {
  256. fputs( "set --", stdout );
  257. for (optIx = pOpts->curOptIdx; optIx < pOpts->origArgCt; optIx++) {
  258. char* pzArg = pOpts->origArgVect[ optIx ];
  259. if (strchr( pzArg, '\'' ) == NULL)
  260. printf( " '%s'", pzArg );
  261. else {
  262. fputs( " '", stdout );
  263. for (;;) {
  264. char ch = *(pzArg++);
  265. switch (ch) {
  266. case '\'': fputs( "'\\''", stdout ); break;
  267. case NUL: goto arg_done;
  268. default: fputc( ch, stdout ); break;
  269. }
  270. } arg_done:;
  271. fputc( '\'', stdout );
  272. }
  273. }
  274. fputs( "\nOPTION_CT=0\n", stdout );
  275. }
  276. }
  277. /*
  278. * Local Variables:
  279. * mode: C
  280. * c-file-style: "stroustrup"
  281. * indent-tabs-mode: nil
  282. * End:
  283. * end of autoopts/putshell.c */