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