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.16 2000/07/15 20:26:34 skimo Exp $ (Berkeley) $Date: 2000/07/15 20:26:34 $";
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(sp
, fm
, tm
, lmode
)
41 size_t blen
, len
, nlen
, tlen
;
47 /* Case 1 -- delete in line mode. */
49 for (lno
= tm
->lno
; lno
>= fm
->lno
; --lno
) {
50 if (db_delete(sp
, lno
))
52 ++sp
->rptlines
[L_DELETED
];
53 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
60 * Case 2 -- delete to EOF. This is a special case because it's
61 * easier to pick it off than try and find it in the other cases.
63 if (db_last(sp
, &lno
))
67 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
69 eof
= tm
->cno
>= len
? 1 : 0;
73 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
74 if (db_delete(sp
, lno
))
76 ++sp
->rptlines
[L_DELETED
];
78 INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
81 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
83 GET_SPACE_RETW(sp
, bp
, blen
, fm
->cno
);
84 MEMCPYW(bp
, p
, fm
->cno
);
85 if (db_set(sp
, fm
->lno
, bp
, fm
->cno
))
91 /* Case 3 -- delete within a single line. */
92 if (tm
->lno
== fm
->lno
) {
93 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
95 GET_SPACE_RETW(sp
, bp
, blen
, len
);
97 MEMCPYW(bp
, p
, fm
->cno
);
98 MEMCPYW(bp
+ fm
->cno
, p
+ (tm
->cno
+ 1),
100 if (db_set(sp
, fm
->lno
,
101 bp
, len
- ((tm
->cno
- fm
->cno
) + 1)))
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
))
114 GET_SPACE_RETW(sp
, bp
, blen
, tlen
+ 256);
115 MEMCPYW(bp
, p
, tlen
);
118 /* Copy the end partial line into place. */
119 if (db_get(sp
, tm
->lno
, DBG_FATAL
, &p
, &len
))
121 if (len
!= 0 && tm
->cno
!= len
- 1) {
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
;
130 msgq(sp
, M_ERR
, "002|Line length overflow");
134 GET_SPACE_RETW(sp
, bp
, blen
, nlen
);
136 ADD_SPACE_RETW(sp
, bp
, blen
, nlen
);
138 MEMCPYW(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
))
146 /* Delete the last and intermediate lines. */
147 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
148 if (db_delete(sp
, lno
))
150 ++sp
->rptlines
[L_DELETED
];
151 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
159 FREE_SPACEW(sp
, bp
, blen
);