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
|
implement Shellbuiltin;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
sh: Sh;
Listnode, Context: import sh;
myself: Shellbuiltin;
include "string.m";
str: String;
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("string: cannot load self: %r"));
str = load String String->PATH;
if (str == nil)
ctxt.fail("bad module",
sys->sprint("string: cannot load %s: %r", String->PATH));
ctxt.addbuiltin("prefix", myself);
ctxt.addbuiltin("in", myself);
names := array[] of {
"splitl", "splitr", "drop", "take", "splitstrl", "splitstrr",
"tolower", "toupper", "len", "alen", "slice", "fields",
"padl", "padr",
};
for (i := 0; i < len names; i++)
ctxt.addsbuiltin(names[i], myself);
return nil;
}
whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
{
return nil;
}
getself(): Shellbuiltin
{
return myself;
}
runbuiltin(ctxt: ref Context, nil: Sh,
argv: list of ref Listnode, nil: int): string
{
case (hd argv).word {
"prefix" =>
(a, b) := earg2("prefix", ctxt, argv);
if (!str->prefix(a, b))
return "false";
"in" =>
(a, b) := earg2("in", ctxt, argv);
if (a == nil || !str->in(a[0], b))
return "false";
}
return nil;
}
runsbuiltin(ctxt: ref Context, nil: Sh,
argv: list of ref Listnode): list of ref Listnode
{
name := (hd argv).word;
case name {
"splitl" =>
(a, b) := earg2("splitl", ctxt, argv);
return mk2(str->splitl(a, b));
"splitr" =>
(a, b) := earg2("splitr", ctxt, argv);
return mk2(str->splitr(a, b));
"drop" =>
(a, b) := earg2("drop", ctxt, argv);
return mk1(str->drop(a, b));
"take" =>
(a, b) := earg2("take", ctxt, argv);
return mk1(str->take(a, b));
"splitstrl" =>
(a, b) := earg2("splitstrl", ctxt, argv);
return mk2(str->splitstrl(a, b));
"splitstrr" =>
(a, b) := earg2("splitstrr", ctxt, argv);
return mk2(str->splitstrr(a, b));
"tolower" =>
return mk1(str->tolower(earg1("tolower", ctxt, argv)));
"toupper" =>
return mk1(str->toupper(earg1("tolower", ctxt, argv)));
"len" =>
return mk1(string len earg1("len", ctxt, argv));
"alen" =>
return mk1(string len array of byte earg1("alen", ctxt, argv));
"slice" =>
return sbuiltin_slice(ctxt, argv);
"fields" =>
return sbuiltin_fields(ctxt, argv);
"padl" =>
return sbuiltin_pad(ctxt, argv, -1);
"padr" =>
return sbuiltin_pad(ctxt, argv, 1);
}
return nil;
}
sbuiltin_pad(ctxt: ref Context, argv: list of ref Listnode, dir: int): list of ref Listnode
{
if (tl argv == nil || !isnum((hd tl argv).word))
ctxt.fail("usage", "usage: " + (hd argv).word + " n [arg...]");
argv = tl argv;
n := int (hd argv).word * dir;
s := "";
for (argv = tl argv; argv != nil; argv = tl argv) {
s += word(hd argv);
if (tl argv != nil)
s[len s] = ' ';
}
if (n != 0)
s = sys->sprint("%*s", n, s);
return ref Listnode(nil, s) :: nil;
}
sbuiltin_fields(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
argv = tl argv;
if (len argv != 2)
ctxt.fail("usage", "usage: fields cl s");
cl := word(hd argv);
s := word(hd tl argv);
r: list of string;
n := 0;
for (i := 0; i < len s; i++) {
if (str->in(s[i], cl)) {
r = s[n:i] :: r;
n = i + 1;
}
}
r = s[n:i] :: r;
rl: list of ref Listnode;
for (; r != nil; r = tl r)
rl = ref Listnode(nil, hd r) :: rl;
return rl;
}
sbuiltin_slice(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
argv = tl argv;
if (len argv != 3 || !isnum((hd argv).word) ||
(hd tl argv).word != "end" && !isnum((hd tl argv).word))
ctxt.fail("usage", "usage: slice start end arg");
n1 := int (hd argv).word;
n2: int;
s := word(hd tl tl argv);
r := "";
if ((hd tl argv).word == "end")
n2 = len s;
else
n2 = int (hd tl argv).word;
if (n2 > len s)
n2 = len s;
if (n1 > len s)
n1 = len s;
if (n2 > n1)
r = s[n1:n2];
return mk1(r);
}
earg2(cmd: string, ctxt: ref Context, argv: list of ref Listnode): (string, string)
{
argv = tl argv;
if (len argv != 2)
ctxt.fail("usage", "usage: " + cmd + " arg1 arg2");
return (word(hd argv), word(hd tl argv));
}
earg1(cmd: string, ctxt: ref Context, argv: list of ref Listnode): string
{
if (len argv != 2)
ctxt.fail("usage", "usage: " + cmd + " arg");
return word(hd tl argv);
}
mk2(x: (string, string)): list of ref Listnode
{
(a, b) := x;
return ref Listnode(nil, a) :: ref Listnode(nil, b) :: nil;
}
mk1(x: string): list of ref Listnode
{
return ref Listnode(nil, x) :: nil;
}
isnum(s: string): int
{
for (i := 0; i < len s; i++)
if (s[i] > '9' || s[i] < '0')
return 0;
return 1;
}
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;
}
|