/* * Set the global timeout-time. */ int dotimeout(char *text) { char *w; int val; w = getword(&text); if (w == NULL) syntaxerr(_("(argument expected)")); if ((val = getnum(w)) == 0) syntaxerr(_("(invalid argument)")); gtimeout = val; return OK; }
/* * Increase the value of a variable. */ int doinc(char *text) { char *w; struct var *v; w = getword(&text); if (w == NULL) syntaxerr(_("(expected variable)")); v = getvar(w, 0); v->value++; return OK; }
/* * Declare a variable (integer) */ int doset(char *text) { char *w; struct var *v; w = getword(&text); if (w == NULL) syntaxerr(_("(missing var name)")); v = getvar(w, 1); if (*text) v->value = getnum(getword(&text)); return OK; }
/* * Turn verbose on/off (= echo stdin to stderr) */ int doverbose(char *text) { char *w; curenv->verbose = 1; if ((w = getword(&text)) != NULL) { if (!strcmp(w, "on")) return OK; if (!strcmp(w, "off")) { curenv->verbose = 0; return OK; } } syntaxerr(_("(unexpected argument)")); return ERR; }
/* * Call another script! */ int docall(char *text) { struct line *oldline; struct env *oldenv; int er; if (*text == 0) syntaxerr(_("(argument expected)")); if (inexpect) { fprintf(stderr, _("script \"%s\" line %d: call inside expect%s\n"), curenv->scriptname, thisline->lineno, "\r"); exit(1); } oldline = thisline; oldenv = curenv; if ((er = execscript(text)) != 0) exit(er); /* freemem(); */ thisline = oldline; curenv = oldenv; return 0; }
/* * Goto a specific label. */ int dogoto(char *text) { char *w; struct line *l; char buf[32]; int len; w = getword(&text); if (w == NULL || *text) syntaxerr(_("(in goto/gosub label)")); snprintf(buf, sizeof(buf), "%s:", w); len = strlen(buf); for (l = curenv->lines; l; l = l->next) if (!strncmp(l->line, buf, len)) break; if (l == NULL) { fprintf(stderr, _("script \"%s\" line %d: label \"%s\" not found%s\n"), curenv->scriptname, thisline->lineno, w, "\r"); exit(1); } thisline = l; /* We return break, to automatically break out of expect loops. */ return BREAK; }
/* * 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); }
/* * 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; }
/* * Read a word and advance pointer. * Also processes quoting, variable substituting, and \ escapes. */ char *getword(char **s) { unsigned int len; int f; int idx = 0; const char *t = *s; int sawesc = 0; int sawq = 0; const char *env; char envbuf[32]; if (**s == 0) return NULL; for (len = 0; ; len++) { if (sawesc && t[len]) { sawesc = 0; if (t[len] <= '7' && t[len] >= '0') { buf_wr(idx, 0); for (f = 0; f < 4 && len < bufsize() && t[len] <= '7' && t[len] >= '0'; f++) buf_wr(idx, 8 * buf_rd(idx) + t[len++] - '0'); if (buf_rd(idx) == 0) buf_wr(idx, NULL_CHARACTER); idx++; len--; continue; } switch (t[len]) { case 'r': buf_wr(idx++, '\r'); break; case 'n': buf_wr(idx++, '\n'); break; case 'b': buf_wr(idx++, '\b'); break; case 'a': buf_wr(idx++, '\a'); break; case 'f': buf_wr(idx++, '\f'); break; case 'c': buf_wr(idx++, SKIP_NEWLINE); break; default: buf_wr(idx++, t[len]); break; } sawesc = 0; continue; } if (t[len] == '\\') { sawesc = 1; continue; } if (t[len] == '"') { if (sawq == 1) { sawq = 0; len++; break; } sawq = 1; continue; } if (t[len] == '$' && t[len + 1] == '(') { for(f = len; t[f] && t[f] != ')'; f++) ; if (t[f] == ')') { strncpy(envbuf, &t[len + 2], f - len - 2); envbuf[f - len - 2] = 0; len = f; env = mygetenv(envbuf); if (env == NULL) env = ""; while (*env) buf_wr(idx++, *env++); continue; } } /* ^ prefix for control chars - jl 3.2002 */ if (sawq == 1 && t[len] == '^' && t[len + 1] != 0) { char c = toupper(t[len + 1]); if (c >= 'A' && c <= '_') { len++; buf_wr(idx++, c - 'A' + 1); continue; } } if ((!sawq && (t[len] == ' ' || t[len] == '\t')) || t[len] == 0) break; buf_wr(idx++, t[len]); } buf_wr(idx, 0); *s += len; skipspace(s); if (sawesc || sawq) syntaxerr(_("(word contains ESC or quote)")); return buf(); }
/* main: * */ int main (int argc, char **argv) { int i; char *infile = NULL; int array = 0, page = 0, newformat = 0; char *error; myself = argv[0]; if (argc<2) syntaxerr(); for (i=1; i<argc; i++) { if (!strcmp(argv[i], "-h")) { if (argc==2) usage(); else syntaxerr(); } if (!strcmp(argv[i], "-e")) { array = 1; continue; } if (!strcmp(argv[i], "-p")) { page = 1; continue; } if (!strcmp(argv[i], "-n")) { newformat = 1; continue; } if (!infile) infile = argv[i]; else syntaxerr(); } switch (i=fix(infile, array, page, newformat)) { case 0x02: error = "Error 02: no input file\n"; break; case 0x10: error = "Error 10: unable to create (%s)\n"; break; case 0x11: error = "Error 11: unable to open (%s)\n"; break; case 0x12: error = "Error 12: unable to read (%s)\n"; break; case 0x13: error = "Error 13: unable to write (%s)\n"; break; case 0x20: error = "Error 20: not enough memory\n"; break; case 0x40: error = "Error 40: invalid EXE (%s)\n"; break; case 0x41: error = "Error 41: image not para aligned (%s)\n"; break; default: error = ""; } printf(error, infile); return i; }