csh.h revision 1.1 1 /*-
2 * Copyright (c) 1980, 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)csh.h 5.15 (Berkeley) 6/8/91
34 */
35
36 /*
37 * Fundamental definitions which may vary from system to system.
38 *
39 * BUFSIZ The i/o buffering size; also limits word size
40 * MAILINTVL How often to mailcheck; more often is more expensive
41 */
42 #ifndef BUFSIZ
43 #define BUFSIZ 1024 /* default buffer size */
44 #endif /* BUFSIZ */
45
46 #define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
47 #define MAILINTVL 600 /* 10 minutes */
48
49 /*
50 * The shell moves std in/out/diag and the old std input away from units
51 * 0, 1, and 2 so that it is easy to set up these standards for invoked
52 * commands.
53 */
54 #define FSHTTY 15 /* /dev/tty when manip pgrps */
55 #define FSHIN 16 /* Preferred desc for shell input */
56 #define FSHOUT 17 /* ... shell output */
57 #define FSHDIAG 18 /* ... shell diagnostics */
58 #define FOLDSTD 19 /* ... old std input */
59
60 #ifdef PROF
61 #define xexit(n) done(n)
62 #endif
63
64 #ifdef SHORT_STRINGS
65 typedef short Char;
66
67 #define SAVE(a) (Strsave(str2short(a)))
68 #else
69 typedef char Char;
70
71 #define SAVE(a) (strsave(a))
72 #endif
73
74 typedef void *ioctl_t; /* Third arg of ioctl */
75
76 typedef void *ptr_t;
77
78 #include "const.h"
79 #include "char.h"
80 #include "err.h"
81
82 #ifdef SYSMALLOC
83 #define xmalloc(i) Malloc(i)
84 #define xrealloc(p, i) Realloc(p, i)
85 #define xcalloc(n, s) Calloc(n, s)
86 #define xfree(p) Free(p)
87 #else
88 #define xmalloc(i) malloc(i)
89 #define xrealloc(p, i) realloc(p, i)
90 #define xcalloc(n, s) calloc(n, s)
91 #define xfree(p) free(p)
92 #endif /* SYSMALLOC */
93
94 #define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR)
95
96 #define SIGN_EXTEND_CHAR(a) \
97 ((a) & 0x80 ? ((int) (a)) | 0xffffff00 : ((int) a) & 0x000000ff)
98
99
100 typedef int bool;
101
102 #define eq(a, b) (Strcmp(a, b) == 0)
103
104 /* globone() flags */
105 #define G_ERROR 0 /* default action: error if multiple words */
106 #define G_IGNORE 1 /* ignore the rest of the words */
107 #define G_APPEND 2 /* make a sentence by cat'ing the words */
108
109 /*
110 * Global flags
111 */
112 bool chkstop; /* Warned of stopped jobs... allow exit */
113 bool didfds; /* Have setup i/o fd's for child */
114 bool doneinp; /* EOF indicator after reset from readc */
115 bool exiterr; /* Exit if error or non-zero exit status */
116 bool child; /* Child shell ... errors cause exit */
117 bool haderr; /* Reset was because of an error */
118 bool intty; /* Input is a tty */
119 bool intact; /* We are interactive... therefore prompt */
120 bool justpr; /* Just print because of :p hist mod */
121 bool loginsh; /* We are a loginsh -> .login/.logout */
122 bool neednote; /* Need to pnotify() */
123 bool noexec; /* Don't execute, just syntax check */
124 bool pjobs; /* want to print jobs if interrupted */
125 bool setintr; /* Set interrupts on/off -> Wait intr... */
126 bool timflg; /* Time the next waited for command */
127 bool havhash; /* path hashing is available */
128
129 #ifdef FILEC
130 bool filec; /* doing filename expansion */
131 #endif
132
133 /*
134 * Global i/o info
135 */
136 Char *arginp; /* Argument input for sh -c and internal `xx` */
137 int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
138 Char *ffile; /* Name of shell file for $0 */
139
140 char *seterr; /* Error message from scanner/parser */
141 Char *shtemp; /* Temp name for << shell files in /tmp */
142
143 #include <sys/time.h>
144 #include <sys/resource.h>
145
146 struct timeval time0; /* Time at which the shell started */
147 struct rusage ru0;
148
149 /*
150 * Miscellany
151 */
152 Char *doldol; /* Character pid for $$ */
153 int uid; /* Invokers uid */
154 int gid; /* Invokers gid */
155 time_t chktim; /* Time mail last checked */
156 int shpgrp; /* Pgrp of shell */
157 int tpgrp; /* Terminal process group */
158
159 /* If tpgrp is -1, leave tty alone! */
160 int opgrp; /* Initial pgrp and tty pgrp */
161
162 /*
163 * These are declared here because they want to be
164 * initialized in sh.init.c (to allow them to be made readonly)
165 */
166
167 extern struct biltins {
168 char *bname;
169 void (*bfunct) ();
170 short minargs, maxargs;
171 } bfunc[];
172 extern int nbfunc;
173
174 extern struct srch {
175 char *s_name;
176 short s_value;
177 } srchn[];
178 extern int nsrchn;
179
180 /*
181 * To be able to redirect i/o for builtins easily, the shell moves the i/o
182 * descriptors it uses away from 0,1,2.
183 * Ideally these should be in units which are closed across exec's
184 * (this saves work) but for version 6, this is not usually possible.
185 * The desired initial values for these descriptors are defined in
186 * local.h.
187 */
188 short SHIN; /* Current shell input (script) */
189 short SHOUT; /* Shell output */
190 short SHDIAG; /* Diagnostic output... shell errs go here */
191 short OLDSTD; /* Old standard input (def for cmds) */
192
193 /*
194 * Error control
195 *
196 * Errors in scanning and parsing set up an error message to be printed
197 * at the end and complete. Other errors always cause a reset.
198 * Because of source commands and .cshrc we need nested error catches.
199 */
200
201 #include <setjmp.h>
202 jmp_buf reslab;
203
204 #define setexit() (setjmp(reslab))
205 #define reset() longjmp(reslab, 1)
206 /* Should use structure assignment here */
207 #define getexit(a) bcopy((char *)reslab, ((char *)(a)), sizeof reslab)
208 #define resexit(a) bcopy((char *)(a), (char *)reslab, sizeof reslab)
209
210 Char *gointr; /* Label for an onintr transfer */
211
212 #include <signal.h>
213 sig_t parintr; /* Parents interrupt catch */
214 sig_t parterm; /* Parents terminate catch */
215
216 /*
217 * Lexical definitions.
218 *
219 * All lexical space is allocated dynamically.
220 * The eighth/sizteenth bit of characters is used to prevent recognition,
221 * and eventually stripped.
222 */
223 #define META 0200
224 #define ASCII 0177
225 #ifdef SHORT_STRINGS
226 #define CHAR 0377
227 #define QUOTE 0100000 /* 16nth char bit used for 'ing */
228 #define TRIM 0077777 /* Mask to strip quote bit */
229 #else
230 #define CHAR 0177
231 #define QUOTE 0200 /* Eighth char bit used for 'ing */
232 #define TRIM 0177 /* Mask to strip quote bit */
233 #endif
234
235 int AsciiOnly; /* If set only 7 bits is expected in characters */
236
237 /*
238 * Each level of input has a buffered input structure.
239 * There are one or more blocks of buffered input for each level,
240 * exactly one if the input is seekable and tell is available.
241 * In other cases, the shell buffers enough blocks to keep all loops
242 * in the buffer.
243 */
244 struct Bin {
245 off_t Bfseekp; /* Seek pointer */
246 off_t Bfbobp; /* Seekp of beginning of buffers */
247 off_t Bfeobp; /* Seekp of end of buffers */
248 short Bfblocks; /* Number of buffer blocks */
249 Char **Bfbuf; /* The array of buffer blocks */
250 } B;
251
252 #define fseekp B.Bfseekp
253 #define fbobp B.Bfbobp
254 #define feobp B.Bfeobp
255 #define fblocks B.Bfblocks
256 #define fbuf B.Bfbuf
257
258 /*
259 * The shell finds commands in loops by reseeking the input
260 * For whiles, in particular, it reseeks to the beginning of the
261 * line the while was on; hence the while placement restrictions.
262 */
263 off_t lineloc;
264
265 bool cantell; /* Is current source tellable ? */
266
267 /*
268 * Input lines are parsed into doubly linked circular
269 * lists of words of the following form.
270 */
271 struct wordent {
272 Char *word;
273 struct wordent *prev;
274 struct wordent *next;
275 };
276
277 /*
278 * During word building, both in the initial lexical phase and
279 * when expanding $ variable substitutions, expansion by `!' and `$'
280 * must be inhibited when reading ahead in routines which are themselves
281 * processing `!' and `$' expansion or after characters such as `\' or in
282 * quotations. The following flags are passed to the getC routines
283 * telling them which of these substitutions are appropriate for the
284 * next character to be returned.
285 */
286 #define DODOL 1
287 #define DOEXCL 2
288 #define DOALL DODOL|DOEXCL
289
290 /*
291 * Labuf implements a general buffer for lookahead during lexical operations.
292 * Text which is to be placed in the input stream can be stuck here.
293 * We stick parsed ahead $ constructs during initial input,
294 * process id's from `$$', and modified variable values (from qualifiers
295 * during expansion in sh.dol.c) here.
296 */
297 Char *lap;
298
299 /*
300 * Parser structure
301 *
302 * Each command is parsed to a tree of command structures and
303 * flags are set bottom up during this process, to be propagated down
304 * as needed during the semantics/exeuction pass (sh.sem.c).
305 */
306 struct command {
307 short t_dtyp; /* Type of node */
308 #define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
309 #define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
310 #define NODE_PIPE 3 /* t_dlef | t_drit */
311 #define NODE_LIST 4 /* t_dlef ; t_drit */
312 #define NODE_OR 5 /* t_dlef || t_drit */
313 #define NODE_AND 6 /* t_dlef && t_drit */
314 short t_dflg; /* Flags, e.g. F_AMPERSAND|... */
315 #define F_SAVE (F_NICE|F_TIME|F_NOHUP) /* save these when re-doing */
316
317 #define F_AMPERSAND (1<<0) /* executes in background */
318 #define F_APPEND (1<<1) /* output is redirected >> */
319 #define F_PIPEIN (1<<2) /* input is a pipe */
320 #define F_PIPEOUT (1<<3) /* output is a pipe */
321 #define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
322 #define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
323 /* spare */
324 #define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
325 #define F_OVERWRITE (1<<8) /* output was ! */
326 #define F_READ (1<<9) /* input redirection is << */
327 #define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
328 #define F_NICE (1<<11) /* t_nice is meaningful */
329 #define F_NOHUP (1<<12) /* nohup this command */
330 #define F_TIME (1<<13) /* time this command */
331 union {
332 Char *T_dlef; /* Input redirect word */
333 struct command *T_dcar; /* Left part of list/pipe */
334 } L;
335 union {
336 Char *T_drit; /* Output redirect word */
337 struct command *T_dcdr; /* Right part of list/pipe */
338 } R;
339 #define t_dlef L.T_dlef
340 #define t_dcar L.T_dcar
341 #define t_drit R.T_drit
342 #define t_dcdr R.T_dcdr
343 Char **t_dcom; /* Command/argument vector */
344 struct command *t_dspr; /* Pointer to ()'d subtree */
345 short t_nice;
346 };
347
348
349 /*
350 * The keywords for the parser
351 */
352 #define T_BREAK 0
353 #define T_BRKSW 1
354 #define T_CASE 2
355 #define T_DEFAULT 3
356 #define T_ELSE 4
357 #define T_END 5
358 #define T_ENDIF 6
359 #define T_ENDSW 7
360 #define T_EXIT 8
361 #define T_FOREACH 9
362 #define T_GOTO 10
363 #define T_IF 11
364 #define T_LABEL 12
365 #define T_LET 13
366 #define T_SET 14
367 #define T_SWITCH 15
368 #define T_TEST 16
369 #define T_THEN 17
370 #define T_WHILE 18
371
372 /*
373 * Structure defining the existing while/foreach loops at this
374 * source level. Loops are implemented by seeking back in the
375 * input. For foreach (fe), the word list is attached here.
376 */
377 struct whyle {
378 off_t w_start; /* Point to restart loop */
379 off_t w_end; /* End of loop (0 if unknown) */
380 Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
381 Char *w_fename; /* Name for fe */
382 struct whyle *w_next; /* Next (more outer) loop */
383 } *whyles;
384
385 /*
386 * Variable structure
387 *
388 * Aliases and variables are stored in AVL balanced binary trees.
389 */
390 struct varent {
391 Char **vec; /* Array of words which is the value */
392 Char *v_name; /* Name of variable/alias */
393 struct varent *v_link[3]; /* The links, see below */
394 int v_bal; /* Balance factor */
395 } shvhed, aliases;
396
397 #define v_left v_link[0]
398 #define v_right v_link[1]
399 #define v_parent v_link[2]
400
401 struct varent *adrof1();
402
403 #define adrof(v) adrof1(v, &shvhed)
404 #define value(v) value1(v, &shvhed)
405
406 /*
407 * The following are for interfacing redo substitution in
408 * aliases to the lexical routines.
409 */
410 struct wordent *alhistp; /* Argument list (first) */
411 struct wordent *alhistt; /* Node after last in arg list */
412 Char **alvec; /* The (remnants of) alias vector */
413
414 /*
415 * Filename/command name expansion variables
416 */
417 short gflag; /* After tglob -> is globbing needed? */
418
419 #define MAXVARLEN 30 /* Maximum number of char in a variable name */
420
421 /*
422 * Variables for filename expansion
423 */
424 extern Char **gargv; /* Pointer to the (stack) arglist */
425 extern long gargc; /* Number args in gargv */
426
427 /*
428 * Variables for command expansion.
429 */
430 extern Char **pargv; /* Pointer to the argv list space */
431 extern long pargc; /* Count of arguments in pargv */
432 Char *pargs; /* Pointer to start current word */
433 long pnleft; /* Number of chars left in pargs */
434 Char *pargcp; /* Current index into pargs */
435
436 /*
437 * History list
438 *
439 * Each history list entry contains an embedded wordlist
440 * from the scanner, a number for the event, and a reference count
441 * to aid in discarding old entries.
442 *
443 * Essentially "invisible" entries are put on the history list
444 * when history substitution includes modifiers, and thrown away
445 * at the next discarding since their event numbers are very negative.
446 */
447 struct Hist {
448 struct wordent Hlex;
449 int Hnum;
450 int Href;
451 long Htime;
452 struct Hist *Hnext;
453 } Histlist;
454
455 struct wordent paraml; /* Current lexical word list */
456 int eventno; /* Next events number */
457 int lastev; /* Last event reference (default) */
458
459 Char HIST; /* history invocation character */
460 Char HISTSUB; /* auto-substitute character */
461
462 /*
463 * strings.h:
464 */
465 #ifndef SHORT_STRINGS
466 #define Strchr(a, b) strchr(a, b)
467 #define Strrchr(a, b) strrchr(a, b)
468 #define Strcat(a, b) strcat(a, b)
469 #define Strncat(a, b, c) strncat(a, b, c)
470 #define Strcpy(a, b) strcpy(a, b)
471 #define Strncpy(a, b, c) strncpy(a, b, c)
472 #define Strlen(a) strlen(a)
473 #define Strcmp(a, b) strcmp(a, b)
474 #define Strncmp(a, b, c) strncmp(a, b, c)
475
476 #define Strspl(a, b) strspl(a, b)
477 #define Strsave(a) strsave(a)
478 #define Strend(a) strend(a)
479 #define Strstr(a, b) strstr(a, b)
480
481 #define str2short(a) (a)
482 #define blk2short(a) saveblk(a)
483 #define short2blk(a) saveblk(a)
484 #define short2str(a) (a)
485 #define short2qstr(a) (a)
486 #else
487 #define Strchr(a, b) s_strchr(a, b)
488 #define Strrchr(a, b) s_strrchr(a, b)
489 #define Strcat(a, b) s_strcat(a, b)
490 #define Strncat(a, b, c) s_strncat(a, b, c)
491 #define Strcpy(a, b) s_strcpy(a, b)
492 #define Strncpy(a, b, c) s_strncpy(a, b, c)
493 #define Strlen(a) s_strlen(a)
494 #define Strcmp(a, b) s_strcmp(a, b)
495 #define Strncmp(a, b, c) s_strncmp(a, b, c)
496
497 #define Strspl(a, b) s_strspl(a, b)
498 #define Strsave(a) s_strsave(a)
499 #define Strend(a) s_strend(a)
500 #define Strstr(a, b) s_strstr(a, b)
501 #endif
502
503 /*
504 * setname is a macro to save space (see sh.err.c)
505 */
506 char *bname;
507
508 #define setname(a) (bname = (a))
509
510 Char *Vsav;
511 Char *Vdp;
512 Char *Vexpath;
513 char **Vt;
514
515 Char **evalvec;
516 Char *evalp;
517
518 extern struct mesg {
519 char *iname; /* name from /usr/include */
520 char *pname; /* print name */
521 } mesg[];
522
523 /* word_chars is set by default to WORD_CHARS but can be overridden by
524 the worchars variable--if unset, reverts to WORD_CHARS */
525
526 Char *word_chars;
527
528 #define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
529
530 Char *STR_SHELLPATH;
531
532 #include <paths.h>
533 #ifdef _PATH_BSHELL
534 Char *STR_BSHELL;
535 #endif
536 Char *STR_WORD_CHARS;
537 Char **STR_environ;
538