xref: /minix3/external/bsd/nvi/dist/ex/ex_at.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_at.c,v 1.5 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_at.c,v 10.16 2001/06/25 15:19:14 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:14 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_at.c,v 1.5 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <ctype.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <stdlib.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc #include "../common/common.h"
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * ex_at -- :@[@ | buffer]
3684d9c625SLionel Sambuc  *	    :*[* | buffer]
3784d9c625SLionel Sambuc  *
3884d9c625SLionel Sambuc  *	Execute the contents of the buffer.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * PUBLIC: int ex_at __P((SCR *, EXCMD *));
4184d9c625SLionel Sambuc  */
4284d9c625SLionel Sambuc int
ex_at(SCR * sp,EXCMD * cmdp)4384d9c625SLionel Sambuc ex_at(SCR *sp, EXCMD *cmdp)
4484d9c625SLionel Sambuc {
4584d9c625SLionel Sambuc 	CB *cbp;
4684d9c625SLionel Sambuc 	ARG_CHAR_T name;
4784d9c625SLionel Sambuc 	EXCMD *ecp;
4884d9c625SLionel Sambuc 	RANGE *rp;
4984d9c625SLionel Sambuc 	TEXT *tp;
5084d9c625SLionel Sambuc 	size_t len;
5184d9c625SLionel Sambuc 	CHAR_T *p;
5284d9c625SLionel Sambuc 
5384d9c625SLionel Sambuc 	/*
5484d9c625SLionel Sambuc 	 * !!!
5584d9c625SLionel Sambuc 	 * Historically, [@*]<carriage-return> and [@*][@*] executed the most
5684d9c625SLionel Sambuc 	 * recently executed buffer in ex mode.
5784d9c625SLionel Sambuc 	 */
5884d9c625SLionel Sambuc 	name = FL_ISSET(cmdp->iflags, E_C_BUFFER) ? cmdp->buffer : '@';
5984d9c625SLionel Sambuc 	if (name == '@' || name == '*') {
6084d9c625SLionel Sambuc 		if (!F_ISSET(sp, SC_AT_SET)) {
6184d9c625SLionel Sambuc 			ex_emsg(sp, NULL, EXM_NOPREVBUF);
6284d9c625SLionel Sambuc 			return (1);
6384d9c625SLionel Sambuc 		}
6484d9c625SLionel Sambuc 		name = sp->at_lbuf;
6584d9c625SLionel Sambuc 	}
6684d9c625SLionel Sambuc 	sp->at_lbuf = name;
6784d9c625SLionel Sambuc 	F_SET(sp, SC_AT_SET);
6884d9c625SLionel Sambuc 
6984d9c625SLionel Sambuc 	CBNAME(sp, cbp, name);
7084d9c625SLionel Sambuc 	if (cbp == NULL) {
7184d9c625SLionel Sambuc 		ex_emsg(sp, (char *)KEY_NAME(sp, name), EXM_EMPTYBUF);
7284d9c625SLionel Sambuc 		return (1);
7384d9c625SLionel Sambuc 	}
7484d9c625SLionel Sambuc 
7584d9c625SLionel Sambuc 	/*
7684d9c625SLionel Sambuc 	 * !!!
7784d9c625SLionel Sambuc 	 * Historically the @ command took a range of lines, and the @ buffer
7884d9c625SLionel Sambuc 	 * was executed once per line.  The historic vi could be trashed by
7984d9c625SLionel Sambuc 	 * this because it didn't notice if the underlying file changed, or,
8084d9c625SLionel Sambuc 	 * for that matter, if there were no more lines on which to operate.
8184d9c625SLionel Sambuc 	 * For example, take a 10 line file, load "%delete" into a buffer,
8284d9c625SLionel Sambuc 	 * and enter :8,10@<buffer>.
8384d9c625SLionel Sambuc 	 *
8484d9c625SLionel Sambuc 	 * The solution is a bit tricky.  If the user specifies a range, take
8584d9c625SLionel Sambuc 	 * the same approach as for global commands, and discard the command
8684d9c625SLionel Sambuc 	 * if exit or switch to a new file/screen.  If the user doesn't specify
8784d9c625SLionel Sambuc 	 * the  range, continue to execute after a file/screen switch, which
8884d9c625SLionel Sambuc 	 * means @ buffers are still useful in a multi-screen environment.
8984d9c625SLionel Sambuc 	 */
9084d9c625SLionel Sambuc 	CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
9184d9c625SLionel Sambuc 	TAILQ_INIT(&ecp->rq);
9284d9c625SLionel Sambuc 	CALLOC_RET(sp, rp, RANGE *, 1, sizeof(RANGE));
9384d9c625SLionel Sambuc 	rp->start = cmdp->addr1.lno;
9484d9c625SLionel Sambuc 	if (F_ISSET(cmdp, E_ADDR_DEF)) {
9584d9c625SLionel Sambuc 		rp->stop = rp->start;
9684d9c625SLionel Sambuc 		FL_SET(ecp->agv_flags, AGV_AT_NORANGE);
9784d9c625SLionel Sambuc 	} else {
9884d9c625SLionel Sambuc 		rp->stop = cmdp->addr2.lno;
9984d9c625SLionel Sambuc 		FL_SET(ecp->agv_flags, AGV_AT);
10084d9c625SLionel Sambuc 	}
10184d9c625SLionel Sambuc 	TAILQ_INSERT_HEAD(&ecp->rq, rp, q);
10284d9c625SLionel Sambuc 
10384d9c625SLionel Sambuc 	/*
10484d9c625SLionel Sambuc 	 * Buffers executed in ex mode or from the colon command line in vi
10584d9c625SLionel Sambuc 	 * were ex commands.  We can't push it on the terminal queue, since
10684d9c625SLionel Sambuc 	 * it has to be executed immediately, and we may be in the middle of
10784d9c625SLionel Sambuc 	 * an ex command already.  Push the command on the ex command stack.
10884d9c625SLionel Sambuc 	 * Build two copies of the command.  We need two copies because the
10984d9c625SLionel Sambuc 	 * ex parser may step on the command string when it's parsing it.
11084d9c625SLionel Sambuc 	 */
11184d9c625SLionel Sambuc 	for (len = 0, tp = TAILQ_LAST(&cbp->textq, _texth);
11284d9c625SLionel Sambuc 	    tp != NULL; tp = TAILQ_PREV(tp, _texth, q))
11384d9c625SLionel Sambuc 		len += tp->len + 1;
11484d9c625SLionel Sambuc 
11584d9c625SLionel Sambuc 	MALLOC_GOTO(sp, ecp->cp, CHAR_T *, len * 2 * sizeof(CHAR_T));
11684d9c625SLionel Sambuc 	ecp->o_cp = ecp->cp;
11784d9c625SLionel Sambuc 	ecp->o_clen = len;
11884d9c625SLionel Sambuc 	ecp->cp[len] = '\0';
11984d9c625SLionel Sambuc 
12084d9c625SLionel Sambuc 	/* Copy the buffer into the command space. */
12184d9c625SLionel Sambuc 	for (p = ecp->cp + len, tp = TAILQ_LAST(&cbp->textq, _texth);
12284d9c625SLionel Sambuc 	    tp != NULL; tp = TAILQ_PREV(tp, _texth, q)) {
12384d9c625SLionel Sambuc 		MEMCPYW(p, tp->lb, tp->len);
12484d9c625SLionel Sambuc 		p += tp->len;
12584d9c625SLionel Sambuc 		*p++ = '\n';
12684d9c625SLionel Sambuc 	}
12784d9c625SLionel Sambuc 
12884d9c625SLionel Sambuc 	LIST_INSERT_HEAD(&sp->wp->ecq, ecp, q);
12984d9c625SLionel Sambuc 	return (0);
13084d9c625SLionel Sambuc alloc_err:
13184d9c625SLionel Sambuc 	free(ecp);
13284d9c625SLionel Sambuc 	return 1;
13384d9c625SLionel Sambuc }
134