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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
implement Shellbuiltin;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
sh: Sh;
Listnode, Context: import sh;
myself: Shellbuiltin;
include "regex.m";
regex: Regex;
initbuiltin(ctxt: ref Context, shmod: Sh): string
{
sys = load Sys Sys->PATH;
sh = shmod;
myself = load Shellbuiltin "$self";
if (myself == nil)
ctxt.fail("bad module", sys->sprint("regex: cannot load self: %r"));
regex = load Regex Regex->PATH;
if (regex == nil)
ctxt.fail("bad module",
sys->sprint("regex: cannot load %s: %r", Regex->PATH));
ctxt.addbuiltin("match", myself);
ctxt.addsbuiltin("re", myself);
return nil;
}
getself(): Shellbuiltin
{
return myself;
}
runbuiltin(ctxt: ref Context, nil: Sh,
argv: list of ref Listnode, nil: int): string
{
case (hd argv).word {
"match" =>
return builtin_match(ctxt, argv);
}
return nil;
}
whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
{
return nil;
}
runsbuiltin(ctxt: ref Context, nil: Sh,
argv: list of ref Listnode): list of ref Listnode
{
name := (hd argv).word;
case name {
"re" =>
return sbuiltin_re(ctxt, argv);
}
return nil;
}
sbuiltin_re(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
if (tl argv == nil)
ctxt.fail("usage", "usage: re (g|v|s|sg|m|mg|M) arg...");
argv = tl argv;
w := (hd argv).word;
case w {
"g" or
"v" =>
return sbuiltin_sel(ctxt, argv, w == "v");
"s" or
"sg" =>
return sbuiltin_sub(ctxt, argv, w == "sg");
"m" =>
return sbuiltin_match(ctxt, argv, 0);
"mg" =>
return sbuiltin_gmatch(ctxt, argv);
"M" =>
return sbuiltin_match(ctxt, argv, 1);
* =>
ctxt.fail("usage", "usage: re (g|v|s|sg|m|mg|M) arg...");
return nil;
}
}
sbuiltin_match(ctxt: ref Context, argv: list of ref Listnode, aflag: int): list of ref Listnode
{
if (len argv != 3)
ctxt.fail("usage", "usage: re " + (hd argv).word + " arg");
argv = tl argv;
re := getregex(ctxt, word(hd argv), aflag);
w := word(hd tl argv);
a := regex->execute(re, w);
if (a == nil)
return nil;
ret: list of ref Listnode;
for (i := len a - 1; i >= 0; i--)
ret = ref Listnode(nil, elem(a, i, w)) :: ret;
return ret;
}
sbuiltin_gmatch(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
if (len argv != 3)
ctxt.fail("usage", "usage: re mg arg");
argv = tl argv;
re := getregex(ctxt, word(hd argv), 0);
w := word(hd tl argv);
ret, nret: list of ref Listnode;
beg := 0;
while ((a := regex->executese(re, w, (beg, len w), beg == 0, 1)) != nil) {
(s, e) := a[0];
ret = ref Listnode(nil, w[s:e]) :: ret;
if (s == e)
break;
beg = e;
}
for (; ret != nil; ret = tl ret)
nret = hd ret :: nret;
return nret;
}
sbuiltin_sel(ctxt: ref Context, argv: list of ref Listnode, vflag: int): list of ref Listnode
{
cmd := (hd argv).word;
argv = tl argv;
if (argv == nil)
ctxt.fail("usage", "usage: " + cmd + " regex [arg...]");
re := getregex(ctxt, word(hd argv), 0);
ret, nret: list of ref Listnode;
for (argv = tl argv; argv != nil; argv = tl argv)
if (vflag ^ (regex->execute(re, word(hd argv)) != nil))
ret = hd argv :: ret;
for (; ret != nil; ret = tl ret)
nret = hd ret :: nret;
return nret;
}
sbuiltin_sub(ctxt: ref Context, argv: list of ref Listnode, gflag: int): list of ref Listnode
{
cmd := (hd argv).word;
argv = tl argv;
if (argv == nil || tl argv == nil)
ctxt.fail("usage", "usage: " + cmd + " regex subs [arg...]");
re := getregex(ctxt, word(hd argv), 1);
subs := word(hd tl argv);
ret, nret: list of ref Listnode;
for (argv = tl tl argv; argv != nil; argv = tl argv)
ret = ref Listnode(nil, substitute(word(hd argv), re, subs, gflag).t1) :: ret;
for (; ret != nil; ret = tl ret)
nret = hd ret :: nret;
return nret;
}
builtin_match(ctxt: ref Context, argv: list of ref Listnode): string
{
if (tl argv == nil)
ctxt.fail("usage", "usage: match regexp [arg...]");
re := getregex(ctxt, word(hd tl argv), 0);
for (argv = tl tl argv; argv != nil; argv = tl argv)
if (regex->execute(re, word(hd argv)) == nil)
return "no match";
return nil;
}
substitute(w: string, re: Regex->Re, subs: string, gflag: int): (int, string)
{
matched := 0;
s := "";
beg := 0;
do {
a := regex->executese(re, w, (beg, len w), beg == 0, 1);
if (a == nil)
break;
matched = 1;
s += w[beg:a[0].t0];
for (i := 0; i < len subs; i++) {
if (subs[i] != '\\' || i == len subs - 1)
s[len s] = subs[i];
else {
c := subs[++i];
if (c < '0' || c > '9')
s[len s] = c;
else
s += elem(a, c - '0', w);
}
}
beg = a[0].t1;
if (a[0].t0 == a[0].t1)
break;
} while (gflag && beg < len w);
return (matched, s + w[beg:]);
}
elem(a: array of (int, int), i: int, w: string): string
{
if (i < 0 || i >= len a)
return nil; # XXX could raise failure here. (invalid backslash escape)
(s, e) := a[i];
if (s == -1)
return nil;
return w[s:e];
}
# XXX could do regex caching here if it was worth it.
getregex(ctxt: ref Context, res: string, flag: int): Regex->Re
{
(re, err) := regex->compile(res, flag);
if (re == nil)
ctxt.fail("bad regex", "regex: bad regex \"" + res + "\": " + err);
return re;
}
word(n: ref Listnode): string
{
if (n.word != nil)
return n.word;
if (n.cmd != nil)
n.word = sh->cmd2string(n.cmd);
return n.word;
}
|