Home | History | Annotate | Line # | Download | only in ed
sub.c revision 1.2
      1  1.1  alm /* sub.c: This file contains the substitution routines for the ed
      2  1.1  alm    line editor */
      3  1.1  alm /*-
      4  1.1  alm  * Copyright (c) 1993 Andrew Moore, Talke Studio.
      5  1.1  alm  * All rights reserved.
      6  1.1  alm  *
      7  1.1  alm  * Redistribution and use in source and binary forms, with or without
      8  1.1  alm  * modification, are permitted provided that the following conditions
      9  1.1  alm  * are met:
     10  1.1  alm  * 1. Redistributions of source code must retain the above copyright
     11  1.1  alm  *    notice, this list of conditions and the following disclaimer.
     12  1.1  alm  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  alm  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  alm  *    documentation and/or other materials provided with the distribution.
     15  1.1  alm  *
     16  1.1  alm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  alm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  alm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  alm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  alm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  alm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  alm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  alm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  alm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  alm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  alm  * SUCH DAMAGE.
     27  1.1  alm  */
     28  1.1  alm 
     29  1.1  alm #ifndef lint
     30  1.1  alm static char *rcsid = "@(#)sub.c,v 1.1 1994/02/01 00:34:44 alm Exp";
     31  1.1  alm #endif /* not lint */
     32  1.1  alm 
     33  1.1  alm #include "ed.h"
     34  1.1  alm 
     35  1.1  alm 
     36  1.1  alm char *rhbuf;			/* rhs substitution buffer */
     37  1.1  alm int rhbufsz;			/* rhs substitution buffer size */
     38  1.1  alm int rhbufi;			/* rhs substitution buffer index */
     39  1.1  alm 
     40  1.1  alm /* extract_subst_tail: extract substitution tail from the command buffer */
     41  1.1  alm int
     42  1.1  alm extract_subst_tail(flagp, np)
     43  1.1  alm 	int *flagp;
     44  1.2  cgd 	long *np;
     45  1.1  alm {
     46  1.1  alm 	char delimiter;
     47  1.1  alm 
     48  1.1  alm 	*flagp = *np = 0;
     49  1.1  alm 	if ((delimiter = *ibufp) == '\n') {
     50  1.1  alm 		rhbufi = 0;
     51  1.1  alm 		*flagp = GPR;
     52  1.1  alm 		return 0;
     53  1.1  alm 	} else if (extract_subst_template() == NULL)
     54  1.1  alm 		return  ERR;
     55  1.1  alm 	else if (*ibufp == '\n') {
     56  1.1  alm 		*flagp = GPR;
     57  1.1  alm 		return 0;
     58  1.1  alm 	} else if (*ibufp == delimiter)
     59  1.1  alm 		ibufp++;
     60  1.1  alm 	if ('1' <= *ibufp && *ibufp <= '9') {
     61  1.1  alm 		STRTOL(*np, ibufp);
     62  1.1  alm 		return 0;
     63  1.1  alm 	} else if (*ibufp == 'g') {
     64  1.1  alm 		ibufp++;
     65  1.1  alm 		*flagp = GSG;
     66  1.1  alm 		return 0;
     67  1.1  alm 	}
     68  1.1  alm 	return 0;
     69  1.1  alm }
     70  1.1  alm 
     71  1.1  alm 
     72  1.1  alm /* extract_subst_template: return pointer to copy of substitution template
     73  1.1  alm    in the command buffer */
     74  1.1  alm char *
     75  1.1  alm extract_subst_template()
     76  1.1  alm {
     77  1.1  alm 	int n = 0;
     78  1.1  alm 	int i = 0;
     79  1.1  alm 	char c;
     80  1.1  alm 	char delimiter = *ibufp++;
     81  1.1  alm 
     82  1.1  alm 	if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
     83  1.1  alm 		ibufp++;
     84  1.1  alm 		if (!rhbuf) sprintf(errmsg, "no previous substitution");
     85  1.1  alm 		return rhbuf;
     86  1.1  alm 	}
     87  1.1  alm 	while (*ibufp != delimiter) {
     88  1.1  alm 		REALLOC(rhbuf, rhbufsz, i + 2, NULL);
     89  1.1  alm 		if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
     90  1.1  alm 			i--, ibufp--;
     91  1.1  alm 			break;
     92  1.1  alm 		} else if (c != '\\')
     93  1.1  alm 			;
     94  1.1  alm 		else if ((rhbuf[i++] = *ibufp++) != '\n')
     95  1.1  alm 			;
     96  1.1  alm 		else if (!isglobal) {
     97  1.1  alm 			while ((n = get_tty_line()) == 0 ||
     98  1.1  alm 			    n > 0 && ibuf[n - 1] != '\n')
     99  1.1  alm 				clearerr(stdin);
    100  1.1  alm 			if (n < 0)
    101  1.1  alm 				return NULL;
    102  1.1  alm 		}
    103  1.1  alm 	}
    104  1.1  alm 	REALLOC(rhbuf, rhbufsz, i + 1, NULL);
    105  1.1  alm 	rhbuf[rhbufi = i] = '\0';
    106  1.1  alm 	return  rhbuf;
    107  1.1  alm }
    108  1.1  alm 
    109  1.1  alm 
    110  1.1  alm char *rbuf;			/* substitute_matching_text buffer */
    111  1.1  alm int rbufsz;			/* substitute_matching_text buffer size */
    112  1.1  alm 
    113  1.1  alm /* search_and_replace: for each line in a range, change text matching a pattern
    114  1.1  alm    according to a substitution template; return status  */
    115  1.1  alm int
    116  1.1  alm search_and_replace(pat, gflag, kth)
    117  1.1  alm 	pattern_t *pat;
    118  1.1  alm 	int gflag;
    119  1.1  alm 	int kth;
    120  1.1  alm {
    121  1.1  alm 	undo_t *up;
    122  1.1  alm 	char *txt;
    123  1.1  alm 	char *eot;
    124  1.1  alm 	long lc;
    125  1.1  alm 	int nsubs = 0;
    126  1.1  alm 	line_t *lp;
    127  1.1  alm 	int len;
    128  1.1  alm 
    129  1.1  alm 	current_addr = first_addr - 1;
    130  1.1  alm 	for (lc = 0; lc <= second_addr - first_addr; lc++) {
    131  1.1  alm 		lp = get_addressed_line_node(++current_addr);
    132  1.1  alm 		if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0)
    133  1.1  alm 			return ERR;
    134  1.1  alm 		else if (len) {
    135  1.1  alm 			up = NULL;
    136  1.1  alm 			if (delete_lines(current_addr, current_addr) < 0)
    137  1.1  alm 				return ERR;
    138  1.1  alm 			txt = rbuf;
    139  1.1  alm 			eot = rbuf + len;
    140  1.1  alm 			SPL1();
    141  1.1  alm 			do {
    142  1.1  alm 				if ((txt = put_sbuf_line(txt)) == NULL) {
    143  1.1  alm 					SPL0();
    144  1.1  alm 					return ERR;
    145  1.1  alm 				} else if (up)
    146  1.1  alm 					up->t = get_addressed_line_node(current_addr);
    147  1.1  alm 				else if ((up = push_undo_stack(UADD,
    148  1.1  alm 				    current_addr, current_addr)) == NULL) {
    149  1.1  alm 					SPL0();
    150  1.1  alm 					return ERR;
    151  1.1  alm 				}
    152  1.1  alm 			} while (txt != eot);
    153  1.1  alm 			SPL0();
    154  1.1  alm 			nsubs++;
    155  1.1  alm 		}
    156  1.1  alm 	}
    157  1.1  alm 	if  (nsubs == 0 && !(gflag & GLB)) {
    158  1.1  alm 		sprintf(errmsg, "no match");
    159  1.1  alm 		return ERR;
    160  1.1  alm 	} else if ((gflag & (GPR | GLS | GNP)) &&
    161  1.1  alm 	    display_lines(current_addr, current_addr, gflag) < 0)
    162  1.1  alm 		return ERR;
    163  1.1  alm 	return 0;
    164  1.1  alm }
    165  1.1  alm 
    166  1.1  alm 
    167  1.1  alm /* substitute_matching_text: replace text matched by a pattern according to
    168  1.1  alm    a substitution template; return pointer to the modified text */
    169  1.1  alm int
    170  1.1  alm substitute_matching_text(pat, lp, gflag, kth)
    171  1.1  alm 	pattern_t *pat;
    172  1.1  alm 	line_t *lp;
    173  1.1  alm 	int gflag;
    174  1.1  alm 	int kth;
    175  1.1  alm {
    176  1.1  alm 	int off = 0;
    177  1.1  alm 	int changed = 0;
    178  1.1  alm 	int matchno = 0;
    179  1.1  alm 	int i = 0;
    180  1.1  alm 	regmatch_t rm[SE_MAX];
    181  1.1  alm 	char *txt;
    182  1.1  alm 	char *eot;
    183  1.1  alm 
    184  1.1  alm 	if ((txt = get_sbuf_line(lp)) == NULL)
    185  1.1  alm 		return ERR;
    186  1.1  alm 	if (isbinary)
    187  1.1  alm 		NUL_TO_NEWLINE(txt, lp->len);
    188  1.1  alm 	eot = txt + lp->len;
    189  1.1  alm 	if (!regexec(pat, txt, SE_MAX, rm, 0)) {
    190  1.1  alm 		do {
    191  1.1  alm 			if (!kth || kth == ++matchno) {
    192  1.1  alm 				changed++;
    193  1.1  alm 				i = rm[0].rm_so;
    194  1.1  alm 				REALLOC(rbuf, rbufsz, off + i, ERR);
    195  1.1  alm 				if (isbinary)
    196  1.1  alm 					NEWLINE_TO_NUL(txt, rm[0].rm_eo);
    197  1.1  alm 				memcpy(rbuf + off, txt, i);
    198  1.1  alm 				off += i;
    199  1.1  alm 				if ((off = apply_subst_template(txt, rm, off,
    200  1.1  alm 				    pat->re_nsub)) < 0)
    201  1.1  alm 					return ERR;
    202  1.1  alm 			} else {
    203  1.1  alm 				i = rm[0].rm_eo;
    204  1.1  alm 				REALLOC(rbuf, rbufsz, off + i, ERR);
    205  1.1  alm 				if (isbinary)
    206  1.1  alm 					NEWLINE_TO_NUL(txt, i);
    207  1.1  alm 				memcpy(rbuf + off, txt, i);
    208  1.1  alm 				off += i;
    209  1.1  alm 			}
    210  1.1  alm 			txt += rm[0].rm_eo;
    211  1.1  alm 		} while (*txt && (!changed || (gflag & GSG) && rm[0].rm_eo) &&
    212  1.1  alm 		    !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
    213  1.1  alm 		i = eot - txt;
    214  1.1  alm 		REALLOC(rbuf, rbufsz, off + i + 2, ERR);
    215  1.1  alm 		if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
    216  1.1  alm 			sprintf(errmsg, "infinite substitution loop");
    217  1.1  alm 			return  ERR;
    218  1.1  alm 		}
    219  1.1  alm 		if (isbinary)
    220  1.1  alm 			NEWLINE_TO_NUL(txt, i);
    221  1.1  alm 		memcpy(rbuf + off, txt, i);
    222  1.1  alm 		memcpy(rbuf + off + i, "\n", 2);
    223  1.1  alm 	}
    224  1.1  alm 	return changed ? off + i + 1 : 0;
    225  1.1  alm }
    226  1.1  alm 
    227  1.1  alm 
    228  1.1  alm /* apply_subst_template: modify text according to a substitution template;
    229  1.1  alm    return offset to end of modified text */
    230  1.1  alm int
    231  1.1  alm apply_subst_template(boln, rm, off, re_nsub)
    232  1.1  alm 	char *boln;
    233  1.1  alm 	regmatch_t *rm;
    234  1.1  alm 	int off;
    235  1.1  alm 	int re_nsub;
    236  1.1  alm {
    237  1.1  alm 	int j = 0;
    238  1.1  alm 	int k = 0;
    239  1.1  alm 	int n;
    240  1.1  alm 	char *sub = rhbuf;
    241  1.1  alm 
    242  1.1  alm 	for (; sub - rhbuf < rhbufi; sub++)
    243  1.1  alm 		if (*sub == '&') {
    244  1.1  alm 			j = rm[0].rm_so;
    245  1.1  alm 			k = rm[0].rm_eo;
    246  1.1  alm 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
    247  1.1  alm 			while (j < k)
    248  1.1  alm 				rbuf[off++] = boln[j++];
    249  1.1  alm 		} else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
    250  1.1  alm 		    (n = *sub - '0') <= re_nsub) {
    251  1.1  alm 			j = rm[n].rm_so;
    252  1.1  alm 			k = rm[n].rm_eo;
    253  1.1  alm 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
    254  1.1  alm 			while (j < k)
    255  1.1  alm 				rbuf[off++] = boln[j++];
    256  1.1  alm 		} else {
    257  1.1  alm 			REALLOC(rbuf, rbufsz, off + 1, ERR);
    258  1.1  alm 			rbuf[off++] = *sub;
    259  1.1  alm 		}
    260  1.1  alm 	REALLOC(rbuf, rbufsz, off + 1, ERR);
    261  1.1  alm 	rbuf[off] = '\0';
    262  1.1  alm 	return off;
    263  1.1  alm }
    264