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