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