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.17 1993/11/19 10:54:36 bostic Exp $ (Berkeley) $Date: 1993/11/19 10:54:36 $";
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. */
36 * The underlying recno stuff handles zero by returning NULL, but
37 * have to have an oob condition for the look-aside into the input
44 * Look-aside into the TEXT buffers and see if the line we want
47 if (F_ISSET(sp
, S_INPUT
) &&
48 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
<= lno
&&
49 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
>= lno
) {
50 for (tp
= sp
->tiq
.cqh_first
;
51 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
56 return (file_rline(sp
, ep
, lno
, lenp
));
61 * Look in the cache for a line; if it's not there retrieve
65 file_rline(sp
, ep
, lno
, lenp
)
68 recno_t lno
; /* Line number. */
69 size_t *lenp
; /* Length store. */
73 /* Check the cache. */
74 if (lno
== ep
->c_lno
) {
81 /* Get the line from the underlying database. */
83 key
.size
= sizeof(lno
);
84 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
87 "Error: %s/%d: unable to get line %u: %s.",
88 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
99 ep
->c_len
= data
.size
;
100 ep
->c_lp
= data
.data
;
107 * Delete a line from the file.
110 file_dline(sp
, ep
, lno
)
117 #if defined(DEBUG) && 0
118 TRACE(sp
, "delete line %lu\n", lno
);
120 /* Delete any marks in the line. */
121 mark_delete(sp
, ep
, lno
);
124 log_line(sp
, ep
, lno
, LOG_LINE_DELETE
);
128 key
.size
= sizeof(lno
);
129 if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
131 "Error: %s/%d: unable to delete line %u: %s.",
132 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
136 /* Flush the cache, update line count, before screen update. */
137 if (lno
<= ep
->c_lno
)
139 if (ep
->c_nlines
!= OOBLNO
)
142 /* File now dirty. */
143 if (F_ISSET(ep
, F_FIRSTMODIFY
))
144 (void)rcv_init(sp
, ep
);
145 F_SET(ep
, F_MODIFIED
);
148 return (scr_update(sp
, ep
, lno
, LINE_DELETE
, 1));
153 * Append a line into the file.
156 file_aline(sp
, ep
, update
, lno
, p
, len
)
167 #if defined(DEBUG) && 0
168 TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
171 * Very nasty special case. The historic vi code displays a single
172 * space (or a '$' if the list option is set) for the first line in
173 * an "empty" file. If we "insert" a line, that line gets scrolled
174 * down, not repainted, so it's incorrect when we refresh the the
175 * screen. This is really hard to find and fix in the vi code -- the
176 * text input functions detect it explicitly and don't insert a new
177 * line. The hack here is to repaint the screen if we're appending
181 if (file_lline(sp
, ep
, &lline
))
189 key
.size
= sizeof(lno
);
192 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
194 "Error: %s/%d: unable to append to line %u: %s.",
195 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
199 /* Flush the cache, update line count, before screen update. */
202 if (ep
->c_nlines
!= OOBLNO
)
205 /* File now dirty. */
206 if (F_ISSET(ep
, F_FIRSTMODIFY
))
207 (void)rcv_init(sp
, ep
);
208 F_SET(ep
, F_MODIFIED
);
211 log_line(sp
, ep
, lno
+ 1, LOG_LINE_APPEND
);
213 /* Push any marks above the line. */
214 mark_insert(sp
, ep
, lno
+ 1);
220 * Nasty hack. If multiple lines are input by the user, they aren't
221 * committed until an <ESC> is entered. The problem is the screen was
222 * updated/scrolled as each line was entered. So, when this routine
223 * is called to copy the new lines from the cut buffer into the file,
224 * it has to know not to update the screen again.
226 return (scr_update(sp
, ep
, lno
, LINE_APPEND
, update
));
231 * Insert a line into the file.
234 file_iline(sp
, ep
, lno
, p
, len
)
244 #if defined(DEBUG) && 0
246 "insert before %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
249 /* Very nasty special case. See comment in file_aline(). */
251 if (file_lline(sp
, ep
, &lline
))
259 key
.size
= sizeof(lno
);
262 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
264 "Error: %s/%d: unable to insert at line %u: %s.",
265 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
269 /* Flush the cache, update line count, before screen update. */
270 if (lno
>= ep
->c_lno
)
272 if (ep
->c_nlines
!= OOBLNO
)
275 /* File now dirty. */
276 if (F_ISSET(ep
, F_FIRSTMODIFY
))
277 (void)rcv_init(sp
, ep
);
278 F_SET(ep
, F_MODIFIED
);
281 log_line(sp
, ep
, lno
, LOG_LINE_INSERT
);
283 /* Push any marks above the line. */
284 mark_insert(sp
, ep
, lno
);
287 return (scr_update(sp
, ep
, lno
, LINE_INSERT
, 1));
292 * Store a line in the file.
295 file_sline(sp
, ep
, lno
, p
, len
)
304 #if defined(DEBUG) && 0
306 "replace line %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
308 /* Log before change. */
309 log_line(sp
, ep
, lno
, LOG_LINE_RESET_B
);
313 key
.size
= sizeof(lno
);
316 if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
318 "Error: %s/%d: unable to store line %u: %s.",
319 tail(__FILE__
), __LINE__
, lno
, strerror(errno
));
323 /* Flush the cache, before logging or screen update. */
324 if (lno
== ep
->c_lno
)
327 /* File now dirty. */
328 if (F_ISSET(ep
, F_FIRSTMODIFY
))
329 (void)rcv_init(sp
, ep
);
330 F_SET(ep
, F_MODIFIED
);
332 /* Log after change. */
333 log_line(sp
, ep
, lno
, LOG_LINE_RESET_F
);
336 return (scr_update(sp
, ep
, lno
, LINE_RESET
, 1));
341 * Return the number of lines in the file.
344 file_lline(sp
, ep
, lnop
)
352 /* Check the cache. */
353 if (ep
->c_nlines
!= OOBLNO
) {
354 *lnop
= (F_ISSET(sp
, S_INPUT
) &&
355 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> ep
->c_nlines
?
356 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: ep
->c_nlines
);
361 key
.size
= sizeof(lno
);
363 switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
366 "Error: %s/%d: unable to get last line: %s.",
367 tail(__FILE__
), __LINE__
, strerror(errno
));
374 memmove(&lno
, key
.data
, sizeof(lno
));
378 /* Fill the cache. */
379 ep
->c_nlines
= ep
->c_lno
= lno
;
380 ep
->c_len
= data
.size
;
381 ep
->c_lp
= data
.data
;
383 *lnop
= (F_ISSET(sp
, S_INPUT
) &&
384 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
385 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
391 * Update all of the screens that are backed by the file that
395 scr_update(sp
, ep
, lno
, op
, current
)
405 for (tsp
= sp
->gp
->dq
.cqh_first
;
406 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
407 if (sp
!= tsp
&& tsp
->ep
== ep
)
408 (void)sp
->s_change(tsp
, ep
, lno
, op
);
409 return (current
&& sp
->s_change(sp
, ep
, lno
, op
));