xref: /minix/external/bsd/nvi/dist/common/delete.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: delete.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: delete.c,v 10.17 2001/06/25 15:19:09 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:09 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: delete.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <errno.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <stdlib.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc #include "common.h"
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * del --
3684d9c625SLionel Sambuc  *	Delete a range of text.
3784d9c625SLionel Sambuc  *
3884d9c625SLionel Sambuc  * PUBLIC: int del __P((SCR *, MARK *, MARK *, int));
3984d9c625SLionel Sambuc  */
4084d9c625SLionel Sambuc int
del(SCR * sp,MARK * fm,MARK * tm,int lmode)4184d9c625SLionel Sambuc del(SCR *sp, MARK *fm, MARK *tm, int lmode)
4284d9c625SLionel Sambuc {
4384d9c625SLionel Sambuc 	db_recno_t lno;
4484d9c625SLionel Sambuc 	size_t blen, len, nlen, tlen;
4584d9c625SLionel Sambuc 	CHAR_T *bp, *p;
4684d9c625SLionel Sambuc 	int eof, rval;
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc 	bp = NULL;
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc 	/* Case 1 -- delete in line mode. */
5184d9c625SLionel Sambuc 	if (lmode) {
5284d9c625SLionel Sambuc 		for (lno = tm->lno; lno >= fm->lno; --lno) {
5384d9c625SLionel Sambuc 			if (db_delete(sp, lno))
5484d9c625SLionel Sambuc 				return (1);
5584d9c625SLionel Sambuc 			++sp->rptlines[L_DELETED];
5684d9c625SLionel Sambuc 			if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
5784d9c625SLionel Sambuc 				break;
5884d9c625SLionel Sambuc 		}
5984d9c625SLionel Sambuc 		goto done;
6084d9c625SLionel Sambuc 	}
6184d9c625SLionel Sambuc 
6284d9c625SLionel Sambuc 	/*
6384d9c625SLionel Sambuc 	 * Case 2 -- delete to EOF.  This is a special case because it's
6484d9c625SLionel Sambuc 	 * easier to pick it off than try and find it in the other cases.
6584d9c625SLionel Sambuc  	 */
6684d9c625SLionel Sambuc 	if (db_last(sp, &lno))
6784d9c625SLionel Sambuc 		return (1);
6884d9c625SLionel Sambuc 	if (tm->lno >= lno) {
6984d9c625SLionel Sambuc 		if (tm->lno == lno) {
7084d9c625SLionel Sambuc 			if (db_get(sp, lno, DBG_FATAL, &p, &len))
7184d9c625SLionel Sambuc 				return (1);
7284d9c625SLionel Sambuc 			eof = tm->cno != ENTIRE_LINE && tm->cno >= len ? 1 : 0;
7384d9c625SLionel Sambuc 		} else
7484d9c625SLionel Sambuc 			eof = 1;
7584d9c625SLionel Sambuc 		if (eof) {
7684d9c625SLionel Sambuc 			for (lno = tm->lno; lno > fm->lno; --lno) {
7784d9c625SLionel Sambuc 				if (db_delete(sp, lno))
7884d9c625SLionel Sambuc 					return (1);
7984d9c625SLionel Sambuc 				++sp->rptlines[L_DELETED];
8084d9c625SLionel Sambuc 				if (lno %
8184d9c625SLionel Sambuc 				    INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
8284d9c625SLionel Sambuc 					break;
8384d9c625SLionel Sambuc 			}
8484d9c625SLionel Sambuc 			if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
8584d9c625SLionel Sambuc 				return (1);
8684d9c625SLionel Sambuc 			GET_SPACE_RETW(sp, bp, blen, fm->cno);
8784d9c625SLionel Sambuc 			MEMCPYW(bp, p, fm->cno);
8884d9c625SLionel Sambuc 			if (db_set(sp, fm->lno, bp, fm->cno))
8984d9c625SLionel Sambuc 				return (1);
9084d9c625SLionel Sambuc 			goto done;
9184d9c625SLionel Sambuc 		}
9284d9c625SLionel Sambuc 	}
9384d9c625SLionel Sambuc 
9484d9c625SLionel Sambuc 	/* Case 3 -- delete within a single line. */
9584d9c625SLionel Sambuc 	if (tm->lno == fm->lno) {
9684d9c625SLionel Sambuc 		if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
9784d9c625SLionel Sambuc 			return (1);
9884d9c625SLionel Sambuc 		GET_SPACE_RETW(sp, bp, blen, len);
9984d9c625SLionel Sambuc 		if (fm->cno != 0)
10084d9c625SLionel Sambuc 			MEMCPYW(bp, p, fm->cno);
10184d9c625SLionel Sambuc 		MEMCPYW(bp + fm->cno, p + (tm->cno + 1),
10284d9c625SLionel Sambuc 			len - (tm->cno + 1));
10384d9c625SLionel Sambuc 		if (db_set(sp, fm->lno,
10484d9c625SLionel Sambuc 		    bp, len - ((tm->cno - fm->cno) + 1)))
10584d9c625SLionel Sambuc 			goto err;
10684d9c625SLionel Sambuc 		goto done;
10784d9c625SLionel Sambuc 	}
10884d9c625SLionel Sambuc 
10984d9c625SLionel Sambuc 	/*
11084d9c625SLionel Sambuc 	 * Case 4 -- delete over multiple lines.
11184d9c625SLionel Sambuc 	 *
11284d9c625SLionel Sambuc 	 * Copy the start partial line into place.
11384d9c625SLionel Sambuc 	 */
11484d9c625SLionel Sambuc 	if ((tlen = fm->cno) != 0) {
11584d9c625SLionel Sambuc 		if (db_get(sp, fm->lno, DBG_FATAL, &p, NULL))
11684d9c625SLionel Sambuc 			return (1);
11784d9c625SLionel Sambuc 		GET_SPACE_RETW(sp, bp, blen, tlen + 256);
11884d9c625SLionel Sambuc 		MEMCPYW(bp, p, tlen);
11984d9c625SLionel Sambuc 	}
12084d9c625SLionel Sambuc 
12184d9c625SLionel Sambuc 	/* Copy the end partial line into place. */
12284d9c625SLionel Sambuc 	if (db_get(sp, tm->lno, DBG_FATAL, &p, &len))
12384d9c625SLionel Sambuc 		goto err;
12484d9c625SLionel Sambuc 	if (len != 0 && tm->cno != len - 1) {
12584d9c625SLionel Sambuc 		/*
12684d9c625SLionel Sambuc 		 * XXX
12784d9c625SLionel Sambuc 		 * We can overflow memory here, if the total length is greater
12884d9c625SLionel Sambuc 		 * than SIZE_T_MAX.  The only portable way I've found to test
12984d9c625SLionel Sambuc 		 * is depending on the overflow being less than the value.
13084d9c625SLionel Sambuc 		 */
13184d9c625SLionel Sambuc 		nlen = (len - (tm->cno + 1)) + tlen;
13284d9c625SLionel Sambuc 		if (tlen > nlen) {
13384d9c625SLionel Sambuc 			msgq(sp, M_ERR, "002|Line length overflow");
13484d9c625SLionel Sambuc 			goto err;
13584d9c625SLionel Sambuc 		}
13684d9c625SLionel Sambuc 		if (tlen == 0) {
13784d9c625SLionel Sambuc 			GET_SPACE_RETW(sp, bp, blen, nlen);
13884d9c625SLionel Sambuc 		} else
13984d9c625SLionel Sambuc 			ADD_SPACE_RETW(sp, bp, blen, nlen);
14084d9c625SLionel Sambuc 
14184d9c625SLionel Sambuc 		MEMCPYW(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1));
14284d9c625SLionel Sambuc 		tlen += len - (tm->cno + 1);
14384d9c625SLionel Sambuc 	}
14484d9c625SLionel Sambuc 
14584d9c625SLionel Sambuc 	/* Set the current line. */
14684d9c625SLionel Sambuc 	if (db_set(sp, fm->lno, bp, tlen))
14784d9c625SLionel Sambuc 		goto err;
14884d9c625SLionel Sambuc 
14984d9c625SLionel Sambuc 	/* Delete the last and intermediate lines. */
15084d9c625SLionel Sambuc 	for (lno = tm->lno; lno > fm->lno; --lno) {
15184d9c625SLionel Sambuc 		if (db_delete(sp, lno))
15284d9c625SLionel Sambuc 			goto err;
15384d9c625SLionel Sambuc 		++sp->rptlines[L_DELETED];
15484d9c625SLionel Sambuc 		if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
15584d9c625SLionel Sambuc 			break;
15684d9c625SLionel Sambuc 	}
15784d9c625SLionel Sambuc 
15884d9c625SLionel Sambuc done:	rval = 0;
15984d9c625SLionel Sambuc 	if (0)
16084d9c625SLionel Sambuc err:		rval = 1;
16184d9c625SLionel Sambuc 	if (bp != NULL)
16284d9c625SLionel Sambuc 		FREE_SPACEW(sp, bp, blen);
16384d9c625SLionel Sambuc 	return (rval);
16484d9c625SLionel Sambuc }
165