1 /* $OpenBSD: put.c,v 1.9 2007/05/14 12:32:29 pyr Exp $ */ 2 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. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #ifndef lint 15 static const char sccsid[] = "@(#)put.c 10.11 (Berkeley) 9/23/96"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/queue.h> 20 21 #include <bitstring.h> 22 #include <ctype.h> 23 #include <limits.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include "common.h" 29 30 /* 31 * put -- 32 * Put text buffer contents into the file. 33 * 34 * PUBLIC: int put(SCR *, CB *, CHAR_T *, MARK *, MARK *, int); 35 */ 36 int 37 put(sp, cbp, namep, cp, rp, append) 38 SCR *sp; 39 CB *cbp; 40 CHAR_T *namep; 41 MARK *cp, *rp; 42 int append; 43 { 44 CHAR_T name; 45 TEXT *ltp, *tp; 46 recno_t lno; 47 size_t blen, clen, len; 48 int rval; 49 char *bp, *p, *t; 50 51 if (cbp == NULL) { 52 if (namep == NULL) { 53 cbp = sp->gp->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 = CIRCLEQ_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 != (void *)&cbp->textq; 91 ++lno, ++sp->rptlines[L_ADDED], 92 tp = CIRCLEQ_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); 98 } 99 } 100 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 != CIRCLEQ_END(&cbp->textq); 106 ++lno, ++sp->rptlines[L_ADDED], tp = CIRCLEQ_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); 112 } 113 114 /* 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. 120 * 121 * Get the first line. 122 */ 123 lno = cp->lno; 124 if (db_get(sp, lno, DBG_FATAL, &p, &len)) 125 return (1); 126 127 GET_SPACE_RET(sp, bp, blen, tp->len + len + 1); 128 t = bp; 129 130 /* Original line, left of the split. */ 131 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) { 132 memcpy(bp, p, clen); 133 p += clen; 134 t += clen; 135 } 136 137 /* First line from the CB. */ 138 if (tp->len != 0) { 139 memcpy(t, tp->lb, tp->len); 140 t += tp->len; 141 } 142 143 /* Calculate length left in the original line. */ 144 clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0)); 145 146 /* 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. 158 */ 159 rp->lno = lno; 160 rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0); 161 162 /* 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. 167 */ 168 if (CIRCLEQ_NEXT(tp, q) == CIRCLEQ_END(&cbp->textq)) { 169 if (clen > 0) { 170 memcpy(t, p, clen); 171 t += clen; 172 } 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]; 178 } 179 } else { 180 /* 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. 186 * 187 * Last part of original line; check for space, reset 188 * the pointer into the buffer. 189 */ 190 ltp = CIRCLEQ_LAST(&cbp->textq); 191 len = t - bp; 192 ADD_SPACE_RET(sp, bp, blen, ltp->len + clen); 193 t = bp + len; 194 195 /* Add in last part of the CB. */ 196 memcpy(t, ltp->lb, ltp->len); 197 if (clen) 198 memcpy(t + ltp->len, p, clen); 199 clen += ltp->len; 200 201 /* 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. 207 * 208 * Output the line replacing the original line. 209 */ 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]; 215 } 216 217 /* Output any intermediate lines in the CB. */ 218 for (tp = CIRCLEQ_NEXT(tp, q); 219 CIRCLEQ_NEXT(tp, q) != CIRCLEQ_END(&cbp->textq); 220 ++lno, ++sp->rptlines[L_ADDED], tp = CIRCLEQ_NEXT(tp, q)) 221 if (db_append(sp, 1, lno, tp->lb, tp->len)) 222 goto err; 223 224 if (db_append(sp, 1, lno, t, clen)) 225 goto err; 226 ++sp->rptlines[L_ADDED]; 227 } 228 rval = 0; 229 230 if (0) 231 err: rval = 1; 232 233 FREE_SPACE(sp, bp, blen); 234 return (rval); 235 } 236