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.41 2001/09/11 20:01:03 skimo Exp $ (Berkeley) $Date: 2001/09/11 20:01: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
)
230 #if defined(DEBUG) && 0
231 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
233 /* Check for no underlying file. */
234 if ((ep
= sp
->ep
) == NULL
) {
235 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
238 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
239 ex_emsg(sp
, NULL
, EXM_LOCKED
);
243 /* Update marks, @ and global commands. */
244 if (mark_insdel(sp
, LINE_DELETE
, lno
))
246 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
250 log_line(sp
, lno
, LOG_LINE_DELETE
);
253 memset(&key
, 0, sizeof(key
));
255 key
.size
= sizeof(lno
);
256 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
257 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
262 /* Flush the cache, update line count, before screen update. */
263 if (lno
<= sp
->c_lno
)
265 if (ep
->c_nlines
!= OOBLNO
)
268 /* File now modified. */
269 if (F_ISSET(ep
, F_FIRSTMODIFY
))
271 F_SET(ep
, F_MODIFIED
);
274 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
277 /* maybe this could be simpler
279 * DB3 behaves differently from DB1
281 * if lno != 0 just go to lno and put the new line after it
282 * if lno == 0 then if there are any record, put in front of the first
283 * otherwise just append to the end thus creating the first
287 append(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
297 memset(&key
, 0, sizeof(key
));
299 key
.size
= sizeof(lno
);
300 memset(&data
, 0, sizeof(data
));
302 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
305 INT2FILE(sp
, p
, len
, fp
, flen
);
308 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
313 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
315 (void)dbcp_put
->c_close(dbcp_put
);
319 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
320 if (sp
->db_error
!= DB_NOTFOUND
)
325 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
330 key
.size
= sizeof(lno
);
333 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
339 (void)dbcp_put
->c_close(dbcp_put
);
346 * Append a line into the file.
348 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
351 db_append(SCR
*sp
, int update
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
356 #if defined(DEBUG) && 0
357 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
359 /* Check for no underlying file. */
360 if ((ep
= sp
->ep
) == NULL
) {
361 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
364 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
365 ex_emsg(sp
, NULL
, EXM_LOCKED
);
370 if (append(sp
, lno
, p
, len
) != 0) {
371 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
376 /* Flush the cache, update line count, before screen update. */
379 if (ep
->c_nlines
!= OOBLNO
)
382 /* File now dirty. */
383 if (F_ISSET(ep
, F_FIRSTMODIFY
))
385 F_SET(ep
, F_MODIFIED
);
388 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
390 /* Update marks, @ and global commands. */
392 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
394 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
401 * Nasty hack. If multiple lines are input by the user, they aren't
402 * committed until an <ESC> is entered. The problem is the screen was
403 * updated/scrolled as each line was entered. So, when this routine
404 * is called to copy the new lines from the cut buffer into the file,
405 * it has to know not to update the screen again.
407 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
412 * Insert a line into the file.
414 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
417 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
424 #if defined(DEBUG) && 0
425 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
426 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
428 /* Check for no underlying file. */
429 if ((ep
= sp
->ep
) == NULL
) {
430 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
433 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
434 ex_emsg(sp
, NULL
, EXM_LOCKED
);
439 if (append(sp
, lno
- 1, p
, len
) != 0) {
440 msgq(sp
, M_DBERR
, "005|unable to insert at line %lu",
445 /* Flush the cache, update line count, before screen update. */
446 if (lno
>= sp
->c_lno
)
448 if (ep
->c_nlines
!= OOBLNO
)
451 /* File now dirty. */
452 if (F_ISSET(ep
, F_FIRSTMODIFY
))
454 F_SET(ep
, F_MODIFIED
);
457 log_line(sp
, lno
, LOG_LINE_INSERT
);
459 /* Update marks, @ and global commands. */
461 if (mark_insdel(sp
, LINE_INSERT
, lno
))
463 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
467 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
472 * Store a line in the file.
474 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
477 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
485 #if defined(DEBUG) && 0
486 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
487 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
489 /* Check for no underlying file. */
490 if ((ep
= sp
->ep
) == NULL
) {
491 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
494 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
495 ex_emsg(sp
, NULL
, EXM_LOCKED
);
499 /* Log before change. */
500 log_line(sp
, lno
, LOG_LINE_RESET_B
);
502 INT2FILE(sp
, p
, len
, fp
, flen
);
505 memset(&key
, 0, sizeof(key
));
507 key
.size
= sizeof(lno
);
508 memset(&data
, 0, sizeof(data
));
511 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
512 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
516 /* Flush the cache, before logging or screen update. */
517 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
518 scrp
= scrp
->eq
.cqe_next
)
519 if (lno
== scrp
->c_lno
)
520 scrp
->c_lno
= OOBLNO
;
522 /* File now dirty. */
523 if (F_ISSET(ep
, F_FIRSTMODIFY
))
525 F_SET(ep
, F_MODIFIED
);
527 /* Log after change. */
528 log_line(sp
, lno
, LOG_LINE_RESET_F
);
531 return (scr_update(sp
, lno
, LINE_RESET
, 1));
536 * Return if a line exists.
538 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
541 db_exist(SCR
*sp
, db_recno_t lno
)
545 /* Check for no underlying file. */
546 if ((ep
= sp
->ep
) == NULL
) {
547 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
555 * Check the last-line number cache. Adjust the cached line
556 * number for the lines used by the text input buffers.
558 if (ep
->c_nlines
!= OOBLNO
)
559 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
560 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
561 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
563 /* Go get the line. */
564 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
569 * Return the number of lines in the file.
571 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
574 db_last(SCR
*sp
, db_recno_t
*lnop
)
583 /* Check for no underlying file. */
584 if ((ep
= sp
->ep
) == NULL
) {
585 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
590 * Check the last-line number cache. Adjust the cached line
591 * number for the lines used by the text input buffers.
593 if (ep
->c_nlines
!= OOBLNO
) {
594 *lnop
= ep
->c_nlines
;
595 if (F_ISSET(sp
, SC_TINPUT
))
596 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
597 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
601 memset(&key
, 0, sizeof(key
));
603 key
.size
= sizeof(lno
);
604 memset(&data
, 0, sizeof(data
));
606 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
608 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
613 (void)dbcp
->c_close(dbcp
);
616 msgq(sp
, M_DBERR
, "007|unable to get last line");
623 memcpy(&lno
, key
.data
, sizeof(lno
));
625 if (lno
!= sp
->c_lno
) {
626 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
628 /* Fill the cache. */
629 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
630 MEMCPYW(sp
->c_lp
, wp
, wlen
);
636 (void)dbcp
->c_close(dbcp
);
638 /* Return the value. */
639 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
640 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
641 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
647 * Report a line error.
649 * PUBLIC: void db_err __P((SCR *, db_recno_t));
652 db_err(SCR
*sp
, db_recno_t lno
)
655 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
660 * Update all of the screens that are backed by the file that
664 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
670 if (F_ISSET(sp
, SC_EX
))
673 /* XXXX goes outside of window */
676 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
678 for (tsp
= wp
->scrq
.cqh_first
;
679 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
680 if (sp
!= tsp
&& tsp
->ep
== ep
)
681 if (vs_change(tsp
, lno
, op
))
683 return (current
? vs_change(sp
, lno
, op
) : 0);