xref: /freebsd/contrib/nvi/ex/ex_at.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_at.c,v 10.16 2001/06/25 15:19:14 skimo Exp $";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "../common/common.h"
28 
29 /*
30  * ex_at -- :@[@ | buffer]
31  *	    :*[* | buffer]
32  *
33  *	Execute the contents of the buffer.
34  *
35  * PUBLIC: int ex_at(SCR *, EXCMD *);
36  */
37 int
38 ex_at(SCR *sp, EXCMD *cmdp)
39 {
40 	CB *cbp;
41 	CHAR_T name;
42 	EXCMD *ecp;
43 	RANGE *rp;
44 	TEXT *tp;
45 	size_t len = 0;
46 	CHAR_T *p;
47 
48 	/*
49 	 * !!!
50 	 * Historically, [@*]<carriage-return> and [@*][@*] executed the most
51 	 * recently executed buffer in ex mode.
52 	 */
53 	name = FL_ISSET(cmdp->iflags, E_C_BUFFER) ? cmdp->buffer : '@';
54 	if (name == '@' || name == '*') {
55 		if (!F_ISSET(sp, SC_AT_SET)) {
56 			ex_emsg(sp, NULL, EXM_NOPREVBUF);
57 			return (1);
58 		}
59 		name = sp->at_lbuf;
60 	}
61 	sp->at_lbuf = name;
62 	F_SET(sp, SC_AT_SET);
63 
64 	CBNAME(sp, cbp, name);
65 	if (cbp == NULL) {
66 		ex_emsg(sp, KEY_NAME(sp, name), EXM_EMPTYBUF);
67 		return (1);
68 	}
69 
70 	/*
71 	 * !!!
72 	 * Historically the @ command took a range of lines, and the @ buffer
73 	 * was executed once per line.  The historic vi could be trashed by
74 	 * this because it didn't notice if the underlying file changed, or,
75 	 * for that matter, if there were no more lines on which to operate.
76 	 * For example, take a 10 line file, load "%delete" into a buffer,
77 	 * and enter :8,10@<buffer>.
78 	 *
79 	 * The solution is a bit tricky.  If the user specifies a range, take
80 	 * the same approach as for global commands, and discard the command
81 	 * if exit or switch to a new file/screen.  If the user doesn't specify
82 	 * the  range, continue to execute after a file/screen switch, which
83 	 * means @ buffers are still useful in a multi-screen environment.
84 	 */
85 	CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
86 	TAILQ_INIT(ecp->rq);
87 	CALLOC_RET(sp, rp, RANGE *, 1, sizeof(RANGE));
88 	rp->start = cmdp->addr1.lno;
89 	if (F_ISSET(cmdp, E_ADDR_DEF)) {
90 		rp->stop = rp->start;
91 		FL_SET(ecp->agv_flags, AGV_AT_NORANGE);
92 	} else {
93 		rp->stop = cmdp->addr2.lno;
94 		FL_SET(ecp->agv_flags, AGV_AT);
95 	}
96 	TAILQ_INSERT_HEAD(ecp->rq, rp, q);
97 
98 	/*
99 	 * Buffers executed in ex mode or from the colon command line in vi
100 	 * were ex commands.  We can't push it on the terminal queue, since
101 	 * it has to be executed immediately, and we may be in the middle of
102 	 * an ex command already.  Push the command on the ex command stack.
103 	 * Build two copies of the command.  We need two copies because the
104 	 * ex parser may step on the command string when it's parsing it.
105 	 */
106 	TAILQ_FOREACH_REVERSE(tp, cbp->textq, _texth, q)
107 		len += tp->len + 1;
108 
109 	MALLOC_RET(sp, ecp->cp, CHAR_T *, len * 2 * sizeof(CHAR_T));
110 	ecp->o_cp = ecp->cp;
111 	ecp->o_clen = len;
112 	ecp->cp[len] = '\0';
113 
114 	/* Copy the buffer into the command space. */
115 	p = ecp->cp + len;
116 	TAILQ_FOREACH_REVERSE(tp, cbp->textq, _texth, q) {
117 		MEMCPY(p, tp->lb, tp->len);
118 		p += tp->len;
119 		*p++ = '\n';
120 	}
121 
122 	SLIST_INSERT_HEAD(sp->gp->ecq, ecp, q);
123 	return (0);
124 }
125