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: db.c,v 8.18 1993/11/22 17:26:52 bostic Exp $ (Berkeley) $Date: 1993/11/22 17:26:52 $";
12 #include <sys/types.h>
19 static inline int scr_update
__P((SCR
*, EXF
*, recno_t
, enum operation
, int));
23 * Look in the text buffers for a line; if it's not there
24 * call file_rline to retrieve it from the database.
27 file_gline(sp
, ep
, lno
, lenp
)
30 recno_t lno
; /* Line number. */
31 size_t *lenp
; /* Length store. */
37 * The underlying recno stuff handles zero by returning NULL, but
38 * have to have an oob condition for the look-aside into the input
45 * Look-aside into the TEXT buffers and see if the line we want
48 if (F_ISSET(sp
, S_INPUT
)) {
49 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
50 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
51 if (l1
<= lno
&& l2
>= lno
) {
52 for (tp
= sp
->tiq
.cqh_first
;
53 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
59 * Adjust the line number for the number of lines used
60 * by the text input buffers.
65 return (file_rline(sp
, ep
, lno
, lenp
));
70 * Look in the cache for a line; if it's not there retrieve
74 file_rline(sp
, ep
, lno
, lenp
)
77 recno_t lno
; /* Line number. */
78 size_t *lenp
; /* Length store. */
82 /* Check the cache. */
83 if (lno
== ep
->c_lno
) {
90 /* Get the line from the underlying database. */
92 key
.size
= sizeof(lno
);
93 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
96 "Error: %s/%d: unable to get line %u: %s.",
97 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
106 /* Fill the cache. */
108 ep
->c_len
= data
.size
;
109 ep
->c_lp
= data
.data
;
116 * Delete a line from the file.
119 file_dline(sp
, ep
, lno
)
126 #if defined(DEBUG) && 0
127 TRACE(sp
, "delete line %lu\n", lno
);
129 /* Delete any marks in the line. */
130 mark_delete(sp
, ep
, lno
);
133 log_line(sp
, ep
, lno
, LOG_LINE_DELETE
);
137 key
.size
= sizeof(lno
);
138 if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
140 "Error: %s/%d: unable to delete line %u: %s.",
141 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
145 /* Flush the cache, update line count, before screen update. */
146 if (lno
<= ep
->c_lno
)
148 if (ep
->c_nlines
!= OOBLNO
)
151 /* File now dirty. */
152 if (F_ISSET(ep
, F_FIRSTMODIFY
))
153 (void)rcv_init(sp
, ep
);
154 F_SET(ep
, F_MODIFIED
);
157 return (scr_update(sp
, ep
, lno
, LINE_DELETE
, 1));
162 * Append a line into the file.
165 file_aline(sp
, ep
, update
, lno
, p
, len
)
176 #if defined(DEBUG) && 0
177 TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
180 * Very nasty special case. The historic vi code displays a single
181 * space (or a '$' if the list option is set) for the first line in
182 * an "empty" file. If we "insert" a line, that line gets scrolled
183 * down, not repainted, so it's incorrect when we refresh the the
184 * screen. This is really hard to find and fix in the vi code -- the
185 * text input functions detect it explicitly and don't insert a new
186 * line. The hack here is to repaint the screen if we're appending
190 if (file_lline(sp
, ep
, &lline
))
198 key
.size
= sizeof(lno
);
201 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
203 "Error: %s/%d: unable to append to line %u: %s.",
204 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
208 /* Flush the cache, update line count, before screen update. */
211 if (ep
->c_nlines
!= OOBLNO
)
214 /* File now dirty. */
215 if (F_ISSET(ep
, F_FIRSTMODIFY
))
216 (void)rcv_init(sp
, ep
);
217 F_SET(ep
, F_MODIFIED
);
220 log_line(sp
, ep
, lno
+ 1, LOG_LINE_APPEND
);
222 /* Push any marks above the line. */
223 mark_insert(sp
, ep
, lno
+ 1);
229 * Nasty hack. If multiple lines are input by the user, they aren't
230 * committed until an <ESC> is entered. The problem is the screen was
231 * updated/scrolled as each line was entered. So, when this routine
232 * is called to copy the new lines from the cut buffer into the file,
233 * it has to know not to update the screen again.
235 return (scr_update(sp
, ep
, lno
, LINE_APPEND
, update
));
240 * Insert a line into the file.
243 file_iline(sp
, ep
, lno
, p
, len
)
253 #if defined(DEBUG) && 0
255 "insert before %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
258 /* Very nasty special case. See comment in file_aline(). */
260 if (file_lline(sp
, ep
, &lline
))
268 key
.size
= sizeof(lno
);
271 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
273 "Error: %s/%d: unable to insert at line %u: %s.",
274 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
278 /* Flush the cache, update line count, before screen update. */
279 if (lno
>= ep
->c_lno
)
281 if (ep
->c_nlines
!= OOBLNO
)
284 /* File now dirty. */
285 if (F_ISSET(ep
, F_FIRSTMODIFY
))
286 (void)rcv_init(sp
, ep
);
287 F_SET(ep
, F_MODIFIED
);
290 log_line(sp
, ep
, lno
, LOG_LINE_INSERT
);
292 /* Push any marks above the line. */
293 mark_insert(sp
, ep
, lno
);
296 return (scr_update(sp
, ep
, lno
, LINE_INSERT
, 1));
301 * Store a line in the file.
304 file_sline(sp
, ep
, lno
, p
, len
)
313 #if defined(DEBUG) && 0
315 "replace line %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
317 /* Log before change. */
318 log_line(sp
, ep
, lno
, LOG_LINE_RESET_B
);
322 key
.size
= sizeof(lno
);
325 if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
327 "Error: %s/%d: unable to store line %u: %s.",
328 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
332 /* Flush the cache, before logging or screen update. */
333 if (lno
== ep
->c_lno
)
336 /* File now dirty. */
337 if (F_ISSET(ep
, F_FIRSTMODIFY
))
338 (void)rcv_init(sp
, ep
);
339 F_SET(ep
, F_MODIFIED
);
341 /* Log after change. */
342 log_line(sp
, ep
, lno
, LOG_LINE_RESET_F
);
345 return (scr_update(sp
, ep
, lno
, LINE_RESET
, 1));
350 * Return the number of lines in the file.
353 file_lline(sp
, ep
, lnop
)
361 /* Check the cache. */
362 if (ep
->c_nlines
!= OOBLNO
) {
363 *lnop
= (F_ISSET(sp
, S_INPUT
) &&
364 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> ep
->c_nlines
?
365 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: ep
->c_nlines
);
370 key
.size
= sizeof(lno
);
372 switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
375 "Error: %s/%d: unable to get last line: %s.",
376 tail(__FILE__
), __LINE__
, strerror(errno
));
383 memmove(&lno
, key
.data
, sizeof(lno
));
387 /* Fill the cache. */
388 ep
->c_nlines
= ep
->c_lno
= lno
;
389 ep
->c_len
= data
.size
;
390 ep
->c_lp
= data
.data
;
392 *lnop
= (F_ISSET(sp
, S_INPUT
) &&
393 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
394 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
400 * Update all of the screens that are backed by the file that
404 scr_update(sp
, ep
, lno
, op
, current
)
414 for (tsp
= sp
->gp
->dq
.cqh_first
;
415 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
416 if (sp
!= tsp
&& tsp
->ep
== ep
)
417 (void)sp
->s_change(tsp
, ep
, lno
, op
);
418 return (current
&& sp
->s_change(sp
, ep
, lno
, op
));