1 /* $NetBSD: delete.c,v 1.2 2013/11/22 15:52:05 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.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <bitstring.h>
31 * Delete a range of text.
33 * PUBLIC: int del __P((SCR *, MARK *, MARK *, int));
36 del(SCR
*sp
, MARK
*fm
, MARK
*tm
, int lmode
)
39 size_t blen
, len
, nlen
, tlen
;
45 /* Case 1 -- delete in line mode. */
47 for (lno
= tm
->lno
; lno
>= fm
->lno
; --lno
) {
48 if (db_delete(sp
, lno
))
50 ++sp
->rptlines
[L_DELETED
];
51 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
58 * Case 2 -- delete to EOF. This is a special case because it's
59 * easier to pick it off than try and find it in the other cases.
61 if (db_last(sp
, &lno
))
65 if (db_get(sp
, lno
, DBG_FATAL
, &p
, &len
))
67 eof
= tm
->cno
!= ENTIRE_LINE
&& tm
->cno
>= len
? 1 : 0;
71 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
72 if (db_delete(sp
, lno
))
74 ++sp
->rptlines
[L_DELETED
];
76 INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
79 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
81 GET_SPACE_RETW(sp
, bp
, blen
, fm
->cno
);
82 MEMCPYW(bp
, p
, fm
->cno
);
83 if (db_set(sp
, fm
->lno
, bp
, fm
->cno
))
89 /* Case 3 -- delete within a single line. */
90 if (tm
->lno
== fm
->lno
) {
91 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, &len
))
93 GET_SPACE_RETW(sp
, bp
, blen
, len
);
95 MEMCPYW(bp
, p
, fm
->cno
);
96 MEMCPYW(bp
+ fm
->cno
, p
+ (tm
->cno
+ 1),
98 if (db_set(sp
, fm
->lno
,
99 bp
, len
- ((tm
->cno
- fm
->cno
) + 1)))
105 * Case 4 -- delete over multiple lines.
107 * Copy the start partial line into place.
109 if ((tlen
= fm
->cno
) != 0) {
110 if (db_get(sp
, fm
->lno
, DBG_FATAL
, &p
, NULL
))
112 GET_SPACE_RETW(sp
, bp
, blen
, tlen
+ 256);
113 MEMCPYW(bp
, p
, tlen
);
116 /* Copy the end partial line into place. */
117 if (db_get(sp
, tm
->lno
, DBG_FATAL
, &p
, &len
))
119 if (len
!= 0 && tm
->cno
!= len
- 1) {
122 * We can overflow memory here, if the total length is greater
123 * than SIZE_T_MAX. The only portable way I've found to test
124 * is depending on the overflow being less than the value.
126 nlen
= (len
- (tm
->cno
+ 1)) + tlen
;
128 msgq(sp
, M_ERR
, "002|Line length overflow");
132 GET_SPACE_RETW(sp
, bp
, blen
, nlen
);
134 ADD_SPACE_RETW(sp
, bp
, blen
, nlen
);
136 MEMCPYW(bp
+ tlen
, p
+ (tm
->cno
+ 1), len
- (tm
->cno
+ 1));
137 tlen
+= len
- (tm
->cno
+ 1);
140 /* Set the current line. */
141 if (db_set(sp
, fm
->lno
, bp
, tlen
))
144 /* Delete the last and intermediate lines. */
145 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
146 if (db_delete(sp
, lno
))
148 ++sp
->rptlines
[L_DELETED
];
149 if (lno
% INTERRUPT_CHECK
== 0 && INTERRUPTED(sp
))
157 FREE_SPACEW(sp
, bp
, blen
);