source: trunk/minix/commands/sh/sh.h@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 10.2 KB
Line 
1#include <stdlib.h>
2#include <string.h>
3#include <unistd.h>
4#include <fcntl.h>
5
6/* Need a way to have void used for ANSI, nothing for K&R. */
7#ifndef _ANSI
8#undef _VOID
9#define _VOID
10#endif
11
12/* -------- sh.h -------- */
13/*
14 * shell
15 */
16
17#define LINELIM 4096
18#define NPUSH 8 /* limit to input nesting */
19
20#define NOFILE 20 /* Number of open files */
21#define NUFILE 10 /* Number of user-accessible files */
22#define FDBASE 10 /* First file usable by Shell */
23
24/*
25 * values returned by wait
26 */
27#define WAITSIG(s) ((s)&0177)
28#define WAITVAL(s) (((s)>>8)&0377)
29#define WAITCORE(s) (((s)&0200)!=0)
30
31/*
32 * library and system defintions
33 */
34#ifdef __STDC__
35typedef void xint; /* base type of jmp_buf, for not broken compilers */
36#else
37typedef char * xint; /* base type of jmp_buf, for broken compilers */
38#endif
39
40/*
41 * shell components
42 */
43/* #include "area.h" */
44/* #include "word.h" */
45/* #include "io.h" */
46/* #include "var.h" */
47
48#define QUOTE 0200
49
50#define NOBLOCK ((struct op *)NULL)
51#define NOWORD ((char *)NULL)
52#define NOWORDS ((char **)NULL)
53#define NOPIPE ((int *)NULL)
54
55/*
56 * Description of a command or an operation on commands.
57 * Might eventually use a union.
58 */
59struct op {
60 int type; /* operation type, see below */
61 char **words; /* arguments to a command */
62 struct ioword **ioact; /* IO actions (eg, < > >>) */
63 struct op *left;
64 struct op *right;
65 char *str; /* identifier for case and for */
66};
67
68#define TCOM 1 /* command */
69#define TPAREN 2 /* (c-list) */
70#define TPIPE 3 /* a | b */
71#define TLIST 4 /* a [&;] b */
72#define TOR 5 /* || */
73#define TAND 6 /* && */
74#define TFOR 7
75#define TDO 8
76#define TCASE 9
77#define TIF 10
78#define TWHILE 11
79#define TUNTIL 12
80#define TELIF 13
81#define TPAT 14 /* pattern in case */
82#define TBRACE 15 /* {c-list} */
83#define TASYNC 16 /* c & */
84
85/*
86 * actions determining the environment of a process
87 */
88#define BIT(i) (1<<(i))
89#define FEXEC BIT(0) /* execute without forking */
90
91/*
92 * flags to control evaluation of words
93 */
94#define DOSUB 1 /* interpret $, `, and quotes */
95#define DOBLANK 2 /* perform blank interpretation */
96#define DOGLOB 4 /* interpret [?* */
97#define DOKEY 8 /* move words with `=' to 2nd arg. list */
98#define DOTRIM 16 /* trim resulting string */
99
100#define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
101
102Extern char **dolv;
103Extern int dolc;
104Extern int exstat;
105Extern char gflg;
106Extern int talking; /* interactive (talking-type wireless) */
107Extern int execflg;
108Extern int multiline; /* \n changed to ; */
109Extern struct op *outtree; /* result from parser */
110
111Extern xint *failpt;
112Extern xint *errpt;
113
114struct brkcon {
115 jmp_buf brkpt;
116 struct brkcon *nextlev;
117} ;
118Extern struct brkcon *brklist;
119Extern int isbreak;
120
121/*
122 * redirection
123 */
124struct ioword {
125 short io_unit; /* unit affected */
126 short io_flag; /* action (below) */
127 char *io_name; /* file name */
128};
129#define IOREAD 1 /* < */
130#define IOHERE 2 /* << (here file) */
131#define IOWRITE 4 /* > */
132#define IOCAT 8 /* >> */
133#define IOXHERE 16 /* ${}, ` in << */
134#define IODUP 32 /* >&digit */
135#define IOCLOSE 64 /* >&- */
136
137#define IODEFAULT (-1) /* token for default IO unit */
138
139Extern struct wdblock *wdlist;
140Extern struct wdblock *iolist;
141
142/*
143 * parsing & execution environment
144 */
145extern struct env {
146 char *linep;
147 struct io *iobase;
148 struct io *iop;
149 xint *errpt;
150 int iofd;
151 struct env *oenv;
152} e;
153
154/*
155 * flags:
156 * -e: quit on error
157 * -k: look for name=value everywhere on command line
158 * -n: no execution
159 * -t: exit after reading and executing one command
160 * -v: echo as read
161 * -x: trace
162 * -u: unset variables net diagnostic
163 */
164extern char *flag;
165
166extern char *null; /* null value for variable */
167extern int intr; /* interrupt pending */
168
169Extern char *trap[_NSIG+1];
170Extern char ourtrap[_NSIG+1];
171Extern int trapset; /* trap pending */
172
173extern int heedint; /* heed interrupt signals */
174
175Extern int yynerrs; /* yacc */
176
177Extern char line[LINELIM];
178extern char *elinep;
179
180/*
181 * other functions
182 */
183#ifdef __STDC__
184int (*inbuilt(char *s ))(void);
185#else
186int (*inbuilt())();
187#endif
188_PROTOTYPE(char *rexecve , (char *c , char **v , char **envp ));
189_PROTOTYPE(char *space , (int n ));
190_PROTOTYPE(char *strsave , (char *s , int a ));
191_PROTOTYPE(char *evalstr , (char *cp , int f ));
192_PROTOTYPE(char *putn , (int n ));
193_PROTOTYPE(char *itoa , (unsigned u , int n ));
194_PROTOTYPE(char *unquote , (char *as ));
195_PROTOTYPE(struct var *lookup , (char *n ));
196_PROTOTYPE(int rlookup , (char *n ));
197_PROTOTYPE(struct wdblock *glob , (char *cp , struct wdblock *wb ));
198_PROTOTYPE(int subgetc , (int ec , int quoted ));
199_PROTOTYPE(char **makenv , (void));
200_PROTOTYPE(char **eval , (char **ap , int f ));
201_PROTOTYPE(int setstatus , (int s ));
202_PROTOTYPE(int waitfor , (int lastpid , int canintr ));
203
204_PROTOTYPE(void onintr , (int s )); /* SIGINT handler */
205
206_PROTOTYPE(int newenv , (int f ));
207_PROTOTYPE(void quitenv , (void));
208_PROTOTYPE(void err , (char *s ));
209_PROTOTYPE(int anys , (char *s1 , char *s2 ));
210_PROTOTYPE(int any , (int c , char *s ));
211_PROTOTYPE(void next , (int f ));
212_PROTOTYPE(void setdash , (void));
213_PROTOTYPE(void onecommand , (void));
214_PROTOTYPE(void runtrap , (int i ));
215_PROTOTYPE(void xfree , (char *s ));
216_PROTOTYPE(int letter , (int c ));
217_PROTOTYPE(int digit , (int c ));
218_PROTOTYPE(int letnum , (int c ));
219_PROTOTYPE(int gmatch , (char *s , char *p ));
220
221/*
222 * error handling
223 */
224_PROTOTYPE(void leave , (void)); /* abort shell (or fail in subshell) */
225_PROTOTYPE(void fail , (void)); /* fail but return to process next command */
226_PROTOTYPE(void warn , (char *s ));
227_PROTOTYPE(void sig , (int i )); /* default signal handler */
228
229/* -------- var.h -------- */
230
231struct var {
232 char *value;
233 char *name;
234 struct var *next;
235 char status;
236};
237#define COPYV 1 /* flag to setval, suggesting copy */
238#define RONLY 01 /* variable is read-only */
239#define EXPORT 02 /* variable is to be exported */
240#define GETCELL 04 /* name & value space was got with getcell */
241
242Extern struct var *vlist; /* dictionary */
243
244Extern struct var *homedir; /* home directory */
245Extern struct var *prompt; /* main prompt */
246Extern struct var *cprompt; /* continuation prompt */
247Extern struct var *path; /* search path for commands */
248Extern struct var *shell; /* shell to interpret command files */
249Extern struct var *ifs; /* field separators */
250
251_PROTOTYPE(int yyparse , (void));
252_PROTOTYPE(struct var *lookup , (char *n ));
253_PROTOTYPE(void setval , (struct var *vp , char *val ));
254_PROTOTYPE(void nameval , (struct var *vp , char *val , char *name ));
255_PROTOTYPE(void export , (struct var *vp ));
256_PROTOTYPE(void ronly , (struct var *vp ));
257_PROTOTYPE(int isassign , (char *s ));
258_PROTOTYPE(int checkname , (char *cp ));
259_PROTOTYPE(int assign , (char *s , int cf ));
260_PROTOTYPE(void putvlist , (int f , int out ));
261_PROTOTYPE(int eqname , (char *n1 , char *n2 ));
262
263_PROTOTYPE(int execute , (struct op *t , int *pin , int *pout , int act ));
264
265/* -------- io.h -------- */
266/* io buffer */
267struct iobuf {
268 unsigned id; /* buffer id */
269 char buf[512]; /* buffer */
270 char *bufp; /* pointer into buffer */
271 char *ebufp; /* pointer to end of buffer */
272};
273
274/* possible arguments to an IO function */
275struct ioarg {
276 char *aword;
277 char **awordlist;
278 int afile; /* file descriptor */
279 unsigned afid; /* buffer id */
280 long afpos; /* file position */
281 struct iobuf *afbuf; /* buffer for this file */
282};
283Extern struct ioarg ioargstack[NPUSH];
284#define AFID_NOBUF (~0)
285#define AFID_ID 0
286
287/* an input generator's state */
288struct io {
289 int (*iofn)(_VOID);
290 struct ioarg *argp;
291 int peekc;
292 char prev; /* previous character read by readc() */
293 char nlcount; /* for `'s */
294 char xchar; /* for `'s */
295 char task; /* reason for pushed IO */
296};
297Extern struct io iostack[NPUSH];
298#define XOTHER 0 /* none of the below */
299#define XDOLL 1 /* expanding ${} */
300#define XGRAVE 2 /* expanding `'s */
301#define XIO 3 /* file IO */
302
303/* in substitution */
304#define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
305
306/*
307 * input generators for IO structure
308 */
309_PROTOTYPE(int nlchar , (struct ioarg *ap ));
310_PROTOTYPE(int strchar , (struct ioarg *ap ));
311_PROTOTYPE(int qstrchar , (struct ioarg *ap ));
312_PROTOTYPE(int filechar , (struct ioarg *ap ));
313_PROTOTYPE(int herechar , (struct ioarg *ap ));
314_PROTOTYPE(int linechar , (struct ioarg *ap ));
315_PROTOTYPE(int gravechar , (struct ioarg *ap , struct io *iop ));
316_PROTOTYPE(int qgravechar , (struct ioarg *ap , struct io *iop ));
317_PROTOTYPE(int dolchar , (struct ioarg *ap ));
318_PROTOTYPE(int wdchar , (struct ioarg *ap ));
319_PROTOTYPE(void scraphere , (void));
320_PROTOTYPE(void freehere , (int area ));
321_PROTOTYPE(void gethere , (void));
322_PROTOTYPE(void markhere , (char *s , struct ioword *iop ));
323_PROTOTYPE(int herein , (char *hname , int xdoll ));
324_PROTOTYPE(int run , (struct ioarg *argp , int (*f)(_VOID)));
325
326/*
327 * IO functions
328 */
329_PROTOTYPE(int eofc , (void));
330_PROTOTYPE(int getc , (int ec ));
331_PROTOTYPE(int readc , (void));
332_PROTOTYPE(void unget , (int c ));
333_PROTOTYPE(void ioecho , (int c ));
334_PROTOTYPE(void prs , (char *s ));
335_PROTOTYPE(void putc , (int c ));
336_PROTOTYPE(void prn , (unsigned u ));
337_PROTOTYPE(void closef , (int i ));
338_PROTOTYPE(void closeall , (void));
339
340/*
341 * IO control
342 */
343_PROTOTYPE(void pushio , (struct ioarg *argp , int (*fn)(_VOID)));
344_PROTOTYPE(int remap , (int fd ));
345_PROTOTYPE(int openpipe , (int *pv ));
346_PROTOTYPE(void closepipe , (int *pv ));
347_PROTOTYPE(struct io *setbase , (struct io *ip ));
348
349extern struct ioarg temparg; /* temporary for PUSHIO */
350#define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
351#define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
352
353/* -------- word.h -------- */
354#ifndef WORD_H
355#define WORD_H 1
356struct wdblock {
357 short w_bsize;
358 short w_nword;
359 /* bounds are arbitrary */
360 char *w_words[1];
361};
362
363_PROTOTYPE(struct wdblock *addword , (char *wd , struct wdblock *wb ));
364_PROTOTYPE(struct wdblock *newword , (int nw ));
365_PROTOTYPE(char **getwords , (struct wdblock *wb ));
366#endif
367
368/* -------- area.h -------- */
369
370/*
371 * storage allocation
372 */
373_PROTOTYPE(char *getcell , (unsigned nbytes ));
374_PROTOTYPE(void garbage , (void));
375_PROTOTYPE(void setarea , (char *cp , int a ));
376_PROTOTYPE(int getarea , (char *cp ));
377_PROTOTYPE(void freearea , (int a ));
378_PROTOTYPE(void freecell , (char *cp ));
379
380Extern int areanum; /* current allocation area */
381
382#define NEW(type) (type *)getcell(sizeof(type))
383#define DELETE(obj) freecell((char *)obj)
Note: See TracBrowser for help on using the repository browser.