1 /* $OpenBSD: ex_global.c,v 1.10 2007/05/14 12:32:29 pyr Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1992, 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #ifndef lint 15 static const char sccsid[] = "@(#)ex_global.c 10.22 (Berkeley) 10/10/96"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/queue.h> 20 21 #include <bitstring.h> 22 #include <ctype.h> 23 #include <errno.h> 24 #include <limits.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "../common/common.h" 31 32 enum which {GLOBAL, V}; 33 34 static int ex_g_setup(SCR *, EXCMD *, enum which); 35 36 /* 37 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands] 38 * Exec on lines matching a pattern. 39 * 40 * PUBLIC: int ex_global(SCR *, EXCMD *); 41 */ 42 int 43 ex_global(sp, cmdp) 44 SCR *sp; 45 EXCMD *cmdp; 46 { 47 return (ex_g_setup(sp, 48 cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL)); 49 } 50 51 /* 52 * ex_v -- [line [,line]] v /pattern/ [commands] 53 * Exec on lines not matching a pattern. 54 * 55 * PUBLIC: int ex_v(SCR *, EXCMD *); 56 */ 57 int 58 ex_v(sp, cmdp) 59 SCR *sp; 60 EXCMD *cmdp; 61 { 62 return (ex_g_setup(sp, cmdp, V)); 63 } 64 65 /* 66 * ex_g_setup -- 67 * Ex global and v commands. 68 */ 69 static int 70 ex_g_setup(sp, cmdp, cmd) 71 SCR *sp; 72 EXCMD *cmdp; 73 enum which cmd; 74 { 75 CHAR_T *ptrn, *p, *t; 76 EXCMD *ecp; 77 MARK abs_mark; 78 RANGE *rp; 79 busy_t btype; 80 recno_t start, end; 81 regex_t *re; 82 regmatch_t match[1]; 83 size_t len; 84 int cnt, delim, eval; 85 char *dbp; 86 87 NEEDFILE(sp, cmdp); 88 89 if (F_ISSET(sp, SC_EX_GLOBAL)) { 90 msgq(sp, M_ERR, 91 "124|The %s command can't be used as part of a global or v command", 92 cmdp->cmd->name); 93 return (1); 94 } 95 96 /* 97 * Skip leading white space. Historic vi allowed any non-alphanumeric 98 * to serve as the global command delimiter. 99 */ 100 if (cmdp->argc == 0) 101 goto usage; 102 for (p = cmdp->argv[0]->bp; isblank(*p); ++p); 103 if (*p == '\0' || isalnum(*p) || 104 *p == '\\' || *p == '|' || *p == '\n') { 105 usage: ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE); 106 return (1); 107 } 108 delim = *p++; 109 110 /* 111 * Get the pattern string, toss escaped characters. 112 * 113 * QUOTING NOTE: 114 * Only toss an escaped character if it escapes a delimiter. 115 */ 116 for (ptrn = t = p;;) { 117 if (p[0] == '\0' || p[0] == delim) { 118 if (p[0] == delim) 119 ++p; 120 /* 121 * !!! 122 * Nul terminate the pattern string -- it's passed 123 * to regcomp which doesn't understand anything else. 124 */ 125 *t = '\0'; 126 break; 127 } 128 if (p[0] == '\\') { 129 if (p[1] == delim) 130 ++p; 131 else if (p[1] == '\\') 132 *t++ = *p++; 133 } 134 *t++ = *p++; 135 } 136 137 /* If the pattern string is empty, use the last one. */ 138 if (*ptrn == '\0') { 139 if (sp->re == NULL) { 140 ex_emsg(sp, NULL, EXM_NOPREVRE); 141 return (1); 142 } 143 144 /* Re-compile the RE if necessary. */ 145 if (!F_ISSET(sp, SC_RE_SEARCH) && re_compile(sp, 146 sp->re, sp->re_len, NULL, NULL, &sp->re_c, RE_C_SEARCH)) 147 return (1); 148 } else { 149 /* Compile the RE. */ 150 if (re_compile(sp, ptrn, t - ptrn, 151 &sp->re, &sp->re_len, &sp->re_c, RE_C_SEARCH)) 152 return (1); 153 154 /* 155 * Set saved RE. Historic practice is that globals set 156 * direction as well as the RE. 157 */ 158 sp->searchdir = FORWARD; 159 } 160 re = &sp->re_c; 161 162 /* The global commands always set the previous context mark. */ 163 abs_mark.lno = sp->lno; 164 abs_mark.cno = sp->cno; 165 if (mark_set(sp, ABSMARK1, &abs_mark, 1)) 166 return (1); 167 168 /* Get an EXCMD structure. */ 169 CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD)); 170 CIRCLEQ_INIT(&ecp->rq); 171 172 /* 173 * Get a copy of the command string; the default command is print. 174 * Don't worry about a set of <blank>s with no command, that will 175 * default to print in the ex parser. We need to have two copies 176 * because the ex parser may step on the command string when it's 177 * parsing it. 178 */ 179 if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) { 180 p = "pp"; 181 len = 1; 182 } 183 184 MALLOC_RET(sp, ecp->cp, char *, len * 2); 185 ecp->o_cp = ecp->cp; 186 ecp->o_clen = len; 187 memcpy(ecp->cp + len, p, len); 188 ecp->range_lno = OOBLNO; 189 FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V); 190 LIST_INSERT_HEAD(&sp->gp->ecq, ecp, q); 191 192 /* 193 * For each line... The semantics of global matching are that we first 194 * have to decide which lines are going to get passed to the command, 195 * and then pass them to the command, ignoring other changes. There's 196 * really no way to do this in a single pass, since arbitrary line 197 * creation, deletion and movement can be done in the ex command. For 198 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d". 199 * What we do is create linked list of lines that are tracked through 200 * each ex command. There's a callback routine which the DB interface 201 * routines call when a line is created or deleted. This doesn't help 202 * the layering much. 203 */ 204 btype = BUSY_ON; 205 cnt = INTERRUPT_CHECK; 206 for (start = cmdp->addr1.lno, 207 end = cmdp->addr2.lno; start <= end; ++start) { 208 if (cnt-- == 0) { 209 if (INTERRUPTED(sp)) { 210 LIST_REMOVE(ecp, q); 211 free(ecp->cp); 212 free(ecp); 213 break; 214 } 215 search_busy(sp, btype); 216 btype = BUSY_UPDATE; 217 cnt = INTERRUPT_CHECK; 218 } 219 if (db_get(sp, start, DBG_FATAL, &dbp, &len)) 220 return (1); 221 match[0].rm_so = 0; 222 match[0].rm_eo = len; 223 switch (eval = 224 regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) { 225 case 0: 226 if (cmd == V) 227 continue; 228 break; 229 case REG_NOMATCH: 230 if (cmd == GLOBAL) 231 continue; 232 break; 233 default: 234 re_error(sp, eval, &sp->re_c); 235 break; 236 } 237 238 /* If follows the last entry, extend the last entry's range. */ 239 if ((rp = CIRCLEQ_LAST(&ecp->rq)) != CIRCLEQ_END(&ecp->rq) && 240 rp->stop == start - 1) { 241 ++rp->stop; 242 continue; 243 } 244 245 /* Allocate a new range, and append it to the list. */ 246 CALLOC(sp, rp, RANGE *, 1, sizeof(RANGE)); 247 if (rp == NULL) 248 return (1); 249 rp->start = rp->stop = start; 250 CIRCLEQ_INSERT_TAIL(&ecp->rq, rp, q); 251 } 252 search_busy(sp, BUSY_OFF); 253 return (0); 254 } 255 256 /* 257 * ex_g_insdel -- 258 * Update the ranges based on an insertion or deletion. 259 * 260 * PUBLIC: int ex_g_insdel(SCR *, lnop_t, recno_t); 261 */ 262 int 263 ex_g_insdel(sp, op, lno) 264 SCR *sp; 265 lnop_t op; 266 recno_t lno; 267 { 268 EXCMD *ecp; 269 RANGE *nrp, *rp; 270 271 /* All insert/append operations are done as inserts. */ 272 if (op == LINE_APPEND) 273 abort(); 274 275 if (op == LINE_RESET) 276 return (0); 277 278 LIST_FOREACH(ecp, &sp->gp->ecq, q) { 279 if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V)) 280 continue; 281 for (rp = CIRCLEQ_FIRST(&ecp->rq); rp != CIRCLEQ_END(&ecp->rq); 282 rp = nrp) { 283 nrp = CIRCLEQ_NEXT(rp, q); 284 285 /* If range less than the line, ignore it. */ 286 if (rp->stop < lno) 287 continue; 288 289 /* 290 * If range greater than the line, decrement or 291 * increment the range. 292 */ 293 if (rp->start > lno) { 294 if (op == LINE_DELETE) { 295 --rp->start; 296 --rp->stop; 297 } else { 298 ++rp->start; 299 ++rp->stop; 300 } 301 continue; 302 } 303 304 /* 305 * Lno is inside the range, decrement the end point 306 * for deletion, and split the range for insertion. 307 * In the latter case, since we're inserting a new 308 * element, neither range can be exhausted. 309 */ 310 if (op == LINE_DELETE) { 311 if (rp->start > --rp->stop) { 312 CIRCLEQ_REMOVE(&ecp->rq, rp, q); 313 free(rp); 314 } 315 } else { 316 CALLOC_RET(sp, nrp, RANGE *, 1, sizeof(RANGE)); 317 nrp->start = lno + 1; 318 nrp->stop = rp->stop + 1; 319 rp->stop = lno - 1; 320 CIRCLEQ_INSERT_AFTER(&ecp->rq, rp, nrp, q); 321 rp = nrp; 322 } 323 } 324 325 /* 326 * If the command deleted/inserted lines, the cursor moves to 327 * the line after the deleted/inserted line. 328 */ 329 ecp->range_lno = lno; 330 } 331 return (0); 332 } 333