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