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.37 2001/05/10 19:32:47 skimo Exp $ (Berkeley) $Date: 2001/05/10 19:32:47 $";
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. */
99 * The underlying recno stuff handles zero by returning NULL, but
100 * have to have an OOB condition for the look-aside into the input
106 /* Check for no underlying file. */
107 if ((ep
= sp
->ep
) == NULL
) {
108 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
112 if (LF_ISSET(DBG_NOCACHE
))
116 * Look-aside into the TEXT buffers and see if the line we want
119 if (F_ISSET(sp
, SC_TINPUT
)) {
120 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
121 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
122 if (l1
<= lno
&& l2
>= lno
) {
123 #if defined(DEBUG) && 0
125 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
127 for (tp
= sp
->tiq
.cqh_first
;
128 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
136 * Adjust the line number for the number of lines used
137 * by the text input buffers.
143 /* Look-aside into the cache, and see if the line we want is there. */
145 * Line cache will not work if different screens view the same
146 * file with different encodings.
147 * Multiple threads accessing the same cache can be a problem as
149 * So, line cache is (temporarily) disabled.
151 if (0 && lno
== ep
->c_lno
) {
152 #if defined(DEBUG) && 0
153 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
166 /* data.size contains length in bytes */
167 BINC_GOTO(sp
, (char *)ep
->c_lp
, ep
->c_blen
, nlen
);
169 /* Get the line from the underlying database. */
170 memset(&key
, 0, sizeof(key
));
172 key
.size
= sizeof(lno
);
173 memset(&data
, 0, sizeof(data
));
174 data
.data
= ep
->c_lp
;
175 data
.ulen
= ep
->c_blen
;
176 data
.flags
= DB_DBT_USERMEM
;
177 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
184 err1
: if (LF_ISSET(DBG_FATAL
))
185 err2
: db_err(sp
, lno
);
187 err3
: if (lenp
!= NULL
)
196 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
197 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
198 F_SET(sp
, SC_CONV_ERROR
);
199 msgq(sp
, M_ERR
, "321|Conversion error on line %d", lno
);
204 /* Reset the cache. */
205 if (wp
!= data
.data
) {
206 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
207 MEMCPYW(ep
->c_lp
, wp
, wlen
);
212 #if defined(DEBUG) && 0
213 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
224 * Delete a line from the file.
226 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
236 #if defined(DEBUG) && 0
237 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
239 /* Check for no underlying file. */
240 if ((ep
= sp
->ep
) == NULL
) {
241 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
244 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
245 ex_emsg(sp
, NULL
, EXM_LOCKED
);
249 /* Update marks, @ and global commands. */
250 if (mark_insdel(sp
, LINE_DELETE
, lno
))
252 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
256 log_line(sp
, lno
, LOG_LINE_DELETE
);
259 memset(&key
, 0, sizeof(key
));
261 key
.size
= sizeof(lno
);
262 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
263 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
268 /* Flush the cache, update line count, before screen update. */
269 if (lno
<= ep
->c_lno
)
271 if (ep
->c_nlines
!= OOBLNO
)
274 /* File now modified. */
275 if (F_ISSET(ep
, F_FIRSTMODIFY
))
277 F_SET(ep
, F_MODIFIED
);
280 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
283 /* maybe this could be simpler
285 * DB3 behaves differently from DB1
287 * if lno != 0 just go to lno and put the new line after it
288 * if lno == 0 then if there are any record, put in front of the first
289 * otherwise just append to the end thus creating the first
293 append(sp
, lno
, p
, len
)
307 memset(&key
, 0, sizeof(key
));
309 key
.size
= sizeof(lno
);
310 memset(&data
, 0, sizeof(data
));
312 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
315 INT2FILE(sp
, p
, len
, fp
, flen
);
318 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
323 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
325 (void)dbcp_put
->c_close(dbcp_put
);
329 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
330 if (sp
->db_error
!= DB_NOTFOUND
)
335 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
340 key
.size
= sizeof(lno
);
343 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
349 (void)dbcp_put
->c_close(dbcp_put
);
356 * Append a line into the file.
358 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
361 db_append(sp
, update
, lno
, p
, len
)
371 #if defined(DEBUG) && 0
372 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
374 /* Check for no underlying file. */
375 if ((ep
= sp
->ep
) == NULL
) {
376 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
379 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
380 ex_emsg(sp
, NULL
, EXM_LOCKED
);
385 if (append(sp
, lno
, p
, len
) != 0) {
386 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
391 /* Flush the cache, update line count, before screen update. */
394 if (ep
->c_nlines
!= OOBLNO
)
397 /* File now dirty. */
398 if (F_ISSET(ep
, F_FIRSTMODIFY
))
400 F_SET(ep
, F_MODIFIED
);
403 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
405 /* Update marks, @ and global commands. */
407 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
409 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
416 * Nasty hack. If multiple lines are input by the user, they aren't
417 * committed until an <ESC> is entered. The problem is the screen was
418 * updated/scrolled as each line was entered. So, when this routine
419 * is called to copy the new lines from the cut buffer into the file,
420 * it has to know not to update the screen again.
422 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
427 * Insert a line into the file.
429 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
432 db_insert(sp
, lno
, p
, len
)
443 #if defined(DEBUG) && 0
444 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
445 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
447 /* Check for no underlying file. */
448 if ((ep
= sp
->ep
) == NULL
) {
449 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
452 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
453 ex_emsg(sp
, NULL
, EXM_LOCKED
);
458 if (append(sp
, lno
- 1, p
, len
) != 0) {
459 msgq(sp
, M_DBERR
, "005|unable to insert at line %lu",
464 /* Flush the cache, update line count, before screen update. */
465 if (lno
>= ep
->c_lno
)
467 if (ep
->c_nlines
!= OOBLNO
)
470 /* File now dirty. */
471 if (F_ISSET(ep
, F_FIRSTMODIFY
))
473 F_SET(ep
, F_MODIFIED
);
476 log_line(sp
, lno
, LOG_LINE_INSERT
);
478 /* Update marks, @ and global commands. */
480 if (mark_insdel(sp
, LINE_INSERT
, lno
))
482 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
486 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
491 * Store a line in the file.
493 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
496 db_set(sp
, lno
, p
, len
)
507 #if defined(DEBUG) && 0
508 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
509 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
511 /* Check for no underlying file. */
512 if ((ep
= sp
->ep
) == NULL
) {
513 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
516 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
517 ex_emsg(sp
, NULL
, EXM_LOCKED
);
521 /* Log before change. */
522 log_line(sp
, lno
, LOG_LINE_RESET_B
);
524 INT2FILE(sp
, p
, len
, fp
, flen
);
527 memset(&key
, 0, sizeof(key
));
529 key
.size
= sizeof(lno
);
530 memset(&data
, 0, sizeof(data
));
533 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
534 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
538 /* Flush the cache, before logging or screen update. */
539 if (lno
== ep
->c_lno
)
542 /* File now dirty. */
543 if (F_ISSET(ep
, F_FIRSTMODIFY
))
545 F_SET(ep
, F_MODIFIED
);
547 /* Log after change. */
548 log_line(sp
, lno
, LOG_LINE_RESET_F
);
551 return (scr_update(sp
, lno
, LINE_RESET
, 1));
556 * Return if a line exists.
558 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
567 /* Check for no underlying file. */
568 if ((ep
= sp
->ep
) == NULL
) {
569 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
577 * Check the last-line number cache. Adjust the cached line
578 * number for the lines used by the text input buffers.
580 if (ep
->c_nlines
!= OOBLNO
)
581 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
582 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
583 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
585 /* Go get the line. */
586 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
591 * Return the number of lines in the file.
593 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
607 /* Check for no underlying file. */
608 if ((ep
= sp
->ep
) == NULL
) {
609 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
614 * Check the last-line number cache. Adjust the cached line
615 * number for the lines used by the text input buffers.
617 if (ep
->c_nlines
!= OOBLNO
) {
618 *lnop
= ep
->c_nlines
;
619 if (F_ISSET(sp
, SC_TINPUT
))
620 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
621 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
625 memset(&key
, 0, sizeof(key
));
627 key
.size
= sizeof(lno
);
628 memset(&data
, 0, sizeof(data
));
630 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
632 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
637 (void)dbcp
->c_close(dbcp
);
640 msgq(sp
, M_DBERR
, "007|unable to get last line");
647 memcpy(&lno
, key
.data
, sizeof(lno
));
649 if (lno
!= ep
->c_lno
) {
650 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
652 /* Fill the cache. */
653 if (wp
!= data
.data
) {
654 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
655 MEMCPYW(ep
->c_lp
, wp
, wlen
);
662 (void)dbcp
->c_close(dbcp
);
664 /* Return the value. */
665 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
666 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
667 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
673 * Report a line error.
675 * PUBLIC: void db_err __P((SCR *, db_recno_t));
683 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
688 * Update all of the screens that are backed by the file that
692 scr_update(sp
, lno
, op
, current
)
702 if (F_ISSET(sp
, SC_EX
))
705 /* XXXX goes outside of window */
708 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
710 for (tsp
= wp
->scrq
.cqh_first
;
711 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
712 if (sp
!= tsp
&& tsp
->ep
== ep
)
713 if (vs_change(tsp
, lno
, op
))
715 return (current
? vs_change(sp
, lno
, op
) : 0);