/
srch.c
740 lines (600 loc) · 19.8 KB
/
srch.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
* TECO for Ultrix Copyright 1986 Matt Fichtenbaum
* This program and its components belong to GenRad Inc, Concord MA 01742
* They may be copied if this copyright notice is included
*/
/* srch.c routines associated with search operations 2/5/86 */
#include <ctype.h>
#include "defs.h"
static int srch_cmp(void);
/*
* routine to read in a string with string-build characters
* used for search, tag, file name operations
* returns 0 if empty string entered, nonzero otherwise
*
* lbuff - addr of q-reg header
*/
int
build_string(struct qh *lbuff)
{
int count; /* char count */
struct buffcell *tp; /* pointer to temporary string */
char c; /* temp character */
/* read terminator */
term_char = (atflag) ? getcmdc(trace_sw) : ESC;
count = atflag = 0; /* initialize char count */
/* if string is not empty */
if (!peekcmdc(term_char)) {
/*
* Create a temporary string and read chars into
* it until the terminator
*/
for (tp = bb.p = get_bcell(), bb.c = 0;
(c = getcmdc(trace_sw)) != term_char; ) {
/* read next char as CTL */
if ((c == '^') && !(ed_val & ED_CARET)) {
if ((c = getcmdc(trace_sw)) == term_char) {
ERROR(msp <= &mstack[0] ? E_UTC : E_UTM);
}
c &= 0x1f;
}
/* if a control char */
if ((c &= 0177) < ' ') {
switch (c) {
/* take next char literally */
case CTL('Q'):
case CTL('R'):
if ((c = getcmdc(trace_sw)) == term_char) {
ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
}
/* fetch character and go store */
break;
/* take next char as lower case */
case CTL('V'):
if (getcmdc(trace_sw) == term_char) {
ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
}
c = mapch_l[cmdc & 0xFF];
break;
/* take next char as upper case */
case CTL('W'):
if ((c = getcmdc(trace_sw)) == term_char) {
ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
}
if (islower(c)) {
c = toupper(c);
}
break;
/* expanded constructs */
case CTL('E'):
if (getcmdc(trace_sw) == term_char) {
ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
}
switch (mapch_l[cmdc & 0xFF]) {
/* use char in q-reg */
case 'u':
if (getcmdc(trace_sw) ==
term_char) ERROR((msp
<= &mstack[0])
? E_UTC :
E_UTM);
c = qreg[getqspec(1,
cmdc)].v & 0x7f;
break;
/* use string in q-reg */
case 'q':
if (getcmdc(trace_sw) == term_char) {
ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
}
/* read the reg spec */
ll = getqspec(1, cmdc);
/* set a pointer to it */
aa.p = qreg[ll].f;
aa.c = 0;
for (mm = 0; mm < qreg[ll].z; mm++) {
/* store char */
bb.p->ch[bb.c] = aa.p->ch[aa.c];
/* store next char */
fwdcx(&bb);
fwdc(&aa);
++count;
}
/*
* Repeat loop without
* storing
*/
continue;
default:
/* not special: store the ^E */
bb.p->ch[bb.c] = CTL('E');
fwdcx(&bb);
++count;
/*
* And go store the following
* char
*/
c = cmdc;
break;
}
}
}
/* store character */
bb.p->ch[bb.c] = c;
/* advance pointer */
fwdcx(&bb);
/* count characters */
++count;
}
free_blist(lbuff->f); /* return old buffer */
lbuff->f = tp; /* put in new one */
lbuff->f->b = (struct buffcell *) lbuff;
/* store count of chars in string */
lbuff->z = count;
} else {
/* empty string: consume terminator */
getcmdc(trace_sw);
}
/* return char count */
return(count);
}
/*
* routine to handle end of a search operation
* called with pass/fail result from search
* returns same pass/fail result
*/
int
end_search(int result)
{
/* if search failed */
if (!result) {
/* if an unbounded search failed, clear ptr */
if (!(esp->flag2 || (ed_val & ED_SFAIL))) {
dot = 0;
}
/* if no real or implied colon, error if failure */
if (!colonflag && !peekcmdc(';')) {
ERROR(E_SRH);
}
}
/* return a value if a :S command */
esp->flag1 = colonflag;
srch_result = esp->val1 = result; /* and leave it for next ";" */
esp->flag2 = colonflag = atflag = 0; /* consume arguments */
esp->op = OP_START;
return(result);
}
/*
* routine to set up for search operation
* reads search arguments, returns search count
*/
static struct qp sm, sb; /* match-string and buffer pointers */
static char *pmap; /* pointer to character mapping table */
static int locb; /* reverse search limit */
static int last_z; /* end point for reverse search */
int
setup_search(void)
{
int count; /* string occurrence counter */
/* set a pointer to start of search */
set_pointer(dot, &aa);
/* ::S is 1,1S */
if (colonflag >= 2) {
esp->flag2 = esp->flag1 = esp->val2 = esp->val1 = 1;
}
/* read search count: default is 1 */
if ((count = get_value(1)) == 0) {
ERROR(E_ISA);
/* search forward */
} else if (count > 0) {
/* if bounded search */
if (esp->flag2) {
/* set limit */
if (esp->val2 < 0) {
esp->val2 = -(esp->val2);
}
/* or z, whichever less */
if ((aa.z = dot + esp->val2) > z) {
aa.z = z;
}
} else {
aa.z = z;
}
} else {
/* if bounded search */
if (esp->flag2) {
/* set limit */
if (esp->val2 < 0) {
esp->val2 = -(esp->val2);
}
/* or 0, whichever greater */
if ((locb = dot - esp->val2) < 0) {
locb = 0;
}
} else {
locb = 0;
}
}
return(count);
}
/*
* routine to do N, _, E_ searches: search, if search fails, then get
* next page and continue
*
* arg - 'n', '_', or 'e' to define which search
*/
int
do_nsearch(char arg)
{
int scount; /* search count */
/* read the search string */
build_string(&sbuf);
/* count must be >0 */
if ((scount = get_value(1)) <= 0) {
ERROR(E_ISA);
}
/* start search at dot */
set_pointer(dot, &aa);
/* make it unbounded */
esp->flag2 = locb = 0;
/* search until found */
while (scount > 0) {
/* search forwards */
if (!do_search(1)) {
/* if search fails... */
/* if no input, quit */
if (infile->eofsw || !infile->fd) {
break;
}
if (arg == 'n') {
set_pointer(0, &aa); /* write file if 'n' */
write_file(&aa, z, ctrl_e);
/*
* Not 'n': if _, and an output file,
* and data to lose, error
*/
} else if ((arg == '_') && (outfile->fd) && (z) &&
(ed_val & ED_YPROT)) {
ERROR(E_YCA);
}
/* clear buffer */
buff_mod = dot = z = 0;
set_pointer(0, &aa);
/* read next page */
read_file(&aa, &z, (ed_val & ED_EXPMEM ? -1 : 0) );
/* search next page from beginning */
set_pointer(0, &aa);
} else {
/* search successful: one fewer to look for */
--scount;
}
}
/* use end_search to clean up */
return( end_search( (scount == 0) ? -1 : 0) );
}
/*
* routine to do "FB" search - m,nFB is search from m to n,
* nFB is search from . to nth line
* convert arguments to args of normal m,nS command
*
* returns search result
*/
int
do_fb(void)
{
/* if two arguments */
if (esp->flag1 && esp->flag2) {
/* start from "m" arg */
dot = esp->val2;
/* get number of chars */
esp->val2 = esp->val1 - esp->val2;
} else {
/* if no or one args, treat as number of lines */
/* number of chars */
esp->val2 = lines(get_value(1));
/* conjure up two args */
esp->flag2 = esp->flag1 = 1;
}
esp->val1 = (esp->val2 > 0) ? 1 : -1; /* set search direction */
/* read search string and terminator */
build_string(&sbuf);
/* do search and return result */
return(end_search( do_search( setup_search() ) ));
}
/*
* routine to do search operation: called with search count as argument
* returns -1 (pass) or 0 (fail)
*/
int
do_search(int count)
{
/* set approp. mapping table */
pmap = (ctrl_x) ? &mapch[0] : &mapch_l[0];
/* copy # of chars in search buffer */
sm.z = sbuf.z;
if (count > 0) {
/* loop to count occurrences */
for (sm.dot = 0; count > 0; count--) {
/* loop to advance search pointer */
for (; aa.dot < aa.z; aa.dot++) {
sb.p = aa.p; sb.c = aa.c; sb.dot = aa.dot;
sm.p = sbuf.f; sm.dot = sm.c = 0;
/* for each char in search string */
for ( ; (sb.dot < z) && (sm.dot < sm.z);
sm.dot++, sb.dot++) {
/* if search string char is "special" */
if (spec_chars[sm.p->ch[sm.c & 0xFF] & 0xFF] & A_A) {
/*
* Then use expanded comparison
* routine
*/
if (!srch_cmp()) {
break;
}
} else if (*(pmap + sb.p->ch[sb.c]) !=
*(pmap + sm.p->ch[sm.c])) {
break;
}
/* else just compare */
/* advance search-string ptr */
if (++sm.c > CELLSIZE-1) {
sm.p = sm.p->f;
sm.c = 0;
}
/* advance buffer ptr */
if (++sb.c > CELLSIZE-1) {
sb.p = sb.p->f;
sb.c = 0;
}
}
/* exit if found */
if (sm.dot >= sm.z) {
break;
}
/* else not found: advance buffer pointer */
if (++aa.c > CELLSIZE-1) {
aa.p = aa.p->f;
aa.c = 0;
}
}
/* if one search failed, don't do more */
if (sm.dot < sm.z) {
break;
} else {
/* otherwise save -length of string found */
ctrl_s = aa.dot - sb.dot;
/* if funny "advance by 1" mode */
if ((ed_val & ED_SMULT) && (count > 1)) {
/* advance buffer pointer by one only */
++aa.dot;
if (++aa.c > CELLSIZE-1) {
aa.p = aa.p->f;
aa.c = 0;
}
} else {
/* advance search pointer past string */
aa.dot = sb.dot;
aa.p = sb.p;
aa.c = sb.c;
}
}
}
} else {
/* search backwards */
/* loop to count occurrences */
for (last_z = z, sm.dot = 0; count < 0; count++) {
/* loop to advance (backwards) search pointer */
for (; aa.dot >= locb; aa.dot--) {
sb.p = aa.p; sb.c = aa.c; sb.dot = aa.dot;
sm.p = sbuf.f; sm.dot = sm.c = 0;
/* loop to compare string */
for ( ; (sb.dot < last_z) && (sm.dot < sm.z);
sm.dot++, sb.dot++) {
/* if search string char is "special" */
if (spec_chars[sm.p->ch[sm.c & 0xFF] & 0xFF] & A_A) {
/*
* Then use expanded comparison
* routine
*/
if (!srch_cmp()) {
break;
}
} else if (*(pmap + sb.p->ch[sb.c])
!= *(pmap + sm.p->ch[sm.c])) {
/* else just compare */
break;
}
/* advance search-string ptr */
if (++sm.c > CELLSIZE-1) {
sm.p = sm.p->f;
sm.c = 0;
}
/* advance buffer ptr */
if (++sb.c > CELLSIZE-1) {
sb.p = sb.p->f;
sb.c = 0;
}
}
/* search matches: */
if (sm.dot >= sm.z) {
/*
* Set last_z to point where this
* string was found
*/
if (!(ed_val & ED_SMULT)) {
last_z = aa.dot;
}
break;
}
/* or if string is beyond end of buffer */
if (sb.dot >= last_z) {
/*
* Make search appear to have
* succeeded
*/
sm.dot = sm.z;
/*
* So as to back up pointer, and
* force one more look
*/
--count;
break;
}
/* else advance buffer pointer (backwards) */
if (--aa.c < 0) {
aa.p = aa.p->b;
aa.c = CELLSIZE-1;
}
}
/* if one search failed, don't do more */
if (sm.dot < sm.z) {
break;
} else {
/*
* If this is not last search, back
* pointer up one
*/
if (count < -1) {
backc(&aa);
} else {
/*
* Otherwise save -length of string
* found
*/
ctrl_s = aa.dot - sb.dot;
/* advance pointer past string */
aa.dot = sb.dot;
aa.p = sb.p;
aa.c = sb.c;
}
}
}
}
/* if search succeeded, update pointer */
if (sm.dot >= sm.z) {
dot = aa.dot;
}
/* set "search occurred" (for ES) */
search_flag = 1;
/* and return -1 (pass) or 0 (fail) */
return((sm.dot >= sm.z) ? -1 : 0);
}
/*
* expanded search comparison
* returns 1 if match, 0 if not
*/
static int
srch_cmp(void)
{
int tq; /* q-reg name for ^EGq */
struct qp tqp; /* pointer to read q reg */
/* what is search character */
switch (mapch_l[sm.p->ch[sm.c & 0xFF] & 0xFF]) {
/* match anything but following construct */
case CTL('N'):
/* don't read past end of string */
if (sm.dot >= sm.z) {
ERROR(E_ISS);
}
/* skip the ^N */
fwdc(&sm);
return(!srch_cmp());
/* match any character */
case CTL('X'):
return(1);
/* take next char literally */
case CTL('Q'):
case CTL('R'):
/* don't read past end of string */
if (sm.dot >= sm.z) {
ERROR(E_ISS);
}
/* skip the ^Q */
fwdc(&sm);
return(*(pmap + sb.p->ch[sb.c]) ==
*(pmap + sm.p->ch[sm.c]));
/* match any nonalphanumeric */
case CTL('S'):
return(!isalnum(sb.p->ch[sb.c]));
case CTL('E'):
/* don't read past end of string */
if (sm.dot >= sm.z)
ERROR(E_ISS);
/* skip the ^E */
fwdc(&sm);
switch (mapch_l[sm.p->ch[sm.c & 0xFF] & 0xFF]) {
/* match any alpha */
case 'a':
return(isalpha(sb.p->ch[sb.c]));
/* match any nonalpha */
case 'b':
return(!isalnum(sb.p->ch[sb.c]));
/* rad50 symbol constituent */
case 'c':
return(!isalnum(sb.p->ch[sb.c]) ||
(sb.p->ch[sb.c] == '$') ||
(sb.p->ch[sb.c] == '.'));
/* digit */
case 'd':
return(isdigit(sb.p->ch[sb.c]));
/* line terminator LF, VT, FF, CR */
case 'l':
return((sb.p->ch[sb.c] == LF) ||
(sb.p->ch[sb.c] == FF) ||
(sb.p->ch[sb.c] == CR) ||
(sb.p->ch[sb.c] == VT));
/* alphanumeric */
case 'r':
return(isalnum(sb.p->ch[sb.c]));
/* lower case */
case 'v':
return(islower(sb.p->ch[sb.c]));
/* upper case */
case 'w':
return(isupper(sb.p->ch[sb.c]));
/* any non-null string of spaces or tabs */
case 's':
/* failure */
if (((sb.p->ch[sb.c]&0177) != ' ') &&
((sb.p->ch[sb.c]&0177) != TAB)) {
return(0);
}
/* skip remaining spaces or tabs */
for ( fwdc(&sb);
((sb.p->ch[sb.c]&0177) == ' ') ||
((sb.p->ch[sb.c]&0177) == TAB);
fwdc(&sb) ) {
;
}
/*
* Back up one char (calling routine will
* skip it)
*/
backc(&sb);
return(1); /* success */
/* any char in specified q register */
case 'g':
/* don't read past end of string */
if (sm.dot >= sm.z) {
ERROR(E_ISS);
}
/* get to the next char */
fwdc(&sm);
/* read q-reg spec */
tq = getqspec(1, sm.p->ch[sm.c]);
for (tqp.dot = tqp.c = 0, tqp.p = qreg[tq].f;
tqp.dot < qreg[tq].z;
fwdc(&tqp)) {
/* match */
if (*(pmap + tqp.p->ch[tqp.c]) ==
*(pmap + sb.p->ch[sb.c])) {
return(1);
}
}
return(0); /* fail */
default:
ERROR(E_ISS);
}
default:
return(*(pmap + sb.p->ch[sb.c]) == *(pmap + sm.p->ch[sm.c]));
}
}