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