123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- static void
- fixupSavedOptionArgs(tOptions * pOpts)
- {
- tOptions * p = pOpts->pSavedState;
- tOptDesc * pOD = pOpts->pOptDesc;
- int ct = pOpts->optCt;
-
- for (; ct-- > 0; pOD++) {
- switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
- case OPARG_TYPE_STRING:
- if (pOD->fOptState & OPTST_STACKED) {
- tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
- q->optCookie = NULL;
- }
- if (pOD->fOptState & OPTST_ALLOC_ARG) {
- tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
- AGDUPSTR(q->optArg.argString, pOD->optArg.argString, "arg");
- }
- break;
- case OPARG_TYPE_HIERARCHY:
- {
- tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
- q->optCookie = NULL;
- }
- }
- }
- }
- void
- optionSaveState(tOptions * pOpts)
- {
- tOptions * p = (tOptions *)pOpts->pSavedState;
- if (p == NULL) {
- size_t sz = sizeof(*pOpts)
- + ((size_t)pOpts->optCt * sizeof(tOptDesc));
- p = AGALOC(sz, "saved option state");
- pOpts->pSavedState = p;
- }
- memcpy(p, pOpts, sizeof(*p));
- memcpy(p + 1, pOpts->pOptDesc, (size_t)p->optCt * sizeof(tOptDesc));
- fixupSavedOptionArgs(pOpts);
- }
- void
- optionRestore(tOptions * pOpts)
- {
- tOptions * p = (tOptions *)pOpts->pSavedState;
- if (p == NULL) {
- char const * pzName = pOpts->pzProgName;
- if (pzName == NULL) {
- pzName = pOpts->pzPROGNAME;
- if (pzName == NULL)
- pzName = zNil;
- }
- fprintf(stderr, zNoState, pzName);
- option_exits(EXIT_FAILURE);
- }
- pOpts->pSavedState = NULL;
- optionFree(pOpts);
- memcpy(pOpts, p, sizeof(*p));
- memcpy(pOpts->pOptDesc, p+1, (size_t)p->optCt * sizeof(tOptDesc));
- pOpts->pSavedState = p;
- fixupSavedOptionArgs(pOpts);
- }
- void
- optionFree(tOptions * pOpts)
- {
- free_saved_state:
- {
- tOptDesc * p = pOpts->pOptDesc;
- int ct = pOpts->optCt;
- do {
- if (p->fOptState & OPTST_ALLOC_ARG) {
- AGFREE(p->optArg.argString);
- p->optArg.argString = NULL;
- p->fOptState &= ~OPTST_ALLOC_ARG;
- }
- switch (OPTST_GET_ARGTYPE(p->fOptState)) {
- case OPARG_TYPE_STRING:
- #ifdef WITH_LIBREGEX
- if ( (p->fOptState & OPTST_STACKED)
- && (p->optCookie != NULL)) {
- p->optArg.argString = ".*";
- optionUnstackArg(pOpts, p);
- }
- #else
- ;
- #endif
- break;
- case OPARG_TYPE_HIERARCHY:
- if (p->optCookie != NULL)
- unload_arg_list(p->optCookie);
- break;
- }
- p->optCookie = NULL;
- } while (p++, --ct > 0);
- }
- if (pOpts->pSavedState != NULL) {
- tOptions * p = (tOptions *)pOpts->pSavedState;
- memcpy(pOpts, p, sizeof(*p));
- memcpy(pOpts->pOptDesc, p+1, (size_t)p->optCt * sizeof(tOptDesc));
- AGFREE(pOpts->pSavedState);
- pOpts->pSavedState = NULL;
- goto free_saved_state;
- }
- }
|