int main(int argc, char** argv) { if (argc == 1) { printf("USAGE: %s arg [ ... ]\n", *argv); return 1; } while (--argc > 0) { char* arg = *(++argv); token_list_t* p = ao_string_tokenize(arg); if (p == NULL) { printf("Parsing string ``%s'' failed:\n\terrno %d (%s)\n", arg, errno, strerror(errno)); } else { int ix = 0; printf("Parsed string ``%s''\ninto %d tokens:\n", arg, p->tkn_ct); do { printf(" %3d: ``%s''\n", ix+1, p->tkn_list[ix]); } while (++ix < p->tkn_ct); free(p); } } return 0; }
/* * doPrognameEnv - check for preset values from the ${PROGNAME} * environment variable. This is accomplished by parsing the text into * tokens, temporarily replacing the arg vector and calling * immediate_opts and/or regular_opts. */ LOCAL void doPrognameEnv(tOptions * pOpts, teEnvPresetType type) { char const* pczOptStr = getenv(pOpts->pzPROGNAME); token_list_t* pTL; int sv_argc; tAoUI sv_flag; char** sv_argv; /* * No such beast? Then bail now. */ if (pczOptStr == NULL) return; /* * Tokenize the string. If there's nothing of interest, we'll bail * here immediately. */ pTL = ao_string_tokenize(pczOptStr); if (pTL == NULL) return; /* * Substitute our $PROGNAME argument list for the real one */ sv_argc = pOpts->origArgCt; sv_argv = pOpts->origArgVect; sv_flag = pOpts->fOptSet; /* * We add a bogus pointer to the start of the list. The program name * has already been pulled from "argv", so it won't get dereferenced. * The option scanning code will skip the "program name" at the start * of this list of tokens, so we accommodate this way .... */ pOpts->origArgVect = (char**)(pTL->tkn_list - 1); pOpts->origArgCt = pTL->tkn_ct + 1; pOpts->fOptSet &= ~OPTPROC_ERRSTOP; pOpts->curOptIdx = 1; pOpts->pzCurOpt = NULL; switch (type) { case ENV_IMM: (void)immediate_opts(pOpts); break; case ENV_ALL: (void)immediate_opts(pOpts); pOpts->curOptIdx = 1; pOpts->pzCurOpt = NULL; /* FALLTHROUGH */ case ENV_NON_IMM: (void)regular_opts(pOpts); } /* * Free up the temporary arg vector and restore the original program args. */ free(pTL); pOpts->origArgVect = sv_argv; pOpts->origArgCt = sv_argc; pOpts->fOptSet = sv_flag; }