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