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.
13 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 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
30 * Delete a range of text.
32 * PUBLIC: int del __P((SCR *, MARK *, MARK *, int));
35 del(SCR
*sp
, MARK
*fm
, MARK
*tm
, int lmode
)
38 size_t blen
, len
, nlen
, tlen
;
44 /* Case 1 -- delete in line mode. */
46 for (lno
= tm
->lno
; lno
>= fm
->lno
; --lno
) {
47 if (db_delete(sp
, lno
))
49 ++sp
->rptlines
[L_DELETED
];
50 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
57 * Case 2 -- delete to EOF. This is a special case because it's
58 * easier to pick it off than try and find it in the other cases.
60 if (db_last(sp
, &lno
))
64 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
66 eof
= tm
->cno
>= len
? 1 : 0;
70 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
71 if (db_delete(sp
, lno
))
73 ++sp
->rptlines
[L_DELETED
];
75 INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
78 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
80 GET_SPACE_RETW(sp
, bp
, blen
, fm
->cno
);
81 MEMCPYW(bp
, p
, fm
->cno
);
82 if (db_set(sp
, fm
->lno
, bp
, fm
->cno
))
88 /* Case 3 -- delete within a single line. */
89 if (tm
->lno
== fm
->lno
) {
90 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
92 GET_SPACE_RETW(sp
, bp
, blen
, len
);
94 MEMCPYW(bp
, p
, fm
->cno
);
95 MEMCPYW(bp
+ fm
->cno
, p
+ (tm
->cno
+ 1),
97 if (db_set(sp
, fm
->lno
,
98 bp
, len
- ((tm
->cno
- fm
->cno
) + 1)))
104 * Case 4 -- delete over multiple lines.
106 * Copy the start partial line into place.
108 if ((tlen
= fm
->cno
) != 0) {
109 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, NULL
))
111 GET_SPACE_RETW(sp
, bp
, blen
, tlen
+ 256);
112 MEMCPYW(bp
, p
, tlen
);
115 /* Copy the end partial line into place. */
116 if (db_get(sp
, tm
->lno
, DBG_FATAL
, &p
, &len
))
118 if (len
!= 0 && tm
->cno
!= len
- 1) {
121 * We can overflow memory here, if the total length is greater
122 * than SIZE_T_MAX. The only portable way I've found to test
123 * is depending on the overflow being less than the value.
125 nlen
= (len
- (tm
->cno
+ 1)) + tlen
;
127 msgq(sp
, M_ERR
, "002|Line length overflow");
131 GET_SPACE_RETW(sp
, bp
, blen
, nlen
);
133 ADD_SPACE_RETW(sp
, bp
, blen
, nlen
);
135 MEMCPYW(bp
+ tlen
, p
+ (tm
->cno
+ 1), len
- (tm
->cno
+ 1));
136 tlen
+= len
- (tm
->cno
+ 1);
139 /* Set the current line. */
140 if (db_set(sp
, fm
->lno
, bp
, tlen
))
143 /* Delete the last and intermediate lines. */
144 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
145 if (db_delete(sp
, lno
))
147 ++sp
->rptlines
[L_DELETED
];
148 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
156 FREE_SPACEW(sp
, bp
, blen
);