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