tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / common / vi_db.c
blobed3962f8ac6458701846a2161bfa821bbe22a22c
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 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 ";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
29 #include "common.h"
30 #include "dbinternal.h"
31 #include "../vi/vi.h"
33 static int append __P((SCR*, db_recno_t, const CHAR_T*, size_t, lnop_t, int));
36 * db_eget --
37 * Front-end to db_get, special case handling for empty files.
39 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
41 int
42 db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
44 /* Line number. */
45 /* Pointer store. */
46 /* Length store. */
49 db_recno_t l1;
51 if (isemptyp != NULL)
52 *isemptyp = 0;
54 /* If the line exists, simply return it. */
55 if (!db_get(sp, lno, 0, pp, lenp))
56 return (0);
59 * If the user asked for line 0 or line 1, i.e. the only possible
60 * line in an empty file, find the last line of the file; db_last
61 * fails loudly.
63 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
64 return (1);
66 /* If the file isn't empty, fail loudly. */
67 if ((lno != 0 && lno != 1) || l1 != 0) {
68 db_err(sp, lno);
69 return (1);
72 if (isemptyp != NULL)
73 *isemptyp = 1;
75 return (1);
79 * db_get --
80 * Look in the text buffers for a line, followed by the cache, followed
81 * by the database.
83 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
85 int
86 db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
87 /* Line number. */ /* Pointer store. */ /* Length store. */
89 DBT data, key;
90 EXF *ep;
91 TEXT *tp;
92 db_recno_t l1, l2;
93 const CHAR_T *wp;
94 size_t wlen;
95 size_t nlen;
98 * The underlying recno stuff handles zero by returning NULL, but
99 * have to have an OOB condition for the look-aside into the input
100 * buffer anyway.
102 if (lno == 0)
103 goto err1;
105 /* Check for no underlying file. */
106 if ((ep = sp->ep) == NULL) {
107 ex_emsg(sp, NULL, EXM_NOFILEYET);
108 goto err3;
111 if (LF_ISSET(DBG_NOCACHE))
112 goto nocache;
115 * Look-aside into the TEXT buffers and see if the line we want
116 * is there.
118 if (F_ISSET(sp, SC_TINPUT)) {
119 l1 = TAILQ_FIRST(&sp->tiq)->lno;
120 l2 = TAILQ_LAST(&sp->tiq, _texth)->lno;
121 if (l1 <= lno && l2 >= lno) {
122 #if defined(DEBUG) && 0
123 vtrace(sp,
124 "retrieve TEXT buffer line %lu\n", (u_long)lno);
125 #endif
126 for (tp = TAILQ_FIRST(&sp->tiq);
127 tp->lno != lno; tp = TAILQ_NEXT(tp, q));
128 if (lenp != NULL)
129 *lenp = tp->len;
130 if (pp != NULL)
131 *pp = tp->lb;
132 return (0);
135 * Adjust the line number for the number of lines used
136 * by the text input buffers.
138 if (lno > l2)
139 lno -= l2 - l1;
142 /* Look-aside into the cache, and see if the line we want is there. */
143 if (lno == sp->c_lno) {
144 #if defined(DEBUG) && 0
145 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
146 #endif
147 if (lenp != NULL)
148 *lenp = sp->c_len;
149 if (pp != NULL)
150 *pp = sp->c_lp;
151 return (0);
153 sp->c_lno = OOBLNO;
155 nocache:
156 nlen = 1024;
157 retry:
158 /* data.size contains length in bytes */
159 BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
161 /* Get the line from the underlying database. */
162 memset(&key, 0, sizeof(key));
163 key.data = &lno;
164 key.size = sizeof(lno);
165 memset(&data, 0, sizeof(data));
166 data.data = sp->c_lp;
167 data.ulen = sp->c_blen;
168 data.flags = DB_DBT_USERMEM;
169 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
170 case DB_BUFFER_SMALL:
171 nlen = data.size;
172 goto retry;
173 default:
174 goto err2;
175 case DB_NOTFOUND:
176 err1: if (LF_ISSET(DBG_FATAL))
177 err2: db_err(sp, lno);
178 alloc_err:
179 err3: if (lenp != NULL)
180 *lenp = 0;
181 if (pp != NULL)
182 *pp = NULL;
183 return (1);
184 case 0:
188 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
189 if (!F_ISSET(sp, SC_CONV_ERROR)) {
190 F_SET(sp, SC_CONV_ERROR);
191 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
193 goto err3;
196 /* Reset the cache. */
197 if (wp != data.data) {
198 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
199 MEMCPYW(sp->c_lp, wp, wlen);
201 sp->c_lno = lno;
202 sp->c_len = wlen;
204 #if defined(DEBUG) && 0
205 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
206 #endif
207 if (lenp != NULL)
208 *lenp = wlen;
209 if (pp != NULL)
210 *pp = sp->c_lp;
211 return (0);
215 * db_delete --
216 * Delete a line from the file.
218 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
221 db_delete(SCR *sp, db_recno_t lno)
223 DBT key;
224 EXF *ep;
226 #if defined(DEBUG) && 0
227 vtrace(sp, "delete line %lu\n", (u_long)lno);
228 #endif
229 /* Check for no underlying file. */
230 if ((ep = sp->ep) == NULL) {
231 ex_emsg(sp, NULL, EXM_NOFILEYET);
232 return (1);
234 if (ep->l_win && ep->l_win != sp->wp) {
235 ex_emsg(sp, NULL, EXM_LOCKED);
236 return 1;
239 /* Update marks, @ and global commands. */
240 if (line_insdel(sp, LINE_DELETE, lno))
241 return 1;
243 /* Log before change. */
244 log_line(sp, lno, LOG_LINE_DELETE_B);
246 /* Update file. */
247 memset(&key, 0, sizeof(key));
248 key.data = &lno;
249 key.size = sizeof(lno);
250 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
251 msgq(sp, M_DBERR, "003|unable to delete line %lu",
252 (u_long)lno);
253 return (1);
256 /* Flush the cache, update line count, before screen update. */
257 update_cache(sp, LINE_DELETE, lno);
259 /* File now modified. */
260 if (F_ISSET(ep, F_FIRSTMODIFY))
261 (void)rcv_init(sp);
262 F_SET(ep, F_MODIFIED);
264 /* Log after change. */
265 log_line(sp, lno, LOG_LINE_DELETE_F);
267 /* Update screen. */
268 return (scr_update(sp, lno, LINE_DELETE, 1));
271 /* maybe this could be simpler
273 * DB3 behaves differently from DB1
275 * if lno != 0 just go to lno and put the new line after it
276 * if lno == 0 then if there are any record, put in front of the first
277 * otherwise just append to the end thus creating the first
278 * line
280 static int
281 append(SCR *sp, db_recno_t lno, const CHAR_T *p, size_t len, lnop_t op, int update)
283 DBT data, key;
284 DBC *dbcp_put;
285 EXF *ep;
286 const char *fp;
287 size_t flen;
288 int rval;
290 /* Check for no underlying file. */
291 if ((ep = sp->ep) == NULL) {
292 ex_emsg(sp, NULL, EXM_NOFILEYET);
293 return (1);
295 if (ep->l_win && ep->l_win != sp->wp) {
296 ex_emsg(sp, NULL, EXM_LOCKED);
297 return 1;
300 /* Log before change. */
301 log_line(sp, lno + 1, LOG_LINE_APPEND_B);
303 /* Update file. */
304 memset(&key, 0, sizeof(key));
305 key.data = &lno;
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)
310 return 1;
312 INT2FILE(sp, p, len, fp, flen);
314 if (lno != 0) {
315 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
316 goto err2;
318 data.data = __UNCONST(fp);
319 data.size = flen;
320 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
321 err2:
322 (void)dbcp_put->c_close(dbcp_put);
323 msgq(sp, M_DBERR,
324 (op == LINE_APPEND)
325 ? "004|unable to append to line %lu"
326 : "005|unable to insert at line %lu",
327 (u_long)lno);
328 return (1);
330 } else {
331 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
332 if (sp->db_error != DB_NOTFOUND)
333 goto err2;
335 data.data = __UNCONST(fp);
336 data.size = flen;
337 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
338 goto err2;
340 } else {
341 key.data = &lno;
342 key.size = sizeof(lno);
343 data.data = __UNCONST(fp);
344 data.size = flen;
345 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
346 goto err2;
351 (void)dbcp_put->c_close(dbcp_put);
353 /* Flush the cache, update line count, before screen update. */
354 update_cache(sp, LINE_INSERT, lno);
356 /* File now dirty. */
357 if (F_ISSET(ep, F_FIRSTMODIFY))
358 (void)rcv_init(sp);
359 F_SET(ep, F_MODIFIED);
361 /* Log after change. */
362 log_line(sp, lno + 1, LOG_LINE_APPEND_F);
364 /* Update marks, @ and global commands. */
365 rval = line_insdel(sp, LINE_INSERT, lno + 1);
368 * Update screen.
370 * comment copied from db_append
371 * XXX
372 * Nasty hack. If multiple lines are input by the user, they aren't
373 * committed until an <ESC> is entered. The problem is the screen was
374 * updated/scrolled as each line was entered. So, when this routine
375 * is called to copy the new lines from the cut buffer into the file,
376 * it has to know not to update the screen again.
378 return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
382 * db_append --
383 * Append a line into the file.
385 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
388 db_append(SCR *sp, int update, db_recno_t lno, const CHAR_T *p, size_t len)
390 #if defined(DEBUG) && 0
391 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
392 #endif
394 /* Update file. */
395 return append(sp, lno, p, len, LINE_APPEND, update);
399 * db_insert --
400 * Insert a line into the file.
402 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
405 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
407 #if defined(DEBUG) && 0
408 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
409 (u_long)lno, (u_long)len, MIN(len, 20), p);
410 #endif
411 return append(sp, lno - 1, p, len, LINE_INSERT, 1);
415 * db_set --
416 * Store a line in the file.
418 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
421 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
423 DBT data, key;
424 EXF *ep;
425 const char *fp;
426 size_t flen;
428 #if defined(DEBUG) && 0
429 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
430 (u_long)lno, (u_long)len, MIN(len, 20), p);
431 #endif
432 /* Check for no underlying file. */
433 if ((ep = sp->ep) == NULL) {
434 ex_emsg(sp, NULL, EXM_NOFILEYET);
435 return (1);
437 if (ep->l_win && ep->l_win != sp->wp) {
438 ex_emsg(sp, NULL, EXM_LOCKED);
439 return 1;
442 /* Log before change. */
443 log_line(sp, lno, LOG_LINE_RESET_B);
445 INT2FILE(sp, p, len, fp, flen);
447 /* Update file. */
448 memset(&key, 0, sizeof(key));
449 key.data = &lno;
450 key.size = sizeof(lno);
451 memset(&data, 0, sizeof(data));
452 data.data = __UNCONST(fp);
453 data.size = flen;
454 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
455 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
456 return (1);
459 /* Flush the cache, update line count, before screen update. */
460 update_cache(sp, LINE_RESET, lno);
462 /* File now dirty. */
463 if (F_ISSET(ep, F_FIRSTMODIFY))
464 (void)rcv_init(sp);
465 F_SET(ep, F_MODIFIED);
467 /* Log after change. */
468 log_line(sp, lno, LOG_LINE_RESET_F);
470 /* Update screen. */
471 return (scr_update(sp, lno, LINE_RESET, 1));
475 * db_exist --
476 * Return if a line exists.
478 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
481 db_exist(SCR *sp, db_recno_t lno)
483 EXF *ep;
485 /* Check for no underlying file. */
486 if ((ep = sp->ep) == NULL) {
487 ex_emsg(sp, NULL, EXM_NOFILEYET);
488 return (1);
491 if (lno == OOBLNO)
492 return (0);
495 * Check the last-line number cache. Adjust the cached line
496 * number for the lines used by the text input buffers.
498 if (ep->c_nlines != OOBLNO)
499 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
500 ep->c_nlines + TAILQ_LAST(&sp->tiq, _texth)->lno -
501 TAILQ_FIRST(&sp->tiq)->lno : ep->c_nlines));
503 /* Go get the line. */
504 return (!db_get(sp, lno, 0, NULL, NULL));
508 * db_last --
509 * Return the number of lines in the file.
511 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
514 db_last(SCR *sp, db_recno_t *lnop)
516 DBT data, key;
517 DBC *dbcp;
518 EXF *ep;
519 db_recno_t lno;
520 const CHAR_T *wp;
521 size_t wlen;
523 /* Check for no underlying file. */
524 if ((ep = sp->ep) == NULL) {
525 ex_emsg(sp, NULL, EXM_NOFILEYET);
526 return (1);
530 * Check the last-line number cache. Adjust the cached line
531 * number for the lines used by the text input buffers.
533 if (ep->c_nlines != OOBLNO) {
534 *lnop = ep->c_nlines;
535 if (F_ISSET(sp, SC_TINPUT))
536 *lnop += TAILQ_LAST(&sp->tiq, _texth)->lno -
537 TAILQ_FIRST(&sp->tiq)->lno;
538 return (0);
541 memset(&key, 0, sizeof(key));
542 key.data = &lno;
543 key.size = sizeof(lno);
544 memset(&data, 0, sizeof(data));
546 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
547 goto err1;
548 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
549 case DB_NOTFOUND:
550 *lnop = 0;
551 return (0);
552 default:
553 (void)dbcp->c_close(dbcp);
554 alloc_err:
555 err1:
556 msgq(sp, M_DBERR, "007|unable to get last line");
557 *lnop = 0;
558 return (1);
559 case 0:
563 memcpy(&lno, key.data, sizeof(lno));
565 if (lno != sp->c_lno) {
566 FILE2INT(sp, data.data, data.size, wp, wlen);
568 /* Fill the cache. */
569 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
570 MEMCPYW(sp->c_lp, wp, wlen);
571 sp->c_lno = lno;
572 sp->c_len = wlen;
574 ep->c_nlines = lno;
576 (void)dbcp->c_close(dbcp);
578 /* Return the value. */
579 *lnop = (F_ISSET(sp, SC_TINPUT) &&
580 TAILQ_LAST(&sp->tiq, _texth)->lno > lno ?
581 TAILQ_LAST(&sp->tiq, _texth)->lno : lno);
582 return (0);
586 * db_err --
587 * Report a line error.
589 * PUBLIC: void db_err __P((SCR *, db_recno_t));
591 void
592 db_err(SCR *sp, db_recno_t lno)
594 msgq(sp, M_ERR,
595 "008|Error: unable to retrieve line %lu", (u_long)lno);
599 * scr_update --
600 * Update all of the screens that are backed by the file that
601 * just changed.
603 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
604 * PUBLIC: lnop_t op, int current));
607 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
609 EXF *ep;
610 SCR *tsp;
611 WIN *wp;
613 if (F_ISSET(sp, SC_EX))
614 return (0);
616 /* XXXX goes outside of window */
617 ep = sp->ep;
618 if (ep->refcnt != 1)
619 TAILQ_FOREACH(wp, &sp->gp->dq, q)
620 TAILQ_FOREACH(tsp, &wp->scrq, q)
621 if (sp != tsp && tsp->ep == ep)
622 if (vs_change(tsp, lno, op))
623 return (1);
624 return (current ? vs_change(sp, lno, op) : 0);
628 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
630 void
631 update_cache(SCR *sp, lnop_t op, db_recno_t lno)
633 SCR* scrp;
634 EXF *ep;
636 ep = sp->ep;
638 /* Flush the cache, update line count, before screen update. */
639 /* The flushing is probably not needed, since it was incorrect
640 * for db_insert. It might be better to adjust it, like
641 * marks, @ and global
643 TAILQ_FOREACH(scrp, &ep->scrq, eq)
644 switch (op) {
645 case LINE_INSERT:
646 case LINE_DELETE:
647 if (lno <= scrp->c_lno)
648 scrp->c_lno = OOBLNO;
649 break;
650 case LINE_RESET:
651 if (lno == scrp->c_lno)
652 scrp->c_lno = OOBLNO;
653 break;
654 case LINE_APPEND:
655 break;
658 if (ep->c_nlines != OOBLNO)
659 switch (op) {
660 case LINE_INSERT:
661 ++ep->c_nlines;
662 break;
663 case LINE_DELETE:
664 --ep->c_nlines;
665 break;
666 case LINE_APPEND:
667 case LINE_RESET:
668 break;
673 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
676 line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
678 int rval;
680 /* Update marks, @ and global commands. */
681 rval = 0;
682 if (mark_insdel(sp, op, lno))
683 rval = 1;
684 if (ex_g_insdel(sp, op, lno))
685 rval = 1;
687 return rval;
690 #ifdef USE_DB4_LOGGING
691 #define VI_DB_INIT_LOG DB_INIT_LOG
692 #else
693 #define VI_DB_INIT_LOG 0
694 #endif
697 * PUBLIC: int db_setup __P((SCR *, EXF *));
700 db_setup(SCR *sp, EXF *ep)
702 char path[MAXPATHLEN];
703 int fd;
704 DB_ENV *env;
706 (void)snprintf(path, sizeof(path), "%s/vi.XXXXXX", O_STR(sp, O_RECDIR));
707 if ((fd = mkstemp(path)) == -1) {
708 msgq(sp, M_SYSERR, "%s", path);
709 goto err;
711 (void)close(fd);
712 (void)unlink(path);
713 if (mkdir(path, S_IRWXU)) {
714 msgq(sp, M_SYSERR, "%s", path);
715 goto err;
717 if (db_env_create(&env, 0)) {
718 msgq(sp, M_ERR, "env_create");
719 goto err;
721 #ifdef USE_DB4_LOGGING
722 if ((sp->db_error = vi_db_init_recover(env))) {
723 msgq(sp, M_DBERR, "init_recover");
724 goto err;
726 if ((sp->db_error = __vi_init_recover(env))) {
727 msgq(sp, M_DBERR, "init_recover");
728 goto err;
730 #endif
731 if ((sp->db_error = db_env_open(env, path,
732 DB_PRIVATE | DB_CREATE | DB_INIT_MPOOL | VI_DB_THREAD
733 | VI_DB_INIT_LOG, 0)) != 0) {
734 msgq(sp, M_DBERR, "env->open");
735 goto err;
738 if ((ep->env_path = strdup(path)) == NULL) {
739 msgq(sp, M_SYSERR, NULL);
740 (void)rmdir(path);
741 goto err;
743 ep->env = env;
744 return 0;
745 err:
746 return 1;
749 /* Round up v to the nearest power of 2 */
750 static size_t round_up(size_t v)
752 ssize_t old_v = v;
754 while (v) {
755 old_v = v;
756 v ^= v & -v;
758 return old_v << 1;
762 * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
764 int db_msg_open(SCR *sp, const char *file, DB **dbp)
766 return (sp->db_error = db_create(dbp, 0, 0)) != 0 ||
767 (sp->db_error = (*dbp)->set_re_source(*dbp, file)) != 0 ||
768 (sp->db_error = db_open(*dbp, NULL, DB_RECNO, 0, 0)) != 0;
772 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
775 db_init(SCR *sp, EXF *ep, char *rcv_name, char *oname, size_t psize, int *open_err)
777 if (db_setup(sp, ep))
778 return 1;
780 /* Open a db structure. */
781 if ((sp->db_error = db_create(&ep->db, 0, 0)) != 0) {
782 msgq(sp, M_DBERR, "db_create");
783 return 1;
786 ep->db->set_re_delim(ep->db, '\n'); /* Always set. */
787 ep->db->set_pagesize(ep->db, round_up(psize));
788 ep->db->set_flags(ep->db, DB_RENUMBER | DB_SNAPSHOT);
789 if (rcv_name == NULL)
790 ep->db->set_re_source(ep->db, oname);
793 * Don't let db use mmap when using fcntl for locking
795 #ifdef HAVE_LOCK_FCNTL
796 #define NOMMAPIFFCNTL DB_NOMMAP
797 #else
798 #define NOMMAPIFFCNTL 0
799 #endif
801 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
803 if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
804 ((rcv_name == 0) ? DB_TRUNCATE : 0) | VI_DB_THREAD | NOMMAPIFFCNTL,
805 _DB_OPEN_MODE)) != 0) {
806 msgq_str(sp,
807 M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
809 * !!!
810 * Historically, vi permitted users to edit files that couldn't
811 * be read. This isn't useful for single files from a command
812 * line, but it's quite useful for "vi *.c", since you can skip
813 * past files that you can't read.
815 ep->db = NULL; /* Don't close it; it wasn't opened */
817 *open_err = 1;
818 return 1;
821 /* re_source is loaded into the database.
822 * Close it and reopen it in the environment.
824 if ((sp->db_error = ep->db->close(ep->db, 0))) {
825 msgq(sp, M_DBERR, "close");
826 return 1;
828 if ((sp->db_error = db_create(&ep->db, ep->env, 0)) != 0) {
829 msgq(sp, M_DBERR, "db_create 2");
830 return 1;
832 if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
833 VI_DB_THREAD | NOMMAPIFFCNTL, _DB_OPEN_MODE)) != 0) {
834 msgq_str(sp,
835 M_DBERR, ep->rcv_path, "%s");
836 return 1;
839 return 0;