/* * Run the script by continously executing "thisline". */ int execscript(const char *s) { volatile int ret = OK; curenv = (struct env *)malloc(sizeof(struct env)); curenv->lines = NULL; curenv->vars = NULL; curenv->verbose = 1; curenv->scriptname = s; if (readscript(s) < 0) { freemem(); free(curenv); return ERR; } signal(SIGALRM, myclock); alarm(1); if (setjmp(curenv->ebuf) == 0) { thisline = curenv->lines; while (thisline != NULL && (ret = s_exec(thisline->line)) != ERR) thisline = thisline->next; } else ret = curenv->exstat ? ERR : 0; free(curenv); return ret; }
int builtin_setenv(t_builtin const *builtin, int callback, t_sh *sh, t_proc *p) { if (callback == BLTIN_CB_BEFORE) return (s_before(p)); if (callback == BLTIN_CB_EXEC) exit(s_exec(builtin, p, sh)); if (callback == BLTIN_CB_AFTER) return (s_after(sh, p)); return (ST_OK); }
/* * If syntax: if n1 [><=] n2 command. */ int doif(char *text) { char *w; int n1; int n2; char op; if ((w = getword(&text)) == NULL) syntaxerr("(if)"); n1 = getnum(w); if ((w = getword(&text)) == NULL) syntaxerr("(if)"); if (strcmp(w, "!=") == 0) op = '!'; else { if (*w == 0 || w[1] != 0) syntaxerr("(if)"); op = *w; } if ((w = getword(&text)) == NULL) syntaxerr("(if)"); n2 = getnum(w); if (!*text) syntaxerr(_("(expected command after if)")); if (op == '=') { if (n1 != n2) return OK; } else if (op == '!') { if (n1 == n2) return OK; } else if (op == '>') { if (n1 <= n2) return OK; } else if (op == '<') { if (n1 >= n2) return OK; } else syntaxerr(_("(unknown operator)")); return s_exec(text); }
/* * Goto a subroutine. */ int dogosub(char *text) { struct line *oldline; int ret = OK; oldline = thisline; dogoto(text); while (ret != ERR) { if ((thisline = thisline->next) == NULL) { fprintf(stderr, _("script \"%s\": no return from gosub%s\n"), curenv->scriptname, "\r"); exit(1); } ret = s_exec(thisline->line); if (ret == RETURN) { ret = OK; thisline = oldline; break; } } return ret; }
/* * Our "expect" function. */ int expect(char *text) { char *s, *w; struct line **volatile seq; struct line oneline; struct line *dflseq[2]; char *volatile toact = "exit 1"; volatile int found = 0; int f, val, c; char *action = NULL; if (inexpect) { fprintf(stderr, _("script \"%s\" line %d: nested expect%s\n"), curenv->scriptname, thisline->lineno, "\r"); exit(1); } etimeout = 120; inexpect = 1; s = getword(&text); if (!strcmp(s, "{")) { if (*text) syntaxerr(_("(garbage after {)")); thisline = thisline->next; seq = buildexpect(); } else { oneline.line = s; oneline.next = NULL; dflseq[0] = &oneline; dflseq[1] = NULL; seq = dflseq; } /* Seek a timeout command */ for (f = 0; seq[f]; f++) { if (!strncmp(seq[f]->line, "timeout", 7)) { c = seq[f]->line[7]; if (c == 0 || (c != ' ' && c != '\t')) continue; s = seq[f]->line + 7; /* seq[f] = NULL; */ skipspace(&s); w = getword(&s); if (w == NULL) syntaxerr(_("(argument expected)")); val = getnum(w); if (val == 0) syntaxerr(_("(invalid argument)")); etimeout = val; skipspace(&s); if (*s != 0) toact = s; break; } } if (sigsetjmp(ejmp, 1) != 0) { f = s_exec(toact); inexpect = 0; return f; } /* Allright. Now do the expect. */ c = OK; while (!found) { action = NULL; readchar(); for (f = 0; seq[f]; f++) { s = seq[f]->line; w = getword(&s); if (expfound(w)) { action = s; found = 1; break; } } if (action != NULL && *action) { found = 0; /* Maybe BREAK or RETURN */ if ((c = s_exec(action)) != OK) found = 1; } } inexpect = 0; etimeout = 0; return c; }