/** * @file input.c * @brief The stack of input sources the parser reads from. */ #include #include #include #include #include "exec.h" #include "interact.h" #include "parser.h" #include "sys.h" #include "var.h" /** * @brief Size of the read buffer of a file or terminal input. */ #define INPUT_BUFSZ ((size_t)4096) /** * @brief The input being read: the top of the stack. */ input_src *input; /** * @brief Which prompt the next terminal read shows: 1 for PS1, 2 for PS2. */ int prompt_state; /** * @brief Push a file descriptor as the current input. * * When commands come from standard input, the commands themselves may * read standard input too, so the shell must not consume more than it has * parsed. A seekable stdin is read in blocks and the read-ahead is given * back with lseek() before each command runs. A pipe is read one byte at * a time. A terminal already delivers one line per read(). * * @param fd Descriptor to read commands from. * @param is_stdin Whether `fd` is shared with the commands being run. */ void input_push_fd(int fd, bool is_stdin) { input_src *in = xmalloc(sizeof *in); *in = (input_src){.prev = input, .fd = fd, .lineno = 1}; in->buf = xmalloc(INPUT_BUFSZ); in->p = in->end = in->buf; /* * z/OS: read a script in whatever character set it is in. Not for * stdin, whose conversion state the commands being run would share. */ if (SYS_MVS && !is_stdin) sys_input_cvt(fd); if (is_stdin) { if (isatty(fd)) in->tty = true; /* first: a z/OS terminal also answers lseek() */ else if (lseek(fd, 0, SEEK_CUR) != (off_t)-1) in->seekable = true; else in->bytewise = true; } input = in; } /** * @brief Push a string as the current input. * @param s The text. It must outlive the input. * @param len Its length. */ void input_push_string(const char *s, size_t len) { input_src *in = xmalloc(sizeof *in); *in = (input_src){.prev = input, .fd = -1}; in->lineno = input ? input->lineno : 1; in->p = s; in->end = s + len; input = in; } /** * @brief Push the text of an alias as input. * * The input removes itself when it runs out (see input_fill()), so the * lexer carries straight on with whatever followed the alias name. * * @param a The alias. Marked in use until its text has been read. */ void input_push_alias(alias *a) { input_push_string(a->value, strlen(a->value)); input->autopop = true; input->alias = a; a->in_use = true; } /** * @brief Push text back so that the lexer reads it again. * * Used when a construct turns out not to be what it looked like. Line * counting done while re-reading carries over to the input underneath. * * @param s Text, which must outlive the parse (parse-arena memory). * @param len Length of `s`. */ void input_push_back(const char *s, size_t len) { input_push_string(s, len); input->autopop = true; } /** * @brief Remove the current input, closing a script file the shell opened. */ void input_pop(void) { input_src *in = input; input = in->prev; if (in->alias) in->alias->in_use = false; else if (in->autopop && input) input->lineno = in->lineno; if (in->fd > 2) /* a script or dot file we opened */ close(in->fd); free(in->buf); free(in); } /** * @brief Pop inputs until `to` is current again (after an error). */ void input_unwind(input_src *to) { while (input != to) input_pop(); } /** * @brief Refill the current input. Called by pgetc() when its buffer is empty. * @return The next character, or PEOF at end of input. */ int input_fill(void) { input_src *in = input; if (in->autopop) { /* Alias or pushed-back text has run out: carry on underneath. */ if (in->alias) { size_t n = strlen(in->alias->value); char last = n ? in->alias->value[n - 1] : '\0'; alias_ended_blank = last == ' ' || last == '\t'; } input_pop(); return pgetc(); } if (in->fd < 0 || in->eof) return PEOF; if (in->tty && opt.interactive) { /* The one branch scripts share with the interactive layer. */ if (!interact_read(in)) { in->eof = true; return PEOF; } if (opt.verbose) { out_write(&out2, in->buf, (size_t)(in->end - in->buf)); out_flush(&out2); } return (unsigned char)*in->p++; } ssize_t n; while ((n = read(in->fd, in->buf, in->bytewise ? 1 : INPUT_BUFSZ)) < 0) { if (errno != EINTR) break; trap_poll(); } if (n <= 0) { in->eof = true; return PEOF; } if (opt.verbose) { out_write(&out2, in->buf, (size_t)n); out_flush(&out2); } in->p = in->buf; in->end = in->buf + n; return (unsigned char)*in->p++; } /** * @brief Give unparsed read-ahead back to a seekable stdin, so that the command * about to run reads from where the parser stopped. */ void input_sync(void) { input_src *in = input; if (in->seekable && in->p < in->end) { if (lseek(in->fd, -(off_t)(in->end - in->p), SEEK_CUR) != (off_t)-1) in->end = in->p; } } /** * @brief After an error at the prompt, drop the rest of the typed line. */ void input_discard_line(void) { if (input->tty) input->p = input->end; } /** * @brief Is nothing but blanks left in the string being read? * * Used to spot the last command of `sh -c`, which may replace the shell * with exec. */ bool input_at_end(void) { for (const input_src *in = input; in; in = in->prev) { if (in->fd >= 0) return false; for (const char *p = in->p; p < in->end; p++) { if (*p != ' ' && *p != '\t' && *p != '\n') return false; } if (!in->autopop) break; } return true; }