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.42 2001/10/11 19:19:03 skimo Exp $ (Berkeley) $Date: 2001/10/11 19:19:03 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 static int scr_update
__P((SCR
*, db_recno_t
, lnop_t
, int));
30 static int append
__P((SCR
*, db_recno_t
, CHAR_T
*, size_t));
34 * Front-end to db_get, special case handling for empty files.
36 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
39 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
51 /* If the line exists, simply return it. */
52 if (!db_get(sp
, lno
, 0, pp
, lenp
))
56 * If the user asked for line 0 or line 1, i.e. the only possible
57 * line in an empty file, find the last line of the file; db_last
60 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
63 /* If the file isn't empty, fail loudly. */
64 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
77 * Look in the text buffers for a line, followed by the cache, followed
80 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
83 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
84 /* Line number. */ /* Pointer store. */ /* 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. */
141 * Line cache will not work if different screens view the same
142 * file with different encodings.
143 * Multiple threads accessing the same cache can be a problem as
145 * So, line cache is (temporarily) disabled.
147 if (lno
== sp
->c_lno
) {
148 #if defined(DEBUG) && 0
149 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
162 /* data.size contains length in bytes */
163 BINC_GOTO(sp
, (char *)sp
->c_lp
, sp
->c_blen
, nlen
);
165 /* Get the line from the underlying database. */
166 memset(&key
, 0, sizeof(key
));
168 key
.size
= sizeof(lno
);
169 memset(&data
, 0, sizeof(data
));
170 data
.data
= sp
->c_lp
;
171 data
.ulen
= sp
->c_blen
;
172 data
.flags
= DB_DBT_USERMEM
;
173 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
180 err1
: if (LF_ISSET(DBG_FATAL
))
181 err2
: db_err(sp
, lno
);
183 err3
: if (lenp
!= NULL
)
192 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
193 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
194 F_SET(sp
, SC_CONV_ERROR
);
195 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
200 /* Reset the cache. */
201 if (wp
!= data
.data
) {
202 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
203 MEMCPYW(sp
->c_lp
, wp
, wlen
);
208 #if defined(DEBUG) && 0
209 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
220 * Delete a line from the file.
222 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
225 db_delete(SCR
*sp
, db_recno_t lno
)
231 #if defined(DEBUG) && 0
232 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
234 /* Check for no underlying file. */
235 if ((ep
= sp
->ep
) == NULL
) {
236 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
239 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
240 ex_emsg(sp
, NULL
, EXM_LOCKED
);
244 /* Update marks, @ and global commands. */
245 if (mark_insdel(sp
, LINE_DELETE
, lno
))
247 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
251 log_line(sp
, lno
, LOG_LINE_DELETE
);
254 memset(&key
, 0, sizeof(key
));
256 key
.size
= sizeof(lno
);
257 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
258 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
263 /* Flush the cache, update line count, before screen update. */
264 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
265 scrp
= scrp
->eq
.cqe_next
)
266 if (lno
<= scrp
->c_lno
)
267 scrp
->c_lno
= OOBLNO
;
268 if (ep
->c_nlines
!= OOBLNO
)
271 /* File now modified. */
272 if (F_ISSET(ep
, F_FIRSTMODIFY
))
274 F_SET(ep
, F_MODIFIED
);
277 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
280 /* maybe this could be simpler
282 * DB3 behaves differently from DB1
284 * if lno != 0 just go to lno and put the new line after it
285 * if lno == 0 then if there are any record, put in front of the first
286 * otherwise just append to the end thus creating the first
290 append(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
300 memset(&key
, 0, sizeof(key
));
302 key
.size
= sizeof(lno
);
303 memset(&data
, 0, sizeof(data
));
305 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
308 INT2FILE(sp
, p
, len
, fp
, flen
);
311 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
316 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
318 (void)dbcp_put
->c_close(dbcp_put
);
322 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
323 if (sp
->db_error
!= DB_NOTFOUND
)
328 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
333 key
.size
= sizeof(lno
);
336 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
342 (void)dbcp_put
->c_close(dbcp_put
);
349 * Append a line into the file.
351 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
354 db_append(SCR
*sp
, int update
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
360 #if defined(DEBUG) && 0
361 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
363 /* Check for no underlying file. */
364 if ((ep
= sp
->ep
) == NULL
) {
365 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
368 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
369 ex_emsg(sp
, NULL
, EXM_LOCKED
);
374 if (append(sp
, lno
, p
, len
) != 0) {
375 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
380 /* Flush the cache, update line count, before screen update. */
381 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
382 scrp
= scrp
->eq
.cqe_next
)
383 if (lno
< scrp
->c_lno
)
384 scrp
->c_lno
= OOBLNO
;
385 if (ep
->c_nlines
!= OOBLNO
)
388 /* File now dirty. */
389 if (F_ISSET(ep
, F_FIRSTMODIFY
))
391 F_SET(ep
, F_MODIFIED
);
394 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
396 /* Update marks, @ and global commands. */
398 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
400 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
407 * Nasty hack. If multiple lines are input by the user, they aren't
408 * committed until an <ESC> is entered. The problem is the screen was
409 * updated/scrolled as each line was entered. So, when this routine
410 * is called to copy the new lines from the cut buffer into the file,
411 * it has to know not to update the screen again.
413 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
418 * Insert a line into the file.
420 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
423 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
431 #if defined(DEBUG) && 0
432 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
433 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
435 /* Check for no underlying file. */
436 if ((ep
= sp
->ep
) == NULL
) {
437 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
440 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
441 ex_emsg(sp
, NULL
, EXM_LOCKED
);
446 if (append(sp
, lno
- 1, p
, len
) != 0) {
447 msgq(sp
, M_DBERR
, "005|unable to insert at line %lu",
452 /* Flush the cache, update line count, before screen update. */
453 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
454 scrp
= scrp
->eq
.cqe_next
)
455 if (lno
>= scrp
->c_lno
)
456 scrp
->c_lno
= OOBLNO
;
457 if (ep
->c_nlines
!= OOBLNO
)
460 /* File now dirty. */
461 if (F_ISSET(ep
, F_FIRSTMODIFY
))
463 F_SET(ep
, F_MODIFIED
);
466 log_line(sp
, lno
, LOG_LINE_INSERT
);
468 /* Update marks, @ and global commands. */
470 if (mark_insdel(sp
, LINE_INSERT
, lno
))
472 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
476 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
481 * Store a line in the file.
483 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
486 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
494 #if defined(DEBUG) && 0
495 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
496 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
498 /* Check for no underlying file. */
499 if ((ep
= sp
->ep
) == NULL
) {
500 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
503 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
504 ex_emsg(sp
, NULL
, EXM_LOCKED
);
508 /* Log before change. */
509 log_line(sp
, lno
, LOG_LINE_RESET_B
);
511 INT2FILE(sp
, p
, len
, fp
, flen
);
514 memset(&key
, 0, sizeof(key
));
516 key
.size
= sizeof(lno
);
517 memset(&data
, 0, sizeof(data
));
520 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
521 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
525 /* Flush the cache, before logging or screen update. */
526 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
527 scrp
= scrp
->eq
.cqe_next
)
528 if (lno
== scrp
->c_lno
)
529 scrp
->c_lno
= OOBLNO
;
531 /* File now dirty. */
532 if (F_ISSET(ep
, F_FIRSTMODIFY
))
534 F_SET(ep
, F_MODIFIED
);
536 /* Log after change. */
537 log_line(sp
, lno
, LOG_LINE_RESET_F
);
540 return (scr_update(sp
, lno
, LINE_RESET
, 1));
545 * Return if a line exists.
547 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
550 db_exist(SCR
*sp
, db_recno_t lno
)
554 /* Check for no underlying file. */
555 if ((ep
= sp
->ep
) == NULL
) {
556 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
564 * Check the last-line number cache. Adjust the cached line
565 * number for the lines used by the text input buffers.
567 if (ep
->c_nlines
!= OOBLNO
)
568 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
569 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
570 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
572 /* Go get the line. */
573 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
578 * Return the number of lines in the file.
580 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
583 db_last(SCR
*sp
, db_recno_t
*lnop
)
592 /* Check for no underlying file. */
593 if ((ep
= sp
->ep
) == NULL
) {
594 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
599 * Check the last-line number cache. Adjust the cached line
600 * number for the lines used by the text input buffers.
602 if (ep
->c_nlines
!= OOBLNO
) {
603 *lnop
= ep
->c_nlines
;
604 if (F_ISSET(sp
, SC_TINPUT
))
605 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
606 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
610 memset(&key
, 0, sizeof(key
));
612 key
.size
= sizeof(lno
);
613 memset(&data
, 0, sizeof(data
));
615 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
617 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
622 (void)dbcp
->c_close(dbcp
);
625 msgq(sp
, M_DBERR
, "007|unable to get last line");
632 memcpy(&lno
, key
.data
, sizeof(lno
));
634 if (lno
!= sp
->c_lno
) {
635 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
637 /* Fill the cache. */
638 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
639 MEMCPYW(sp
->c_lp
, wp
, wlen
);
645 (void)dbcp
->c_close(dbcp
);
647 /* Return the value. */
648 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
649 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
650 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
656 * Report a line error.
658 * PUBLIC: void db_err __P((SCR *, db_recno_t));
661 db_err(SCR
*sp
, db_recno_t lno
)
664 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
669 * Update all of the screens that are backed by the file that
673 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
679 if (F_ISSET(sp
, SC_EX
))
682 /* XXXX goes outside of window */
685 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
687 for (tsp
= wp
->scrq
.cqh_first
;
688 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
689 if (sp
!= tsp
&& tsp
->ep
== ep
)
690 if (vs_change(tsp
, lno
, op
))
692 return (current
? vs_change(sp
, lno
, op
) : 0);