head	1.12;
access;
symbols
	perseant-exfatfs-base-20250801:1.12
	perseant-exfatfs-base-20240630:1.12
	perseant-exfatfs:1.12.0.36
	perseant-exfatfs-base:1.12
	cjep_sun2x:1.12.0.34
	cjep_sun2x-base:1.12
	cjep_staticlib_x-base1:1.12
	cjep_staticlib_x:1.12.0.32
	cjep_staticlib_x-base:1.12
	phil-wifi-20200421:1.12
	phil-wifi-20200411:1.12
	phil-wifi-20200406:1.12
	pgoyette-compat-merge-20190127:1.12
	pgoyette-compat-20190127:1.12
	pgoyette-compat-20190118:1.12
	pgoyette-compat-1226:1.12
	pgoyette-compat-1126:1.12
	pgoyette-compat-1020:1.12
	pgoyette-compat-0930:1.12
	pgoyette-compat-0906:1.12
	pgoyette-compat-0728:1.12
	pgoyette-compat-0625:1.12
	pgoyette-compat-0521:1.12
	pgoyette-compat-0502:1.12
	pgoyette-compat-0422:1.12
	pgoyette-compat-0415:1.12
	pgoyette-compat-0407:1.12
	pgoyette-compat-0330:1.12
	pgoyette-compat-0322:1.12
	pgoyette-compat-0315:1.12
	pgoyette-compat:1.12.0.30
	pgoyette-compat-base:1.12
	perseant-stdc-iso10646:1.12.0.28
	perseant-stdc-iso10646-base:1.12
	prg-localcount2-base3:1.12
	prg-localcount2-base2:1.12
	prg-localcount2-base1:1.12
	prg-localcount2:1.12.0.26
	prg-localcount2-base:1.12
	pgoyette-localcount-20170426:1.12
	bouyer-socketcan-base1:1.12
	pgoyette-localcount-20170320:1.12
	bouyer-socketcan:1.12.0.24
	bouyer-socketcan-base:1.12
	pgoyette-localcount-20170107:1.12
	pgoyette-localcount-20161104:1.12
	localcount-20160914:1.12
	pgoyette-localcount-20160806:1.12
	pgoyette-localcount-20160726:1.12
	pgoyette-localcount:1.12.0.22
	pgoyette-localcount-base:1.12
	netbsd-5-2-3-RELEASE:1.12
	netbsd-5-1-5-RELEASE:1.12
	yamt-pagecache-base9:1.12
	yamt-pagecache-tag8:1.12
	tls-earlyentropy:1.12.0.18
	tls-earlyentropy-base:1.12
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.12
	riastradh-drm2-base3:1.12
	netbsd-5-2-2-RELEASE:1.12
	netbsd-5-1-4-RELEASE:1.12
	netbsd-5-2-1-RELEASE:1.12
	netbsd-5-1-3-RELEASE:1.12
	agc-symver:1.12.0.20
	agc-symver-base:1.12
	tls-maxphys-base:1.12
	yamt-pagecache-base8:1.12
	netbsd-5-2:1.12.0.16
	yamt-pagecache-base7:1.12
	netbsd-5-2-RELEASE:1.12
	netbsd-5-2-RC1:1.12
	yamt-pagecache-base6:1.12
	yamt-pagecache-base5:1.12
	yamt-pagecache-base4:1.12
	netbsd-5-1-2-RELEASE:1.12
	netbsd-5-1-1-RELEASE:1.12
	yamt-pagecache-base3:1.12
	yamt-pagecache-base2:1.12
	yamt-pagecache:1.12.0.14
	yamt-pagecache-base:1.12
	bouyer-quota2-nbase:1.12
	bouyer-quota2:1.12.0.12
	bouyer-quota2-base:1.12
	matt-nb5-pq3:1.12.0.10
	matt-nb5-pq3-base:1.12
	netbsd-5-1:1.12.0.8
	netbsd-5-1-RELEASE:1.12
	netbsd-5-1-RC4:1.12
	netbsd-5-1-RC3:1.12
	netbsd-5-1-RC2:1.12
	netbsd-5-1-RC1:1.12
	netbsd-5-0-2-RELEASE:1.12
	netbsd-5-0-1-RELEASE:1.12
	jym-xensuspend-nbase:1.12
	netbsd-5-0:1.12.0.6
	netbsd-5-0-RELEASE:1.12
	netbsd-5-0-RC4:1.12
	netbsd-5-0-RC3:1.12
	netbsd-5-0-RC2:1.12
	jym-xensuspend:1.12.0.4
	jym-xensuspend-base:1.12
	netbsd-5-0-RC1:1.12
	netbsd-5:1.12.0.2
	netbsd-5-base:1.12
	matt-mips64-base2:1.11
	matt-mips64:1.11.0.32
	mjf-devfs2:1.11.0.30
	mjf-devfs2-base:1.11
	netbsd-4-0-1-RELEASE:1.11
	wrstuden-revivesa-base-3:1.11
	wrstuden-revivesa-base-2:1.11
	wrstuden-fixsa-newbase:1.11
	wrstuden-revivesa-base-1:1.11
	yamt-pf42-base4:1.11
	yamt-pf42-base3:1.11
	hpcarm-cleanup-nbase:1.11
	yamt-pf42-baseX:1.11
	yamt-pf42-base2:1.11
	wrstuden-revivesa:1.11.0.28
	wrstuden-revivesa-base:1.11
	yamt-pf42:1.11.0.26
	yamt-pf42-base:1.11
	keiichi-mipv6-nbase:1.11
	keiichi-mipv6:1.11.0.24
	keiichi-mipv6-base:1.11
	matt-armv6-nbase:1.11
	matt-armv6-prevmlocking:1.11
	wrstuden-fixsa-base-1:1.11
	netbsd-4-0:1.11.0.22
	netbsd-4-0-RELEASE:1.11
	cube-autoconf:1.11.0.20
	cube-autoconf-base:1.11
	netbsd-4-0-RC5:1.11
	netbsd-4-0-RC4:1.11
	netbsd-4-0-RC3:1.11
	netbsd-4-0-RC2:1.11
	netbsd-4-0-RC1:1.11
	matt-armv6:1.11.0.18
	matt-armv6-base:1.11
	matt-mips64-base:1.11
	hpcarm-cleanup:1.11.0.16
	hpcarm-cleanup-base:1.11
	netbsd-3-1-1-RELEASE:1.11
	netbsd-3-0-3-RELEASE:1.11
	wrstuden-fixsa:1.11.0.14
	wrstuden-fixsa-base:1.11
	abandoned-netbsd-4-base:1.11
	abandoned-netbsd-4:1.11.0.8
	netbsd-3-1:1.11.0.10
	netbsd-3-1-RELEASE:1.11
	netbsd-3-0-2-RELEASE:1.11
	netbsd-3-1-RC4:1.11
	netbsd-3-1-RC3:1.11
	netbsd-3-1-RC2:1.11
	netbsd-3-1-RC1:1.11
	netbsd-4:1.11.0.12
	netbsd-4-base:1.11
	chap-midi-nbase:1.11
	netbsd-3-0-1-RELEASE:1.11
	chap-midi:1.11.0.6
	chap-midi-base:1.11
	netbsd-3-0:1.11.0.4
	netbsd-3-0-RELEASE:1.11
	netbsd-3-0-RC6:1.11
	netbsd-3-0-RC5:1.11
	netbsd-3-0-RC4:1.11
	netbsd-3-0-RC3:1.11
	netbsd-3-0-RC2:1.11
	netbsd-3-0-RC1:1.11
	netbsd-2-0-3-RELEASE:1.10.4.2
	netbsd-2-1:1.10.6.1.0.2
	netbsd-2-1-RELEASE:1.10.6.1
	netbsd-2-1-RC6:1.10.6.1
	netbsd-2-1-RC5:1.10.6.1
	netbsd-2-1-RC4:1.10.6.1
	netbsd-2-1-RC3:1.10.6.1
	netbsd-2-1-RC2:1.10.6.1
	netbsd-2-1-RC1:1.10.6.1
	netbsd-2-0-2-RELEASE:1.10.4.2
	netbsd-3:1.11.0.2
	netbsd-3-base:1.11
	netbsd-2-0-1-RELEASE:1.10
	netbsd-2:1.10.0.6
	netbsd-2-base:1.10
	netbsd-2-0-RELEASE:1.10
	netbsd-2-0-RC5:1.10
	netbsd-2-0-RC4:1.10
	netbsd-2-0-RC3:1.10
	netbsd-2-0-RC2:1.10
	netbsd-2-0-RC1:1.10
	netbsd-2-0:1.10.0.4
	netbsd-2-0-base:1.10
	netbsd-1-6-PATCH002-RELEASE:1.10
	netbsd-1-6-PATCH002:1.10
	netbsd-1-6-PATCH002-RC4:1.10
	netbsd-1-6-PATCH002-RC3:1.10
	netbsd-1-6-PATCH002-RC2:1.10
	netbsd-1-6-PATCH002-RC1:1.10
	netbsd-1-6-PATCH001:1.10
	netbsd-1-6-PATCH001-RELEASE:1.10
	netbsd-1-6-PATCH001-RC3:1.10
	netbsd-1-6-PATCH001-RC2:1.10
	netbsd-1-6-PATCH001-RC1:1.10
	fvdl_fs64_base:1.10
	netbsd-1-6-RELEASE:1.10
	netbsd-1-6-RC3:1.10
	netbsd-1-6-RC2:1.10
	netbsd-1-6-RC1:1.10
	netbsd-1-6:1.10.0.2
	netbsd-1-6-base:1.10
	netbsd-1-5-PATCH003:1.8
	netbsd-1-5-PATCH002:1.8
	netbsd-1-5-PATCH001:1.8
	nvi_1_79:1.1.1.6
	netbsd-1-5-RELEASE:1.8
	netbsd-1-5-BETA2:1.8
	netbsd-1-5-BETA:1.8
	netbsd-1-4-PATCH003:1.7
	netbsd-1-5-ALPHA2:1.8
	netbsd-1-5:1.8.0.4
	netbsd-1-5-base:1.8
	minoura-xpg4dl-base:1.8
	minoura-xpg4dl:1.8.0.2
	netbsd-1-4-PATCH002:1.7
	wrstuden-devbsize-19991221:1.7
	wrstuden-devbsize:1.7.0.6
	wrstuden-devbsize-base:1.7
	comdex-fall-1999:1.7.0.4
	comdex-fall-1999-base:1.7
	netbsd-1-4-PATCH001:1.7
	netbsd-1-4-RELEASE:1.7
	netbsd-1-4:1.7.0.2
	netbsd-1-4-base:1.7
	netbsd-1-3-PATCH003:1.6
	netbsd-1-3-PATCH003-CANDIDATE2:1.6
	netbsd-1-3-PATCH003-CANDIDATE1:1.6
	netbsd-1-3-PATCH003-CANDIDATE0:1.6
	netbsd-1-3-PATCH002:1.6
	netbsd-1-3-PATCH001:1.6
	netbsd-1-3-RELEASE:1.6
	netbsd-1-3-BETA:1.6
	netbsd-1-3:1.6.0.2
	netbsd-1-3-base:1.6
	netbsd-1-2-PATCH001:1.6
	netbsd-1-2-RELEASE:1.6
	netbsd-1-2-BETA:1.6
	netbsd-1-2:1.6.0.4
	netbsd-1-2-base:1.6
	nvi_1_66:1.1.1.5
	BOSTIC:1.1.1
	netbsd-1-1-PATCH001:1.5
	netbsd-1-1-RELEASE:1.5
	netbsd-1-1:1.5.0.4
	netbsd-1-1-base:1.5
	netbsd-1-0-PATCH06:1.5
	netbsd-1-0-PATCH05:1.5
	netbsd-1-0-PATCH04:1.5
	netbsd-1-0-PATCH03:1.5
	netbsd-1-0-PATCH02:1.5
	netbsd-1-0-PATCH1:1.5
	netbsd-1-0-PATCH0:1.5
	netbsd-1-0-RELEASE:1.5
	netbsd-1-0:1.5.0.2
	nvi-1-34b:1.1.1.4
	nvi-1-33b:1.1.1.3
	netbsd-1-0-base:1.3
	nvi-1-11b:1.1.1.2
	nvi-1-03:1.1.1.1
	bostic-nvi:1.1.1;
