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