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