5 * Time-stamp: "2012-03-31 13:14:18 bkorb"
7 * This module will interpret the options set in the tOptions
8 * structure and print them to standard out in a fashion that
9 * will allow them to be interpreted by the Bourne or Korn shells.
11 * This file is part of AutoOpts, a companion to AutoGen.
12 * AutoOpts is free software.
13 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
15 * AutoOpts is available under any one of two licenses. The license
16 * in use must be one of these two and the choice is under the control
17 * of the user of the license.
19 * The GNU Lesser General Public License, version 3 or later
20 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
22 * The Modified Berkeley Software Distribution License
23 * See the file "COPYING.mbsd"
25 * These files have the following md5sums:
27 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
28 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
29 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
32 /* = = = START-STATIC-FORWARD = = = */
34 print_quot_str(char const * pzStr
);
37 print_enumeration(tOptions
* pOpts
, tOptDesc
* pOD
);
40 print_membership(tOptions
* pOpts
, tOptDesc
* pOD
);
43 print_stacked_arg(tOptions
* pOpts
, tOptDesc
* pOD
);
46 print_reordering(tOptions
* pOpts
);
47 /* = = = END-STATIC-FORWARD = = = */
50 * Make sure embedded single quotes come out okay. The initial quote has
51 * been emitted and the closing quote will be upon return.
54 print_quot_str(char const * pzStr
)
57 * Handle empty strings to make the rest of the logic simpler.
59 if ((pzStr
== NULL
) || (*pzStr
== NUL
)) {
60 fputs(EMPTY_ARG
, stdout
);
65 * Emit any single quotes/apostrophes at the start of the string and
66 * bail if that is all we need to do.
68 while (*pzStr
== '\'') {
69 fputs(QUOT_APOS
, stdout
);
76 * Start the single quote string
80 char const * pz
= strchr(pzStr
, '\'');
85 * Emit the string up to the single quote (apostrophe) we just found.
87 (void)fwrite(pzStr
, (size_t)(pz
- pzStr
), (size_t)1, stdout
);
92 * Emit an escaped apostrophe for every one we find.
93 * If that ends the string, do not re-open the single quotes.
95 while (*++pzStr
== '\'') fputs("\\'", stdout
);
103 * If we broke out of the loop, we must still emit the remaining text
104 * and then close the single quote string.
106 fputs(pzStr
, stdout
);
111 print_enumeration(tOptions
* pOpts
, tOptDesc
* pOD
)
113 uintptr_t e_val
= pOD
->optArg
.argEnum
;
114 printf(OPT_VAL_FMT
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
117 * Convert value to string, print that and restore numeric value.
119 (*(pOD
->pOptProc
))(OPTPROC_RETURN_VALNAME
, pOD
);
120 printf(QUOT_ARG_FMT
, pOD
->optArg
.argString
);
121 if (pOD
->fOptState
& OPTST_ALLOC_ARG
)
122 AGFREE(pOD
->optArg
.argString
);
123 pOD
->optArg
.argEnum
= e_val
;
125 printf(OPT_END_FMT
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
129 print_membership(tOptions
* pOpts
, tOptDesc
* pOD
)
133 printf(zOptNumFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
134 (int)(uintptr_t)(pOD
->optCookie
));
135 pOD
->optCookie
= (void*)(uintptr_t)~0UL;
136 (*(pOD
->pOptProc
))(OPTPROC_RETURN_VALNAME
, pOD
);
139 * We are building the typeset list. The list returned starts with
140 * 'none + ' for use by option saving stuff. We must ignore that.
142 pz
= pOD
->optArg
.argString
+ 7;
144 printf("typeset -x -i %s_", pOD
->pz_NAME
);
145 pz
= SPN_PLUS_N_SPACE_CHARS(pz
);
149 if (IS_LOWER_CASE_CHAR(ch
)) fputc(toupper(ch
), stdout
);
150 else if (IS_UPPER_CASE_CHAR(ch
)) fputc(ch
, stdout
);
151 else if (IS_PLUS_N_SPACE_CHAR(ch
)) goto name_done
;
152 else if (ch
== NUL
) { pz
--; goto name_done
; }
153 else fputc('_', stdout
);
155 printf(SHOW_VAL_FMT
, (unsigned long)val
);
159 AGFREE(pOD
->optArg
.argString
);
160 pOD
->optArg
.argString
= NULL
;
161 pOD
->fOptState
&= ~OPTST_ALLOC_ARG
;
165 print_stacked_arg(tOptions
* pOpts
, tOptDesc
* pOD
)
167 tArgList
* pAL
= (tArgList
*)pOD
->optCookie
;
168 tCC
** ppz
= pAL
->apzArgs
;
171 printf(zOptCookieCt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
, ct
);
174 printf(ARG_BY_NUM_FMT
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
176 print_quot_str(*(ppz
++));
177 printf(EXPORT_ARG_FMT
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
183 print_reordering(tOptions
* pOpts
)
187 fputs(set_dash
, stdout
);
189 for (optIx
= pOpts
->curOptIdx
;
190 optIx
< pOpts
->origArgCt
;
193 char* pzArg
= pOpts
->origArgVect
[ optIx
];
195 if (strchr(pzArg
, '\'') == NULL
)
196 printf(arg_fmt
, pzArg
);
201 char ch
= *(pzArg
++);
203 case '\'': fputs(apostrophy
, stdout
); break;
204 case NUL
: goto arg_done
;
205 default: fputc(ch
, stdout
); break;
211 fputs(init_optct
, stdout
);
214 /*=export_func optionPutShell
215 * what: write a portable shell script to parse options
217 * arg: tOptions*, pOpts, the program options descriptor
218 * doc: This routine will emit portable shell script text for parsing
219 * the options described in the option definitions.
222 optionPutShell(tOptions
* pOpts
)
226 printf(zOptCtFmt
, pOpts
->curOptIdx
-1);
229 tOptDesc
* pOD
= pOpts
->pOptDesc
+ optIx
;
231 if ((pOD
->fOptState
& OPTST_NO_OUTPUT_MASK
) != 0)
235 * Equivalence classes are hard to deal with. Where the
236 * option data wind up kind of squishes around. For the purposes
237 * of emitting shell state, they are not recommended, but we'll
238 * do something. I guess we'll emit the equivalenced-to option
239 * at the point in time when the base option is found.
241 if (pOD
->optEquivIndex
!= NO_EQUIVALENT
)
242 continue; /* equivalence to a different option */
245 * Equivalenced to a different option. Process the current option
246 * as the equivalenced-to option. Keep the persistent state bits,
247 * but copy over the set-state bits.
249 if (pOD
->optActualIndex
!= optIx
) {
250 tOptDesc
* p
= pOpts
->pOptDesc
+ pOD
->optActualIndex
;
251 p
->optArg
= pOD
->optArg
;
252 p
->fOptState
&= OPTST_PERSISTENT_MASK
;
253 p
->fOptState
|= pOD
->fOptState
& ~OPTST_PERSISTENT_MASK
;
254 printf(zEquivMode
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
, p
->pz_NAME
);
259 * If the argument type is a set membership bitmask, then we always
260 * emit the thing. We do this because it will always have some sort
261 * of bitmask value and we need to emit the bit values.
263 if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_MEMBERSHIP
) {
264 print_membership(pOpts
, pOD
);
269 * IF the option was either specified or it wakes up enabled,
270 * then we will emit information. Otherwise, skip it.
271 * The idea is that if someone defines an option to initialize
272 * enabled, we should tell our shell script that it is enabled.
274 if (UNUSED_OPT(pOD
) && DISABLED_OPT(pOD
))
278 * Handle stacked arguments
280 if ( (pOD
->fOptState
& OPTST_STACKED
)
281 && (pOD
->optCookie
!= NULL
) ) {
282 print_stacked_arg(pOpts
, pOD
);
287 * If the argument has been disabled,
288 * Then set its value to the disablement string
290 if ((pOD
->fOptState
& OPTST_DISABLED
) != 0) {
291 printf(zOptDisabl
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
292 (pOD
->pz_DisablePfx
!= NULL
)
293 ? pOD
->pz_DisablePfx
: "false");
298 * If the argument type is numeric, the last arg pointer
299 * is really the VALUE of the string that was pointed to.
301 if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_NUMERIC
) {
302 printf(zOptNumFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
303 (int)pOD
->optArg
.argInt
);
308 * If the argument type is an enumeration, then it is much
309 * like a text value, except we call the callback function
310 * to emit the value corresponding to the "optArg" number.
312 if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_ENUMERATION
) {
313 print_enumeration(pOpts
, pOD
);
318 * If the argument type is numeric, the last arg pointer
319 * is really the VALUE of the string that was pointed to.
321 if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_BOOLEAN
) {
322 printf(zFullOptFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
323 (pOD
->optArg
.argBool
== 0) ? "false" : "true");
328 * IF the option has an empty value,
329 * THEN we set the argument to the occurrence count.
331 if ( (pOD
->optArg
.argString
== NULL
)
332 || (pOD
->optArg
.argString
[0] == NUL
) ) {
334 printf(zOptNumFmt
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
,
340 * This option has a text value
342 printf(OPT_VAL_FMT
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
343 print_quot_str(pOD
->optArg
.argString
);
344 printf(OPT_END_FMT
, pOpts
->pzPROGNAME
, pOD
->pz_NAME
);
346 } while (++optIx
< pOpts
->presetOptCt
);
348 if ( ((pOpts
->fOptSet
& OPTPROC_REORDER
) != 0)
349 && (pOpts
->curOptIdx
< pOpts
->origArgCt
))
350 print_reordering(pOpts
);
358 * c-file-style: "stroustrup"
359 * indent-tabs-mode: nil
361 * end of autoopts/putshell.c */