-
Notifications
You must be signed in to change notification settings - Fork 1
/
re.c
664 lines (620 loc) · 13.5 KB
/
re.c
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* re - regular expression search for bvi
*
* NOTE: Edit this file with tabstop=4 !
*
* 1996-01-06 created;
* 1999-01-19 V 1.1.0
* 1999-03-17 V 1.1.1
* 1999-09-10 V 1.2.0
* 2000-04-25 V 1.3.0 beta
* 2000-09-29 V 1.3.0 final
* 2010-06-02 V 1.3.4
* 2013-08-24 V 1.4.0
* 2019-01-28 V 1.4.1
*
* Copyright 1996-2019 by Gerhard Buergmann
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 3, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* See file COPYING for information on distribution conditions.
*/
/* You cannot use a common regexp subroutine, because \0 is a regular
* character in a binary string !
*/
#include "bvi.h"
#include "set.h"
static int sbracket();
char act_pat[MAXCMD]; /* found pattern */
char pattern[MAXCMD + 1];
char search_pat[BUFFER]; /* / or ? command */
char *notfound = "Fail|Pattern not found";
char *noprev = "No previous expression";
char *emptyclass = "Bad character class|Empty byte class '[]' or '[^]' cannot match";
PTR
bregexec(start, scan)
PTR start;
char *scan;
{
char *act;
int count, test;
act = act_pat;
while (*scan != 0) {
switch (*scan++) {
case ONE: /* exactly one character */
count = *scan++;
if (P(P_IC) && smode == ASCII) test = toupper(*start);
else test = *start;
if (count == 1) {
/* if (test != *scan) return 0; */
if (test != *scan) return NULL;
scan++;
} else if (count > 1) {
/* if (sbracket(test, scan, count)) return 0; */
if (sbracket(test, scan, count)) return NULL;
scan += count;
}
*act++ = *start++;
break;
case STAR: /* zero or more characters */
count = *scan++;
if (P(P_IC) && smode == ASCII) test = toupper(*start);
else test = *start;
if (count == 1) { /* only one character, 0 - n times */
while(test == *scan) {
*act++ = *start++;
if (P(P_IC) && smode == ASCII) test = toupper(*start);
else test = *start;
}
scan++;
} else if (count > 1) { /* characters in bracket */
if (*scan == '^') {
while (start < maxpos) {
if (bregexec(start, scan + count)) {
*act = '\0';
/* return 1; */
return start;
}
/* if (sbracket(test, scan, count)) return 0; */
if (sbracket(test, scan, count)) return NULL;
*act++ = *start++;
if (P(P_IC) && smode == ASCII) test = toupper(*start);
else test = *start;
}
} else {
while(!sbracket(test, scan, count)) {
*act++ = *start++;
if (P(P_IC) && smode == ASCII) test = toupper(*start);
else test = *start;
}
scan += count;
}
} else { /* ".*" */
while (start < maxpos) {
/* if (bregexec(start, scan)) { *act = '\0'; return 1; } */
if (bregexec(start, scan)) {
*act = '\0';
return start;
}
start++;
}
}
}
}
*act = '\0';
return start; /* found */
/* return 1; */
}
static int
sbracket(start, scan, count)
int start;
char *scan;
int count;
{
if (*scan++ == '^') {
if (!memchr(scan, start, --count)) return 0;
} else {
if (memchr(scan, start, --count)) return 0;
}
return 1;
}
PTR
end_word(start)
PTR start;
{
PTR pos;
pos = start;
if (!isprint(*pos & 0xff)) return start;
while (isprint(*pos & 0xff)) if (pos++ > maxpos) return start;
return --pos;
}
/* wordsearch serves the 'W' and 'w' - command
*/
PTR
wordsearch(start, mode)
PTR start;
char mode;
{
PTR found;
PTR pos;
int ccount;
pos = start + 1;
do {
while (isprint(*pos & 0xff)) if (pos++ > maxpos) return start;
while (!isprint(*pos & 0xff)) if (pos++ > maxpos) return start;
found = pos;
ccount = 0;
while (isprint(*pos & 0xff)) {
if (pos++ > maxpos) return start;
ccount++; }
if (ccount < P(P_WL)) continue;
if (mode == 'W') {
if (*pos == '\0' || *pos == '\n') return found;
} else {
return found;
}
} while (pos < maxpos);
return start;
}
/* backsearch serves the 'b' and 'B' command
*/
PTR
backsearch(start, mode)
PTR start;
char mode;
{
PTR pos;
int ccount;
pos = start - 1;
do {
if (mode == 'B') {
while (*pos != '\0' && *pos != '\n') if (pos-- < mem) return start;
} else {
while (!isprint(*pos & 0xff)) if (pos-- < mem) return start;
}
pos--;
ccount = 0;
while (isprint(*pos & 0xff)) {
if (pos-- < mem) return start;
ccount++; }
if (ccount >= P(P_WL)) return (pos + 1);
} while (pos > mem);
return start;
}
/* used by :s
*/
int
do_substitution(delim, line, startpos, endpos)
int delim;
char *line;
PTR startpos;
PTR endpos;
{
int n;
char *found;
char *cmd = NULL;
int repl_count = 0;
int global = 0;
int conf = 0;
static int direct;
static int pat_len = -1;
static int ch;
static char find_pat[BUFFER];
static char repl_pat[MAXCMD];
ignore_case = P(P_IC);
magic = P(P_MA);
switch (delim) {
case '/':
case '?':
ch = delim;
direct = (ch == '/' ? FORWARD : BACKWARD);
cmd = patcpy(pattern, line, ch);
if (pattern[0] == '\0') break;
if (ascii_comp(find_pat, pattern)) return 0;
cmd = patcpy(pattern, cmd, ch); /* Replace Pattern */
poi = pattern;
pat_len = 0;
while (*poi) {
if (*poi == '\\') {
switch (*(++poi)) {
case '0': repl_pat[pat_len++] = '\0'; break;
case 'n': repl_pat[pat_len++] = '\n'; break;
case 'r': repl_pat[pat_len++] = '\r'; break;
case 't': repl_pat[pat_len++] = '\t'; break;
case '\\': repl_pat[pat_len++] = '\\'; break;
default: sprintf(string,
"No such escape sequence \\%c", *poi);
emsg(string);
return 0;
}
} else {
repl_pat[pat_len++] = *poi;
}
poi++;
}
break;
case '#':
case '\\':
ch = delim;
direct = (ch == '\\' ? FORWARD : BACKWARD);
cmd = patcpy(pattern, line, ch);
if (hex_comp(find_pat, pattern)) return 0;
cmd = patcpy(pattern, cmd, ch); /* Replace Pattern */
poi = pattern;
pat_len = 0;
while (*poi) {
if (*poi == ' ' || *poi == '\t') {
poi++;
} else {
if ((n = hexchar()) < 0) {
emsg("Badly formed replacement pattern");
return 0;
}
repl_pat[pat_len] = n;
pat_len++;
}
}
break;
case '\0':
case 'g':
case 'c':
break;
default:
emsg("Extra chars|Extra characters at end of command");
return -1;
}
if (pat_len == -1) {
emsg("No previous substitute re|No previous substitute to repeat");
return -1;
}
if (delim != '\0') {
if (strchr(cmd, 'g')) global = 1;
if (strchr(cmd, 'c')) conf = 1;
}
if ((strchr("\\#", ch) && loc == ASCII)
|| (strchr("/?", ch) && loc == HEX)) {
toggle();
}
startpos--;
move(maxy, 0);
refresh();
if (global) {
if ((undo_count = alloc_buf(endpos - startpos, &undo_buf))) {
memcpy(undo_buf, startpos + 1, undo_count);
}
undo_start = startpos + 1;
edits = U_EDIT;
}
AGAIN:
if (direct == FORWARD) {
found = fsearch(startpos + 1, endpos, find_pat);
} else {
found = rsearch(startpos - 1, mem, find_pat);
}
if (!found) {
if (!repl_count) {
if (P(P_WS)) {
emsg(notfound);
} else {
if (P(P_TE))
sprintf(string, "No match to %s", direct == FORWARD ? "BOTTOM" : "TOP");
else
sprintf(string, "Address search hit %s without matching pattern",
direct == FORWARD ? "BOTTOM" : "TOP");
emsg(string);
}
}
return repl_count;
} else {
setpage(found);
if (conf) {
repaint();
msg("Replace?");
move(y, x);
if (vgetc() != 'y') goto SKIP;
}
repl_count++;
current_start = pagepos + y * Anzahl + xpos();
if (!global) {
if ((undo_count = alloc_buf(pat_len, &undo_buf))) {
memcpy(undo_buf, current_start, undo_count);
}
undo_start = current_start;
edits = U_EDIT;
}
memcpy(current_start, repl_pat, pat_len);
SKIP:
if (global) {
startpos = found + pat_len - 1;
goto AGAIN;
}
}
return repl_count;
}
/*
* Used by /, ?, \, #, n or N command
* ch is this command
* line are the characters after
*
* return address found
*/
PTR
searching(ch, line, startpos, endpos, flag)
int ch;
char *line;
PTR startpos;
PTR endpos;
int flag;
{
char *cmd = NULL;
PTR found;
int sdir;
static char m[2];
static int direct;
if (line[0] == '\0' && again == 0) {
emsg(noprev);
return 0L;
}
ignore_case = (P(P_IC));
magic = P(P_MA);
start_addr--;
if ((strchr("\\#", ch) && loc == ASCII)
|| (strchr("/?", ch) && loc == HEX)) {
toggle();
}
if (!strchr("Nn", ch)) {
m[0] = ch;
m[1] = '\0';
switch (ch) {
case '/':
case '?':
direct = (ch == '/' ? FORWARD : BACKWARD);
cmd = patcpy(pattern, line, ch);
if (pattern[0] != '\0') {
if (ascii_comp(search_pat, pattern)) return 0L;
again = 1;
}
break;
case '#':
case '\\':
direct = (ch == '\\' ? FORWARD : BACKWARD);
cmd = patcpy(pattern, line, ch);
if (pattern[0] != '\0') {
if (hex_comp(search_pat, pattern)) break;
again = 1;
}
break;
}
if (!again) return 0L;
} else {
cmd = "";
msg(m);
}
move(maxy, 0);
refresh();
sdir = (ch == 'N') ? !direct : direct;
if (sdir == FORWARD) {
found = fsearch(startpos + 1, endpos, search_pat);
if (flag & S_GLOBAL) return(found);
if (!found)
if (flag & 1) {
msg("Search wrapped BOTTOM|Search wrapped around BOTTOM of buffer");
found = fsearch(mem, startpos, search_pat);
}
} else {
found = rsearch(startpos - 1, mem, search_pat);
if (flag & S_GLOBAL) return(found);
if (!found)
if (flag & 1) {
msg("Search wrapped TOP|Search wrapped around TOP of buffer");
found = rsearch(endpos, startpos, search_pat);
}
}
if (!found) {
if (flag & 1) {
emsg(notfound);
} else {
if (P(P_TE)) {
sprintf(string, "No match to %s", sdir == FORWARD ? "BOTTOM" : "TOP");
} else {
sprintf(string, "Address search hit %s without matching pattern",
sdir == FORWARD ? "BOTTOM" : "TOP");
}
emsg(string);
}
} else {
setpage(found);
if (cmd) {
switch(*cmd) {
case 'z': do_z(*++cmd);
break;
case 's': do_substitution(ch, cmd + 2, found, endpos);
repaint();
break;
case ';': searching(*(cmd + 1), cmd + 2, found, maxpos - 1, flag);
case '\0': break;
default: beep();
}
}
}
return found;
}
/* Copies a string from s2 to s1, up to delim or 0
* returns pointer to next character
*/
char *
patcpy(s1, s2, delim)
char *s1, *s2;
char delim;
{
while (*s2 != '\0' && *s2 != delim) {
if (*s2 == '\\' && *(s2 + 1) == delim) s2++;
*s1++ = *s2++;
}
*s1 = '\0';
if (*s2 == delim) s2++;
return s2;
}
PTR
fsearch_end(start, end, smem, s_end)
/*
fsearch(start, end, smem)
*/
PTR start;
PTR end;
char *smem;
PTR *s_end;
{
PTR spos;
signal(SIGINT, jmpproc);
for (spos = start; spos <= end; spos++) {
/* if (bregexec(spos, smem)) { */
if (*s_end = bregexec(spos, smem)) {
signal(SIGINT, SIG_IGN);
*s_end++;
return(spos);
}
}
signal(SIGINT, SIG_IGN);
return(NULL);
}
PTR
fsearch(start, end, smem)
PTR start;
PTR end;
char *smem;
{
PTR s_end;
return fsearch_end(start, end, smem, &s_end);
}
PTR
rsearch(start, end, smem)
PTR start;
PTR end;
char *smem;
{
PTR spos;
signal(SIGINT, jmpproc);
for (spos = start; spos >= end; spos--) {
if (bregexec(spos, smem)) {
signal(SIGINT, SIG_IGN);
return(spos);
}
}
signal(SIGINT, SIG_IGN);
return(NULL);
}
/* Calculates an address of a colon command
* returns NULL on error or default_address, if nothing found
*/
PTR
calc_addr(pointer, def_addr)
char **pointer;
PTR def_addr;
{
PTR addr;
int ch, mark;
char *cmd;
cmd = *pointer;
addr = def_addr;
SKIP_WHITE
if (*cmd >= '1' && *cmd <= '9') {
addr = mem + strtoll(cmd, &cmd, 10) - P(P_OF);
} else {
ch = *cmd;
switch (ch) {
case '.': /* Current position */
addr = current;
cmd++;
break;
case '^':
addr = mem;
cmd++;
break;
case '$':
addr = maxpos - 1;
cmd++;
break;
case '\'': /* Mark */
mark = (*++cmd);
if (mark == '\'') {
addr = last_motion;
cmd++;
break;
} else if (mark < 'a' || mark > 'z') {
emsg("Marks are ' and a-z");
return NULL;
}
if (markbuf[mark - 'a'] == NULL) {
emsg("Mark not defined");
return NULL;
}
addr = markbuf[mark - 'a'];
cmd++;
break;
case '\\':
case '/':
cmd = patcpy(pattern, cmd + 1, ch);
if (pattern[0] == '\0' && again == 0) {
emsg(noprev);
return NULL;
}
if (pattern[0] != '\0') {
again = 1;
if (ch == '/') {
if (ascii_comp(search_pat, pattern)) return NULL;
} else {
if (hex_comp(search_pat, pattern)) return NULL;
}
}
addr = fsearch(mem, maxpos - 1, search_pat);
break;
case '#':
case '?':
cmd = patcpy(pattern, cmd + 1, ch);
if (pattern[0] == '\0' && again == 0) {
emsg(noprev);
return NULL;
}
if (pattern[0] != '\0') {
again = 1;
if (ch == '?') {
if (ascii_comp(search_pat, pattern)) return NULL;
} else {
if (hex_comp(search_pat, pattern)) return NULL;
}
}
addr = rsearch(maxpos - 1, mem, search_pat);
break;
case '0':
addr = mem + strtol(cmd, &cmd, 16) - P(P_OF);
break;
}
}
SKIP_WHITE
while (*cmd == '+' || *cmd == '-') {
if (*cmd == '+') {
cmd++;
SKIP_WHITE
addr += strtoll(cmd, &cmd, 0);
} else {
cmd++;
SKIP_WHITE
addr -= strtoll(cmd, &cmd, 0);
}
SKIP_WHITE
}
if (*pointer != cmd) {
*pointer = cmd;
addr_flag++;
}
return addr;
}