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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
*** src.rgx/config.c Thu Jan 2 23:34:31 1997
--- config.c Thu Jan 2 23:51:21 1997
***************
*** 19,24 ****
--- 19,25 ----
# include "compile.h"
# include "csupport.h"
# include "table.h"
+ # include "rgx.h"
typedef struct {
char *name; /* name of the option */
***************
*** 810,815 ****
--- 811,819 ----
/* initialize interpreter */
i_init(conf[CREATE].u.str);
+
+ /* initialize regular expressions */
+ rgx_init();
/* initialize compiler */
c_init(conf[AUTO_OBJECT].u.str,
*** src.rgx/kfun/extra.c Tue Sep 27 09:28:26 1994
--- kfun/extra.c Thu Feb 2 22:25:18 1995
***************
*** 560,562 ****
--- 560,640 ----
error("Not yet implemented");
}
# endif
+
+
+ # ifdef FUNCDEF
+ FUNCDEF("regexp_compile", kf_regexp_compile, p_regexp_compile)
+ # else
+ char p_regexp_compile[] = { C_TYPECHECKED | C_STATIC | C_VARARGS,
+ T_STRING | (1 << REFSHIFT), 2, T_STRING, T_INT };
+
+ /*
+ * NAME: kfun->regexp_compile()
+ * DESCRIPTION: compile a regexp pattern
+ */
+ int kf_regexp_compile(nargs)
+ int nargs;
+ {
+ int case_matters;
+ array *compiled;
+
+ if (nargs < 1)
+ return -1;
+
+ case_matters = (nargs == 2 ? ! (sp++)->u.number : 1);
+
+ compiled = rgx_new(sp->u.string, case_matters);
+
+ str_del(sp->u.string);
+ sp->type = T_ARRAY;
+ arr_ref(sp->u.array = compiled);
+
+ return 0;
+ }
+ # endif
+
+
+ # ifdef FUNCDEF
+ FUNCDEF("regexp_match", kf_regexp_match, p_regexp_match)
+ # else
+ char p_regexp_match[] = { C_TYPECHECKED | C_STATIC | C_VARARGS,
+ T_INT | (1 << REFSHIFT), 3,
+ T_STRING | (1 << REFSHIFT), T_STRING, T_INT };
+
+ /*
+ * NAME: kfun->regexp_match()
+ * DESCRIPTION: perform regexp matching with a previously compiled pattern
+ */
+ int kf_regexp_match(nargs)
+ int nargs;
+ {
+ int reverse;
+ string *subject;
+ array *compiled, *result;
+
+ if (nargs < 2)
+ return -1;
+
+ reverse = (nargs == 3 ? (sp++)->u.number : 0);
+ subject = sp->u.string;
+ compiled = sp[1].u.array;
+
+ if (compiled->size != 3)
+ return 1;
+
+ result = rgx_match(d_get_elts(compiled), subject, reverse);
+
+ str_del((sp++)->u.string);
+ arr_del(sp->u.array);
+
+ if (result == (array *) 0)
+ {
+ sp->type = T_INT;
+ sp->u.number = 0;
+ }
+ else
+ arr_ref(sp->u.array = result);
+
+ return 0;
+ }
+ # endif
*** src.rgx/kfun/kfun.h Sun May 8 08:15:01 1994
--- kfun/kfun.h Thu Feb 2 22:25:18 1995
***************
*** 5,7 ****
--- 5,8 ----
# include "xfloat.h"
# include "interpret.h"
# include "data.h"
+ # include "rgx.h"
*** src.rgx/rgx.c Thu Jan 2 21:41:55 1997
--- rgx.c Thu Jan 2 21:17:46 1997
***************
*** 0 ****
--- 1,213 ----
+ # include "dgd.h"
+ # include "str.h"
+ # include "array.h"
+ # include "interpret.h"
+ # include <gnuregex.h>
+ # include "rgx.h"
+ # include <memory.h>
+
+ static char trans_table[256];
+
+ /*
+ * NAME: regexp->init()
+ * DESCRIPTION: initialize regexp handling
+ */
+ void rgx_init()
+ {
+ register int i;
+
+ for (i = 0; i < 256; ++i)
+ trans_table[i] = i;
+ for (i = 'a'; i <= 'z'; ++i)
+ trans_table[i] = i + 'A' - 'a';
+ }
+
+ /*
+ * NAME: regexp->new()
+ * DESCRIPTION: create a new regexp buffer
+ */
+ array *rgx_new(pattern, case_matters)
+ string *pattern;
+ int case_matters;
+ {
+ char *translate;
+ struct re_pattern_buffer patbuf;
+ char fastmap[256];
+ const char *compile_error;
+ array *result;
+ register value *v;
+ string *s;
+
+ translate = (case_matters ? (char *) 0 : trans_table);
+
+ patbuf.buffer = 0;
+ patbuf.allocated = 0;
+ patbuf.used = 0;
+
+ patbuf.fastmap = fastmap;
+ patbuf.translate = translate;
+
+ patbuf.fastmap_accurate = 0;
+
+ {
+ int i;
+ long n = 0;
+ for (i = 0; i < pattern->len; i++) {
+ switch (pattern->text[i]) {
+ case '[':
+ if (pattern->text[++i] == '^')
+ i++;
+ for (i++; i < pattern->len; i++)
+ if (pattern->text[i] == ']')
+ break;
+ break;
+ case '%':
+ pattern->text[i++] = '\\'; /* skip escaped char */
+ break;
+ case '\\':
+ pattern->text[i] == '%'; /* mark for expansion */
+ n++;
+ break;
+ }
+ }
+ if (n) {
+ int j;
+
+ s = str_new(NULL, pattern->len + n);
+ for (i = j = 0; i < pattern->len; i++, j++) {
+ switch (pattern->text[i]) {
+ case '[':
+ s->text[j++] = pattern->text[i++];
+ if (i == pattern->len)
+ goto breakout;
+ if (pattern->text[i] == '^') {
+ s->text[j++] = pattern->text[i++];
+ if (i == pattern->len)
+ goto breakout;
+ }
+ s->text[j++] = pattern->text[i++];
+ if (i == pattern->len)
+ goto breakout;
+ for ( ; i < pattern->len; i++, j++) {
+ if ((s->text[j] = pattern->text[i]) == ']')
+ break;
+ }
+ break;
+ case '%': /* expand */
+ s->text[j++] = '\\';
+ s->text[j] = '\\';
+ break;
+ case '\\': /* skip escaped char */
+ s->text[j++] = pattern->text[i++];
+ if (i == pattern->len)
+ goto breakout;
+ /* fallthru */
+ default:
+ s->text[j] = pattern->text[i];
+ }
+ }
+ breakout:
+ }
+ }
+ compile_error = re_compile_pattern(s->text, s->len, &patbuf);
+ str_del(s);
+ if (compile_error != (char *) 0)
+ {
+ regfree(&patbuf);
+ error(compile_error);
+ }
+
+ re_compile_fastmap(&patbuf);
+
+ result = arr_new(3L);
+ v = result->elts;
+
+ v->type = T_STRING;
+ str_ref(v->u.string = str_new((char *) &patbuf, (long) sizeof(patbuf)));
+ ++v;
+ v->type = T_STRING;
+ str_ref(v->u.string = str_new((char *) patbuf.buffer,
+ (long) patbuf.allocated));
+ ++v;
+ v->type = T_STRING;
+ str_ref(v->u.string = str_new(fastmap, 256L));
+
+ /* don't let regfree() try to free these */
+ patbuf.fastmap = 0;
+ patbuf.translate = 0;
+
+ regfree(&patbuf);
+
+ return result;
+ }
+
+ /*
+ * NAME: regexp->match()
+ * DESCRIPTION: perform regexp matching, given a pattern and subject string
+ */
+ array *rgx_match(pattern, subject, reverse)
+ value *pattern;
+ string *subject;
+ int reverse;
+ {
+ long sub_len;
+ struct re_pattern_buffer patbuf;
+ struct re_registers regs;
+ regoff_t starts[RGX_NREGS + 1], ends[RGX_NREGS + 1];
+ array *result;
+ register value *v;
+ register int i;
+
+ if (pattern[0].u.string->len != sizeof(struct re_pattern_buffer))
+ error("Invalid compiled pattern");
+
+ memcpy((char *) &patbuf, pattern[0].u.string->text,
+ sizeof(struct re_pattern_buffer));
+
+ if (patbuf.allocated != (unsigned long) pattern[1].u.string->len ||
+ pattern[2].u.string->len != 256)
+ error("Invalid compiled pattern");
+
+ patbuf.buffer = (unsigned char *) pattern[1].u.string->text;
+ patbuf.fastmap = pattern[2].u.string->text;
+
+ regs.num_regs = RGX_NREGS;
+ regs.start = starts;
+ regs.end = ends;
+ patbuf.regs_allocated = REGS_FIXED;
+
+ sub_len = subject->len;
+ if (re_search(&patbuf, subject->text, sub_len, reverse ? sub_len : 0,
+ reverse ? -(sub_len + 1) : sub_len + 1, ®s) == -1)
+ return (array *) 0;
+
+ result = arr_new((long) RGX_NREGS * 2);
+ v = result->elts;
+
+ v->type = T_INT;
+ v->u.number = starts[0];
+ ++v;
+
+ v->type = T_INT;
+ v->u.number = ends[0] - 1;
+ ++v;
+
+ for (i = 1; i < RGX_NREGS; ++i, v += 2)
+ {
+ v[0].type = T_INT;
+ v[1].type = T_INT;
+
+ if (starts[i] == -1)
+ {
+ v[0].u.number = 0;
+ v[1].u.number = -1;
+ }
+ else
+ {
+ v[0].u.number = starts[i];
+ v[1].u.number = ends[i] - 1;
+ }
+ }
+
+ return result;
+ }
*** src.rgx/rgx.h Thu Jan 2 21:42:05 1997
--- rgx.h Fri Feb 3 03:09:54 1995
***************
*** 0 ****
--- 1,5 ----
+ # define RGX_NREGS 10
+
+ extern void rgx_init P((void));
+ extern array *rgx_new P((string*, int));
+ extern array *rgx_match P((value*, string*, int));
*** doc.rgx/example.c Thu Jan 1 00:00:00 1970
--- ../doc/rgx_example.c Fri Feb 3 03:30:01 1995
***************
*** 0 ****
--- 1,49 ----
+ /*
+ * This file shows how an interface can be built to cache regexp patterns
+ * and ultimately provide a more streamlined interface to the regexp kfuns.
+ *
+ * Note that since regexp_match() severely depends on the return result from
+ * regexp_compile() being unaltered, it is a good idea to provide an
+ * interface like this, and also to mask the regexp_match() kfun from the
+ * auto object.
+ */
+
+ # define CACHE_SIZE 10
+
+ private mapping cache;
+ private string *list;
+ private string last_pattern;
+
+ static
+ void create(void)
+ {
+ cache = ([ ]);
+ list = ({ });
+ }
+
+ int *match(string subject, string pattern)
+ {
+ string *buffer;
+
+ if ((buffer = cache[pattern]) == 0)
+ {
+ buffer = regexp_compile(pattern);
+
+ if (sizeof(list) >= CACHE_SIZE)
+ {
+ cache[list[0]] = 0;
+ list = list[1 ..] + ({ pattern });
+ }
+ else
+ list += ({ pattern });
+
+ cache[pattern] = buffer;
+ }
+ else if (pattern != last_pattern)
+ {
+ list = list - ({ pattern }) + ({ pattern });
+ last_pattern = pattern;
+ }
+
+ return regexp_match(buffer, subject);
+ }
diff -crN doc.rgx/kfun/regexp_compile doc/kfun/regexp_compile
*** doc.rgx/kfun/regexp_compile Thu Jan 1 00:00:00 1970
--- ../doc/kfun/regexp_compile Tue Jul 26 00:02:34 1994
***************
*** 0 ****
--- 1,27 ----
+ NAME
+ regexp_compile - compile a regular expression
+
+ SYNOPSIS
+ varargs string *regexp_compile(string pattern, int case_insensitive)
+
+ DESCRIPTION
+ The argument pattern is compiled as a regular expression. If the
+ argument case_insensitive is nonzero, the pattern is compiled in
+ such a way that subsequent matching will be done without case
+ sensitivity. The default is to be case-sensitive.
+
+ An array of strings is returned; these strings contain binary
+ data and must not be altered in any way before being passed to
+ regexp_match().
+
+ The compiled regexp can be saved and used any number of times with
+ regexp_match().
+
+ ERRORS
+ If the argument pattern contains a syntactically malformed regular
+ expression, an error will result. An error can also occur if the
+ pattern is too complicated, or if there is not enough memory to
+ compile the pattern.
+
+ SEE ALSO
+ kfun/regexp_match
*** doc.rgx/kfun/regexp_match Thu Jan 1 00:00:00 1970
--- ../doc/kfun/regexp_match Mon Jul 25 22:19:42 1994
***************
*** 0 ****
--- 1,34 ----
+ NAME
+ regexp_match - perform regular expression matching
+
+ SYNOPSIS
+ varargs int *regexp_match(string *pattern, string subject, int reverse)
+
+ DESCRIPTION
+ The argument subject is matched against the compiled regular
+ expression pattern. If the argument reverse is nonzero, matching
+ is performed from right-to-left; otherwise, matching is performed
+ left-to-right.
+
+ The pattern argument must be an array of strings exactly as it
+ was received from regexp_compile(); otherwise, the result of
+ calling this function is undefined.
+
+ If the argument subject could not be matched with the regular
+ expression, 0 is returned. Otherwise, an array of 20 integers
+ is returned with this format:
+
+ ({ start0, end0, start1, end1, ..., start9, end9 })
+
+ Each element is a character index into the subject string. The
+ first two elements, start0 and end0, indicate the part of the subject
+ that was matched by the regular expression as a whole. The following
+ elements indicate the starting and ending indices of each
+ subexpression (denoted by "%(" and "%)" pairs in the original
+ pattern) that were matched.
+
+ If any subexpression was not matched, the corresponding start and
+ end elements will be 0 and -1, respectively.
+
+ SEE ALSO
+ kfun/regexp_compile
*** doc.rgx/regexps Thu Jan 1 00:00:00 1970
--- ../doc/regexps Mon Jul 25 22:58:57 1994
***************
*** 0 ****
--- 1,32 ----
+
+ Regular expressions are composed of the following operators:
+
+ . Match any single character
+ XY Match X immediately followed by Y
+ X* Match zero-or-more of X
+ X+ Match one-or-more of X
+ X? Match zero-or-one of X
+ X%|Y Match either X or Y
+ [charset] Match any single character in `charset'
+ [^charset] Match any single character not in `charset'
+ %(X%) Match X, but also remember the match as a subexpression
+ %digit Match the numbered previous subexpression
+ ^X Match X anchored at the beginning of a line
+ X$ Match X anchored at the end of a line
+ %b Match the empty string at the beginning or end of a word
+ %B Match the empty string only within the middle of a word
+ %< Match the beginning of a word
+ %> Match the end of a word
+ %w Match any word-constituent character
+ %W Match any character that is not word-constituent
+
+ Any other character in a regular expression is matched literally with itself.
+ To match any of the special operator characters .*+?%[^$ literally, precede
+ the character with `%'.
+
+ A `charset' is formed by listing all desired characters with brackets. To
+ include a literal `^' in a charset, do not list it in the first position. To
+ include a literal `]', list it immediately after the opening `[' or `[^'. All
+ characters are non-special (and should not be escaped) within a charset,
+ except `-', which denotes a character range. To include a literal `-', list it
+ either first or last.
*** README.rgx.old Fri Jan 3 03:17:21 1997
--- ../README.rgx Fri Jan 3 03:14:29 1997
***************
*** 0 ****
--- 1,18 ----
+ dgd-rgx was written by Robert Leslie <rob@ccs.neu.edu> as an LPC interface to
+ GNU regex, adding two kfuns to DGD for regular expression matching:
+
+ regexp_compile()
+ regexp_match()
+
+ For a description of the regular expression language accepted by these kfuns,
+ please read doc/regexps.
+
+ Complete details for the two kfuns can be found in the doc/kfun directory.
+
+ Adapted by Adam David <adam@veda.is> for DGD 1.0.97 and to use the unmodified
+ GNU regexp library.
+
+ This software is a modification of DGD, and is therefore protected by the
+ DGD Copyright.
+
+ There is no warranty for this software.
|