NetBSD/usr.bin/vi/common/line.c

578 lines
12 KiB
C
Raw Normal View History

1994-08-17 20:17:19 +04:00
/*-
* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
1996-05-20 06:01:36 +04:00
* Copyright (c) 1992, 1993, 1994, 1995, 1996
* Keith Bostic. All rights reserved.
1994-08-17 20:17:19 +04:00
*
1996-05-20 06:01:36 +04:00
* See the LICENSE file for redistribution information.
1994-08-17 20:17:19 +04:00
*/
1996-05-20 06:01:36 +04:00
#include "config.h"
1994-08-17 20:17:19 +04:00
#ifndef lint
1996-05-20 06:01:36 +04:00
static const char sccsid[] = "@(#)line.c 10.20 (Berkeley) 4/27/96";
1994-08-17 20:17:19 +04:00
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
1996-05-20 06:01:36 +04:00
#include "common.h"
#include "../vi/vi.h"
1994-08-17 20:17:19 +04:00
1996-05-20 06:01:36 +04:00
static int scr_update __P((SCR *, recno_t, lnop_t, int));
1994-08-17 20:17:19 +04:00
1996-05-20 06:01:36 +04:00
/*
* db_eget --
* Front-end to db_get, special case handling for empty files.
*
* PUBLIC: int db_eget __P((SCR *, recno_t, char **, size_t *, int *));
*/
int
db_eget(sp, lno, pp, lenp, isemptyp)
SCR *sp;
recno_t lno; /* Line number. */
char **pp; /* Pointer store. */
size_t *lenp; /* Length store. */
int *isemptyp;
{
recno_t l1;
if (isemptyp != NULL)
*isemptyp = 0;
/* If the line exists, simply return it. */
if (!db_get(sp, lno, 0, pp, lenp))
return (0);
/*
* If the user asked for line 0 or line 1, i.e. the only possible
* line in an empty file, find the last line of the file; db_last
* fails loudly.
*/
if ((lno == 0 || lno == 1) && db_last(sp, &l1))
return (1);
/* If the file isn't empty, fail loudly. */
if (lno != 0 && lno != 1 || l1 != 0) {
db_err(sp, lno);
return (1);
}
if (isemptyp != NULL)
*isemptyp = 1;
return (1);
}
1994-08-17 20:17:19 +04:00
/*
1996-05-20 06:01:36 +04:00
* db_get --
* Look in the text buffers for a line, followed by the cache, followed
* by the database.
*
* PUBLIC: int db_get __P((SCR *, recno_t, u_int32_t, char **, size_t *));
1994-08-17 20:17:19 +04:00
*/
1996-05-20 06:01:36 +04:00
int
db_get(sp, lno, flags, pp, lenp)
1994-08-17 20:17:19 +04:00
SCR *sp;
recno_t lno; /* Line number. */
1996-05-20 06:01:36 +04:00
u_int32_t flags;
char **pp; /* Pointer store. */
1994-08-17 20:17:19 +04:00
size_t *lenp; /* Length store. */
{
1996-05-20 06:01:36 +04:00
DBT data, key;
EXF *ep;
1994-08-17 20:17:19 +04:00
TEXT *tp;
recno_t l1, l2;
/*
* The underlying recno stuff handles zero by returning NULL, but
1996-05-20 06:01:36 +04:00
* have to have an OOB condition for the look-aside into the input
1994-08-17 20:17:19 +04:00
* buffer anyway.
*/
if (lno == 0)
1996-05-20 06:01:36 +04:00
goto err1;
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
goto err3;
}
if (LF_ISSET(DBG_NOCACHE))
goto nocache;
1994-08-17 20:17:19 +04:00
/*
* Look-aside into the TEXT buffers and see if the line we want
* is there.
*/
1996-05-20 06:01:36 +04:00
if (F_ISSET(sp, SC_TINPUT)) {
l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
1994-08-17 20:17:19 +04:00
if (l1 <= lno && l2 >= lno) {
1996-05-20 06:01:36 +04:00
#if defined(DEBUG) && 0
TRACE(sp, "retrieve TEXT buffer line %lu\n", (u_long)lno);
#endif
for (tp = sp->tiq.cqh_first;
1994-08-17 20:17:19 +04:00
tp->lno != lno; tp = tp->q.cqe_next);
1996-05-20 06:01:36 +04:00
if (lenp != NULL)
1994-08-17 20:17:19 +04:00
*lenp = tp->len;
1996-05-20 06:01:36 +04:00
if (pp != NULL)
*pp = tp->lb;
return (0);
1994-08-17 20:17:19 +04:00
}
/*
* Adjust the line number for the number of lines used
* by the text input buffers.
*/
if (lno > l2)
lno -= l2 - l1;
}
1996-05-20 06:01:36 +04:00
/* Look-aside into the cache, and see if the line we want is there. */
1994-08-17 20:17:19 +04:00
if (lno == ep->c_lno) {
1996-05-20 06:01:36 +04:00
#if defined(DEBUG) && 0
TRACE(sp, "retrieve cached line %lu\n", (u_long)lno);
#endif
if (lenp != NULL)
1994-08-17 20:17:19 +04:00
*lenp = ep->c_len;
1996-05-20 06:01:36 +04:00
if (pp != NULL)
*pp = ep->c_lp;
return (0);
1994-08-17 20:17:19 +04:00
}
ep->c_lno = OOBLNO;
1996-05-20 06:01:36 +04:00
nocache:
1994-08-17 20:17:19 +04:00
/* Get the line from the underlying database. */
key.data = &lno;
key.size = sizeof(lno);
switch (ep->db->get(ep->db, &key, &data, 0)) {
case -1:
1996-05-20 06:01:36 +04:00
goto err2;
case 1:
err1: if (LF_ISSET(DBG_FATAL))
err2: db_err(sp, lno);
err3: if (lenp != NULL)
*lenp = 0;
if (pp != NULL)
*pp = NULL;
return (1);
1994-08-17 20:17:19 +04:00
}
1996-05-20 06:01:36 +04:00
/* Reset the cache. */
1994-08-17 20:17:19 +04:00
ep->c_lno = lno;
ep->c_len = data.size;
ep->c_lp = data.data;
1996-05-20 06:01:36 +04:00
#if defined(DEBUG) && 0
TRACE(sp, "retrieve DB line %lu\n", (u_long)lno);
#endif
if (lenp != NULL)
*lenp = data.size;
if (pp != NULL)
*pp = ep->c_lp;
return (0);
1994-08-17 20:17:19 +04:00
}
/*
1996-05-20 06:01:36 +04:00
* db_delete --
1994-08-17 20:17:19 +04:00
* Delete a line from the file.
1996-05-20 06:01:36 +04:00
*
* PUBLIC: int db_delete __P((SCR *, recno_t));
1994-08-17 20:17:19 +04:00
*/
int
1996-05-20 06:01:36 +04:00
db_delete(sp, lno)
1994-08-17 20:17:19 +04:00
SCR *sp;
recno_t lno;
{
DBT key;
1996-05-20 06:01:36 +04:00
EXF *ep;
1994-08-17 20:17:19 +04:00
#if defined(DEBUG) && 0
1996-05-20 06:01:36 +04:00
TRACE(sp, "delete line %lu\n", (u_long)lno);
1994-08-17 20:17:19 +04:00
#endif
1996-05-20 06:01:36 +04:00
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
return (1);
}
/* Update marks, @ and global commands. */
if (mark_insdel(sp, LINE_DELETE, lno))
return (1);
if (ex_g_insdel(sp, LINE_DELETE, lno))
return (1);
1994-08-17 20:17:19 +04:00
/* Log change. */
1996-05-20 06:01:36 +04:00
log_line(sp, lno, LOG_LINE_DELETE);
1994-08-17 20:17:19 +04:00
/* Update file. */
key.data = &lno;
key.size = sizeof(lno);
1996-05-20 06:01:36 +04:00
SIGBLOCK;
1994-08-17 20:17:19 +04:00
if (ep->db->del(ep->db, &key, 0) == 1) {
1996-05-20 06:01:36 +04:00
msgq(sp, M_SYSERR,
"003|unable to delete line %lu", (u_long)lno);
1994-08-17 20:17:19 +04:00
return (1);
}
1996-05-20 06:01:36 +04:00
SIGUNBLOCK;
1994-08-17 20:17:19 +04:00
/* Flush the cache, update line count, before screen update. */
if (lno <= ep->c_lno)
ep->c_lno = OOBLNO;
if (ep->c_nlines != OOBLNO)
--ep->c_nlines;
1996-05-20 06:01:36 +04:00
/* File now modified. */
1994-08-17 20:17:19 +04:00
if (F_ISSET(ep, F_FIRSTMODIFY))
1996-05-20 06:01:36 +04:00
(void)rcv_init(sp);
1994-08-17 20:17:19 +04:00
F_SET(ep, F_MODIFIED);
/* Update screen. */
1996-05-20 06:01:36 +04:00
return (scr_update(sp, lno, LINE_DELETE, 1));
1994-08-17 20:17:19 +04:00
}
/*
1996-05-20 06:01:36 +04:00
* db_append --
1994-08-17 20:17:19 +04:00
* Append a line into the file.
1996-05-20 06:01:36 +04:00
*
* PUBLIC: int db_append __P((SCR *, int, recno_t, char *, size_t));
1994-08-17 20:17:19 +04:00
*/
int
1996-05-20 06:01:36 +04:00
db_append(sp, update, lno, p, len)
1994-08-17 20:17:19 +04:00
SCR *sp;
int update;
recno_t lno;
char *p;
size_t len;
{
DBT data, key;
1996-05-20 06:01:36 +04:00
EXF *ep;
int rval;
1994-08-17 20:17:19 +04:00
#if defined(DEBUG) && 0
TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
#endif
1996-05-20 06:01:36 +04:00
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
return (1);
1994-08-17 20:17:19 +04:00
}
1996-05-20 06:01:36 +04:00
1994-08-17 20:17:19 +04:00
/* Update file. */
key.data = &lno;
key.size = sizeof(lno);
data.data = p;
data.size = len;
1996-05-20 06:01:36 +04:00
SIGBLOCK;
1994-08-17 20:17:19 +04:00
if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
1996-05-20 06:01:36 +04:00
msgq(sp, M_SYSERR,
"004|unable to append to line %lu", (u_long)lno);
1994-08-17 20:17:19 +04:00
return (1);
}
1996-05-20 06:01:36 +04:00
SIGUNBLOCK;
1994-08-17 20:17:19 +04:00
/* Flush the cache, update line count, before screen update. */
if (lno < ep->c_lno)
ep->c_lno = OOBLNO;
if (ep->c_nlines != OOBLNO)
++ep->c_nlines;
/* File now dirty. */
if (F_ISSET(ep, F_FIRSTMODIFY))
1996-05-20 06:01:36 +04:00
(void)rcv_init(sp);
1994-08-17 20:17:19 +04:00
F_SET(ep, F_MODIFIED);
/* Log change. */
1996-05-20 06:01:36 +04:00
log_line(sp, lno + 1, LOG_LINE_APPEND);
1994-08-17 20:17:19 +04:00
1996-05-20 06:01:36 +04:00
/* Update marks, @ and global commands. */
rval = 0;
if (mark_insdel(sp, LINE_INSERT, lno + 1))
rval = 1;
if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
rval = 1;
1994-08-17 20:17:19 +04:00
/*
* Update screen.
*
* XXX
* Nasty hack. If multiple lines are input by the user, they aren't
* committed until an <ESC> is entered. The problem is the screen was
* updated/scrolled as each line was entered. So, when this routine
* is called to copy the new lines from the cut buffer into the file,
* it has to know not to update the screen again.
*/
1996-05-20 06:01:36 +04:00
return (scr_update(sp, lno, LINE_APPEND, update) || rval);
1994-08-17 20:17:19 +04:00
}
/*
1996-05-20 06:01:36 +04:00
* db_insert --
1994-08-17 20:17:19 +04:00
* Insert a line into the file.
1996-05-20 06:01:36 +04:00
*
* PUBLIC: int db_insert __P((SCR *, recno_t, char *, size_t));
1994-08-17 20:17:19 +04:00
*/
int
1996-05-20 06:01:36 +04:00
db_insert(sp, lno, p, len)
1994-08-17 20:17:19 +04:00
SCR *sp;
recno_t lno;
char *p;
size_t len;
{
DBT data, key;
1996-05-20 06:01:36 +04:00
EXF *ep;
int rval;
1994-08-17 20:17:19 +04:00
#if defined(DEBUG) && 0
1996-05-20 06:01:36 +04:00
TRACE(sp, "insert before %lu: len %lu {%.*s}\n",
(u_long)lno, (u_long)len, MIN(len, 20), p);
1994-08-17 20:17:19 +04:00
#endif
1996-05-20 06:01:36 +04:00
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
return (1);
1994-08-17 20:17:19 +04:00
}
1996-05-20 06:01:36 +04:00
1994-08-17 20:17:19 +04:00
/* Update file. */
key.data = &lno;
key.size = sizeof(lno);
data.data = p;
data.size = len;
1996-05-20 06:01:36 +04:00
SIGBLOCK;
1994-08-17 20:17:19 +04:00
if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
1996-05-20 06:01:36 +04:00
msgq(sp, M_SYSERR,
"005|unable to insert at line %lu", (u_long)lno);
1994-08-17 20:17:19 +04:00
return (1);
}
1996-05-20 06:01:36 +04:00
SIGUNBLOCK;
1994-08-17 20:17:19 +04:00
/* Flush the cache, update line count, before screen update. */
if (lno >= ep->c_lno)
ep->c_lno = OOBLNO;
if (ep->c_nlines != OOBLNO)
++ep->c_nlines;
/* File now dirty. */
if (F_ISSET(ep, F_FIRSTMODIFY))
1996-05-20 06:01:36 +04:00
(void)rcv_init(sp);
1994-08-17 20:17:19 +04:00
F_SET(ep, F_MODIFIED);
/* Log change. */
1996-05-20 06:01:36 +04:00
log_line(sp, lno, LOG_LINE_INSERT);
1994-08-17 20:17:19 +04:00
1996-05-20 06:01:36 +04:00
/* Update marks, @ and global commands. */
rval = 0;
if (mark_insdel(sp, LINE_INSERT, lno))
rval = 1;
if (ex_g_insdel(sp, LINE_INSERT, lno))
rval = 1;
1994-08-17 20:17:19 +04:00
/* Update screen. */
1996-05-20 06:01:36 +04:00
return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
1994-08-17 20:17:19 +04:00
}
/*
1996-05-20 06:01:36 +04:00
* db_set --
1994-08-17 20:17:19 +04:00
* Store a line in the file.
1996-05-20 06:01:36 +04:00
*
* PUBLIC: int db_set __P((SCR *, recno_t, char *, size_t));
1994-08-17 20:17:19 +04:00
*/
int
1996-05-20 06:01:36 +04:00
db_set(sp, lno, p, len)
1994-08-17 20:17:19 +04:00
SCR *sp;
recno_t lno;
char *p;
size_t len;
{
DBT data, key;
1996-05-20 06:01:36 +04:00
EXF *ep;
1994-08-17 20:17:19 +04:00
#if defined(DEBUG) && 0
1996-05-20 06:01:36 +04:00
TRACE(sp, "replace line %lu: len %lu {%.*s}\n",
(u_long)lno, (u_long)len, MIN(len, 20), p);
1994-08-17 20:17:19 +04:00
#endif
1996-05-20 06:01:36 +04:00
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
return (1);
}
1994-08-17 20:17:19 +04:00
/* Log before change. */
1996-05-20 06:01:36 +04:00
log_line(sp, lno, LOG_LINE_RESET_B);
1994-08-17 20:17:19 +04:00
/* Update file. */
key.data = &lno;
key.size = sizeof(lno);
data.data = p;
data.size = len;
1996-05-20 06:01:36 +04:00
SIGBLOCK;
1994-08-17 20:17:19 +04:00
if (ep->db->put(ep->db, &key, &data, 0) == -1) {
1996-05-20 06:01:36 +04:00
msgq(sp, M_SYSERR,
"006|unable to store line %lu", (u_long)lno);
1994-08-17 20:17:19 +04:00
return (1);
}
1996-05-20 06:01:36 +04:00
SIGUNBLOCK;
1994-08-17 20:17:19 +04:00
/* Flush the cache, before logging or screen update. */
if (lno == ep->c_lno)
ep->c_lno = OOBLNO;
/* File now dirty. */
if (F_ISSET(ep, F_FIRSTMODIFY))
1996-05-20 06:01:36 +04:00
(void)rcv_init(sp);
1994-08-17 20:17:19 +04:00
F_SET(ep, F_MODIFIED);
/* Log after change. */
1996-05-20 06:01:36 +04:00
log_line(sp, lno, LOG_LINE_RESET_F);
1994-08-17 20:17:19 +04:00
/* Update screen. */
1996-05-20 06:01:36 +04:00
return (scr_update(sp, lno, LINE_RESET, 1));
1994-08-17 20:17:19 +04:00
}
/*
1996-05-20 06:01:36 +04:00
* db_exist --
* Return if a line exists.
*
* PUBLIC: int db_exist __P((SCR *, recno_t));
1994-08-17 20:17:19 +04:00
*/
int
1996-05-20 06:01:36 +04:00
db_exist(sp, lno)
1994-08-17 20:17:19 +04:00
SCR *sp;
1996-05-20 06:01:36 +04:00
recno_t lno;
{
1994-08-17 20:17:19 +04:00
EXF *ep;
1996-05-20 06:01:36 +04:00
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
return (1);
}
if (lno == OOBLNO)
return (0);
/*
* Check the last-line number cache. Adjust the cached line
* number for the lines used by the text input buffers.
*/
if (ep->c_nlines != OOBLNO)
return (lno <= (F_ISSET(sp, SC_TINPUT) ?
ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
/* Go get the line. */
return (!db_get(sp, lno, 0, NULL, NULL));
}
/*
* db_last --
* Return the number of lines in the file.
*
* PUBLIC: int db_last __P((SCR *, recno_t *));
*/
int
db_last(sp, lnop)
SCR *sp;
1994-08-17 20:17:19 +04:00
recno_t *lnop;
{
DBT data, key;
1996-05-20 06:01:36 +04:00
EXF *ep;
1994-08-17 20:17:19 +04:00
recno_t lno;
1996-05-20 06:01:36 +04:00
/* Check for no underlying file. */
if ((ep = sp->ep) == NULL) {
ex_emsg(sp, NULL, EXM_NOFILEYET);
return (1);
}
/*
* Check the last-line number cache. Adjust the cached line
* number for the lines used by the text input buffers.
*/
1994-08-17 20:17:19 +04:00
if (ep->c_nlines != OOBLNO) {
1996-05-20 06:01:36 +04:00
*lnop = ep->c_nlines;
if (F_ISSET(sp, SC_TINPUT))
*lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
((TEXT *)sp->tiq.cqh_first)->lno;
1994-08-17 20:17:19 +04:00
return (0);
}
key.data = &lno;
key.size = sizeof(lno);
switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
case -1:
1996-05-20 06:01:36 +04:00
msgq(sp, M_SYSERR, "007|unable to get last line");
1994-08-17 20:17:19 +04:00
*lnop = 0;
return (1);
case 1:
*lnop = 0;
return (0);
default:
break;
}
/* Fill the cache. */
memmove(&lno, key.data, sizeof(lno));
ep->c_nlines = ep->c_lno = lno;
ep->c_len = data.size;
ep->c_lp = data.data;
/* Return the value. */
1996-05-20 06:01:36 +04:00
*lnop = (F_ISSET(sp, SC_TINPUT) &&
((TEXT *)sp->tiq.cqh_last)->lno > lno ?
((TEXT *)sp->tiq.cqh_last)->lno : lno);
1994-08-17 20:17:19 +04:00
return (0);
}
1996-05-20 06:01:36 +04:00
/*
* db_err --
* Report a line error.
*
* PUBLIC: void db_err __P((SCR *, recno_t));
*/
void
db_err(sp, lno)
SCR *sp;
recno_t lno;
{
msgq(sp, M_ERR,
"008|Error: unable to retrieve line %lu", (u_long)lno);
}
1994-08-17 20:17:19 +04:00
/*
* scr_update --
* Update all of the screens that are backed by the file that
* just changed.
*/
1996-05-20 06:01:36 +04:00
static int
scr_update(sp, lno, op, current)
1994-08-17 20:17:19 +04:00
SCR *sp;
recno_t lno;
1996-05-20 06:01:36 +04:00
lnop_t op;
1994-08-17 20:17:19 +04:00
int current;
{
1996-05-20 06:01:36 +04:00
EXF *ep;
1994-08-17 20:17:19 +04:00
SCR *tsp;
1996-05-20 06:01:36 +04:00
if (F_ISSET(sp, SC_EX))
return (0);
ep = sp->ep;
1994-08-17 20:17:19 +04:00
if (ep->refcnt != 1)
for (tsp = sp->gp->dq.cqh_first;
tsp != (void *)&sp->gp->dq; tsp = tsp->q.cqe_next)
if (sp != tsp && tsp->ep == ep)
1996-05-20 06:01:36 +04:00
if (vs_change(tsp, lno, op))
return (1);
return (current ? vs_change(sp, lno, op) : 0);
1994-08-17 20:17:19 +04:00
}