locks; strict;
comment	@ * @;


1.12
date	2008.10.29.16.49.37;	author christos;	state dead;
branches;
next	1.11;

1.11
date	2005.02.12.12.53.23;	author aymeric;	state Exp;
branches;
next	1.10;

1.10
date	2002.04.09.01.47.33;	author thorpej;	state Exp;
branches
	1.10.4.1
	1.10.6.1;
next	1.9;

1.9
date	2001.03.31.11.37.50;	author aymeric;	state Exp;
branches;
next	1.8;

1.8
date	2000.03.17.02.23.17;	author christos;	state Exp;
branches;
next	1.7;

1.7
date	98.01.09.08.07.49;	author perry;	state Exp;
branches;
next	1.6;

1.6
date	96.05.20.03.47.35;	author mrg;	state Exp;
branches;
next	1.5;

1.5
date	94.08.17.20.12.31;	author cgd;	state Exp;
branches
	1.5.2.1;
next	1.4;

1.4
date	94.08.17.16.35.57;	author cgd;	state Exp;
branches;
next	1.3;

1.3
date	94.03.28.04.29.20;	author cgd;	state Exp;
branches;
next	1.2;

1.2
date	94.01.24.06.40.19;	author cgd;	state Exp;
branches;
next	1.1;

1.1
date	94.01.24.05.53.02;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.10.4.1
date	2005.02.12.12.24.13;	author aymeric;	state Exp;
branches;
next	1.10.4.2;

1.10.4.2
date	2005.02.12.12.46.26;	author aymeric;	state Exp;
branches;
next	;

1.10.6.1
date	2005.05.06.14.42.05;	author riz;	state Exp;
branches;
next	;

1.5.2.1
date	94.08.17.20.12.31;	author cgd;	state dead;
branches;
next	1.5.2.2;

1.5.2.2
date	94.08.17.20.12.32;	author cgd;	state Exp;
branches;
next	;

1.1.1.1
date	94.01.24.05.53.03;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	94.03.28.02.54.03;	author cgd;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	94.08.17.16.16.33;	author cgd;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	94.08.17.19.17.39;	author cgd;	state Exp;
branches;
next	1.1.1.5;

1.1.1.5
date	96.05.20.01.54.47;	author mrg;	state Exp;
branches;
next	1.1.1.6;

1.1.1.6
date	2001.03.31.11.29.46;	author aymeric;	state Exp;
branches;
next	;


desc
@@


1.12
log
@bye old vi!
@
text
@/*	$NetBSD: ex_global.c,v 1.11 2005/02/12 12:53:23 aymeric Exp $	*/

