* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)line.c 8.1 (Berkeley) 6/9/93";
* Macro to walk the screens and update all of them that are backed
* by the file that just changed.
#define UPDATE_SCREENS(op) { \
if (sp->s_change != NULL) \
sp->s_change(sp, ep, lno, op); \
for (__tsp = sp->gp->scrhdr.next; \
__tsp != (SCR *)&sp->gp->scrhdr; \
__tsp->s_change != NULL) { \
sp->s_change(__tsp, ep, lno, op); \
sp->s_refresh(__tsp, ep); \
* Look in the text buffers for a line; if it's not there
* call file_rline to retrieve it from the database.
file_gline(sp
, ep
, lno
, lenp
)
recno_t lno
; /* Line number. */
size_t *lenp
; /* Length store. */
* The underlying recno stuff handles zero by returning NULL, but
* have to have an oob condition for the look-aside into the input
* Look-aside into the TEXT buffers and see if the line we want
if (F_ISSET(&sp
->txthdr
, HDR_INUSE
) &&
((TEXT
*)sp
->txthdr
.next
)->lno
<= lno
&&
((TEXT
*)sp
->txthdr
.prev
)->lno
>= lno
) {
for (tp
= sp
->txthdr
.next
; tp
->lno
!= lno
; tp
= tp
->next
);
if (F_ISSET(&sp
->bhdr
, HDR_INUSE
) &&
((TEXT
*)sp
->bhdr
.next
)->lno
<= lno
&&
((TEXT
*)sp
->bhdr
.prev
)->lno
>= lno
) {
for (tp
= sp
->bhdr
.next
; tp
->lno
!= lno
; tp
= tp
->next
);
return (file_rline(sp
, ep
, lno
, lenp
));
* Look in the cache for a line; if it's not there retrieve
file_rline(sp
, ep
, lno
, lenp
)
recno_t lno
; /* Line number. */
size_t *lenp
; /* Length store. */
/* Get the line from the underlying database. */
switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
"Error: %s/%d: unable to get line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
* Delete a line from the file.
TRACE(sp
, "delete line %lu\n", lno
);
log_line(sp
, ep
, lno
, LOG_LINE_DELETE
);
if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
"Error: %s/%d: unable to delete line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, update line count, before screen update. */
if (ep
->c_nlines
!= OOBLNO
)
if (F_ISSET(ep
, F_FIRSTMODIFY
))
UPDATE_SCREENS(LINE_DELETE
);
* Append a line into the file.
file_aline(sp
, ep
, update
, lno
, p
, len
)
TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
"Error: %s/%d: unable to append to line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, update line count, before screen update. */
if (ep
->c_nlines
!= OOBLNO
)
if (F_ISSET(ep
, F_FIRSTMODIFY
))
log_line(sp
, ep
, lno
+ 1, LOG_LINE_APPEND
);
* Nasty hack. If multiple lines are input by the user, they aren't
* committed until an <ESC> is entered. The problem is the screen was
* updated/scrolled as each line was entered. So, when this routine
* is called to copy the new lines from the cut buffer into the file,
* it has to know not to update the screen again.
UPDATE_SCREENS(LINE_APPEND
);
* Insert a line into the file.
file_iline(sp
, ep
, lno
, p
, len
)
"insert before %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
"Error: %s/%d: unable to insert at line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, update line count, before screen update. */
if (ep
->c_nlines
!= OOBLNO
)
if (F_ISSET(ep
, F_FIRSTMODIFY
))
log_line(sp
, ep
, lno
, LOG_LINE_INSERT
);
UPDATE_SCREENS(LINE_INSERT
);
* Store a line in the file.
file_sline(sp
, ep
, lno
, p
, len
)
"replace line %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
log_line(sp
, ep
, lno
, LOG_LINE_RESET_B
);
if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
"Error: %s/%d: unable to store line %u: %s.",
tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
/* Flush the cache, before logging or screen update. */
if (F_ISSET(ep
, F_FIRSTMODIFY
))
log_line(sp
, ep
, lno
, LOG_LINE_RESET_F
);
UPDATE_SCREENS(LINE_RESET
);
* Return the number of lines in the file.
if (ep
->c_nlines
!= OOBLNO
) {
*lnop
= (F_ISSET(sp
, S_INPUT
) &&
((TEXT
*)sp
->txthdr
.prev
)->lno
> ep
->c_nlines
?
((TEXT
*)sp
->txthdr
.prev
)->lno
: ep
->c_nlines
);
switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
"Error: %s/%d: unable to get last line: %s.",
tail(__FILE__
), __LINE__
, strerror(errno
));
memmove(&lno
, key
.data
, sizeof(lno
));
ep
->c_nlines
= ep
->c_lno
= lno
;
*lnop
= (F_ISSET(sp
, S_INPUT
) &&
((TEXT
*)sp
->txthdr
.prev
)->lno
> lno
?
((TEXT
*)sp
->txthdr
.prev
)->lno
: lno
);