xref: /netbsd-src/external/bsd/nvi/dist/common/delete.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: delete.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
16 
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "common.h"
28 
29 /*
30  * del --
31  *	Delete a range of text.
32  *
33  * PUBLIC: int del __P((SCR *, MARK *, MARK *, int));
34  */
35 int
36 del(SCR *sp, MARK *fm, MARK *tm, int lmode)
37 {
38 	db_recno_t lno;
39 	size_t blen, len, nlen, tlen;
40 	CHAR_T *bp, *p;
41 	int eof, rval;
42 
43 	bp = NULL;
44 
45 	/* Case 1 -- delete in line mode. */
46 	if (lmode) {
47 		for (lno = tm->lno; lno >= fm->lno; --lno) {
48 			if (db_delete(sp, lno))
49 				return (1);
50 			++sp->rptlines[L_DELETED];
51 			if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
52 				break;
53 		}
54 		goto done;
55 	}
56 
57 	/*
58 	 * Case 2 -- delete to EOF.  This is a special case because it's
59 	 * easier to pick it off than try and find it in the other cases.
60  	 */
61 	if (db_last(sp, &lno))
62 		return (1);
63 	if (tm->lno >= lno) {
64 		if (tm->lno == lno) {
65 			if (db_get(sp, lno, DBG_FATAL, &p, &len))
66 				return (1);
67 			eof = tm->cno != ENTIRE_LINE && tm->cno >= len ? 1 : 0;
68 		} else
69 			eof = 1;
70 		if (eof) {
71 			for (lno = tm->lno; lno > fm->lno; --lno) {
72 				if (db_delete(sp, lno))
73 					return (1);
74 				++sp->rptlines[L_DELETED];
75 				if (lno %
76 				    INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
77 					break;
78 			}
79 			if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
80 				return (1);
81 			GET_SPACE_RETW(sp, bp, blen, fm->cno);
82 			MEMCPYW(bp, p, fm->cno);
83 			if (db_set(sp, fm->lno, bp, fm->cno))
84 				return (1);
85 			goto done;
86 		}
87 	}
88 
89 	/* Case 3 -- delete within a single line. */
90 	if (tm->lno == fm->lno) {
91 		if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
92 			return (1);
93 		GET_SPACE_RETW(sp, bp, blen, len);
94 		if (fm->cno != 0)
95 			MEMCPYW(bp, p, fm->cno);
96 		MEMCPYW(bp + fm->cno, p + (tm->cno + 1),
97 			len - (tm->cno + 1));
98 		if (db_set(sp, fm->lno,
99 		    bp, len - ((tm->cno - fm->cno) + 1)))
100 			goto err;
101 		goto done;
102 	}
103 
104 	/*
105 	 * Case 4 -- delete over multiple lines.
106 	 *
107 	 * Copy the start partial line into place.
108 	 */
109 	if ((tlen = fm->cno) != 0) {
110 		if (db_get(sp, fm->lno, DBG_FATAL, &p, NULL))
111 			return (1);
112 		GET_SPACE_RETW(sp, bp, blen, tlen + 256);
113 		MEMCPYW(bp, p, tlen);
114 	}
115 
116 	/* Copy the end partial line into place. */
117 	if (db_get(sp, tm->lno, DBG_FATAL, &p, &len))
118 		goto err;
119 	if (len != 0 && tm->cno != len - 1) {
120 		/*
121 		 * XXX
122 		 * We can overflow memory here, if the total length is greater
123 		 * than SIZE_T_MAX.  The only portable way I've found to test
124 		 * is depending on the overflow being less than the value.
125 		 */
126 		nlen = (len - (tm->cno + 1)) + tlen;
127 		if (tlen > nlen) {
128 			msgq(sp, M_ERR, "002|Line length overflow");
129 			goto err;
130 		}
131 		if (tlen == 0) {
132 			GET_SPACE_RETW(sp, bp, blen, nlen);
133 		} else
134 			ADD_SPACE_RETW(sp, bp, blen, nlen);
135 
136 		MEMCPYW(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1));
137 		tlen += len - (tm->cno + 1);
138 	}
139 
140 	/* Set the current line. */
141 	if (db_set(sp, fm->lno, bp, tlen))
142 		goto err;
143 
144 	/* Delete the last and intermediate lines. */
145 	for (lno = tm->lno; lno > fm->lno; --lno) {
146 		if (db_delete(sp, lno))
147 			goto err;
148 		++sp->rptlines[L_DELETED];
149 		if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
150 			break;
151 	}
152 
153 done:	rval = 0;
154 	if (0)
155 err:		rval = 1;
156 	if (bp != NULL)
157 		FREE_SPACEW(sp, bp, blen);
158 	return (rval);
159 }
160