1 /***********************************************************************
2  *                                                                      *
3  *               This software is part of the ast package               *
4  *          Copyright (c) 1982-2013 AT&T Intellectual Property          *
5  *                      and is licensed under the                       *
6  *                 Eclipse Public License, Version 1.0                  *
7  *                    by AT&T Intellectual Property                     *
8  *                                                                      *
9  *                A copy of the License is available at                 *
10  *          http://www.eclipse.org/org/documents/epl-v10.html           *
11  *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12  *                                                                      *
13  *              Information and Software Systems Research               *
14  *                            AT&T Research                             *
15  *                           Florham Park NJ                            *
16  *                                                                      *
17  *                    David Korn <dgkorn@gmail.com>                     *
18  *                                                                      *
19  ***********************************************************************/
20 //
21 // UNIX shell
22 // Written by David Korn
23 //
24 #ifndef _SHNODES_H
25 #define _SHNODES_H 1
26 
27 #include "argnod.h"
28 #include "shell.h"
29 
30 // Command tree for tretyp.
31 #define FINT (02 << COMBITS)         // non-interruptable
32 #define FAMP (04 << COMBITS)         // background
33 #define FPIN (010 << COMBITS)        // input is a pipe
34 #define FPOU (040 << COMBITS)        // output is a pipe
35 #define FPCL (0100 << COMBITS)       // close the pipe
36 #define FCOOP (0200 << COMBITS)      // cooperating process
37 #define FSHOWME (0400 << COMBITS)    // set for showme commands
38 #define FALTPIPE (02000 << COMBITS)  // alternate pipes &|
39 #define FPOSIX (02 << COMBITS)       // posix semantics function
40 #define FLINENO (04 << COMBITS)      // for/case has line number
41 #define FSHVALUE (0100 << COMBITS)   // function set .sh.value
42 #define FOPTGET (0200 << COMBITS)    // function calls getopts
43 
44 #define TNEGATE (01 << COMBITS)  // ! inside [[...]]
45 #define TBINARY (02 << COMBITS)  // binary operator in [[...]]
46 #define TUNARY (04 << COMBITS)   // unary operator in [[...]]
47 #define TTEST (010 << COMBITS)
48 #define TPAREN (TBINARY | TUNARY)
49 #define TSHIFT (COMBITS + 4)
50 #define TNSPACE (TFUN | COMSCAN)
51 
52 // These values can be no larger than four bits per `COMBITS` and `COMMSK`.
53 // They are the least significant four bits of the trenod.tretyp.
54 #define TCOM 0
55 #define TPAR 1
56 #define TFIL 2
57 #define TLST 3
58 #define TIF 4
59 #define TWH 5
60 #define TTST 6
61 #define TSW 7
62 #define TAND 8
63 #define TORF 9
64 #define TFORK 10
65 #define TFOR 11
66 #define TARITH 12
67 #define TTIME 13
68 #define TSETIO 14
69 #define TFUN 15
70 // These symbols are special-case variations of command types above.
71 #define TUN (TWH | COMSCAN)
72 #define TSELECT (TFOR | COMSCAN)
73 
74 // This node is a proforma for those that follow.
75 struct trenod {
76     int tretyp;
77     struct ionod *treio;
78 };
79 
80 struct forknod {
81     int forktyp;
82     struct ionod *forkio;
83     Shnode_t *forktre;
84     int64_t forkline;
85 };
86 
87 struct ifnod {
88     int iftyp;
89     Shnode_t *iftre;
90     Shnode_t *thtre;
91     Shnode_t *eltre;
92 };
93 
94 struct whnod {
95     int whtyp;
96     Shnode_t *whtre;
97     Shnode_t *dotre;
98     struct arithnod *whinc;
99 };
100 
101 struct fornod {
102     int fortyp;
103     char *fornam;
104     Shnode_t *fortre;
105     struct comnod *forlst;
106     int64_t forline;
107 };
108 
109 struct swnod {
110     int swtyp;
111     struct argnod *swarg;
112     struct regnod *swlst;
113     struct ionod *swio;
114     int64_t swline;
115 };
116 
117 struct regnod {
118     struct argnod *regptr;
119     Shnode_t *regcom;
120     struct regnod *regnxt;
121     char regflag;
122 };
123 
124 struct parnod {
125     int partyp;
126     Shnode_t *partre;
127 };
128 
129 struct lstnod {
130     int lsttyp;
131     Shnode_t *lstlef;
132     Shnode_t *lstrit;
133 };
134 
135 // tstnod is same as lstnod, but with extra field for line number.
136 struct tstnod {
137     struct lstnod tstlst;
138     int64_t tstline;
139 };
140 
141 struct functnod {
142     int functtyp;
143     char *functnam;
144     Shnode_t *functtre;
145     int64_t functline;
146     off_t functloc;
147     struct slnod *functstak;
148     struct comnod *functargs;
149 };
150 
151 struct arithnod {
152     int artyp;
153     struct argnod *arexpr;
154     void *arcomp;
155     int64_t arline;
156 };
157 
158 // Types of ionodes stored in iofile.
159 #define IOUFD 0x3f         // file descriptor number mask
160 #define IOPUT 0x40         // > redirection operator
161 #define IOAPP 0x80         // >> redirection operator
162 #define IODOC 0x100        // << redirection operator
163 #define IOMOV 0x200        // <& or >& operators
164 #define IOCLOB 0x400       // noclobber bit
165 #define IORDW 0x800        // <> redirection operator
166 #define IORAW 0x1000       // no expansion needed for filename
167 #define IOSTRG 0x2000      // here-document stored as incore string
168 #define IOSTRIP 0x4000     // strip leading tabs for here-document
169 #define IOQUOTE 0x8000     // here-document delimiter was quoted
170 #define IOVNM 0x10000      // iovname field is non-zero
171 #define IOLSEEK 0x20000    // seek operators <# or >#
172 #define IOARITH 0x40000    // arithmetic seek <# ((expr))
173 #define IOREWRITE 0x80000  // arithmetic seek <# ((expr))
174 #define IOCOPY IOCLOB      // copy skipped lines onto standard output
175 #define IOPROCSUB IOARITH  // process substitution redirection
176 
177 union Shnode_u {
178     struct argnod arg;
179     struct ionod io;
180     struct whnod wh;
181     struct swnod sw;
182     struct ifnod if_;
183     struct dolnod dol;
184     struct comnod com;
185     struct trenod tre;
186     struct forknod fork;
187     struct fornod for_;
188     struct regnod reg;
189     struct parnod par;
190     struct lstnod lst;
191     struct tstnod tst;
192     struct functnod funct;
193     struct arithnod ar;
194 };
195 
196 extern void sh_freeup(Shell_t *);
197 extern void sh_funstaks(struct slnod *, int);
198 extern Sfio_t *sh_subshell(Shell_t *, Shnode_t *, volatile int, int);
199 extern int sh_tdump(Sfio_t *, const Shnode_t *);
200 extern Shnode_t *sh_trestore(Shell_t *, Sfio_t *);
201 
202 #endif  // _SHNODES_H
203