/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
#if 0
static const char sccsid[] = "@@(#)ex_global.c	10.22 (Berkeley) 10/10/96";
#else
__RCSID("$NetBSD: ex_global.c,v 1.11 2005/02/12 12:53:23 aymeric Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>

#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../common/common.h"

enum which {GLOBAL, V};

static int ex_g_setup __P((SCR *, EXCMD *, enum which));

/*
 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
 *	Exec on lines matching a pattern.
 *
 * PUBLIC: int ex_global __P((SCR *, EXCMD *));
 */
int
ex_global(sp, cmdp)
	SCR *sp;
	EXCMD *cmdp;
{
	return (ex_g_setup(sp,
	    cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL));
}

/*
 * ex_v -- [line [,line]] v /pattern/ [commands]
 *	Exec on lines not matching a pattern.
 *
 * PUBLIC: int ex_v __P((SCR *, EXCMD *));
 */
int
ex_v(sp, cmdp)
	SCR *sp;
	EXCMD *cmdp;
{
	return (ex_g_setup(sp, cmdp, V));
}

/*
 * ex_g_setup --
 *	Ex global and v commands.
 */
static int
ex_g_setup(sp, cmdp, cmd)
	SCR *sp;
	EXCMD *cmdp;
	enum which cmd;
{
	CHAR_T *ptrn, *p, *t;
	EXCMD *ecp;
	MARK abs;
	RANGE *rp;
	busy_t btype;
	recno_t start, end;
	regex_t *re;
	regmatch_t match[1];
	size_t len;
	int cnt, delim, eval;
	char *dbp;
#ifndef REG_STARTEND
	char c;
#endif

	NEEDFILE(sp, cmdp);

	if (F_ISSET(sp, SC_EX_GLOBAL)) {
		msgq(sp, M_ERR,
	"124|The %s command can't be used as part of a global or v command",
		    cmdp->cmd->name);
		return (1);
	}

	/*
	 * Skip leading white space.  Historic vi allowed any non-alphanumeric
	 * to serve as the global command delimiter.
	 */
	if (cmdp->argc == 0)
		goto usage;
	for (p = cmdp->argv[0]->bp; isblank(*p); ++p);
	if (*p == '\0' || isalnum(*p) ||
	    *p == '\\' || *p == '|' || *p == '\n') {
usage:		ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
		return (1);
	}
	delim = *p++;

	/*
	 * Get the pattern string, toss escaped characters.
	 *
	 * QUOTING NOTE:
	 * Only toss an escaped character if it escapes a delimiter.
	 */
	for (ptrn = t = p;;) {
		if (p[0] == '\0' || p[0] == delim) {
			if (p[0] == delim)
				++p;
			/*
			 * !!!
			 * Nul terminate the pattern string -- it's passed
			 * to regcomp which doesn't understand anything else.
			 */
			*t = '\0';
			break;
		}
		if (p[0] == '\\')
			if (p[1] == delim)
				++p;
			else if (p[1] == '\\')
				*t++ = *p++;
		*t++ = *p++;
	}

	/* If the pattern string is empty, use the last one. */
	if (*ptrn == '\0') {
		if (sp->re == NULL) {
			ex_emsg(sp, NULL, EXM_NOPREVRE);
			return (1);
		}

		/* Re-compile the RE if necessary. */
		if (!F_ISSET(sp, SC_RE_SEARCH) && re_compile(sp,
		    sp->re, sp->re_len, NULL, NULL, &sp->re_c, RE_C_SEARCH))
			return (1);
	} else {
		/* Compile the RE. */
		if (re_compile(sp, ptrn, t - ptrn,
		    &sp->re, &sp->re_len, &sp->re_c, RE_C_SEARCH))
			return (1);

		/*
		 * Set saved RE.  Historic practice is that globals set
		 * direction as well as the RE.
		 */
		sp->searchdir = FORWARD;
	}
	re = &sp->re_c;

	/* The global commands always set the previous context mark. */
	abs.lno = sp->lno;
	abs.cno = sp->cno;
	if (mark_set(sp, ABSMARK1, &abs, 1))
		return (1);

	/* Get an EXCMD structure. */
	CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
	CIRCLEQ_INIT(&ecp->rq);

	/*
	 * Get a copy of the command string; the default command is print.
	 * Don't worry about a set of <blank>s with no command, that will
	 * default to print in the ex parser.  We need to have two copies
	 * because the ex parser may step on the command string when it's
	 * parsing it.
	 */
	if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
		p = "pp";
		len = 1;
	}

	MALLOC_RET(sp, ecp->cp, char *, len * 2);
	ecp->o_cp = ecp->cp;
	ecp->o_clen = len;
	memcpy(ecp->cp + len, p, len);
	ecp->range_lno = OOBLNO;
	FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V);
	LIST_INSERT_HEAD(&sp->gp->ecq, ecp, q);

	/*
	 * For each line...  The semantics of global matching are that we first
	 * have to decide which lines are going to get passed to the command,
	 * and then pass them to the command, ignoring other changes.  There's
	 * really no way to do this in a single pass, since arbitrary line
	 * creation, deletion and movement can be done in the ex command.  For
	 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
	 * What we do is create linked list of lines that are tracked through
	 * each ex command.  There's a callback routine which the DB interface
	 * routines call when a line is created or deleted.  This doesn't help
	 * the layering much.
	 */
	btype = BUSY_ON;
	cnt = INTERRUPT_CHECK;
	for (start = cmdp->addr1.lno,
	    end = cmdp->addr2.lno; start <= end; ++start) {
		if (cnt-- == 0) {
			if (INTERRUPTED(sp)) {
				LIST_REMOVE(ecp, q);
				free(ecp->cp);
				free(ecp);
				break;
			}
			search_busy(sp, btype);
			btype = BUSY_UPDATE;
			cnt = INTERRUPT_CHECK;
		}
		if (db_get(sp, start, DBG_FATAL, &dbp, &len))
			return (1);
#ifdef REG_STARTEND
		match[0].rm_so = 0;
		match[0].rm_eo = len;
		eval = regexec(&sp->re_c, dbp, 0, match, REG_STARTEND);
#else
		c = dbp[len];
		dbp[len] = '\0';
		eval = regexec(&sp->re_c, dbp, 0, match, 0);
		dbp[len] = c;
#endif
		switch (eval) {
		case 0:
			if (cmd == V)
				continue;
			break;
		case REG_NOMATCH:
			if (cmd == GLOBAL)
				continue;
			break;
		default:
			re_error(sp, eval, &sp->re_c);
			break;
		}

		/* If follows the last entry, extend the last entry's range. */
		if ((rp = ecp->rq.cqh_last) != (void *)&ecp->rq &&
		    rp->stop == start - 1) {
			++rp->stop;
			continue;
		}

		/* Allocate a new range, and append it to the list. */
		CALLOC(sp, rp, RANGE *, 1, sizeof(RANGE));
		if (rp == NULL)
			return (1);
		rp->start = rp->stop = start;
		CIRCLEQ_INSERT_TAIL(&ecp->rq, rp, q);
	}
	search_busy(sp, BUSY_OFF);
	return (0);
}

/*
 * ex_g_insdel --
 *	Update the ranges based on an insertion or deletion.
 *
 * PUBLIC: int ex_g_insdel __P((SCR *, lnop_t, recno_t));
 */
