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.19 1993/12/27 16:50:37 bostic Exp $ (Berkeley) $Date: 1993/12/27 16:50:37 $";
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
);
132 * Marks and global commands have to know when lines are
133 * inserted or deleted.
135 mark_insdel(sp
, ep
, LINE_DELETE
, lno
);
136 global_insdel(sp
, ep
, LINE_DELETE
, lno
);
139 log_line(sp
, ep
, lno
, LOG_LINE_DELETE
);
143 key
.size
= sizeof(lno
);
144 if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
146 "Error: %s/%d: unable to delete line %u: %s.",
147 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
151 /* Flush the cache, update line count, before screen update. */
152 if (lno
<= ep
->c_lno
)
154 if (ep
->c_nlines
!= OOBLNO
)
157 /* File now dirty. */
158 if (F_ISSET(ep
, F_FIRSTMODIFY
))
159 (void)rcv_init(sp
, ep
);
160 F_SET(ep
, F_MODIFIED
);
163 return (scr_update(sp
, ep
, lno
, LINE_DELETE
, 1));
168 * Append a line into the file.
171 file_aline(sp
, ep
, update
, lno
, p
, len
)
182 #if defined(DEBUG) && 0
183 TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
186 * Very nasty special case. The historic vi code displays a single
187 * space (or a '$' if the list option is set) for the first line in
188 * an "empty" file. If we "insert" a line, that line gets scrolled
189 * down, not repainted, so it's incorrect when we refresh the the
190 * screen. This is really hard to find and fix in the vi code -- the
191 * text input functions detect it explicitly and don't insert a new
192 * line. The hack here is to repaint the screen if we're appending
196 if (file_lline(sp
, ep
, &lline
))
204 key
.size
= sizeof(lno
);
207 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
209 "Error: %s/%d: unable to append to line %u: %s.",
210 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
214 /* Flush the cache, update line count, before screen update. */
217 if (ep
->c_nlines
!= OOBLNO
)
220 /* File now dirty. */
221 if (F_ISSET(ep
, F_FIRSTMODIFY
))
222 (void)rcv_init(sp
, ep
);
223 F_SET(ep
, F_MODIFIED
);
226 log_line(sp
, ep
, lno
+ 1, LOG_LINE_APPEND
);
231 * Marks and global commands have to know when lines are
232 * inserted or deleted.
234 mark_insdel(sp
, ep
, LINE_INSERT
, lno
+ 1);
235 global_insdel(sp
, ep
, LINE_INSERT
, lno
+ 1);
241 * Nasty hack. If multiple lines are input by the user, they aren't
242 * committed until an <ESC> is entered. The problem is the screen was
243 * updated/scrolled as each line was entered. So, when this routine
244 * is called to copy the new lines from the cut buffer into the file,
245 * it has to know not to update the screen again.
247 return (scr_update(sp
, ep
, lno
, LINE_APPEND
, update
));
252 * Insert a line into the file.
255 file_iline(sp
, ep
, lno
, p
, len
)
265 #if defined(DEBUG) && 0
267 "insert before %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
270 /* Very nasty special case. See comment in file_aline(). */
272 if (file_lline(sp
, ep
, &lline
))
280 key
.size
= sizeof(lno
);
283 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
285 "Error: %s/%d: unable to insert at line %u: %s.",
286 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
290 /* Flush the cache, update line count, before screen update. */
291 if (lno
>= ep
->c_lno
)
293 if (ep
->c_nlines
!= OOBLNO
)
296 /* File now dirty. */
297 if (F_ISSET(ep
, F_FIRSTMODIFY
))
298 (void)rcv_init(sp
, ep
);
299 F_SET(ep
, F_MODIFIED
);
302 log_line(sp
, ep
, lno
, LOG_LINE_INSERT
);
307 * Marks and global commands have to know when lines are
308 * inserted or deleted.
310 mark_insdel(sp
, ep
, LINE_INSERT
, lno
);
311 global_insdel(sp
, ep
, LINE_INSERT
, lno
);
314 return (scr_update(sp
, ep
, lno
, LINE_INSERT
, 1));
319 * Store a line in the file.
322 file_sline(sp
, ep
, lno
, p
, len
)
331 #if defined(DEBUG) && 0
333 "replace line %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
335 /* Log before change. */
336 log_line(sp
, ep
, lno
, LOG_LINE_RESET_B
);
340 key
.size
= sizeof(lno
);
343 if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
345 "Error: %s/%d: unable to store line %u: %s.",
346 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
350 /* Flush the cache, before logging or screen update. */
351 if (lno
== ep
->c_lno
)
354 /* File now dirty. */
355 if (F_ISSET(ep
, F_FIRSTMODIFY
))
356 (void)rcv_init(sp
, ep
);
357 F_SET(ep
, F_MODIFIED
);
359 /* Log after change. */
360 log_line(sp
, ep
, lno
, LOG_LINE_RESET_F
);
363 return (scr_update(sp
, ep
, lno
, LINE_RESET
, 1));
368 * Return the number of lines in the file.
371 file_lline(sp
, ep
, lnop
)
379 /* Check the cache. */
380 if (ep
->c_nlines
!= OOBLNO
) {
381 *lnop
= (F_ISSET(sp
, S_INPUT
) &&
382 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> ep
->c_nlines
?
383 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: ep
->c_nlines
);
388 key
.size
= sizeof(lno
);
390 switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
393 "Error: %s/%d: unable to get last line: %s.",
394 tail(__FILE__
), __LINE__
, strerror(errno
));
401 memmove(&lno
, key
.data
, sizeof(lno
));
405 /* Fill the cache. */
406 ep
->c_nlines
= ep
->c_lno
= lno
;
407 ep
->c_len
= data
.size
;
408 ep
->c_lp
= data
.data
;
410 *lnop
= (F_ISSET(sp
, S_INPUT
) &&
411 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
412 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
418 * Update all of the screens that are backed by the file that
422 scr_update(sp
, ep
, lno
, op
, current
)
432 for (tsp
= sp
->gp
->dq
.cqh_first
;
433 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
434 if (sp
!= tsp
&& tsp
->ep
== ep
)
435 (void)sp
->s_change(tsp
, ep
, lno
, op
);
436 return (current
&& sp
->s_change(sp
, ep
, lno
, op
));