xref: /netbsd-src/bin/sh/parser.h (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: parser.h,v 1.28 2019/02/13 21:40:50 kre Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)parser.h	8.3 (Berkeley) 5/4/95
35  */
36 
37 /* control characters in argument strings */
38 #define CTL_FIRST '\201'	/* first 'special' character */
39 #define CTLESC '\201'		/* escape next character */
40 #define CTLVAR '\202'		/* variable defn */
41 #define CTLENDVAR '\203'
42 #define CTLBACKQ '\204'
43 #define CTLQUOTE 01		/* ored with CTLBACKQ code if in quotes */
44 /*	CTLBACKQ | CTLQUOTE == '\205' */
45 #define	CTLARI	'\206'		/* arithmetic expression */
46 #define	CTLENDARI '\207'
47 #define	CTLQUOTEMARK '\210'
48 #define	CTLQUOTEEND '\211'	/* only inside ${...} */
49 #define	CTLNONL '\212'		/* The \n in a deleted \ \n sequence */
50 			/* pure concidence that (CTLNONL & 0x7f) == '\n' */
51 #define	CTLCNL	'\213'		/* A $'\n' - newline not counted */
52 #define	CTL_LAST '\213'		/* last 'special' character */
53 
54 /* variable substitution byte (follows CTLVAR) */
55 #define VSTYPE		0x0f	/* type of variable substitution */
56 #define VSNUL		0x10	/* colon--treat the empty string as unset */
57 #define VSLINENO	0x20	/* expansion of $LINENO, the line number
58 				   follows immediately */
59 #define VSPATQ		0x40	/* ensure correct pattern quoting in ${x#pat} */
60 #define VSQUOTE	 	0x80	/* inside double quotes--suppress splitting */
61 
62 /* values of VSTYPE field */
63 #define VSNORMAL	0x1		/* normal variable:  $var or ${var} */
64 #define VSMINUS		0x2		/* ${var-text} */
65 #define VSPLUS		0x3		/* ${var+text} */
66 #define VSQUESTION	0x4		/* ${var?message} */
67 #define VSASSIGN	0x5		/* ${var=text} */
68 #define VSTRIMLEFT	0x6		/* ${var#pattern} */
69 #define VSTRIMLEFTMAX	0x7		/* ${var##pattern} */
70 #define VSTRIMRIGHT	0x8		/* ${var%pattern} */
71 #define VSTRIMRIGHTMAX 	0x9		/* ${var%%pattern} */
72 #define VSLENGTH	0xa		/* ${#var} */
73 
74 union node *parsecmd(int);
75 void fixredir(union node *, const char *, int);
76 int goodname(const char *);
77 int isassignment(const char *);
78 const char *getprompt(void *);
79 const char *expandstr(char *, int);
80 const char *expandenv(char *);
81 
82 struct HereDoc;
83 union node;
84 struct nodelist;
85 
86 struct parse_state {
87 	struct HereDoc *ps_heredoclist;	/* list of here documents to read */
88 	int ps_parsebackquote;		/* nonzero inside backquotes */
89 	int ps_doprompt;		/* if set, prompt the user */
90 	int ps_needprompt;		/* true if interactive at line start */
91 	int ps_lasttoken;		/* last token read */
92 	int ps_tokpushback;		/* last token pushed back */
93 	char *ps_wordtext;	/* text of last word returned by readtoken */
94 	int ps_checkkwd;		/* word expansion flags, see below */
95 	struct nodelist *ps_backquotelist; /* list of cmdsubs to process */
96 	union node *ps_redirnode;	/* node for current redirect */
97 	struct HereDoc *ps_heredoc;	/* current heredoc << being parsed */
98 	int ps_quoteflag;		/* set if (part) of token was quoted */
99 	int ps_startlinno;		/* line # where last token started */
100 	int ps_funclinno;		/* line # of the current function */
101 	int ps_elided_nl;		/* count of \ \n pairs we have seen */
102 };
103 
104 /*
105  * The parser references the elements of struct parse_state quite
106  * frequently - they used to be simple globals, so one memory ref
107  * per access, adding an indirect through a global ptr would not be
108  * nice.   The following gross hack allows most of that cost to be
109  * avoided, by allowing the compiler to understand that the global
110  * pointer is in fact constant in any function, and so its value can
111  * be cached, rather than needing to be fetched every time in case
112  * some other called function has changed it.
113  *
114  * The rule to make this work is that any function that wants
115  * to alter the global must restore it before it returns (and thus
116  * must have an error trap handler).  That means that the struct
117  * used for the new parser state can be a local in that function's
118  * stack frame, it never needs to be malloc'd.
119  */
120 
121 union parse_state_p {
122 	struct parse_state *const	c_current_parser;
123 	struct parse_state *		v_current_parser;
124 };
125 
126 extern union parse_state_p psp;
127 
128 #define	current_parser (psp.c_current_parser)
129 
130 /*
131  * Perhaps one day emulate "static" by moving most of these definitions into
132  * parser.c ...  (only checkkwd & tokpushback are used outside parser.c,
133  * and only in init.c as a RESET activity)
134  */
135 #define	tokpushback	(current_parser->ps_tokpushback)
136 #define	checkkwd	(current_parser->ps_checkkwd)
137 
138 #define	heredoclist	(current_parser->ps_heredoclist)
139 #define	parsebackquote	(current_parser->ps_parsebackquote)
140 #define	doprompt	(current_parser->ps_doprompt)
141 #define	needprompt	(current_parser->ps_needprompt)
142 #define	lasttoken	(current_parser->ps_lasttoken)
143 #define	wordtext	(current_parser->ps_wordtext)
144 #define	backquotelist	(current_parser->ps_backquotelist)
145 #define	redirnode	(current_parser->ps_redirnode)
146 #define	heredoc		(current_parser->ps_heredoc)
147 #define	quoteflag	(current_parser->ps_quoteflag)
148 #define	startlinno	(current_parser->ps_startlinno)
149 #define	funclinno	(current_parser->ps_funclinno)
150 #define	elided_nl	(current_parser->ps_elided_nl)
151 
152 /*
153  * Values that can be set in checkkwd
154  */
155 #define CHKKWD		0x01		/* turn word into keyword (if it is) */
156 #define CHKNL		0x02		/* ignore leading \n's */
157 #define CHKALIAS	0x04		/* lookup words as aliases and ... */
158 
159 /*
160  * NEOF is returned by parsecmd when it encounters an end of file.  It
161  * must be distinct from NULL, so we use the address of a variable that
162  * happens to be handy.
163  */
164 #define NEOF ((union node *)&psp)
165 
166 #ifdef DEBUG
167 extern int parsing;
168 #endif
169