mod_export char * parse_subscript(char *s, int sub) { int l = strlen(s), err; char *t; if (!*s || *s == ']') return 0; lexsave(); untokenize(t = dupstring(s)); inpush(t, 0, NULL); strinbeg(0); len = 0; bptr = tokstr = s; bsiz = l + 1; err = dquote_parse(']', sub); if (err) { err = *bptr; *bptr = 0; untokenize(s); *bptr = err; s = 0; } else s = bptr; strinend(); inpop(); DPUTS(cmdsp, "BUG: parse_subscript: cmdstack not empty."); lexrestore(); return s; }
mod_export int parsestrnoerr(char *s) { int l = strlen(s), err; lexsave(); untokenize(s); inpush(dupstring(s), 0, NULL); strinbeg(0); len = 0; bptr = tokstr = s; bsiz = l + 1; err = dquote_parse('\0', 1); *bptr = '\0'; strinend(); inpop(); DPUTS(cmdsp, "BUG: parsestr: cmdstack not empty."); lexrestore(); return err; }
mod_export int parse_subst_string(char *s) { int c, l = strlen(s), err, olen, lexstop_ret; if (!*s || !strcmp(s, nulstring)) return 0; lexsave(); untokenize(s); inpush(dupstring(s), 0, NULL); strinbeg(0); len = 0; bptr = tokstr = s; bsiz = l + 1; c = hgetc(); lexstop_ret = lexstop; c = gettokstr(c, 1); err = errflag; strinend(); inpop(); DPUTS(cmdsp, "BUG: parse_subst_string: cmdstack not empty."); olen = len; lexrestore(); errflag = err; if (c == LEXERR) { untokenize(s); return 1; } #ifdef DEBUG if (c != STRING || olen != l || errflag) { fprintf(stderr, "Oops. Bug in parse_subst_string: %s\n", olen < l ? "len < l" : errflag ? "errflag" : "c != STRING"); fflush(stderr); untokenize(s); return 1; } #endif return 0; }
mod_export int parse_subst_string(char *s) { int c, l = strlen(s), err, olen, lexstop_ret; char *ptr; if (!*s || !strcmp(s, nulstring)) return 0; lexsave(); untokenize(s); inpush(dupstring(s), 0, NULL); strinbeg(0); len = 0; bptr = tokstr = s; bsiz = l + 1; c = hgetc(); lexstop_ret = lexstop; c = gettokstr(c, 1); err = errflag; strinend(); inpop(); DPUTS(cmdsp, "BUG: parse_subst_string: cmdstack not empty."); olen = len; lexrestore(); errflag = err; if (c == LEXERR) { untokenize(s); return 1; } #ifdef DEBUG /* * Historical note: we used to check here for olen == l, but * that's not necessarily the case if we stripped an RCQUOTE. */ if (c != STRING || (errflag && !noerrs)) { fprintf(stderr, "Oops. Bug in parse_subst_string: %s\n", errflag ? "errflag" : "c != STRING"); fflush(stderr); untokenize(s); return 1; } #endif /* Check for $'...' quoting. This needs special handling. */ for (ptr = s; *ptr; ) { if (*ptr == String && ptr[1] == Snull) { char *t; int len, tlen, diff; t = getkeystring(ptr + 2, &len, GETKEYS_DOLLARS_QUOTE, NULL); len += 2; tlen = strlen(t); diff = len - tlen; /* * Yuk. * parse_subst_string() currently handles strings in-place. * That's not so easy to fix without knowing whether * additional memory should come off the heap or * otherwise. So we cheat by copying the unquoted string * into place, unless it's too long. That's not the * normal case, but I'm worried there are are pathological * cases with converting metafied multibyte strings. * If someone can prove there aren't I will be very happy. */ if (diff < 0) { DPUTS(1, "$'...' subst too long: fix get_parse_string()"); return 1; } memcpy(ptr, t, tlen); ptr += tlen; if (diff > 0) { char *dptr = ptr; char *sptr = ptr + diff; while ((*dptr++ = *sptr++)) ; } } else ptr++; } return 0; }
enum loop_return loop(int toplevel, int justonce) { Eprog prog; int err, non_empty = 0; pushheap(); if (!toplevel) lexsave(); for (;;) { freeheap(); if (stophist == 3) /* re-entry via preprompt() */ hend(NULL); hbegin(1); /* init history mech */ if (isset(SHINSTDIN)) { setblock_stdin(); if (interact && toplevel) { int hstop = stophist; stophist = 3; preprompt(); if (stophist != 3) hbegin(1); else stophist = hstop; errflag = 0; } } use_exit_printed = 0; intr(); /* interrupts on */ lexinit(); /* initialize lexical state */ if (!(prog = parse_event())) { /* if we couldn't parse a list */ hend(NULL); if ((tok == ENDINPUT && !errflag) || (tok == LEXERR && (!isset(SHINSTDIN) || !toplevel)) || justonce) break; if (exit_pending) { /* * Something down there (a ZLE function?) decided * to exit when there was stuff to clear up. * Handle that now. */ stopmsg = 1; zexit(exit_pending >> 1, 0); } if (tok == LEXERR && !lastval) lastval = 1; continue; } if (hend(prog)) { int toksav = tok; non_empty = 1; if (toplevel && (getshfunc("preexec") || paramtab->getnode(paramtab, "preexec" HOOK_SUFFIX))) { LinkList args; char *cmdstr; /* * As we're about to freeheap() or popheap() * anyway, there's no gain in using permanent * storage here. */ args = newlinklist(); addlinknode(args, "preexec"); /* If curline got dumped from the history, we don't know * what the user typed. */ if (hist_ring && curline.histnum == curhist) addlinknode(args, hist_ring->node.nam); else addlinknode(args, ""); addlinknode(args, dupstring(getjobtext(prog, NULL))); addlinknode(args, cmdstr = getpermtext(prog, NULL, 0)); callhookfunc("preexec", args, 1, NULL); /* The only permanent storage is from getpermtext() */ zsfree(cmdstr); errflag = 0; } if (stopmsg) /* unset 'you have stopped jobs' flag */ stopmsg--; execode(prog, 0, 0, toplevel ? "toplevel" : "file"); tok = toksav; if (toplevel) noexitct = 0; } if (ferror(stderr)) { zerr("write error"); clearerr(stderr); } if (subsh) /* how'd we get this far in a subshell? */ exit(lastval); if (((!interact || sourcelevel) && errflag) || retflag) break; if (isset(SINGLECOMMAND) && toplevel) { if (sigtrapped[SIGEXIT]) dotrap(SIGEXIT); exit(lastval); } if (justonce) break; }