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: db1.c,v 10.1 2002/03/09 12:53:57 skimo Exp (Berkeley) Date: 2002/03/09 12:53:57 ";
18 __RCSID("$NetBSD: vi_db1.c,v 1.7 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"
39 * Front-end to db_get, special case handling for empty files.
41 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
44 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
56 /* If the line exists, simply return it. */
57 if (!db_get(sp
, lno
, 0, pp
, lenp
))
61 * If the user asked for line 0 or line 1, i.e. the only possible
62 * line in an empty file, find the last line of the file; db_last
65 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
68 /* If the file isn't empty, fail loudly. */
69 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
82 * Look in the text buffers for a line, followed by the cache, followed
85 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
88 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
104 * The underlying recno stuff handles zero by returning NULL, but
105 * have to have an OOB condition for the look-aside into the input
111 /* Check for no underlying file. */
112 if ((ep
= sp
->ep
) == NULL
) {
113 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
117 if (LF_ISSET(DBG_NOCACHE
))
121 * Look-aside into the TEXT buffers and see if the line we want
124 if (F_ISSET(sp
, SC_TINPUT
)) {
125 l1
= TAILQ_FIRST(&sp
->tiq
)->lno
;
126 l2
= TAILQ_LAST(&sp
->tiq
, _texth
)->lno
;
127 if (l1
<= lno
&& l2
>= lno
) {
128 #if defined(DBDEBUG) && defined(TRACE)
130 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
132 for (tp
= TAILQ_FIRST(&sp
->tiq
);
133 tp
->lno
!= lno
; tp
= TAILQ_NEXT(tp
, q
));
141 * Adjust the line number for the number of lines used
142 * by the text input buffers.
148 /* Look-aside into the cache, and see if the line we want is there. */
149 if (lno
== sp
->c_lno
) {
150 #if defined(DBDEBUG) && defined(TRACE)
151 vtrace("retrieve cached line %lu\n", (u_long
)lno
);
164 /* data.size contains length in bytes */
165 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
167 /* Get the line from the underlying database. */
169 key
.size
= sizeof(lno
);
170 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
174 err1
: if (LF_ISSET(DBG_FATAL
))
175 err2
: db_err(sp
, lno
);
177 err3
: if (lenp
!= NULL
)
183 if (data
.size
> nlen
) {
187 memcpy(sp
->c_lp
, data
.data
, nlen
);
190 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
191 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
192 F_SET(sp
, SC_CONV_ERROR
);
193 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
198 /* Reset the cache. */
199 if (wp
!= data
.data
) {
200 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
201 MEMCPYW(sp
->c_lp
, wp
, wlen
);
206 #if defined(DBDEBUG) && defined(TRACE)
207 vtrace("retrieve DB line %lu\n", (u_long
)lno
);
218 * Delete a line from the file.
220 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
223 db_delete(SCR
*sp
, db_recno_t lno
)
228 #if defined(DBDEBUG) && defined(TRACE)
229 vtrace("delete line %lu\n", (u_long
)lno
);
231 /* Check for no underlying file. */
232 if ((ep
= sp
->ep
) == NULL
) {
233 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
236 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
237 ex_emsg(sp
, NULL
, EXM_LOCKED
);
241 /* Update marks, @ and global commands. */
242 if (mark_insdel(sp
, LINE_DELETE
, lno
))
244 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
248 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
252 key
.size
= sizeof(lno
);
253 sp
->db_error
= ep
->db
->del(ep
->db
, &key
, 0);
254 if (sp
->db_error
!= 0) {
255 if (sp
->db_error
== -1)
256 sp
->db_error
= errno
;
258 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
263 /* Flush the cache, update line count, before screen update. */
264 update_cache(sp
, LINE_DELETE
, lno
);
266 /* File now modified. */
267 if (F_ISSET(ep
, F_FIRSTMODIFY
))
269 F_SET(ep
, F_MODIFIED
);
271 /* Log after change. */
272 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
275 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
280 * Append a line into the file.
282 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
285 db_append(SCR
*sp
, int update
, db_recno_t lno
, const CHAR_T
*p
, size_t len
)
293 #if defined(DBDEBUG) && defined(TRACE)
294 vtrace("append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
296 /* Check for no underlying file. */
297 if ((ep
= sp
->ep
) == NULL
) {
298 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
301 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
302 ex_emsg(sp
, NULL
, EXM_LOCKED
);
306 /* Log before change. */
307 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
309 INT2FILE(sp
, p
, len
, fp
, flen
);
313 key
.size
= sizeof(lno
);
314 data
.data
= __UNCONST(fp
);
316 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
)) {
317 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
322 /* Flush the cache, update line count, before screen update. */
323 update_cache(sp
, LINE_INSERT
, lno
);
325 /* File now dirty. */
326 if (F_ISSET(ep
, F_FIRSTMODIFY
))
328 F_SET(ep
, F_MODIFIED
);
330 /* Log after change. */
331 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
333 /* Update marks, @ and global commands. */
335 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
337 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
344 * Nasty hack. If multiple lines are input by the user, they aren't
345 * committed until an <ESC> is entered. The problem is the screen was
346 * updated/scrolled as each line was entered. So, when this routine
347 * is called to copy the new lines from the cut buffer into the file,
348 * it has to know not to update the screen again.
350 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
355 * Insert a line into the file.
357 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
360 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
368 #if defined(DBDEBUG) && defined(TRACE)
369 vtrace("insert before %lu: len %lu {%.*s}\n",
370 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
372 /* Check for no underlying file. */
373 if ((ep
= sp
->ep
) == NULL
) {
374 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
377 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
378 ex_emsg(sp
, NULL
, EXM_LOCKED
);
382 /* Log before change. */
383 log_line(sp
, lno
, LOG_LINE_APPEND_B
);
385 INT2FILE(sp
, p
, len
, fp
, flen
);
389 key
.size
= sizeof(lno
);
390 data
.data
= __UNCONST(fp
);
392 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
)) {
394 "005|unable to insert at line %lu", (u_long
)lno
);
398 /* Flush the cache, update line count, before screen update. */
399 update_cache(sp
, LINE_INSERT
, lno
);
401 /* File now dirty. */
402 if (F_ISSET(ep
, F_FIRSTMODIFY
))
404 F_SET(ep
, F_MODIFIED
);
406 /* Log after change. */
407 log_line(sp
, lno
, LOG_LINE_APPEND_F
);
409 /* Update marks, @ and global commands. */
411 if (mark_insdel(sp
, LINE_INSERT
, lno
))
413 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
417 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
422 * Store a line in the file.
424 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
427 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
434 #if defined(DBDEBUG) && defined(TRACE)
435 vtrace("replace line %lu: len %lu {%.*s}\n",
436 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
438 /* Check for no underlying file. */
439 if ((ep
= sp
->ep
) == NULL
) {
440 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
443 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
444 ex_emsg(sp
, NULL
, EXM_LOCKED
);
448 /* Log before change. */
449 log_line(sp
, lno
, LOG_LINE_RESET_B
);
451 INT2FILE(sp
, p
, len
, fp
, flen
);
455 key
.size
= sizeof(lno
);
456 data
.data
= __UNCONST(fp
);
459 ep
->db
->put(ep
->db
, &key
, &data
, 0);
460 if (sp
->db_error
!= 0) {
461 if (sp
->db_error
== -1)
462 sp
->db_error
= errno
;
464 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
468 /* Flush the cache, before logging or screen update. */
469 update_cache(sp
, LINE_RESET
, lno
);
471 /* File now dirty. */
472 if (F_ISSET(ep
, F_FIRSTMODIFY
))
474 F_SET(ep
, F_MODIFIED
);
476 /* Log after change. */
477 log_line(sp
, lno
, LOG_LINE_RESET_F
);
480 return (scr_update(sp
, lno
, LINE_RESET
, 1));
485 * Return if a line exists.
487 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
490 db_exist(SCR
*sp
, db_recno_t lno
)
494 /* Check for no underlying file. */
495 if ((ep
= sp
->ep
) == NULL
) {
496 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
504 * Check the last-line number cache. Adjust the cached line
505 * number for the lines used by the text input buffers.
507 if (ep
->c_nlines
!= OOBLNO
)
508 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
509 ep
->c_nlines
+ (TAILQ_LAST(&sp
->tiq
, _texth
)->lno
-
510 TAILQ_FIRST(&sp
->tiq
)->lno
) : ep
->c_nlines
));
512 /* Go get the line. */
513 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
518 * Return the number of lines in the file.
520 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
523 db_last(SCR
*sp
, db_recno_t
*lnop
)
531 /* Check for no underlying file. */
532 if ((ep
= sp
->ep
) == NULL
) {
533 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
538 * Check the last-line number cache. Adjust the cached line
539 * number for the lines used by the text input buffers.
541 if (ep
->c_nlines
!= OOBLNO
) {
542 *lnop
= ep
->c_nlines
;
543 if (F_ISSET(sp
, SC_TINPUT
))
544 *lnop
+= TAILQ_LAST(&sp
->tiq
, _texth
)->lno
-
545 TAILQ_FIRST(&sp
->tiq
)->lno
;
550 key
.size
= sizeof(lno
);
552 sp
->db_error
= ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
);
553 switch (sp
->db_error
) {
558 sp
->db_error
= errno
;
560 msgq(sp
, M_DBERR
, "007|unable to get last line");
567 memcpy(&lno
, key
.data
, sizeof(lno
));
569 if (lno
!= sp
->c_lno
) {
570 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
572 /* Fill the cache. */
573 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
574 MEMCPYW(sp
->c_lp
, wp
, wlen
);
580 /* Return the value. */
581 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
582 TAILQ_LAST(&sp
->tiq
, _texth
)->lno
> lno
?
583 TAILQ_LAST(&sp
->tiq
, _texth
)->lno
: lno
);
589 * Report a line error.
591 * PUBLIC: void db_err __P((SCR *, db_recno_t));
594 db_err(SCR
*sp
, db_recno_t lno
)
597 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
602 * Update all of the screens that are backed by the file that
604 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
605 * PUBLIC: lnop_t op, int current));
608 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
614 if (F_ISSET(sp
, SC_EX
))
617 /* XXXX goes outside of window */
620 TAILQ_FOREACH(wp
, &sp
->gp
->dq
, q
)
621 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
622 if (sp
!= tsp
&& tsp
->ep
== ep
)
623 if (vs_change(tsp
, lno
, op
))
625 return (current
? vs_change(sp
, lno
, op
) : 0);
629 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
632 update_cache(SCR
*sp
, lnop_t op
, db_recno_t lno
)
639 /* Flush the cache, update line count, before screen update. */
640 /* The flushing is probably not needed, since it was incorrect
641 * for db_insert. It might be better to adjust it, like
642 * marks, @ and global
644 TAILQ_FOREACH(scrp
, &ep
->scrq
, eq
)
648 if (lno
<= scrp
->c_lno
)
649 scrp
->c_lno
= OOBLNO
;
652 if (lno
== scrp
->c_lno
)
653 scrp
->c_lno
= OOBLNO
;
659 if (ep
->c_nlines
!= OOBLNO
)
674 * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
676 int db_msg_open(SCR
*sp
, const char *file
, DB
**dbp
)
678 *dbp
= dbopen(file
, O_NONBLOCK
| O_RDONLY
, 0, DB_RECNO
, NULL
);
684 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
687 db_init(SCR
*sp
, EXF
*ep
, char *rcv_name
, char *oname
, size_t psize
, int *open_err
)
691 memset(&oinfo
, 0, sizeof(RECNOINFO
));
692 oinfo
.bval
= '\n'; /* Always set. */
694 * If we are not recovering, set the pagesize and arrange to
695 * first get a snapshot of the file.
697 if (rcv_name
== NULL
) {
699 oinfo
.flags
= R_SNAPSHOT
;
702 * Always set the btree name, otherwise we are going to be using
703 * an in-memory database for the btree.
705 oinfo
.bfname
= ep
->rcv_path
;
707 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
709 ep
->db
= dbopen(rcv_name
== NULL
? oname
: NULL
,
710 O_NONBLOCK
| O_RDONLY
, _DB_OPEN_MODE
, DB_RECNO
, &oinfo
);
714 M_DBERR
, rcv_name
== NULL
? oname
: rcv_name
, "%s");
717 * Historically, vi permitted users to edit files that couldn't
718 * be read. This isn't useful for single files from a command
719 * line, but it's quite useful for "vi *.c", since you can skip
720 * past files that you can't read.
722 ep
->db
= NULL
; /* Don't close it; it wasn't opened */
728 * We always sync the underlying btree so that the header
731 ep
->db
->sync(ep
->db
, R_RECNOSYNC
);
738 db_strerror(int error
)
740 return error
> 0 ? strerror(error
) : "record not found";