xref: /netbsd-src/bin/sh/parser.h (revision a7e8b4a5c84baeacaa93eecdd02a7533285bb4d4)
1*a7e8b4a5Skre /*	$NetBSD: parser.h,v 1.30 2024/10/21 15:57:45 kre Exp $	*/
249f0ad86Scgd 
361f28255Scgd /*-
437ed7877Sjtc  * Copyright (c) 1991, 1993
537ed7877Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Kenneth Almquist.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  *
3407bae7edSchristos  *	@(#)parser.h	8.3 (Berkeley) 5/4/95
3561f28255Scgd  */
3661f28255Scgd 
3761f28255Scgd /* control characters in argument strings */
38c02b3bbdSchristos #define CTL_FIRST '\201'	/* first 'special' character */
39c02b3bbdSchristos #define CTLESC '\201'		/* escape next character */
40c02b3bbdSchristos #define CTLVAR '\202'		/* variable defn */
4161f28255Scgd #define CTLENDVAR '\203'
4261f28255Scgd #define CTLBACKQ '\204'
4361f28255Scgd #define CTLQUOTE 01		/* ored with CTLBACKQ code if in quotes */
4437ed7877Sjtc /*	CTLBACKQ | CTLQUOTE == '\205' */
45c02b3bbdSchristos #define	CTLARI	'\206'		/* arithmetic expression */
4637ed7877Sjtc #define	CTLENDARI '\207'
471fbf0781Smycroft #define	CTLQUOTEMARK '\210'
48c6cbc16dSdsl #define	CTLQUOTEEND '\211'	/* only inside ${...} */
49727a69dcSkre #define	CTLNONL '\212'		/* The \n in a deleted \ \n sequence */
50727a69dcSkre 			/* pure concidence that (CTLNONL & 0x7f) == '\n' */
515f92382cSkre #define	CTLCNL	'\213'		/* A $'\n' - newline not counted */
52*a7e8b4a5Skre #define	CTLVARMOD '\214'	/* a modifier in a variable expansion */
53*a7e8b4a5Skre #define	CTL_LAST '\214'		/* last 'special' character */
5461f28255Scgd 
5561f28255Scgd /* variable substitution byte (follows CTLVAR) */
56ad8d5369Schristos #define VSTYPE		0x0f	/* type of variable substitution */
57ad8d5369Schristos #define VSNUL		0x10	/* colon--treat the empty string as unset */
58018a6f78Schristos #define VSLINENO	0x20	/* expansion of $LINENO, the line number
59018a6f78Schristos 				   follows immediately */
60606614c8Schristos #define VSPATQ		0x40	/* ensure correct pattern quoting in ${x#pat} */
61ad8d5369Schristos #define VSQUOTE	 	0x80	/* inside double quotes--suppress splitting */
6261f28255Scgd 
63*a7e8b4a5Skre /* values of VSTYPE field (nb: 0 reserved for "not determined yet") */
64ad8d5369Schristos #define VSNORMAL	0x1		/* normal variable:  $var or ${var} */
65ad8d5369Schristos #define VSMINUS		0x2		/* ${var-text} */
66ad8d5369Schristos #define VSPLUS		0x3		/* ${var+text} */
67ad8d5369Schristos #define VSQUESTION	0x4		/* ${var?message} */
68ad8d5369Schristos #define VSASSIGN	0x5		/* ${var=text} */
69ad8d5369Schristos #define VSTRIMLEFT	0x6		/* ${var#pattern} */
70ad8d5369Schristos #define VSTRIMLEFTMAX	0x7		/* ${var##pattern} */
71ad8d5369Schristos #define VSTRIMRIGHT	0x8		/* ${var%pattern} */
72ad8d5369Schristos #define VSTRIMRIGHTMAX 	0x9		/* ${var%%pattern} */
73ad8d5369Schristos #define VSLENGTH	0xa		/* ${#var} */
74*a7e8b4a5Skre #define VSUNKNOWN	0xf		/* unknown modifier */
7561f28255Scgd 
761fca9bbfSkre union node *parsecmd(int);
771fca9bbfSkre void fixredir(union node *, const char *, int);
78ae1a4788Skre int goodname(const char *);
79021ba509Skre int isassignment(const char *);
801fca9bbfSkre const char *getprompt(void *);
811fca9bbfSkre const char *expandstr(char *, int);
82ce134619Skre const char *expandvar(char *, int);
83e3847ee4Skre const char *expandenv(char *);
841fca9bbfSkre 
851fca9bbfSkre struct HereDoc;
861fca9bbfSkre union node;
871fca9bbfSkre struct nodelist;
881fca9bbfSkre 
891fca9bbfSkre struct parse_state {
901fca9bbfSkre 	struct HereDoc *ps_heredoclist;	/* list of here documents to read */
911fca9bbfSkre 	int ps_parsebackquote;		/* nonzero inside backquotes */
921fca9bbfSkre 	int ps_doprompt;		/* if set, prompt the user */
931fca9bbfSkre 	int ps_needprompt;		/* true if interactive at line start */
941fca9bbfSkre 	int ps_lasttoken;		/* last token read */
951fca9bbfSkre 	int ps_tokpushback;		/* last token pushed back */
961fca9bbfSkre 	char *ps_wordtext;	/* text of last word returned by readtoken */
97021ba509Skre 	int ps_checkkwd;		/* word expansion flags, see below */
981fca9bbfSkre 	struct nodelist *ps_backquotelist; /* list of cmdsubs to process */
991fca9bbfSkre 	union node *ps_redirnode;	/* node for current redirect */
100ec83c7c4Skre 	struct HereDoc *ps_heredoc;	/* current heredoc << being parsed */
1011fca9bbfSkre 	int ps_quoteflag;		/* set if (part) of token was quoted */
1021fca9bbfSkre 	int ps_startlinno;		/* line # where last token started */
1031fca9bbfSkre 	int ps_funclinno;		/* line # of the current function */
1041fca9bbfSkre 	int ps_elided_nl;		/* count of \ \n pairs we have seen */
1051fca9bbfSkre };
1061fca9bbfSkre 
1071fca9bbfSkre /*
1081fca9bbfSkre  * The parser references the elements of struct parse_state quite
1091fca9bbfSkre  * frequently - they used to be simple globals, so one memory ref
110ec83c7c4Skre  * per access, adding an indirect through a global ptr would not be
1111fca9bbfSkre  * nice.   The following gross hack allows most of that cost to be
1121fca9bbfSkre  * avoided, by allowing the compiler to understand that the global
1131fca9bbfSkre  * pointer is in fact constant in any function, and so its value can
1141fca9bbfSkre  * be cached, rather than needing to be fetched every time in case
1151fca9bbfSkre  * some other called function has changed it.
1161fca9bbfSkre  *
1171fca9bbfSkre  * The rule to make this work is that any function that wants
1181fca9bbfSkre  * to alter the global must restore it before it returns (and thus
1191fca9bbfSkre  * must have an error trap handler).  That means that the struct
1201fca9bbfSkre  * used for the new parser state can be a local in that function's
1211fca9bbfSkre  * stack frame, it never needs to be malloc'd.
1221fca9bbfSkre  */
1231fca9bbfSkre 
1241fca9bbfSkre union parse_state_p {
1251fca9bbfSkre 	struct parse_state *const	c_current_parser;
1261fca9bbfSkre 	struct parse_state *		v_current_parser;
1271fca9bbfSkre };
1281fca9bbfSkre 
1291fca9bbfSkre extern union parse_state_p psp;
1301fca9bbfSkre 
1311fca9bbfSkre #define	current_parser (psp.c_current_parser)
1321fca9bbfSkre 
1331fca9bbfSkre /*
1341fca9bbfSkre  * Perhaps one day emulate "static" by moving most of these definitions into
1351fca9bbfSkre  * parser.c ...  (only checkkwd & tokpushback are used outside parser.c,
1361fca9bbfSkre  * and only in init.c as a RESET activity)
1371fca9bbfSkre  */
1381fca9bbfSkre #define	tokpushback	(current_parser->ps_tokpushback)
1391fca9bbfSkre #define	checkkwd	(current_parser->ps_checkkwd)
1401fca9bbfSkre 
1411fca9bbfSkre #define	heredoclist	(current_parser->ps_heredoclist)
1421fca9bbfSkre #define	parsebackquote	(current_parser->ps_parsebackquote)
1431fca9bbfSkre #define	doprompt	(current_parser->ps_doprompt)
1441fca9bbfSkre #define	needprompt	(current_parser->ps_needprompt)
1451fca9bbfSkre #define	lasttoken	(current_parser->ps_lasttoken)
1461fca9bbfSkre #define	wordtext	(current_parser->ps_wordtext)
1471fca9bbfSkre #define	backquotelist	(current_parser->ps_backquotelist)
1481fca9bbfSkre #define	redirnode	(current_parser->ps_redirnode)
1491fca9bbfSkre #define	heredoc		(current_parser->ps_heredoc)
1501fca9bbfSkre #define	quoteflag	(current_parser->ps_quoteflag)
1511fca9bbfSkre #define	startlinno	(current_parser->ps_startlinno)
1521fca9bbfSkre #define	funclinno	(current_parser->ps_funclinno)
1531fca9bbfSkre #define	elided_nl	(current_parser->ps_elided_nl)
15461f28255Scgd 
15561f28255Scgd /*
156021ba509Skre  * Values that can be set in checkkwd
157021ba509Skre  */
158021ba509Skre #define CHKKWD		0x01		/* turn word into keyword (if it is) */
159021ba509Skre #define CHKNL		0x02		/* ignore leading \n's */
160021ba509Skre #define CHKALIAS	0x04		/* lookup words as aliases and ... */
161021ba509Skre 
162021ba509Skre /*
16361f28255Scgd  * NEOF is returned by parsecmd when it encounters an end of file.  It
16461f28255Scgd  * must be distinct from NULL, so we use the address of a variable that
16561f28255Scgd  * happens to be handy.
16661f28255Scgd  */
1671fca9bbfSkre #define NEOF ((union node *)&psp)
168727a69dcSkre 
169727a69dcSkre #ifdef DEBUG
170727a69dcSkre extern int parsing;
171727a69dcSkre #endif
172