Commit | Line | Data |
---|---|---|
5d9efaf5 BJ |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | #include "sh.local.h" | |
3 | /* | |
4 | * C shell | |
5 | * | |
6 | * Bill Joy, UC Berkeley | |
7 | * October, 1978 | |
8 | */ | |
9 | #include <sys/types.h> | |
10 | #include <sys/stat.h> | |
11 | #include <sys/dir.h> | |
12 | ||
13 | #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR) | |
14 | ||
15 | #include <errno.h> | |
16 | #include <setjmp.h> | |
17 | #include <signal.h> | |
18 | ||
19 | typedef char bool; | |
20 | ||
21 | #define eq(a, b) (strcmp(a, b) == 0) | |
22 | ||
23 | /* | |
24 | * Global flags | |
25 | */ | |
26 | bool didcch; /* Have closed unused fd's for child */ | |
27 | bool didfds; /* Have setup i/o fd's for child */ | |
28 | bool doneinp; /* EOF indicator after reset from readc */ | |
29 | bool exiterr; /* Exit if error or non-zero exit status */ | |
30 | bool child; /* Child shell ... errors cause exit */ | |
31 | bool haderr; /* Reset was because of an error */ | |
32 | bool intty; /* Input is a tty */ | |
33 | bool intact; /* We are interactive... therefore prompt */ | |
34 | bool justpr; /* Just print because of :p hist mod */ | |
35 | bool loginsh; /* We are a loginsh -> .login/.logout */ | |
36 | bool noexec; /* Don't execute, just syntax check */ | |
37 | bool setintr; /* Set interrupts on/off -> Wait intr... */ | |
38 | bool timflg; /* Time the next waited for command */ | |
39 | ||
40 | /* | |
41 | * Global i/o info | |
42 | */ | |
43 | char *arginp; /* Argument input for sh -c and internal `xx` */ | |
44 | int onelflg; /* 2 -> need line for -t, 1 -> exit on read */ | |
45 | char *file; /* Name of shell file for $0 */ | |
46 | ||
47 | char *err; /* Error message from scanner/parser */ | |
48 | int errno; /* Error from C library routines */ | |
49 | char *shtemp; /* Temp name for << shell files in /tmp */ | |
50 | time_t time0; /* Time at which the shell started */ | |
51 | ||
52 | /* | |
53 | * Miscellany | |
54 | */ | |
55 | char *doldol; /* Character pid for $$ */ | |
56 | int uid; /* Invokers uid */ | |
57 | time_t chktim; /* Time mail last checked */ | |
58 | ||
59 | /* | |
60 | * These are declared here because they want to be | |
61 | * initialized in sh.init.c (to allow them to be made readonly) | |
62 | */ | |
63 | char *mesg[]; | |
64 | ||
65 | struct biltins { | |
66 | char *bname; | |
67 | int (*bfunct)(); | |
68 | short minargs, maxargs; | |
69 | } bfunc[]; | |
70 | ||
71 | struct srch { | |
72 | char *s_name; | |
73 | short s_value; | |
74 | } srchn[]; | |
75 | ||
76 | /* | |
77 | * To be able to redirect i/o for builtins easily, the shell moves the i/o | |
78 | * descriptors it uses away from 0,1,2. | |
79 | * Ideally these should be in units which are closed across exec's | |
80 | * (this saves work) but for version 6, this is not usually possible. | |
81 | * The desired initial values for these descriptors are defined in | |
82 | * sh.local.h. | |
83 | */ | |
84 | short SHIN; /* Current shell input (script) */ | |
85 | short SHOUT; /* Shell output */ | |
86 | short SHDIAG; /* Diagnostic output... shell errs go here */ | |
87 | short OLDSTD; /* Old standard input (def for cmds) */ | |
88 | ||
89 | /* | |
90 | * Error control | |
91 | * | |
92 | * Errors in scanning and parsing set up an error message to be printed | |
93 | * at the end and complete. Other errors always cause a reset. | |
94 | * Because of source commands and .cshrc we need nested error catches. | |
95 | */ | |
96 | ||
97 | jmp_buf reslab; | |
98 | ||
99 | #define setexit() setjmp(reslab) | |
100 | #define reset() longjmp(reslab) | |
101 | /* Should use structure assignment here */ | |
102 | #define getexit(a) copy(a, reslab, sizeof reslab) | |
103 | #define resexit(a) copy(reslab, a, sizeof reslab) | |
104 | ||
105 | char *gointr; /* Label for an onintr transfer */ | |
106 | int (*parintr)(); /* Parents interrupt catch */ | |
107 | int (*parterm)(); /* Parents terminate catch */ | |
108 | ||
109 | /* | |
110 | * Lexical definitions. | |
111 | * | |
112 | * All lexical space is allocated dynamically. | |
113 | * The eighth bit of characters is used to prevent recognition, | |
114 | * and eventually stripped. | |
115 | */ | |
116 | #define QUOTE 0200 /* Eighth char bit used internally for 'ing */ | |
117 | #define TRIM 0177 /* Mask to strip quote bit */ | |
118 | ||
119 | /* | |
120 | * Each level of input has a buffered input structure. | |
121 | * There are one or more blocks of buffered input for each level, | |
122 | * exactly one if the input is seekable and tell is available. | |
123 | * In other cases, the shell buffers enough blocks to keep all loops | |
124 | * in the buffer. | |
125 | */ | |
126 | struct Bin { | |
127 | off_t Bfseekp; /* Seek pointer */ | |
128 | off_t Bfbobp; /* Seekp of beginning of buffers */ | |
129 | off_t Bfeobp; /* Seekp of end of buffers */ | |
130 | short Bfblocks; /* Number of buffer blocks */ | |
131 | char **Bfbuf; /* The array of buffer blocks */ | |
132 | } B; | |
133 | ||
134 | #define fseekp B.Bfseekp | |
135 | #define fbobp B.Bfbobp | |
136 | #define feobp B.Bfeobp | |
137 | #define fblocks B.Bfblocks | |
138 | #define fbuf B.Bfbuf | |
139 | ||
140 | off_t btell(); | |
141 | ||
142 | /* | |
143 | * The shell finds commands in loops by reseeking the input | |
144 | * For whiles, in particular, it reseeks to the beginning of the | |
145 | * line the while was on; hence the while placement restrictions. | |
146 | */ | |
147 | off_t lineloc; | |
148 | ||
149 | #ifdef TELL | |
150 | off_t tell(); | |
151 | bool cantell; /* Is current source tellable ? */ | |
152 | #endif | |
153 | ||
154 | /* | |
155 | * Input lines are parsed into doubly linked circular | |
156 | * lists of words of the following form. | |
157 | */ | |
158 | struct wordent { | |
159 | char *word; | |
160 | struct wordent *prev; | |
161 | struct wordent *next; | |
162 | }; | |
163 | ||
164 | /* | |
165 | * During word building, both in the initial lexical phase and | |
166 | * when expanding $ variable substitutions, expansion by `!' and `$' | |
167 | * must be inhibited when reading ahead in routines which are themselves | |
168 | * processing `!' and `$' expansion or after characters such as `\' or in | |
169 | * quotations. The following flags are passed to the getC routines | |
170 | * telling them which of these substitutions are appropriate for the | |
171 | * next character to be returned. | |
172 | */ | |
173 | #define DODOL 1 | |
174 | #define DOEXCL 2 | |
175 | #define DOALL DODOL|DOEXCL | |
176 | ||
177 | /* | |
178 | * Labuf implements a general buffer for lookahead during lexical operations. | |
179 | * Text which is to be placed in the input stream can be stuck here. | |
180 | * We stick parsed ahead $ constructs during initial input, | |
181 | * process id's from `$$', and modified variable values (from qualifiers | |
182 | * during expansion in sh.dol.c) here. | |
183 | */ | |
184 | char labuf[256]; | |
185 | char *lap; | |
186 | ||
187 | /* | |
188 | * Parser structure | |
189 | * | |
190 | * Each command is parsed to a tree of command structures and | |
191 | * flags are set bottom up during this process, to be propagated down | |
192 | * as needed during the semantics/exeuction pass (sh.sem.c). | |
193 | */ | |
194 | struct command { | |
195 | short t_dtyp; /* Type of node */ | |
196 | short t_dflg; /* Flags, e.g. FAND|... */ | |
197 | union { | |
198 | char *T_dlef; /* Input redirect word */ | |
199 | struct command *T_dcar; /* Left part of list/pipe */ | |
200 | } L; | |
201 | union { | |
202 | char *T_drit; /* Output redirect word */ | |
203 | struct command *T_dcdr; /* Right part of list/pipe */ | |
204 | } R; | |
205 | #define t_dlef L.T_dlef | |
206 | #define t_dcar L.T_dcar | |
207 | #define t_drit R.T_drit | |
208 | #define t_dcdr R.T_dcdr | |
209 | char **t_dcom; /* Command/argument vector */ | |
210 | struct command *t_dspr; /* Pointer to ()'d subtree */ | |
211 | }; | |
212 | ||
213 | #define TCOM 1 /* t_dcom <t_dlef >t_drit */ | |
214 | #define TPAR 2 /* ( t_dspr ) <t_dlef >t_drit */ | |
215 | #define TFIL 3 /* t_dlef | t_drit */ | |
216 | #define TLST 4 /* t_dlef ; t_drit */ | |
217 | #define TOR 5 /* t_dlef || t_drit */ | |
218 | #define TAND 6 /* t_dlef && t_drit */ | |
219 | ||
220 | #define FAND (1<<0) /* executes in background */ | |
221 | #define FCAT (1<<1) /* output is redirected >> */ | |
222 | #define FPIN (1<<2) /* input is a pipe */ | |
223 | #define FPOU (1<<3) /* output is a pipe */ | |
224 | #define FPAR (1<<4) /* don't fork, last ()ized cmd */ | |
225 | #define FINT (1<<5) /* don't make interruptible */ | |
226 | #define FPRS (1<<6) /* print number when forked */ | |
227 | #define FDIAG (1<<7) /* redirect unit 2 with unit 1 */ | |
228 | #define FANY (1<<8) /* output was ! */ | |
229 | #define FHERE (1<<9) /* input redirection is << */ | |
230 | #define FREDO (1<<10) /* reexec aft if, repeat,... */ | |
231 | ||
232 | /* | |
233 | * The keywords for the parser | |
234 | */ | |
235 | #define ZBREAK 0 | |
236 | #define ZBRKSW 1 | |
237 | #define ZCASE 2 | |
238 | #define ZDEFAULT 3 | |
239 | #define ZELSE 4 | |
240 | #define ZEND 5 | |
241 | #define ZENDIF 6 | |
242 | #define ZENDSW 7 | |
243 | #define ZEXIT 8 | |
244 | #define ZFOREACH 9 | |
245 | #define ZGOTO 10 | |
246 | #define ZIF 11 | |
247 | #define ZLABEL 12 | |
248 | #define ZLET 13 | |
249 | #define ZSET 14 | |
250 | #define ZSWITCH 15 | |
251 | #define ZTEST 16 | |
252 | #define ZTHEN 17 | |
253 | #define ZWHILE 18 | |
254 | ||
255 | /* | |
256 | * Structure defining the existing while/foreach loops at this | |
257 | * source level. Loops are implemented by seeking back in the | |
258 | * input. For foreach (fe), the word list is attached here. | |
259 | */ | |
260 | struct whyle { | |
261 | off_t w_start; /* Point to restart loop */ | |
262 | off_t w_end; /* End of loop (0 if unknown) */ | |
263 | char **w_fe, **w_fe0; /* Current/initial wordlist for fe */ | |
264 | char *w_fename; /* Name for fe */ | |
265 | struct whyle *w_next; /* Next (more outer) loop */ | |
266 | } *whyles; | |
267 | ||
268 | /* | |
269 | * Variable structure | |
270 | * | |
271 | * Lists of aliases and variables are sorted alphabetically by name | |
272 | */ | |
273 | struct varent { | |
274 | char **vec; /* Array of words which is the value */ | |
275 | char *name; /* Name of variable/alias */ | |
276 | struct varent *link; | |
277 | } shvhed, aliases; | |
278 | ||
279 | /* | |
280 | * The following are for interfacing redo substitution in | |
281 | * aliases to the lexical routines. | |
282 | */ | |
283 | struct wordent *alhistp; /* Argument list (first) */ | |
284 | struct wordent *alhistt; /* Node after last in arg list */ | |
285 | char **alvec; /* The (remnants of) alias vector */ | |
286 | ||
287 | /* | |
288 | * Filename/command name expansion variables | |
289 | */ | |
290 | short gflag; /* After tglob -> is globbing needed? */ | |
291 | ||
292 | /* | |
293 | * A reasonable limit on number of arguments would seem to be | |
294 | * the maximum number of characters in an arg list / 6. | |
295 | */ | |
296 | #define GAVSIZ NCARGS / 6 | |
297 | ||
298 | /* | |
299 | * Variables for filename expansion | |
300 | */ | |
301 | char **gargv; /* Pointer to the (stack) arglist */ | |
302 | short gargc; /* Number args in gargv */ | |
303 | short gnleft; | |
304 | ||
305 | /* | |
306 | * Variables for command expansion. | |
307 | */ | |
308 | char **pargv; /* Pointer to the argv list space */ | |
309 | char *pargs; /* Pointer to start current word */ | |
310 | short pargc; /* Count of arguments in pargv */ | |
311 | short pnleft; /* Number of chars left in pargs */ | |
312 | char *pargcp; /* Current index into pargs */ | |
313 | ||
314 | /* | |
315 | * History list | |
316 | * | |
317 | * Each history list entry contains an embedded wordlist | |
318 | * from the scanner, a number for the event, and a reference count | |
319 | * to aid in discarding old entries. | |
320 | * | |
321 | * Essentially "invisible" entries are put on the history list | |
322 | * when history substitution includes modifiers, and thrown away | |
323 | * at the next discarding since their event numbers are very negative. | |
324 | */ | |
325 | struct Hist { | |
326 | struct wordent Hlex; | |
327 | int Hnum; | |
328 | int Href; | |
329 | struct Hist *Hnext; | |
330 | } Histlist; | |
331 | ||
332 | int eventno; /* Next events number */ | |
333 | int lastev; /* Last event reference (default) */ | |
334 | ||
335 | char *Dfix1(); | |
336 | struct varent *adrof(), *adrof1(); | |
337 | char **blkcat(); | |
338 | char **blkcpy(); | |
339 | char **blkend(); | |
340 | char **blkspl(); | |
341 | char *calloc(); | |
342 | char *cname(); | |
343 | char **copyblk(); | |
344 | char **dobackp(); | |
345 | char *domod(); | |
346 | struct wordent *dosub(); | |
347 | char *exp3(); | |
348 | char *exp3a(); | |
349 | char *exp4(); | |
350 | char *exp5(); | |
351 | char *exp6(); | |
352 | struct Hist *enthist(); | |
353 | struct Hist *findev(); | |
354 | struct wordent *freenod(); | |
355 | char *getenv(); | |
356 | char *getinx(); | |
357 | struct varent *getvx(); | |
358 | struct passwd *getpwnam(); | |
359 | struct wordent *gethent(); | |
360 | struct wordent *getsub(); | |
361 | char *globone(); | |
362 | struct biltins *isbfunc(); | |
363 | char **glob(); | |
364 | char *operate(); | |
365 | int pintr(); | |
366 | char *putn(); | |
367 | char **saveblk(); | |
368 | char *savestr(); | |
369 | char *strcat(); | |
370 | char *strcpy(); | |
371 | char *strend(); | |
372 | char *strings(); | |
373 | char *strip(); | |
374 | char *strspl(); | |
375 | char *subword(); | |
376 | struct command *syntax(); | |
377 | struct command *syn0(); | |
378 | struct command *syn1(); | |
379 | struct command *syn1a(); | |
380 | struct command *syn1b(); | |
381 | struct command *syn2(); | |
382 | struct command *syn3(); | |
383 | int tglob(); | |
384 | int trim(); | |
385 | char *value(), *value1(); | |
386 | char *xhome(); | |
387 | char *xname(); | |
388 | char *xset(); | |
389 | ||
390 | #define NOSTR ((char *) 0) | |
391 | ||
392 | /* | |
393 | * setname is a macro to save space (see sh.err.c) | |
394 | */ | |
395 | char *bname; | |
396 | #define setname(a) bname = (a); | |
397 | ||
398 | #ifdef VFORK | |
399 | char *Vsav; | |
400 | char **Vav; | |
401 | char *Vdp; | |
402 | #endif |