Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / vi / put.c
blobaebefa6fd73a153cea5e924beee5d918c3bb683c
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifndef lint
35 static char sccsid[] = "@(#)put.c 8.3 (Berkeley) 3/14/94";
36 #endif /* not lint */
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/time.h>
42 #include <bitstring.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
51 #include "compat.h"
52 #include <db.h>
53 #include <regex.h>
55 #include "vi.h"
58 * put --
59 * Put text buffer contents into the file.
61 * !!!
62 * Historically, pasting into a file with no lines in vi would preserve
63 * the single blank line. This is almost certainly a result of the fact
64 * that historic vi couldn't deal with a file that had no lines in it.
65 * This implementation treats that as a bug, and does not retain the blank
66 * line.
68 int
69 put(sp, ep, cbp, namep, cp, rp, append)
70 SCR *sp;
71 EXF *ep;
72 CB *cbp;
73 CHAR_T *namep;
74 MARK *cp, *rp;
75 int append;
77 CHAR_T name;
78 TEXT *ltp, *tp;
79 recno_t lno;
80 size_t blen, clen, len;
81 char *bp, *p, *t;
83 if (cbp == NULL)
84 if (namep == NULL) {
85 cbp = sp->gp->dcbp;
86 if (cbp == NULL) {
87 msgq(sp, M_ERR, "The default buffer is empty.");
88 return (1);
90 } else {
91 name = *namep;
92 CBNAME(sp, cbp, name);
93 if (cbp == NULL) {
94 msgq(sp, M_ERR,
95 "Buffer %s is empty.", charname(sp, name));
96 return (1);
99 tp = cbp->textq.cqh_first;
102 * It's possible to do a put into an empty file, meaning that the
103 * cut buffer simply becomes the file. It's a special case so
104 * that we can ignore it in general.
106 * Historical practice is that the cursor ends up on the first
107 * non-blank character of the first line inserted.
109 if (cp->lno == 1) {
110 if (file_lline(sp, ep, &lno))
111 return (1);
112 if (lno == 0) {
113 for (; tp != (void *)&cbp->textq;
114 ++lno, tp = tp->q.cqe_next)
115 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
116 return (1);
117 rp->lno = 1;
118 rp->cno = 0;
119 (void)nonblank(sp, ep, rp->lno, &rp->cno);
120 goto ret;
124 /* If a line mode buffer, append each new line into the file. */
125 if (F_ISSET(cbp, CB_LMODE)) {
126 lno = append ? cp->lno : cp->lno - 1;
127 rp->lno = lno + 1;
128 for (; tp != (void *)&cbp->textq; ++lno, tp = tp->q.cqe_next)
129 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
130 return (1);
131 rp->cno = 0;
132 (void)nonblank(sp, ep, rp->lno, &rp->cno);
133 goto ret;
137 * If buffer was cut in character mode, replace the current line with
138 * one built from the portion of the first line to the left of the
139 * split plus the first line in the CB. Append each intermediate line
140 * in the CB. Append a line built from the portion of the first line
141 * to the right of the split plus the last line in the CB.
143 * Get the first line.
145 lno = cp->lno;
146 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
147 GETLINE_ERR(sp, lno);
148 return (1);
151 GET_SPACE_RET(sp, bp, blen, tp->len + len + 1);
152 t = bp;
154 /* Original line, left of the split. */
155 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
156 memmove(bp, p, clen);
157 p += clen;
158 t += clen;
161 /* First line from the CB. */
162 memmove(t, tp->lb, tp->len);
163 t += tp->len;
165 /* Calculate length left in original line. */
166 clen = len ? len - cp->cno - (append ? 1 : 0) : 0;
169 * If no more lines in the CB, append the rest of the original
170 * line and quit. Otherwise, build the last line before doing
171 * the intermediate lines, because the line changes will lose
172 * the cached line.
174 if (tp->q.cqe_next == (void *)&cbp->textq) {
176 * Historical practice is that if a non-line mode put
177 * is inside a single line, the cursor ends up on the
178 * last character inserted.
180 rp->lno = lno;
181 rp->cno = (t - bp) - 1;
183 if (clen > 0) {
184 memmove(t, p, clen);
185 t += clen;
187 if (file_sline(sp, ep, lno, bp, t - bp))
188 goto mem;
189 } else {
191 * Have to build both the first and last lines of the
192 * put before doing any sets or we'll lose the cached
193 * line. Build both the first and last lines in the
194 * same buffer, so we don't have to have another buffer
195 * floating around.
197 * Last part of original line; check for space, reset
198 * the pointer into the buffer.
200 ltp = cbp->textq.cqh_last;
201 len = t - bp;
202 ADD_SPACE_RET(sp, bp, blen, ltp->len + clen);
203 t = bp + len;
205 /* Add in last part of the CB. */
206 memmove(t, ltp->lb, ltp->len);
207 if (clen)
208 memmove(t + ltp->len, p, clen);
209 clen += ltp->len;
212 * Now: bp points to the first character of the first
213 * line, t points to the last character of the last
214 * line, t - bp is the length of the first line, and
215 * clen is the length of the last. Just figured you'd
216 * want to know.
218 * Output the line replacing the original line.
220 if (file_sline(sp, ep, lno, bp, t - bp))
221 goto mem;
224 * Historical practice is that if a non-line mode put
225 * covers multiple lines, the cursor ends up on the
226 * first character inserted. (Of course.)
228 rp->lno = lno;
229 rp->cno = (t - bp) - 1;
231 /* Output any intermediate lines in the CB. */
232 for (tp = tp->q.cqe_next;
233 tp->q.cqe_next != (void *)&cbp->textq;
234 ++lno, tp = tp->q.cqe_next)
235 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
236 goto mem;
238 if (file_aline(sp, ep, 1, lno, t, clen)) {
239 mem: FREE_SPACE(sp, bp, blen);
240 return (1);
243 FREE_SPACE(sp, bp, blen);
245 /* Reporting... */
246 ret: sp->rptlines[L_PUT] += lno - cp->lno;
248 return (0);