int
ex_g_insdel(sp, op, lno)
	SCR *sp;
	lnop_t op;
	recno_t lno;
{
	EXCMD *ecp;
	RANGE *nrp, *rp;

	/* All insert/append operations are done as inserts. */
	if (op == LINE_APPEND)
		abort();

	if (op == LINE_RESET)
		return (0);

	for (ecp = sp->gp->ecq.lh_first; ecp != NULL; ecp = ecp->q.le_next) {
		if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
			continue;
		for (rp = ecp->rq.cqh_first; rp != (void *)&ecp->rq; rp = nrp) {
			nrp = rp->q.cqe_next;

			/* If range less than the line, ignore it. */
			if (rp->stop < lno)
				continue;
			
			/*
			 * If range greater than the line, decrement or
			 * increment the range.
			 */
			if (rp->start > lno) {
				if (op == LINE_DELETE) {
					--rp->start;
					--rp->stop;
				} else {
					++rp->start;
					++rp->stop;
				}
				continue;
			}

			/*
			 * Lno is inside the range, decrement the end point
			 * for deletion, and split the range for insertion.
			 * In the latter case, since we're inserting a new
			 * element, neither range can be exhausted.
			 */
			if (op == LINE_DELETE) {
				if (rp->start > --rp->stop) {
					CIRCLEQ_REMOVE(&ecp->rq, rp, q);
					free(rp);
				}
			} else {
				CALLOC_RET(sp, nrp, RANGE *, 1, sizeof(RANGE));
				nrp->start = lno + 1;
				nrp->stop = rp->stop + 1;
				rp->stop = lno - 1;
				CIRCLEQ_INSERT_AFTER(&ecp->rq, rp, nrp, q);
				rp = nrp;
			}
		}

		/*
		 * If the command deleted/inserted lines, the cursor moves to
		 * the line after the deleted/inserted line.
		 */
		ecp->range_lno = lno;
	}
	return (0);
}
@


1.11
log
@Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: ex_global.c,v 1.10 2002/04/09 01:47:33 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.10
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: ex_global.c,v 1.9 2001/03/31 11:37:50 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD");
@


