Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / put.c
blob6aad56cc3ba969f6772e4527ce109c553e79480b
1 /* $NetBSD: put.c,v 1.1.1.2 2008/05/18 14:29:50 aymeric Exp $ */
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.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "common.h"
31 * put --
32 * Put text buffer contents into the file.
34 * PUBLIC: int put __P((SCR *, CB *, CHAR_T *, MARK *, MARK *, int));
36 int
37 put(SCR *sp, CB *cbp, CHAR_T *namep, MARK *cp, MARK *rp, int append)
39 CHAR_T name;
40 TEXT *ltp, *tp;
41 db_recno_t lno;
42 size_t blen, clen, len;
43 int rval;
44 CHAR_T *bp, *t;
45 CHAR_T *p;
47 if (cbp == NULL) {
48 if (namep == NULL) {
49 cbp = sp->wp->dcbp;
50 if (cbp == NULL) {
51 msgq(sp, M_ERR,
52 "053|The default buffer is empty");
53 return (1);
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);
65 tp = cbp->textq.cqh_first;
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.
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.
79 * Historical practice is that the cursor ends at the first character
80 * in the file.
82 if (cp->lno == 1) {
83 if (db_last(sp, &lno))
84 return (1);
85 if (lno == 0) {
86 for (; tp != (void *)&cbp->textq;
87 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
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);
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 != (void *)&cbp->textq;
101 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
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);
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.
116 * Get the first line.
118 lno = cp->lno;
119 if (db_get(sp, lno, DBG_FATAL, &p, &len))
120 return (1);
122 GET_SPACE_RETW(sp, bp, blen, tp->len + len + 1);
123 t = bp;
125 /* Original line, left of the split. */
126 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
127 MEMCPYW(bp, p, clen);
128 p += clen;
129 t += clen;
132 /* First line from the CB. */
133 if (tp->len != 0) {
134 MEMCPYW(t, tp->lb, tp->len);
135 t += tp->len;
138 /* Calculate length left in the original line. */
139 clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0));
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.
154 rp->lno = lno;
155 rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);
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.
163 if (tp->q.cqe_next == (void *)&cbp->textq) {
164 if (clen > 0) {
165 MEMCPYW(t, p, clen);
166 t += clen;
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];
174 } else {
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.
182 * Last part of original line; check for space, reset
183 * the pointer into the buffer.
185 ltp = cbp->textq.cqh_last;
186 len = t - bp;
187 ADD_SPACE_RETW(sp, bp, blen, ltp->len + clen);
188 t = bp + len;
190 /* Add in last part of the CB. */
191 MEMCPYW(t, ltp->lb, ltp->len);
192 if (clen)
193 MEMCPYW(t + ltp->len, p, clen);
194 clen += ltp->len;
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.
203 * Output the line replacing the original line.
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];
212 /* Output any intermediate lines in the CB. */
213 for (tp = tp->q.cqe_next;
214 tp->q.cqe_next != (void *)&cbp->textq;
215 ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next)
216 if (db_append(sp, 1, lno, tp->lb, tp->len))
217 goto err;
219 if (db_append(sp, 1, lno, t, clen))
220 goto err;
221 ++sp->rptlines[L_ADDED];
223 rval = 0;
225 if (0)
226 err: rval = 1;
228 FREE_SPACEW(sp, bp, blen);
229 return (rval);