-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsearch.c
621 lines (547 loc) · 13.6 KB
/
search.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
/*_ search.c Tue Nov 17 1987 */
/* $Header$ */
/*
* The functions in this file implement commands that search in the forward
* and backward directions. There are no special characters in the search
* strings. Probably should have a regular expression search, or something
* like that.
*
* REVISION HISTORY:
*
* ? Steve Wilhite, 1-Dec-85
* - massive cleanup on code.
*/
#include <stdio.h>
#include <ctype.h>
#include "ed.h"
#define CASESENSITIVE TRUE /* TRUE means case sensitive */
#ifdef MYIO
extern int Dnoask_search;
#endif
#if CASESENSITIVE
#define eq(a,b) ((a) == (b))
#endif
/*
* Search forward. Get a search string from the user, and search, beginning at
* ".", for the string. If found, reset the "." to be just after the match
* string, and [perhaps] repaint the display. Bound to "C-S".
*/
forwsearch(f, n)
{
register LINE *clp;
register int cbo;
register int len;
register LINE*tlp;
register int tbo;
register int c;
register char *pp;
register int s;
if ((s = readpattern("Search: ",pat)) != TRUE)
return (s);
clp = curwp->w_dotp; /* get pointer to current line */
cbo = curwp->w_doto; /* and offset into that line */
len = llength(clp);
while (clp != curbp->b_linep) /* while not end of buffer */
{
while (cbo < len)
if (eq(lgetc(clp,cbo++),pat[0]))
goto match1;
cbo = 0;
clp = lforw(clp);
len = llength(clp);
if (!eq('\n',pat[0]))
continue;
match1:
{
tlp = clp;
tbo = cbo; /* remember where start of pattern */
for (pp = &pat[1]; *pp; pp++)
{
if (tlp == curbp->b_linep) /* if reached end of buffer */
goto fail;
if (tbo == llength(tlp))
{
tlp = lforw(tlp);
tbo = 0;
c = '\n';
}
else
c = lgetc(tlp, tbo++);
if (!eq(c, *pp))
goto fail;
}
/* We've found it. It starts at clp,cbo and ends before tlp,tbo */
curwp->w_dotp = tlp;
curwp->w_doto = tbo;
curwp->w_flag |= WFMOVE;
return (TRUE);
}
fail:;
}
mlwrite("Not found");
return (FALSE);
}
/*
* Reverse search. Get a search string from the user, and search, starting at
* "." and proceeding toward the front of the buffer. If found "." is left
* pointing at the first character of the pattern [the last character that was
* matched]. Bound to "C-R".
*/
backsearch(f, n)
{
register LINE *clp;
register int cbo;
register LINE *tlp;
register int tbo;
register int c;
register char *epp;
register char *pp;
register int s;
if ((s = readpattern("Reverse search: ",pat)) != TRUE)
return (s);
for (epp = &pat[0]; epp[1] != 0; ++epp)
;
clp = curwp->w_dotp;
cbo = curwp->w_doto;
for (;;)
{
if (cbo == 0)
{
clp = lback(clp);
if (clp == curbp->b_linep)
{
mlwrite("Not found");
return (FALSE);
}
cbo = llength(clp)+1;
}
if (--cbo == llength(clp))
c = '\n';
else
c = lgetc(clp, cbo);
if (eq(c, *epp))
{
tlp = clp;
tbo = cbo;
pp = epp;
while (pp != &pat[0])
{
if (tbo == 0)
{
tlp = lback(tlp);
if (tlp == curbp->b_linep)
goto fail;
tbo = llength(tlp)+1;
}
if (--tbo == llength(tlp))
c = '\n';
else
c = lgetc(tlp, tbo);
if (!eq(c, *--pp))
goto fail;
}
curwp->w_dotp = tlp;
curwp->w_doto = tbo;
curwp->w_flag |= WFMOVE;
return (TRUE);
}
fail:;
}
}
/*
* Compare two characters. The "bc" comes from the buffer. It has it's case
* folded out. The "pc" is from the pattern.
*/
#ifndef eq
eq(bc, pc)
int bc;
int pc;
{
if (bc>='a' && bc<='z')
bc -= 0x20;
if (pc>='a' && pc<='z')
pc -= 0x20;
return (bc == pc);
}
#endif
/*********************************
* Replace occurrences of pat with withpat.
*/
int replacestring(f,n)
{
return replace(FALSE);
}
/********************************
*/
int queryreplacestring(f,n)
{
return replace(TRUE);
}
/*************************
* Do the replacements.
* Input:
* query if TRUE then it's a query-search-replace
*/
int replace(query)
{
register LINE * clp;
register int cbo;
register LINE * tlp;
register int tbo;
register int c;
register char *pp;
register int s;
register int numreplacements;
register int retval;
struct LINE *dotpsave;
int dotosave;
static char withpat[NPAT];
int stop;
#if DLC || __ZTC__
_chkstack(); /* check for stack overflow */
#endif
if ((s = readpattern("Replace: ", pat)) != TRUE)
return (s); /* must have search pattern */
/*withpat[0] = 0;*/ /* no default replacement pattern */
readpattern ("With: ", withpat); /* replacement pattern can be null */
stop = FALSE;
retval = TRUE;
numreplacements = 0;
dotpsave = curwp->w_dotp;
dotosave = curwp->w_doto; /* save original position */
clp = curwp -> w_dotp; /* get pointer to current line */
cbo = curwp -> w_doto; /* and offset into that line */
while (clp != curbp -> b_linep) /* while not end of buffer */
{
/* Compute c, the character at the current position */
if (cbo >= llength (clp)) /* if at end of line */
{
clp = lforw (clp);
cbo = 0;
c = '\n'; /* then current char is a newline */
}
else
c = lgetc (clp, cbo++); /* else get char from line */
if (eq (c, pat[0])) /* if char matches start of pattern */
{
tlp = clp;
tbo = cbo; /* remember where start of pattern */
for (pp = &pat[1]; *pp; pp++)
{
if (tlp == curbp -> b_linep)/* if reached end of buffer */
goto fail;
if (tbo == llength (tlp))
{
tlp = lforw (tlp);
tbo = 0;
c = '\n';
}
else
c = lgetc (tlp, tbo++);
if (!eq (c, *pp))
goto fail;
}
/* We've found it. It starts before clp,cbo and ends */
/* before tlp,tbo */
/* If query, get user input about this */
if (query)
{
curwp->w_dotp= clp;
curwp->w_doto = cbo;
backchar(FALSE,1);
mlwrite("' ' change 'n' continue '!' change rest '.' change and stop ^G abort");
curwp->w_flag |= WFMOVE;
tryagain:
update();
switch (getkey())
{
case 'n': /* don't change, but continue */
goto fail;
/*case 'R':*/ /* enter recursive edit */
case '!': /* change rest w/o asking */
query = FALSE;
/* FALL-THROUGH */
case ' ': /* change and continue to next */
break;
case '.': /* change and stop */
stop = TRUE;
break;
case CTRL('G'): /* abort */
goto abortreplace;
default: /* illegal command */
(*term.t_beep)();
goto tryagain;
}
}
/* Delete the pattern by setting the current position to */
/* the start of the pattern, and deleting 'n' characters */
curwp -> w_flag |= WFHARD;
curwp->w_dotp= clp;
curwp->w_doto = cbo;
if (backchar(FALSE,1) == FALSE || line_delete(pp - pat,FALSE) == FALSE)
goto L1;
/* 'Yank' the replacement pattern back in at dot (also */
/* moving cursor past end of replacement pattern to prevent */
/* recursive replaces). */
for (pp = withpat; *pp; pp++)
if (line_insert(1,*pp) == FALSE)
{
L1:
if (dotpsave == clp)
dotpsave = curwp->w_dotp;
retval = FALSE;
goto abortreplace;
}
/* Take care of case where line_insert() reallocated the line */
if (dotpsave == clp)
dotpsave = curwp->w_dotp;
clp = curwp -> w_dotp;
cbo = curwp -> w_doto; /* continue from end of with text */
numreplacements++;
if (stop)
break;
}
fail: ;
}
abortreplace:
curwp->w_dotp = dotpsave;
curwp->w_doto = dotosave; /* back to original position */
curwp->w_flag |= WFMOVE;
mlwrite ("%d replacements done",numreplacements);
return retval;
}
/*
* Read a pattern. Stash it in the external variable "pat". The "pat" is not
* updated if the user types in an empty line. If the user typed an empty line,
* and there is no old pattern, it is an error. Display the old pattern, in the
* style of Jeff Lomicka. There is some do-it-yourself control expansion.
*/
int readpattern(prompt,pat)
char *prompt,*pat;
{
register char *cp1;
register char *cp2;
register int c;
register int s;
char tpat[NPAT+20];
#ifdef MYIO
if( Dnoask_search )
return( pat[0] != '\0' );
#endif
#if 0
cp1 = &tpat[0]; /* Copy prompt */
cp2 = prompt;
while ((c = *cp2++) != '\0')
*cp1++ = c;
if (pat[0] != '\0') /* Old pattern */
{
*cp1++ = ' ';
*cp1++ = '[';
cp2 = &pat[0];
while ((c = *cp2++) != 0)
{
if (cp1 < &tpat[NPAT+20-6]) /* "??]: \0" */
{
if (c<0x20 || c==0x7F) {
*cp1++ = '^';
c ^= 0x40;
}
else if (c == '%') /* Map "%" to */
*cp1++ = c; /* "%%". */
*cp1++ = c;
}
}
*cp1++ = ']';
}
*cp1++ = ':'; /* Finish prompt */
*cp1++ = ' ';
*cp1++ = '\0';
s = mlreply(tpat, tpat, NPAT); /* Read pattern */
#else
strcpy(tpat,pat);
s = mlreply(prompt,tpat,NPAT);
#endif
if (s == TRUE) /* Specified */
strcpy(pat, tpat);
else if (s == FALSE && pat[0] != 0) /* CR, but old one */
s = TRUE;
return (s);
}
/*********************************
* Examine line at '.'.
* Returns:
* HASH_xxx
* 0 anything else
*/
#define HASH_IF 1
#define HASH_ELIF 2
#define HASH_ELSE 3
#define HASH_ENDIF 4
static int ifhash(clp)
LINE *clp;
{
register int len;
register int i,h;
static char *hash[] = {"if","elif","else","endif"};
len = llength(clp);
if (len < 3 || lgetc(clp,0) != '#')
goto ret0;
for (i = 1; ; i++)
{
if (i >= len)
goto ret0;
if (!isspace(lgetc(clp,i)))
break;
}
for (h = 0; h < arraysize(hash); h++)
if (len - i >= strlen(hash[h]) &&
memcmp(&clp->l_text[i],hash[h],strlen(hash[h])) == 0)
return h + 1;
ret0:
return 0;
}
/*********************************
* Search for the next occurence of the character at '.'.
* If character is a (){}[]<>, search for matching bracket.
* If '.' is on #if, #elif, or #else search for next #elif, #else or #endif.
* If '.' is on #endif, search backwards for corresponding #if.
*/
int search_paren(f, n)
{
register LINE *clp;
register int cbo;
register int len;
register int i;
char chinc,chdec,ch;
int count;
int forward;
int h;
static char bracket[][2] = {{'(',')'},{'<','>'},{'[',']'},{'{','}'}};
clp = curwp->w_dotp; /* get pointer to current line */
cbo = curwp->w_doto; /* and offset into that line */
count = 0;
len = llength(clp);
if (cbo >= len)
chinc = '\n';
else
chinc = lgetc(clp,cbo);
if (cbo == 0 && (h = ifhash(clp)) != 0)
{ forward = h != HASH_ENDIF;
}
else
{
if (inword())
{ // Search for word the cursor is currently on
int s;
do
s = backchar(FALSE, 1);
while (s && inword());
if (s && forwchar(FALSE, 1))
{
int start = curwp->w_doto;
if (word_forw(FALSE, 1))
{
cbo = curwp->w_doto;
int i;
for (i = 0; i < NPAT - 1 && start + i < cbo; i++)
{
pat[i] = lgetc(clp, start + i);
}
pat[i] = 0;
if (Dsearchagain(f, n))
return backchar(FALSE, 1);
}
}
mlwrite("Not found");
return FALSE;
}
forward = TRUE; /* forward */
h = 0;
chdec = chinc;
for (i = 0; i < 4; i++)
if (bracket[i][0] == chinc)
{ chdec = bracket[i][1];
break;
}
for (i = 0; i < 4; i++)
if (bracket[i][1] == chinc)
{ chdec = bracket[i][0];
forward = FALSE; /* search backwards */
break;
}
}
while (1) /* while not end of buffer */
{
if (forward)
{
if (h || cbo >= len)
{
clp = lforw(clp);
if (clp == curbp->b_linep) /* if end of buffer */
break;
len = llength(clp);
cbo = 0;
}
else
cbo++;
}
else /* backward */
{
if (h || cbo == 0)
{
clp = lback(clp);
if (clp == curbp->b_linep)
break;
len = llength(clp);
cbo = len;
}
else
--cbo;
}
if (h)
{ int h2;
cbo = 0;
h2 = ifhash(clp);
if (h2)
{ if (h == HASH_ENDIF)
{
if (h2 == HASH_ENDIF)
count++;
else if (h2 == HASH_IF)
{ if (count-- == 0)
goto found;
}
}
else
{ if (h2 == HASH_IF)
count++;
else
{ if (count == 0)
goto found;
if (h2 == HASH_ENDIF)
count--;
}
}
}
}
else
{
ch = (cbo < len) ? lgetc(clp,cbo) : '\n';
if (eq(ch,chdec))
{ if (count-- == 0)
{
/* We've found it */
found:
curwp->w_dotp = clp;
curwp->w_doto = cbo;
curwp->w_flag |= WFMOVE;
return (TRUE);
}
}
else if (eq(ch,chinc))
count++;
}
}
mlwrite("Not found");
return (FALSE);
}