1/*	$NetBSD: lex.h,v 1.7 2005/09/11 22:16:00 christos Exp $	*/
2
3/*
4 * Source input, lexer and parser
5 */
6
7/* $Id: lex.h,v 1.7 2005/09/11 22:16:00 christos Exp $ */
8
9#define	IDENT	64
10
11typedef struct source Source;
12struct source {
13	const char *str;	/* input pointer */
14	int	type;		/* input type */
15	const char *start;	/* start of current buffer */
16	union {
17		char **strv;	/* string [] */
18		struct shf *shf; /* shell file */
19		struct tbl *tblp; /* alias (SALIAS) */
20		char *freeme;	/* also for SREREAD */
21	} u;
22	char	ugbuf[2];	/* buffer for ungetsc() (SREREAD) and
23				 * alias (SALIAS) */
24	int	line;		/* line number */
25	int	errline;	/* line the error occurred on (0 if not set) */
26	const char *file;	/* input file name */
27	int	flags;		/* SF_* */
28	Area	*areap;
29	XString	xs;		/* input buffer */
30	Source *next;		/* stacked source */
31};
32
33/* Source.type values */
34#define	SEOF		0	/* input EOF */
35#define	SFILE		1	/* file input */
36#define SSTDIN		2	/* read stdin */
37#define	SSTRING		3	/* string */
38#define	SWSTR		4	/* string without \n */
39#define	SWORDS		5	/* string[] */
40#define	SWORDSEP	6	/* string[] separator */
41#define	SALIAS		7	/* alias expansion */
42#define SREREAD		8	/* read ahead to be re-scanned */
43
44/* Source.flags values */
45#define SF_ECHO		BIT(0)	/* echo input to shlout */
46#define SF_ALIAS	BIT(1)	/* faking space at end of alias */
47#define SF_ALIASEND	BIT(2)	/* faking space at end of alias */
48#define SF_TTY		BIT(3)	/* type == SSTDIN & it is a tty */
49
50/*
51 * states while lexing word
52 */
53#define	SBASE	0		/* outside any lexical constructs */
54#define	SWORD	1		/* implicit quoting for substitute() */
55#ifdef KSH
56#define	SLETPAREN 2		/* inside (( )), implicit quoting */
57#endif /* KSH */
58#define	SSQUOTE	3		/* inside '' */
59#define	SDQUOTE	4		/* inside "" */
60#define	SBRACE	5		/* inside ${} */
61#define	SCSPAREN 6		/* inside $() */
62#define	SBQUOTE	7		/* inside `` */
63#define	SASPAREN 8		/* inside $(( )) */
64#define SHEREDELIM 9		/* parsing <<,<<- delimiter */
65#define SHEREDQUOTE 10		/* parsing " in <<,<<- delimiter */
66#define SPATTERN 11		/* parsing *(...|...) pattern (*+?@!) */
67#define STBRACE 12		/* parsing ${..[#%]..} */
68
69typedef union {
70	int	i;
71	char   *cp;
72	char  **wp;
73	struct op *o;
74	struct ioword *iop;
75} YYSTYPE;
76
77/* If something is added here, add it to tokentab[] in syn.c as well */
78#define	LWORD	256
79#define	LOGAND	257		/* && */
80#define	LOGOR	258		/* || */
81#define	BREAK	259		/* ;; */
82#define	IF	260
83#define	THEN	261
84#define	ELSE	262
85#define	ELIF	263
86#define	FI	264
87#define	CASE	265
88#define	ESAC	266
89#define	FOR	267
90#define SELECT	268
91#define	WHILE	269
92#define	UNTIL	270
93#define	DO	271
94#define	DONE	272
95#define	IN	273
96#define	FUNCTION 274
97#define	TIME	275
98#define	REDIR	276
99#ifdef KSH
100#define MDPAREN	277		/* (( )) */
101#endif /* KSH */
102#define BANG	278		/* ! */
103#define DBRACKET 279		/* [[ .. ]] */
104#define COPROC	280		/* |& */
105#define	YYERRCODE 300
106
107/* flags to yylex */
108#define	CONTIN	BIT(0)		/* skip new lines to complete command */
109#define	ONEWORD	BIT(1)		/* single word for substitute() */
110#define	ALIAS	BIT(2)		/* recognize alias */
111#define	KEYWORD	BIT(3)		/* recognize keywords */
112#define LETEXPR	BIT(4)		/* get expression inside (( )) */
113#define VARASN	BIT(5)		/* check for var=word */
114#define ARRAYVAR BIT(6)		/* parse x[1 & 2] as one word */
115#define ESACONLY BIT(7)		/* only accept esac keyword */
116#define CMDWORD BIT(8)		/* parsing simple command (alias related) */
117#define HEREDELIM BIT(9)	/* parsing <<,<<- delimiter */
118#define HEREDOC BIT(10)		/* parsing heredoc */
119
120#define	HERES	10		/* max << in line */
121
122EXTERN	Source *source;		/* yyparse/yylex source */
123EXTERN	YYSTYPE	yylval;		/* result from yylex */
124EXTERN	struct ioword *heres [HERES], **herep;
125EXTERN	char	ident [IDENT+1];
126
127#ifdef HISTORY
128# define HISTORYSIZE	128	/* size of saved history */
129
130EXTERN	char  **histlist;	/* saved commands */
131EXTERN	char  **histptr;	/* last history item */
132EXTERN	int	histsize;	/* history size */
133#endif /* HISTORY */
134