2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: delete.c,v 8.7 1993/12/09 19:42:05 bostic Exp $ (Berkeley) $Date: 1993/12/09 19:42:05 $";
12 #include <sys/types.h>
22 * Delete a range of text.
25 delete(sp
, ep
, fm
, tm
, lmode
)
32 size_t blen
, len
, tlen
;
36 #if defined(DEBUG) && 0
37 TRACE(sp
, "delete: from %lu/%d to %lu/%d%s\n",
38 fm
->lno
, fm
->cno
, tm
->lno
, tm
->cno
, lmode
? " (LINE MODE)" : "");
42 /* Case 1 -- delete in line mode. */
44 for (lno
= tm
->lno
; lno
>= fm
->lno
; --lno
)
45 if (file_dline(sp
, ep
, lno
))
51 * Case 2 -- delete to EOF. This is a special case because it's
52 * easier to pick it off than try and find it in the other cases.
54 if (file_lline(sp
, ep
, &lno
))
58 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
62 eof
= tm
->cno
>= len
? 1 : 0;
66 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
) {
67 if (file_dline(sp
, ep
, lno
))
69 ++sp
->rptlines
[L_DELETED
];
71 if ((p
= file_gline(sp
, ep
, fm
->lno
, &len
)) == NULL
) {
72 GETLINE_ERR(sp
, fm
->lno
);
75 GET_SPACE_RET(sp
, bp
, blen
, fm
->cno
);
76 memmove(bp
, p
, fm
->cno
);
77 if (file_sline(sp
, ep
, fm
->lno
, bp
, fm
->cno
))
83 /* Case 3 -- delete within a single line. */
84 if (tm
->lno
== fm
->lno
) {
85 if ((p
= file_gline(sp
, ep
, fm
->lno
, &len
)) == NULL
) {
86 GETLINE_ERR(sp
, fm
->lno
);
89 GET_SPACE_RET(sp
, bp
, blen
, len
);
90 memmove(bp
, p
, fm
->cno
);
91 memmove(bp
+ fm
->cno
, p
+ tm
->cno
, len
- tm
->cno
);
92 if (file_sline(sp
, ep
, fm
->lno
, bp
, len
- (tm
->cno
- fm
->cno
)))
98 * Case 4 -- delete over multiple lines.
100 * Figure out how big a buffer we need.
102 if ((p
= file_gline(sp
, ep
, fm
->lno
, &len
)) == NULL
) {
103 GETLINE_ERR(sp
, fm
->lno
);
107 if ((p
= file_gline(sp
, ep
, tm
->lno
, &len
)) == NULL
) {
108 GETLINE_ERR(sp
, tm
->lno
);
114 * We can overflow memory here, if (len + tlen) > SIZE_T_MAX. The
115 * only portable way I've found to test is to depend on the overflow
116 * being less than the value.
120 msgq(sp
, M_ERR
, "Error: line length overflow");
124 GET_SPACE_RET(sp
, bp
, blen
, tlen
);
126 /* Copy the start partial line into place. */
127 if ((p
= file_gline(sp
, ep
, fm
->lno
, &len
)) == NULL
) {
128 GETLINE_ERR(sp
, fm
->lno
);
131 memmove(bp
, p
, fm
->cno
);
134 /* Copy the end partial line into place. */
135 if ((p
= file_gline(sp
, ep
, tm
->lno
, &len
)) == NULL
) {
136 GETLINE_ERR(sp
, tm
->lno
);
139 memmove(bp
+ tlen
, p
+ tm
->cno
, len
- tm
->cno
);
140 tlen
+= len
- tm
->cno
;
142 /* Set the current line. */
143 if (file_sline(sp
, ep
, fm
->lno
, bp
, tlen
))
146 /* Delete the last and intermediate lines. */
147 for (lno
= tm
->lno
; lno
> fm
->lno
; --lno
)
148 if (file_dline(sp
, ep
, lno
))
152 vdone
: sp
->rptlines
[L_DELETED
] += tm
->lno
- fm
->lno
+ 1;
154 done
: if (bp
!= NULL
)
155 FREE_SPACE(sp
, bp
, blen
);
160 err
: FREE_SPACE(sp
, bp
, blen
);