putshell.c 11 KB

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