5 * Id: 5d14243d5d32d234f05bc8a20b1a6464716b30aa
6 * Time-stamp: "2008-07-27 12:14:38 bkorb"
8 * This module will interpret the options set in the tOptions
9 * structure and print them to standard out in a fashion that
10 * will allow them to be interpreted by the Bourne or Korn shells.
12 * This file is part of AutoOpts, a companion to AutoGen.
13 * AutoOpts is free software.
14 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
16 * AutoOpts is available under any one of two licenses. The license
17 * in use must be one of these two and the choice is under the control
18 * of the user of the license.
20 * The GNU Lesser General Public License, version 3 or later
21 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
23 * The Modified Berkeley Software Distribution License
24 * See the file "COPYING.mbsd"
26 * These files have the following md5sums:
28 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
29 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
30 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
33 /* = = = START-STATIC-FORWARD = = = */
34 /* static forward declarations maintained by mk-fwd */
36 putQuotedStr( tCC
* pzStr
);
37 /* = = = END-STATIC-FORWARD = = = */
40 * Make sure embedded single quotes come out okay. The initial quote has
41 * been emitted and the closing quote will be upon return.
44 putQuotedStr( tCC
* pzStr
)
47 * Handle empty strings to make the rest of the logic simpler.
49 if ((pzStr
== NULL
) || (*pzStr
== NUL
)) {
50 fputs( "''", stdout
);
55 * Emit any single quotes/apostrophes at the start of the string and
56 * bail if that is all we need to do.
58 while (*pzStr
== '\'') {
59 fputs( "\\'", stdout
);
66 * Start the single quote string
68 fputc( '\'', stdout
);
70 tCC
* pz
= strchr( pzStr
, '\'' );
75 * Emit the string up to the single quote (apostrophe) we just found.
77 (void)fwrite( pzStr
, (size_t)(pz
- pzStr
), (size_t)1, stdout
);
78 fputc( '\'', stdout
);
82 * Emit an escaped apostrophe for every one we find.
83 * If that ends the string, do not re-open the single quotes.
85 while (*++pzStr
== '\'') fputs( "\\'", stdout
);
89 fputc( '\'', stdout
);
93 * If we broke out of the loop, we must still emit the remaining text
94 * and then close the single quote string.
96 fputs( pzStr
, stdout
);
97 fputc( '\'', stdout
);
101 /*=export_func optionPutShell
102 * what: write a portable shell script to parse options
104 * arg: tOptions*, pOpts, the program options descriptor
105 * doc: This routine will emit portable shell script text for parsing
106 * the options described in the option definitions.
109 optionPutShell( tOptions
* pOpts
)
112 tSCC zOptCtFmt
[] = "OPTION_CT=%d\nexport OPTION_CT\n";
113 tSCC zOptNumFmt
[] = "%1$s_%2$s=%3$d # 0x%3$X\nexport %1$s_%2$s\n";
114 tSCC zOptDisabl
[] = "%1$s_%2$s=%3$s\nexport %1$s_%2$s\n";
115 tSCC zOptValFmt
[] = "%s_%s=";
116 tSCC zOptEnd
[] = "\nexport %s_%s\n";
117 tSCC zFullOptFmt
[]= "%1$s_%2$s='%3$s'\nexport %1$s_%2$s\n";
118 tSCC zEquivMode
[] = "%1$s_%2$s_MODE='%3$s'\nexport %1$s_%2$s_MODE\n";
120 printf( zOptCtFmt
, pOpts
->curOptIdx
-1 );
123 tOptDesc
* pOD
= pOpts
->pOptDesc
+ optIx
;
129 * Equivalence classes are hard to deal with. Where the
130 * option data wind up kind of squishes around. For the purposes
131 * of emitting shell state, they are not recommended, but we'll
132 * do something. I guess we'll emit the equivalenced-to option
133 * at the point in time when the base option is found.
135 if (pOD
->optEquivIndex
!= NO_EQUIVALENT
)
136 continue; /* equivalence to a different option */
139 * Equivalenced to a different option. Process the current option
140 * as the equivalenced-to option. Keep the persistent state bits,
141 * but copy over the set-state bits.
143 if (pOD
->optActualIndex
!= optIx
) {
144 tOptDesc
* p
= pOpts
->pOptDesc
+ pOD
->optActualIndex
;
145 p
->optArg
= pOD
->optArg
;
146 p
->fOptState
&= OPTST_PERSISTENT_MASK
;
147 p
->fOptState
|= pOD
->fOptState
& ~OPTST_PERSISTENT_MASK
;
148 printf( zEquivMode
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
, p
->pz_NAME
);
153 * If the argument type is a set membership bitmask, then we always
154 * emit the thing. We do this because it will always have some sort
155 * of bitmask value and we need to emit the bit values.
157 if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_MEMBERSHIP
) {
160 printf( zOptNumFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
161 (int)(uintptr_t)(pOD
->optCookie
) );
162 pOD
->optCookie
= (void*)(uintptr_t)~0UL;
163 (*(pOD
->pOptProc
))(OPTPROC_RETURN_VALNAME
, pOD
);
166 * We are building the typeset list. The list returned starts with
167 * 'none + ' for use by option saving stuff. We must ignore that.
169 pz
= pOD
->optArg
.argString
+ 7;
171 printf( "typeset -x -i %s_", pOD
->pz_NAME
);
172 while (IS_PLUS_N_SPACE_CHAR(*pz
)) pz
++;
176 if (IS_LOWER_CASE_CHAR(ch
)) fputc(toupper(ch
), stdout
);
177 else if (IS_UPPER_CASE_CHAR(ch
)) fputc(ch
, stdout
);
178 else if (IS_PLUS_N_SPACE_CHAR(ch
)) goto name_done
;
179 else if (ch
== NUL
) { pz
--; goto name_done
; }
180 else fputc( '_', stdout
);
182 printf( "=%1$lu # 0x%1$lX\n", (unsigned long)val
);
186 AGFREE(pOD
->optArg
.argString
);
187 pOD
->optArg
.argString
= NULL
;
188 pOD
->fOptState
&= ~OPTST_ALLOC_ARG
;
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.
198 if (UNUSED_OPT( pOD
) && DISABLED_OPT( pOD
))
202 * Handle stacked arguments
204 if ( (pOD
->fOptState
& OPTST_STACKED
)
205 && (pOD
->optCookie
!= NULL
) ) {
206 tSCC zOptCookieCt
[] = "%1$s_%2$s_CT=%3$d\nexport %1$s_%2$s_CT\n";
208 tArgList
* pAL
= (tArgList
*)pOD
->optCookie
;
209 tCC
** ppz
= pAL
->apzArgs
;
212 printf( zOptCookieCt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
, ct
);
215 tSCC numarg_z
[] = "%s_%s_%d=";
216 tSCC end_z
[] = "\nexport %s_%s_%d\n";
218 printf( numarg_z
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
220 putQuotedStr( *(ppz
++) );
221 printf( end_z
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
227 * If the argument has been disabled,
228 * Then set its value to the disablement string
230 else if ((pOD
->fOptState
& OPTST_DISABLED
) != 0)
231 printf( zOptDisabl
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
232 (pOD
->pz_DisablePfx
!= NULL
)
233 ? pOD
->pz_DisablePfx
: "false" );
236 * If the argument type is numeric, the last arg pointer
237 * is really the VALUE of the string that was pointed to.
239 else if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_NUMERIC
)
240 printf( zOptNumFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
241 (int)pOD
->optArg
.argInt
);
244 * If the argument type is an enumeration, then it is much
245 * like a text value, except we call the callback function
246 * to emit the value corresponding to the "optArg" number.
248 else if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_ENUMERATION
) {
249 uintptr_t e_val
= pOD
->optArg
.argEnum
;
250 printf( zOptValFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
253 * Convert value to string, print that and restore numeric value.
255 (*(pOD
->pOptProc
))(OPTPROC_RETURN_VALNAME
, pOD
);
256 printf("'%s'", pOD
->optArg
.argString
);
257 if (pOD
->fOptState
& OPTST_ALLOC_ARG
)
258 AGFREE(pOD
->optArg
.argString
);
259 pOD
->optArg
.argEnum
= e_val
;
261 printf(zOptEnd
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
265 * If the argument type is numeric, the last arg pointer
266 * is really the VALUE of the string that was pointed to.
268 else if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_BOOLEAN
)
269 printf( zFullOptFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
270 (pOD
->optArg
.argBool
== 0) ? "false" : "true" );
273 * IF the option has an empty value,
274 * THEN we set the argument to the occurrence count.
276 else if ( (pOD
->optArg
.argString
== NULL
)
277 || (pOD
->optArg
.argString
[0] == NUL
) )
279 printf( zOptNumFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
283 * This option has a text value
286 printf( zOptValFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
287 putQuotedStr( pOD
->optArg
.argString
);
288 printf( zOptEnd
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
290 } while (++optIx
< pOpts
->presetOptCt
);
292 if ( ((pOpts
->fOptSet
& OPTPROC_REORDER
) != 0)
293 && (pOpts
->curOptIdx
< pOpts
->origArgCt
)) {
294 fputs( "set --", stdout
);
295 for (optIx
= pOpts
->curOptIdx
; optIx
< pOpts
->origArgCt
; optIx
++) {
296 char* pzArg
= pOpts
->origArgVect
[ optIx
];
297 if (strchr( pzArg
, '\'' ) == NULL
)
298 printf( " '%s'", pzArg
);
300 fputs( " '", stdout
);
302 char ch
= *(pzArg
++);
304 case '\'': fputs( "'\\''", stdout
); break;
305 case NUL
: goto arg_done
;
306 default: fputc( ch
, stdout
); break;
309 fputc( '\'', stdout
);
312 fputs( "\nOPTION_CT=0\n", stdout
);
319 * c-file-style: "stroustrup"
320 * indent-tabs-mode: nil
322 * end of autoopts/putshell.c */