use the library globbing routines instead of rolling our own
[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 *
3d31e3bf 6 * @(#)sh.h 5.6 (Berkeley) 2/25/91
b79f4fa9 7 */
3d31e3bf 8#include <sys/param.h>
3ffa6b49
BJ
9#include <sys/time.h>
10#include <sys/resource.h>
35371dec
EW
11#include <sys/stat.h>
12#include <sys/signal.h>
13#include <errno.h>
14#include <setjmp.h>
15#include "sh.local.h"
c2dc294d 16#include "sh.char.h"
8c95f417
BJ
17
18/*
19 * C shell
20 *
21 * Bill Joy, UC Berkeley
22 * October, 1978; May 1980
23 *
24 * Jim Kulp, IIASA, Laxenburg Austria
25 * April, 1980
26 */
8c95f417
BJ
27
28#define isdir(d) ((d.st_mode & S_IFMT) == S_IFDIR)
29
8c95f417
BJ
30typedef char bool;
31
32#define eq(a, b) (strcmp(a, b) == 0)
2f2a0649 33
8c95f417
BJ
34/*
35 * Global flags
36 */
37bool chkstop; /* Warned of stopped jobs... allow exit */
8c95f417
BJ
38bool didfds; /* Have setup i/o fd's for child */
39bool doneinp; /* EOF indicator after reset from readc */
40bool exiterr; /* Exit if error or non-zero exit status */
41bool child; /* Child shell ... errors cause exit */
42bool haderr; /* Reset was because of an error */
43bool intty; /* Input is a tty */
44bool intact; /* We are interactive... therefore prompt */
45bool justpr; /* Just print because of :p hist mod */
46bool loginsh; /* We are a loginsh -> .login/.logout */
47bool neednote; /* Need to pnotify() */
48bool noexec; /* Don't execute, just syntax check */
49bool pjobs; /* want to print jobs if interrupted */
50bool setintr; /* Set interrupts on/off -> Wait intr... */
51bool timflg; /* Time the next waited for command */
35371dec
EW
52bool havhash; /* path hashing is available */
53#ifdef FILEC
54bool filec; /* doing filename expansion */
55#endif
8c95f417
BJ
56
57/*
58 * Global i/o info
59 */
60char *arginp; /* Argument input for sh -c and internal `xx` */
61int onelflg; /* 2 -> need line for -t, 1 -> exit on read */
62char *file; /* Name of shell file for $0 */
63
64char *err; /* Error message from scanner/parser */
65int errno; /* Error from C library routines */
66char *shtemp; /* Temp name for << shell files in /tmp */
ae3d9709 67struct timeval time0; /* Time at which the shell started */
ea775389 68struct rusage ru0;
8c95f417
BJ
69
70/*
71 * Miscellany
72 */
73char *doldol; /* Character pid for $$ */
74int uid; /* Invokers uid */
75time_t chktim; /* Time mail last checked */
cc519405
SL
76int shpgrp; /* Pgrp of shell */
77int tpgrp; /* Terminal process group */
8c95f417 78/* If tpgrp is -1, leave tty alone! */
cc519405 79int opgrp; /* Initial pgrp and tty pgrp */
8c95f417
BJ
80
81/*
82 * These are declared here because they want to be
83 * initialized in sh.init.c (to allow them to be made readonly)
84 */
85
86struct biltins {
87 char *bname;
88 int (*bfunct)();
89 short minargs, maxargs;
90} bfunc[];
35371dec 91extern int nbfunc;
8c95f417
BJ
92
93struct srch {
94 char *s_name;
95 short s_value;
96} srchn[];
35371dec 97extern int nsrchn;
8c95f417
BJ
98
99/*
100 * To be able to redirect i/o for builtins easily, the shell moves the i/o
101 * descriptors it uses away from 0,1,2.
102 * Ideally these should be in units which are closed across exec's
103 * (this saves work) but for version 6, this is not usually possible.
104 * The desired initial values for these descriptors are defined in
105 * sh.local.h.
106 */
107short SHIN; /* Current shell input (script) */
108short SHOUT; /* Shell output */
109short SHDIAG; /* Diagnostic output... shell errs go here */
110short OLDSTD; /* Old standard input (def for cmds) */
111
112/*
113 * Error control
114 *
115 * Errors in scanning and parsing set up an error message to be printed
116 * at the end and complete. Other errors always cause a reset.
117 * Because of source commands and .cshrc we need nested error catches.
118 */
119
120jmp_buf reslab;
121
35371dec
EW
122#define setexit() ((void) setjmp(reslab))
123#define reset() longjmp(reslab, 0)
8c95f417 124 /* Should use structure assignment here */
3d31e3bf
KB
125#define getexit(a) bcopy((char *)reslab, (char *)(a), sizeof reslab)
126#define resexit(a) bcopy(((char *)(a)), (char *)reslab, sizeof reslab)
8c95f417
BJ
127
128char *gointr; /* Label for an onintr transfer */
c5020dd9
KB
129sig_t parintr; /* Parents interrupt catch */
130sig_t parterm; /* Parents terminate catch */
8c95f417
BJ
131
132/*
133 * Lexical definitions.
134 *
135 * All lexical space is allocated dynamically.
136 * The eighth bit of characters is used to prevent recognition,
137 * and eventually stripped.
138 */
139#define QUOTE 0200 /* Eighth char bit used internally for 'ing */
140#define TRIM 0177 /* Mask to strip quote bit */
141
142/*
143 * Each level of input has a buffered input structure.
144 * There are one or more blocks of buffered input for each level,
145 * exactly one if the input is seekable and tell is available.
146 * In other cases, the shell buffers enough blocks to keep all loops
147 * in the buffer.
148 */
149struct Bin {
150 off_t Bfseekp; /* Seek pointer */
151 off_t Bfbobp; /* Seekp of beginning of buffers */
152 off_t Bfeobp; /* Seekp of end of buffers */
153 short Bfblocks; /* Number of buffer blocks */
154 char **Bfbuf; /* The array of buffer blocks */
155} B;
156
157#define fseekp B.Bfseekp
158#define fbobp B.Bfbobp
159#define feobp B.Bfeobp
160#define fblocks B.Bfblocks
161#define fbuf B.Bfbuf
162
c2dc294d
JL
163#define btell() fseekp
164
165#ifndef btell
8c95f417 166off_t btell();
c2dc294d 167#endif
8c95f417
BJ
168
169/*
170 * The shell finds commands in loops by reseeking the input
171 * For whiles, in particular, it reseeks to the beginning of the
172 * line the while was on; hence the while placement restrictions.
173 */
174off_t lineloc;
175
176#ifdef TELL
8c95f417
BJ
177bool cantell; /* Is current source tellable ? */
178#endif
179
180/*
181 * Input lines are parsed into doubly linked circular
182 * lists of words of the following form.
183 */
184struct wordent {
185 char *word;
186 struct wordent *prev;
187 struct wordent *next;
188};
189
190/*
191 * During word building, both in the initial lexical phase and
192 * when expanding $ variable substitutions, expansion by `!' and `$'
193 * must be inhibited when reading ahead in routines which are themselves
194 * processing `!' and `$' expansion or after characters such as `\' or in
195 * quotations. The following flags are passed to the getC routines
196 * telling them which of these substitutions are appropriate for the
197 * next character to be returned.
198 */
199#define DODOL 1
200#define DOEXCL 2
201#define DOALL DODOL|DOEXCL
202
203/*
204 * Labuf implements a general buffer for lookahead during lexical operations.
205 * Text which is to be placed in the input stream can be stuck here.
206 * We stick parsed ahead $ constructs during initial input,
207 * process id's from `$$', and modified variable values (from qualifiers
208 * during expansion in sh.dol.c) here.
209 */
8c95f417 210char labuf[BUFSIZ];
8c95f417
BJ
211
212char *lap;
213
214/*
215 * Parser structure
216 *
217 * Each command is parsed to a tree of command structures and
218 * flags are set bottom up during this process, to be propagated down
219 * as needed during the semantics/exeuction pass (sh.sem.c).
220 */
221struct command {
222 short t_dtyp; /* Type of node */
223 short t_dflg; /* Flags, e.g. FAND|... */
224 union {
225 char *T_dlef; /* Input redirect word */
226 struct command *T_dcar; /* Left part of list/pipe */
227 } L;
228 union {
229 char *T_drit; /* Output redirect word */
230 struct command *T_dcdr; /* Right part of list/pipe */
231 } R;
232#define t_dlef L.T_dlef
233#define t_dcar L.T_dcar
234#define t_drit R.T_drit
235#define t_dcdr R.T_dcdr
236 char **t_dcom; /* Command/argument vector */
237 struct command *t_dspr; /* Pointer to ()'d subtree */
238 short t_nice;
239};
240
241#define TCOM 1 /* t_dcom <t_dlef >t_drit */
242#define TPAR 2 /* ( t_dspr ) <t_dlef >t_drit */
243#define TFIL 3 /* t_dlef | t_drit */
244#define TLST 4 /* t_dlef ; t_drit */
245#define TOR 5 /* t_dlef || t_drit */
246#define TAND 6 /* t_dlef && t_drit */
247
248#define FSAVE (FNICE|FTIME|FNOHUP) /* save these when re-doing */
249
250#define FAND (1<<0) /* executes in background */
251#define FCAT (1<<1) /* output is redirected >> */
252#define FPIN (1<<2) /* input is a pipe */
253#define FPOU (1<<3) /* output is a pipe */
254#define FPAR (1<<4) /* don't fork, last ()ized cmd */
255#define FINT (1<<5) /* should be immune from intr's */
256/* spare */
257#define FDIAG (1<<7) /* redirect unit 2 with unit 1 */
258#define FANY (1<<8) /* output was ! */
259#define FHERE (1<<9) /* input redirection is << */
260#define FREDO (1<<10) /* reexec aft if, repeat,... */
261#define FNICE (1<<11) /* t_nice is meaningful */
262#define FNOHUP (1<<12) /* nohup this command */
263#define FTIME (1<<13) /* time this command */
264
265/*
266 * The keywords for the parser
267 */
268#define ZBREAK 0
269#define ZBRKSW 1
270#define ZCASE 2
271#define ZDEFAULT 3
272#define ZELSE 4
273#define ZEND 5
274#define ZENDIF 6
275#define ZENDSW 7
276#define ZEXIT 8
277#define ZFOREACH 9
278#define ZGOTO 10
279#define ZIF 11
280#define ZLABEL 12
281#define ZLET 13
282#define ZSET 14
283#define ZSWITCH 15
284#define ZTEST 16
285#define ZTHEN 17
286#define ZWHILE 18
287
288/*
289 * Structure defining the existing while/foreach loops at this
290 * source level. Loops are implemented by seeking back in the
291 * input. For foreach (fe), the word list is attached here.
292 */
293struct whyle {
294 off_t w_start; /* Point to restart loop */
295 off_t w_end; /* End of loop (0 if unknown) */
296 char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
297 char *w_fename; /* Name for fe */
298 struct whyle *w_next; /* Next (more outer) loop */
299} *whyles;
300
301/*
302 * Variable structure
303 *
35371dec 304 * Aliases and variables are stored in AVL balanced binary trees.
8c95f417
BJ
305 */
306struct varent {
307 char **vec; /* Array of words which is the value */
35371dec
EW
308 char *v_name; /* Name of variable/alias */
309 struct varent *v_link[3]; /* The links, see below */
310 int v_bal; /* Balance factor */
8c95f417 311} shvhed, aliases;
35371dec
EW
312#define v_left v_link[0]
313#define v_right v_link[1]
314#define v_parent v_link[2]
315
316struct varent *adrof1();
317#define adrof(v) adrof1(v, &shvhed)
318#define value(v) value1(v, &shvhed)
8c95f417
BJ
319
320/*
321 * The following are for interfacing redo substitution in
322 * aliases to the lexical routines.
323 */
324struct wordent *alhistp; /* Argument list (first) */
325struct wordent *alhistt; /* Node after last in arg list */
326char **alvec; /* The (remnants of) alias vector */
327
328/*
329 * Filename/command name expansion variables
330 */
331short gflag; /* After tglob -> is globbing needed? */
332
333/*
334 * A reasonable limit on number of arguments would seem to be
335 * the maximum number of characters in an arg list / 6.
336 */
337#define GAVSIZ NCARGS / 6
338
339/*
340 * Variables for filename expansion
341 */
342char **gargv; /* Pointer to the (stack) arglist */
343short gargc; /* Number args in gargv */
8c95f417
BJ
344
345/*
346 * Variables for command expansion.
347 */
348char **pargv; /* Pointer to the argv list space */
349char *pargs; /* Pointer to start current word */
350short pargc; /* Count of arguments in pargv */
351short pnleft; /* Number of chars left in pargs */
352char *pargcp; /* Current index into pargs */
353
354/*
355 * History list
356 *
357 * Each history list entry contains an embedded wordlist
358 * from the scanner, a number for the event, and a reference count
359 * to aid in discarding old entries.
360 *
361 * Essentially "invisible" entries are put on the history list
362 * when history substitution includes modifiers, and thrown away
363 * at the next discarding since their event numbers are very negative.
364 */
365struct Hist {
366 struct wordent Hlex;
367 int Hnum;
368 int Href;
369 struct Hist *Hnext;
370} Histlist;
371
372struct wordent paraml; /* Current lexical word list */
373int eventno; /* Next events number */
374int lastev; /* Last event reference (default) */
375
376char HIST; /* history invocation character */
377char HISTSUB; /* auto-substitute character */
378
35371dec
EW
379/*
380 * In lines for frequently called functions
381 */
382#define XFREE(cp) { \
383 extern char end[]; \
384 char stack; \
385 if ((cp) >= end && (cp) < &stack) \
386 free(cp); \
387}
388char *alloctmp;
389#define xalloc(i) ((alloctmp = malloc(i)) ? alloctmp : (char *)nomem(i))
3d31e3bf 390#define xrealloc(p, i) ((alloctmp = realloc(p, i)) ? alloctmp : (char *)nomem(i))
35371dec 391
8c95f417 392char *Dfix1();
8c95f417
BJ
393char **blkcat();
394char **blkcpy();
395char **blkend();
396char **blkspl();
397char *calloc();
35371dec 398char *malloc();
3d31e3bf 399char *realloc();
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();
3d31e3bf 420char **globall();
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[];