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.34 2000/07/22 17:31:18 skimo Exp $ (Berkeley) $Date: 2000/07/22 17:31:18 $";
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(sp
, lno
, pp
, lenp
, isemptyp
)
41 db_recno_t lno
; /* Line number. */
42 CHAR_T
**pp
; /* Pointer store. */
43 size_t *lenp
; /* Length store. */
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(sp
, lno
, flags
, pp
, lenp
)
85 db_recno_t lno
; /* Line number. */
87 CHAR_T
**pp
; /* Pointer store. */
88 size_t *lenp
; /* Length store. */
102 * The underlying recno stuff handles zero by returning NULL, but
103 * have to have an OOB condition for the look-aside into the input
109 /* Check for no underlying file. */
110 if ((ep
= sp
->ep
) == NULL
) {
111 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
115 if (LF_ISSET(DBG_NOCACHE
))
119 * Look-aside into the TEXT buffers and see if the line we want
122 if (F_ISSET(sp
, SC_TINPUT
)) {
123 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
124 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
125 if (l1
<= lno
&& l2
>= lno
) {
126 #if defined(DEBUG) && 0
128 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
130 for (tp
= sp
->tiq
.cqh_first
;
131 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
139 * Adjust the line number for the number of lines used
140 * by the text input buffers.
146 /* Look-aside into the cache, and see if the line we want is there. */
147 /* Disable cache for now */
148 if (0 && lno
== ep
->c_lno
) {
149 #if defined(DEBUG) && 0
150 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
162 GET_SPACE_GOTO(sp
, bp
, blen
, nlen
);
164 retry
: ADD_SPACE_GOTO(sp
, bp
, blen
, nlen
);
167 BINC_GOTO(sp, (char *)ep->c_lp, ep->c_blen, nlen);
170 /* Get the line from the underlying database. */
171 memset(&key
, 0, sizeof(key
));
173 key
.size
= sizeof(lno
);
174 memset(&data
, 0, sizeof(data
));
177 data
.flags
= DB_DBT_USERMEM
;
178 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
185 err1
: if (LF_ISSET(DBG_FATAL
))
186 err2
: db_err(sp
, lno
);
188 err3
: if (lenp
!= NULL
)
193 FREE_SPACE(sp
, bp
, blen
);
199 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
201 /* Reset the cache. */
202 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
203 MEMCPYW(ep
->c_lp
, wp
, wlen
);
207 #if defined(DEBUG) && 0
208 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
215 FREE_SPACE(sp
, bp
, blen
);
221 * Delete a line from the file.
223 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
233 #if defined(DEBUG) && 0
234 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
236 /* Check for no underlying file. */
237 if ((ep
= sp
->ep
) == NULL
) {
238 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
241 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
242 ex_emsg(sp
, NULL
, EXM_LOCKED
);
246 /* Update marks, @ and global commands. */
247 if (mark_insdel(sp
, LINE_DELETE
, lno
))
249 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
253 log_line(sp
, lno
, LOG_LINE_DELETE
);
256 memset(&key
, 0, sizeof(key
));
258 key
.size
= sizeof(lno
);
259 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
260 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
265 /* Flush the cache, update line count, before screen update. */
266 if (lno
<= ep
->c_lno
)
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(sp
, lno
, p
, len
)
304 memset(&key
, 0, sizeof(key
));
306 key
.size
= sizeof(lno
);
307 memset(&data
, 0, sizeof(data
));
309 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
312 INT2FILE(sp
, p
, len
, fp
, flen
);
315 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
320 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
322 (void)dbcp_put
->c_close(dbcp_put
);
326 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
327 if (sp
->db_error
!= DB_NOTFOUND
)
332 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
337 key
.size
= sizeof(lno
);
340 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
346 (void)dbcp_put
->c_close(dbcp_put
);
353 * Append a line into the file.
355 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
358 db_append(sp
, update
, lno
, p
, len
)
368 #if defined(DEBUG) && 0
369 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
371 /* Check for no underlying file. */
372 if ((ep
= sp
->ep
) == NULL
) {
373 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
376 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
377 ex_emsg(sp
, NULL
, EXM_LOCKED
);
382 if (append(sp
, lno
, p
, len
) != 0) {
383 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
388 /* Flush the cache, update line count, before screen update. */
391 if (ep
->c_nlines
!= OOBLNO
)
394 /* File now dirty. */
395 if (F_ISSET(ep
, F_FIRSTMODIFY
))
397 F_SET(ep
, F_MODIFIED
);
400 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
402 /* Update marks, @ and global commands. */
404 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
406 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
413 * Nasty hack. If multiple lines are input by the user, they aren't
414 * committed until an <ESC> is entered. The problem is the screen was
415 * updated/scrolled as each line was entered. So, when this routine
416 * is called to copy the new lines from the cut buffer into the file,
417 * it has to know not to update the screen again.
419 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
424 * Insert a line into the file.
426 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
429 db_insert(sp
, lno
, p
, len
)
440 #if defined(DEBUG) && 0
441 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
442 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
444 /* Check for no underlying file. */
445 if ((ep
= sp
->ep
) == NULL
) {
446 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
449 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
450 ex_emsg(sp
, NULL
, EXM_LOCKED
);
455 if (append(sp
, lno
- 1, p
, len
) != 0) {
456 msgq(sp
, M_DBERR
, "005|unable to insert at line %lu",
461 /* Flush the cache, update line count, before screen update. */
462 if (lno
>= ep
->c_lno
)
464 if (ep
->c_nlines
!= OOBLNO
)
467 /* File now dirty. */
468 if (F_ISSET(ep
, F_FIRSTMODIFY
))
470 F_SET(ep
, F_MODIFIED
);
473 log_line(sp
, lno
, LOG_LINE_INSERT
);
475 /* Update marks, @ and global commands. */
477 if (mark_insdel(sp
, LINE_INSERT
, lno
))
479 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
483 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
488 * Store a line in the file.
490 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
493 db_set(sp
, lno
, p
, len
)
504 #if defined(DEBUG) && 0
505 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
506 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
508 /* Check for no underlying file. */
509 if ((ep
= sp
->ep
) == NULL
) {
510 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
513 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
514 ex_emsg(sp
, NULL
, EXM_LOCKED
);
518 /* Log before change. */
519 log_line(sp
, lno
, LOG_LINE_RESET_B
);
521 INT2FILE(sp
, p
, len
, fp
, flen
);
524 memset(&key
, 0, sizeof(key
));
526 key
.size
= sizeof(lno
);
527 memset(&data
, 0, sizeof(data
));
530 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
531 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
535 /* Flush the cache, before logging or screen update. */
536 if (lno
== ep
->c_lno
)
539 /* File now dirty. */
540 if (F_ISSET(ep
, F_FIRSTMODIFY
))
542 F_SET(ep
, F_MODIFIED
);
544 /* Log after change. */
545 log_line(sp
, lno
, LOG_LINE_RESET_F
);
548 return (scr_update(sp
, lno
, LINE_RESET
, 1));
553 * Return if a line exists.
555 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
564 /* Check for no underlying file. */
565 if ((ep
= sp
->ep
) == NULL
) {
566 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
574 * Check the last-line number cache. Adjust the cached line
575 * number for the lines used by the text input buffers.
577 if (ep
->c_nlines
!= OOBLNO
)
578 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
579 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
580 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
582 /* Go get the line. */
583 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
588 * Return the number of lines in the file.
590 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
604 /* Check for no underlying file. */
605 if ((ep
= sp
->ep
) == NULL
) {
606 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
611 * Check the last-line number cache. Adjust the cached line
612 * number for the lines used by the text input buffers.
614 if (ep
->c_nlines
!= OOBLNO
) {
615 *lnop
= ep
->c_nlines
;
616 if (F_ISSET(sp
, SC_TINPUT
))
617 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
618 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
622 memset(&key
, 0, sizeof(key
));
624 key
.size
= sizeof(lno
);
625 memset(&data
, 0, sizeof(data
));
627 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
629 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
634 (void)dbcp
->c_close(dbcp
);
637 msgq(sp
, M_DBERR
, "007|unable to get last line");
643 (void)dbcp
->c_close(dbcp
);
645 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
647 /* Fill the cache. */
648 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
649 MEMCPYW(ep
->c_lp
, wp
, wlen
);
650 memcpy(&lno
, key
.data
, sizeof(lno
));
651 ep
->c_nlines
= ep
->c_lno
= lno
;
654 /* Return the value. */
655 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
656 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
657 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
663 * Report a line error.
665 * PUBLIC: void db_err __P((SCR *, db_recno_t));
673 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
678 * Update all of the screens that are backed by the file that
682 scr_update(sp
, lno
, op
, current
)
692 if (F_ISSET(sp
, SC_EX
))
695 /* XXXX goes outside of window */
698 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
700 for (tsp
= wp
->scrq
.cqh_first
;
701 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
702 if (sp
!= tsp
&& tsp
->ep
== ep
)
703 if (vs_change(tsp
, lno
, op
))
705 return (current
? vs_change(sp
, lno
, op
) : 0);