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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
27 * Delete a range of text.
29 * PUBLIC: int del(SCR *, MARK *, MARK *, int);
32 del(SCR *sp, MARK *fm, MARK *tm, int lmode)
35 size_t blen, len, nlen, tlen;
41 /* Case 1 -- delete in line mode. */
43 for (lno = tm->lno; lno >= fm->lno; --lno) {
44 if (db_delete(sp, lno))
46 ++sp->rptlines[L_DELETED];
47 if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
54 * Case 2 -- delete to EOF. This is a special case because it's
55 * easier to pick it off than try and find it in the other cases.
57 if (db_last(sp, &lno))
61 if (db_get(sp, lno, DBG_FATAL, &p, &len))
63 eof = tm->cno != ENTIRE_LINE && tm->cno >= len ? 1 : 0;
67 for (lno = tm->lno; lno > fm->lno; --lno) {
68 if (db_delete(sp, lno))
70 ++sp->rptlines[L_DELETED];
72 INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
75 if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
77 GET_SPACE_RETW(sp, bp, blen, fm->cno);
78 MEMCPY(bp, p, fm->cno);
79 if (db_set(sp, fm->lno, bp, fm->cno))
85 /* Case 3 -- delete within a single line. */
86 if (tm->lno == fm->lno) {
87 if (db_get(sp, fm->lno, DBG_FATAL, &p, &len))
90 GET_SPACE_RETW(sp, bp, blen, len);
92 MEMCPY(bp, p, fm->cno);
93 MEMCPY(bp + fm->cno, p + (tm->cno + 1),
95 if (db_set(sp, fm->lno,
96 bp, len - ((tm->cno - fm->cno) + 1)))
103 * Case 4 -- delete over multiple lines.
105 * Copy the start partial line into place.
107 if ((tlen = fm->cno) != 0) {
108 if (db_get(sp, fm->lno, DBG_FATAL, &p, NULL))
110 GET_SPACE_RETW(sp, bp, blen, tlen + 256);
114 /* Copy the end partial line into place. */
115 if (db_get(sp, tm->lno, DBG_FATAL, &p, &len))
117 if (len != 0 && tm->cno != len - 1) {
120 * We can overflow memory here, if the total length is greater
121 * than SIZE_T_MAX. The only portable way I've found to test
122 * is depending on the overflow being less than the value.
124 nlen = (len - (tm->cno + 1)) + tlen;
126 msgq(sp, M_ERR, "002|Line length overflow");
130 GET_SPACE_RETW(sp, bp, blen, nlen);
132 ADD_SPACE_RETW(sp, bp, blen, nlen);
134 MEMCPY(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1));
135 tlen += len - (tm->cno + 1);
138 /* Set the current line. */
139 if (db_set(sp, fm->lno, bp, tlen))
142 /* Delete the last and intermediate lines. */
143 for (lno = tm->lno; lno > fm->lno; --lno) {
144 if (db_delete(sp, lno))
146 ++sp->rptlines[L_DELETED];
147 if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp))
155 FREE_SPACEW(sp, bp, blen);