xref: /netbsd/bin/sh/syntax.c (revision 12cd2adf)
1 /*	$NetBSD: syntax.c,v 1.8 2019/02/04 09:56:48 kre Exp $	*/
2 
3 #include <sys/cdefs.h>
4 __RCSID("$NetBSD: syntax.c,v 1.8 2019/02/04 09:56:48 kre Exp $");
5 
6 #include <limits.h>
7 #include "shell.h"
8 #include "syntax.h"
9 #include "parser.h"
10 
11 #if CWORD != 0
12 #error initialisation assumes 'CWORD' is zero
13 #endif
14 
15 #define ndx(ch) (ch + 2 - CHAR_MIN)
16 #define set(ch, val) [ndx(ch)] = val,
17 #define set_range(s, e, val) [ndx(s) ... ndx(e)] = val,
18 
19 /* syntax table used when not in quotes */
20 const char basesyntax[258] = { CFAKE, CEOF,
21     set_range(CTL_FIRST, CTL_LAST, CCTL)
22 /* Note code assumes that only the above are CCTL in basesyntax */
23     set('\n', CNL)
24     set('\\', CBACK)
25     set('\'', CSQUOTE)
26     set('"', CDQUOTE)
27     set('`', CBQUOTE)
28     set('$', CVAR)
29     set('}', CENDVAR)
30     set('<', CSPCL)
31     set('>', CSPCL)
32     set('(', CSPCL)
33     set(')', CSPCL)
34     set(';', CSPCL)
35     set('&', CSPCL)
36     set('|', CSPCL)
37     set(' ', CSPCL)
38     set('\t', CSPCL)
39 };
40 
41 /* syntax table used when in double quotes */
42 const char dqsyntax[258] = { CFAKE, CEOF,
43     set_range(CTL_FIRST, CTL_LAST, CCTL)
44     set('\n', CNL)
45     set('\\', CBACK)
46     set('"', CDQUOTE)
47     set('`', CBQUOTE)
48     set('$', CVAR)
49     set('}', CENDVAR)
50     /* ':/' for tilde expansion, '-]' for [a\-x] pattern ranges */
51     set('!', CCTL)
52     set('*', CCTL)
53     set('?', CCTL)
54     set('[', CCTL)
55     set('=', CCTL)
56     set('~', CCTL)
57     set(':', CCTL)
58     set('/', CCTL)
59     set('-', CCTL)
60     set(']', CCTL)
61 };
62 
63 /* syntax table used when in single quotes */
64 const char sqsyntax[258] = { CFAKE, CEOF,
65 /* CCTL includes anything that might perhaps need to be escaped if quoted */
66     set_range(CTL_FIRST, CTL_LAST, CCTL)
67     set('\n', CNL)
68     set('\'', CSQUOTE)
69     set('\\', CSBACK)
70     /* ':/' for tilde expansion, '-]' for [a\-x] pattern ranges */
71     set('!', CCTL)
72     set('*', CCTL)
73     set('?', CCTL)
74     set('[', CCTL)
75     set('=', CCTL)
76     set('~', CCTL)
77     set(':', CCTL)
78     set('/', CCTL)
79     set('-', CCTL)
80     set(']', CCTL)
81 };
82 
83 /* syntax table used when in arithmetic */
84 const char arisyntax[258] = { CFAKE, CEOF,
85     set_range(CTL_FIRST, CTL_LAST, CCTL)
86     set('\n', CNL)
87     set('\\', CBACK)
88     set('`', CBQUOTE)
89     set('\'', CSQUOTE)
90     set('"', CDQUOTE)
91     set('$', CVAR)
92     set('}', CENDVAR)
93     set('(', CLP)
94     set(')', CRP)
95 };
96 
97 /* character classification table */
98 const char is_type[258] = { 0, 0,
99     set_range('0', '9', ISDIGIT)
100     set_range('a', 'z', ISLOWER)
101     set_range('A', 'Z', ISUPPER)
102     set('_', ISUNDER)
103     set('#', ISSPECL)
104     set('?', ISSPECL)
105     set('$', ISSPECL)
106     set('!', ISSPECL)
107     set('-', ISSPECL)
108     set('*', ISSPECL)
109     set('@', ISSPECL)
110     set(' ', ISSPACE)
111     set('\t', ISSPACE)
112     set('\n', ISSPACE)
113 };
114