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