1 /* $NetBSD: v_at.c,v 1.4 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: v_at.c,v 10.11 2001/06/25 15:19:30 skimo Exp (Berkeley) Date: 2001/06/25 15:19:30 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: v_at.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
20 #endif
21
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 #include <sys/time.h>
25
26 #include <bitstring.h>
27 #include <ctype.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "../common/common.h"
33 #include "vi.h"
34
35 /*
36 * v_at -- @
37 * Execute a buffer.
38 *
39 * PUBLIC: int v_at __P((SCR *, VICMD *));
40 */
41 int
v_at(SCR * sp,VICMD * vp)42 v_at(SCR *sp, VICMD *vp)
43 {
44 CB *cbp;
45 ARG_CHAR_T name;
46 TEXT *tp;
47 size_t len;
48 char nbuf[20];
49 CHAR_T wbuf[20];
50 const CHAR_T *wp;
51 size_t wlen;
52
53 /*
54 * !!!
55 * Historically, [@*]<carriage-return> and [@*][@*] executed the most
56 * recently executed buffer in ex mode. In vi mode, only @@ repeated
57 * the last buffer. We change historic practice and make @* work from
58 * vi mode as well, it's simpler and more consistent.
59 *
60 * My intent is that *[buffer] will, in the future, pass the buffer to
61 * whatever interpreter is loaded.
62 */
63 name = F_ISSET(vp, VC_BUFFER) ? vp->buffer : '@';
64 if (name == '@' || name == '*') {
65 if (!F_ISSET(sp, SC_AT_SET)) {
66 ex_emsg(sp, NULL, EXM_NOPREVBUF);
67 return (1);
68 }
69 name = sp->at_lbuf;
70 }
71 F_SET(sp, SC_AT_SET);
72
73 CBNAME(sp, cbp, name);
74 if (cbp == NULL) {
75 ex_emsg(sp, (char *)KEY_NAME(sp, name), EXM_EMPTYBUF);
76 return (1);
77 }
78
79 /* Save for reuse. */
80 sp->at_lbuf = name;
81
82 /*
83 * The buffer is executed in vi mode, while in vi mode, so simply
84 * push it onto the terminal queue and continue.
85 *
86 * !!!
87 * Historic practice is that if the buffer was cut in line mode,
88 * <newlines> were appended to each line as it was pushed onto
89 * the stack. If the buffer was cut in character mode, <newlines>
90 * were appended to all lines but the last one.
91 *
92 * XXX
93 * Historic practice is that execution of an @ buffer could be
94 * undone by a single 'u' command, i.e. the changes were grouped
95 * together. We don't get this right; I'm waiting for the new DB
96 * logging code to be available.
97 */
98 TAILQ_FOREACH_REVERSE(tp, &cbp->textq, _texth, q) {
99 static CHAR_T nl[] = { '\n', 0 };
100 if (((F_ISSET(cbp, CB_LMODE) ||
101 TAILQ_NEXT(tp, q) != NULL) &&
102 v_event_push(sp, NULL, nl, 1, 0)) ||
103 v_event_push(sp, NULL, tp->lb, tp->len, 0))
104 return (1);
105 }
106
107 /*
108 * !!!
109 * If any count was supplied, it applies to the first command in the
110 * at buffer.
111 */
112 if (F_ISSET(vp, VC_C1SET)) {
113 len = snprintf(nbuf, sizeof(nbuf), "%lu", vp->count);
114 CHAR2INT(sp, nbuf, len, wp, wlen);
115 MEMCPYW(wbuf, wp, wlen);
116 if (v_event_push(sp, NULL, wp, wlen, 0))
117 return (1);
118 }
119 return (0);
120 }
121