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 // These are the definitions for the lexical analyzer.
25 //
26 #ifndef _SHLEX_H
27 #define _SHLEX_H 1
28 
29 #include "cdt.h"
30 #include "lexstates.h"
31 #include "shell.h"
32 #include "shnodes.h"
33 #include "shtable.h"
34 
35 struct lexdata {
36     char nocopy;
37     char paren;
38     char dolparen;
39     char nest;
40     char docword;
41     char nested_tilde;
42     char *docend;
43     char noarg;
44     char balance;
45     char warn;
46     char message;
47     char arith;
48     char *first;
49     int level;
50     int lastc;
51     int lex_max;
52     int *lex_match;
53     int lex_state;
54     int docextra;
55     off_t kiaoff;
56 };
57 
58 //
59 // This structure allows for arbitrary depth nesting of (...), {...}, [...].
60 //
61 struct lexstate {
62     char incase;       // 1 for case pattern, 2 after case
63     char intest;       // 1 inside [[...]]
64     char testop1;      // 1 when unary test op legal
65     char testop2;      // 1 when binary test op legal
66     char reservok;     // >0 for reserved word legal
67     char skipword;     // next word can't be reserved
68     char last_quote;   // last multi-line quote character
69     char nestedbrace;  // ${var op {...}}
70 };
71 
72 struct _shlex_ {
73     Shell_t *sh;            // pointer to the interpreter
74     struct argnod *arg;     // current word
75     struct ionod *heredoc;  // pending here document list
76     int token;              // current token number
77     int lastline;           // last line number
78     int lasttok;            // previous token number
79     int digits;             // numerical value with word token
80     int nonstandard;        // nonstandard construct in profile
81     char aliasok;           // on when alias is legal
82     char assignok;          // on when name=value is legal
83     char inexec;            // on when processing exec
84     char intypeset;         // on when processing typeset
85     char comp_assign;       // in compound assignment
86     char comsub;            // parsing command substitution
87     char noreserv;          // reserved works not legal
88     char typed;             // possible type definition on PATH
89     int inlineno;           // saved value of sh.inlineno
90     int firstline;          // saved value of sh.st.firstline
91     int assignlevel;        // nesting level for assignment
92     short fundepth;         // nesting level for functions
93     Sfio_t *kiafile;        // kia output file
94     Sfio_t *kiatmp;         // kia reference file
95     unsigned long script;   // script entity number
96     unsigned long fscript;  // script file entity number
97     unsigned long current;  // current entity number
98     unsigned long unknown;  // <unknown> entity number
99     off_t kiabegin;         // offset of first entry
100     char *scriptname;       // name of script file
101     Dt_t *entity_tree;      // for entity ids
102     struct lexdata lexd;
103     struct lexstate lex;
104 };
105 
106 // Symbols for parsing.
107 #define NL '\n'
108 #define NOTSYM '!'
109 #define SYMRES 0400  // reserved word symbols
110 #define DOSYM (SYMRES | 01)
111 #define FISYM (SYMRES | 02)
112 #define ELIFSYM (SYMRES | 03)
113 #define ELSESYM (SYMRES | 04)
114 #define INSYM (SYMRES | 05)
115 #define THENSYM (SYMRES | 06)
116 #define DONESYM (SYMRES | 07)
117 #define ESACSYM (SYMRES | 010)
118 #define IFSYM (SYMRES | 011)
119 #define FORSYM (SYMRES | 012)
120 #define WHILESYM (SYMRES | 013)
121 #define UNTILSYM (SYMRES | 014)
122 #define CASESYM (SYMRES | 015)
123 #define FUNCTSYM (SYMRES | 016)
124 #define SELECTSYM (SYMRES | 017)
125 #define TIMESYM (SYMRES | 020)
126 #define NSPACESYM (SYMRES | 021)
127 
128 #define SYMREP 01000  // symbols for doubled characters
129 #define BREAKCASESYM (SYMREP | ';')
130 #define ANDFSYM (SYMREP | '&')
131 #define ORFSYM (SYMREP | '|')
132 #define IOAPPSYM (SYMREP | '>')
133 #define IODOCSYM (SYMREP | '<')
134 #define EXPRSYM (SYMREP | '(')
135 #define BTESTSYM (SYMREP | '[')
136 #define ETESTSYM (SYMREP | ']')
137 
138 #define SYMMASK 0170000
139 #define SYMPIPE 010000    // trailing '|'
140 #define SYMLPAR 020000    // trailing LPAREN
141 #define SYMAMP 040000     // trailing '&'
142 #define SYMGT 0100000     // trailing '>'
143 #define SYMSEMI 0110000   // trailing ';'
144 #define SYMSHARP 0120000  // trailing '#'
145 #define IOSEEKSYM (SYMSHARP | '<')
146 #define IOMOV0SYM (SYMAMP | '<')
147 #define IOMOV1SYM (SYMAMP | '>')
148 #define FALLTHRUSYM (SYMAMP | ';')
149 #define COOPSYM (SYMAMP | '|')
150 #define IORDWRSYM (SYMGT | '<')
151 #define IORDWRSYMT (SYMSEMI | '<')
152 #define IOCLOBSYM (SYMPIPE | '>')
153 #define IPROCSYM (SYMLPAR | '<')
154 #define OPROCSYM (SYMLPAR | '>')
155 #define EOFSYM 04000  // end-of-file
156 #define TESTUNOP 04001
157 #define TESTBINOP 04002
158 #define LABLSYM 04003
159 #define IOVNAME 04004
160 
161 // Additional parser flag, others in <shell.h>.
162 #define SH_EMPTY 04
163 #define SH_NOIO 010
164 #define SH_ASSIGN 020
165 #define SH_FUNDEF 040
166 #define SH_ARRAY 0100
167 #define SH_SEMI 0200  // semicolon after newline ok
168 
169 #define SH_COMPASSIGN 010  // allow compound assignments only
170 
171 extern const char e_unexpected[];
172 extern const char e_unmatched[];
173 extern const char e_endoffile[];
174 extern const char e_newline[];
175 
176 // Odd chars.
177 #define LBRACE '{'
178 #define RBRACE '}'
179 #define LPAREN '('
180 #define RPAREN ')'
181 #define LBRACT '['
182 #define RBRACT ']'
183 
184 extern int sh_lex(Lex_t *);
185 extern Shnode_t *sh_dolparen(Lex_t *);
186 extern Lex_t *sh_lexopen(Lex_t *, Shell_t *, int);
187 extern void sh_lexskip(Lex_t *, int, int, int);
188 extern __attribute__((noreturn)) void sh_syntax(Lex_t *);
189 extern int kiaclose(Lex_t *);
190 extern unsigned long kiaentity(Lex_t *, const char *, int, int, int, int, unsigned long, int, int,
191                                const char *);
192 
193 #endif  // _SHLEX_H
194