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