xref: /minix3/external/bsd/nvi/dist/common/vi_db.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: db.c,v 10.48 2002/06/08 19:32:52 skimo Exp  (Berkeley) Date: 2002/06/08 19:32:52 ";
1684d9c625SLionel Sambuc #endif /* not lint */
17*0a6a1f1dSLionel Sambuc #else
18*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: vi_db.c,v 1.6 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 <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc #include <stdlib.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc #include "common.h"
3584d9c625SLionel Sambuc #include "dbinternal.h"
3684d9c625SLionel Sambuc #include "../vi/vi.h"
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc static int append __P((SCR*, db_recno_t, const CHAR_T*, size_t, lnop_t, int));
3984d9c625SLionel Sambuc 
4084d9c625SLionel Sambuc /*
4184d9c625SLionel Sambuc  * db_eget --
4284d9c625SLionel Sambuc  *	Front-end to db_get, special case handling for empty files.
4384d9c625SLionel Sambuc  *
4484d9c625SLionel Sambuc  * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
4584d9c625SLionel Sambuc  */
4684d9c625SLionel Sambuc int
db_eget(SCR * sp,db_recno_t lno,CHAR_T ** pp,size_t * lenp,int * isemptyp)4784d9c625SLionel Sambuc db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
4884d9c625SLionel Sambuc 
4984d9c625SLionel Sambuc 	               				/* Line number. */
5084d9c625SLionel Sambuc 	            				/* Pointer store. */
5184d9c625SLionel Sambuc 	             				/* Length store. */
5284d9c625SLionel Sambuc 
5384d9c625SLionel Sambuc {
5484d9c625SLionel Sambuc 	db_recno_t l1;
5584d9c625SLionel Sambuc 
5684d9c625SLionel Sambuc 	if (isemptyp != NULL)
5784d9c625SLionel Sambuc 		*isemptyp = 0;
5884d9c625SLionel Sambuc 
5984d9c625SLionel Sambuc 	/* If the line exists, simply return it. */
6084d9c625SLionel Sambuc 	if (!db_get(sp, lno, 0, pp, lenp))
6184d9c625SLionel Sambuc 		return (0);
6284d9c625SLionel Sambuc 
6384d9c625SLionel Sambuc 	/*
6484d9c625SLionel Sambuc 	 * If the user asked for line 0 or line 1, i.e. the only possible
6584d9c625SLionel Sambuc 	 * line in an empty file, find the last line of the file; db_last
6684d9c625SLionel Sambuc 	 * fails loudly.
6784d9c625SLionel Sambuc 	 */
6884d9c625SLionel Sambuc 	if ((lno == 0 || lno == 1) && db_last(sp, &l1))
6984d9c625SLionel Sambuc 		return (1);
7084d9c625SLionel Sambuc 
7184d9c625SLionel Sambuc 	/* If the file isn't empty, fail loudly. */
7284d9c625SLionel Sambuc 	if ((lno != 0 && lno != 1) || l1 != 0) {
7384d9c625SLionel Sambuc 		db_err(sp, lno);
7484d9c625SLionel Sambuc 		return (1);
7584d9c625SLionel Sambuc 	}
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc 	if (isemptyp != NULL)
7884d9c625SLionel Sambuc 		*isemptyp = 1;
7984d9c625SLionel Sambuc 
8084d9c625SLionel Sambuc 	return (1);
8184d9c625SLionel Sambuc }
8284d9c625SLionel Sambuc 
8384d9c625SLionel Sambuc /*
8484d9c625SLionel Sambuc  * db_get --
8584d9c625SLionel Sambuc  *	Look in the text buffers for a line, followed by the cache, followed
8684d9c625SLionel Sambuc  *	by the database.
8784d9c625SLionel Sambuc  *
8884d9c625SLionel Sambuc  * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
8984d9c625SLionel Sambuc  */
9084d9c625SLionel Sambuc int
db_get(SCR * sp,db_recno_t lno,u_int32_t flags,CHAR_T ** pp,size_t * lenp)9184d9c625SLionel Sambuc db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
9284d9c625SLionel Sambuc 		/* Line number. */ /* Pointer store. */ /* Length store. */
9384d9c625SLionel Sambuc {
9484d9c625SLionel Sambuc 	DBT data, key;
9584d9c625SLionel Sambuc 	EXF *ep;
9684d9c625SLionel Sambuc 	TEXT *tp;
9784d9c625SLionel Sambuc 	db_recno_t l1, l2;
9884d9c625SLionel Sambuc 	const CHAR_T *wp;
9984d9c625SLionel Sambuc 	size_t wlen;
10084d9c625SLionel Sambuc 	size_t nlen;
10184d9c625SLionel Sambuc 
10284d9c625SLionel Sambuc 	/*
10384d9c625SLionel Sambuc 	 * The underlying recno stuff handles zero by returning NULL, but
10484d9c625SLionel Sambuc 	 * have to have an OOB condition for the look-aside into the input
10584d9c625SLionel Sambuc 	 * buffer anyway.
10684d9c625SLionel Sambuc 	 */
10784d9c625SLionel Sambuc 	if (lno == 0)
10884d9c625SLionel Sambuc 		goto err1;
10984d9c625SLionel Sambuc 
11084d9c625SLionel Sambuc 	/* Check for no underlying file. */
11184d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
11284d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
11384d9c625SLionel Sambuc 		goto err3;
11484d9c625SLionel Sambuc 	}
11584d9c625SLionel Sambuc 
11684d9c625SLionel Sambuc 	if (LF_ISSET(DBG_NOCACHE))
11784d9c625SLionel Sambuc 		goto nocache;
11884d9c625SLionel Sambuc 
11984d9c625SLionel Sambuc 	/*
12084d9c625SLionel Sambuc 	 * Look-aside into the TEXT buffers and see if the line we want
12184d9c625SLionel Sambuc 	 * is there.
12284d9c625SLionel Sambuc 	 */
12384d9c625SLionel Sambuc 	if (F_ISSET(sp, SC_TINPUT)) {
12484d9c625SLionel Sambuc 		l1 = TAILQ_FIRST(&sp->tiq)->lno;
12584d9c625SLionel Sambuc 		l2 = TAILQ_LAST(&sp->tiq, _texth)->lno;
12684d9c625SLionel Sambuc 		if (l1 <= lno && l2 >= lno) {
12784d9c625SLionel Sambuc #if defined(DEBUG) && 0
12884d9c625SLionel Sambuc 			vtrace(sp,
12984d9c625SLionel Sambuc 			    "retrieve TEXT buffer line %lu\n", (u_long)lno);
13084d9c625SLionel Sambuc #endif
13184d9c625SLionel Sambuc 			for (tp = TAILQ_FIRST(&sp->tiq);
13284d9c625SLionel Sambuc 			    tp->lno != lno; tp = TAILQ_NEXT(tp, q));
13384d9c625SLionel Sambuc 			if (lenp != NULL)
13484d9c625SLionel Sambuc 				*lenp = tp->len;
13584d9c625SLionel Sambuc 			if (pp != NULL)
13684d9c625SLionel Sambuc 				*pp = tp->lb;
13784d9c625SLionel Sambuc 			return (0);
13884d9c625SLionel Sambuc 		}
13984d9c625SLionel Sambuc 		/*
14084d9c625SLionel Sambuc 		 * Adjust the line number for the number of lines used
14184d9c625SLionel Sambuc 		 * by the text input buffers.
14284d9c625SLionel Sambuc 		 */
14384d9c625SLionel Sambuc 		if (lno > l2)
14484d9c625SLionel Sambuc 			lno -= l2 - l1;
14584d9c625SLionel Sambuc 	}
14684d9c625SLionel Sambuc 
14784d9c625SLionel Sambuc 	/* Look-aside into the cache, and see if the line we want is there. */
14884d9c625SLionel Sambuc 	if (lno == sp->c_lno) {
14984d9c625SLionel Sambuc #if defined(DEBUG) && 0
15084d9c625SLionel Sambuc 		vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
15184d9c625SLionel Sambuc #endif
15284d9c625SLionel Sambuc 		if (lenp != NULL)
15384d9c625SLionel Sambuc 			*lenp = sp->c_len;
15484d9c625SLionel Sambuc 		if (pp != NULL)
15584d9c625SLionel Sambuc 			*pp = sp->c_lp;
15684d9c625SLionel Sambuc 		return (0);
15784d9c625SLionel Sambuc 	}
15884d9c625SLionel Sambuc 	sp->c_lno = OOBLNO;
15984d9c625SLionel Sambuc 
16084d9c625SLionel Sambuc nocache:
16184d9c625SLionel Sambuc 	nlen = 1024;
16284d9c625SLionel Sambuc retry:
16384d9c625SLionel Sambuc 	/* data.size contains length in bytes */
16484d9c625SLionel Sambuc 	BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
16584d9c625SLionel Sambuc 
16684d9c625SLionel Sambuc 	/* Get the line from the underlying database. */
16784d9c625SLionel Sambuc 	memset(&key, 0, sizeof(key));
16884d9c625SLionel Sambuc 	key.data = &lno;
16984d9c625SLionel Sambuc 	key.size = sizeof(lno);
17084d9c625SLionel Sambuc 	memset(&data, 0, sizeof(data));
17184d9c625SLionel Sambuc 	data.data = sp->c_lp;
17284d9c625SLionel Sambuc 	data.ulen = sp->c_blen;
17384d9c625SLionel Sambuc 	data.flags = DB_DBT_USERMEM;
17484d9c625SLionel Sambuc 	switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
17584d9c625SLionel Sambuc 	case DB_BUFFER_SMALL:
17684d9c625SLionel Sambuc 		nlen = data.size;
17784d9c625SLionel Sambuc 		goto retry;
17884d9c625SLionel Sambuc         default:
17984d9c625SLionel Sambuc 		goto err2;
18084d9c625SLionel Sambuc 	case DB_NOTFOUND:
18184d9c625SLionel Sambuc err1:		if (LF_ISSET(DBG_FATAL))
18284d9c625SLionel Sambuc err2:			db_err(sp, lno);
18384d9c625SLionel Sambuc alloc_err:
18484d9c625SLionel Sambuc err3:		if (lenp != NULL)
18584d9c625SLionel Sambuc 			*lenp = 0;
18684d9c625SLionel Sambuc 		if (pp != NULL)
18784d9c625SLionel Sambuc 			*pp = NULL;
18884d9c625SLionel Sambuc 		return (1);
18984d9c625SLionel Sambuc 	case 0:
19084d9c625SLionel Sambuc 		;
19184d9c625SLionel Sambuc 	}
19284d9c625SLionel Sambuc 
19384d9c625SLionel Sambuc 	if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
19484d9c625SLionel Sambuc 	    if (!F_ISSET(sp, SC_CONV_ERROR)) {
19584d9c625SLionel Sambuc 		F_SET(sp, SC_CONV_ERROR);
19684d9c625SLionel Sambuc 		msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
19784d9c625SLionel Sambuc 	    }
19884d9c625SLionel Sambuc 	    goto err3;
19984d9c625SLionel Sambuc 	}
20084d9c625SLionel Sambuc 
20184d9c625SLionel Sambuc 	/* Reset the cache. */
20284d9c625SLionel Sambuc 	if (wp != data.data) {
20384d9c625SLionel Sambuc 	    BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
20484d9c625SLionel Sambuc 	    MEMCPYW(sp->c_lp, wp, wlen);
20584d9c625SLionel Sambuc 	}
20684d9c625SLionel Sambuc 	sp->c_lno = lno;
20784d9c625SLionel Sambuc 	sp->c_len = wlen;
20884d9c625SLionel Sambuc 
20984d9c625SLionel Sambuc #if defined(DEBUG) && 0
21084d9c625SLionel Sambuc 	vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
21184d9c625SLionel Sambuc #endif
21284d9c625SLionel Sambuc 	if (lenp != NULL)
21384d9c625SLionel Sambuc 		*lenp = wlen;
21484d9c625SLionel Sambuc 	if (pp != NULL)
21584d9c625SLionel Sambuc 		*pp = sp->c_lp;
21684d9c625SLionel Sambuc 	return (0);
21784d9c625SLionel Sambuc }
21884d9c625SLionel Sambuc 
21984d9c625SLionel Sambuc /*
22084d9c625SLionel Sambuc  * db_delete --
22184d9c625SLionel Sambuc  *	Delete a line from the file.
22284d9c625SLionel Sambuc  *
22384d9c625SLionel Sambuc  * PUBLIC: int db_delete __P((SCR *, db_recno_t));
22484d9c625SLionel Sambuc  */
22584d9c625SLionel Sambuc int
db_delete(SCR * sp,db_recno_t lno)22684d9c625SLionel Sambuc db_delete(SCR *sp, db_recno_t lno)
22784d9c625SLionel Sambuc {
22884d9c625SLionel Sambuc 	DBT key;
22984d9c625SLionel Sambuc 	EXF *ep;
23084d9c625SLionel Sambuc 
23184d9c625SLionel Sambuc #if defined(DEBUG) && 0
23284d9c625SLionel Sambuc 	vtrace(sp, "delete line %lu\n", (u_long)lno);
23384d9c625SLionel Sambuc #endif
23484d9c625SLionel Sambuc 	/* Check for no underlying file. */
23584d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
23684d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
23784d9c625SLionel Sambuc 		return (1);
23884d9c625SLionel Sambuc 	}
23984d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
24084d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
24184d9c625SLionel Sambuc 		return 1;
24284d9c625SLionel Sambuc 	}
24384d9c625SLionel Sambuc 
24484d9c625SLionel Sambuc 	/* Update marks, @ and global commands. */
24584d9c625SLionel Sambuc 	if (line_insdel(sp, LINE_DELETE, lno))
24684d9c625SLionel Sambuc 		return 1;
24784d9c625SLionel Sambuc 
24884d9c625SLionel Sambuc 	/* Log before change. */
24984d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_DELETE_B);
25084d9c625SLionel Sambuc 
25184d9c625SLionel Sambuc 	/* Update file. */
25284d9c625SLionel Sambuc 	memset(&key, 0, sizeof(key));
25384d9c625SLionel Sambuc 	key.data = &lno;
25484d9c625SLionel Sambuc 	key.size = sizeof(lno);
25584d9c625SLionel Sambuc 	if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
25684d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "003|unable to delete line %lu",
25784d9c625SLionel Sambuc 		    (u_long)lno);
25884d9c625SLionel Sambuc 		return (1);
25984d9c625SLionel Sambuc 	}
26084d9c625SLionel Sambuc 
26184d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
26284d9c625SLionel Sambuc 	update_cache(sp, LINE_DELETE, lno);
26384d9c625SLionel Sambuc 
26484d9c625SLionel Sambuc 	/* File now modified. */
26584d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
26684d9c625SLionel Sambuc 		(void)rcv_init(sp);
26784d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
26884d9c625SLionel Sambuc 
26984d9c625SLionel Sambuc 	/* Log after change. */
27084d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_DELETE_F);
27184d9c625SLionel Sambuc 
27284d9c625SLionel Sambuc 	/* Update screen. */
27384d9c625SLionel Sambuc 	return (scr_update(sp, lno, LINE_DELETE, 1));
27484d9c625SLionel Sambuc }
27584d9c625SLionel Sambuc 
27684d9c625SLionel Sambuc /* maybe this could be simpler
27784d9c625SLionel Sambuc  *
27884d9c625SLionel Sambuc  * DB3 behaves differently from DB1
27984d9c625SLionel Sambuc  *
28084d9c625SLionel Sambuc  * if lno != 0 just go to lno and put the new line after it
28184d9c625SLionel Sambuc  * if lno == 0 then if there are any record, put in front of the first
28284d9c625SLionel Sambuc  *		    otherwise just append to the end thus creating the first
28384d9c625SLionel Sambuc  *				line
28484d9c625SLionel Sambuc  */
28584d9c625SLionel Sambuc static int
append(SCR * sp,db_recno_t lno,const CHAR_T * p,size_t len,lnop_t op,int update)28684d9c625SLionel Sambuc append(SCR *sp, db_recno_t lno, const CHAR_T *p, size_t len, lnop_t op, int update)
28784d9c625SLionel Sambuc {
28884d9c625SLionel Sambuc 	DBT data, key;
28984d9c625SLionel Sambuc 	DBC *dbcp_put;
29084d9c625SLionel Sambuc 	EXF *ep;
29184d9c625SLionel Sambuc 	const char *fp;
29284d9c625SLionel Sambuc 	size_t flen;
29384d9c625SLionel Sambuc 	int rval;
29484d9c625SLionel Sambuc 
29584d9c625SLionel Sambuc 	/* Check for no underlying file. */
29684d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
29784d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
29884d9c625SLionel Sambuc 		return (1);
29984d9c625SLionel Sambuc 	}
30084d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
30184d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
30284d9c625SLionel Sambuc 		return 1;
30384d9c625SLionel Sambuc 	}
30484d9c625SLionel Sambuc 
30584d9c625SLionel Sambuc 	/* Log before change. */
30684d9c625SLionel Sambuc 	log_line(sp, lno + 1, LOG_LINE_APPEND_B);
30784d9c625SLionel Sambuc 
30884d9c625SLionel Sambuc 	/* Update file. */
30984d9c625SLionel Sambuc 	memset(&key, 0, sizeof(key));
31084d9c625SLionel Sambuc 	key.data = &lno;
31184d9c625SLionel Sambuc 	key.size = sizeof(lno);
31284d9c625SLionel Sambuc 	memset(&data, 0, sizeof(data));
31384d9c625SLionel Sambuc 
31484d9c625SLionel Sambuc 	if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
31584d9c625SLionel Sambuc 	    return 1;
31684d9c625SLionel Sambuc 
31784d9c625SLionel Sambuc 	INT2FILE(sp, p, len, fp, flen);
31884d9c625SLionel Sambuc 
31984d9c625SLionel Sambuc 	if (lno != 0) {
32084d9c625SLionel Sambuc 	    if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
32184d9c625SLionel Sambuc 		goto err2;
32284d9c625SLionel Sambuc 
32384d9c625SLionel Sambuc 	    data.data = __UNCONST(fp);
32484d9c625SLionel Sambuc 	    data.size = flen;
32584d9c625SLionel Sambuc 	    if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
32684d9c625SLionel Sambuc err2:
32784d9c625SLionel Sambuc 		(void)dbcp_put->c_close(dbcp_put);
32884d9c625SLionel Sambuc 		msgq(sp, M_DBERR,
32984d9c625SLionel Sambuc 			(op == LINE_APPEND)
33084d9c625SLionel Sambuc 			    ? "004|unable to append to line %lu"
33184d9c625SLionel Sambuc 			    : "005|unable to insert at line %lu",
33284d9c625SLionel Sambuc 			(u_long)lno);
33384d9c625SLionel Sambuc 		return (1);
33484d9c625SLionel Sambuc 	    }
33584d9c625SLionel Sambuc 	} else {
33684d9c625SLionel Sambuc 	    if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
33784d9c625SLionel Sambuc 		if (sp->db_error != DB_NOTFOUND)
33884d9c625SLionel Sambuc 		    goto err2;
33984d9c625SLionel Sambuc 
34084d9c625SLionel Sambuc 		data.data = __UNCONST(fp);
34184d9c625SLionel Sambuc 		data.size = flen;
34284d9c625SLionel Sambuc 		if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
34384d9c625SLionel Sambuc 		    goto err2;
34484d9c625SLionel Sambuc 		}
34584d9c625SLionel Sambuc 	    } else {
34684d9c625SLionel Sambuc 		key.data = &lno;
34784d9c625SLionel Sambuc 		key.size = sizeof(lno);
34884d9c625SLionel Sambuc 		data.data = __UNCONST(fp);
34984d9c625SLionel Sambuc 		data.size = flen;
35084d9c625SLionel Sambuc 		if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
35184d9c625SLionel Sambuc 		    goto err2;
35284d9c625SLionel Sambuc 		}
35384d9c625SLionel Sambuc 	    }
35484d9c625SLionel Sambuc 	}
35584d9c625SLionel Sambuc 
35684d9c625SLionel Sambuc 	(void)dbcp_put->c_close(dbcp_put);
35784d9c625SLionel Sambuc 
35884d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
35984d9c625SLionel Sambuc 	update_cache(sp, LINE_INSERT, lno);
36084d9c625SLionel Sambuc 
36184d9c625SLionel Sambuc 	/* File now dirty. */
36284d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
36384d9c625SLionel Sambuc 		(void)rcv_init(sp);
36484d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
36584d9c625SLionel Sambuc 
36684d9c625SLionel Sambuc 	/* Log after change. */
36784d9c625SLionel Sambuc 	log_line(sp, lno + 1, LOG_LINE_APPEND_F);
36884d9c625SLionel Sambuc 
36984d9c625SLionel Sambuc 	/* Update marks, @ and global commands. */
37084d9c625SLionel Sambuc 	rval = line_insdel(sp, LINE_INSERT, lno + 1);
37184d9c625SLionel Sambuc 
37284d9c625SLionel Sambuc 	/*
37384d9c625SLionel Sambuc 	 * Update screen.
37484d9c625SLionel Sambuc 	 *
37584d9c625SLionel Sambuc 	 * comment copied from db_append
37684d9c625SLionel Sambuc 	 * XXX
37784d9c625SLionel Sambuc 	 * Nasty hack.  If multiple lines are input by the user, they aren't
37884d9c625SLionel Sambuc 	 * committed until an <ESC> is entered.  The problem is the screen was
37984d9c625SLionel Sambuc 	 * updated/scrolled as each line was entered.  So, when this routine
38084d9c625SLionel Sambuc 	 * is called to copy the new lines from the cut buffer into the file,
38184d9c625SLionel Sambuc 	 * it has to know not to update the screen again.
38284d9c625SLionel Sambuc 	 */
38384d9c625SLionel Sambuc 	return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
38484d9c625SLionel Sambuc }
38584d9c625SLionel Sambuc 
38684d9c625SLionel Sambuc /*
38784d9c625SLionel Sambuc  * db_append --
38884d9c625SLionel Sambuc  *	Append a line into the file.
38984d9c625SLionel Sambuc  *
39084d9c625SLionel Sambuc  * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
39184d9c625SLionel Sambuc  */
39284d9c625SLionel Sambuc int
db_append(SCR * sp,int update,db_recno_t lno,const CHAR_T * p,size_t len)39384d9c625SLionel Sambuc db_append(SCR *sp, int update, db_recno_t lno, const CHAR_T *p, size_t len)
39484d9c625SLionel Sambuc {
39584d9c625SLionel Sambuc #if defined(DEBUG) && 0
39684d9c625SLionel Sambuc 	vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
39784d9c625SLionel Sambuc #endif
39884d9c625SLionel Sambuc 
39984d9c625SLionel Sambuc 	/* Update file. */
40084d9c625SLionel Sambuc 	return append(sp, lno, p, len, LINE_APPEND, update);
40184d9c625SLionel Sambuc }
40284d9c625SLionel Sambuc 
40384d9c625SLionel Sambuc /*
40484d9c625SLionel Sambuc  * db_insert --
40584d9c625SLionel Sambuc  *	Insert a line into the file.
40684d9c625SLionel Sambuc  *
40784d9c625SLionel Sambuc  * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
40884d9c625SLionel Sambuc  */
40984d9c625SLionel Sambuc int
db_insert(SCR * sp,db_recno_t lno,CHAR_T * p,size_t len)41084d9c625SLionel Sambuc db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
41184d9c625SLionel Sambuc {
41284d9c625SLionel Sambuc #if defined(DEBUG) && 0
41384d9c625SLionel Sambuc 	vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
41484d9c625SLionel Sambuc 	    (u_long)lno, (u_long)len, MIN(len, 20), p);
41584d9c625SLionel Sambuc #endif
41684d9c625SLionel Sambuc 	return append(sp, lno - 1, p, len, LINE_INSERT, 1);
41784d9c625SLionel Sambuc }
41884d9c625SLionel Sambuc 
41984d9c625SLionel Sambuc /*
42084d9c625SLionel Sambuc  * db_set --
42184d9c625SLionel Sambuc  *	Store a line in the file.
42284d9c625SLionel Sambuc  *
42384d9c625SLionel Sambuc  * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
42484d9c625SLionel Sambuc  */
42584d9c625SLionel Sambuc int
db_set(SCR * sp,db_recno_t lno,CHAR_T * p,size_t len)42684d9c625SLionel Sambuc db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
42784d9c625SLionel Sambuc {
42884d9c625SLionel Sambuc 	DBT data, key;
42984d9c625SLionel Sambuc 	EXF *ep;
43084d9c625SLionel Sambuc 	const char *fp;
43184d9c625SLionel Sambuc 	size_t flen;
43284d9c625SLionel Sambuc 
43384d9c625SLionel Sambuc #if defined(DEBUG) && 0
43484d9c625SLionel Sambuc 	vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
43584d9c625SLionel Sambuc 	    (u_long)lno, (u_long)len, MIN(len, 20), p);
43684d9c625SLionel Sambuc #endif
43784d9c625SLionel Sambuc 	/* Check for no underlying file. */
43884d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
43984d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
44084d9c625SLionel Sambuc 		return (1);
44184d9c625SLionel Sambuc 	}
44284d9c625SLionel Sambuc 	if (ep->l_win && ep->l_win != sp->wp) {
44384d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_LOCKED);
44484d9c625SLionel Sambuc 		return 1;
44584d9c625SLionel Sambuc 	}
44684d9c625SLionel Sambuc 
44784d9c625SLionel Sambuc 	/* Log before change. */
44884d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_RESET_B);
44984d9c625SLionel Sambuc 
45084d9c625SLionel Sambuc 	INT2FILE(sp, p, len, fp, flen);
45184d9c625SLionel Sambuc 
45284d9c625SLionel Sambuc 	/* Update file. */
45384d9c625SLionel Sambuc 	memset(&key, 0, sizeof(key));
45484d9c625SLionel Sambuc 	key.data = &lno;
45584d9c625SLionel Sambuc 	key.size = sizeof(lno);
45684d9c625SLionel Sambuc 	memset(&data, 0, sizeof(data));
45784d9c625SLionel Sambuc 	data.data = __UNCONST(fp);
45884d9c625SLionel Sambuc 	data.size = flen;
45984d9c625SLionel Sambuc 	if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
46084d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
46184d9c625SLionel Sambuc 		return (1);
46284d9c625SLionel Sambuc 	}
46384d9c625SLionel Sambuc 
46484d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
46584d9c625SLionel Sambuc 	update_cache(sp, LINE_RESET, lno);
46684d9c625SLionel Sambuc 
46784d9c625SLionel Sambuc 	/* File now dirty. */
46884d9c625SLionel Sambuc 	if (F_ISSET(ep, F_FIRSTMODIFY))
46984d9c625SLionel Sambuc 		(void)rcv_init(sp);
47084d9c625SLionel Sambuc 	F_SET(ep, F_MODIFIED);
47184d9c625SLionel Sambuc 
47284d9c625SLionel Sambuc 	/* Log after change. */
47384d9c625SLionel Sambuc 	log_line(sp, lno, LOG_LINE_RESET_F);
47484d9c625SLionel Sambuc 
47584d9c625SLionel Sambuc 	/* Update screen. */
47684d9c625SLionel Sambuc 	return (scr_update(sp, lno, LINE_RESET, 1));
47784d9c625SLionel Sambuc }
47884d9c625SLionel Sambuc 
47984d9c625SLionel Sambuc /*
48084d9c625SLionel Sambuc  * db_exist --
48184d9c625SLionel Sambuc  *	Return if a line exists.
48284d9c625SLionel Sambuc  *
48384d9c625SLionel Sambuc  * PUBLIC: int db_exist __P((SCR *, db_recno_t));
48484d9c625SLionel Sambuc  */
48584d9c625SLionel Sambuc int
db_exist(SCR * sp,db_recno_t lno)48684d9c625SLionel Sambuc db_exist(SCR *sp, db_recno_t lno)
48784d9c625SLionel Sambuc {
48884d9c625SLionel Sambuc 	EXF *ep;
48984d9c625SLionel Sambuc 
49084d9c625SLionel Sambuc 	/* Check for no underlying file. */
49184d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
49284d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
49384d9c625SLionel Sambuc 		return (1);
49484d9c625SLionel Sambuc 	}
49584d9c625SLionel Sambuc 
49684d9c625SLionel Sambuc 	if (lno == OOBLNO)
49784d9c625SLionel Sambuc 		return (0);
49884d9c625SLionel Sambuc 
49984d9c625SLionel Sambuc 	/*
50084d9c625SLionel Sambuc 	 * Check the last-line number cache.  Adjust the cached line
50184d9c625SLionel Sambuc 	 * number for the lines used by the text input buffers.
50284d9c625SLionel Sambuc 	 */
50384d9c625SLionel Sambuc 	if (ep->c_nlines != OOBLNO)
50484d9c625SLionel Sambuc 		return (lno <= (F_ISSET(sp, SC_TINPUT) ?
50584d9c625SLionel Sambuc 		    ep->c_nlines + TAILQ_LAST(&sp->tiq, _texth)->lno -
50684d9c625SLionel Sambuc 		    TAILQ_FIRST(&sp->tiq)->lno : ep->c_nlines));
50784d9c625SLionel Sambuc 
50884d9c625SLionel Sambuc 	/* Go get the line. */
50984d9c625SLionel Sambuc 	return (!db_get(sp, lno, 0, NULL, NULL));
51084d9c625SLionel Sambuc }
51184d9c625SLionel Sambuc 
51284d9c625SLionel Sambuc /*
51384d9c625SLionel Sambuc  * db_last --
51484d9c625SLionel Sambuc  *	Return the number of lines in the file.
51584d9c625SLionel Sambuc  *
51684d9c625SLionel Sambuc  * PUBLIC: int db_last __P((SCR *, db_recno_t *));
51784d9c625SLionel Sambuc  */
51884d9c625SLionel Sambuc int
db_last(SCR * sp,db_recno_t * lnop)51984d9c625SLionel Sambuc db_last(SCR *sp, db_recno_t *lnop)
52084d9c625SLionel Sambuc {
52184d9c625SLionel Sambuc 	DBT data, key;
52284d9c625SLionel Sambuc 	DBC *dbcp;
52384d9c625SLionel Sambuc 	EXF *ep;
52484d9c625SLionel Sambuc 	db_recno_t lno;
52584d9c625SLionel Sambuc 	const CHAR_T *wp;
52684d9c625SLionel Sambuc 	size_t wlen;
52784d9c625SLionel Sambuc 
52884d9c625SLionel Sambuc 	/* Check for no underlying file. */
52984d9c625SLionel Sambuc 	if ((ep = sp->ep) == NULL) {
53084d9c625SLionel Sambuc 		ex_emsg(sp, NULL, EXM_NOFILEYET);
53184d9c625SLionel Sambuc 		return (1);
53284d9c625SLionel Sambuc 	}
53384d9c625SLionel Sambuc 
53484d9c625SLionel Sambuc 	/*
53584d9c625SLionel Sambuc 	 * Check the last-line number cache.  Adjust the cached line
53684d9c625SLionel Sambuc 	 * number for the lines used by the text input buffers.
53784d9c625SLionel Sambuc 	 */
53884d9c625SLionel Sambuc 	if (ep->c_nlines != OOBLNO) {
53984d9c625SLionel Sambuc 		*lnop = ep->c_nlines;
54084d9c625SLionel Sambuc 		if (F_ISSET(sp, SC_TINPUT))
54184d9c625SLionel Sambuc 			*lnop += TAILQ_LAST(&sp->tiq, _texth)->lno -
54284d9c625SLionel Sambuc 			    TAILQ_FIRST(&sp->tiq)->lno;
54384d9c625SLionel Sambuc 		return (0);
54484d9c625SLionel Sambuc 	}
54584d9c625SLionel Sambuc 
54684d9c625SLionel Sambuc 	memset(&key, 0, sizeof(key));
54784d9c625SLionel Sambuc 	key.data = &lno;
54884d9c625SLionel Sambuc 	key.size = sizeof(lno);
54984d9c625SLionel Sambuc 	memset(&data, 0, sizeof(data));
55084d9c625SLionel Sambuc 
55184d9c625SLionel Sambuc 	if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
55284d9c625SLionel Sambuc 	    goto err1;
55384d9c625SLionel Sambuc 	switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
55484d9c625SLionel Sambuc         case DB_NOTFOUND:
55584d9c625SLionel Sambuc 		*lnop = 0;
55684d9c625SLionel Sambuc 		return (0);
55784d9c625SLionel Sambuc 	default:
55884d9c625SLionel Sambuc 		(void)dbcp->c_close(dbcp);
55984d9c625SLionel Sambuc alloc_err:
56084d9c625SLionel Sambuc err1:
56184d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "007|unable to get last line");
56284d9c625SLionel Sambuc 		*lnop = 0;
56384d9c625SLionel Sambuc 		return (1);
56484d9c625SLionel Sambuc         case 0:
56584d9c625SLionel Sambuc 		;
56684d9c625SLionel Sambuc 	}
56784d9c625SLionel Sambuc 
56884d9c625SLionel Sambuc 	memcpy(&lno, key.data, sizeof(lno));
56984d9c625SLionel Sambuc 
57084d9c625SLionel Sambuc 	if (lno != sp->c_lno) {
57184d9c625SLionel Sambuc 	    FILE2INT(sp, data.data, data.size, wp, wlen);
57284d9c625SLionel Sambuc 
57384d9c625SLionel Sambuc 	    /* Fill the cache. */
57484d9c625SLionel Sambuc 	    BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
57584d9c625SLionel Sambuc 	    MEMCPYW(sp->c_lp, wp, wlen);
57684d9c625SLionel Sambuc 	    sp->c_lno = lno;
57784d9c625SLionel Sambuc 	    sp->c_len = wlen;
57884d9c625SLionel Sambuc 	}
57984d9c625SLionel Sambuc 	ep->c_nlines = lno;
58084d9c625SLionel Sambuc 
58184d9c625SLionel Sambuc 	(void)dbcp->c_close(dbcp);
58284d9c625SLionel Sambuc 
58384d9c625SLionel Sambuc 	/* Return the value. */
58484d9c625SLionel Sambuc 	*lnop = (F_ISSET(sp, SC_TINPUT) &&
58584d9c625SLionel Sambuc 	    TAILQ_LAST(&sp->tiq, _texth)->lno > lno ?
58684d9c625SLionel Sambuc 	    TAILQ_LAST(&sp->tiq, _texth)->lno : lno);
58784d9c625SLionel Sambuc 	return (0);
58884d9c625SLionel Sambuc }
58984d9c625SLionel Sambuc 
59084d9c625SLionel Sambuc /*
59184d9c625SLionel Sambuc  * db_err --
59284d9c625SLionel Sambuc  *	Report a line error.
59384d9c625SLionel Sambuc  *
59484d9c625SLionel Sambuc  * PUBLIC: void db_err __P((SCR *, db_recno_t));
59584d9c625SLionel Sambuc  */
59684d9c625SLionel Sambuc void
db_err(SCR * sp,db_recno_t lno)59784d9c625SLionel Sambuc db_err(SCR *sp, db_recno_t lno)
59884d9c625SLionel Sambuc {
59984d9c625SLionel Sambuc 	msgq(sp, M_ERR,
60084d9c625SLionel Sambuc 	    "008|Error: unable to retrieve line %lu", (u_long)lno);
60184d9c625SLionel Sambuc }
60284d9c625SLionel Sambuc 
60384d9c625SLionel Sambuc /*
60484d9c625SLionel Sambuc  * scr_update --
60584d9c625SLionel Sambuc  *	Update all of the screens that are backed by the file that
60684d9c625SLionel Sambuc  *	just changed.
60784d9c625SLionel Sambuc  *
60884d9c625SLionel Sambuc  * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
60984d9c625SLionel Sambuc  * PUBLIC: 			lnop_t op, int current));
61084d9c625SLionel Sambuc  */
61184d9c625SLionel Sambuc int
scr_update(SCR * sp,db_recno_t lno,lnop_t op,int current)61284d9c625SLionel Sambuc scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
61384d9c625SLionel Sambuc {
61484d9c625SLionel Sambuc 	EXF *ep;
61584d9c625SLionel Sambuc 	SCR *tsp;
61684d9c625SLionel Sambuc 	WIN *wp;
61784d9c625SLionel Sambuc 
61884d9c625SLionel Sambuc 	if (F_ISSET(sp, SC_EX))
61984d9c625SLionel Sambuc 		return (0);
62084d9c625SLionel Sambuc 
62184d9c625SLionel Sambuc 	/* XXXX goes outside of window */
62284d9c625SLionel Sambuc 	ep = sp->ep;
62384d9c625SLionel Sambuc 	if (ep->refcnt != 1)
62484d9c625SLionel Sambuc 		TAILQ_FOREACH(wp, &sp->gp->dq, q)
62584d9c625SLionel Sambuc 			TAILQ_FOREACH(tsp, &wp->scrq, q)
62684d9c625SLionel Sambuc 				if (sp != tsp && tsp->ep == ep)
62784d9c625SLionel Sambuc 					if (vs_change(tsp, lno, op))
62884d9c625SLionel Sambuc 						return (1);
62984d9c625SLionel Sambuc 	return (current ? vs_change(sp, lno, op) : 0);
63084d9c625SLionel Sambuc }
63184d9c625SLionel Sambuc 
63284d9c625SLionel Sambuc /*
63384d9c625SLionel Sambuc  * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
63484d9c625SLionel Sambuc  */
63584d9c625SLionel Sambuc void
update_cache(SCR * sp,lnop_t op,db_recno_t lno)63684d9c625SLionel Sambuc update_cache(SCR *sp, lnop_t op, db_recno_t lno)
63784d9c625SLionel Sambuc {
63884d9c625SLionel Sambuc 	SCR* scrp;
63984d9c625SLionel Sambuc 	EXF *ep;
64084d9c625SLionel Sambuc 
64184d9c625SLionel Sambuc 	ep = sp->ep;
64284d9c625SLionel Sambuc 
64384d9c625SLionel Sambuc 	/* Flush the cache, update line count, before screen update. */
64484d9c625SLionel Sambuc 	/* The flushing is probably not needed, since it was incorrect
64584d9c625SLionel Sambuc 	 * for db_insert.  It might be better to adjust it, like
64684d9c625SLionel Sambuc 	 * marks, @ and global
64784d9c625SLionel Sambuc 	 */
64884d9c625SLionel Sambuc 	TAILQ_FOREACH(scrp, &ep->scrq, eq)
64984d9c625SLionel Sambuc 		switch (op) {
65084d9c625SLionel Sambuc 		case LINE_INSERT:
65184d9c625SLionel Sambuc 		case LINE_DELETE:
65284d9c625SLionel Sambuc 			if (lno <= scrp->c_lno)
65384d9c625SLionel Sambuc 				scrp->c_lno = OOBLNO;
65484d9c625SLionel Sambuc 			break;
65584d9c625SLionel Sambuc 		case LINE_RESET:
65684d9c625SLionel Sambuc 			if (lno == scrp->c_lno)
65784d9c625SLionel Sambuc 				scrp->c_lno = OOBLNO;
65884d9c625SLionel Sambuc 			break;
65984d9c625SLionel Sambuc 		case LINE_APPEND:
66084d9c625SLionel Sambuc 			break;
66184d9c625SLionel Sambuc 		}
66284d9c625SLionel Sambuc 
66384d9c625SLionel Sambuc 	if (ep->c_nlines != OOBLNO)
66484d9c625SLionel Sambuc 		switch (op) {
66584d9c625SLionel Sambuc 		case LINE_INSERT:
66684d9c625SLionel Sambuc 			++ep->c_nlines;
66784d9c625SLionel Sambuc 			break;
66884d9c625SLionel Sambuc 		case LINE_DELETE:
66984d9c625SLionel Sambuc 			--ep->c_nlines;
67084d9c625SLionel Sambuc 			break;
67184d9c625SLionel Sambuc 		case LINE_APPEND:
67284d9c625SLionel Sambuc 		case LINE_RESET:
67384d9c625SLionel Sambuc 			break;
67484d9c625SLionel Sambuc 		}
67584d9c625SLionel Sambuc }
67684d9c625SLionel Sambuc 
67784d9c625SLionel Sambuc /*
67884d9c625SLionel Sambuc  * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
67984d9c625SLionel Sambuc  */
68084d9c625SLionel Sambuc int
line_insdel(SCR * sp,lnop_t op,db_recno_t lno)68184d9c625SLionel Sambuc line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
68284d9c625SLionel Sambuc {
68384d9c625SLionel Sambuc 	int rval;
68484d9c625SLionel Sambuc 
68584d9c625SLionel Sambuc 	/* Update marks, @ and global commands. */
68684d9c625SLionel Sambuc 	rval = 0;
68784d9c625SLionel Sambuc 	if (mark_insdel(sp, op, lno))
68884d9c625SLionel Sambuc 		rval = 1;
68984d9c625SLionel Sambuc 	if (ex_g_insdel(sp, op, lno))
69084d9c625SLionel Sambuc 		rval = 1;
69184d9c625SLionel Sambuc 
69284d9c625SLionel Sambuc 	return rval;
69384d9c625SLionel Sambuc }
69484d9c625SLionel Sambuc 
69584d9c625SLionel Sambuc #ifdef USE_DB4_LOGGING
69684d9c625SLionel Sambuc #define VI_DB_INIT_LOG	DB_INIT_LOG
69784d9c625SLionel Sambuc #else
69884d9c625SLionel Sambuc #define VI_DB_INIT_LOG	0
69984d9c625SLionel Sambuc #endif
70084d9c625SLionel Sambuc 
70184d9c625SLionel Sambuc /*
70284d9c625SLionel Sambuc  * PUBLIC: int db_setup __P((SCR *, EXF *));
70384d9c625SLionel Sambuc  */
70484d9c625SLionel Sambuc int
db_setup(SCR * sp,EXF * ep)70584d9c625SLionel Sambuc db_setup(SCR *sp, EXF *ep)
70684d9c625SLionel Sambuc {
70784d9c625SLionel Sambuc 	char path[MAXPATHLEN];
70884d9c625SLionel Sambuc 	int fd;
70984d9c625SLionel Sambuc 	DB_ENV	*env;
71084d9c625SLionel Sambuc 
71184d9c625SLionel Sambuc 	(void)snprintf(path, sizeof(path), "%s/vi.XXXXXX", O_STR(sp, O_RECDIR));
71284d9c625SLionel Sambuc 	if ((fd = mkstemp(path)) == -1) {
71384d9c625SLionel Sambuc 		msgq(sp, M_SYSERR, "%s", path);
71484d9c625SLionel Sambuc 		goto err;
71584d9c625SLionel Sambuc 	}
71684d9c625SLionel Sambuc 	(void)close(fd);
71784d9c625SLionel Sambuc 	(void)unlink(path);
71884d9c625SLionel Sambuc 	if (mkdir(path, S_IRWXU)) {
71984d9c625SLionel Sambuc 		msgq(sp, M_SYSERR, "%s", path);
72084d9c625SLionel Sambuc 		goto err;
72184d9c625SLionel Sambuc 	}
72284d9c625SLionel Sambuc 	if (db_env_create(&env, 0)) {
72384d9c625SLionel Sambuc 		msgq(sp, M_ERR, "env_create");
72484d9c625SLionel Sambuc 		goto err;
72584d9c625SLionel Sambuc 	}
72684d9c625SLionel Sambuc #ifdef USE_DB4_LOGGING
72784d9c625SLionel Sambuc 	if ((sp->db_error = vi_db_init_recover(env))) {
72884d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "init_recover");
72984d9c625SLionel Sambuc 		goto err;
73084d9c625SLionel Sambuc 	}
73184d9c625SLionel Sambuc 	if ((sp->db_error = __vi_init_recover(env))) {
73284d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "init_recover");
73384d9c625SLionel Sambuc 		goto err;
73484d9c625SLionel Sambuc 	}
73584d9c625SLionel Sambuc #endif
73684d9c625SLionel Sambuc 	if ((sp->db_error = db_env_open(env, path,
73784d9c625SLionel Sambuc 	    DB_PRIVATE | DB_CREATE | DB_INIT_MPOOL | VI_DB_THREAD
73884d9c625SLionel Sambuc 	    | VI_DB_INIT_LOG, 0)) != 0) {
73984d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "env->open");
74084d9c625SLionel Sambuc 		goto err;
74184d9c625SLionel Sambuc 	}
74284d9c625SLionel Sambuc 
74384d9c625SLionel Sambuc 	if ((ep->env_path = strdup(path)) == NULL) {
74484d9c625SLionel Sambuc 		msgq(sp, M_SYSERR, NULL);
74584d9c625SLionel Sambuc 		(void)rmdir(path);
74684d9c625SLionel Sambuc 		goto err;
74784d9c625SLionel Sambuc 	}
74884d9c625SLionel Sambuc 	ep->env = env;
74984d9c625SLionel Sambuc 	return 0;
75084d9c625SLionel Sambuc err:
75184d9c625SLionel Sambuc 	return 1;
75284d9c625SLionel Sambuc }
75384d9c625SLionel Sambuc 
75484d9c625SLionel Sambuc /* Round up v to the nearest power of 2 */
round_up(size_t v)75584d9c625SLionel Sambuc static size_t round_up(size_t v)
75684d9c625SLionel Sambuc {
75784d9c625SLionel Sambuc 	ssize_t old_v = v;
75884d9c625SLionel Sambuc 
75984d9c625SLionel Sambuc 	while (v) {
76084d9c625SLionel Sambuc 		old_v = v;
76184d9c625SLionel Sambuc 		v ^= v & -v;
76284d9c625SLionel Sambuc 	}
76384d9c625SLionel Sambuc 	return old_v << 1;
76484d9c625SLionel Sambuc }
76584d9c625SLionel Sambuc 
76684d9c625SLionel Sambuc /*
76784d9c625SLionel Sambuc  * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
76884d9c625SLionel Sambuc  */
db_msg_open(SCR * sp,const char * file,DB ** dbp)76984d9c625SLionel Sambuc int db_msg_open(SCR *sp, const char *file, DB **dbp)
77084d9c625SLionel Sambuc {
77184d9c625SLionel Sambuc 	return  (sp->db_error = db_create(dbp, 0, 0)) != 0 ||
77284d9c625SLionel Sambuc 		(sp->db_error = (*dbp)->set_re_source(*dbp, file)) != 0 ||
77384d9c625SLionel Sambuc 		(sp->db_error = db_open(*dbp, NULL, DB_RECNO, 0, 0)) != 0;
77484d9c625SLionel Sambuc }
77584d9c625SLionel Sambuc 
77684d9c625SLionel Sambuc /*
77784d9c625SLionel Sambuc  * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
77884d9c625SLionel Sambuc  */
77984d9c625SLionel Sambuc int
db_init(SCR * sp,EXF * ep,char * rcv_name,char * oname,size_t psize,int * open_err)78084d9c625SLionel Sambuc db_init(SCR *sp, EXF *ep, char *rcv_name, char *oname, size_t psize, int *open_err)
78184d9c625SLionel Sambuc {
78284d9c625SLionel Sambuc 	if (db_setup(sp, ep))
78384d9c625SLionel Sambuc 		return 1;
78484d9c625SLionel Sambuc 
78584d9c625SLionel Sambuc 	/* Open a db structure. */
78684d9c625SLionel Sambuc 	if ((sp->db_error = db_create(&ep->db, 0, 0)) != 0) {
78784d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "db_create");
78884d9c625SLionel Sambuc 		return 1;
78984d9c625SLionel Sambuc 	}
79084d9c625SLionel Sambuc 
79184d9c625SLionel Sambuc 	ep->db->set_re_delim(ep->db, '\n');		/* Always set. */
79284d9c625SLionel Sambuc 	ep->db->set_pagesize(ep->db, round_up(psize));
79384d9c625SLionel Sambuc 	ep->db->set_flags(ep->db, DB_RENUMBER | DB_SNAPSHOT);
79484d9c625SLionel Sambuc 	if (rcv_name == NULL)
79584d9c625SLionel Sambuc 		ep->db->set_re_source(ep->db, oname);
79684d9c625SLionel Sambuc 
79784d9c625SLionel Sambuc /*
79884d9c625SLionel Sambuc  * Don't let db use mmap when using fcntl for locking
79984d9c625SLionel Sambuc  */
80084d9c625SLionel Sambuc #ifdef HAVE_LOCK_FCNTL
80184d9c625SLionel Sambuc #define NOMMAPIFFCNTL DB_NOMMAP
80284d9c625SLionel Sambuc #else
80384d9c625SLionel Sambuc #define NOMMAPIFFCNTL 0
80484d9c625SLionel Sambuc #endif
80584d9c625SLionel Sambuc 
80684d9c625SLionel Sambuc #define _DB_OPEN_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
80784d9c625SLionel Sambuc 
80884d9c625SLionel Sambuc 	if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
80984d9c625SLionel Sambuc 	    ((rcv_name == 0) ? DB_TRUNCATE : 0) | VI_DB_THREAD | NOMMAPIFFCNTL,
81084d9c625SLionel Sambuc 	    _DB_OPEN_MODE)) != 0) {
81184d9c625SLionel Sambuc 		msgq_str(sp,
81284d9c625SLionel Sambuc 		    M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
81384d9c625SLionel Sambuc 		/*
81484d9c625SLionel Sambuc 		 * !!!
81584d9c625SLionel Sambuc 		 * Historically, vi permitted users to edit files that couldn't
81684d9c625SLionel Sambuc 		 * be read.  This isn't useful for single files from a command
81784d9c625SLionel Sambuc 		 * line, but it's quite useful for "vi *.c", since you can skip
81884d9c625SLionel Sambuc 		 * past files that you can't read.
81984d9c625SLionel Sambuc 		 */
82084d9c625SLionel Sambuc 		ep->db = NULL; /* Don't close it; it wasn't opened */
82184d9c625SLionel Sambuc 
82284d9c625SLionel Sambuc 		*open_err = 1;
82384d9c625SLionel Sambuc 		return 1;
82484d9c625SLionel Sambuc 	}
82584d9c625SLionel Sambuc 
82684d9c625SLionel Sambuc 	/* re_source is loaded into the database.
82784d9c625SLionel Sambuc 	 * Close it and reopen it in the environment.
82884d9c625SLionel Sambuc 	 */
82984d9c625SLionel Sambuc 	if ((sp->db_error = ep->db->close(ep->db, 0))) {
83084d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "close");
83184d9c625SLionel Sambuc 		return 1;
83284d9c625SLionel Sambuc 	}
83384d9c625SLionel Sambuc 	if ((sp->db_error = db_create(&ep->db, ep->env, 0)) != 0) {
83484d9c625SLionel Sambuc 		msgq(sp, M_DBERR, "db_create 2");
83584d9c625SLionel Sambuc 		return 1;
83684d9c625SLionel Sambuc 	}
83784d9c625SLionel Sambuc 	if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
83884d9c625SLionel Sambuc 	    VI_DB_THREAD | NOMMAPIFFCNTL, _DB_OPEN_MODE)) != 0) {
83984d9c625SLionel Sambuc 		msgq_str(sp,
84084d9c625SLionel Sambuc 		    M_DBERR, ep->rcv_path, "%s");
84184d9c625SLionel Sambuc 		return 1;
84284d9c625SLionel Sambuc 	}
84384d9c625SLionel Sambuc 
84484d9c625SLionel Sambuc 	return 0;
84584d9c625SLionel Sambuc }
846