-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwgrep.winxed
255 lines (188 loc) · 4.47 KB
/
wgrep.winxed
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#! winxed
/*
=pod
Copyright (C) 2012, Alvis Yardley
=head1 NAME
wgrep.winxed - A minimial, winxed implementation of grep.
=head1 DESCRIPTION
This is a winxed implementation of Rob Pike's implementation of grep in C. See
BRIAN W. KERNIGHAN & ROB PIKE, "The Practice of Programming" 222-228 (Addison
Wesley pub., 1999).
=cut
*/
/*
=item C<match(string regexp, string text)>
Search for regexp anywhere in text.
=cut
*/
function match(string regexp, string text)
{
var regex = split('', regexp);
/* I need to advance 'regexp' to point past (or, since I don't have
pointers, not contain) the '^' character. */
if (regex[0] == '^') {
return matchhere(substr(regexp, 1, length(regexp) - 1), text);
}
do { /* Need to look even if the string is empty. */
if (matchhere(regexp, text))
return 1;
} while ((text = substr(text, 1)) != ''); /* Simulate '*text++' */
return 0;
}
/*
=item C<matchhere(string regexp, string text)>
Search for regexp at beginning of text.
=cut
*/
function matchhere(string regexp, string text)
{
var regex = split('', regexp);
var tex = split('', text);
if (regex[0] == '')
return 1;
if (regex[1] == '*')
return matchstar(regex[0],
substr(regexp, 2, length(regexp) - 2),
text);
if (regex[0] == '$' && regex[1] == '')
return tex[0] == "\n";
if (text != '' && (regex[0] == '.' || regex[0] == tex[0]))
return matchhere(substr(regexp, 1, length(regexp) - 1),
substr(text, 1, length(text) - 1));
return 0;
}
/*
=item C<matchstar(string c, string regexp, string text)>
Search for the character at the beginning of text.
=cut
*/
function matchstar(string c, string regexp, string text)
{
int i = 0;
var tex = split('', text);
do { /* a '*' matches zero or more times. */
if (matchhere(regexp, text))
return 1;
} while (tex[i] != '' && (tex[i++] == c || c == '.'));
return 0;
}
/*
=item C<grep(string regexp, var file, string name)>
Search for regexp in a file.
=cut
*/
function grep(string regexp, var file, string name)
{
int nmatch;
string text;
nmatch = 0;
while (text = file.readline()) {
if (match(regexp, text)) {
nmatch++;
if (name != null)
print(name, ":");
print(text);
}
}
return nmatch;
}
/*
=item C<main(var args)>
=cut
*/
function main[main](var args)
{
int i, nmatch;
if (args == 1) {
show_help();
exit(0);
}
switch (args[1]) {
case '-h' :
case '--help' :
show_help();
exit(0);
break;
case '-i' :
case '--case-insensitive' :
say("Option unimplemented at this time.");
exit(0);
break;
case '-n' :
case '--line-numbers' :
say("Option unimplemented at this time.");
exit(0);
break;
case '-s' :
case '--invert-sense' :
say("Option unimplemented at this time.");
exit(0);
break;
case '-v' :
case '--version' :
show_version();
exit(0);
break;
default :
break;
}
nmatch = 0;
if (args == 2) {
// on stdin
if (grep(args[1], getstdin(), null))
nmatch++;
}
else {
// on file
var file = new 'FileHandle';
for (i = 2; i < args; i++) {
try {
file.open(args[i], "r");
}
catch() {
/* Note: We want to continue in case there are other
programs to grep on the command line. */
say("unable to open ", args[i], ":");
}
string name;
(args > 3) ? name = args[i] : name = null;
if (grep(args[1], file, name) > 0)
nmatch++;
file.close();
}
}
return nmatch == 0;
}
/*
=item C<show_help()>
Display help, depending on whether the user specifics the '--help' option
or, alternatively, specifics an incorrect option.
=cut
*/
function show_help()
{
say(<<:
Usage: wgrep [options] regexp [filename ...]
-h|--help Show this help
-i|--case-insensitive Case insensitve match
-n|--line-numbers Display line numbers
-s|--invert-sense Invert sense of match
-v|--version Show version information
:>>
); // Note: ');' must be in col. 0 to prevent 'Unterminated heredoc' error
}
/*
=item C<show_version()>
Display wgrep's version and copyright information.
=cut
*/
function show_version()
{
say(<<:
This is wgrep, v0.1
Copyright (C) 2012, Alvis Yardley. All Rights Reserved.
The author hereby distributes this program under the terms of The Artistic
License 2.0, a copy of which is included in this distribution.
:>>
);
}