modern syntax for asgops & inits cause Donn's latest ccom rejects the old.
[unix-history] / usr / src / bin / csh / csh.h
CommitLineData
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
31typedef char bool;
32
33#define eq(a, b) (strcmp(a, b) == 0)
2f2a0649 34
8c95f417
BJ
35/*
36 * Global flags
37 */
38bool chkstop; /* Warned of stopped jobs... allow exit */
8c95f417
BJ
39bool didfds; /* Have setup i/o fd's for child */
40bool doneinp; /* EOF indicator after reset from readc */
41bool exiterr; /* Exit if error or non-zero exit status */
42bool child; /* Child shell ... errors cause exit */
43bool haderr; /* Reset was because of an error */
44bool intty; /* Input is a tty */
45bool intact; /* We are interactive... therefore prompt */
46bool justpr; /* Just print because of :p hist mod */
47bool loginsh; /* We are a loginsh -> .login/.logout */
48bool neednote; /* Need to pnotify() */
49bool noexec; /* Don't execute, just syntax check */
50bool pjobs; /* want to print jobs if interrupted */
51bool setintr; /* Set interrupts on/off -> Wait intr... */
52bool timflg; /* Time the next waited for command */
35371dec
EW
53bool havhash; /* path hashing is available */
54#ifdef FILEC
55bool filec; /* doing filename expansion */
56#endif
8c95f417
BJ
57
58/*
59 * Global i/o info
60 */
61char *arginp; /* Argument input for sh -c and internal `xx` */
62int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
63char *file; /* Name of shell file for $0 */
64
65char *err; /* Error message from scanner/parser */
66int errno; /* Error from C library routines */
67char *shtemp; /* Temp name for << shell files in /tmp */
ae3d9709 68struct timeval time0; /* Time at which the shell started */
ea775389 69struct rusage ru0;
8c95f417
BJ
70
71/*
72 * Miscellany
73 */
74char *doldol; /* Character pid for $$ */
75int uid; /* Invokers uid */
76time_t chktim; /* Time mail last checked */
cc519405
SL
77int shpgrp; /* Pgrp of shell */
78int tpgrp; /* Terminal process group */
8c95f417 79/* If tpgrp is -1, leave tty alone! */
cc519405 80int opgrp; /* Initial pgrp and tty pgrp */
8c95f417 81int 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
88struct biltins {
89 char *bname;
90 int (*bfunct)();
91 short minargs, maxargs;
92} bfunc[];
35371dec 93extern int nbfunc;
8c95f417
BJ
94
95struct srch {
96 char *s_name;
97 short s_value;
98} srchn[];
35371dec 99extern 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 */
109short SHIN; /* Current shell input (script) */
110short SHOUT; /* Shell output */
111short SHDIAG; /* Diagnostic output... shell errs go here */
112short 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
122jmp_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
130char *gointr; /* Label for an onintr transfer */
131int (*parintr)(); /* Parents interrupt catch */
132int (*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 */
151struct 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 168off_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 */
176off_t lineloc;
177
178#ifdef TELL
8c95f417
BJ
179bool 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 */
186struct 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 212char labuf[BUFSIZ];
8c95f417
BJ
213
214char *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 */
223struct 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 */
295struct 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 */
308struct 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
318struct 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 */
326struct wordent *alhistp; /* Argument list (first) */
327struct wordent *alhistt; /* Node after last in arg list */
328char **alvec; /* The (remnants of) alias vector */
329
330/*
331 * Filename/command name expansion variables
332 */
333short 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 */
344char **gargv; /* Pointer to the (stack) arglist */
345short gargc; /* Number args in gargv */
346short gnleft;
347
348/*
349 * Variables for command expansion.
350 */
351char **pargv; /* Pointer to the argv list space */
352char *pargs; /* Pointer to start current word */
353short pargc; /* Count of arguments in pargv */
354short pnleft; /* Number of chars left in pargs */
355char *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 */
368struct Hist {
369 struct wordent Hlex;
370 int Hnum;
371 int Href;
372 struct Hist *Hnext;
373} Histlist;
374
375struct wordent paraml; /* Current lexical word list */
376int eventno; /* Next events number */
377int lastev; /* Last event reference (default) */
378
379char HIST; /* history invocation character */
380char 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}
391char *alloctmp;
392#define xalloc(i) ((alloctmp = malloc(i)) ? alloctmp : (char *)nomem(i))
393
8c95f417 394char *Dfix1();
8c95f417
BJ
395char **blkcat();
396char **blkcpy();
397char **blkend();
398char **blkspl();
399char *calloc();
35371dec 400char *malloc();
8c95f417
BJ
401char *cname();
402char **copyblk();
403char **dobackp();
404char *domod();
405struct wordent *dosub();
406char *exp3();
407char *exp3a();
408char *exp4();
409char *exp5();
410char *exp6();
411struct Hist *enthist();
412struct Hist *findev();
413struct wordent *freenod();
414char *getenv();
415char *getinx();
416struct varent *getvx();
417struct passwd *getpwnam();
418struct wordent *gethent();
419struct wordent *getsub();
420char *getwd();
35371dec 421char **glob();
8c95f417 422char *globone();
35371dec 423char *index();
8c95f417 424struct biltins *isbfunc();
35371dec 425off_t lseek();
8c95f417 426char *operate();
d7929fa7 427int phup();
8c95f417
BJ
428int pintr();
429int pchild();
430char *putn();
35371dec 431char *rindex();
8c95f417
BJ
432char **saveblk();
433char *savestr();
434char *strcat();
435char *strcpy();
436char *strend();
437char *strings();
438char *strip();
439char *strspl();
440char *subword();
441struct command *syntax();
442struct command *syn0();
443struct command *syn1();
444struct command *syn1a();
445struct command *syn1b();
446struct command *syn2();
447struct command *syn3();
35371dec 448char *value1();
8c95f417
BJ
449char *xhome();
450char *xname();
451char *xset();
452
453#define NOSTR ((char *) 0)
454
455/*
456 * setname is a macro to save space (see sh.err.c)
457 */
458char *bname;
35371dec 459#define setname(a) (bname = (a))
8c95f417
BJ
460
461#ifdef VFORK
462char *Vsav;
463char **Vav;
464char *Vdp;
465#endif
466
8c95f417
BJ
467char **evalvec;
468char *evalp;
469
470struct mesg {
471 char *iname; /* name from /usr/include */
472 char *pname; /* print name */
473} mesg[];