Commit | Line | Data |
---|---|---|
3ffa6b49 | 1 | /* csh.h 4.10 83/07/01 */ |
8c95f417 BJ |
2 | |
3 | #include "sh.local.h" | |
3ffa6b49 BJ |
4 | #include <sys/time.h> |
5 | #include <sys/resource.h> | |
8c95f417 BJ |
6 | |
7 | /* | |
8 | * C shell | |
9 | * | |
10 | * Bill Joy, UC Berkeley | |
11 | * October, 1978; May 1980 | |
12 | * | |
13 | * Jim Kulp, IIASA, Laxenburg Austria | |
14 | * April, 1980 | |
15 | */ | |
3d9f6a35 | 16 | #include <sys/param.h> |
8c95f417 | 17 | #include <sys/stat.h> |
8c95f417 BJ |
18 | |
19 | #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR) | |
20 | ||
21 | #include <errno.h> | |
22 | #include <setjmp.h> | |
23 | #include <signal.h> | |
24 | #include <sys/times.h> | |
25 | ||
26 | typedef char bool; | |
27 | ||
28 | #define eq(a, b) (strcmp(a, b) == 0) | |
2f2a0649 SL |
29 | |
30 | /* | |
31 | * For 4.2bsd signals. | |
32 | */ | |
6ca601c1 | 33 | #define mask(s) (1 << ((s)-1)) |
2f2a0649 SL |
34 | #define sigsys(s, a) signal(s, a) |
35 | #define sighold(s) sigblock(mask(s)) | |
8c95f417 BJ |
36 | |
37 | /* | |
38 | * Global flags | |
39 | */ | |
40 | bool chkstop; /* Warned of stopped jobs... allow exit */ | |
41 | bool didcch; /* Have closed unused fd's for child */ | |
42 | bool didfds; /* Have setup i/o fd's for child */ | |
43 | bool doneinp; /* EOF indicator after reset from readc */ | |
44 | bool exiterr; /* Exit if error or non-zero exit status */ | |
45 | bool child; /* Child shell ... errors cause exit */ | |
46 | bool haderr; /* Reset was because of an error */ | |
47 | bool intty; /* Input is a tty */ | |
48 | bool intact; /* We are interactive... therefore prompt */ | |
49 | bool justpr; /* Just print because of :p hist mod */ | |
50 | bool loginsh; /* We are a loginsh -> .login/.logout */ | |
51 | bool neednote; /* Need to pnotify() */ | |
52 | bool noexec; /* Don't execute, just syntax check */ | |
53 | bool pjobs; /* want to print jobs if interrupted */ | |
54 | bool setintr; /* Set interrupts on/off -> Wait intr... */ | |
55 | bool timflg; /* Time the next waited for command */ | |
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 | int oldisc; /* Initial line discipline or -1 */ |
81 | struct tms shtimes; /* shell and child times for process timing */ | |
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[]; | |
93 | ||
94 | #define INF 1000 | |
95 | ||
96 | struct srch { | |
97 | char *s_name; | |
98 | short s_value; | |
99 | } srchn[]; | |
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 | ||
124 | #define setexit() setjmp(reslab) | |
125 | #define reset() longjmp(reslab) | |
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 | ||
165 | off_t btell(); | |
166 | ||
167 | /* | |
168 | * The shell finds commands in loops by reseeking the input | |
169 | * For whiles, in particular, it reseeks to the beginning of the | |
170 | * line the while was on; hence the while placement restrictions. | |
171 | */ | |
172 | off_t lineloc; | |
173 | ||
174 | #ifdef TELL | |
175 | off_t tell(); | |
176 | bool cantell; /* Is current source tellable ? */ | |
177 | #endif | |
178 | ||
179 | /* | |
180 | * Input lines are parsed into doubly linked circular | |
181 | * lists of words of the following form. | |
182 | */ | |
183 | struct wordent { | |
184 | char *word; | |
185 | struct wordent *prev; | |
186 | struct wordent *next; | |
187 | }; | |
188 | ||
189 | /* | |
190 | * During word building, both in the initial lexical phase and | |
191 | * when expanding $ variable substitutions, expansion by `!' and `$' | |
192 | * must be inhibited when reading ahead in routines which are themselves | |
193 | * processing `!' and `$' expansion or after characters such as `\' or in | |
194 | * quotations. The following flags are passed to the getC routines | |
195 | * telling them which of these substitutions are appropriate for the | |
196 | * next character to be returned. | |
197 | */ | |
198 | #define DODOL 1 | |
199 | #define DOEXCL 2 | |
200 | #define DOALL DODOL|DOEXCL | |
201 | ||
202 | /* | |
203 | * Labuf implements a general buffer for lookahead during lexical operations. | |
204 | * Text which is to be placed in the input stream can be stuck here. | |
205 | * We stick parsed ahead $ constructs during initial input, | |
206 | * process id's from `$$', and modified variable values (from qualifiers | |
207 | * during expansion in sh.dol.c) here. | |
208 | */ | |
8c95f417 | 209 | char labuf[BUFSIZ]; |
8c95f417 BJ |
210 | |
211 | char *lap; | |
212 | ||
213 | /* | |
214 | * Parser structure | |
215 | * | |
216 | * Each command is parsed to a tree of command structures and | |
217 | * flags are set bottom up during this process, to be propagated down | |
218 | * as needed during the semantics/exeuction pass (sh.sem.c). | |
219 | */ | |
220 | struct command { | |
221 | short t_dtyp; /* Type of node */ | |
222 | short t_dflg; /* Flags, e.g. FAND|... */ | |
223 | union { | |
224 | char *T_dlef; /* Input redirect word */ | |
225 | struct command *T_dcar; /* Left part of list/pipe */ | |
226 | } L; | |
227 | union { | |
228 | char *T_drit; /* Output redirect word */ | |
229 | struct command *T_dcdr; /* Right part of list/pipe */ | |
230 | } R; | |
231 | #define t_dlef L.T_dlef | |
232 | #define t_dcar L.T_dcar | |
233 | #define t_drit R.T_drit | |
234 | #define t_dcdr R.T_dcdr | |
235 | char **t_dcom; /* Command/argument vector */ | |
236 | struct command *t_dspr; /* Pointer to ()'d subtree */ | |
237 | short t_nice; | |
238 | }; | |
239 | ||
240 | #define TCOM 1 /* t_dcom <t_dlef >t_drit */ | |
241 | #define TPAR 2 /* ( t_dspr ) <t_dlef >t_drit */ | |
242 | #define TFIL 3 /* t_dlef | t_drit */ | |
243 | #define TLST 4 /* t_dlef ; t_drit */ | |
244 | #define TOR 5 /* t_dlef || t_drit */ | |
245 | #define TAND 6 /* t_dlef && t_drit */ | |
246 | ||
247 | #define FSAVE (FNICE|FTIME|FNOHUP) /* save these when re-doing */ | |
248 | ||
249 | #define FAND (1<<0) /* executes in background */ | |
250 | #define FCAT (1<<1) /* output is redirected >> */ | |
251 | #define FPIN (1<<2) /* input is a pipe */ | |
252 | #define FPOU (1<<3) /* output is a pipe */ | |
253 | #define FPAR (1<<4) /* don't fork, last ()ized cmd */ | |
254 | #define FINT (1<<5) /* should be immune from intr's */ | |
255 | /* spare */ | |
256 | #define FDIAG (1<<7) /* redirect unit 2 with unit 1 */ | |
257 | #define FANY (1<<8) /* output was ! */ | |
258 | #define FHERE (1<<9) /* input redirection is << */ | |
259 | #define FREDO (1<<10) /* reexec aft if, repeat,... */ | |
260 | #define FNICE (1<<11) /* t_nice is meaningful */ | |
261 | #define FNOHUP (1<<12) /* nohup this command */ | |
262 | #define FTIME (1<<13) /* time this command */ | |
263 | ||
264 | /* | |
265 | * The keywords for the parser | |
266 | */ | |
267 | #define ZBREAK 0 | |
268 | #define ZBRKSW 1 | |
269 | #define ZCASE 2 | |
270 | #define ZDEFAULT 3 | |
271 | #define ZELSE 4 | |
272 | #define ZEND 5 | |
273 | #define ZENDIF 6 | |
274 | #define ZENDSW 7 | |
275 | #define ZEXIT 8 | |
276 | #define ZFOREACH 9 | |
277 | #define ZGOTO 10 | |
278 | #define ZIF 11 | |
279 | #define ZLABEL 12 | |
280 | #define ZLET 13 | |
281 | #define ZSET 14 | |
282 | #define ZSWITCH 15 | |
283 | #define ZTEST 16 | |
284 | #define ZTHEN 17 | |
285 | #define ZWHILE 18 | |
286 | ||
287 | /* | |
288 | * Structure defining the existing while/foreach loops at this | |
289 | * source level. Loops are implemented by seeking back in the | |
290 | * input. For foreach (fe), the word list is attached here. | |
291 | */ | |
292 | struct whyle { | |
293 | off_t w_start; /* Point to restart loop */ | |
294 | off_t w_end; /* End of loop (0 if unknown) */ | |
295 | char **w_fe, **w_fe0; /* Current/initial wordlist for fe */ | |
296 | char *w_fename; /* Name for fe */ | |
297 | struct whyle *w_next; /* Next (more outer) loop */ | |
298 | } *whyles; | |
299 | ||
300 | /* | |
301 | * Variable structure | |
302 | * | |
303 | * Lists of aliases and variables are sorted alphabetically by name | |
304 | */ | |
305 | struct varent { | |
306 | char **vec; /* Array of words which is the value */ | |
307 | char *name; /* Name of variable/alias */ | |
308 | struct varent *link; | |
309 | } shvhed, aliases; | |
310 | ||
311 | /* | |
312 | * The following are for interfacing redo substitution in | |
313 | * aliases to the lexical routines. | |
314 | */ | |
315 | struct wordent *alhistp; /* Argument list (first) */ | |
316 | struct wordent *alhistt; /* Node after last in arg list */ | |
317 | char **alvec; /* The (remnants of) alias vector */ | |
318 | ||
319 | /* | |
320 | * Filename/command name expansion variables | |
321 | */ | |
322 | short gflag; /* After tglob -> is globbing needed? */ | |
323 | ||
324 | /* | |
325 | * A reasonable limit on number of arguments would seem to be | |
326 | * the maximum number of characters in an arg list / 6. | |
327 | */ | |
328 | #define GAVSIZ NCARGS / 6 | |
329 | ||
330 | /* | |
331 | * Variables for filename expansion | |
332 | */ | |
333 | char **gargv; /* Pointer to the (stack) arglist */ | |
334 | short gargc; /* Number args in gargv */ | |
335 | short gnleft; | |
336 | ||
337 | /* | |
338 | * Variables for command expansion. | |
339 | */ | |
340 | char **pargv; /* Pointer to the argv list space */ | |
341 | char *pargs; /* Pointer to start current word */ | |
342 | short pargc; /* Count of arguments in pargv */ | |
343 | short pnleft; /* Number of chars left in pargs */ | |
344 | char *pargcp; /* Current index into pargs */ | |
345 | ||
346 | /* | |
347 | * History list | |
348 | * | |
349 | * Each history list entry contains an embedded wordlist | |
350 | * from the scanner, a number for the event, and a reference count | |
351 | * to aid in discarding old entries. | |
352 | * | |
353 | * Essentially "invisible" entries are put on the history list | |
354 | * when history substitution includes modifiers, and thrown away | |
355 | * at the next discarding since their event numbers are very negative. | |
356 | */ | |
357 | struct Hist { | |
358 | struct wordent Hlex; | |
359 | int Hnum; | |
360 | int Href; | |
361 | struct Hist *Hnext; | |
362 | } Histlist; | |
363 | ||
364 | struct wordent paraml; /* Current lexical word list */ | |
365 | int eventno; /* Next events number */ | |
366 | int lastev; /* Last event reference (default) */ | |
367 | ||
368 | char HIST; /* history invocation character */ | |
369 | char HISTSUB; /* auto-substitute character */ | |
370 | ||
371 | char *Dfix1(); | |
372 | struct varent *adrof(), *adrof1(); | |
373 | char **blkcat(); | |
374 | char **blkcpy(); | |
375 | char **blkend(); | |
376 | char **blkspl(); | |
377 | char *calloc(); | |
378 | char *cname(); | |
379 | char **copyblk(); | |
380 | char **dobackp(); | |
381 | char *domod(); | |
382 | struct wordent *dosub(); | |
383 | char *exp3(); | |
384 | char *exp3a(); | |
385 | char *exp4(); | |
386 | char *exp5(); | |
387 | char *exp6(); | |
388 | struct Hist *enthist(); | |
389 | struct Hist *findev(); | |
390 | struct wordent *freenod(); | |
391 | char *getenv(); | |
392 | char *getinx(); | |
393 | struct varent *getvx(); | |
394 | struct passwd *getpwnam(); | |
395 | struct wordent *gethent(); | |
396 | struct wordent *getsub(); | |
397 | char *getwd(); | |
398 | char *globone(); | |
399 | struct biltins *isbfunc(); | |
400 | char **glob(); | |
401 | char *operate(); | |
d7929fa7 | 402 | int phup(); |
8c95f417 BJ |
403 | int pintr(); |
404 | int pchild(); | |
405 | char *putn(); | |
406 | char **saveblk(); | |
407 | char *savestr(); | |
408 | char *strcat(); | |
409 | char *strcpy(); | |
410 | char *strend(); | |
411 | char *strings(); | |
412 | char *strip(); | |
413 | char *strspl(); | |
414 | char *subword(); | |
415 | struct command *syntax(); | |
416 | struct command *syn0(); | |
417 | struct command *syn1(); | |
418 | struct command *syn1a(); | |
419 | struct command *syn1b(); | |
420 | struct command *syn2(); | |
421 | struct command *syn3(); | |
422 | int tglob(); | |
423 | int trim(); | |
424 | char *value(), *value1(); | |
425 | char *xhome(); | |
426 | char *xname(); | |
427 | char *xset(); | |
428 | ||
429 | #define NOSTR ((char *) 0) | |
430 | ||
431 | /* | |
432 | * setname is a macro to save space (see sh.err.c) | |
433 | */ | |
434 | char *bname; | |
435 | #define setname(a) bname = (a); | |
436 | ||
437 | #ifdef VFORK | |
438 | char *Vsav; | |
439 | char **Vav; | |
440 | char *Vdp; | |
441 | #endif | |
442 | ||
8c95f417 BJ |
443 | char **evalvec; |
444 | char *evalp; | |
445 | ||
446 | struct mesg { | |
447 | char *iname; /* name from /usr/include */ | |
448 | char *pname; /* print name */ | |
449 | } mesg[]; |