GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/vi/build/../ex/ex_join.c Lines: 0 51 0.0 %
Date: 2017-11-07 Branches: 0 96 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ex_join.c,v 1.8 2016/01/06 22:28:52 millert 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_join -- :[line [,line]] j[oin][!] [count] [flags]
28
 *	Join lines.
29
 *
30
 * PUBLIC: int ex_join(SCR *, EXCMD *);
31
 */
32
int
33
ex_join(SCR *sp, EXCMD *cmdp)
34
{
35
	recno_t from, to;
36
	size_t blen, clen, len, tlen;
37
	int echar, extra, first;
38
	char *bp, *p, *tbp;
39
40
	NEEDFILE(sp, cmdp);
41
42
	from = cmdp->addr1.lno;
43
	to = cmdp->addr2.lno;
44
45
	/* Check for no lines to join. */
46
	if (!db_exist(sp, from + 1)) {
47
		msgq(sp, M_ERR, "No following lines to join");
48
		return (1);
49
	}
50
51
	GET_SPACE_RET(sp, bp, blen, 256);
52
53
	/*
54
	 * The count for the join command was off-by-one,
55
	 * historically, to other counts for other commands.
56
	 */
57
	if (FL_ISSET(cmdp->iflags, E_C_COUNT))
58
		++cmdp->addr2.lno;
59
60
	/*
61
	 * If only a single address specified, or, the same address
62
	 * specified twice, the from/two addresses will be the same.
63
	 */
64
	if (cmdp->addr1.lno == cmdp->addr2.lno)
65
		++cmdp->addr2.lno;
66
67
	clen = tlen = 0;
68
        for (first = 1,
69
	    from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
70
		/*
71
		 * Get next line.  Historic versions of vi allowed "10J" while
72
		 * less than 10 lines from the end-of-file, so we do too.
73
		 */
74
		if (db_get(sp, from, 0, &p, &len)) {
75
			cmdp->addr2.lno = from - 1;
76
			break;
77
		}
78
79
		/* Empty lines just go away. */
80
		if (len == 0)
81
			continue;
82
83
		/*
84
		 * Get more space if necessary.  Note, tlen isn't the length
85
		 * of the new line, it's roughly the amount of space needed.
86
		 * tbp - bp is the length of the new line.
87
		 */
88
		tlen += len + 2;
89
		ADD_SPACE_RET(sp, bp, blen, tlen);
90
		tbp = bp + clen;
91
92
		/*
93
		 * Historic practice:
94
		 *
95
		 * If force specified, join without modification.
96
		 * If the current line ends with whitespace, strip leading
97
		 *    whitespace from the joined line.
98
		 * If the next line starts with a ), do nothing.
99
		 * If the current line ends with ., insert two spaces.
100
		 * Else, insert one space.
101
		 *
102
		 * One change -- add ? and ! to the list of characters for
103
		 * which we insert two spaces.  I expect that POSIX 1003.2
104
		 * will require this as well.
105
		 *
106
		 * Echar is the last character in the last line joined.
107
		 */
108
		extra = 0;
109
		if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
110
			if (isblank(echar))
111
				for (; len && isblank(*p); --len, ++p);
112
			else if (p[0] != ')') {
113
				if (strchr(".?!", echar)) {
114
					*tbp++ = ' ';
115
					++clen;
116
					extra = 1;
117
				}
118
				*tbp++ = ' ';
119
				++clen;
120
				for (; len && isblank(*p); --len, ++p);
121
			}
122
		}
123
124
		if (len != 0) {
125
			memcpy(tbp, p, len);
126
			tbp += len;
127
			clen += len;
128
			echar = p[len - 1];
129
		} else
130
			echar = ' ';
131
132
		/*
133
		 * Historic practice for vi was to put the cursor at the first
134
		 * inserted whitespace character, if there was one, or the
135
		 * first character of the joined line, if there wasn't, or the
136
		 * last character of the line if joined to an empty line.  If
137
		 * a count was specified, the cursor was moved as described
138
		 * for the first line joined, ignoring subsequent lines.  If
139
		 * the join was a ':' command, the cursor was placed at the
140
		 * first non-blank character of the line unless the cursor was
141
		 * "attracted" to the end of line when the command was executed
142
		 * in which case it moved to the new end of line.  There are
143
		 * probably several more special cases, but frankly, my dear,
144
		 * I don't give a damn.  This implementation puts the cursor
145
		 * on the first inserted whitespace character, the first
146
		 * character of the joined line, or the last character of the
147
		 * line regardless.  Note, if the cursor isn't on the joined
148
		 * line (possible with : commands), it is reset to the starting
149
		 * line.
150
		 */
151
		if (first) {
152
			sp->cno = (tbp - bp) - (1 + extra);
153
			first = 0;
154
		} else
155
			sp->cno = (tbp - bp) - len - (1 + extra);
156
	}
157
	sp->lno = cmdp->addr1.lno;
158
159
	/* Delete the joined lines. */
160
        for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
161
		if (db_delete(sp, to))
162
			goto err;
163
164
	/* If the original line changed, reset it. */
165
	if (!first && db_set(sp, from, bp, tbp - bp)) {
166
err:		FREE_SPACE(sp, bp, blen);
167
		return (1);
168
	}
169
	FREE_SPACE(sp, bp, blen);
170
171
	sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
172
	return (0);
173
}