2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: db.c,v 10.23 1996/12/17 14:50:09 bostic Exp $ (Berkeley) $Date: 1996/12/17 14:50:09 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 static int scr_update
__P((SCR
*, recno_t
, lnop_t
, int));
33 * Front-end to db_get, special case handling for empty files.
35 * PUBLIC: int db_eget __P((SCR *, recno_t, char **, size_t *, int *));
38 db_eget(sp
, lno
, pp
, lenp
, isemptyp
)
40 recno_t lno
; /* Line number. */
41 char **pp
; /* Pointer store. */
42 size_t *lenp
; /* Length store. */
50 /* If the line exists, simply return it. */
51 if (!db_get(sp
, lno
, 0, pp
, lenp
))
55 * If the user asked for line 0 or line 1, i.e. the only possible
56 * line in an empty file, find the last line of the file; db_last
59 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
62 /* If the file isn't empty, fail loudly. */
63 if (lno
!= 0 && lno
!= 1 || l1
!= 0) {
76 * Look in the text buffers for a line, followed by the cache, followed
79 * PUBLIC: int db_get __P((SCR *, recno_t, u_int32_t, char **, size_t *));
82 db_get(sp
, lno
, flags
, pp
, lenp
)
84 recno_t lno
; /* Line number. */
86 char **pp
; /* Pointer store. */
87 size_t *lenp
; /* Length store. */
95 * The underlying recno stuff handles zero by returning NULL, but
96 * have to have an OOB condition for the look-aside into the input
102 /* Check for no underlying file. */
103 if ((ep
= sp
->ep
) == NULL
) {
104 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
108 if (LF_ISSET(DBG_NOCACHE
))
112 * Look-aside into the TEXT buffers and see if the line we want
115 if (F_ISSET(sp
, SC_TINPUT
)) {
116 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
117 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
118 if (l1
<= lno
&& l2
>= lno
) {
119 #if defined(DEBUG) && 0
121 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
123 for (tp
= sp
->tiq
.cqh_first
;
124 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
132 * Adjust the line number for the number of lines used
133 * by the text input buffers.
139 /* Look-aside into the cache, and see if the line we want is there. */
140 if (lno
== ep
->c_lno
) {
141 #if defined(DEBUG) && 0
142 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
153 /* Get the line from the underlying database. */
155 key
.size
= sizeof(lno
);
156 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
160 err1
: if (LF_ISSET(DBG_FATAL
))
161 err2
: db_err(sp
, lno
);
162 err3
: if (lenp
!= NULL
)
169 /* Reset the cache. */
171 ep
->c_len
= data
.size
;
172 ep
->c_lp
= data
.data
;
174 #if defined(DEBUG) && 0
175 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
186 * Delete a line from the file.
188 * PUBLIC: int db_delete __P((SCR *, recno_t));
198 #if defined(DEBUG) && 0
199 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
201 /* Check for no underlying file. */
202 if ((ep
= sp
->ep
) == NULL
) {
203 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
207 /* Update marks, @ and global commands. */
208 if (mark_insdel(sp
, LINE_DELETE
, lno
))
210 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
214 log_line(sp
, lno
, LOG_LINE_DELETE
);
218 key
.size
= sizeof(lno
);
220 if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
222 "003|unable to delete line %lu", (u_long
)lno
);
227 /* Flush the cache, update line count, before screen update. */
228 if (lno
<= ep
->c_lno
)
230 if (ep
->c_nlines
!= OOBLNO
)
233 /* File now modified. */
234 if (F_ISSET(ep
, F_FIRSTMODIFY
))
236 F_SET(ep
, F_MODIFIED
);
239 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
244 * Append a line into the file.
246 * PUBLIC: int db_append __P((SCR *, int, recno_t, char *, size_t));
249 db_append(sp
, update
, lno
, p
, len
)
260 #if defined(DEBUG) && 0
261 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
263 /* Check for no underlying file. */
264 if ((ep
= sp
->ep
) == NULL
) {
265 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
271 key
.size
= sizeof(lno
);
275 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
277 "004|unable to append to line %lu", (u_long
)lno
);
282 /* Flush the cache, update line count, before screen update. */
285 if (ep
->c_nlines
!= OOBLNO
)
288 /* File now dirty. */
289 if (F_ISSET(ep
, F_FIRSTMODIFY
))
291 F_SET(ep
, F_MODIFIED
);
294 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
296 /* Update marks, @ and global commands. */
298 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
300 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
307 * Nasty hack. If multiple lines are input by the user, they aren't
308 * committed until an <ESC> is entered. The problem is the screen was
309 * updated/scrolled as each line was entered. So, when this routine
310 * is called to copy the new lines from the cut buffer into the file,
311 * it has to know not to update the screen again.
313 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
318 * Insert a line into the file.
320 * PUBLIC: int db_insert __P((SCR *, recno_t, char *, size_t));
323 db_insert(sp
, lno
, p
, len
)
333 #if defined(DEBUG) && 0
334 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
335 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
337 /* Check for no underlying file. */
338 if ((ep
= sp
->ep
) == NULL
) {
339 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
345 key
.size
= sizeof(lno
);
349 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
351 "005|unable to insert at line %lu", (u_long
)lno
);
356 /* Flush the cache, update line count, before screen update. */
357 if (lno
>= ep
->c_lno
)
359 if (ep
->c_nlines
!= OOBLNO
)
362 /* File now dirty. */
363 if (F_ISSET(ep
, F_FIRSTMODIFY
))
365 F_SET(ep
, F_MODIFIED
);
368 log_line(sp
, lno
, LOG_LINE_INSERT
);
370 /* Update marks, @ and global commands. */
372 if (mark_insdel(sp
, LINE_INSERT
, lno
))
374 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
378 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
383 * Store a line in the file.
385 * PUBLIC: int db_set __P((SCR *, recno_t, char *, size_t));
388 db_set(sp
, lno
, p
, len
)
397 #if defined(DEBUG) && 0
398 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
399 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
402 /* Check for no underlying file. */
403 if ((ep
= sp
->ep
) == NULL
) {
404 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
408 /* Log before change. */
409 log_line(sp
, lno
, LOG_LINE_RESET_B
);
413 key
.size
= sizeof(lno
);
417 if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
419 "006|unable to store line %lu", (u_long
)lno
);
424 /* Flush the cache, before logging or screen update. */
425 if (lno
== ep
->c_lno
)
428 /* File now dirty. */
429 if (F_ISSET(ep
, F_FIRSTMODIFY
))
431 F_SET(ep
, F_MODIFIED
);
433 /* Log after change. */
434 log_line(sp
, lno
, LOG_LINE_RESET_F
);
437 return (scr_update(sp
, lno
, LINE_RESET
, 1));
442 * Return if a line exists.
444 * PUBLIC: int db_exist __P((SCR *, recno_t));
453 /* Check for no underlying file. */
454 if ((ep
= sp
->ep
) == NULL
) {
455 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
463 * Check the last-line number cache. Adjust the cached line
464 * number for the lines used by the text input buffers.
466 if (ep
->c_nlines
!= OOBLNO
)
467 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
468 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
469 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
471 /* Go get the line. */
472 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
477 * Return the number of lines in the file.
479 * PUBLIC: int db_last __P((SCR *, recno_t *));
490 /* Check for no underlying file. */
491 if ((ep
= sp
->ep
) == NULL
) {
492 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
497 * Check the last-line number cache. Adjust the cached line
498 * number for the lines used by the text input buffers.
500 if (ep
->c_nlines
!= OOBLNO
) {
501 *lnop
= ep
->c_nlines
;
502 if (F_ISSET(sp
, SC_TINPUT
))
503 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
504 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
509 key
.size
= sizeof(lno
);
511 switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
513 msgq(sp
, M_SYSERR
, "007|unable to get last line");
523 /* Fill the cache. */
524 memcpy(&lno
, key
.data
, sizeof(lno
));
525 ep
->c_nlines
= ep
->c_lno
= lno
;
526 ep
->c_len
= data
.size
;
527 ep
->c_lp
= data
.data
;
529 /* Return the value. */
530 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
531 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
532 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
538 * Report a line error.
540 * PUBLIC: void db_err __P((SCR *, recno_t));
548 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
553 * Update all of the screens that are backed by the file that
557 scr_update(sp
, lno
, op
, current
)
566 if (F_ISSET(sp
, SC_EX
))
571 for (tsp
= sp
->gp
->dq
.cqh_first
;
572 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
573 if (sp
!= tsp
&& tsp
->ep
== ep
)
574 if (vs_change(tsp
, lno
, op
))
576 return (current
? vs_change(sp
, lno
, op
) : 0);