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