1994-08-16 23:36:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
#ifndef lint
|
2013-08-11 09:44:58 +00:00
|
|
|
static const char sccsid[] = "$Id: put.c,v 10.19 04/07/11 17:00:24 zy Exp $";
|
1994-08-16 23:36:45 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <sys/time.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "common.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* put --
|
|
|
|
* Put text buffer contents into the file.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
|
|
|
* PUBLIC: int put __P((SCR *, CB *, CHAR_T *, MARK *, MARK *, int));
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
put(
|
|
|
|
SCR *sp,
|
|
|
|
CB *cbp,
|
|
|
|
CHAR_T *namep,
|
|
|
|
MARK *cp,
|
|
|
|
MARK *rp,
|
|
|
|
int append)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
CHAR_T name;
|
|
|
|
TEXT *ltp, *tp;
|
|
|
|
recno_t lno;
|
|
|
|
size_t blen, clen, len;
|
|
|
|
int rval;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *bp, *t;
|
|
|
|
CHAR_T *p;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
if (cbp == NULL)
|
|
|
|
if (namep == NULL) {
|
|
|
|
cbp = sp->gp->dcbp;
|
|
|
|
if (cbp == NULL) {
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq(sp, M_ERR,
|
|
|
|
"053|The default buffer is empty");
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = *namep;
|
|
|
|
CBNAME(sp, cbp, name);
|
|
|
|
if (cbp == NULL) {
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq(sp, M_ERR, "054|Buffer %s is empty",
|
|
|
|
KEY_NAME(sp, name));
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 09:44:58 +00:00
|
|
|
tp = TAILQ_FIRST(cbp->textq);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It's possible to do a put into an empty file, meaning that the cut
|
|
|
|
* buffer simply becomes the file. It's a special case so that we can
|
|
|
|
* ignore it in general.
|
|
|
|
*
|
|
|
|
* !!!
|
|
|
|
* Historically, pasting into a file with no lines in vi would preserve
|
|
|
|
* the single blank line. This is surely a result of the fact that the
|
|
|
|
* historic vi couldn't deal with a file that had no lines in it. This
|
|
|
|
* implementation treats that as a bug, and does not retain the blank
|
|
|
|
* line.
|
|
|
|
*
|
|
|
|
* Historical practice is that the cursor ends at the first character
|
|
|
|
* in the file.
|
|
|
|
*/
|
|
|
|
if (cp->lno == 1) {
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_last(sp, &lno))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
if (lno == 0) {
|
2013-08-11 09:44:58 +00:00
|
|
|
for (; tp != NULL;
|
|
|
|
++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_append(sp, 1, lno, tp->lb, tp->len))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
rp->lno = 1;
|
|
|
|
rp->cno = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a line mode buffer, append each new line into the file. */
|
|
|
|
if (F_ISSET(cbp, CB_LMODE)) {
|
|
|
|
lno = append ? cp->lno : cp->lno - 1;
|
|
|
|
rp->lno = lno + 1;
|
2013-08-11 09:44:58 +00:00
|
|
|
for (; tp != NULL;
|
|
|
|
++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_append(sp, 1, lno, tp->lb, tp->len))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
rp->cno = 0;
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)nonblank(sp, rp->lno, &rp->cno);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If buffer was cut in character mode, replace the current line with
|
|
|
|
* one built from the portion of the first line to the left of the
|
|
|
|
* split plus the first line in the CB. Append each intermediate line
|
|
|
|
* in the CB. Append a line built from the portion of the first line
|
|
|
|
* to the right of the split plus the last line in the CB.
|
|
|
|
*
|
|
|
|
* Get the first line.
|
|
|
|
*/
|
|
|
|
lno = cp->lno;
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_get(sp, lno, DBG_FATAL, &p, &len))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
GET_SPACE_RETW(sp, bp, blen, tp->len + len + 1);
|
1994-08-16 23:36:45 +00:00
|
|
|
t = bp;
|
|
|
|
|
|
|
|
/* Original line, left of the split. */
|
|
|
|
if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(bp, p, clen);
|
1994-08-16 23:36:45 +00:00
|
|
|
p += clen;
|
|
|
|
t += clen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First line from the CB. */
|
1996-11-01 06:45:43 +00:00
|
|
|
if (tp->len != 0) {
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(t, tp->lb, tp->len);
|
1996-11-01 06:45:43 +00:00
|
|
|
t += tp->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate length left in the original line. */
|
|
|
|
clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0));
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* In the historical 4BSD version of vi, character mode puts within
|
|
|
|
* a single line have two cursor behaviors: if the put is from the
|
|
|
|
* unnamed buffer, the cursor moves to the character inserted which
|
|
|
|
* appears last in the file. If the put is from a named buffer,
|
|
|
|
* the cursor moves to the character inserted which appears first
|
|
|
|
* in the file. In System III/V, it was changed at some point and
|
|
|
|
* the cursor always moves to the first character. In both versions
|
|
|
|
* of vi, character mode puts that cross line boundaries leave the
|
|
|
|
* cursor on the first character. Nvi implements the System III/V
|
|
|
|
* behavior, and expect POSIX.2 to do so as well.
|
|
|
|
*/
|
|
|
|
rp->lno = lno;
|
|
|
|
rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If no more lines in the CB, append the rest of the original
|
|
|
|
* line and quit. Otherwise, build the last line before doing
|
|
|
|
* the intermediate lines, because the line changes will lose
|
|
|
|
* the cached line.
|
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
if (TAILQ_NEXT(tp, q) == NULL) {
|
1994-08-16 23:36:45 +00:00
|
|
|
if (clen > 0) {
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(t, p, clen);
|
1994-08-16 23:36:45 +00:00
|
|
|
t += clen;
|
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_set(sp, lno, bp, t - bp))
|
|
|
|
goto err;
|
1994-08-16 23:36:45 +00:00
|
|
|
if (sp->rptlchange != lno) {
|
|
|
|
sp->rptlchange = lno;
|
|
|
|
++sp->rptlines[L_CHANGED];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Have to build both the first and last lines of the
|
|
|
|
* put before doing any sets or we'll lose the cached
|
|
|
|
* line. Build both the first and last lines in the
|
|
|
|
* same buffer, so we don't have to have another buffer
|
|
|
|
* floating around.
|
|
|
|
*
|
|
|
|
* Last part of original line; check for space, reset
|
|
|
|
* the pointer into the buffer.
|
|
|
|
*/
|
2013-08-11 09:44:58 +00:00
|
|
|
ltp = TAILQ_LAST(cbp->textq, _texth);
|
1994-08-16 23:36:45 +00:00
|
|
|
len = t - bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, ltp->len + clen);
|
1994-08-16 23:36:45 +00:00
|
|
|
t = bp + len;
|
|
|
|
|
|
|
|
/* Add in last part of the CB. */
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(t, ltp->lb, ltp->len);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (clen)
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(t + ltp->len, p, clen);
|
1994-08-16 23:36:45 +00:00
|
|
|
clen += ltp->len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now: bp points to the first character of the first
|
|
|
|
* line, t points to the last character of the last
|
|
|
|
* line, t - bp is the length of the first line, and
|
|
|
|
* clen is the length of the last. Just figured you'd
|
|
|
|
* want to know.
|
|
|
|
*
|
|
|
|
* Output the line replacing the original line.
|
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_set(sp, lno, bp, t - bp))
|
|
|
|
goto err;
|
1994-08-16 23:36:45 +00:00
|
|
|
if (sp->rptlchange != lno) {
|
|
|
|
sp->rptlchange = lno;
|
|
|
|
++sp->rptlines[L_CHANGED];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output any intermediate lines in the CB. */
|
2013-08-11 09:44:58 +00:00
|
|
|
for (tp = TAILQ_NEXT(tp, q); TAILQ_NEXT(tp, q) != NULL;
|
|
|
|
++lno, ++sp->rptlines[L_ADDED], tp = TAILQ_NEXT(tp, q))
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_append(sp, 1, lno, tp->lb, tp->len))
|
|
|
|
goto err;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_append(sp, 1, lno, t, clen))
|
|
|
|
goto err;
|
1994-08-16 23:36:45 +00:00
|
|
|
++sp->rptlines[L_ADDED];
|
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
rval = 0;
|
|
|
|
|
|
|
|
if (0)
|
|
|
|
err: rval = 1;
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
FREE_SPACEW(sp, bp, blen);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (rval);
|
|
|
|
}
|