/*================================================+ * evaluate_cond -- Evaluate conditional expression *===============================================*/ BOOLEAN evaluate_cond (PNODE node, SYMTAB stab, BOOLEAN *eflg) { PVALUE val; BOOLEAN rc; PNODE var = node, expr = inext(node); if (!expr) { expr = var; var = NULL; } if (var && !iistype(var, IIDENT)) { *eflg = TRUE; prog_error(node, "1st arg in conditional must be variable"); return FALSE; } val = evaluate(expr, stab, eflg); if (*eflg || !val) { *eflg = TRUE; prog_error(node, "error in conditional expression"); return FALSE; } #ifdef DEBUG llwprintf("interp_if: cond = "); show_pvalue(val); wprintf("\n"); #endif if (var) assign_iden(stab, iident(node), copy_pvalue(val)); coerce_pvalue(PBOOL, val, eflg); rc = pvalue_to_bool(val); delete_pvalue(val); return rc; }
/*====================================+ * evaluate_iden -- Evaluate identifier * makes & returns copy *===================================*/ PVALUE evaluate_iden (PNODE node, SYMTAB stab, BOOLEAN *eflg) { STRING iden = (STRING) iident(node); if (prog_trace) trace_outl("evaluate_iden called: iden = %s", iden); *eflg = FALSE; return valueof_iden(node, stab, iden, eflg); }
void filesub(char **namptr, int assign) { char *sub = NULL, *str, *ptr; int len; filesubstr(namptr, assign); if (!assign) return; if (assign < 3) if ((*namptr)[1] && (sub = strchr(*namptr + 1, Equals))) { if (assign == 1) for (ptr = *namptr; ptr != sub; ptr++) if (!iident(*ptr) && !INULL(*ptr)) return; str = sub + 1; if ((sub[1] == Tilde || sub[1] == Equals) && filesubstr(&str, assign)) { sub[1] = '\0'; *namptr = dyncat(*namptr, str); } } else return; ptr = *namptr; while ((sub = strchr(ptr, ':'))) { str = sub + 1; len = sub - *namptr; if ((sub[1] == Tilde || sub[1] == Equals) && filesubstr(&str, assign)) { sub[1] = '\0'; *namptr = dyncat(*namptr, str); } ptr = *namptr + len + 1; } }
/*================================================+ * evaluate_ufunc -- Evaluate user defined function * node: [in] parsed node of function definition * stab: [in] function's symbol table * eflg: [out] error flag *===============================================*/ PVALUE evaluate_ufunc (PNODE node, SYMTAB stab, BOOLEAN *eflg) { STRING procname = (STRING) iname(node); PNODE func, arg, parm; SYMTAB newstab = NULL; PVALUE val=NULL; INTERPTYPE irc; INT count=0; *eflg = TRUE; /* find func in local or global table */ func = get_proc_node(procname, irptinfo(node)->functab, gfunctab, &count); if (!func) { if (!count) prog_error(node, _("Undefined func: %s"), procname); else prog_error(node, _("Ambiguous call to func: %s"), procname); goto ufunc_leave; } newstab = create_symtab_proc(procname, stab); arg = (PNODE) iargs(node); parm = (PNODE) iargs(func); while (arg && parm) { BOOLEAN eflg=TRUE; PVALUE value = evaluate(arg, stab, &eflg); if (eflg) { if (getlloptint("FullReportCallStack", 0) > 0) prog_error(node, "In user function %s()", procname); return INTERROR; } insert_symtab(newstab, iident(parm), value); arg = inext(arg); parm = inext(parm); } if (arg || parm) { prog_error(node, "``%s'': mismatched args and params\n", procname); goto ufunc_leave; } irc = interpret((PNODE) ibody(func), newstab, &val); switch (irc) { case INTRETURN: case INTOKAY: #ifdef DEBUG llwprintf("Successful ufunc call -- val returned was "); show_pvalue(val); llwprintf("\n"); #endif *eflg = FALSE; goto ufunc_leave; case INTBREAK: case INTCONTINUE: case INTERROR: break; } if (getlloptint("FullReportCallStack", 0) > 0) prog_error(node, "In user function %s()", procname); *eflg = TRUE; delete_pvalue(val); val=NULL; ufunc_leave: if (newstab) { remove_symtab(newstab); newstab = NULL; } return val; }
int getvisrchstr(void) { char *sbuf = halloc(80); int sptr = 1, ret = 0, ssbuf = 80; int cmd; int *obindtab = bindtab; if (visrchstr) { zsfree(visrchstr); visrchstr = NULL; } clearlist = 1; statusline = sbuf; sbuf[0] = (visrchsense == -1) ? '?' : '/'; bindtab = mainbindtab; while (sptr) { sbuf[sptr] = '_'; statusll = sptr + 1; refresh(); if ((cmd = getkeycmd()) < 0 || cmd == z_sendbreak) { ret = 0; break; } if(cmd == z_magicspace) { c = ' '; cmd = z_selfinsert; } switch(cmd) { case z_redisplay: redisplay(); break; case z_clearscreen: clearscreen(); break; case z_acceptline: case z_vicmdmode: sbuf[sptr] = 0; visrchstr = metafy(sbuf + 1, sptr - 1, META_DUP); ret = 1; sptr = 0; break; case z_backwarddeletechar: case z_vibackwarddeletechar: sptr--; break; case z_backwardkillword: case z_vibackwardkillword: while(sptr != 1 && iblank(sbuf[sptr - 1])) sptr--; if(iident(sbuf[sptr - 1])) while(sptr != 1 && iident(sbuf[sptr - 1])) sptr--; else while(sptr != 1 && !iident(sbuf[sptr - 1]) && !iblank(sbuf[sptr - 1])) sptr--; break; case z_sendstring: sendstring(); break; case z_viquotedinsert: sbuf[sptr] = '^'; refresh(); /* fall through */ case z_quotedinsert: if ((c = getkey(0)) == EOF) { feep(); break; } goto ins; case z_selfinsertunmeta: c &= 0x7f; if(c == '\r') c = '\n'; case z_selfinsert: ins: if(sptr == ssbuf - 1) { char *newbuf = halloc(ssbuf *= 2); strcpy(newbuf, sbuf); statusline = sbuf = newbuf; } sbuf[sptr++] = c; break; default: feep(); } } statusline = NULL; bindtab = obindtab; return ret; }
static int gettokstr(int c, int sub) { int bct = 0, pct = 0, brct = 0, fdpar = 0; int intpos = 1, in_brace_param = 0; int peek, inquote, unmatched = 0; char endchar='"'; #ifdef DEBUG int ocmdsp = cmdsp; #endif peek = STRING; if (!sub) { len = 0; bptr = tokstr = (char *) hcalloc(bsiz = 32); } for (;;) { int act; int e; int inbl = inblank(c); if (fdpar && !inbl && c != ')') fdpar = 0; if (inbl && !in_brace_param && !pct) act = LX2_BREAK; else { act = lexact2[STOUC(c)]; c = lextok2[STOUC(c)]; } switch (act) { case LX2_BREAK: if (!in_brace_param && !sub) goto brk; break; case LX2_META: c = hgetc(); #ifdef DEBUG if (lexstop) { fputs("BUG: input terminated by Meta\n", stderr); fflush(stderr); goto brk; } #endif add(Meta); break; case LX2_OUTPAR: if (fdpar) { /* this is a single word `( )', treat as INOUTPAR */ add(c); *bptr = '\0'; return INOUTPAR; } if ((sub || in_brace_param) && isset(SHGLOB)) break; if (!in_brace_param && !pct--) { if (sub) { pct = 0; break; } else goto brk; } c = Outpar; break; case LX2_BAR: if (!pct && !in_brace_param) { if (sub) break; else goto brk; } if (unset(SHGLOB) || (!sub && !in_brace_param)) c = Bar; break; case LX2_STRING: e = hgetc(); if (e == '[') { cmdpush(CS_MATHSUBST); add(String); add(Inbrack); c = dquote_parse(']', sub); cmdpop(); if (c) { peek = LEXERR; goto brk; } c = Outbrack; } else if (e == '(') { add(String); c = cmd_or_math_sub(); if (c) { peek = LEXERR; goto brk; } c = Outpar; } else { if (e == '{') { add(c); c = Inbrace; ++bct; cmdpush(CS_BRACEPAR); if (!in_brace_param) in_brace_param = bct; } else { hungetc(e); lexstop = 0; } } break; case LX2_INBRACK: if (!in_brace_param) brct++; c = Inbrack; break; case LX2_OUTBRACK: if (!in_brace_param) brct--; if (brct < 0) brct = 0; c = Outbrack; break; case LX2_INPAR: if (isset(SHGLOB)) { if (sub || in_brace_param) break; if (incasepat && !len) return INPAR; } if (!in_brace_param) { if (!sub) { e = hgetc(); hungetc(e); lexstop = 0; /* For command words, parentheses are only * special at the start. But now we're tokenising * the remaining string. So I don't see what * the old incmdpos test here is for. * pws 1999/6/8 * * Oh, no. * func1( ) * is a valid function definition in [k]sh. The best * thing we can do, without really nasty lookahead tricks, * is break if we find a blank after a parenthesis. At * least this can't happen inside braces or brackets. We * only allow this with SHGLOB (set for both sh and ksh). * * Things like `print @( |foo)' should still * work, because [k]sh don't allow multiple words * in a function definition, so we only do this * in command position. * pws 1999/6/14 */ if (e == ')' || (isset(SHGLOB) && inblank(e) && !bct && !brct && !intpos && incmdpos)) goto brk; } /* * This also handles the [k]sh `foo( )' function definition. * Maintain a variable fdpar, set as long as a single set of * parentheses contains only space. Then if we get to the * closing parenthesis and it is still set, we can assume we * have a function definition. Only do this at the start of * the word, since the (...) must be a separate token. */ if (!pct++ && isset(SHGLOB) && intpos && !bct && !brct) fdpar = 1; } c = Inpar; break; case LX2_INBRACE: if (isset(IGNOREBRACES) || sub) c = '{'; else { if (!len && incmdpos) { add('{'); *bptr = '\0'; return STRING; } if (in_brace_param) { cmdpush(CS_BRACE); } bct++; } break; case LX2_OUTBRACE: if ((isset(IGNOREBRACES) || sub) && !in_brace_param) break; if (!bct) break; if (in_brace_param) { cmdpop(); } if (bct-- == in_brace_param) in_brace_param = 0; c = Outbrace; break; case LX2_COMMA: if (unset(IGNOREBRACES) && !sub && bct > in_brace_param) c = Comma; break; case LX2_OUTANG: if (!intpos) { if (in_brace_param || sub) break; else goto brk; } e = hgetc(); if (e != '(') { hungetc(e); lexstop = 0; goto brk; } add(Outang); if (skipcomm()) { peek = LEXERR; goto brk; } c = Outpar; break; case LX2_INANG: if (isset(SHGLOB) && sub) break; e = hgetc(); if(e == '(' && intpos) { add(Inang); if (skipcomm()) { peek = LEXERR; goto brk; } c = Outpar; break; } hungetc(e); if(isnumglob()) { add(Inang); while ((c = hgetc()) != '>') add(c); c = Outang; break; } lexstop = 0; if (in_brace_param || sub) break; goto brk; case LX2_EQUALS: if (intpos) { e = hgetc(); if (e != '(') { hungetc(e); lexstop = 0; c = Equals; } else { add(Equals); if (skipcomm()) { peek = LEXERR; goto brk; } c = Outpar; } } else if (!sub && peek != ENVSTRING && incmdpos && !bct && !brct) { char *t = tokstr; if (idigit(*t)) while (++t < bptr && idigit(*t)); else { while (iident(*t) && ++t < bptr); if (t < bptr) { *bptr = '\0'; skipparens(Inbrack, Outbrack, &t); } } if (*t == '+') t++; if (t == bptr) { e = hgetc(); if (e == '(' && incmdpos) { *bptr = '\0'; return ENVARRAY; } hungetc(e); lexstop = 0; peek = ENVSTRING; intpos = 2; } else c = Equals; } else c = Equals; break; #ifndef __SYMBIAN32__ case LX2_BKSLASH: c = hgetc(); if (c == '\n') { c = hgetc(); if (!lexstop) continue; } else add(Bnull); if (lexstop) goto brk; break; #endif case LX2_QUOTE: { int strquote = (len && bptr[-1] == String); add(Snull); cmdpush(CS_QUOTE); for (;;) { STOPHIST while ((c = hgetc()) != '\'' && !lexstop) { if (strquote && c == '\\') { add(c); c = hgetc(); if (lexstop) break; } else if (!sub && isset(CSHJUNKIEQUOTES) && c == '\n') { if (bptr[-1] == '\\') bptr--, len--; else break; } add(c); } ALLOWHIST if (c != '\'') { unmatched = '\''; peek = LEXERR; cmdpop(); goto brk; } e = hgetc(); if (e != '\'' || unset(RCQUOTES) || strquote) break; add(c); } cmdpop(); hungetc(e); lexstop = 0; c = Snull; break; } case LX2_DQUOTE: add(Dnull); cmdpush(CS_DQUOTE); c = dquote_parse('"', sub); cmdpop(); if (c) { unmatched = '"'; peek = LEXERR; goto brk; } c = Dnull; break; case LX2_BQUOTE: add(Tick); cmdpush(CS_BQUOTE); SETPARBEGIN inquote = 0; while ((c = hgetc()) != '`' && !lexstop) { if (c == '\\') { c = hgetc(); if (c != '\n') { add(c == '`' || c == '\\' || c == '$' ? Bnull : '\\'); add(c); } else if (!sub && isset(CSHJUNKIEQUOTES)) add(c); } else { if (!sub && isset(CSHJUNKIEQUOTES) && c == '\n') { break; } add(c); if (c == '\'') { if ((inquote = !inquote)) STOPHIST else ALLOWHIST } } } if (inquote) ALLOWHIST cmdpop(); if (c != '`') { unmatched = '`'; peek = LEXERR; goto brk; } c = Tick; SETPAREND break; } #ifdef __SYMBIAN32__ if(c=='\\') { c = hgetc(); if (c != '\n') { if (c == endchar) add(Bnull); else { /* lexstop is implicitly handled here */ add('\\'); } } } #endif add(c); c = hgetc(); if (intpos) intpos--; if (lexstop) break; } brk: hungetc(c); if (unmatched) zerr("unmatched %c", NULL, unmatched); if (in_brace_param) { while(bct-- >= in_brace_param) cmdpop(); zerr("closing brace expected", NULL, 0); } else if (unset(IGNOREBRACES) && !sub && len > 1 && peek == STRING && bptr[-1] == '}' && bptr[-2] != Bnull) { /* hack to get {foo} command syntax work */ bptr--; len--; lexstop = 0; hungetc('}'); } *bptr = '\0'; DPUTS(cmdsp != ocmdsp, "BUG: gettok: cmdstack changed."); return peek; }