GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/vi/build/../ex/ex_at.c Lines: 0 34 0.0 %
Date: 2017-11-13 Branches: 0 34 0.0 %

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