123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- static size_t
- string_size(char const * scan, size_t nl_len)
- {
-
- size_t res_ln = 3;
- for (;;) {
- char ch = *(scan++);
- if ((ch >= ' ') && (ch <= '~')) {
-
- res_ln += ((ch == '"') || (ch == '\\')) ? 2 : 1;
- }
-
- else switch (ch) {
- case NUL:
- return res_ln;
- case NL:
- res_ln += nl_len;
- break;
- case HT:
- case BEL:
- case BS:
- case FF:
- case CR:
- case VT:
- res_ln += 2;
- break;
- default:
- res_ln += 4;
- }
- }
- }
- char const *
- optionQuoteString(char const * text, char const * nl)
- {
- size_t nl_len = strlen(nl);
- size_t out_sz = string_size(text, nl_len);
- char * out;
- char * res = out = AGALOC(out_sz, "quot str");
- *(out++) = '"';
- for (;;) {
- unsigned char ch = (unsigned char)*text;
- if ((ch >= ' ') && (ch <= '~')) {
- if ((ch == '"') || (ch == '\\'))
-
- *(out++) = '\\';
- *(out++) = (char)ch;
- } else switch (ch) {
- # define add_esc_ch(_ch) { *(out++) = '\\'; *(out++) = (_ch); }
- case BEL: add_esc_ch('a'); break;
- case BS: add_esc_ch('b'); break;
- case HT: add_esc_ch('t'); break;
- case VT: add_esc_ch('v'); break;
- case FF: add_esc_ch('f'); break;
- case CR: add_esc_ch('r'); break;
- case LF:
-
- while (*++text == NL)
- add_esc_ch('n');
-
- if (*text != NUL) {
- memcpy(out, nl, nl_len);
- out += nl_len;
- continue;
- }
- add_esc_ch('n');
-
- case NUL:
-
- *(out++) = '"';
- *(out++) = NUL;
- #ifndef NDEBUG
- if ((size_t)(out - res) > out_sz) {
- fputs(misguess_len, stderr);
- option_exits(EXIT_FAILURE);
- }
- #endif
- return res;
- default:
-
- out += sprintf(out, MK_STR_OCT_FMT, ch);
- }
- text++;
- # undef add_esc_ch
- }
- }
- static char const *
- print_quoted_apostrophes(char const * str)
- {
- while (*str == APOSTROPHE) {
- fputs(QUOT_APOS, stdout);
- str++;
- }
- return str;
- }
- static void
- print_quot_str(char const * str)
- {
-
- if ((str == NULL) || (*str == NUL)) {
- fputs(EMPTY_ARG, stdout);
- return;
- }
-
- str = print_quoted_apostrophes(str);
- if (*str == NUL)
- return;
-
- fputc(APOSTROPHE, stdout);
- for (;;) {
- char const * pz = strchr(str, APOSTROPHE);
- if (pz == NULL)
- break;
-
- (void)fwrite(str, (size_t)(pz - str), (size_t)1, stdout);
-
- fputc(APOSTROPHE, stdout);
- str = print_quoted_apostrophes(pz);
- if (*str == NUL)
- return;
- fputc(APOSTROPHE, stdout);
- }
-
- fputs(str, stdout);
- fputc(APOSTROPHE, stdout);
- }
- static void
- print_enumeration(tOptions * pOpts, tOptDesc * pOD)
- {
- uintptr_t e_val = pOD->optArg.argEnum;
- printf(OPT_VAL_FMT, pOpts->pzPROGNAME, pOD->pz_NAME);
-
- (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD);
- printf(QUOT_ARG_FMT, pOD->optArg.argString);
- if (pOD->fOptState & OPTST_ALLOC_ARG)
- AGFREE(pOD->optArg.argString);
- pOD->optArg.argEnum = e_val;
- printf(OPT_END_FMT, pOpts->pzPROGNAME, pOD->pz_NAME);
- }
- static void
- print_membership(tOptions * pOpts, tOptDesc * pOD)
- {
- char const * svstr = pOD->optArg.argString;
- char const * pz;
- uintptr_t val = 1;
- printf(zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
- (int)(uintptr_t)(pOD->optCookie));
- pOD->optCookie = VOIDP(~0UL);
- (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD);
- pz = pOD->optArg.argString;
- while (*pz != NUL) {
- printf("readonly %s_", pOD->pz_NAME);
- pz = SPN_PLUS_N_SPACE_CHARS(pz);
- for (;;) {
- int ch = *(pz++);
- if (IS_LOWER_CASE_CHAR(ch)) fputc(toupper(ch), stdout);
- else if (IS_UPPER_CASE_CHAR(ch)) fputc(ch, stdout);
- else if (IS_PLUS_N_SPACE_CHAR(ch)) goto name_done;
- else if (ch == NUL) { pz--; goto name_done; }
- else fputc('_', stdout);
- } name_done:;
- printf(SHOW_VAL_FMT, (unsigned long)val);
- val <<= 1;
- }
- AGFREE(pOD->optArg.argString);
- pOD->optArg.argString = svstr;
- }
- static void
- print_stacked_arg(tOptions * pOpts, tOptDesc * pOD)
- {
- tArgList * pAL = (tArgList *)pOD->optCookie;
- char const ** ppz = pAL->apzArgs;
- int ct = pAL->useCt;
- printf(zOptCookieCt, pOpts->pzPROGNAME, pOD->pz_NAME, ct);
- while (--ct >= 0) {
- printf(ARG_BY_NUM_FMT, pOpts->pzPROGNAME, pOD->pz_NAME,
- pAL->useCt - ct);
- print_quot_str(*(ppz++));
- printf(EXPORT_ARG_FMT, pOpts->pzPROGNAME, pOD->pz_NAME,
- pAL->useCt - ct);
- }
- }
- static void
- print_reordering(tOptions * opts)
- {
- unsigned int ix;
- fputs(set_dash, stdout);
- for (ix = opts->curOptIdx;
- ix < opts->origArgCt;
- ix++) {
- fputc(' ', stdout);
- print_quot_str(opts->origArgVect[ ix ]);
- }
- fputs(init_optct, stdout);
- }
- void
- optionPutShell(tOptions * pOpts)
- {
- int optIx = 0;
- printf(zOptCtFmt, pOpts->curOptIdx-1);
- do {
- tOptDesc * pOD = pOpts->pOptDesc + optIx;
- if ((pOD->fOptState & OPTST_NO_OUTPUT_MASK) != 0)
- continue;
-
- if (pOD->optEquivIndex != NO_EQUIVALENT)
- continue;
-
- if (pOD->optActualIndex != optIx) {
- tOptDesc * p = pOpts->pOptDesc + pOD->optActualIndex;
- p->optArg = pOD->optArg;
- p->fOptState &= OPTST_PERSISTENT_MASK;
- p->fOptState |= pOD->fOptState & ~OPTST_PERSISTENT_MASK;
- printf(zEquivMode, pOpts->pzPROGNAME, pOD->pz_NAME, p->pz_NAME);
- pOD = p;
- }
-
- if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP) {
- print_membership(pOpts, pOD);
- continue;
- }
-
- if (UNUSED_OPT(pOD) && DISABLED_OPT(pOD))
- continue;
-
- if ( (pOD->fOptState & OPTST_STACKED)
- && (pOD->optCookie != NULL) ) {
- print_stacked_arg(pOpts, pOD);
- continue;
- }
-
- if ((pOD->fOptState & OPTST_DISABLED) != 0) {
- printf(zOptDisabl, pOpts->pzPROGNAME, pOD->pz_NAME,
- (pOD->pz_DisablePfx != NULL)
- ? pOD->pz_DisablePfx : "false");
- continue;
- }
-
- if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NUMERIC) {
- printf(zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
- (int)pOD->optArg.argInt);
- continue;
- }
-
- if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_ENUMERATION) {
- print_enumeration(pOpts, pOD);
- continue;
- }
-
- if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_BOOLEAN) {
- printf(zFullOptFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
- (pOD->optArg.argBool == 0) ? "false" : "true");
- continue;
- }
-
- if ( (pOD->optArg.argString == NULL)
- || (pOD->optArg.argString[0] == NUL) ) {
- printf(zOptNumFmt, pOpts->pzPROGNAME, pOD->pz_NAME,
- pOD->optOccCt);
- continue;
- }
-
- printf(OPT_VAL_FMT, pOpts->pzPROGNAME, pOD->pz_NAME);
- print_quot_str(pOD->optArg.argString);
- printf(OPT_END_FMT, pOpts->pzPROGNAME, pOD->pz_NAME);
- } while (++optIx < pOpts->presetOptCt );
- if ( ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
- && (pOpts->curOptIdx < pOpts->origArgCt))
- print_reordering(pOpts);
- fflush(stdout);
- }
|