xref: /openbsd-src/usr.bin/vi/common/put.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: put.c,v 1.12 2013/11/26 13:16:06 otto 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.h"
25 
26 /*
27  * put --
28  *	Put text buffer contents into the file.
29  *
30  * PUBLIC: int put(SCR *, CB *, CHAR_T *, MARK *, MARK *, int);
31  */
32 int
33 put(sp, cbp, namep, cp, rp, append)
34 	SCR *sp;
35 	CB *cbp;
36 	CHAR_T *namep;
37 	MARK *cp, *rp;
38 	int append;
39 {
40 	CHAR_T name;
41 	TEXT *ltp, *tp;
42 	recno_t lno;
43 	size_t blen, clen, len;
44 	int rval;
45 	char *bp, *p, *t;
46 
47 	if (cbp == NULL) {
48 		if (namep == NULL) {
49 			cbp = sp->gp->dcbp;
50 			if (cbp == NULL) {
51 				msgq(sp, M_ERR,
52 				    "053|The default buffer is empty");
53 				return (1);
54 			}
55 		} else {
56 			name = *namep;
57 			CBNAME(sp, cbp, name);
58 			if (cbp == NULL) {
59 				msgq(sp, M_ERR, "054|Buffer %s is empty",
60 				    KEY_NAME(sp, name));
61 				return (1);
62 			}
63 		}
64 	}
65 	tp = TAILQ_FIRST(&cbp->textq);
66 
67 	/*
68 	 * It's possible to do a put into an empty file, meaning that the cut
69 	 * buffer simply becomes the file.  It's a special case so that we can
70 	 * ignore it in general.
71 	 *
72 	 * !!!
73 	 * Historically, pasting into a file with no lines in vi would preserve
74 	 * the single blank line.  This is surely a result of the fact that the
75 	 * historic vi couldn't deal with a file that had no lines in it.  This
76 	 * implementation treats that as a bug, and does not retain the blank
77 	 * line.
78 	 *
79 	 * Historical practice is that the cursor ends at the first character
80 	 * in the file.
81 	 */
82 	if (cp->lno == 1) {
83 		if (db_last(sp, &lno))
84 			return (1);
85 		if (lno == 0) {
86 			for (; tp; ++lno, ++sp->rptlines[L_ADDED],
87 			    tp = TAILQ_NEXT(tp, q))
88 				if (db_append(sp, 1, lno, tp->lb, tp->len))
89 					return (1);
90 			rp->lno = 1;
91 			rp->cno = 0;
92 			return (0);
93 		}
94 	}
95 
96 	/* If a line mode buffer, append each new line into the file. */
97 	if (F_ISSET(cbp, CB_LMODE)) {
98 		lno = append ? cp->lno : cp->lno - 1;
99 		rp->lno = lno + 1;
100 		for (; tp;
101 		    ++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
102 			if (db_append(sp, 1, lno, tp->lb, tp->len))
103 				return (1);
104 		rp->cno = 0;
105 		(void)nonblank(sp, rp->lno, &rp->cno);
106 		return (0);
107 	}
108 
109 	/*
110 	 * If buffer was cut in character mode, replace the current line with
111 	 * one built from the portion of the first line to the left of the
112 	 * split plus the first line in the CB.  Append each intermediate line
113 	 * in the CB.  Append a line built from the portion of the first line
114 	 * to the right of the split plus the last line in the CB.
115 	 *
116 	 * Get the first line.
117 	 */
118 	lno = cp->lno;
119 	if (db_get(sp, lno, DBG_FATAL, &p, &len))
120 		return (1);
121 
122 	GET_SPACE_RET(sp, bp, blen, tp->len + len + 1);
123 	t = bp;
124 
125 	/* Original line, left of the split. */
126 	if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
127 		memcpy(bp, p, clen);
128 		p += clen;
129 		t += clen;
130 	}
131 
132 	/* First line from the CB. */
133 	if (tp->len != 0) {
134 		memcpy(t, tp->lb, tp->len);
135 		t += tp->len;
136 	}
137 
138 	/* Calculate length left in the original line. */
139 	clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0));
140 
141 	/*
142 	 * !!!
143 	 * In the historical 4BSD version of vi, character mode puts within
144 	 * a single line have two cursor behaviors: if the put is from the
145 	 * unnamed buffer, the cursor moves to the character inserted which
146 	 * appears last in the file.  If the put is from a named buffer,
147 	 * the cursor moves to the character inserted which appears first
148 	 * in the file.  In System III/V, it was changed at some point and
149 	 * the cursor always moves to the first character.  In both versions
150 	 * of vi, character mode puts that cross line boundaries leave the
151 	 * cursor on the first character.  Nvi implements the System III/V
152 	 * behavior, and expect POSIX.2 to do so as well.
153 	 */
154 	rp->lno = lno;
155 	rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);
156 
157 	/*
158 	 * If no more lines in the CB, append the rest of the original
159 	 * line and quit.  Otherwise, build the last line before doing
160 	 * the intermediate lines, because the line changes will lose
161 	 * the cached line.
162 	 */
163 	if (TAILQ_NEXT(tp, q) == NULL) {
164 		if (clen > 0) {
165 			memcpy(t, p, clen);
166 			t += clen;
167 		}
168 		if (db_set(sp, lno, bp, t - bp))
169 			goto err;
170 		if (sp->rptlchange != lno) {
171 			sp->rptlchange = lno;
172 			++sp->rptlines[L_CHANGED];
173 		}
174 	} else {
175 		/*
176 		 * Have to build both the first and last lines of the
177 		 * put before doing any sets or we'll lose the cached
178 		 * line.  Build both the first and last lines in the
179 		 * same buffer, so we don't have to have another buffer
180 		 * floating around.
181 		 *
182 		 * Last part of original line; check for space, reset
183 		 * the pointer into the buffer.
184 		 */
185 		ltp = TAILQ_LAST(&cbp->textq, _texth);
186 		len = t - bp;
187 		ADD_SPACE_RET(sp, bp, blen, ltp->len + clen);
188 		t = bp + len;
189 
190 		/* Add in last part of the CB. */
191 		memcpy(t, ltp->lb, ltp->len);
192 		if (clen)
193 			memcpy(t + ltp->len, p, clen);
194 		clen += ltp->len;
195 
196 		/*
197 		 * Now: bp points to the first character of the first
198 		 * line, t points to the last character of the last
199 		 * line, t - bp is the length of the first line, and
200 		 * clen is the length of the last.  Just figured you'd
201 		 * want to know.
202 		 *
203 		 * Output the line replacing the original line.
204 		 */
205 		if (db_set(sp, lno, bp, t - bp))
206 			goto err;
207 		if (sp->rptlchange != lno) {
208 			sp->rptlchange = lno;
209 			++sp->rptlines[L_CHANGED];
210 		}
211 
212 		/* Output any intermediate lines in the CB. */
213 		for (tp = TAILQ_NEXT(tp, q); TAILQ_NEXT(tp, q);
214 		    ++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
215 			if (db_append(sp, 1, lno, tp->lb, tp->len))
216 				goto err;
217 
218 		if (db_append(sp, 1, lno, t, clen))
219 			goto err;
220 		++sp->rptlines[L_ADDED];
221 	}
222 	rval = 0;
223 
224 	if (0)
225 err:		rval = 1;
226 
227 	FREE_SPACE(sp, bp, blen);
228 	return (rval);
229 }
230