1.10.6.1
log
@Pull up revision 1.11 (requested by aymeric in ticket #1195):
Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: ex_global.c,v 1.10 2002/04/09 01:47:33 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.10.4.1
log
@Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: ex_global.c,v 1.10 2002/04/09 01:47:33 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.10.4.2
log
@Backout previous. Sorry.
@
text
@d19 1
a19 1
__RCSID("$NetBSD");
@


1.9
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: ex_global.c,v 1.8 2000/03/17 02:23:17 christos Exp $	*/
d14 1
d16 1
d18 3
@


1.8
log
@Don't depend on BSD regexec extensions (REG_STARTEND)
Emulate it if it is not defined.
@
text
@d1 1
a1 1
/*	$NetBSD: ex_global.c,v 1.7 1998/01/09 08:07:49 perry Exp $	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)ex_global.c	10.20 (Berkeley) 5/3/96";
d75 1
d85 1
a85 1
	char *ptrn, *p, *t;
d146 3
a148 3
		/* Compile the RE if necessary. */
		if (!F_ISSET(sp, SC_RE_SEARCH) &&
		    re_compile(sp, sp->re, NULL, NULL, &sp->re_c, RE_C_SEARCH))
d152 2
a153 2
		if (re_compile(sp,
		    ptrn, &sp->re, &sp->re_len, &sp->re_c, RE_C_SEARCH))
d181 1
a181 1
	if ((len = strlen(p)) == 0) {
d186 1
a186 2
	/* See ex.h for a discussion of SEARCH_TERMINATION. */
	MALLOC_RET(sp, ecp->cp, char *, len * 2 + SEARCH_TERMINATION);
d189 1
a189 1
	memmove(ecp->cp + len + SEARCH_TERMINATION, p, len);
d221 1
a221 1
		if (db_get(sp, start, DBG_FATAL, &p, &len))
d226 1
a226 1
		eval = regexec(&sp->re_c, p, 0, match, REG_STARTEND);
d228 4
a231 4
		c = p[len];
		p[len] = '\0';
		eval = regexec(&sp->re_c, p, 0, match, 0);
		p[len] = c;
@


1.7
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d85 3
d223 1
d226 8
a233 1
		switch (eval = regexec(&sp->re_c, p, 0, match, REG_STARTEND)) {
@


1.6
log
@merge in nvi 1.66
@
text
@d1 2
@


1.5
log
@clean up import.
@
text
@d4 2
d7 1
a7 27
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
d10 2
d13 1
a13 1
static char sccsid[] = "@@(#)ex_global.c	8.43 (Berkeley) 8/17/94";
a17 1
#include <sys/time.h>
a22 1
#include <signal.h>
a25 1
#include <termios.h>
d28 1
a28 6
#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"
#include "excmd.h"
d30 1
a30 1
enum which {GLOBAL, VGLOBAL};
d32 1
a32 1
static int	global __P((SCR *, EXF *, EXCMDARG *, enum which));
d37 2
d41 1
a41 1
ex_global(sp, ep, cmdp)
d43 1
a43 2
	EXF *ep;
	EXCMDARG *cmdp;
d45 2
a46 2
	return (global(sp, ep,
	    cmdp, F_ISSET(cmdp, E_FORCE) ? VGLOBAL : GLOBAL));
d50 1
a50 1
 * ex_vglobal -- [line [,line]] v[global] /pattern/ [commands]
d52 2
d56 1
a56 1
ex_vglobal(sp, ep, cmdp)
d58 1
a58 2
	EXF *ep;
	EXCMDARG *cmdp;
d60 1
a60 1
	return (global(sp, ep, cmdp, VGLOBAL));
d63 4
d68 1
a68 1
global(sp, ep, cmdp, cmd)
d70 1
a70 2
	EXF *ep;
	EXCMDARG *cmdp;
d73 1
d76 3
a78 2
	EX_PRIVATE *exp;
	recno_t elno, lno;
d80 12
a91 4
	regex_t *re, lre;
	size_t clen, len;
	int delim, eval, reflags, replaced, rval;
	char *cb, *ptrn, *p, *t;
d94 2
a95 2
	 * Skip leading white space.  Historic vi allowed any non-
	 * alphanumeric to serve as the global command delimiter.
d97 2
d100 3
a102 2
	if (*p == '\0' || isalnum(*p)) {
		msgq(sp, M_ERR, "Usage: %s", cmdp->cmd->usage);
d125 5
a129 2
		if (p[0] == '\\' && p[1] == delim)
			++p;
d135 2
a136 2
		if (!F_ISSET(sp, S_SRE_SET)) {
			msgq(sp, M_ERR, "No previous regular expression");
a138 8
		re = &sp->sre;
	} else {
		/* Set RE flags. */
		reflags = 0;
		if (O_ISSET(sp, O_EXTENDED))
			reflags |= REG_EXTENDED;
		if (O_ISSET(sp, O_IGNORECASE))
			reflags |= REG_ICASE;
d140 3
a142 2
		/* Convert vi-style RE's to POSIX 1003.2 RE's. */
		if (re_conv(sp, &ptrn, &replaced))
d144 1
a144 1

d146 2
a147 9
		re = &lre;
		eval = regcomp(re, ptrn, reflags);

		/* Free up any allocated memory. */
		if (replaced)
			FREE_SPACE(sp, ptrn, 0);

		if (eval) {
			re_error(sp, eval, re);
a148 1
		}
d151 2
a152 2
		 * Set saved RE.  Historic practice is that
		 * globals set direction as well as the RE.
a153 1
		sp->sre = lre;
a154 1
		F_SET(sp, S_SRE_SET);
d156 11
d171 3
a173 1
	 * default to print in the ex parser.
d175 3
a177 3
	if ((clen = strlen(p)) == 0) {
		p = "p";
		clen = 1;
a178 2
	MALLOC_RET(sp, cb, char *, clen);
	memmove(cb, p, clen);
d180 8
a187 15
	/*
	 * The global commands sets the substitute RE as well as
	 * the everything-else RE.
	 */
	sp->subre = sp->sre;
	F_SET(sp, S_SUBRE_SET);

	/* Set the global flag. */
	F_SET(sp, S_GLOBAL);

	/* The global commands always set the previous context mark. */
	abs.lno = sp->lno;
	abs.cno = sp->cno;
	if (mark_set(sp, ep, ABSMARK1, &abs, 1))
		goto err;
d201 14
a214 11
	exp = EXP(sp);
	for (rval = 0, lno = cmdp->addr1.lno,
	    elno = cmdp->addr2.lno; lno <= elno; ++lno) {
		/* Someone's unhappy, time to stop. */
		if (INTERRUPTED(sp))
			goto interrupted;

		/* Get the line and search for a match. */
		if ((t = file_gline(sp, ep, lno, &len)) == NULL) {
			GETLINE_ERR(sp, lno);
			goto err;
d216 2
d220 1
a220 1
		switch(eval = regexec(re, t, 1, match, REG_STARTEND)) {
d222 1
a222 1
			if (cmd == VGLOBAL)
d230 2
a231 2
			re_error(sp, eval, re);
			goto err;
d235 2
a236 2
		if ((rp = exp->rangeq.cqh_last) != (void *)&exp->rangeq &&
		    rp->stop == lno - 1) {
a243 40
			goto err;
		rp->start = rp->stop = lno;
		CIRCLEQ_INSERT_TAIL(&exp->rangeq, rp, q);
	}

	exp = EXP(sp);
	exp->range_lno = OOBLNO;
	for (;;) {
		/*
		 * Start at the beginning of the range each time, it may have
		 * been changed (or exhausted) if lines were inserted/deleted.
		 */
		if ((rp = exp->rangeq.cqh_first) == (void *)&exp->rangeq)
			break;
		if (rp->start > rp->stop) {
			CIRCLEQ_REMOVE(&exp->rangeq, exp->rangeq.cqh_first, q);
			free(rp);
			continue;
		}

		/*
		 * Execute the command, setting the cursor to the line so that
		 * relative addressing works.  This means that the cursor moves
		 * to the last line sent to the command, by default, even if
		 * the command fails.
		 */
		exp->range_lno = sp->lno = rp->start++;
		if (ex_cmd(sp, ep, cb, clen, 0))
			goto err;

		/* Someone's unhappy, time to stop. */
		if (INTERRUPTED(sp)) {
interrupted:		msgq(sp, M_INFO, "Interrupted");
			break;
		}
	}

	/* Set the cursor to the new value, making sure it exists. */
	if (exp->range_lno != OOBLNO) {
		if (file_lline(sp, ep, &lno))
d245 2
a246 5
		sp->lno =
		    lno < exp->range_lno ? (lno ? lno : 1) : exp->range_lno;
	}
	if (0) {
err:		rval = 1;
d248 2
a249 14

	/* Command we ran may have set the autoprint flag, clear it. */
	F_CLR(exp, EX_AUTOPRINT);

	/* Clear the global flag. */
	F_CLR(sp, S_GLOBAL);

	/* Free any remaining ranges and the command buffer. */
	while ((rp = exp->rangeq.cqh_first) != (void *)&exp->rangeq) {
		CIRCLEQ_REMOVE(&exp->rangeq, exp->rangeq.cqh_first, q);
		free(rp);
	}
	free(cb);
	return (rval);
d253 1
a253 1
 * global_insdel --
d255 2
d258 2
a259 2
void
global_insdel(sp, ep, op, lno)
d261 1
a261 2
	EXF *ep;
	enum operation op;
d264 1
a264 1
	EX_PRIVATE *exp;
d267 6
a272 1
	exp = EXP(sp);
d274 4
a277 6
	switch (op) {
	case LINE_APPEND:
		return;
	case LINE_DELETE:
		for (rp = exp->rangeq.cqh_first;
		    rp != (void *)&exp->rangeq; rp = nrp) {
d279 1
d283 5
a287 1
			/* If range greater than the line, decrement range. */
d289 7
a295 21
				--rp->start;
				--rp->stop;
				continue;
			}
			/* Lno is inside the range, decrement the end point. */
			if (rp->start > --rp->stop) {
				CIRCLEQ_REMOVE(&exp->rangeq, rp, q);
				free(rp);
			}
		}
		break;
	case LINE_INSERT:
		for (rp = exp->rangeq.cqh_first;
		    rp != (void *)&exp->rangeq; rp = rp->q.cqe_next) {
			/* If range less than the line, ignore it. */
			if (rp->stop < lno)
				continue;
			/* If range greater than the line, increment range. */
			if (rp->start >= lno) {
				++rp->start;
				++rp->stop;
d298 1
d300 4
a303 3
			 * Lno is inside the range, so the range must be split.
			 * Since we're inserting a new element, neither range
			 * can be exhausted.
d305 12
a316 4
			CALLOC(sp, nrp, RANGE *, 1, sizeof(RANGE));
			if (nrp == NULL) {
				F_SET(sp, S_INTERRUPTED);
				return;
a317 5
			nrp->start = lno + 1;
			nrp->stop = rp->stop + 1;
			rp->stop = lno - 1;
			CIRCLEQ_INSERT_AFTER(&exp->rangeq, rp, nrp, q);
			rp = nrp;
d319 6
a324 3
		break;
	case LINE_RESET:
		return;
d326 1
a326 5
	/*
	 * If the command deleted/inserted lines, the cursor moves to
	 * the line after the deleted/inserted line.
	 */
	exp->range_lno = lno;
@


1.5.2.1
log
@file ex_global.c was added on branch netbsd-1-0 on 1994-08-17 20:12:32 +0000
@
text
@d1 400
@


1.5.2.2
log
@clean up import.
@
text
@a0 400
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@@(#)ex_global.c	8.43 (Berkeley) 8/17/94";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"
#include "excmd.h"

enum which {GLOBAL, VGLOBAL};

static int	global __P((SCR *, EXF *, EXCMDARG *, enum which));

/*
 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
 *	Exec on lines matching a pattern.
 */
int
ex_global(sp, ep, cmdp)
	SCR *sp;
	EXF *ep;
	EXCMDARG *cmdp;
{
	return (global(sp, ep,
	    cmdp, F_ISSET(cmdp, E_FORCE) ? VGLOBAL : GLOBAL));
}

/*
 * ex_vglobal -- [line [,line]] v[global] /pattern/ [commands]
 *	Exec on lines not matching a pattern.
 */
int
ex_vglobal(sp, ep, cmdp)
	SCR *sp;
	EXF *ep;
	EXCMDARG *cmdp;
{
	return (global(sp, ep, cmdp, VGLOBAL));
}

static int
global(sp, ep, cmdp, cmd)
	SCR *sp;
	EXF *ep;
	EXCMDARG *cmdp;
	enum which cmd;
{
	MARK abs;
	RANGE *rp;
	EX_PRIVATE *exp;
	recno_t elno, lno;
	regmatch_t match[1];
	regex_t *re, lre;
	size_t clen, len;
	int delim, eval, reflags, replaced, rval;
	char *cb, *ptrn, *p, *t;

	/*
	 * Skip leading white space.  Historic vi allowed any non-
	 * alphanumeric to serve as the global command delimiter.
	 */
	for (p = cmdp->argv[0]->bp; isblank(*p); ++p);
	if (*p == '\0' || isalnum(*p)) {
		msgq(sp, M_ERR, "Usage: %s", cmdp->cmd->usage);
		return (1);
	}
	delim = *p++;

	/*
	 * Get the pattern string, toss escaped characters.
	 *
	 * QUOTING NOTE:
	 * Only toss an escaped character if it escapes a delimiter.
	 */
	for (ptrn = t = p;;) {
		if (p[0] == '\0' || p[0] == delim) {
			if (p[0] == delim)
				++p;
			/*
			 * !!!
			 * Nul terminate the pattern string -- it's passed
			 * to regcomp which doesn't understand anything else.
			 */
			*t = '\0';
			break;
		}
		if (p[0] == '\\' && p[1] == delim)
			++p;
		*t++ = *p++;
	}

	/* If the pattern string is empty, use the last one. */
	if (*ptrn == '\0') {
		if (!F_ISSET(sp, S_SRE_SET)) {
			msgq(sp, M_ERR, "No previous regular expression");
			return (1);
		}
		re = &sp->sre;
	} else {
		/* Set RE flags. */
		reflags = 0;
		if (O_ISSET(sp, O_EXTENDED))
			reflags |= REG_EXTENDED;
		if (O_ISSET(sp, O_IGNORECASE))
			reflags |= REG_ICASE;

		/* Convert vi-style RE's to POSIX 1003.2 RE's. */
		if (re_conv(sp, &ptrn, &replaced))
			return (1);

		/* Compile the RE. */
		re = &lre;
		eval = regcomp(re, ptrn, reflags);

		/* Free up any allocated memory. */
		if (replaced)
			FREE_SPACE(sp, ptrn, 0);

		if (eval) {
			re_error(sp, eval, re);
			return (1);
		}

		/*
		 * Set saved RE.  Historic practice is that
		 * globals set direction as well as the RE.
		 */
		sp->sre = lre;
		sp->searchdir = FORWARD;
		F_SET(sp, S_SRE_SET);
	}

	/*
	 * Get a copy of the command string; the default command is print.
	 * Don't worry about a set of <blank>s with no command, that will
	 * default to print in the ex parser.
	 */
	if ((clen = strlen(p)) == 0) {
		p = "p";
		clen = 1;
	}
	MALLOC_RET(sp, cb, char *, clen);
	memmove(cb, p, clen);

	/*
	 * The global commands sets the substitute RE as well as
	 * the everything-else RE.
	 */
	sp->subre = sp->sre;
	F_SET(sp, S_SUBRE_SET);

	/* Set the global flag. */
	F_SET(sp, S_GLOBAL);

	/* The global commands always set the previous context mark. */
	abs.lno = sp->lno;
	abs.cno = sp->cno;
	if (mark_set(sp, ep, ABSMARK1, &abs, 1))
		goto err;

	/*
	 * For each line...  The semantics of global matching are that we first
	 * have to decide which lines are going to get passed to the command,
	 * and then pass them to the command, ignoring other changes.  There's
	 * really no way to do this in a single pass, since arbitrary line
	 * creation, deletion and movement can be done in the ex command.  For
	 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
	 * What we do is create linked list of lines that are tracked through
	 * each ex command.  There's a callback routine which the DB interface
	 * routines call when a line is created or deleted.  This doesn't help
	 * the layering much.
	 */
	exp = EXP(sp);
	for (rval = 0, lno = cmdp->addr1.lno,
	    elno = cmdp->addr2.lno; lno <= elno; ++lno) {
		/* Someone's unhappy, time to stop. */
		if (INTERRUPTED(sp))
			goto interrupted;

		/* Get the line and search for a match. */
		if ((t = file_gline(sp, ep, lno, &len)) == NULL) {
			GETLINE_ERR(sp, lno);
			goto err;
		}
		match[0].rm_so = 0;
		match[0].rm_eo = len;
		switch(eval = regexec(re, t, 1, match, REG_STARTEND)) {
		case 0:
			if (cmd == VGLOBAL)
				continue;
			break;
		case REG_NOMATCH:
			if (cmd == GLOBAL)
				continue;
			break;
		default:
			re_error(sp, eval, re);
			goto err;
		}

		/* If follows the last entry, extend the last entry's range. */
		if ((rp = exp->rangeq.cqh_last) != (void *)&exp->rangeq &&
		    rp->stop == lno - 1) {
			++rp->stop;
			continue;
		}

		/* Allocate a new range, and append it to the list. */
		CALLOC(sp, rp, RANGE *, 1, sizeof(RANGE));
		if (rp == NULL)
			goto err;
		rp->start = rp->stop = lno;
		CIRCLEQ_INSERT_TAIL(&exp->rangeq, rp, q);
	}

	exp = EXP(sp);
	exp->range_lno = OOBLNO;
	for (;;) {
		/*
		 * Start at the beginning of the range each time, it may have
		 * been changed (or exhausted) if lines were inserted/deleted.
		 */
		if ((rp = exp->rangeq.cqh_first) == (void *)&exp->rangeq)
			break;
		if (rp->start > rp->stop) {
			CIRCLEQ_REMOVE(&exp->rangeq, exp->rangeq.cqh_first, q);
			free(rp);
			continue;
		}

		/*
		 * Execute the command, setting the cursor to the line so that
		 * relative addressing works.  This means that the cursor moves
		 * to the last line sent to the command, by default, even if
		 * the command fails.
		 */
		exp->range_lno = sp->lno = rp->start++;
		if (ex_cmd(sp, ep, cb, clen, 0))
			goto err;

		/* Someone's unhappy, time to stop. */
		if (INTERRUPTED(sp)) {
interrupted:		msgq(sp, M_INFO, "Interrupted");
			break;
		}
	}

	/* Set the cursor to the new value, making sure it exists. */
	if (exp->range_lno != OOBLNO) {
		if (file_lline(sp, ep, &lno))
			return (1);
		sp->lno =
		    lno < exp->range_lno ? (lno ? lno : 1) : exp->range_lno;
	}
	if (0) {
err:		rval = 1;
	}

	/* Command we ran may have set the autoprint flag, clear it. */
	F_CLR(exp, EX_AUTOPRINT);

	/* Clear the global flag. */
	F_CLR(sp, S_GLOBAL);

	/* Free any remaining ranges and the command buffer. */
	while ((rp = exp->rangeq.cqh_first) != (void *)&exp->rangeq) {
		CIRCLEQ_REMOVE(&exp->rangeq, exp->rangeq.cqh_first, q);
		free(rp);
	}
	free(cb);
	return (rval);
}

/*
 * global_insdel --
 *	Update the ranges based on an insertion or deletion.
 */
void
global_insdel(sp, ep, op, lno)
	SCR *sp;
	EXF *ep;
	enum operation op;
	recno_t lno;
{
	EX_PRIVATE *exp;
	RANGE *nrp, *rp;

	exp = EXP(sp);

	switch (op) {
	case LINE_APPEND:
		return;
	case LINE_DELETE:
		for (rp = exp->rangeq.cqh_first;
		    rp != (void *)&exp->rangeq; rp = nrp) {
			nrp = rp->q.cqe_next;
			/* If range less than the line, ignore it. */
			if (rp->stop < lno)
				continue;
			/* If range greater than the line, decrement range. */
			if (rp->start > lno) {
				--rp->start;
				--rp->stop;
				continue;
			}
			/* Lno is inside the range, decrement the end point. */
			if (rp->start > --rp->stop) {
				CIRCLEQ_REMOVE(&exp->rangeq, rp, q);
				free(rp);
			}
		}
		break;
	case LINE_INSERT:
		for (rp = exp->rangeq.cqh_first;
		    rp != (void *)&exp->rangeq; rp = rp->q.cqe_next) {
			/* If range less than the line, ignore it. */
			if (rp->stop < lno)
				continue;
			/* If range greater than the line, increment range. */
			if (rp->start >= lno) {
				++rp->start;
				++rp->stop;
				continue;
			}
			/*
			 * Lno is inside the range, so the range must be split.
			 * Since we're inserting a new element, neither range
			 * can be exhausted.
			 */
			CALLOC(sp, nrp, RANGE *, 1, sizeof(RANGE));
			if (nrp == NULL) {
				F_SET(sp, S_INTERRUPTED);
				return;
			}
			nrp->start = lno + 1;
			nrp->stop = rp->stop + 1;
			rp->stop = lno - 1;
			CIRCLEQ_INSERT_AFTER(&exp->rangeq, rp, nrp, q);
			rp = nrp;
		}
		break;
	case LINE_RESET:
		return;
	}
	/*
	 * If the command deleted/inserted lines, the cursor moves to
	 * the line after the deleted/inserted line.
	 */
	exp->range_lno = lno;
}
@


1.4
log
@clean up import.  still have to hack some things.
@
text
@d35 1
a35 1
static const char sccsid[] = "@@(#)ex_global.c	8.42 (Berkeley) 8/17/94";
@


1.3
log
@nvi 1.11(beta) from bostic.  reconcile conflicts/kill rcsids.
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)ex_global.c	8.32 (Berkeley) 3/22/94";
d98 1
d105 1
a105 1
	int delim, eval, reflags, replaced, rval, teardown;
d114 1
a114 1
		msgq(sp, M_ERR, "Usage: %s.", cmdp->cmd->usage);
d145 1
a145 1
			msgq(sp, M_ERR, "No previous regular expression.");
d167 1
a167 1
			free(ptrn);
d183 5
a187 1
	/* Get a copy of the command string. */
d189 2
a190 2
		msgq(sp, M_ERR, "No command string specified.");
		return (1);
d202 1
a202 1
	/* Set the global flag, and set up interrupts. */
d204 6
a209 1
	teardown = !intr_init(sp);
d227 1
a227 1
		if (F_ISSET(sp, S_INTERRUPTED))
d288 1
a288 1
		if (ex_cmd(sp, ep, cb, clen))
d292 2
a293 2
		if (F_ISSET(sp, S_INTERRUPTED)) {
interrupted:		msgq(sp, M_INFO, "Interrupted.");
d309 4
a313 2
	if (teardown)
		intr_end(sp);
@


1.2
log
@more Ids than you'll ever want.
@
text
@d2 1
a2 1
 * Copyright (c) 1992, 1993
d35 1
a35 2
/* from: static char sccsid[] = "@@(#)ex_global.c	8.29 (Berkeley) 1/9/94"; */
static char *rcsid = "$Id$";
d39 2
d42 1
d45 3
d50 1
d53 4
a58 1
#include "interrupt.h"
a62 1
static void	global_intr __P((int));
a97 1
	DECLARE_INTERRUPTS;
d104 1
a104 1
	int delim, eval, reflags, replaced, rval;
d199 1
a199 1
	SET_UP_INTERRUPTS(global_intr);
d211 1
a211 1
	 * the layering much. 
a298 1
interrupt_err:
d300 2
a301 1
	TEAR_DOWN_INTERRUPTS;
a387 23
}

/*
 * global_intr --
 *	Set the interrupt bit in any screen that is running an interruptible
 *	global.
 *
 * XXX
 * In the future this may be a problem.  The user should be able to move to
 * another screen and keep typing while this runs.  If so, and the user has
 * more than one global running, it will be hard to decide which one to
 * stop.
 */
static void
global_intr(signo)
	int signo;
{
	SCR *sp;

	for (sp = __global_list->dq.cqh_first;
	    sp != (void *)&__global_list->dq; sp = sp->q.cqe_next)
		if (F_ISSET(sp, S_GLOBAL) && F_ISSET(sp, S_INTERRUPTIBLE))
			F_SET(sp, S_INTERRUPTED);
@


1.1
log
@Initial revision
@
text
@d35 2
a36 1
static char sccsid[] = "@@(#)ex_global.c	8.29 (Berkeley) 1/9/94";
@


1.1.1.1
log
@nvi 1.03, from ftp.cs.berkeley.edu, per keith bostic's permission.
@
text
@@


1.1.1.2
log
@nvi/nex 1.11beta from bostic.
@
text
@d2 1
a2 1
 * Copyright (c) 1992, 1993, 1994
d35 1
a35 1
static char sccsid[] = "@@(#)ex_global.c	8.32 (Berkeley) 3/22/94";
a38 2
#include <sys/queue.h>
#include <sys/time.h>
a39 1
#include <bitstring.h>
a41 3
#include <limits.h>
#include <signal.h>
#include <stdio.h>
a43 1
#include <termios.h>
a45 4
#include "compat.h"
#include <db.h>
#include <regex.h>

d48 1
d53 1
d89 1
d96 1
a96 1
	int delim, eval, reflags, replaced, rval, teardown;
d191 1
a191 1
	teardown = !intr_init(sp);
d203 1
a203 1
	 * the layering much.
d291 1
d293 1
a293 2
	if (teardown)
		intr_end(sp);
d380 23
@


1.1.1.3
log
@new public version of nvi
@
text
@d35 1
a35 1
static const char sccsid[] = "@@(#)ex_global.c	8.42 (Berkeley) 8/17/94";
a97 1
	MARK abs;
d104 1
a104 1
	int delim, eval, reflags, replaced, rval;
d113 1
a113 1
		msgq(sp, M_ERR, "Usage: %s", cmdp->cmd->usage);
d144 1
a144 1
			msgq(sp, M_ERR, "No previous regular expression");
d166 1
a166 1
			FREE_SPACE(sp, ptrn, 0);
d182 1
a182 5
	/*
	 * Get a copy of the command string; the default command is print.
	 * Don't worry about a set of <blank>s with no command, that will
	 * default to print in the ex parser.
	 */
d184 2
a185 2
		p = "p";
		clen = 1;
d197 1
a197 1
	/* Set the global flag. */
d199 1
a199 6

	/* The global commands always set the previous context mark. */
	abs.lno = sp->lno;
	abs.cno = sp->cno;
	if (mark_set(sp, ep, ABSMARK1, &abs, 1))
		goto err;
d217 1
a217 1
		if (INTERRUPTED(sp))
d278 1
a278 1
		if (ex_cmd(sp, ep, cb, clen, 0))
d282 2
a283 2
		if (INTERRUPTED(sp)) {
interrupted:		msgq(sp, M_INFO, "Interrupted");
a298 4
	/* Command we ran may have set the autoprint flag, clear it. */
	F_CLR(exp, EX_AUTOPRINT);

	/* Clear the global flag. */
d300 2
@


1.1.1.4
log
@new public version of nvi
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)ex_global.c	8.43 (Berkeley) 8/17/94";
@


1.1.1.5
log
@import of nvi 1.66
@
text
@a3 2
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
d5 27
a31 1
 * See the LICENSE file for redistribution information.
a33 2
#include "config.h"

d35 1
a35 1
static const char sccsid[] = "@@(#)ex_global.c	10.20 (Berkeley) 5/3/96";
d40 1
d46 1
d50 1
d53 6
a58 1
#include "../common/common.h"
d60 1
a60 1
enum which {GLOBAL, V};
d62 1
a62 1
static int ex_g_setup __P((SCR *, EXCMD *, enum which));
a66 2
 *
 * PUBLIC: int ex_global __P((SCR *, EXCMD *));
d69 1
a69 1
ex_global(sp, cmdp)
d71 2
a72 1
	EXCMD *cmdp;
d74 2
a75 2
	return (ex_g_setup(sp,
	    cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL));
d79 1
a79 1
 * ex_v -- [line [,line]] v /pattern/ [commands]
a80 2
 *
 * PUBLIC: int ex_v __P((SCR *, EXCMD *));
d83 1
a83 1
ex_v(sp, cmdp)
d85 2
a86 1
	EXCMD *cmdp;
d88 1
a88 1
	return (ex_g_setup(sp, cmdp, V));
a90 4
/*
 * ex_g_setup --
 *	Ex global and v commands.
 */
d92 1
a92 1
ex_g_setup(sp, cmdp, cmd)
d94 2
a95 1
	EXCMD *cmdp;
a97 1
	EXCMD *ecp;
d100 2
a101 3
	busy_t btype;
	recno_t start, end;
	regex_t *re;
d103 4
a106 12
	size_t len;
	int cnt, delim, eval;
	char *ptrn, *p, *t;

	NEEDFILE(sp, cmdp);

	if (F_ISSET(sp, SC_EX_GLOBAL)) {
		msgq(sp, M_ERR,
	"124|The %s command can't be used as part of a global or v command",
		    cmdp->cmd->name);
		return (1);
	}
d109 2
a110 2
	 * Skip leading white space.  Historic vi allowed any non-alphanumeric
	 * to serve as the global command delimiter.
a111 2
	if (cmdp->argc == 0)
		goto usage;
d113 2
a114 3
	if (*p == '\0' || isalnum(*p) ||
	    *p == '\\' || *p == '|' || *p == '\n') {
usage:		ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
d137 2
a138 5
		if (p[0] == '\\')
			if (p[1] == delim)
				++p;
			else if (p[1] == '\\')
				*t++ = *p++;
d144 2
a145 2
		if (sp->re == NULL) {
			ex_emsg(sp, NULL, EXM_NOPREVRE);
d148 8
d157 2
a158 3
		/* Compile the RE if necessary. */
		if (!F_ISSET(sp, SC_RE_SEARCH) &&
		    re_compile(sp, sp->re, NULL, NULL, &sp->re_c, RE_C_SEARCH))
d160 1
a160 1
	} else {
d162 9
a170 2
		if (re_compile(sp,
		    ptrn, &sp->re, &sp->re_len, &sp->re_c, RE_C_SEARCH))
d172 1
d175 2
a176 2
		 * Set saved RE.  Historic practice is that globals set
		 * direction as well as the RE.
d178 1
d180 1
a181 11
	re = &sp->re_c;

	/* The global commands always set the previous context mark. */
	abs.lno = sp->lno;
	abs.cno = sp->cno;
	if (mark_set(sp, ABSMARK1, &abs, 1))
		return (1);

	/* Get an EXCMD structure. */
	CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
	CIRCLEQ_INIT(&ecp->rq);
d186 1
a186 3
	 * default to print in the ex parser.  We need to have two copies
	 * because the ex parser may step on the command string when it's
	 * parsing it.
d188 3
a190 3
	if ((len = strlen(p)) == 0) {
		p = "pp";
		len = 1;
d192 2
d195 15
a209 8
	/* See ex.h for a discussion of SEARCH_TERMINATION. */
	MALLOC_RET(sp, ecp->cp, char *, len * 2 + SEARCH_TERMINATION);
	ecp->o_cp = ecp->cp;
	ecp->o_clen = len;
	memmove(ecp->cp + len + SEARCH_TERMINATION, p, len);
	ecp->range_lno = OOBLNO;
	FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V);
	LIST_INSERT_HEAD(&sp->gp->ecq, ecp, q);
d223 11
a233 14
	btype = BUSY_ON;
	cnt = INTERRUPT_CHECK;
	for (start = cmdp->addr1.lno,
	    end = cmdp->addr2.lno; start <= end; ++start) {
		if (cnt-- == 0) {
			if (INTERRUPTED(sp)) {
				LIST_REMOVE(ecp, q);
				free(ecp->cp);
				free(ecp);
				break;
			}
			search_busy(sp, btype);
			btype = BUSY_UPDATE;
			cnt = INTERRUPT_CHECK;
a234 2
		if (db_get(sp, start, DBG_FATAL, &p, &len))
			return (1);
d237 1
a237 1
		switch (eval = regexec(&sp->re_c, p, 0, match, REG_STARTEND)) {
d239 1
a239 1
			if (cmd == V)
d247 2
a248 2
			re_error(sp, eval, &sp->re_c);
			break;
d252 2
a253 2
		if ((rp = ecp->rq.cqh_last) != (void *)&ecp->rq &&
		    rp->stop == start - 1) {
d261 40
d302 5
a306 2
		rp->start = rp->stop = start;
		CIRCLEQ_INSERT_TAIL(&ecp->rq, rp, q);
d308 14
a321 2
	search_busy(sp, BUSY_OFF);
	return (0);
d325 1
a325 1
 * ex_g_insdel --
a326 2
 *
 * PUBLIC: int ex_g_insdel __P((SCR *, lnop_t, recno_t));
d328 2
a329 2
int
ex_g_insdel(sp, op, lno)
d331 2
a332 1
	lnop_t op;
d335 1
a335 1
	EXCMD *ecp;
d338 1
a338 6
	/* All insert/append operations are done as inserts. */
	if (op == LINE_APPEND)
		abort();

	if (op == LINE_RESET)
		return (0);
d340 6
a345 4
	for (ecp = sp->gp->ecq.lh_first; ecp != NULL; ecp = ecp->q.le_next) {
		if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
			continue;
		for (rp = ecp->rq.cqh_first; rp != (void *)&ecp->rq; rp = nrp) {
a346 1

d350 1
a350 5
			
			/*
			 * If range greater than the line, decrement or
			 * increment the range.
			 */
d352 21
a372 7
				if (op == LINE_DELETE) {
					--rp->start;
					--rp->stop;
				} else {
					++rp->start;
					++rp->stop;
				}
a374 1

d376 3
a378 4
			 * Lno is inside the range, decrement the end point
			 * for deletion, and split the range for insertion.
			 * In the latter case, since we're inserting a new
			 * element, neither range can be exhausted.
d380 4
a383 12
			if (op == LINE_DELETE) {
				if (rp->start > --rp->stop) {
					CIRCLEQ_REMOVE(&ecp->rq, rp, q);
					free(rp);
				}
			} else {
				CALLOC_RET(sp, nrp, RANGE *, 1, sizeof(RANGE));
				nrp->start = lno + 1;
				nrp->stop = rp->stop + 1;
				rp->stop = lno - 1;
				CIRCLEQ_INSERT_AFTER(&ecp->rq, rp, nrp, q);
				rp = nrp;
d385 5
d391 3
a393 6

		/*
		 * If the command deleted/inserted lines, the cursor moves to
		 * the line after the deleted/inserted line.
		 */
		ecp->range_lno = lno;
d395 5
a399 1
	return (0);
@


1.1.1.6
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)ex_global.c	10.22 (Berkeley) 10/10/96";
a72 1
	CHAR_T *ptrn, *p, *t;
d82 1
a82 1
	char *dbp;
d140 3
a142 3
		/* Re-compile the RE if necessary. */
		if (!F_ISSET(sp, SC_RE_SEARCH) && re_compile(sp,
		    sp->re, sp->re_len, NULL, NULL, &sp->re_c, RE_C_SEARCH))
d146 2
a147 2
		if (re_compile(sp, ptrn, t - ptrn,
		    &sp->re, &sp->re_len, &sp->re_c, RE_C_SEARCH))
d175 1
a175 1
	if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
d180 2
a181 1
	MALLOC_RET(sp, ecp->cp, char *, len * 2);
d184 1
a184 1
	memcpy(ecp->cp + len, p, len);
d216 1
a216 1
		if (db_get(sp, start, DBG_FATAL, &dbp, &len))
d220 1
a220 2
		switch (eval =
		    regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) {
@

