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