xref: /minix3/external/bsd/nvi/dist/common/vi_db1.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
184d9c625SLionel Sambuc /*-
284d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
384d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
484d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
584d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
684d9c625SLionel Sambuc  *
784d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
884d9c625SLionel Sambuc  */
984d9c625SLionel Sambuc 
1084d9c625SLionel Sambuc #include "config.h"
1184d9c625SLionel Sambuc 
12*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
13*0a6a1f1dSLionel Sambuc #if 0
1484d9c625SLionel Sambuc #ifndef lint
1584d9c625SLionel Sambuc 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 ";
1684d9c625SLionel Sambuc #endif /* not lint */
17*0a6a1f1dSLionel Sambuc #else
18*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: vi_db1.c,v 1.7 2014/01/26 21:43:45 christos Exp $");
19*0a6a1f1dSLionel Sambuc #endif
2084d9c625SLionel Sambuc 
2184d9c625SLionel Sambuc #include <sys/types.h>
2284d9c625SLionel Sambuc #include <sys/queue.h>
2384d9c625SLionel Sambuc #include <sys/time.h>
2484d9c625SLionel Sambuc #include <sys/stat.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <errno.h>
2884d9c625SLionel Sambuc #include <fcntl.h>
2984d9c625SLionel Sambuc #include <limits.h>
3084d9c625SLionel Sambuc #include <stdio.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc 
3384d9c625SLionel Sambuc #include "common.h"
3484d9c625SLionel Sambuc #include "../vi/vi.h"
3584d9c625SLionel Sambuc #include "dbinternal.h"
3684d9c625SLionel Sambuc 
3784d9c625SLionel Sambuc /*
3884d9c625SLionel Sambuc  * db_eget --
3984d9c625SLionel Sambuc  *	Front-end to db_get, special case handling for empty files.
4084d9c625SLionel Sambuc  *
4184d9c625SLionel Sambuc  * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
4284d9c625SLionel Sambuc  */
4384d9c625SLionel Sambuc int
db_eget(SCR * sp,db_recno_t lno,CHAR_T ** pp,size_t * lenp,int * isemptyp)4484d9c625SLionel Sambuc db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
4584d9c625SLionel Sambuc 
4684d9c625SLionel Sambuc 	               				/* Line number. */
4784d9c625SLionel Sambuc 	            				/* Pointer store. */
4884d9c625SLionel Sambuc 	             				/* Length store. */
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc {
5184d9c625SLionel Sambuc 	db_recno_t l1;
5284d9c625SLionel Sambuc 
5384d9c625SLionel Sambuc 	if (isemptyp != NULL)
5484d9c625SLionel Sambuc 		*isemptyp = 0;
5584d9c625SLionel Sambuc 
5684d9c625SLionel Sambuc 	/* If the line exists, simply return it. */
5784d9c625SLionel Sambuc 	if (!db_get(sp, lno, 0, pp, lenp))
5884d9c625SLionel Sambuc 		return (0);
5984d9c625SLionel Sambuc 
6084d9c625SLionel Sambuc 	/*
6184d9c625SLionel Sambuc 	 * If the user asked for line 0 or line 1, i.e. the only possible
6284d9c625SLionel Sambuc 	 * line in an empty file, find the last line of the file; db_last
6384d9c625SLionel Sambuc 	 * fails loudly.
6484d9c625SLionel Sambuc 	 */
6584d9c625SLionel Sambuc 	if ((lno == 0 || lno == 1) && db_last(sp, &l1))
6684d9c625SLionel Sambuc 		return (1);
6784d9c625SLionel Sambuc 
6884d9c625SLionel Sambuc 	/* If the file isn't empty, fail loudly. */
6984d9c625SLionel Sambuc 	if ((lno != 0 && lno != 1) || l1 != 0) {
7084d9c625SLionel Sambuc 		db_err(sp, lno);
7184d9c625SLionel Sambuc 		return (1);
7284d9c625SLionel Sambuc 	}
7384d9c625SLionel Sambuc 
7484d9c625SLionel Sambuc 	if (isemptyp != NULL)
7584d9c625SLionel Sambuc 		*isemptyp = 1;
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc 	return (1);
7884d9c625SLionel Sambuc }
7984d9c625SLionel Sambuc 
8084d9c625SLionel Sambuc /*
8184d9c625SLionel Sambuc  * db_get --
8284d9c625SLionel Sambuc  *	Look in the text buffers for a line, followed by the cache, followed
8384d9c625SLionel Sambuc  *	by the database.
8484d9c625SLionel Sambuc  *
8584d9c625SLionel Sambuc  * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
8684d9c625SLionel Sambuc  */
8784d9c625SLionel Sambuc int
db_get(SCR * sp,db_recno_t lno,u_int32_t flags,CHAR_T ** pp,size_t * lenp)8884d9c625SLionel Sambuc db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
8984d9c625SLionel Sambuc 
9084d9c625SLionel Sambuc 	               				/* Line number. */
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc 	            				/* Pointer store. */
9384d9c625SLionel Sambuc 	             				/* Length store. */
9484d9c625SLionel Sambuc {
9584d9c625SLionel Sambuc 	DBT data, key;
9684d9c625SLionel Sambuc 	EXF *ep;
9784d9c625SLionel Sambuc 	TEXT *tp;
9884d9c625SLionel Sambuc 	db_recno_t l1, l2;
9984d9c625SLionel Sambuc 	const CHAR_T *wp;
10084d9c625SLionel Sambuc 	size_t wlen;
10184d9c625SLionel Sambuc 	size_t nlen;
10284d9c625SLionel Sambuc 
10384d9c625SLionel Sambuc 	/*
10484d9c625SLionel Sambuc 	 * The underlying recno stuff handles zero by returning NULL, but
10584d9c625SLionel Sambuc 	 * have to have an OOB condition for the look-aside into the input
10684d9c625SLionel Sambuc 	 * buffer anyway.
10784d9c625SLionel Sambuc 	 */
10884d9c625SLionel Sambuc 	if (lno == 0)
10984d9c625SLionel Sambuc 		goto err1;
11084d9c625SLionel Sambuc 
11184d9c625SLionel Sambuc 	/* Check for no underlying file. */
11284d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
11384d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
11484d9c625SLionel Sambuc 		goto err3;
11584d9c625SLionel Sambuc 	}
11684d9c625SLionel Sambuc 
11784d9c625SLionel Sambuc 	if (LF_ISSET(DBG_NOCACHE))
11884d9c625SLionel Sambuc 		goto nocache;
11984d9c625SLionel Sambuc 
12084d9c625SLionel Sambuc 	/*
12184d9c625SLionel Sambuc 	 * Look-aside into the TEXT buffers and see if the line we want
12284d9c625SLionel Sambuc 	 * is there.
12384d9c625SLionel Sambuc 	 */
12484d9c625SLionel Sambuc 	if (F_ISSET(sp, SC_TINPUT)) {
12584d9c625SLionel Sambuc 		l1 = TAILQ_FIRST(&sp->tiq)->lno;
12684d9c625SLionel Sambuc 		l2 = TAILQ_LAST(&sp->tiq, _texth)->lno;
12784d9c625SLionel Sambuc 		if (l1 <= lno && l2 >= lno) {
12884d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
12984d9c625SLionel Sambuc 			vtrace(
13084d9c625SLionel Sambuc 			    "retrieve TEXT buffer line %lu\n", (u_long)lno);
13184d9c625SLionel Sambuc #endif
13284d9c625SLionel Sambuc 			for (tp = TAILQ_FIRST(&sp->tiq);
13384d9c625SLionel Sambuc 			    tp->lno != lno; tp = TAILQ_NEXT(tp, q));
13484d9c625SLionel Sambuc 			if (lenp != NULL)
13584d9c625SLionel Sambuc 				*lenp = tp->len;
13684d9c625SLionel Sambuc 			if (pp != NULL)
13784d9c625SLionel Sambuc 				*pp = tp->lb;
13884d9c625SLionel Sambuc 			return (0);
13984d9c625SLionel Sambuc 		}
14084d9c625SLionel Sambuc 		/*
14184d9c625SLionel Sambuc 		 * Adjust the line number for the number of lines used
14284d9c625SLionel Sambuc 		 * by the text input buffers.
14384d9c625SLionel Sambuc 		 */
14484d9c625SLionel Sambuc 		if (lno > l2)
14584d9c625SLionel Sambuc 			lno -= l2 - l1;
14684d9c625SLionel Sambuc 	}
14784d9c625SLionel Sambuc 
14884d9c625SLionel Sambuc 	/* Look-aside into the cache, and see if the line we want is there. */
14984d9c625SLionel Sambuc 	if (lno == sp->c_lno) {
15084d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
15184d9c625SLionel Sambuc 		vtrace("retrieve cached line %lu\n", (u_long)lno);
15284d9c625SLionel Sambuc #endif
15384d9c625SLionel Sambuc 		if (lenp != NULL)
15484d9c625SLionel Sambuc 			*lenp = sp->c_len;
15584d9c625SLionel Sambuc 		if (pp != NULL)
15684d9c625SLionel Sambuc 			*pp = sp->c_lp;
15784d9c625SLionel Sambuc 		return (0);
15884d9c625SLionel Sambuc 	}
15984d9c625SLionel Sambuc 	sp->c_lno = OOBLNO;
16084d9c625SLionel Sambuc 
16184d9c625SLionel Sambuc nocache:
16284d9c625SLionel Sambuc 	nlen = 1024;
16384d9c625SLionel Sambuc retry:
16484d9c625SLionel Sambuc 	/* data.size contains length in bytes */
16584d9c625SLionel Sambuc 	BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
16684d9c625SLionel Sambuc 
16784d9c625SLionel Sambuc 	/* Get the line from the underlying database. */
16884d9c625SLionel Sambuc 	key.data = &lno;
16984d9c625SLionel Sambuc 	key.size = sizeof(lno);
17084d9c625SLionel Sambuc 	switch (ep->db->get(ep->db, &key, &data, 0)) {
17184d9c625SLionel Sambuc         case -1:
17284d9c625SLionel Sambuc 		goto err2;
17384d9c625SLionel Sambuc 	case 1:
17484d9c625SLionel Sambuc err1:		if (LF_ISSET(DBG_FATAL))
17584d9c625SLionel Sambuc err2:			db_err(sp, lno);
17684d9c625SLionel Sambuc alloc_err:
17784d9c625SLionel Sambuc err3:		if (lenp != NULL)
17884d9c625SLionel Sambuc 			*lenp = 0;
17984d9c625SLionel Sambuc 		if (pp != NULL)
18084d9c625SLionel Sambuc 			*pp = NULL;
18184d9c625SLionel Sambuc 		return (1);
18284d9c625SLionel Sambuc 	case 0:
18384d9c625SLionel Sambuc 		if (data.size > nlen) {
18484d9c625SLionel Sambuc 			nlen = data.size;
18584d9c625SLionel Sambuc 			goto retry;
18684d9c625SLionel Sambuc 		} else
18784d9c625SLionel Sambuc 			memcpy(sp->c_lp, data.data, nlen);
18884d9c625SLionel Sambuc 	}
18984d9c625SLionel Sambuc 
19084d9c625SLionel Sambuc 	if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
19184d9c625SLionel Sambuc 	    if (!F_ISSET(sp, SC_CONV_ERROR)) {
19284d9c625SLionel Sambuc 		F_SET(sp, SC_CONV_ERROR);
19384d9c625SLionel Sambuc 		msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
19484d9c625SLionel Sambuc 	    }
19584d9c625SLionel Sambuc 	    goto err3;
19684d9c625SLionel Sambuc 	}
19784d9c625SLionel Sambuc 
19884d9c625SLionel Sambuc 	/* Reset the cache. */
19984d9c625SLionel Sambuc 	if (wp != data.data) {
20084d9c625SLionel Sambuc 	    BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
20184d9c625SLionel Sambuc 	    MEMCPYW(sp->c_lp, wp, wlen);
20284d9c625SLionel Sambuc 	}
20384d9c625SLionel Sambuc 	sp->c_lno = lno;
20484d9c625SLionel Sambuc 	sp->c_len = wlen;
20584d9c625SLionel Sambuc 
20684d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
20784d9c625SLionel Sambuc 	vtrace("retrieve DB line %lu\n", (u_long)lno);
20884d9c625SLionel Sambuc #endif
20984d9c625SLionel Sambuc 	if (lenp != NULL)
21084d9c625SLionel Sambuc 		*lenp = wlen;
21184d9c625SLionel Sambuc 	if (pp != NULL)
21284d9c625SLionel Sambuc 		*pp = sp->c_lp;
21384d9c625SLionel Sambuc 	return (0);
21484d9c625SLionel Sambuc }
21584d9c625SLionel Sambuc 
21684d9c625SLionel Sambuc /*
21784d9c625SLionel Sambuc  * db_delete --
21884d9c625SLionel Sambuc  *	Delete a line from the file.
21984d9c625SLionel Sambuc  *
22084d9c625SLionel Sambuc  * PUBLIC: int db_delete __P((SCR *, db_recno_t));
22184d9c625SLionel Sambuc  */
22284d9c625SLionel Sambuc int
db_delete(SCR * sp,db_recno_t lno)22384d9c625SLionel Sambuc db_delete(SCR *sp, db_recno_t lno)
22484d9c625SLionel Sambuc {
22584d9c625SLionel Sambuc 	DBT key;
22684d9c625SLionel Sambuc 	EXF *ep;
22784d9c625SLionel Sambuc 
22884d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
22984d9c625SLionel Sambuc 	vtrace("delete line %lu\n", (u_long)lno);
23084d9c625SLionel Sambuc #endif
23184d9c625SLionel Sambuc 	/* Check for no underlying file. */
23284d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
23384d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
23484d9c625SLionel Sambuc 		return (1);
23584d9c625SLionel Sambuc 	}
23684d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
23784d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
23884d9c625SLionel Sambuc 		return 1;
23984d9c625SLionel Sambuc 	}
24084d9c625SLionel Sambuc 
24184d9c625SLionel Sambuc 	/* Update marks, @ and global commands. */
24284d9c625SLionel Sambuc 	if (mark_insdel(sp, LINE_DELETE, lno))
24384d9c625SLionel Sambuc 		return (1);
24484d9c625SLionel Sambuc 	if (ex_g_insdel(sp, LINE_DELETE, lno))
24584d9c625SLionel Sambuc 		return (1);
24684d9c625SLionel Sambuc 
24784d9c625SLionel Sambuc 	/* Log change. */
24884d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_DELETE_B);
24984d9c625SLionel Sambuc 
25084d9c625SLionel Sambuc 	/* Update file. */
25184d9c625SLionel Sambuc 	key.data = &lno;
25284d9c625SLionel Sambuc 	key.size = sizeof(lno);
25384d9c625SLionel Sambuc 	sp->db_error = ep->db->del(ep->db, &key, 0);
25484d9c625SLionel Sambuc 	if (sp->db_error != 0) {
25584d9c625SLionel Sambuc 		if (sp->db_error == -1)
25684d9c625SLionel Sambuc 			sp->db_error = errno;
25784d9c625SLionel Sambuc 
25884d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "003|unable to delete line %lu",
25984d9c625SLionel Sambuc 		    (u_long)lno);
26084d9c625SLionel Sambuc 		return (1);
26184d9c625SLionel Sambuc 	}
26284d9c625SLionel Sambuc 
26384d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
26484d9c625SLionel Sambuc 	update_cache(sp, LINE_DELETE, lno);
26584d9c625SLionel Sambuc 
26684d9c625SLionel Sambuc 	/* File now modified. */
26784d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
26884d9c625SLionel Sambuc 		(void)rcv_init(sp);
26984d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
27084d9c625SLionel Sambuc 
27184d9c625SLionel Sambuc 	/* Log after change. */
27284d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_DELETE_F);
27384d9c625SLionel Sambuc 
27484d9c625SLionel Sambuc 	/* Update screen. */
27584d9c625SLionel Sambuc 	return (scr_update(sp, lno, LINE_DELETE, 1));
27684d9c625SLionel Sambuc }
27784d9c625SLionel Sambuc 
27884d9c625SLionel Sambuc /*
27984d9c625SLionel Sambuc  * db_append --
28084d9c625SLionel Sambuc  *	Append a line into the file.
28184d9c625SLionel Sambuc  *
28284d9c625SLionel Sambuc  * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
28384d9c625SLionel Sambuc  */
28484d9c625SLionel Sambuc int
db_append(SCR * sp,int update,db_recno_t lno,const CHAR_T * p,size_t len)28584d9c625SLionel Sambuc db_append(SCR *sp, int update, db_recno_t lno, const CHAR_T *p, size_t len)
28684d9c625SLionel Sambuc {
28784d9c625SLionel Sambuc 	DBT data, key;
28884d9c625SLionel Sambuc 	EXF *ep;
28984d9c625SLionel Sambuc 	const char *fp;
29084d9c625SLionel Sambuc 	size_t flen;
29184d9c625SLionel Sambuc 	int rval;
29284d9c625SLionel Sambuc 
29384d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
29484d9c625SLionel Sambuc 	vtrace("append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
29584d9c625SLionel Sambuc #endif
29684d9c625SLionel Sambuc 	/* Check for no underlying file. */
29784d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
29884d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
29984d9c625SLionel Sambuc 		return (1);
30084d9c625SLionel Sambuc 	}
30184d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
30284d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
30384d9c625SLionel Sambuc 		return 1;
30484d9c625SLionel Sambuc 	}
30584d9c625SLionel Sambuc 
30684d9c625SLionel Sambuc 	/* Log before change. */
30784d9c625SLionel Sambuc 	log_line(sp, lno + 1, LOG_LINE_APPEND_B);
30884d9c625SLionel Sambuc 
30984d9c625SLionel Sambuc 	INT2FILE(sp, p, len, fp, flen);
31084d9c625SLionel Sambuc 
31184d9c625SLionel Sambuc 	/* Update file. */
31284d9c625SLionel Sambuc 	key.data = &lno;
31384d9c625SLionel Sambuc 	key.size = sizeof(lno);
31484d9c625SLionel Sambuc 	data.data = __UNCONST(fp);
31584d9c625SLionel Sambuc 	data.size = flen;
31684d9c625SLionel Sambuc 	if (ep->db->put(ep->db, &key, &data, R_IAFTER)) {
31784d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "004|unable to append to line %lu",
31884d9c625SLionel Sambuc 			(u_long)lno);
31984d9c625SLionel Sambuc 		return (1);
32084d9c625SLionel Sambuc 	}
32184d9c625SLionel Sambuc 
32284d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
32384d9c625SLionel Sambuc 	update_cache(sp, LINE_INSERT, lno);
32484d9c625SLionel Sambuc 
32584d9c625SLionel Sambuc 	/* File now dirty. */
32684d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
32784d9c625SLionel Sambuc 		(void)rcv_init(sp);
32884d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
32984d9c625SLionel Sambuc 
33084d9c625SLionel Sambuc 	/* Log after change. */
33184d9c625SLionel Sambuc 	log_line(sp, lno + 1, LOG_LINE_APPEND_F);
33284d9c625SLionel Sambuc 
33384d9c625SLionel Sambuc 	/* Update marks, @ and global commands. */
33484d9c625SLionel Sambuc 	rval = 0;
33584d9c625SLionel Sambuc 	if (mark_insdel(sp, LINE_INSERT, lno + 1))
33684d9c625SLionel Sambuc 		rval = 1;
33784d9c625SLionel Sambuc 	if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
33884d9c625SLionel Sambuc 		rval = 1;
33984d9c625SLionel Sambuc 
34084d9c625SLionel Sambuc 	/*
34184d9c625SLionel Sambuc 	 * Update screen.
34284d9c625SLionel Sambuc 	 *
34384d9c625SLionel Sambuc 	 * XXX
34484d9c625SLionel Sambuc 	 * Nasty hack.  If multiple lines are input by the user, they aren't
34584d9c625SLionel Sambuc 	 * committed until an <ESC> is entered.  The problem is the screen was
34684d9c625SLionel Sambuc 	 * updated/scrolled as each line was entered.  So, when this routine
34784d9c625SLionel Sambuc 	 * is called to copy the new lines from the cut buffer into the file,
34884d9c625SLionel Sambuc 	 * it has to know not to update the screen again.
34984d9c625SLionel Sambuc 	 */
35084d9c625SLionel Sambuc 	return (scr_update(sp, lno, LINE_APPEND, update) || rval);
35184d9c625SLionel Sambuc }
35284d9c625SLionel Sambuc 
35384d9c625SLionel Sambuc /*
35484d9c625SLionel Sambuc  * db_insert --
35584d9c625SLionel Sambuc  *	Insert a line into the file.
35684d9c625SLionel Sambuc  *
35784d9c625SLionel Sambuc  * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
35884d9c625SLionel Sambuc  */
35984d9c625SLionel Sambuc int
db_insert(SCR * sp,db_recno_t lno,CHAR_T * p,size_t len)36084d9c625SLionel Sambuc db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
36184d9c625SLionel Sambuc {
36284d9c625SLionel Sambuc 	DBT data, key;
36384d9c625SLionel Sambuc 	EXF *ep;
36484d9c625SLionel Sambuc 	const char *fp;
36584d9c625SLionel Sambuc 	size_t flen;
36684d9c625SLionel Sambuc 	int rval;
36784d9c625SLionel Sambuc 
36884d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
36984d9c625SLionel Sambuc 	vtrace("insert before %lu: len %lu {%.*s}\n",
37084d9c625SLionel Sambuc 	    (u_long)lno, (u_long)len, MIN(len, 20), p);
37184d9c625SLionel Sambuc #endif
37284d9c625SLionel Sambuc 	/* Check for no underlying file. */
37384d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
37484d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
37584d9c625SLionel Sambuc 		return (1);
37684d9c625SLionel Sambuc 	}
37784d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
37884d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
37984d9c625SLionel Sambuc 		return 1;
38084d9c625SLionel Sambuc 	}
38184d9c625SLionel Sambuc 
38284d9c625SLionel Sambuc 	/* Log before change. */
38384d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_APPEND_B);
38484d9c625SLionel Sambuc 
38584d9c625SLionel Sambuc 	INT2FILE(sp, p, len, fp, flen);
38684d9c625SLionel Sambuc 
38784d9c625SLionel Sambuc 	/* Update file. */
38884d9c625SLionel Sambuc 	key.data = &lno;
38984d9c625SLionel Sambuc 	key.size = sizeof(lno);
39084d9c625SLionel Sambuc 	data.data = __UNCONST(fp);
39184d9c625SLionel Sambuc 	data.size = flen;
39284d9c625SLionel Sambuc 	if (ep->db->put(ep->db, &key, &data, R_IBEFORE)) {
39384d9c625SLionel Sambuc 		msgq(sp, M_SYSERR,
39484d9c625SLionel Sambuc 		    "005|unable to insert at line %lu", (u_long)lno);
39584d9c625SLionel Sambuc 		return (1);
39684d9c625SLionel Sambuc 	}
39784d9c625SLionel Sambuc 
39884d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
39984d9c625SLionel Sambuc 	update_cache(sp, LINE_INSERT, lno);
40084d9c625SLionel Sambuc 
40184d9c625SLionel Sambuc 	/* File now dirty. */
40284d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
40384d9c625SLionel Sambuc 		(void)rcv_init(sp);
40484d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
40584d9c625SLionel Sambuc 
40684d9c625SLionel Sambuc 	/* Log after change. */
40784d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_APPEND_F);
40884d9c625SLionel Sambuc 
40984d9c625SLionel Sambuc 	/* Update marks, @ and global commands. */
41084d9c625SLionel Sambuc 	rval = 0;
41184d9c625SLionel Sambuc 	if (mark_insdel(sp, LINE_INSERT, lno))
41284d9c625SLionel Sambuc 		rval = 1;
41384d9c625SLionel Sambuc 	if (ex_g_insdel(sp, LINE_INSERT, lno))
41484d9c625SLionel Sambuc 		rval = 1;
41584d9c625SLionel Sambuc 
41684d9c625SLionel Sambuc 	/* Update screen. */
41784d9c625SLionel Sambuc 	return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
41884d9c625SLionel Sambuc }
41984d9c625SLionel Sambuc 
42084d9c625SLionel Sambuc /*
42184d9c625SLionel Sambuc  * db_set --
42284d9c625SLionel Sambuc  *	Store a line in the file.
42384d9c625SLionel Sambuc  *
42484d9c625SLionel Sambuc  * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
42584d9c625SLionel Sambuc  */
42684d9c625SLionel Sambuc int
db_set(SCR * sp,db_recno_t lno,CHAR_T * p,size_t len)42784d9c625SLionel Sambuc db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
42884d9c625SLionel Sambuc {
42984d9c625SLionel Sambuc 	DBT data, key;
43084d9c625SLionel Sambuc 	EXF *ep;
43184d9c625SLionel Sambuc 	const char *fp;
43284d9c625SLionel Sambuc 	size_t flen;
43384d9c625SLionel Sambuc 
43484d9c625SLionel Sambuc #if defined(DBDEBUG) && defined(TRACE)
43584d9c625SLionel Sambuc 	vtrace("replace line %lu: len %lu {%.*s}\n",
43684d9c625SLionel Sambuc 	    (u_long)lno, (u_long)len, MIN(len, 20), p);
43784d9c625SLionel Sambuc #endif
43884d9c625SLionel Sambuc 	/* Check for no underlying file. */
43984d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
44084d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
44184d9c625SLionel Sambuc 		return (1);
44284d9c625SLionel Sambuc 	}
44384d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
44484d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
44584d9c625SLionel Sambuc 		return 1;
44684d9c625SLionel Sambuc 	}
44784d9c625SLionel Sambuc 
44884d9c625SLionel Sambuc 	/* Log before change. */
44984d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_RESET_B);
45084d9c625SLionel Sambuc 
45184d9c625SLionel Sambuc 	INT2FILE(sp, p, len, fp, flen);
45284d9c625SLionel Sambuc 
45384d9c625SLionel Sambuc 	/* Update file. */
45484d9c625SLionel Sambuc 	key.data = &lno;
45584d9c625SLionel Sambuc 	key.size = sizeof(lno);
45684d9c625SLionel Sambuc 	data.data = __UNCONST(fp);
45784d9c625SLionel Sambuc 	data.size = flen;
45884d9c625SLionel Sambuc 	sp->db_error =
45984d9c625SLionel Sambuc 		ep->db->put(ep->db, &key, &data, 0);
46084d9c625SLionel Sambuc 	if (sp->db_error != 0) {
46184d9c625SLionel Sambuc 		if (sp->db_error == -1)
46284d9c625SLionel Sambuc 			sp->db_error = errno;
46384d9c625SLionel Sambuc 
46484d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
46584d9c625SLionel Sambuc 		return (1);
46684d9c625SLionel Sambuc 	}
46784d9c625SLionel Sambuc 
46884d9c625SLionel Sambuc 	/* Flush the cache, before logging or screen update. */
46984d9c625SLionel Sambuc 	update_cache(sp, LINE_RESET, lno);
47084d9c625SLionel Sambuc 
47184d9c625SLionel Sambuc 	/* File now dirty. */
47284d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
47384d9c625SLionel Sambuc 		(void)rcv_init(sp);
47484d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
47584d9c625SLionel Sambuc 
47684d9c625SLionel Sambuc 	/* Log after change. */
47784d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_RESET_F);
47884d9c625SLionel Sambuc 
47984d9c625SLionel Sambuc 	/* Update screen. */
48084d9c625SLionel Sambuc 	return (scr_update(sp, lno, LINE_RESET, 1));
48184d9c625SLionel Sambuc }
48284d9c625SLionel Sambuc 
48384d9c625SLionel Sambuc /*
48484d9c625SLionel Sambuc  * db_exist --
48584d9c625SLionel Sambuc  *	Return if a line exists.
48684d9c625SLionel Sambuc  *
48784d9c625SLionel Sambuc  * PUBLIC: int db_exist __P((SCR *, db_recno_t));
48884d9c625SLionel Sambuc  */
48984d9c625SLionel Sambuc int
db_exist(SCR * sp,db_recno_t lno)49084d9c625SLionel Sambuc db_exist(SCR *sp, db_recno_t lno)
49184d9c625SLionel Sambuc {
49284d9c625SLionel Sambuc 	EXF *ep;
49384d9c625SLionel Sambuc 
49484d9c625SLionel Sambuc 	/* Check for no underlying file. */
49584d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
49684d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
49784d9c625SLionel Sambuc 		return (1);
49884d9c625SLionel Sambuc 	}
49984d9c625SLionel Sambuc 
50084d9c625SLionel Sambuc 	if (lno == OOBLNO)
50184d9c625SLionel Sambuc 		return (0);
50284d9c625SLionel Sambuc 
50384d9c625SLionel Sambuc 	/*
50484d9c625SLionel Sambuc 	 * Check the last-line number cache.  Adjust the cached line
50584d9c625SLionel Sambuc 	 * number for the lines used by the text input buffers.
50684d9c625SLionel Sambuc 	 */
50784d9c625SLionel Sambuc 	if (ep->c_nlines != OOBLNO)
50884d9c625SLionel Sambuc 		return (lno <= (F_ISSET(sp, SC_TINPUT) ?
50984d9c625SLionel Sambuc 		    ep->c_nlines + (TAILQ_LAST(&sp->tiq, _texth)->lno -
51084d9c625SLionel Sambuc 		    TAILQ_FIRST(&sp->tiq)->lno) : ep->c_nlines));
51184d9c625SLionel Sambuc 
51284d9c625SLionel Sambuc 	/* Go get the line. */
51384d9c625SLionel Sambuc 	return (!db_get(sp, lno, 0, NULL, NULL));
51484d9c625SLionel Sambuc }
51584d9c625SLionel Sambuc 
51684d9c625SLionel Sambuc /*
51784d9c625SLionel Sambuc  * db_last --
51884d9c625SLionel Sambuc  *	Return the number of lines in the file.
51984d9c625SLionel Sambuc  *
52084d9c625SLionel Sambuc  * PUBLIC: int db_last __P((SCR *, db_recno_t *));
52184d9c625SLionel Sambuc  */
52284d9c625SLionel Sambuc int
db_last(SCR * sp,db_recno_t * lnop)52384d9c625SLionel Sambuc db_last(SCR *sp, db_recno_t *lnop)
52484d9c625SLionel Sambuc {
52584d9c625SLionel Sambuc 	DBT data, key;
52684d9c625SLionel Sambuc 	EXF *ep;
52784d9c625SLionel Sambuc 	db_recno_t lno;
52884d9c625SLionel Sambuc 	const CHAR_T *wp;
52984d9c625SLionel Sambuc 	size_t wlen;
53084d9c625SLionel Sambuc 
53184d9c625SLionel Sambuc 	/* Check for no underlying file. */
53284d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
53384d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
53484d9c625SLionel Sambuc 		return (1);
53584d9c625SLionel Sambuc 	}
53684d9c625SLionel Sambuc 
53784d9c625SLionel Sambuc 	/*
53884d9c625SLionel Sambuc 	 * Check the last-line number cache.  Adjust the cached line
53984d9c625SLionel Sambuc 	 * number for the lines used by the text input buffers.
54084d9c625SLionel Sambuc 	 */
54184d9c625SLionel Sambuc 	if (ep->c_nlines != OOBLNO) {
54284d9c625SLionel Sambuc 		*lnop = ep->c_nlines;
54384d9c625SLionel Sambuc 		if (F_ISSET(sp, SC_TINPUT))
54484d9c625SLionel Sambuc 			*lnop += TAILQ_LAST(&sp->tiq, _texth)->lno -
54584d9c625SLionel Sambuc 			    TAILQ_FIRST(&sp->tiq)->lno;
54684d9c625SLionel Sambuc 		return (0);
54784d9c625SLionel Sambuc 	}
54884d9c625SLionel Sambuc 
54984d9c625SLionel Sambuc 	key.data = &lno;
55084d9c625SLionel Sambuc 	key.size = sizeof(lno);
55184d9c625SLionel Sambuc 
55284d9c625SLionel Sambuc 	sp->db_error = ep->db->seq(ep->db, &key, &data, R_LAST);
55384d9c625SLionel Sambuc 	switch (sp->db_error) {
55484d9c625SLionel Sambuc 	case 1:
55584d9c625SLionel Sambuc 		*lnop = 0;
55684d9c625SLionel Sambuc 		return (0);
55784d9c625SLionel Sambuc 	case -1:
55884d9c625SLionel Sambuc 		sp->db_error = errno;
55984d9c625SLionel Sambuc alloc_err:
56084d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "007|unable to get last line");
56184d9c625SLionel Sambuc 		*lnop = 0;
56284d9c625SLionel Sambuc 		return (1);
56384d9c625SLionel Sambuc         case 0:
56484d9c625SLionel Sambuc 		;
56584d9c625SLionel Sambuc 	}
56684d9c625SLionel Sambuc 
56784d9c625SLionel Sambuc 	memcpy(&lno, key.data, sizeof(lno));
56884d9c625SLionel Sambuc 
56984d9c625SLionel Sambuc 	if (lno != sp->c_lno) {
57084d9c625SLionel Sambuc 	    FILE2INT(sp, data.data, data.size, wp, wlen);
57184d9c625SLionel Sambuc 
57284d9c625SLionel Sambuc 	    /* Fill the cache. */
57384d9c625SLionel Sambuc 	    BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
57484d9c625SLionel Sambuc 	    MEMCPYW(sp->c_lp, wp, wlen);
57584d9c625SLionel Sambuc 	    sp->c_lno = lno;
57684d9c625SLionel Sambuc 	    sp->c_len = wlen;
57784d9c625SLionel Sambuc 	}
57884d9c625SLionel Sambuc 	ep->c_nlines = lno;
57984d9c625SLionel Sambuc 
58084d9c625SLionel Sambuc 	/* Return the value. */
58184d9c625SLionel Sambuc 	*lnop = (F_ISSET(sp, SC_TINPUT) &&
58284d9c625SLionel Sambuc 	    TAILQ_LAST(&sp->tiq, _texth)->lno > lno ?
58384d9c625SLionel Sambuc 	    TAILQ_LAST(&sp->tiq, _texth)->lno : lno);
58484d9c625SLionel Sambuc 	return (0);
58584d9c625SLionel Sambuc }
58684d9c625SLionel Sambuc 
58784d9c625SLionel Sambuc /*
58884d9c625SLionel Sambuc  * db_err --
58984d9c625SLionel Sambuc  *	Report a line error.
59084d9c625SLionel Sambuc  *
59184d9c625SLionel Sambuc  * PUBLIC: void db_err __P((SCR *, db_recno_t));
59284d9c625SLionel Sambuc  */
59384d9c625SLionel Sambuc void
db_err(SCR * sp,db_recno_t lno)59484d9c625SLionel Sambuc db_err(SCR *sp, db_recno_t lno)
59584d9c625SLionel Sambuc {
59684d9c625SLionel Sambuc 	msgq(sp, M_ERR,
59784d9c625SLionel Sambuc 	    "008|Error: unable to retrieve line %lu", (u_long)lno);
59884d9c625SLionel Sambuc }
59984d9c625SLionel Sambuc 
60084d9c625SLionel Sambuc /*
60184d9c625SLionel Sambuc  * scr_update --
60284d9c625SLionel Sambuc  *	Update all of the screens that are backed by the file that
60384d9c625SLionel Sambuc  *	just changed.
60484d9c625SLionel Sambuc  * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
60584d9c625SLionel Sambuc  * PUBLIC:                      lnop_t op, int current));
60684d9c625SLionel Sambuc  */
60784d9c625SLionel Sambuc int
scr_update(SCR * sp,db_recno_t lno,lnop_t op,int current)60884d9c625SLionel Sambuc scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
60984d9c625SLionel Sambuc {
61084d9c625SLionel Sambuc 	EXF *ep;
61184d9c625SLionel Sambuc 	SCR *tsp;
61284d9c625SLionel Sambuc 	WIN *wp;
61384d9c625SLionel Sambuc 
61484d9c625SLionel Sambuc 	if (F_ISSET(sp, SC_EX))
61584d9c625SLionel Sambuc 		return (0);
61684d9c625SLionel Sambuc 
61784d9c625SLionel Sambuc 	/* XXXX goes outside of window */
61884d9c625SLionel Sambuc 	ep = sp->ep;
61984d9c625SLionel Sambuc 	if (ep->refcnt != 1)
62084d9c625SLionel Sambuc 		TAILQ_FOREACH(wp, &sp->gp->dq, q)
62184d9c625SLionel Sambuc 			TAILQ_FOREACH(tsp, &wp->scrq, q)
62284d9c625SLionel Sambuc 				if (sp != tsp && tsp->ep == ep)
62384d9c625SLionel Sambuc 					if (vs_change(tsp, lno, op))
62484d9c625SLionel Sambuc 						return (1);
62584d9c625SLionel Sambuc 	return (current ? vs_change(sp, lno, op) : 0);
62684d9c625SLionel Sambuc }
62784d9c625SLionel Sambuc 
62884d9c625SLionel Sambuc /*
62984d9c625SLionel Sambuc  * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
63084d9c625SLionel Sambuc  */
63184d9c625SLionel Sambuc void
update_cache(SCR * sp,lnop_t op,db_recno_t lno)63284d9c625SLionel Sambuc update_cache(SCR *sp, lnop_t op, db_recno_t lno)
63384d9c625SLionel Sambuc {
63484d9c625SLionel Sambuc 	SCR* scrp;
63584d9c625SLionel Sambuc 	EXF *ep;
63684d9c625SLionel Sambuc 
63784d9c625SLionel Sambuc 	ep = sp->ep;
63884d9c625SLionel Sambuc 
63984d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
64084d9c625SLionel Sambuc 	/* The flushing is probably not needed, since it was incorrect
64184d9c625SLionel Sambuc 	 * for db_insert.  It might be better to adjust it, like
64284d9c625SLionel Sambuc 	 * marks, @ and global
64384d9c625SLionel Sambuc 	 */
64484d9c625SLionel Sambuc 	TAILQ_FOREACH(scrp, &ep->scrq, eq)
64584d9c625SLionel Sambuc 		switch (op) {
64684d9c625SLionel Sambuc 		case LINE_INSERT:
64784d9c625SLionel Sambuc 		case LINE_DELETE:
64884d9c625SLionel Sambuc 			if (lno <= scrp->c_lno)
64984d9c625SLionel Sambuc 				scrp->c_lno = OOBLNO;
65084d9c625SLionel Sambuc 			break;
65184d9c625SLionel Sambuc 		case LINE_RESET:
65284d9c625SLionel Sambuc 			if (lno == scrp->c_lno)
65384d9c625SLionel Sambuc 				scrp->c_lno = OOBLNO;
65484d9c625SLionel Sambuc 			break;
65584d9c625SLionel Sambuc 		case LINE_APPEND:
65684d9c625SLionel Sambuc 			break;
65784d9c625SLionel Sambuc 		}
65884d9c625SLionel Sambuc 
65984d9c625SLionel Sambuc 	if (ep->c_nlines != OOBLNO)
66084d9c625SLionel Sambuc 		switch (op) {
66184d9c625SLionel Sambuc 		case LINE_INSERT:
66284d9c625SLionel Sambuc 			++ep->c_nlines;
66384d9c625SLionel Sambuc 			break;
66484d9c625SLionel Sambuc 		case LINE_DELETE:
66584d9c625SLionel Sambuc 			--ep->c_nlines;
66684d9c625SLionel Sambuc 			break;
66784d9c625SLionel Sambuc 		case LINE_APPEND:
66884d9c625SLionel Sambuc 		case LINE_RESET:
66984d9c625SLionel Sambuc 			break;
67084d9c625SLionel Sambuc 		}
67184d9c625SLionel Sambuc }
67284d9c625SLionel Sambuc 
67384d9c625SLionel Sambuc /*
67484d9c625SLionel Sambuc  * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
67584d9c625SLionel Sambuc  */
db_msg_open(SCR * sp,const char * file,DB ** dbp)67684d9c625SLionel Sambuc int db_msg_open(SCR *sp, const char *file, DB **dbp)
67784d9c625SLionel Sambuc {
67884d9c625SLionel Sambuc 	*dbp = dbopen(file, O_NONBLOCK | O_RDONLY, 0, DB_RECNO, NULL);
67984d9c625SLionel Sambuc 
68084d9c625SLionel Sambuc 	return *dbp == NULL;
68184d9c625SLionel Sambuc }
68284d9c625SLionel Sambuc 
68384d9c625SLionel Sambuc /*
68484d9c625SLionel Sambuc  * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
68584d9c625SLionel Sambuc  */
68684d9c625SLionel Sambuc int
db_init(SCR * sp,EXF * ep,char * rcv_name,char * oname,size_t psize,int * open_err)68784d9c625SLionel Sambuc db_init(SCR *sp, EXF *ep, char *rcv_name, char *oname, size_t psize, int *open_err)
68884d9c625SLionel Sambuc {
68984d9c625SLionel Sambuc 	RECNOINFO oinfo;
69084d9c625SLionel Sambuc 
69184d9c625SLionel Sambuc 	memset(&oinfo, 0, sizeof(RECNOINFO));
69284d9c625SLionel Sambuc 	oinfo.bval = '\n';			/* Always set. */
69384d9c625SLionel Sambuc 	/*
69484d9c625SLionel Sambuc 	 * If we are not recovering, set the pagesize and arrange to
69584d9c625SLionel Sambuc 	 * first get a snapshot of the file.
69684d9c625SLionel Sambuc 	 */
69784d9c625SLionel Sambuc 	if (rcv_name == NULL) {
69884d9c625SLionel Sambuc 		oinfo.psize = psize;
69984d9c625SLionel Sambuc 		oinfo.flags = R_SNAPSHOT;
70084d9c625SLionel Sambuc 	}
70184d9c625SLionel Sambuc 	/*
70284d9c625SLionel Sambuc 	 * Always set the btree name, otherwise we are going to be using
70384d9c625SLionel Sambuc 	 * an in-memory database for the btree.
70484d9c625SLionel Sambuc 	 */
70584d9c625SLionel Sambuc 	oinfo.bfname = ep->rcv_path;
70684d9c625SLionel Sambuc 
70784d9c625SLionel Sambuc #define _DB_OPEN_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
70884d9c625SLionel Sambuc 
70984d9c625SLionel Sambuc 	ep->db = dbopen(rcv_name == NULL ? oname : NULL,
71084d9c625SLionel Sambuc 	    O_NONBLOCK | O_RDONLY, _DB_OPEN_MODE, DB_RECNO, &oinfo);
71184d9c625SLionel Sambuc 
71284d9c625SLionel Sambuc 	if (!ep->db) {
71384d9c625SLionel Sambuc 		msgq_str(sp,
71484d9c625SLionel Sambuc 		    M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
71584d9c625SLionel Sambuc 		/*
71684d9c625SLionel Sambuc 		 * !!!
71784d9c625SLionel Sambuc 		 * Historically, vi permitted users to edit files that couldn't
71884d9c625SLionel Sambuc 		 * be read.  This isn't useful for single files from a command
71984d9c625SLionel Sambuc 		 * line, but it's quite useful for "vi *.c", since you can skip
72084d9c625SLionel Sambuc 		 * past files that you can't read.
72184d9c625SLionel Sambuc 		 */
72284d9c625SLionel Sambuc 		ep->db = NULL; /* Don't close it; it wasn't opened */
72384d9c625SLionel Sambuc 
72484d9c625SLionel Sambuc 		*open_err = 1;
72584d9c625SLionel Sambuc 		return 1;
72684d9c625SLionel Sambuc 	} else {
72784d9c625SLionel Sambuc 		/*
72884d9c625SLionel Sambuc 		 * We always sync the underlying btree so that the header
72984d9c625SLionel Sambuc 		 * is written first
73084d9c625SLionel Sambuc 		 */
73184d9c625SLionel Sambuc 		ep->db->sync(ep->db, R_RECNOSYNC);
73284d9c625SLionel Sambuc 	}
73384d9c625SLionel Sambuc 
73484d9c625SLionel Sambuc 	return 0;
73584d9c625SLionel Sambuc }
73684d9c625SLionel Sambuc 
73784d9c625SLionel Sambuc const char *
db_strerror(int error)73884d9c625SLionel Sambuc db_strerror(int error)
73984d9c625SLionel Sambuc {
74084d9c625SLionel Sambuc 	return error > 0 ? strerror(error) : "record not found";
74184d9c625SLionel Sambuc }
742