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