1 /* $NetBSD: delete.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: delete.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
36 * Delete a range of text.
38 * PUBLIC: int del __P((SCR *, MARK *, MARK *, int));
41 del(SCR
*sp
, MARK
*fm
, MARK
*tm
, int lmode
)
44 size_t blen
, len
, nlen
, tlen
;
50 /* Case 1 -- delete in line mode. */
52 for (lno
= tm
->lno
; lno
>= fm
->lno
; --lno
) {
53 if (db_delete(sp
, lno
))
55 ++sp
->rptlines
[L_DELETED
];
56 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
63 * Case 2 -- delete to EOF. This is a special case because it's
64 * easier to pick it off than try and find it in the other cases.
66 if (db_last(sp
, &lno
))
70 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
72 eof
= tm
->cno
!= ENTIRE_LINE
&& tm
->cno
>= len
? 1 : 0;
76 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
77 if (db_delete(sp
, lno
))
79 ++sp
->rptlines
[L_DELETED
];
81 INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
84 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
86 GET_SPACE_RETW(sp
, bp
, blen
, fm
->cno
);
87 MEMCPYW(bp
, p
, fm
->cno
);
88 if (db_set(sp
, fm
->lno
, bp
, fm
->cno
))
94 /* Case 3 -- delete within a single line. */
95 if (tm
->lno
== fm
->lno
) {
96 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
98 GET_SPACE_RETW(sp
, bp
, blen
, len
);
100 MEMCPYW(bp
, p
, fm
->cno
);
101 MEMCPYW(bp
+ fm
->cno
, p
+ (tm
->cno
+ 1),
102 len
- (tm
->cno
+ 1));
103 if (db_set(sp
, fm
->lno
,
104 bp
, len
- ((tm
->cno
- fm
->cno
) + 1)))
110 * Case 4 -- delete over multiple lines.
112 * Copy the start partial line into place.
114 if ((tlen
= fm
->cno
) != 0) {
115 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, NULL
))
117 GET_SPACE_RETW(sp
, bp
, blen
, tlen
+ 256);
118 MEMCPYW(bp
, p
, tlen
);
121 /* Copy the end partial line into place. */
122 if (db_get(sp
, tm
->lno
, DBG_FATAL
, &p
, &len
))
124 if (len
!= 0 && tm
->cno
!= len
- 1) {
127 * We can overflow memory here, if the total length is greater
128 * than SIZE_T_MAX. The only portable way I've found to test
129 * is depending on the overflow being less than the value.
131 nlen
= (len
- (tm
->cno
+ 1)) + tlen
;
133 msgq(sp
, M_ERR
, "002|Line length overflow");
137 GET_SPACE_RETW(sp
, bp
, blen
, nlen
);
139 ADD_SPACE_RETW(sp
, bp
, blen
, nlen
);
141 MEMCPYW(bp
+ tlen
, p
+ (tm
->cno
+ 1), len
- (tm
->cno
+ 1));
142 tlen
+= len
- (tm
->cno
+ 1);
145 /* Set the current line. */
146 if (db_set(sp
, fm
->lno
, bp
, tlen
))
149 /* Delete the last and intermediate lines. */
150 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
151 if (db_delete(sp
, lno
))
153 ++sp
->rptlines
[L_DELETED
];
154 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
162 FREE_SPACEW(sp
, bp
, blen
);