summaryrefslogtreecommitdiff
path: root/bin/sh/syntax.c
blob: 7b460d4c2f58d52379b0ba899b13c9a891c42d2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*	$NetBSD: syntax.c,v 1.7 2018/12/03 06:40:26 kre Exp $	*/

#include <sys/cdefs.h>
__RCSID("$NetBSD: syntax.c,v 1.7 2018/12/03 06:40:26 kre Exp $");

#include <limits.h>
#include "shell.h"
#include "syntax.h"
#include "parser.h"

#if CWORD != 0
#error initialisation assumes 'CWORD' is zero
#endif

#define ndx(ch) (ch + 2 - CHAR_MIN)
#define set(ch, val) [ndx(ch)] = val,
#define set_range(s, e, val) [ndx(s) ... ndx(e)] = val,

/* syntax table used when not in quotes */
const char basesyntax[258] = { CFAKE, CEOF,
    set_range(CTL_FIRST, CTL_LAST, CCTL)
    set('\n', CNL)
    set('\\', CBACK)
    set('\'', CSQUOTE)
    set('"', CDQUOTE)
    set('`', CBQUOTE)
    set('$', CVAR)
    set('}', CENDVAR)
    set('<', CSPCL)
    set('>', CSPCL)
    set('(', CSPCL)
    set(')', CSPCL)
    set(';', CSPCL)
    set('&', CSPCL)
    set('|', CSPCL)
    set(' ', CSPCL)
    set('\t', CSPCL)
};

/* syntax table used when in double quotes */
const char dqsyntax[258] = { CFAKE, CEOF,
    set_range(CTL_FIRST, CTL_LAST, CCTL)
    set('\n', CNL)
    set('\\', CBACK)
    set('"', CDQUOTE)
    set('`', CBQUOTE)
    set('$', CVAR)
    set('}', CENDVAR)
    /* ':/' for tilde expansion, '-]' for [a\-x] pattern ranges */
    set('!', CCTL)
    set('*', CCTL)
    set('?', CCTL)
    set('[', CCTL)
    set('=', CCTL)
    set('~', CCTL)
    set(':', CCTL)
    set('/', CCTL)
    set('-', CCTL)
    set(']', CCTL)
};

/* syntax table used when in single quotes */
const char sqsyntax[258] = { CFAKE, CEOF,
    set_range(CTL_FIRST, CTL_LAST, CCTL)
    set('\n', CNL)
    set('\'', CSQUOTE)
    set('\\', CSBACK)
    /* ':/' for tilde expansion, '-]' for [a\-x] pattern ranges */
    set('!', CCTL)
    set('*', CCTL)
    set('?', CCTL)
    set('[', CCTL)
    set('=', CCTL)
    set('~', CCTL)
    set(':', CCTL)
    set('/', CCTL)
    set('-', CCTL)
    set(']', CCTL)
};

/* syntax table used when in arithmetic */
const char arisyntax[258] = { CFAKE, CEOF,
    set_range(CTL_FIRST, CTL_LAST, CCTL)
    set('\n', CNL)
    set('\\', CBACK)
    set('`', CBQUOTE)
    set('\'', CSQUOTE)
    set('"', CDQUOTE)
    set('$', CVAR)
    set('}', CENDVAR)
    set('(', CLP)
    set(')', CRP)
};

/* character classification table */
const char is_type[258] = { 0, 0,
    set_range('0', '9', ISDIGIT)
    set_range('a', 'z', ISLOWER)
    set_range('A', 'Z', ISUPPER)
    set('_', ISUNDER)
    set('#', ISSPECL)
    set('?', ISSPECL)
    set('$', ISSPECL)
    set('!', ISSPECL)
    set('-', ISSPECL)
    set('*', ISSPECL)
    set('@', ISSPECL)
    set(' ', ISSPACE)
    set('\t', ISSPACE)
    set('\n', ISSPACE)
};