Annotation of src/usr.bin/mg/re_search.c, Revision 1.20
1.20 ! kjell 1: /* $OpenBSD: re_search.c,v 1.19 2005/10/14 15:41:33 deraadt Exp $ */
1.17 kjell 2:
3: /* This file is in the public domain. */
1.7 niklas 4:
1.1 deraadt 5: /*
1.6 millert 6: * regular expression search commands for Mg
1.1 deraadt 7: *
1.6 millert 8: * This file contains functions to implement several of gnuemacs's regular
9: * expression functions for Mg. Several of the routines below are just minor
10: * re-arrangements of Mg's non-regular expression search functions. Some of
1.9 mickey 11: * them are similar in structure to the original MicroEMACS, others are
1.6 millert 12: * modifications of Rich Ellison's code. Peter Newton re-wrote about half of
13: * them from scratch.
1.1 deraadt 14: */
15:
1.6 millert 16: #ifdef REGEX
1.2 millert 17: #include <sys/types.h>
18: #include <regex.h>
19:
1.6 millert 20: #include "def.h"
21: #include "macro.h"
1.1 deraadt 22:
1.6 millert 23: #define SRCH_BEGIN (0) /* search sub-codes */
1.1 deraadt 24: #define SRCH_FORW (-1)
25: #define SRCH_BACK (-2)
26: #define SRCH_NOPR (-3)
27: #define SRCH_ACCM (-4)
28: #define SRCH_MARK (-5)
29:
1.6 millert 30: #define RE_NMATCH 10 /* max number of matches */
31: #define REPLEN 256 /* max length of replacement string */
1.2 millert 32:
1.6 millert 33: char re_pat[NPAT]; /* regex pattern */
34: int re_srch_lastdir = SRCH_NOPR; /* last search flags */
35: int casefoldsearch = TRUE; /* does search ignore case? */
36:
1.20 ! kjell 37: static int re_doreplace(RSIZE, char *);
1.11 millert 38: static int re_forwsrch(void);
39: static int re_backsrch(void);
40: static int re_readpattern(char *);
41: static int killmatches(int);
42: static int countmatches(int);
1.1 deraadt 43:
44: /*
45: * Search forward.
1.9 mickey 46: * Get a search string from the user and search for it starting at ".". If
47: * found, move "." to just after the matched characters. display does all
1.6 millert 48: * the hard stuff. If not found, it just prints a message.
1.1 deraadt 49: */
1.5 millert 50: /* ARGSUSED */
1.6 millert 51: int
1.12 cloder 52: re_forwsearch(int f, int n)
1.5 millert 53: {
1.6 millert 54: int s;
1.1 deraadt 55:
1.5 millert 56: if ((s = re_readpattern("RE Search")) != TRUE)
1.1 deraadt 57: return (s);
58: if (re_forwsrch() == FALSE) {
59: ewprintf("Search failed: \"%s\"", re_pat);
60: return (FALSE);
61: }
62: re_srch_lastdir = SRCH_FORW;
63: return (TRUE);
64: }
65:
66: /*
67: * Reverse search.
1.14 db 68: * Get a search string from the user, and search, starting at "."
1.1 deraadt 69: * and proceeding toward the front of the buffer. If found "." is left
70: * pointing at the first character of the pattern [the last character that
71: * was matched].
72: */
1.5 millert 73: /* ARGSUSED */
1.6 millert 74: int
1.12 cloder 75: re_backsearch(int f, int n)
1.5 millert 76: {
1.6 millert 77: int s;
1.1 deraadt 78:
1.5 millert 79: if ((s = re_readpattern("RE Search backward")) != TRUE)
1.1 deraadt 80: return (s);
81: if (re_backsrch() == FALSE) {
82: ewprintf("Search failed: \"%s\"", re_pat);
83: return (FALSE);
84: }
85: re_srch_lastdir = SRCH_BACK;
86: return (TRUE);
87: }
88:
89: /*
1.9 mickey 90: * Search again, using the same search string and direction as the last search
91: * command. The direction has been saved in "srch_lastdir", so you know which
1.6 millert 92: * way to go.
93: *
94: * XXX: This code has problems -- some incompatibility(?) with extend.c causes
95: * match to fail when it should not.
1.1 deraadt 96: */
1.5 millert 97: /* ARGSUSED */
1.6 millert 98: int
1.12 cloder 99: re_searchagain(int f, int n)
1.5 millert 100: {
101: if (re_srch_lastdir == SRCH_NOPR) {
102: ewprintf("No last search");
103: return (FALSE);
104: }
105: if (re_srch_lastdir == SRCH_FORW) {
106: if (re_forwsrch() == FALSE) {
107: ewprintf("Search failed: \"%s\"", re_pat);
108: return (FALSE);
109: }
110: return (TRUE);
111: }
1.9 mickey 112: if (re_srch_lastdir == SRCH_BACK)
1.5 millert 113: if (re_backsrch() == FALSE) {
114: ewprintf("Search failed: \"%s\"", re_pat);
115: return (FALSE);
116: }
1.6 millert 117:
118: return (TRUE);
1.1 deraadt 119: }
120:
121: /* Compiled regex goes here-- changed only when new pattern read */
1.6 millert 122: static regex_t re_buff;
123: static regmatch_t re_match[RE_NMATCH];
1.1 deraadt 124:
125: /*
126: * Re-Query Replace.
127: * Replace strings selectively. Does a search and replace operation.
128: */
1.5 millert 129: /* ARGSUSED */
1.6 millert 130: int
1.12 cloder 131: re_queryrepl(int f, int n)
1.5 millert 132: {
1.14 db 133: int rcnt = 0; /* replacements made so far */
1.13 vincent 134: int plen, s; /* length of found string */
1.19 deraadt 135: char news[NPAT]; /* replacement string */
1.1 deraadt 136:
1.5 millert 137: if ((s = re_readpattern("RE Query replace")) != TRUE)
1.1 deraadt 138: return (s);
1.19 deraadt 139: if (eread("Query replace %s with: ", news, NPAT,
140: EFNUL | EFNEW | EFCR, re_pat) == NULL)
1.13 vincent 141: return (ABORT);
1.1 deraadt 142: ewprintf("Query replacing %s with %s:", re_pat, news);
143:
144: /*
145: * Search forward repeatedly, checking each time whether to insert
146: * or not. The "!" case makes the check always true, so it gets put
147: * into a tighter loop for efficiency.
148: */
149: while (re_forwsrch() == TRUE) {
1.5 millert 150: retry:
1.1 deraadt 151: update();
152: switch (getkey(FALSE)) {
153: case ' ':
1.2 millert 154: plen = re_match[0].rm_eo - re_match[0].rm_so;
1.20 ! kjell 155: if (re_doreplace((RSIZE)plen, news) == FALSE)
1.1 deraadt 156: return (FALSE);
157: rcnt++;
158: break;
159:
160: case '.':
1.2 millert 161: plen = re_match[0].rm_eo - re_match[0].rm_so;
1.20 ! kjell 162: if (re_doreplace((RSIZE)plen, news) == FALSE)
1.1 deraadt 163: return (FALSE);
164: rcnt++;
165: goto stopsearch;
166:
1.6 millert 167: case CCHR('G'): /* ^G */
1.8 art 168: (void)ctrlg(FFRAND, 0);
1.6 millert 169: case CCHR('['): /* ESC */
1.1 deraadt 170: case '`':
171: goto stopsearch;
172: case '!':
173: do {
1.2 millert 174: plen = re_match[0].rm_eo - re_match[0].rm_so;
1.20 ! kjell 175: if (re_doreplace((RSIZE)plen, news) == FALSE)
1.1 deraadt 176: return (FALSE);
177: rcnt++;
178: } while (re_forwsrch() == TRUE);
179: goto stopsearch;
180:
1.6 millert 181: case CCHR('?'): /* To not replace */
1.1 deraadt 182: break;
183:
184: default:
1.5 millert 185: ewprintf("<SP> replace, [.] rep-end, <DEL> don't, [!] repl rest <ESC> quit");
1.1 deraadt 186: goto retry;
187: }
188: }
1.6 millert 189:
1.1 deraadt 190: stopsearch:
191: curwp->w_flag |= WFHARD;
192: update();
193: if (!inmacro) {
194: if (rcnt == 0)
195: ewprintf("(No replacements done)");
196: else if (rcnt == 1)
197: ewprintf("(1 replacement done)");
198: else
199: ewprintf("(%d replacements done)", rcnt);
200: }
1.14 db 201: return (TRUE);
1.1 deraadt 202: }
203:
1.5 millert 204: /*
205: * Routine re_doreplace calls lreplace to make replacements needed by
206: * re_query replace. Its reason for existence is to deal with \1, \2. etc.
1.12 cloder 207: * plen: length to remove
208: * st: replacement string
1.1 deraadt 209: */
1.6 millert 210: static int
1.20 ! kjell 211: re_doreplace(RSIZE plen, char *st)
1.6 millert 212: {
213: int j, k, s, more, num, state;
214: LINE *clp;
215: char repstr[REPLEN];
1.5 millert 216:
217: clp = curwp->w_dotp;
218: more = TRUE;
219: j = 0;
220: state = 0;
1.6 millert 221: num = 0;
1.5 millert 222:
223: /* The following FSA parses the replacement string */
224: while (more) {
225: switch (state) {
226: case 0:
227: if (*st == '\\') {
228: st++;
229: state = 1;
230: } else if (*st == '\0')
231: more = FALSE;
232: else {
233: repstr[j] = *st;
234: j++;
235: if (j >= REPLEN)
236: return (FALSE);
237: st++;
238: }
239: break;
240: case 1:
241: if (*st >= '0' && *st <= '9') {
242: num = *st - '0';
243: st++;
244: state = 2;
245: } else if (*st == '\0')
246: more = FALSE;
247: else {
248: repstr[j] = *st;
249: j++;
250: if (j >= REPLEN)
251: return (FALSE);
252: st++;
253: state = 0;
254: }
255: break;
256: case 2:
257: if (*st >= '0' && *st <= '9') {
258: num = 10 * num + *st - '0';
259: st++;
260: } else {
261: if (num >= RE_NMATCH)
262: return (FALSE);
263: k = re_match[num].rm_eo - re_match[num].rm_so;
264: if (j + k >= REPLEN)
265: return (FALSE);
1.9 mickey 266: bcopy(&(clp->l_text[re_match[num].rm_so]),
1.6 millert 267: &repstr[j], k);
1.5 millert 268: j += k;
269: if (*st == '\0')
270: more = FALSE;
271: if (*st == '\\') {
272: st++;
273: state = 1;
274: } else {
275: repstr[j] = *st;
276: j++;
277: if (j >= REPLEN)
278: return (FALSE);
279: st++;
280: state = 0;
281: }
282: }
283: break;
1.6 millert 284: } /* switch (state) */
285: } /* while (more) */
1.1 deraadt 286:
1.5 millert 287: repstr[j] = '\0';
1.20 ! kjell 288: s = lreplace(plen, repstr);
1.5 millert 289: return (s);
1.1 deraadt 290: }
291:
292: /*
1.9 mickey 293: * This routine does the real work of a forward search. The pattern is
294: * sitting in the external variable "pat". If found, dot is updated, the
1.6 millert 295: * window system is notified of the change, and TRUE is returned. If the
1.1 deraadt 296: * string isn't found, FALSE is returned.
297: */
1.6 millert 298: static int
1.12 cloder 299: re_forwsrch(void)
1.5 millert 300: {
1.6 millert 301: int tbo, error;
302: LINE *clp;
1.5 millert 303:
304: clp = curwp->w_dotp;
305: tbo = curwp->w_doto;
306:
307: if (tbo == clp->l_used)
308: /*
1.6 millert 309: * Don't start matching past end of line -- must move to
310: * beginning of next line, unless at end of file.
1.5 millert 311: */
312: if (clp != curbp->b_linep) {
313: clp = lforw(clp);
314: tbo = 0;
315: }
316: /*
317: * Note this loop does not process the last line, but this editor
318: * always makes the last line empty so this is good.
319: */
320: while (clp != (curbp->b_linep)) {
321: re_match[0].rm_so = tbo;
322: re_match[0].rm_eo = llength(clp);
1.9 mickey 323: error = regexec(&re_buff, ltext(clp), RE_NMATCH, re_match,
1.6 millert 324: REG_STARTEND);
325: if (error != 0) {
1.5 millert 326: clp = lforw(clp);
327: tbo = 0;
328: } else {
329: curwp->w_doto = re_match[0].rm_eo;
330: curwp->w_dotp = clp;
331: curwp->w_flag |= WFMOVE;
332: return (TRUE);
333: }
334: }
335: return (FALSE);
1.1 deraadt 336: }
337:
338: /*
1.6 millert 339: * This routine does the real work of a backward search. The pattern is sitting
1.9 mickey 340: * in the external variable "re_pat". If found, dot is updated, the window
341: * system is notified of the change, and TRUE is returned. If the string isn't
1.6 millert 342: * found, FALSE is returned.
1.1 deraadt 343: */
1.6 millert 344: static int
1.12 cloder 345: re_backsrch(void)
1.5 millert 346: {
1.6 millert 347: LINE *clp;
348: int tbo;
349: regmatch_t lastmatch;
1.5 millert 350:
351: clp = curwp->w_dotp;
352: tbo = curwp->w_doto;
353:
354: /* Start search one position to the left of dot */
355: tbo = tbo - 1;
356: if (tbo < 0) {
357: /* must move up one line */
358: clp = lback(clp);
359: tbo = llength(clp);
360: }
1.6 millert 361:
1.5 millert 362: /*
363: * Note this loop does not process the last line, but this editor
364: * always makes the last line empty so this is good.
365: */
366: while (clp != (curbp->b_linep)) {
367: re_match[0].rm_so = 0;
368: re_match[0].rm_eo = llength(clp);
369: lastmatch.rm_so = -1;
370: /*
371: * Keep searching until we don't match any longer. Assumes a
372: * non-match does not modify the re_match array. We have to
373: * do this character-by-character after the first match since
374: * POSIX regexps don't give you a way to do reverse matches.
375: */
376: while (!regexec(&re_buff, ltext(clp), RE_NMATCH, re_match,
377: REG_STARTEND) && re_match[0].rm_so < tbo) {
378: memcpy(&lastmatch, &re_match[0], sizeof(regmatch_t));
379: re_match[0].rm_so++;
380: re_match[0].rm_eo = llength(clp);
381: }
382: if (lastmatch.rm_so == -1) {
383: clp = lback(clp);
384: tbo = llength(clp);
385: } else {
386: memcpy(&re_match[0], &lastmatch, sizeof(regmatch_t));
387: curwp->w_doto = re_match[0].rm_so;
388: curwp->w_dotp = clp;
389: curwp->w_flag |= WFMOVE;
390: return (TRUE);
391: }
392: }
393: return (FALSE);
1.1 deraadt 394: }
395:
396: /*
397: * Read a pattern.
398: * Stash it in the external variable "re_pat". The "pat" is
399: * not updated if the user types in an empty line. If the user typed
400: * an empty line, and there is no old pattern, it is an error.
401: * Display the old pattern, in the style of Jeff Lomicka. There is
402: * some do-it-yourself control expansion.
403: */
1.6 millert 404: static int
1.12 cloder 405: re_readpattern(char *prompt)
1.5 millert 406: {
1.6 millert 407: static int dofree = 0;
1.13 vincent 408: int flags, error, s;
409: char tpat[NPAT], *rep;
1.5 millert 410:
411: if (re_pat[0] == '\0')
1.18 kjell 412: rep = eread("%s: ", tpat, NPAT, EFNEW | EFCR, prompt);
1.5 millert 413: else
1.15 kjell 414: rep = eread("%s: (default %s) ", tpat, NPAT,
415: EFNUL | EFNEW | EFCR, prompt, re_pat);
1.18 kjell 416: if (rep == NULL)
417: return (ABORT);
418: if (rep[0] != '\0') {
1.5 millert 419: /* New pattern given */
1.14 db 420: (void)strlcpy(re_pat, tpat, sizeof(re_pat));
1.5 millert 421: if (casefoldsearch)
422: flags = REG_EXTENDED | REG_ICASE;
423: else
424: flags = REG_EXTENDED;
425: if (dofree)
426: regfree(&re_buff);
427: error = regcomp(&re_buff, re_pat, flags);
1.6 millert 428: if (error != 0) {
429: char message[256];
1.5 millert 430: regerror(error, &re_buff, message, sizeof(message));
431: ewprintf("Regex Error: %s", message);
432: re_pat[0] = '\0';
433: return (FALSE);
434: }
435: dofree = 1;
1.13 vincent 436: s = TRUE;
437: } else if (rep[0] == '\0' && re_pat[0] != '\0')
1.5 millert 438: /* Just using old pattern */
439: s = TRUE;
1.13 vincent 440: else
441: s = FALSE;
1.1 deraadt 442: return (s);
443: }
444:
1.5 millert 445: /*
446: * Cause case to not matter in searches. This is the default. If called
447: * with argument cause case to matter.
1.1 deraadt 448: */
1.6 millert 449: int
1.12 cloder 450: setcasefold(int f, int n)
1.5 millert 451: {
452: if (f & FFARG) {
453: casefoldsearch = FALSE;
454: ewprintf("Case-fold-search unset");
455: } else {
456: casefoldsearch = TRUE;
457: ewprintf("Case-fold-search set");
458: }
1.1 deraadt 459:
1.5 millert 460: /*
461: * Invalidate the regular expression pattern since I'm too lazy to
462: * recompile it.
463: */
464: re_pat[0] = '\0';
465: return (TRUE);
1.6 millert 466: }
1.1 deraadt 467:
1.5 millert 468: /*
1.14 db 469: * Delete all lines after dot that contain a string matching regex.
1.1 deraadt 470: */
1.6 millert 471: int
1.12 cloder 472: delmatchlines(int f, int n)
1.5 millert 473: {
1.6 millert 474: int s;
1.1 deraadt 475:
1.9 mickey 476: if ((s = re_readpattern("Flush lines (containing match for regexp)"))
1.6 millert 477: != TRUE)
1.5 millert 478: return (s);
1.1 deraadt 479:
1.5 millert 480: s = killmatches(TRUE);
481: return (s);
1.1 deraadt 482: }
483:
1.5 millert 484: /*
1.14 db 485: * Delete all lines after dot that don't contain a string matching regex.
1.1 deraadt 486: */
1.6 millert 487: int
1.12 cloder 488: delnonmatchlines(int f, int n)
1.5 millert 489: {
1.6 millert 490: int s;
1.1 deraadt 491:
1.9 mickey 492: if ((s = re_readpattern("Keep lines (containing match for regexp)"))
1.6 millert 493: != TRUE)
1.5 millert 494: return (s);
1.1 deraadt 495:
1.5 millert 496: s = killmatches(FALSE);
497: return (s);
1.1 deraadt 498: }
499:
1.9 mickey 500: /*
1.14 db 501: * This function does the work of deleting matching lines.
1.6 millert 502: */
503: static int
1.12 cloder 504: killmatches(int cond)
1.1 deraadt 505: {
1.6 millert 506: int s, error;
507: int count = 0;
508: LINE *clp;
1.5 millert 509:
510: clp = curwp->w_dotp;
511: if (curwp->w_doto == llength(clp))
512: /* Consider dot on next line */
513: clp = lforw(clp);
514:
515: while (clp != (curbp->b_linep)) {
516: /* see if line matches */
517: re_match[0].rm_so = 0;
518: re_match[0].rm_eo = llength(clp);
1.9 mickey 519: error = regexec(&re_buff, ltext(clp), RE_NMATCH, re_match,
1.6 millert 520: REG_STARTEND);
1.5 millert 521:
522: /* Delete line when appropriate */
523: if ((cond == FALSE && error) || (cond == TRUE && !error)) {
524: curwp->w_doto = 0;
525: curwp->w_dotp = clp;
526: count++;
527: s = ldelete(llength(clp) + 1, KNONE);
528: clp = curwp->w_dotp;
529: curwp->w_flag |= WFMOVE;
530: if (s == FALSE)
531: return (FALSE);
532: } else
533: clp = lforw(clp);
534: }
1.1 deraadt 535:
1.5 millert 536: ewprintf("%d line(s) deleted", count);
537: if (count > 0)
538: curwp->w_flag |= WFMOVE;
1.1 deraadt 539:
1.5 millert 540: return (TRUE);
1.1 deraadt 541: }
542:
1.5 millert 543: /*
1.14 db 544: * Count lines matching regex.
1.1 deraadt 545: */
1.6 millert 546: int
1.12 cloder 547: cntmatchlines(int f, int n)
1.5 millert 548: {
1.6 millert 549: int s;
1.1 deraadt 550:
1.5 millert 551: if ((s = re_readpattern("Count lines (matching regexp)")) != TRUE)
552: return (s);
553: s = countmatches(TRUE);
1.14 db 554:
1.5 millert 555: return (s);
1.1 deraadt 556: }
557:
1.5 millert 558: /*
1.14 db 559: * Count lines that fail to match regex.
1.1 deraadt 560: */
1.6 millert 561: int
1.12 cloder 562: cntnonmatchlines(int f, int n)
1.5 millert 563: {
1.6 millert 564: int s;
1.1 deraadt 565:
1.5 millert 566: if ((s = re_readpattern("Count lines (not matching regexp)")) != TRUE)
567: return (s);
568: s = countmatches(FALSE);
1.1 deraadt 569:
1.5 millert 570: return (s);
1.1 deraadt 571: }
572:
1.6 millert 573: /*
574: * This function does the work of counting matching lines.
575: */
576: int
1.12 cloder 577: countmatches(int cond)
1.1 deraadt 578: {
1.6 millert 579: int error;
580: int count = 0;
581: LINE *clp;
1.5 millert 582:
583: clp = curwp->w_dotp;
584: if (curwp->w_doto == llength(clp))
585: /* Consider dot on next line */
586: clp = lforw(clp);
587:
588: while (clp != (curbp->b_linep)) {
589: /* see if line matches */
590: re_match[0].rm_so = 0;
591: re_match[0].rm_eo = llength(clp);
1.9 mickey 592: error = regexec(&re_buff, ltext(clp), RE_NMATCH, re_match,
1.6 millert 593: REG_STARTEND);
1.5 millert 594:
595: /* Count line when appropriate */
596: if ((cond == FALSE && error) || (cond == TRUE && !error))
597: count++;
598: clp = lforw(clp);
599: }
1.1 deraadt 600:
1.5 millert 601: if (cond)
602: ewprintf("Number of lines matching: %d", count);
603: else
604: ewprintf("Number of lines not matching: %d", count);
605:
606: return (TRUE);
1.1 deraadt 607: }
1.6 millert 608: #endif /* REGEX */