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

163 lines
3.6 KiB
C
Raw Normal View History

1998-01-09 11:03:16 +03:00
/* $NetBSD: delete.c,v 1.2 1998/01/09 08:06:35 perry Exp $ */
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[] = "@(#)delete.c 10.10 (Berkeley) 3/6/96";
1994-08-17 20:17:19 +04:00
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1996-05-20 06:01:36 +04:00
#include "common.h"
1994-08-17 20:17:19 +04:00
/*
* delete --
* Delete a range of text.
1996-05-20 06:01:36 +04:00
*
* PUBLIC: int delete __P((SCR *, MARK *, MARK *, int));
1994-08-17 20:17:19 +04:00
*/
int
1996-05-20 06:01:36 +04:00
delete(sp, fm, tm, lmode)
1994-08-17 20:17:19 +04:00
SCR *sp;
MARK *fm, *tm;
int lmode;
{
recno_t lno;
size_t blen, len, nlen, tlen;
char *bp, *p;
1996-05-20 06:01:36 +04:00
int eof, rval;
1994-08-17 20:17:19 +04:00
bp = NULL;
/* Case 1 -- delete in line mode. */
if (lmode) {
1996-05-20 06:01:36 +04:00
for (lno = tm->lno; lno >= fm->lno; --lno) {
if (db_delete(sp, lno))
1994-08-17 20:17:19 +04:00
return (1);
1996-05-20 06:01:36 +04:00
++sp->rptlines[L_DELETED];
if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
break;
}
goto done;
1994-08-17 20:17:19 +04:00
}
/*
* Case 2 -- delete to EOF. This is a special case because it's
* easier to pick it off than try and find it in the other cases.
*/
1996-05-20 06:01:36 +04:00
if (db_last(sp, &lno))
1994-08-17 20:17:19 +04:00
return (1);
if (tm->lno >= lno) {
if (tm->lno == lno) {
1996-05-20 06:01:36 +04:00
if (db_get(sp, lno, DBG_FATAL, &p, &len))
1994-08-17 20:17:19 +04:00
return (1);
eof = tm->cno >= len ? 1 : 0;
} else
eof = 1;
if (eof) {
for (lno = tm->lno; lno > fm->lno; --lno) {
1996-05-20 06:01:36 +04:00
if (db_delete(sp, lno))
1994-08-17 20:17:19 +04:00
return (1);
++sp->rptlines[L_DELETED];
1996-05-20 06:01:36 +04:00
if (lno %
INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
break;
1994-08-17 20:17:19 +04:00
}
1996-05-20 06:01:36 +04:00
if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
1994-08-17 20:17:19 +04:00
return (1);
GET_SPACE_RET(sp, bp, blen, fm->cno);
memmove(bp, p, fm->cno);
1996-05-20 06:01:36 +04:00
if (db_set(sp, fm->lno, bp, fm->cno))
1994-08-17 20:17:19 +04:00
return (1);
goto done;
}
}
/* Case 3 -- delete within a single line. */
if (tm->lno == fm->lno) {
1996-05-20 06:01:36 +04:00
if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
1994-08-17 20:17:19 +04:00
return (1);
GET_SPACE_RET(sp, bp, blen, len);
if (fm->cno != 0)
memmove(bp, p, fm->cno);
memmove(bp + fm->cno, p + (tm->cno + 1), len - (tm->cno + 1));
1996-05-20 06:01:36 +04:00
if (db_set(sp, fm->lno,
1994-08-17 20:17:19 +04:00
bp, len - ((tm->cno - fm->cno) + 1)))
goto err;
goto done;
}
/*
* Case 4 -- delete over multiple lines.
*
* Copy the start partial line into place.
*/
if ((tlen = fm->cno) != 0) {
1996-05-20 06:01:36 +04:00
if (db_get(sp, fm->lno, DBG_FATAL, &p, NULL))
1994-08-17 20:17:19 +04:00
return (1);
GET_SPACE_RET(sp, bp, blen, tlen + 256);
memmove(bp, p, tlen);
}
/* Copy the end partial line into place. */
1996-05-20 06:01:36 +04:00
if (db_get(sp, tm->lno, DBG_FATAL, &p, &len))
1994-08-17 20:17:19 +04:00
goto err;
if (len != 0 && tm->cno != len - 1) {
/*
* XXX
* We can overflow memory here, if the total length is greater
* than SIZE_T_MAX. The only portable way I've found to test
* is depending on the overflow being less than the value.
*/
nlen = (len - (tm->cno + 1)) + tlen;
if (tlen > nlen) {
1996-05-20 06:01:36 +04:00
msgq(sp, M_ERR, "002|Line length overflow");
1994-08-17 20:17:19 +04:00
goto err;
}
if (tlen == 0) {
GET_SPACE_RET(sp, bp, blen, nlen);
} else
ADD_SPACE_RET(sp, bp, blen, nlen);
memmove(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1));
tlen += len - (tm->cno + 1);
}
/* Set the current line. */
1996-05-20 06:01:36 +04:00
if (db_set(sp, fm->lno, bp, tlen))
1994-08-17 20:17:19 +04:00
goto err;
/* Delete the last and intermediate lines. */
1996-05-20 06:01:36 +04:00
for (lno = tm->lno; lno > fm->lno; --lno) {
if (db_delete(sp, lno))
1994-08-17 20:17:19 +04:00
goto err;
1996-05-20 06:01:36 +04:00
++sp->rptlines[L_DELETED];
if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
break;
}
1994-08-17 20:17:19 +04:00
1996-05-20 06:01:36 +04:00
done: rval = 0;
if (0)
err: rval = 1;
if (bp != NULL)
1994-08-17 20:17:19 +04:00
FREE_SPACE(sp, bp, blen);
1996-05-20 06:01:36 +04:00
return (rval);
1994-08-17 20:17:19 +04:00
}