1
0

putshell.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * $Id: putshell.c,v 4.9 2006/03/25 19:24:56 bkorb Exp $
  3. * Time-stamp: "2005-12-13 10:28:47 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-2006 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. fwrite( pzStr, (pz - pzStr), 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->pzLastArg = pOD->pzLastArg;
  150. p->fOptState &= OPTST_PERSISTENT;
  151. p->fOptState |= pOD->fOptState & ~OPTST_PERSISTENT;
  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* pz;
  162. uintptr_t val = 1;
  163. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  164. (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 = (char*)pOD->pzLastArg + 7;
  172. while (*pz != NUL) {
  173. printf( "typeset -x -i %s_", pOD->pz_NAME );
  174. pz += strspn( pz, " +\t\n\f" );
  175. for (;;) {
  176. char 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$ld # 0x%1$lX\n", val );
  185. val <<= 1;
  186. }
  187. free( (void*)(pOD->pzLastArg) );
  188. continue;
  189. }
  190. /*
  191. * IF the option was either specified or it wakes up enabled,
  192. * then we will emit information. Otherwise, skip it.
  193. * The idea is that if someone defines an option to initialize
  194. * enabled, we should tell our shell script that it is enabled.
  195. */
  196. if (UNUSED_OPT( pOD ) && DISABLED_OPT( pOD ))
  197. continue;
  198. /*
  199. * Handle stacked arguments
  200. */
  201. if ( (pOD->fOptState & OPTST_STACKED)
  202. && (pOD->optCookie != NULL) ) {
  203. tSCC zOptCookieCt[] = "%1$s_%2$s_CT=%3$d\nexport %1$s_%2$s_CT\n";
  204. tArgList* pAL = (tArgList*)pOD->optCookie;
  205. tCC** ppz = pAL->apzArgs;
  206. int ct = pAL->useCt;
  207. printf( zOptCookieCt, pOpts->pzPROGNAME, pOD->pz_NAME, ct );
  208. while (--ct >= 0) {
  209. tSCC zOptNumArg[] = "%s_%s_%d=";
  210. tSCC zOptEnd[] = "\nexport %s_%s_%d\n";
  211. printf( zOptNumArg, pOpts->pzPROGNAME, pOD->pz_NAME,
  212. pAL->useCt - ct );
  213. putQuotedStr( *(ppz++) );
  214. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME,
  215. pAL->useCt - ct );
  216. }
  217. }
  218. /*
  219. * If the argument has been disabled,
  220. * Then set its value to the disablement string
  221. */
  222. else if ((pOD->fOptState & OPTST_DISABLED) != 0)
  223. printf( zOptDisabl, pOpts->pzPROGNAME, pOD->pz_NAME,
  224. (pOD->pz_DisablePfx != NULL)
  225. ? pOD->pz_DisablePfx : "false" );
  226. /*
  227. * If the argument type is numeric, the last arg pointer
  228. * is really the VALUE of the string that was pointed to.
  229. */
  230. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NUMERIC)
  231. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  232. (uintptr_t)(pOD->pzLastArg) );
  233. /*
  234. * If the argument type is an enumeration, then it is much
  235. * like a text value, except we call the callback function
  236. * to emit the value corresponding to the "pzLastArg" number.
  237. */
  238. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_ENUMERATION) {
  239. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  240. fputc( '\'', stdout );
  241. (*(pOD->pOptProc))( (tOptions*)1UL, pOD );
  242. fputc( '\'', stdout );
  243. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  244. }
  245. /*
  246. * If the argument type is numeric, the last arg pointer
  247. * is really the VALUE of the string that was pointed to.
  248. */
  249. else if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_BOOLEAN)
  250. printf( zFullOptFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  251. ((uintptr_t)(pOD->pzLastArg) == 0) ? "false" : "true" );
  252. /*
  253. * IF the option has an empty value,
  254. * THEN we set the argument to the occurrence count.
  255. */
  256. else if ( (pOD->pzLastArg == NULL)
  257. || (pOD->pzLastArg[0] == NUL) )
  258. printf( zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
  259. (int)pOD->optOccCt );
  260. /*
  261. * This option has a text value
  262. */
  263. else {
  264. printf( zOptValFmt, pOpts->pzPROGNAME, pOD->pz_NAME );
  265. putQuotedStr( pOD->pzLastArg );
  266. printf( zOptEnd, pOpts->pzPROGNAME, pOD->pz_NAME );
  267. }
  268. } while (++optIx < pOpts->presetOptCt );
  269. if ( ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
  270. && (pOpts->curOptIdx < pOpts->origArgCt)) {
  271. fputs( "set --", stdout );
  272. for (optIx = pOpts->curOptIdx; optIx < pOpts->origArgCt; optIx++) {
  273. char* pzArg = pOpts->origArgVect[ optIx ];
  274. if (strchr( pzArg, '\'' ) == NULL)
  275. printf( " '%s'", pzArg );
  276. else {
  277. fputs( " '", stdout );
  278. for (;;) {
  279. char ch = *(pzArg++);
  280. switch (ch) {
  281. case '\'': fputs( "'\\''", stdout ); break;
  282. case NUL: goto arg_done;
  283. default: fputc( ch, stdout ); break;
  284. }
  285. } arg_done:;
  286. fputc( '\'', stdout );
  287. }
  288. }
  289. fputs( "\nOPTION_CT=0\n", stdout );
  290. }
  291. }
  292. /*
  293. * Local Variables:
  294. * mode: C
  295. * c-file-style: "stroustrup"
  296. * tab-width: 4
  297. * indent-tabs-mode: nil
  298. * End:
  299. * end of autoopts/putshell.c */