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