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.
12 #include <sys/cdefs.h>
15 static const char sccsid
[] = "Id: db.c,v 10.48 2002/06/08 19:32:52 skimo Exp (Berkeley) Date: 2002/06/08 19:32:52 ";
18 __RCSID("$NetBSD: vi_db.c,v 1.6 2014/01/26 21:43:45 christos Exp $");
21 #include <sys/types.h>
22 #include <sys/queue.h>
26 #include <bitstring.h>
35 #include "dbinternal.h"
38 static int append
__P((SCR
*, db_recno_t
, const CHAR_T
*, size_t, lnop_t
, int));
42 * Front-end to db_get, special case handling for empty files.
44 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
47 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
59 /* If the line exists, simply return it. */
60 if (!db_get(sp
, lno
, 0, pp
, lenp
))
64 * If the user asked for line 0 or line 1, i.e. the only possible
65 * line in an empty file, find the last line of the file; db_last
68 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
71 /* If the file isn't empty, fail loudly. */
72 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
85 * Look in the text buffers for a line, followed by the cache, followed
88 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
91 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
92 /* Line number. */ /* Pointer store. */ /* Length store. */
103 * The underlying recno stuff handles zero by returning NULL, but
104 * have to have an OOB condition for the look-aside into the input
110 /* Check for no underlying file. */
111 if ((ep
= sp
->ep
) == NULL
) {
112 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
116 if (LF_ISSET(DBG_NOCACHE
))
120 * Look-aside into the TEXT buffers and see if the line we want
123 if (F_ISSET(sp
, SC_TINPUT
)) {
124 l1
= TAILQ_FIRST(&sp
->tiq
)->lno
;
125 l2
= TAILQ_LAST(&sp
->tiq
, _texth
)->lno
;
126 if (l1
<= lno
&& l2
>= lno
) {
127 #if defined(DEBUG) && 0
129 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
131 for (tp
= TAILQ_FIRST(&sp
->tiq
);
132 tp
->lno
!= lno
; tp
= TAILQ_NEXT(tp
, q
));
140 * Adjust the line number for the number of lines used
141 * by the text input buffers.
147 /* Look-aside into the cache, and see if the line we want is there. */
148 if (lno
== sp
->c_lno
) {
149 #if defined(DEBUG) && 0
150 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
163 /* data.size contains length in bytes */
164 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
166 /* Get the line from the underlying database. */
167 memset(&key
, 0, sizeof(key
));
169 key
.size
= sizeof(lno
);
170 memset(&data
, 0, sizeof(data
));
171 data
.data
= sp
->c_lp
;
172 data
.ulen
= sp
->c_blen
;
173 data
.flags
= DB_DBT_USERMEM
;
174 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
175 case DB_BUFFER_SMALL
:
181 err1
: if (LF_ISSET(DBG_FATAL
))
182 err2
: db_err(sp
, lno
);
184 err3
: if (lenp
!= NULL
)
193 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
194 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
195 F_SET(sp
, SC_CONV_ERROR
);
196 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
201 /* Reset the cache. */
202 if (wp
!= data
.data
) {
203 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
204 MEMCPYW(sp
->c_lp
, wp
, wlen
);
209 #if defined(DEBUG) && 0
210 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
221 * Delete a line from the file.
223 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
226 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 (line_insdel(sp
, LINE_DELETE
, lno
))
248 /* Log before change. */
249 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
252 memset(&key
, 0, sizeof(key
));
254 key
.size
= sizeof(lno
);
255 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
256 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
261 /* Flush the cache, update line count, before screen update. */
262 update_cache(sp
, LINE_DELETE
, lno
);
264 /* File now modified. */
265 if (F_ISSET(ep
, F_FIRSTMODIFY
))
267 F_SET(ep
, F_MODIFIED
);
269 /* Log after change. */
270 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
273 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
276 /* maybe this could be simpler
278 * DB3 behaves differently from DB1
280 * if lno != 0 just go to lno and put the new line after it
281 * if lno == 0 then if there are any record, put in front of the first
282 * otherwise just append to the end thus creating the first
286 append(SCR
*sp
, db_recno_t lno
, const CHAR_T
*p
, size_t len
, lnop_t op
, int update
)
295 /* Check for no underlying file. */
296 if ((ep
= sp
->ep
) == NULL
) {
297 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
300 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
301 ex_emsg(sp
, NULL
, EXM_LOCKED
);
305 /* Log before change. */
306 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
309 memset(&key
, 0, sizeof(key
));
311 key
.size
= sizeof(lno
);
312 memset(&data
, 0, sizeof(data
));
314 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
317 INT2FILE(sp
, p
, len
, fp
, flen
);
320 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
323 data
.data
= __UNCONST(fp
);
325 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
327 (void)dbcp_put
->c_close(dbcp_put
);
330 ? "004|unable to append to line %lu"
331 : "005|unable to insert at line %lu",
336 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
337 if (sp
->db_error
!= DB_NOTFOUND
)
340 data
.data
= __UNCONST(fp
);
342 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
347 key
.size
= sizeof(lno
);
348 data
.data
= __UNCONST(fp
);
350 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
356 (void)dbcp_put
->c_close(dbcp_put
);
358 /* Flush the cache, update line count, before screen update. */
359 update_cache(sp
, LINE_INSERT
, lno
);
361 /* File now dirty. */
362 if (F_ISSET(ep
, F_FIRSTMODIFY
))
364 F_SET(ep
, F_MODIFIED
);
366 /* Log after change. */
367 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
369 /* Update marks, @ and global commands. */
370 rval
= line_insdel(sp
, LINE_INSERT
, lno
+ 1);
375 * comment copied from db_append
377 * Nasty hack. If multiple lines are input by the user, they aren't
378 * committed until an <ESC> is entered. The problem is the screen was
379 * updated/scrolled as each line was entered. So, when this routine
380 * is called to copy the new lines from the cut buffer into the file,
381 * it has to know not to update the screen again.
383 return (scr_update(sp
, lno
+ 1, LINE_INSERT
, update
) || rval
);
388 * Append a line into the file.
390 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
393 db_append(SCR
*sp
, int update
, db_recno_t lno
, const CHAR_T
*p
, size_t len
)
395 #if defined(DEBUG) && 0
396 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
400 return append(sp
, lno
, p
, len
, LINE_APPEND
, update
);
405 * Insert a line into the file.
407 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
410 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
412 #if defined(DEBUG) && 0
413 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
414 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
416 return append(sp
, lno
- 1, p
, len
, LINE_INSERT
, 1);
421 * Store a line in the file.
423 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
426 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
433 #if defined(DEBUG) && 0
434 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
435 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
437 /* Check for no underlying file. */
438 if ((ep
= sp
->ep
) == NULL
) {
439 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
442 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
443 ex_emsg(sp
, NULL
, EXM_LOCKED
);
447 /* Log before change. */
448 log_line(sp
, lno
, LOG_LINE_RESET_B
);
450 INT2FILE(sp
, p
, len
, fp
, flen
);
453 memset(&key
, 0, sizeof(key
));
455 key
.size
= sizeof(lno
);
456 memset(&data
, 0, sizeof(data
));
457 data
.data
= __UNCONST(fp
);
459 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
460 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
464 /* Flush the cache, update line count, before screen update. */
465 update_cache(sp
, LINE_RESET
, lno
);
467 /* File now dirty. */
468 if (F_ISSET(ep
, F_FIRSTMODIFY
))
470 F_SET(ep
, F_MODIFIED
);
472 /* Log after change. */
473 log_line(sp
, lno
, LOG_LINE_RESET_F
);
476 return (scr_update(sp
, lno
, LINE_RESET
, 1));
481 * Return if a line exists.
483 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
486 db_exist(SCR
*sp
, db_recno_t lno
)
490 /* Check for no underlying file. */
491 if ((ep
= sp
->ep
) == NULL
) {
492 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
500 * Check the last-line number cache. Adjust the cached line
501 * number for the lines used by the text input buffers.
503 if (ep
->c_nlines
!= OOBLNO
)
504 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
505 ep
->c_nlines
+ TAILQ_LAST(&sp
->tiq
, _texth
)->lno
-
506 TAILQ_FIRST(&sp
->tiq
)->lno
: ep
->c_nlines
));
508 /* Go get the line. */
509 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
514 * Return the number of lines in the file.
516 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
519 db_last(SCR
*sp
, db_recno_t
*lnop
)
528 /* Check for no underlying file. */
529 if ((ep
= sp
->ep
) == NULL
) {
530 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
535 * Check the last-line number cache. Adjust the cached line
536 * number for the lines used by the text input buffers.
538 if (ep
->c_nlines
!= OOBLNO
) {
539 *lnop
= ep
->c_nlines
;
540 if (F_ISSET(sp
, SC_TINPUT
))
541 *lnop
+= TAILQ_LAST(&sp
->tiq
, _texth
)->lno
-
542 TAILQ_FIRST(&sp
->tiq
)->lno
;
546 memset(&key
, 0, sizeof(key
));
548 key
.size
= sizeof(lno
);
549 memset(&data
, 0, sizeof(data
));
551 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
553 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
558 (void)dbcp
->c_close(dbcp
);
561 msgq(sp
, M_DBERR
, "007|unable to get last line");
568 memcpy(&lno
, key
.data
, sizeof(lno
));
570 if (lno
!= sp
->c_lno
) {
571 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
573 /* Fill the cache. */
574 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
575 MEMCPYW(sp
->c_lp
, wp
, wlen
);
581 (void)dbcp
->c_close(dbcp
);
583 /* Return the value. */
584 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
585 TAILQ_LAST(&sp
->tiq
, _texth
)->lno
> lno
?
586 TAILQ_LAST(&sp
->tiq
, _texth
)->lno
: lno
);
592 * Report a line error.
594 * PUBLIC: void db_err __P((SCR *, db_recno_t));
597 db_err(SCR
*sp
, db_recno_t lno
)
600 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
605 * Update all of the screens that are backed by the file that
608 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
609 * PUBLIC: lnop_t op, int current));
612 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
618 if (F_ISSET(sp
, SC_EX
))
621 /* XXXX goes outside of window */
624 TAILQ_FOREACH(wp
, &sp
->gp
->dq
, q
)
625 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
626 if (sp
!= tsp
&& tsp
->ep
== ep
)
627 if (vs_change(tsp
, lno
, op
))
629 return (current
? vs_change(sp
, lno
, op
) : 0);
633 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
636 update_cache(SCR
*sp
, lnop_t op
, db_recno_t lno
)
643 /* Flush the cache, update line count, before screen update. */
644 /* The flushing is probably not needed, since it was incorrect
645 * for db_insert. It might be better to adjust it, like
646 * marks, @ and global
648 TAILQ_FOREACH(scrp
, &ep
->scrq
, eq
)
652 if (lno
<= scrp
->c_lno
)
653 scrp
->c_lno
= OOBLNO
;
656 if (lno
== scrp
->c_lno
)
657 scrp
->c_lno
= OOBLNO
;
663 if (ep
->c_nlines
!= OOBLNO
)
678 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
681 line_insdel(SCR
*sp
, lnop_t op
, db_recno_t lno
)
685 /* Update marks, @ and global commands. */
687 if (mark_insdel(sp
, op
, lno
))
689 if (ex_g_insdel(sp
, op
, lno
))
695 #ifdef USE_DB4_LOGGING
696 #define VI_DB_INIT_LOG DB_INIT_LOG
698 #define VI_DB_INIT_LOG 0
702 * PUBLIC: int db_setup __P((SCR *, EXF *));
705 db_setup(SCR
*sp
, EXF
*ep
)
707 char path
[MAXPATHLEN
];
711 (void)snprintf(path
, sizeof(path
), "%s/vi.XXXXXX", O_STR(sp
, O_RECDIR
));
712 if ((fd
= mkstemp(path
)) == -1) {
713 msgq(sp
, M_SYSERR
, "%s", path
);
718 if (mkdir(path
, S_IRWXU
)) {
719 msgq(sp
, M_SYSERR
, "%s", path
);
722 if (db_env_create(&env
, 0)) {
723 msgq(sp
, M_ERR
, "env_create");
726 #ifdef USE_DB4_LOGGING
727 if ((sp
->db_error
= vi_db_init_recover(env
))) {
728 msgq(sp
, M_DBERR
, "init_recover");
731 if ((sp
->db_error
= __vi_init_recover(env
))) {
732 msgq(sp
, M_DBERR
, "init_recover");
736 if ((sp
->db_error
= db_env_open(env
, path
,
737 DB_PRIVATE
| DB_CREATE
| DB_INIT_MPOOL
| VI_DB_THREAD
738 | VI_DB_INIT_LOG
, 0)) != 0) {
739 msgq(sp
, M_DBERR
, "env->open");
743 if ((ep
->env_path
= strdup(path
)) == NULL
) {
744 msgq(sp
, M_SYSERR
, NULL
);
754 /* Round up v to the nearest power of 2 */
755 static size_t round_up(size_t v
)
767 * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
769 int db_msg_open(SCR
*sp
, const char *file
, DB
**dbp
)
771 return (sp
->db_error
= db_create(dbp
, 0, 0)) != 0 ||
772 (sp
->db_error
= (*dbp
)->set_re_source(*dbp
, file
)) != 0 ||
773 (sp
->db_error
= db_open(*dbp
, NULL
, DB_RECNO
, 0, 0)) != 0;
777 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
780 db_init(SCR
*sp
, EXF
*ep
, char *rcv_name
, char *oname
, size_t psize
, int *open_err
)
782 if (db_setup(sp
, ep
))
785 /* Open a db structure. */
786 if ((sp
->db_error
= db_create(&ep
->db
, 0, 0)) != 0) {
787 msgq(sp
, M_DBERR
, "db_create");
791 ep
->db
->set_re_delim(ep
->db
, '\n'); /* Always set. */
792 ep
->db
->set_pagesize(ep
->db
, round_up(psize
));
793 ep
->db
->set_flags(ep
->db
, DB_RENUMBER
| DB_SNAPSHOT
);
794 if (rcv_name
== NULL
)
795 ep
->db
->set_re_source(ep
->db
, oname
);
798 * Don't let db use mmap when using fcntl for locking
800 #ifdef HAVE_LOCK_FCNTL
801 #define NOMMAPIFFCNTL DB_NOMMAP
803 #define NOMMAPIFFCNTL 0
806 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
808 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
809 ((rcv_name
== 0) ? DB_TRUNCATE
: 0) | VI_DB_THREAD
| NOMMAPIFFCNTL
,
810 _DB_OPEN_MODE
)) != 0) {
812 M_DBERR
, rcv_name
== NULL
? oname
: rcv_name
, "%s");
815 * Historically, vi permitted users to edit files that couldn't
816 * be read. This isn't useful for single files from a command
817 * line, but it's quite useful for "vi *.c", since you can skip
818 * past files that you can't read.
820 ep
->db
= NULL
; /* Don't close it; it wasn't opened */
826 /* re_source is loaded into the database.
827 * Close it and reopen it in the environment.
829 if ((sp
->db_error
= ep
->db
->close(ep
->db
, 0))) {
830 msgq(sp
, M_DBERR
, "close");
833 if ((sp
->db_error
= db_create(&ep
->db
, ep
->env
, 0)) != 0) {
834 msgq(sp
, M_DBERR
, "db_create 2");
837 if ((sp
->db_error
= db_open(ep
->db
, ep
->rcv_path
, DB_RECNO
,
838 VI_DB_THREAD
| NOMMAPIFFCNTL
, _DB_OPEN_MODE
)) != 0) {
840 M_DBERR
, ep
->rcv_path
, "%s");