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