-
Notifications
You must be signed in to change notification settings - Fork 4
/
rsearch.c
389 lines (350 loc) · 8.18 KB
/
rsearch.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
/*
Search commands of uE.
pm: It now uses only regular expression search, and search/replace.
*/
#include "ue.h"
#include "regexp.h"
uchar pat[NPAT]; /* Pattern */
static regexp *repat = NULL; /* compiled reg exp of pat[] */
static void failed(void)
{
mlwrite("Search failed: \"%s\"", pat);
}
void casesensitize(uchar *tpat)
{
uchar *p;
uchar *q;
uchar *r;
unsigned char c;
if (varnum("case_insensitive"))
{ /* replace each lower letter x by [xX] */
r = malloc(((uint) (4 * strlen(tpat) + 1)));
if (r)
{
for (p = r, q = tpat; (c = *q++) != 0; )
{
if ('a' <= c && c <= 'z')
{
*p++ = '[';
*p++ = c;
*p++ = c - 'a' + 'A';
*p++ = ']';
} else
{
*p++ = c;
}
}
*p = '\0';
if (strlen(r) < 2 * NPAT)
strcpy(tpat, r);
free(r);
}
}
}
/* pm: 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. To
escape metachars, you precede them by ^Q; the escape char in regex(3)
is '\\' */
static int readpattern(uchar *prompt)
{
int s;
uchar *p;
uchar *q;
unsigned char c;
uchar tpat[2 * NPAT];
tpat[0] = '\0';
if (pat[0] == '\0')
s = mlreply(sprintp("%s: ", prompt), tpat, NPAT);
else
s = mlreply(sprintp("%s: (default %s) ", prompt, pat), tpat, NPAT);
if (s == '\007')
return ABORT;
if (tpat[0])
{
strcpy(pat, tpat);
for (q = pat, p = tpat; (c = *q++) != 0; *p++ = c)
if (c == '\\')
*p++ = CTRLQ;
*p = '\0';
strcpy(pat, tpat);
}
gfree(repat);
repat = NULL;
if (pat[0])
{
for (q = pat, p = tpat; (c = *q++) != 0; *p++ = c)
if (c == CTRLQ)
c = '\\';
*p = '\0';
casesensitize(tpat);
repat = regcomp(tpat);
}
return repat != NULL;
}
/* Replace plen characters before dot with argument string st.
Control-J characters in st are interpreted as newlines. There is a
casehack enable flag (normally it likes to match case of replacement
to what was there). */
/* by Rich Ellison. */
static int lreplace(int plen, uchar *st)
{
int dflen; /* replacement/src length diff */
uint c; /* used for random characters */
int doto; /* offset into line */
backchar(TRUE, plen);
/*
* make the string lengths match (either pad the line
* so that it will fit, or scrunch out the excess).
* be careful with dot's offset.
*/
if ((dflen = plen - (int)strlen(st)) != 0)
{
doto = curwp->w_doto;
if (dflen > 0)
ldelete(dflen, KNONE);
else if (linsert(-dflen, ' ') == FALSE)
return FALSE;
curwp->w_doto = doto;
}
/* do the replacement */
while ((c = *st++) != 0)
{
if (c == '\n')
{
if (curwp->w_doto == llength(curwp->w_dotp))
forwchar(FALSE, 1);
else
{
if (ldelete(1, KNONE) != FALSE)
lnewline();
}
} else if (curwp->w_dotp == curbp->b_linep)
{
linsert((RSIZE) 1, c);
} else if (curwp->w_doto == llength(curwp->w_dotp))
{
if (ldelete(1, KNONE) != FALSE)
linsert(1, c);
} else
lputc(curwp->w_dotp, curwp->w_doto++, c);
}
lchange(WFHARD);
return TRUE;
}
/* Perform substitutions after a regexp match. Written by Henry
Spencer. Copyright (c) 1986 by University of Toronto. Not derived
from licensed software. modified by pm@Case: return a ptr to the new
string; must be *g*free()'d */
static char *regsubst(regexp *prog, char *source)
{
#define UCHARAT(p) ((int)(unsigned char)(*(p)))
char *src;
char *p;
char c;
int no;
WS *ws; /* type really is WS * */
char ord[2];
if (prog == NULL || source == NULL || (UCHARAT(prog->program) != MAGIC))
return NULL;
src = source;
ws = initws();
ord[1] = '\0';
while ((c = *src++) != '\0')
{
if (c == '&')
no = 0;
else if (c == '\\' && '0' <= *src && *src <= '9')
no = *src++ - '0';
else
no = -1;
if (no < 0)
{
ord[0] = c;
strwcat(ws, ord, 0);
} else if (prog->startp[no] != NULL && prog->endp[no] != NULL)
{
p = prog->endp[no];
c = *p;
*p = '\0';
strwcat(ws, prog->startp[no], 0);
*p = c;
}
}
p = ws->ps;
gfree(ws);
return p;
}
/* This routine does the real work of a forward search. The pattern
is sitting in the external variable "pat". If found, dot is updated,
the window system is notified of the change, and TRUE is returned. */
static int fsearch(void)
{
ELINE *clp;
int cbo;
char *s;
clp = curwp->w_dotp;
cbo = curwp->w_doto;
while (clp != curbp->b_linep)
{
s = makelnstr(clp);
if (regexec(repat, s + cbo) == 1)
goto found;
clp = lforw(clp);
cbo = 0;
}
return FALSE;
found:
curwp->w_dotp = clp;
curwp->w_doto = (int)(repat->endp[0] - s);
curwp->w_flag |= WFMOVE;
return TRUE;
}
/* This routine does the real work of a backward search. The pattern
is sitting in the external variable "pat". If found, dot is updated,
the window system is notified of the change, and TRUE is returned. If
the string isn't found, FALSE is returned. */
static int rbsearch(void)
{
ELINE *clp;
int cbo;
int i;
char *s;
char c;
clp = curwp->w_dotp;
if (clp == curbp->b_linep)
clp = lback(clp);
cbo = curwp->w_doto;
c = clp->l_text[cbo];
while (clp != curbp->b_linep)
{
s = makelnstr(clp);
if (cbo >= 0)
{
c = s[cbo];
s[cbo] = '\0';
}
i = regexec(repat, s);
if (cbo >= 0)
{
s[cbo] = c;
cbo = -1;
}
if (i == 1)
goto found;
clp = lback(clp);
}
return FALSE;
found:
for (i = (int)strlen(s); i; i--)
if (regexec(repat, s + i) == 1)
break;
curwp->w_dotp = clp;
curwp->w_doto = i;
curwp->w_flag |= WFMOVE;
return TRUE;
}
/* Search forward. Get a search string from the user, and search for
it, starting at ".". If found, "." gets moved to just after the
matched characters, and display does all the hard stuff. If not
found, it just prints a message. */
int forwsearch(int f, int n)
{
int s;
UNUSED(f);
UNUSED(n);
s = readpattern("re Search Forward");
if (s == ABORT || s == FALSE)
return s;
s = (s == 0x12 ? rbsearch() : fsearch());
if (s == FALSE)
failed();
return s;
}
/* 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]. */
int backsearch(int f, int n)
{
int s;
UNUSED(f);
UNUSED(n);
s = readpattern("re Search Backward");
if ((s == ABORT) || (s == FALSE))
return s;
s = (s == 0x13 ? fsearch() : rbsearch());
if (s == FALSE)
failed();
return s;
}
/* Query Replace. Replace strings selectively. Does a search
and replace operation.
*/
int queryrepl(int f, int n)
{
int s;
KEY c;
int all;
int rcnt; /* Replacements made so far */
int plen; /* length of found string */
char news[NPAT]; /* replacement string */
char *p;
UNUSED(f);
UNUSED(n);
news[0] = '\0';
if ((s = readpattern("re Query replace")) != TRUE)
return s;
s = mlreply(sprintp("re Query replace %s with: ", pat), news, NPAT);
if (s == CCHR('G'))
return ABORT;
mlwrite("re Query replacing %s with %s:", pat, news);
rcnt = all = 0;
while (fsearch() == TRUE)
{
loopbgn:
if (all)
{
c = usertyped();
if (c == 0)
c = ' ';
if ((rcnt & 0x003f) == 0)
update();
} else
{
update();
c = inkey();
}
switch (c)
{
case 0x7F:
break;
case '!':
all = 1; /* fall through */
case ' ':
case '.':
p = regsubst(repat, news);
plen = (int)(repat->endp[0] - repat->startp[0]);
s = lreplace(plen, p);
gfree(p);
if (s == FALSE)
goto stopsearch;
rcnt++;
if (c != '.')
break;
/* else fall through to stopsearch */
case '\007':
case '\003':
case '\033':
goto stopsearch;
default:
mlwrite("<SP> replace, [.] rep-end, <DEL> don't, [!] repl rest <ESC> quit");
goto loopbgn;
}
}
stopsearch:
curwp->w_flag |= WFHARD;
update();
mlwrite("(%d replacements done)", rcnt);
return TRUE;
}