1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/**
* @file input.c
* @brief The stack of input sources the parser reads from.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell.h"
#include "charset.h"
#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.
* An ASCII build needs it for its own scripts; an EBCDIC build reads
* IBM-1047 as it stands, and must not be handed a converted copy.
*/
if (SYS_MVS && ZASH_ASCII && !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 Is this alias's text still being read?
* @param a The alias.
* @return true while some input source is reading it.
*/
bool input_alias_active(const alias *a) {
for (const input_src *in = input; in; in = in->prev) {
if (in->alias == a)
return true;
}
return false;
}
/**
* @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. The
* text is copied: the alias itself may be redefined or removed while it
* is being read.
*
* @param a The alias.
*/
void input_push_alias(alias *a) {
size_t n = strlen(a->value);
input_push_string(a->value, n);
input_src *in = input;
/* The input owns a copy: this very command line may redefine or remove
* the alias while its text is still being read. */
in->buf = xmalloc(n + 1);
memcpy(in->buf, a->value, n + 1);
in->p = in->buf;
in->end = in->buf + n;
in->autopop = true;
in->alias = a;
}
/**
* @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->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) {
/* POSIX: a value ending in a blank lets the next word be
* expanded too. The text read decides, not the alias it came
* from, which may have been replaced by now. */
char last = in->end > in->buf ? in->end[-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) {
/* A read error is not the end of the input. At the outermost level
* nothing can recover from it, so the shell fails. */
if (!in->prev)
sh_error("cannot read input: %s", strerror(errno));
sh_warn("cannot read input: %s", strerror(errno));
}
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;
}
|