xref: /freebsd/contrib/nvi/vi/v_at.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include "../common/common.h"
23 #include "vi.h"
24 
25 /*
26  * v_at -- @
27  *	Execute a buffer.
28  *
29  * PUBLIC: int v_at(SCR *, VICMD *);
30  */
31 int
32 v_at(SCR *sp, VICMD *vp)
33 {
34 	CB *cbp;
35 	CHAR_T name;
36 	TEXT *tp;
37 	size_t len;
38 	char nbuf[20];
39 	CHAR_T wbuf[20];
40 	CHAR_T *wp;
41 	size_t wlen;
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 	TAILQ_FOREACH_REVERSE(tp, cbp->textq, _texth, q) {
89 		if (((F_ISSET(cbp, CB_LMODE) ||
90 		    TAILQ_NEXT(tp, q) != NULL) &&
91 		    v_event_push(sp, NULL, L("\n"), 1, 0)) ||
92 		    v_event_push(sp, NULL, tp->lb, tp->len, 0))
93 			return (1);
94 	}
95 
96 	/*
97 	 * !!!
98 	 * If any count was supplied, it applies to the first command in the
99 	 * at buffer.
100 	 */
101 	if (F_ISSET(vp, VC_C1SET)) {
102 		len = snprintf(nbuf, sizeof(nbuf), "%lu", vp->count);
103 		CHAR2INT(sp, nbuf, len, wp, wlen);
104 		MEMCPY(wbuf, wp, wlen);
105 		if (v_event_push(sp, NULL, wp, wlen, 0))
106 			return (1);
107 	}
108 	return (0);
109 }
110