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