xref: /plan9-contrib/sys/src/cmd/rc/rc.h (revision c6df144405f586b73992827d584728dc975dff14)
1 /*
2  * Assume plan 9 by default; if Unix is defined, assume unix.
3  * Please don't litter the code with ifdefs.  The three below should be enough.
4  */
5 
6 typedef struct tree tree;
7 typedef struct word word;
8 typedef struct io io;
9 typedef union code code;
10 typedef struct var var;
11 typedef struct list list;
12 typedef struct redir redir;
13 typedef struct thread thread;
14 typedef struct builtin builtin;
15 
16 #define isdigit(c) ((c) >= '0' && (c) <= '9')	/* NB: unsafe macro */
17 
18 #ifndef Unix
19 /* plan 9 */
20 #include <u.h>
21 #include <libc.h>
22 
23 #pragma incomplete word
24 #pragma incomplete io
25 
26 /* unix compatibility */
27 #define unixclsexec(wdirfd)
28 
29 #define	NSIG	32
30 #define	SIGINT	2
31 #define	SIGQUIT	3
32 #else
33 #include "unix.h"
34 #endif
35 
36 #define	YYMAXDEPTH	500
37 #ifndef YYPREFIX
38 #ifndef PAREN
39 #include "x.tab.h"
40 #endif
41 #endif
42 
43 struct tree{
44 	int	type;
45 	int	rtype, fd0, fd1;	/* details of REDIR PIPE DUP tokens */
46 	char	*str;
47 	int	quoted;
48 	int	iskw;
49 	tree	*child[3];
50 	tree	*next;
51 };
52 tree *newtree(void);
53 tree *token(char*, int), *klook(char*), *tree1(int, tree*);
54 tree *tree2(int, tree*, tree*), *tree3(int, tree*, tree*, tree*);
55 tree *mung1(tree*, tree*), *mung2(tree*, tree*, tree*);
56 tree *mung3(tree*, tree*, tree*, tree*), *epimung(tree*, tree*);
57 tree *simplemung(tree*), *heredoc(tree*);
58 void freetree(tree*);
59 tree *cmdtree;
60 
61 /*
62  * The first word of any code vector is a reference count.
63  * Always create a new reference to a code vector by calling codecopy(.).
64  * Always call codefree(.) when deleting a reference.
65  */
66 union code{
67 	void	(*f)(void);
68 	int	i;
69 	char	*s;
70 };
71 
72 char *promptstr;
73 int doprompt;
74 
75 #define	NTOK	8192		/* maximum bytes in a word (token) */
76 
77 char tok[NTOK + UTFmax];
78 
79 #define	APPEND	1
80 #define	WRITE	2
81 #define	READ	3
82 #define	HERE	4
83 #define	DUPFD	5
84 #define	CLOSE	6
85 #define RDWR	7
86 
87 struct var{
88 	char	*name;		/* ascii name */
89 	word	*val;		/* value */
90 	int	changed;
91 	code	*fn;		/* pointer to function's code vector */
92 	int	fnchanged;
93 	int	pc;		/* pc of start of function */
94 	var	*next;		/* next on hash or local list */
95 };
96 var *vlook(char*), *gvlook(char*), *newvar(char*, var*);
97 
98 #define	NVAR	521
99 
100 var *gvar[NVAR];		/* hash for globals */
101 
102 #define	new(type)	((type *)emalloc(sizeof(type)))
103 
104 void *emalloc(long);
105 void *Malloc(ulong);
106 void efree(void *);
107 
108 struct here{
109 	tree	*tag;
110 	char	*name;
111 	struct here *next;
112 };
113 int mypid;
114 
115 /*
116  * Glob character escape in strings:
117  *	In a string, GLOB must be followed by *?[ or GLOB.
118  *	GLOB* matches any string
119  *	GLOB? matches any single character
120  *	GLOB[...] matches anything in the brackets
121  *	GLOBGLOB matches GLOB
122  */
123 #define	GLOB	'\001'
124 
125 char **argp;
126 char **args;
127 int nerror;		/* number of errors encountered during compilation */
128 int doprompt;		/* is it time for a prompt? */
129 /*
130  * Which fds are the reading/writing end of a pipe?
131  * Unfortunately, this can vary from system to system.
132  * 9th edition Unix doesn't care, the following defines
133  * work on plan 9.
134  */
135 #define	PRD	0
136 #define	PWR	1
137 
138 char *Rcmain, *Fdprefix;
139 /*
140  * How many dot commands have we executed?
141  * Used to ensure that -v flag doesn't print rcmain.
142  */
143 int ndot;
144 char *getstatus(void);
145 Rune lastc;
146 int lastword;
147 
148 #define	NFLAG	128		/* limited to ascii */
149 
150 extern char *flag[NFLAG];
151 extern char flagset[];
152