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