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