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