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