1
0

putshell.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * $Id: putshell.c,v 4.20 2007/07/04 21:36:38 bkorb Exp $
  3. * Time-stamp: "2007-07-04 11:34:33 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-2007 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. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  26. * fa82ca978890795162346e661b47161a pkg/libopts/COPYING.lgplv3
  27. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  28. */
  29. /* = = = START-STATIC-FORWARD = = = */
  30. /* static forward declarations maintained by :mkfwd */
  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))( (tOptions*)2UL, 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. pz += strspn( pz, " +\t\n\f" );
  153. for (;;) {
  154. int ch = *(pz++);
  155. if (islower( ch )) fputc( toupper( ch ), stdout );
  156. else if (isalnum( ch )) fputc( ch, stdout );
  157. else if (isspace( ch )
  158. || (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. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  220. fputc( '\'', stdout );
  221. (*(pOD->pOptProc))( (tOptions*)1UL, pOD );
  222. fputc( '\'', stdout );
  223. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  224. }
  225. /*
  226. * If the argument type is numeric, the last arg pointer
  227. * is really the VALUE of the string that was pointed to.
  228. */
  229. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_BOOLEAN)
  230. printf( zFullOptFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  231. (pOD->optArg.argBool == 0) ? "false" : "true" );
  232. /*
  233. * IF the option has an empty value,
  234. * THEN we set the argument to the occurrence count.
  235. */
  236. else if ( (pOD->optArg.argString == NULL)
  237. || (pOD->optArg.argString[0] == NUL) )
  238. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  239. pOD->optOccCt );
  240. /*
  241. * This option has a text value
  242. */
  243. else {
  244. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  245. putQuotedStr( pOD->optArg.argString );
  246. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  247. }
  248. } while (++optIx < pOpts->presetOptCt );
  249. if ( ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
  250. && (pOpts->curOptIdx < pOpts->origArgCt)) {
  251. fputs( "set --", stdout );
  252. for (optIx = pOpts->curOptIdx; optIx < pOpts->origArgCt; optIx++) {
  253. char* pzArg = pOpts->origArgVect[ optIx ];
  254. if (strchr( pzArg, '\'' ) == NULL)
  255. printf( " '%s'", pzArg );
  256. else {
  257. fputs( " '", stdout );
  258. for (;;) {
  259. char ch = *(pzArg++);
  260. switch (ch) {
  261. case '\'': fputs( "'\\''", stdout ); break;
  262. case NUL: goto arg_done;
  263. default: fputc( ch, stdout ); break;
  264. }
  265. } arg_done:;
  266. fputc( '\'', stdout );
  267. }
  268. }
  269. fputs( "\nOPTION_CT=0\n", stdout );
  270. }
  271. }
  272. /*
  273. * Local Variables:
  274. * mode: C
  275. * c-file-style: "stroustrup"
  276. * indent-tabs-mode: nil
  277. * End:
  278. * end of autoopts/putshell.c */