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