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.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
32 * Delete a range of text.
34 * PUBLIC: int del __P((SCR *, MARK *, MARK *, int));
37 del(SCR
*sp
, MARK
*fm
, MARK
*tm
, int lmode
)
40 size_t blen
, len
, nlen
, tlen
;
46 /* Case 1 -- delete in line mode. */
48 for (lno
= tm
->lno
; lno
>= fm
->lno
; --lno
) {
49 if (db_delete(sp
, lno
))
51 ++sp
->rptlines
[L_DELETED
];
52 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
59 * Case 2 -- delete to EOF. This is a special case because it's
60 * easier to pick it off than try and find it in the other cases.
62 if (db_last(sp
, &lno
))
66 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
68 eof
= tm
->cno
>= len
? 1 : 0;
72 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
73 if (db_delete(sp
, lno
))
75 ++sp
->rptlines
[L_DELETED
];
77 INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
80 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
82 GET_SPACE_RETW(sp
, bp
, blen
, fm
->cno
);
83 MEMCPYW(bp
, p
, fm
->cno
);
84 if (db_set(sp
, fm
->lno
, bp
, fm
->cno
))
90 /* Case 3 -- delete within a single line. */
91 if (tm
->lno
== fm
->lno
) {
92 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
94 GET_SPACE_RETW(sp
, bp
, blen
, len
);
96 MEMCPYW(bp
, p
, fm
->cno
);
97 MEMCPYW(bp
+ fm
->cno
, p
+ (tm
->cno
+ 1),
99 if (db_set(sp
, fm
->lno
,
100 bp
, len
- ((tm
->cno
- fm
->cno
) + 1)))
106 * Case 4 -- delete over multiple lines.
108 * Copy the start partial line into place.
110 if ((tlen
= fm
->cno
) != 0) {
111 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, NULL
))
113 GET_SPACE_RETW(sp
, bp
, blen
, tlen
+ 256);
114 MEMCPYW(bp
, p
, tlen
);
117 /* Copy the end partial line into place. */
118 if (db_get(sp
, tm
->lno
, DBG_FATAL
, &p
, &len
))
120 if (len
!= 0 && tm
->cno
!= len
- 1) {
123 * We can overflow memory here, if the total length is greater
124 * than SIZE_T_MAX. The only portable way I've found to test
125 * is depending on the overflow being less than the value.
127 nlen
= (len
- (tm
->cno
+ 1)) + tlen
;
129 msgq(sp
, M_ERR
, "002|Line length overflow");
133 GET_SPACE_RETW(sp
, bp
, blen
, nlen
);
135 ADD_SPACE_RETW(sp
, bp
, blen
, nlen
);
137 MEMCPYW(bp
+ tlen
, p
+ (tm
->cno
+ 1), len
- (tm
->cno
+ 1));
138 tlen
+= len
- (tm
->cno
+ 1);
141 /* Set the current line. */
142 if (db_set(sp
, fm
->lno
, bp
, tlen
))
145 /* Delete the last and intermediate lines. */
146 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
147 if (db_delete(sp
, lno
))
149 ++sp
->rptlines
[L_DELETED
];
150 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
158 FREE_SPACEW(sp
, bp
, blen
);