putshell.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * $Id: putshell.c,v 4.18 2007/02/04 17:44:12 bkorb Exp $
  3. * Time-stamp: "2007-01-13 10:29:39 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. /*
  10. * Automated Options copyright 1992-2007 Bruce Korb
  11. *
  12. * Automated Options is free software.
  13. * You may redistribute it and/or modify it under the terms of the
  14. * GNU General Public License, as published by the Free Software
  15. * Foundation; either version 2, or (at your option) any later version.
  16. *
  17. * Automated Options is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with Automated Options. See the file "COPYING". If not,
  24. * write to: The Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor,
  26. * Boston, MA 02110-1301, USA.
  27. *
  28. * As a special exception, Bruce Korb gives permission for additional
  29. * uses of the text contained in his release of AutoOpts.
  30. *
  31. * The exception is that, if you link the AutoOpts library with other
  32. * files to produce an executable, this does not by itself cause the
  33. * resulting executable to be covered by the GNU General Public License.
  34. * Your use of that executable is in no way restricted on account of
  35. * linking the AutoOpts library code into it.
  36. *
  37. * This exception does not however invalidate any other reasons why
  38. * the executable file might be covered by the GNU General Public License.
  39. *
  40. * This exception applies only to the code released by Bruce Korb under
  41. * the name AutoOpts. If you copy code from other sources under the
  42. * General Public License into a copy of AutoOpts, as the General Public
  43. * License permits, the exception does not apply to the code that you add
  44. * in this way. To avoid misleading anyone as to the status of such
  45. * modified files, you must delete this exception notice from them.
  46. *
  47. * If you write modifications of your own for AutoOpts, it is your choice
  48. * whether to permit this exception to apply to your modifications.
  49. * If you do not wish that, delete this exception notice.
  50. */
  51. /* = = = START-STATIC-FORWARD = = = */
  52. /* static forward declarations maintained by :mkfwd */
  53. static void
  54. putQuotedStr( tCC* pzStr );
  55. /* = = = END-STATIC-FORWARD = = = */
  56. /*
  57. * Make sure embedded single quotes come out okay. The initial quote has
  58. * been emitted and the closing quote will be upon return.
  59. */
  60. static void
  61. putQuotedStr( tCC* pzStr )
  62. {
  63. /*
  64. * Handle empty strings to make the rest of the logic simpler.
  65. */
  66. if ((pzStr == NULL) || (*pzStr == NUL)) {
  67. fputs( "''", stdout );
  68. return;
  69. }
  70. /*
  71. * Emit any single quotes/apostrophes at the start of the string and
  72. * bail if that is all we need to do.
  73. */
  74. while (*pzStr == '\'') {
  75. fputs( "\\'", stdout );
  76. pzStr++;
  77. }
  78. if (*pzStr == NUL)
  79. return;
  80. /*
  81. * Start the single quote string
  82. */
  83. fputc( '\'', stdout );
  84. for (;;) {
  85. tCC* pz = strchr( pzStr, '\'' );
  86. if (pz == NULL)
  87. break;
  88. /*
  89. * Emit the string up to the single quote (apostrophe) we just found.
  90. */
  91. (void)fwrite( pzStr, (size_t)(pz - pzStr), (size_t)1, stdout );
  92. fputc( '\'', stdout );
  93. pzStr = pz;
  94. /*
  95. * Emit an escaped apostrophe for every one we find.
  96. * If that ends the string, do not re-open the single quotes.
  97. */
  98. while (*++pzStr == '\'') fputs( "\\'", stdout );
  99. if (*pzStr == NUL)
  100. return;
  101. fputc( '\'', stdout );
  102. }
  103. /*
  104. * If we broke out of the loop, we must still emit the remaining text
  105. * and then close the single quote string.
  106. */
  107. fputs( pzStr, stdout );
  108. fputc( '\'', stdout );
  109. }
  110. /*=export_func optionPutShell
  111. * what: write a portable shell script to parse options
  112. * private:
  113. * arg: tOptions*, pOpts, the program options descriptor
  114. * doc: This routine will emit portable shell script text for parsing
  115. * the options described in the option definitions.
  116. =*/
  117. void
  118. optionPutShell( tOptions* pOpts )
  119. {
  120. int optIx = 0;
  121. tSCC zOptCtFmt[] = "OPTION_CT=%d\nexport OPTION_CT\n";
  122. tSCC zOptNumFmt[] = "%1$s_%2$s=%3$d # 0x%3$X\nexport %1$s_%2$s\n";
  123. tSCC zOptDisabl[] = "%1$s_%2$s=%3$s\nexport %1$s_%2$s\n";
  124. tSCC zOptValFmt[] = "%s_%s=";
  125. tSCC zOptEnd[] = "\nexport %s_%s\n";
  126. tSCC zFullOptFmt[]= "%1$s_%2$s='%3$s'\nexport %1$s_%2$s\n";
  127. tSCC zEquivMode[] = "%1$s_%2$s_MODE='%3$s'\nexport %1$s_%2$s_MODE\n";
  128. printf( zOptCtFmt, pOpts->curOptIdx-1 );
  129. do {
  130. tOptDesc* pOD = pOpts->pOptDesc + optIx;
  131. if (SKIP_OPT(pOD))
  132. continue;
  133. /*
  134. * Equivalence classes are hard to deal with. Where the
  135. * option data wind up kind of squishes around. For the purposes
  136. * of emitting shell state, they are not recommended, but we'll
  137. * do something. I guess we'll emit the equivalenced-to option
  138. * at the point in time when the base option is found.
  139. */
  140. if (pOD->optEquivIndex != NO_EQUIVALENT)
  141. continue; /* equivalence to a different option */
  142. /*
  143. * Equivalenced to a different option. Process the current option
  144. * as the equivalenced-to option. Keep the persistent state bits,
  145. * but copy over the set-state bits.
  146. */
  147. if (pOD->optActualIndex != optIx) {
  148. tOptDesc* p = pOpts->pOptDesc + pOD->optActualIndex;
  149. p->optArg = pOD->optArg;
  150. p->fOptState &= OPTST_PERSISTENT_MASK;
  151. p->fOptState |= pOD->fOptState & ~OPTST_PERSISTENT_MASK;
  152. printf( zEquivMode, pOpts->pzPROGNAME, pOD->pz_NAME, p->pz_NAME );
  153. pOD = p;
  154. }
  155. /*
  156. * If the argument type is a set membership bitmask, then we always
  157. * emit the thing. We do this because it will always have some sort
  158. * of bitmask value and we need to emit the bit values.
  159. */
  160. if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP) {
  161. char const * pz;
  162. uintptr_t val = 1;
  163. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  164. (int)(uintptr_t)(pOD->optCookie) );
  165. pOD->optCookie = (void*)(uintptr_t)~0UL;
  166. (*(pOD->pOptProc))( (tOptions*)2UL, pOD );
  167. /*
  168. * We are building the typeset list. The list returned starts with
  169. * 'none + ' for use by option saving stuff. We must ignore that.
  170. */
  171. pz = pOD->optArg.argString + 7;
  172. while (*pz != NUL) {
  173. printf( "typeset -x -i %s_", pOD->pz_NAME );
  174. pz += strspn( pz, " +\t\n\f" );
  175. for (;;) {
  176. int ch = *(pz++);
  177. if (islower( ch )) fputc( toupper( ch ), stdout );
  178. else if (isalnum( ch )) fputc( ch, stdout );
  179. else if (isspace( ch )
  180. || (ch == '+')) goto name_done;
  181. else if (ch == NUL) { pz--; goto name_done; }
  182. else fputc( '_', stdout );
  183. } name_done:;
  184. printf( "=%1$lu # 0x%1$lX\n", (unsigned long)val );
  185. val <<= 1;
  186. }
  187. AGFREE(pOD->optArg.argString);
  188. pOD->optArg.argString = NULL;
  189. pOD->fOptState &= ~OPTST_ALLOC_ARG;
  190. continue;
  191. }
  192. /*
  193. * IF the option was either specified or it wakes up enabled,
  194. * then we will emit information. Otherwise, skip it.
  195. * The idea is that if someone defines an option to initialize
  196. * enabled, we should tell our shell script that it is enabled.
  197. */
  198. if (UNUSED_OPT( pOD ) && DISABLED_OPT( pOD ))
  199. continue;
  200. /*
  201. * Handle stacked arguments
  202. */
  203. if ( (pOD->fOptState & OPTST_STACKED)
  204. && (pOD->optCookie != NULL) ) {
  205. tSCC zOptCookieCt[] = "%1$s_%2$s_CT=%3$d\nexport %1$s_%2$s_CT\n";
  206. tArgList* pAL = (tArgList*)pOD->optCookie;
  207. tCC** ppz = pAL->apzArgs;
  208. int ct = pAL->useCt;
  209. printf( zOptCookieCt, pOpts->pzPROGNAME, pOD->pz_NAME, ct );
  210. while (--ct >= 0) {
  211. tSCC numarg_z[] = "%s_%s_%d=";
  212. tSCC end_z[] = "\nexport %s_%s_%d\n";
  213. printf( numarg_z, pOpts->pzPROGNAME, pOD->pz_NAME,
  214. pAL->useCt - ct );
  215. putQuotedStr( *(ppz++) );
  216. printf( end_z, pOpts->pzPROGNAME, pOD->pz_NAME,
  217. pAL->useCt - ct );
  218. }
  219. }
  220. /*
  221. * If the argument has been disabled,
  222. * Then set its value to the disablement string
  223. */
  224. else if ((pOD->fOptState & OPTST_DISABLED) != 0)
  225. printf( zOptDisabl, pOpts->pzPROGNAME, pOD->pz_NAME,
  226. (pOD->pz_DisablePfx != NULL)
  227. ? pOD->pz_DisablePfx : "false" );
  228. /*
  229. * If the argument type is numeric, the last arg pointer
  230. * is really the VALUE of the string that was pointed to.
  231. */
  232. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NUMERIC)
  233. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  234. (int)pOD->optArg.argInt );
  235. /*
  236. * If the argument type is an enumeration, then it is much
  237. * like a text value, except we call the callback function
  238. * to emit the value corresponding to the "optArg" number.
  239. */
  240. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_ENUMERATION) {
  241. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  242. fputc( '\'', stdout );
  243. (*(pOD->pOptProc))( (tOptions*)1UL, pOD );
  244. fputc( '\'', stdout );
  245. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  246. }
  247. /*
  248. * If the argument type is numeric, the last arg pointer
  249. * is really the VALUE of the string that was pointed to.
  250. */
  251. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_BOOLEAN)
  252. printf( zFullOptFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  253. (pOD->optArg.argBool == 0) ? "false" : "true" );
  254. /*
  255. * IF the option has an empty value,
  256. * THEN we set the argument to the occurrence count.
  257. */
  258. else if ( (pOD->optArg.argString == NULL)
  259. || (pOD->optArg.argString[0] == NUL) )
  260. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  261. pOD->optOccCt );
  262. /*
  263. * This option has a text value
  264. */
  265. else {
  266. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  267. putQuotedStr( pOD->optArg.argString );
  268. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  269. }
  270. } while (++optIx < pOpts->presetOptCt );
  271. if ( ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
  272. && (pOpts->curOptIdx < pOpts->origArgCt)) {
  273. fputs( "set --", stdout );
  274. for (optIx = pOpts->curOptIdx; optIx < pOpts->origArgCt; optIx++) {
  275. char* pzArg = pOpts->origArgVect[ optIx ];
  276. if (strchr( pzArg, '\'' ) == NULL)
  277. printf( " '%s'", pzArg );
  278. else {
  279. fputs( " '", stdout );
  280. for (;;) {
  281. char ch = *(pzArg++);
  282. switch (ch) {
  283. case '\'': fputs( "'\\''", stdout ); break;
  284. case NUL: goto arg_done;
  285. default: fputc( ch, stdout ); break;
  286. }
  287. } arg_done:;
  288. fputc( '\'', stdout );
  289. }
  290. }
  291. fputs( "\nOPTION_CT=0\n", stdout );
  292. }
  293. }
  294. /*
  295. * Local Variables:
  296. * mode: C
  297. * c-file-style: "stroustrup"
  298. * indent-tabs-mode: nil
  299. * End:
  300. * end of autoopts/putshell.c */