static_fn int sh_outenum(Shell_t *shp, Sfio_t *iop, Namval_t *tp) { Namval_t *mp; Dt_t *dp = NULL; struct Enum *ep; int i; if (!tp) { mp = nv_open(NV_CLASS, shp->var_tree, NV_NOADD | NV_VARNAME); if (!mp) return 0; dp = nv_dict(mp); tp = (Namval_t *)dtfirst(dp); } while (tp) { if (!tp->nvfun) continue; ep = (struct Enum *)nv_hasdisc(tp, &ENUM_disc); if (!ep) continue; sfprintf(iop, "enum %s%s=(\n", (ep->iflag ? "-i " : ""), tp->nvname); for (i = 0; i < ep->nelem; i++) sfprintf(iop, "\t%s\n", ep->values[i]); sfprintf(iop, ")\n"); if (!dp) break; tp = (Namval_t *)dtnext(dp, tp); } return 0; }
static int sh_outenum(Shell_t *shp, Sfio_t *iop, Namval_t *tp) { Namval_t *mp; Dt_t *dp=0; char nvtype[sizeof(NV_CLASS)]; struct Enum *ep; int i; if(!tp) { strcpy(nvtype,NV_CLASS); if(!(mp = nv_open(nvtype, shp->var_tree,NV_NOADD|NV_VARNAME))) return(0); dp = nv_dict(mp); tp = (Namval_t*)dtfirst(dp); } while(tp) { if(!tp->nvfun || !(ep=(struct Enum *)nv_hasdisc(tp,&ENUM_disc))) continue; sfprintf(iop,"enum %s%s=(\n",(ep->iflag?"-i ":""),tp->nvname); for(i=0; i <ep->nelem ; i++) sfprintf(iop,"\t%s\n",ep->values[i]); sfprintf(iop,")\n"); if(!dp) break; tp = (Namval_t*)dtnext(dp,tp); } return(0); }
void *nv_diropen(Namval_t *np,const char *name) { char *next,*last; int c,len=strlen(name); struct nvdir *save, *dp = new_of(struct nvdir,len+1); Namval_t *nq=0,fake; Namfun_t *nfp=0; if(!dp) return(0); memset((void*)dp, 0, sizeof(*dp)); dp->data = (char*)(dp+1); if(name[len-1]=='*' || name[len-1]=='@') len -= 1; name = memcpy(dp->data,name,len); dp->data[len] = 0; dp->len = len; dp->root = sh.last_root?sh.last_root:sh.var_tree; #if 1 while(1) { dp->table = sh.last_table; sh.last_table = 0; if(*(last=(char*)name)==0) break; if(!(next=nextdot(last))) break; *next = 0; np = nv_open(name, dp->root, NV_NOFAIL); *next = '.'; if(!np || !nv_istable(np)) break; dp->root = nv_dict(np); name = next+1; } #else dp->table = sh.last_table; sh.last_table = 0; last = dp->data; #endif if(*name) { fake.nvname = (char*)name; if(dp->hp = (Namval_t*)dtprev(dp->root,&fake)) { char *cp = nv_name(dp->hp); c = strlen(cp); if(memcmp(name,cp,c) || name[c]!='[') dp->hp = (Namval_t*)dtnext(dp->root,dp->hp); else { np = dp->hp; last = 0; } } else dp->hp = (Namval_t*)dtfirst(dp->root); } else dp->hp = (Namval_t*)dtfirst(dp->root); while(1) { if(!last) next = 0; else if(next= nextdot(last)) { c = *next; *next = 0; } if(!np) { if(nfp && nfp->disc && nfp->disc->createf) { np = (*nfp->disc->createf)(nq,last,0,nfp); if(*nfp->last == '[') { nv_endsubscript(np,nfp->last,NV_NOADD); if(nq = nv_opensub(np)) np = nq; } } else np = nv_search(last,dp->root,0); } if(next) *next = c; if(np==dp->hp && !next) dp->hp = (Namval_t*)dtnext(dp->root,dp->hp); if(np && ((nfp=nextdisc(np)) || nv_istable(np))) { if(!(save = new_of(struct nvdir,0))) return(0); *save = *dp; dp->prev = save; if(nv_istable(np)) dp->root = nv_dict(np); else dp->root = (Dt_t*)np; if(nfp) { dp->nextnode = nfp->disc->nextf; dp->table = np; dp->otable = sh.last_table; dp->fun = nfp; dp->hp = (*dp->nextnode)(np,(Dt_t*)0,nfp); } else dp->nextnode = 0; } else break; if(!next || next[1]==0) break; last = next+1; nq = np; np = 0; }