head	1.4;
access;
symbols
	perseant-exfatfs-base-20250801:1.4
	perseant-exfatfs-base-20240630:1.4
	perseant-exfatfs:1.4.0.36
	perseant-exfatfs-base:1.4
	cjep_sun2x:1.4.0.34
	cjep_sun2x-base:1.4
	cjep_staticlib_x-base1:1.4
	cjep_staticlib_x:1.4.0.32
	cjep_staticlib_x-base:1.4
	phil-wifi-20200421:1.4
	phil-wifi-20200411:1.4
	phil-wifi-20200406:1.4
	pgoyette-compat-merge-20190127:1.4
	pgoyette-compat-20190127:1.4
	pgoyette-compat-20190118:1.4
	pgoyette-compat-1226:1.4
	pgoyette-compat-1126:1.4
	pgoyette-compat-1020:1.4
	pgoyette-compat-0930:1.4
	pgoyette-compat-0906:1.4
	pgoyette-compat-0728:1.4
	pgoyette-compat-0625:1.4
	pgoyette-compat-0521:1.4
	pgoyette-compat-0502:1.4
	pgoyette-compat-0422:1.4
	pgoyette-compat-0415:1.4
	pgoyette-compat-0407:1.4
	pgoyette-compat-0330:1.4
	pgoyette-compat-0322:1.4
	pgoyette-compat-0315:1.4
	pgoyette-compat:1.4.0.30
	pgoyette-compat-base:1.4
	perseant-stdc-iso10646:1.4.0.28
	perseant-stdc-iso10646-base:1.4
	prg-localcount2-base3:1.4
	prg-localcount2-base2:1.4
	prg-localcount2-base1:1.4
	prg-localcount2:1.4.0.26
	prg-localcount2-base:1.4
	pgoyette-localcount-20170426:1.4
	bouyer-socketcan-base1:1.4
	pgoyette-localcount-20170320:1.4
	bouyer-socketcan:1.4.0.24
	bouyer-socketcan-base:1.4
	pgoyette-localcount-20170107:1.4
	pgoyette-localcount-20161104:1.4
	localcount-20160914:1.4
	pgoyette-localcount-20160806:1.4
	pgoyette-localcount-20160726:1.4
	pgoyette-localcount:1.4.0.22
	pgoyette-localcount-base:1.4
	netbsd-5-2-3-RELEASE:1.4
	netbsd-5-1-5-RELEASE:1.4
	yamt-pagecache-base9:1.4
	yamt-pagecache-tag8:1.4
	tls-earlyentropy:1.4.0.18
	tls-earlyentropy-base:1.4
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.4
	riastradh-drm2-base3:1.4
	netbsd-5-2-2-RELEASE:1.4
	netbsd-5-1-4-RELEASE:1.4
	netbsd-5-2-1-RELEASE:1.4
	netbsd-5-1-3-RELEASE:1.4
	agc-symver:1.4.0.20
	agc-symver-base:1.4
	tls-maxphys-base:1.4
	yamt-pagecache-base8:1.4
	netbsd-5-2:1.4.0.16
	yamt-pagecache-base7:1.4
	netbsd-5-2-RELEASE:1.4
	netbsd-5-2-RC1:1.4
	yamt-pagecache-base6:1.4
	yamt-pagecache-base5:1.4
	yamt-pagecache-base4:1.4
	netbsd-5-1-2-RELEASE:1.4
	netbsd-5-1-1-RELEASE:1.4
	yamt-pagecache-base3:1.4
	yamt-pagecache-base2:1.4
	yamt-pagecache:1.4.0.14
	yamt-pagecache-base:1.4
	bouyer-quota2-nbase:1.4
	bouyer-quota2:1.4.0.12
	bouyer-quota2-base:1.4
	matt-nb5-pq3:1.4.0.10
	matt-nb5-pq3-base:1.4
	netbsd-5-1:1.4.0.8
	netbsd-5-1-RELEASE:1.4
	netbsd-5-1-RC4:1.4
	netbsd-5-1-RC3:1.4
	netbsd-5-1-RC2:1.4
	netbsd-5-1-RC1:1.4
	netbsd-5-0-2-RELEASE:1.4
	netbsd-5-0-1-RELEASE:1.4
	jym-xensuspend-nbase:1.4
	netbsd-5-0:1.4.0.6
	netbsd-5-0-RELEASE:1.4
	netbsd-5-0-RC4:1.4
	netbsd-5-0-RC3:1.4
	netbsd-5-0-RC2:1.4
	jym-xensuspend:1.4.0.4
	jym-xensuspend-base:1.4
	netbsd-5-0-RC1:1.4
	netbsd-5:1.4.0.2
	netbsd-5-base:1.4
	matt-mips64-base2:1.3
	matt-mips64:1.3.0.40
	mjf-devfs2:1.3.0.38
	mjf-devfs2-base:1.3
	netbsd-4-0-1-RELEASE:1.3
	wrstuden-revivesa-base-3:1.3
	wrstuden-revivesa-base-2:1.3
	wrstuden-fixsa-newbase:1.3
	wrstuden-revivesa-base-1:1.3
	yamt-pf42-base4:1.3
	yamt-pf42-base3:1.3
	hpcarm-cleanup-nbase:1.3
	yamt-pf42-baseX:1.3
	yamt-pf42-base2:1.3
	wrstuden-revivesa:1.3.0.36
	wrstuden-revivesa-base:1.3
	yamt-pf42:1.3.0.34
	yamt-pf42-base:1.3
	keiichi-mipv6-nbase:1.3
	keiichi-mipv6:1.3.0.32
	keiichi-mipv6-base:1.3
	matt-armv6-nbase:1.3
	matt-armv6-prevmlocking:1.3
	wrstuden-fixsa-base-1:1.3
	netbsd-4-0:1.3.0.30
	netbsd-4-0-RELEASE:1.3
	cube-autoconf:1.3.0.28
	cube-autoconf-base:1.3
	netbsd-4-0-RC5:1.3
	netbsd-4-0-RC4:1.3
	netbsd-4-0-RC3:1.3
	netbsd-4-0-RC2:1.3
	netbsd-4-0-RC1:1.3
	matt-armv6:1.3.0.26
	matt-armv6-base:1.3
	matt-mips64-base:1.3
	hpcarm-cleanup:1.3.0.24
	hpcarm-cleanup-base:1.3
	netbsd-3-1-1-RELEASE:1.3
	netbsd-3-0-3-RELEASE:1.3
	wrstuden-fixsa:1.3.0.22
	wrstuden-fixsa-base:1.3
	abandoned-netbsd-4-base:1.3
	abandoned-netbsd-4:1.3.0.16
	netbsd-3-1:1.3.0.18
	netbsd-3-1-RELEASE:1.3
	netbsd-3-0-2-RELEASE:1.3
	netbsd-3-1-RC4:1.3
	netbsd-3-1-RC3:1.3
	netbsd-3-1-RC2:1.3
	netbsd-3-1-RC1:1.3
	netbsd-4:1.3.0.20
	netbsd-4-base:1.3
	chap-midi-nbase:1.3
	netbsd-3-0-1-RELEASE:1.3
	chap-midi:1.3.0.14
	chap-midi-base:1.3
	netbsd-3-0:1.3.0.12
	netbsd-3-0-RELEASE:1.3
	netbsd-3-0-RC6:1.3
	netbsd-3-0-RC5:1.3
	netbsd-3-0-RC4:1.3
	netbsd-3-0-RC3:1.3
	netbsd-3-0-RC2:1.3
	netbsd-3-0-RC1:1.3
	netbsd-2-0-3-RELEASE:1.3
	netbsd-2-1:1.3.0.10
	netbsd-2-1-RELEASE:1.3
	netbsd-2-1-RC6:1.3
	netbsd-2-1-RC5:1.3
	netbsd-2-1-RC4:1.3
	netbsd-2-1-RC3:1.3
	netbsd-2-1-RC2:1.3
	netbsd-2-1-RC1:1.3
	netbsd-2-0-2-RELEASE:1.3
	netbsd-3:1.3.0.8
	netbsd-3-base:1.3
	netbsd-2-0-1-RELEASE:1.3
	netbsd-2:1.3.0.6
	netbsd-2-base:1.3
	netbsd-2-0-RELEASE:1.3
	netbsd-2-0-RC5:1.3
	netbsd-2-0-RC4:1.3
	netbsd-2-0-RC3:1.3
	netbsd-2-0-RC2:1.3
	netbsd-2-0-RC1:1.3
	netbsd-2-0:1.3.0.4
	netbsd-2-0-base:1.3
	netbsd-1-6-PATCH002-RELEASE:1.3
	netbsd-1-6-PATCH002:1.3
	netbsd-1-6-PATCH002-RC4:1.3
	netbsd-1-6-PATCH002-RC3:1.3
	netbsd-1-6-PATCH002-RC2:1.3
	netbsd-1-6-PATCH002-RC1:1.3
	netbsd-1-6-PATCH001:1.3
	netbsd-1-6-PATCH001-RELEASE:1.3
	netbsd-1-6-PATCH001-RC3:1.3
	netbsd-1-6-PATCH001-RC2:1.3
	netbsd-1-6-PATCH001-RC1:1.3
	fvdl_fs64_base:1.3
	netbsd-1-6-RELEASE:1.3
	netbsd-1-6-RC3:1.3
	netbsd-1-6-RC2:1.3
	netbsd-1-6-RC1:1.3
	netbsd-1-6:1.3.0.2
	netbsd-1-6-base:1.3
	netbsd-1-5-PATCH003:1.2
	netbsd-1-5-PATCH002:1.2
	netbsd-1-5-PATCH001:1.2
	nvi_1_79:1.1.1.3
	netbsd-1-5-RELEASE:1.2
	netbsd-1-5-BETA2:1.2
	netbsd-1-5-BETA:1.2
	netbsd-1-4-PATCH003:1.2
	netbsd-1-5-ALPHA2:1.2
	netbsd-1-5:1.2.0.10
	netbsd-1-5-base:1.2
	minoura-xpg4dl-base:1.2
	minoura-xpg4dl:1.2.0.8
	netbsd-1-4-PATCH002:1.2
	wrstuden-devbsize-19991221:1.2
	wrstuden-devbsize:1.2.0.6
	wrstuden-devbsize-base:1.2
	comdex-fall-1999:1.2.0.4
	comdex-fall-1999-base:1.2
	netbsd-1-4-PATCH001:1.2
	netbsd-1-4-RELEASE:1.2
	netbsd-1-4:1.2.0.2
	netbsd-1-4-base:1.2
	netbsd-1-3-PATCH003:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.1.3
	netbsd-1-3-PATCH002:1.1.1.3
	netbsd-1-3-PATCH001:1.1.1.3
	netbsd-1-3-RELEASE:1.1.1.3
	netbsd-1-3-BETA:1.1.1.3
	netbsd-1-3:1.1.1.3.0.2
	netbsd-1-3-base:1.1.1.3
	netbsd-1-2-PATCH001:1.1.1.3
	netbsd-1-2-RELEASE:1.1.1.3
	netbsd-1-2-BETA:1.1.1.3
	netbsd-1-2:1.1.1.3.0.4
	netbsd-1-2-base:1.1.1.3
	nvi_1_66:1.1.1.3
	BOSTIC:1.1.1
	netbsd-1-1-PATCH001:1.1.1.2
	netbsd-1-1-RELEASE:1.1.1.2
	netbsd-1-1:1.1.1.2.0.4
	netbsd-1-1-base:1.1.1.2
	netbsd-1-0-PATCH06:1.1.1.2
	netbsd-1-0-PATCH05:1.1.1.2
	netbsd-1-0-PATCH04:1.1.1.2
	netbsd-1-0-PATCH03:1.1.1.2
	netbsd-1-0-PATCH02:1.1.1.2
	netbsd-1-0-PATCH1:1.1.1.2
	netbsd-1-0-PATCH0:1.1.1.2
	netbsd-1-0-RELEASE:1.1.1.2
	netbsd-1-0:1.1.1.2.0.2
	nvi-1-34b:1.1.1.2
	nvi-1-33b:1.1.1.1
	bostic-nvi:1.1.1;
locks; strict;
comment	@ * @;


1.4
date	2008.10.29.16.49.34;	author christos;	state dead;
branches;
next	1.3;

1.3
date	2002.04.09.01.47.31;	author thorpej;	state Exp;
branches;
next	1.2;

1.2
date	98.01.09.08.06.45;	author perry;	state Exp;
branches;
next	1.1;

1.1
date	94.08.17.16.19.03;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	94.08.17.16.19.04;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	94.08.17.19.24.59;	author cgd;	state Exp;
branches
	1.1.1.2.2.1;
next	1.1.1.3;

1.1.1.3
date	96.05.20.02.04.17;	author mrg;	state Exp;
branches;
next	;

1.1.1.2.2.1
date	94.08.17.19.24.59;	author cgd;	state dead;
branches;
next	1.1.1.2.2.2;

1.1.1.2.2.2
date	94.08.17.19.25.00;	author cgd;	state Exp;
branches;
next	;


desc
@@


1.4
log
@bye old vi!
@
text
@/*	$NetBSD: log.c,v 1.3 2002/04/09 01:47:31 thorpej Exp $	*/

/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
#if 0
static const char sccsid[] = "@@(#)log.c	10.8 (Berkeley) 3/6/96";
#else
__RCSID("$NetBSD: log.c,v 1.3 2002/04/09 01:47:31 thorpej Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>

#include <bitstring.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"

/*
 * The log consists of records, each containing a type byte and a variable
 * length byte string, as follows:
 *
 *	LOG_CURSOR_INIT		MARK
 *	LOG_CURSOR_END		MARK
 *	LOG_LINE_APPEND 	recno_t		char *
 *	LOG_LINE_DELETE		recno_t		char *
 *	LOG_LINE_INSERT		recno_t		char *
 *	LOG_LINE_RESET_F	recno_t		char *
 *	LOG_LINE_RESET_B	recno_t		char *
 *	LOG_MARK		LMARK
 *
 * We do before image physical logging.  This means that the editor layer
 * MAY NOT modify records in place, even if simply deleting or overwriting
 * characters.  Since the smallest unit of logging is a line, we're using
 * up lots of space.  This may eventually have to be reduced, probably by
 * doing logical logging, which is a much cooler database phrase.
 *
 * The implementation of the historic vi 'u' command, using roll-forward and
 * roll-back, is simple.  Each set of changes has a LOG_CURSOR_INIT record,
 * followed by a number of other records, followed by a LOG_CURSOR_END record.
 * LOG_LINE_RESET records come in pairs.  The first is a LOG_LINE_RESET_B
 * record, and is the line before the change.  The second is LOG_LINE_RESET_F,
 * and is the line after the change.  Roll-back is done by backing up to the
 * first LOG_CURSOR_INIT record before a change.  Roll-forward is done in a
 * similar fashion.
 *
 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
 * record for a line different from the current one.  It should be noted that
 * this means that a subsequent 'u' command will make a change based on the
 * new position of the log's cursor.  This is okay, and, in fact, historic vi
 * behaved that way.
 */

static int	log_cursor1 __P((SCR *, int));
static void	log_err __P((SCR *, char *, int));
#if defined(DEBUG) && 0
static void	log_trace __P((SCR *, char *, recno_t, u_char *));
#endif

/* Try and restart the log on failure, i.e. if we run out of memory. */
#define	LOG_ERR {							\
	log_err(sp, __FILE__, __LINE__);				\
	return (1);							\
}

/*
 * log_init --
 *	Initialize the logging subsystem.
 *
 * PUBLIC: int log_init __P((SCR *, EXF *));
 */
int
log_init(sp, ep)
	SCR *sp;
	EXF *ep;
{
	/*
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 *
	 * Initialize the buffer.  The logging subsystem has its own
	 * buffers because the global ones are almost by definition
	 * going to be in use when the log runs.
	 */
	ep->l_lp = NULL;
	ep->l_len = 0;
	ep->l_cursor.lno = 1;		/* XXX Any valid recno. */
	ep->l_cursor.cno = 0;
	ep->l_high = ep->l_cur = 1;

	ep->log = dbopen(NULL, O_CREAT | O_NONBLOCK | O_RDWR,
	    S_IRUSR | S_IWUSR, DB_RECNO, NULL);
	if (ep->log == NULL) {
		msgq(sp, M_SYSERR, "009|Log file");
		F_SET(ep, F_NOLOG);
		return (1);
	}

	return (0);
}

/*
 * log_end --
 *	Close the logging subsystem.
 *
 * PUBLIC: int log_end __P((SCR *, EXF *));
 */
int
log_end(sp, ep)
	SCR *sp;
	EXF *ep;
{
	/*
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 */
	if (ep->log != NULL) {
		(void)(ep->log->close)(ep->log);
		ep->log = NULL;
	}
	if (ep->l_lp != NULL) {
		free(ep->l_lp);
		ep->l_lp = NULL;
	}
	ep->l_len = 0;
	ep->l_cursor.lno = 1;		/* XXX Any valid recno. */
	ep->l_cursor.cno = 0;
	ep->l_high = ep->l_cur = 1;
	return (0);
}

/*
 * log_cursor --
 *	Log the current cursor position, starting an event.
 *
 * PUBLIC: int log_cursor __P((SCR *));
 */
int
log_cursor(sp)
	SCR *sp;
{
	EXF *ep;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/*
	 * If any changes were made since the last cursor init,
	 * put out the ending cursor record.
	 */
	if (ep->l_cursor.lno == OOBLNO) {
		ep->l_cursor.lno = sp->lno;
		ep->l_cursor.cno = sp->cno;
		return (log_cursor1(sp, LOG_CURSOR_END));
	}
	ep->l_cursor.lno = sp->lno;
	ep->l_cursor.cno = sp->cno;
	return (0);
}

/*
 * log_cursor1 --
 *	Actually push a cursor record out.
 */
static int
log_cursor1(sp, type)
	SCR *sp;
	int type;
{
	DBT data, key;
	EXF *ep;

	ep = sp->ep;
	BINC_RET(sp, ep->l_lp, ep->l_len, sizeof(u_char) + sizeof(MARK));
	ep->l_lp[0] = type;
	memmove(ep->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));

	key.data = &ep->l_cur;
	key.size = sizeof(recno_t);
	data.data = ep->l_lp;
	data.size = sizeof(u_char) + sizeof(MARK);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(DEBUG) && 0
	TRACE(sp, "%lu: %s: %u/%u\n", ep->l_cur,
	    type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
	    sp->lno, sp->cno);
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;

	return (0);
}

/*
 * log_line --
 *	Log a line change.
 *
 * PUBLIC: int log_line __P((SCR *, recno_t, u_int));
 */
int
log_line(sp, lno, action)
	SCR *sp;
	recno_t lno;
	u_int action;
{
	DBT data, key;
	EXF *ep;
	size_t len;
	char *lp;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/*
	 * XXX
	 *
	 * Kluge for vi.  Clear the EXF undo flag so that the
	 * next 'u' command does a roll-back, regardless.
	 */
	F_CLR(ep, F_UNDO);

	/* Put out one initial cursor record per set of changes. */
	if (ep->l_cursor.lno != OOBLNO) {
		if (log_cursor1(sp, LOG_CURSOR_INIT))
			return (1);
		ep->l_cursor.lno = OOBLNO;
	}

	/*
	 * Put out the changes.  If it's a LOG_LINE_RESET_B call, it's a
	 * special case, avoid the caches.  Also, if it fails and it's
	 * line 1, it just means that the user started with an empty file,
	 * so fake an empty length line.
	 */
	if (action == LOG_LINE_RESET_B) {
		if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
			if (lno != 1) {
				db_err(sp, lno);
				return (1);
			}
			len = 0;
			lp = "";
		}
	} else
		if (db_get(sp, lno, DBG_FATAL, &lp, &len))
			return (1);
	BINC_RET(sp,
	    ep->l_lp, ep->l_len, len + sizeof(u_char) + sizeof(recno_t));
	ep->l_lp[0] = action;
	memmove(ep->l_lp + sizeof(u_char), &lno, sizeof(recno_t));
	memmove(ep->l_lp + sizeof(u_char) + sizeof(recno_t), lp, len);

	key.data = &ep->l_cur;
	key.size = sizeof(recno_t);
	data.data = ep->l_lp;
	data.size = len + sizeof(u_char) + sizeof(recno_t);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(DEBUG) && 0
	switch (action) {
	case LOG_LINE_APPEND:
		TRACE(sp, "%u: log_line: append: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_DELETE:
		TRACE(sp, "%lu: log_line: delete: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_INSERT:
		TRACE(sp, "%lu: log_line: insert: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_RESET_F:
		TRACE(sp, "%lu: log_line: reset_f: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_RESET_B:
		TRACE(sp, "%lu: log_line: reset_b: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	}
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;

	return (0);
}

/*
 * log_mark --
 *	Log a mark position.  For the log to work, we assume that there
 *	aren't any operations that just put out a log record -- this
 *	would mean that undo operations would only reset marks, and not
 *	cause any other change.
 *
 * PUBLIC: int log_mark __P((SCR *, LMARK *));
 */
int
log_mark(sp, lmp)
	SCR *sp;
	LMARK *lmp;
{
	DBT data, key;
	EXF *ep;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/* Put out one initial cursor record per set of changes. */
	if (ep->l_cursor.lno != OOBLNO) {
		if (log_cursor1(sp, LOG_CURSOR_INIT))
			return (1);
		ep->l_cursor.lno = OOBLNO;
	}

	BINC_RET(sp, ep->l_lp,
	    ep->l_len, sizeof(u_char) + sizeof(LMARK));
	ep->l_lp[0] = LOG_MARK;
	memmove(ep->l_lp + sizeof(u_char), lmp, sizeof(LMARK));

	key.data = &ep->l_cur;
	key.size = sizeof(recno_t);
	data.data = ep->l_lp;
	data.size = sizeof(u_char) + sizeof(LMARK);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(DEBUG) && 0
	TRACE(sp, "%lu: mark %c: %lu/%u\n",
	    ep->l_cur, lmp->name, lmp->lno, lmp->cno);
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;
	return (0);
}

/*
 * Log_backward --
 *	Roll the log backward one operation.
 *
 * PUBLIC: int log_backward __P((SCR *, MARK *));
 */
int
log_backward(sp, rp)
	SCR *sp;
	MARK *rp;
{
	DBT key, data;
	EXF *ep;
	LMARK lm;
	MARK m;
	recno_t lno;
	int didop;
	u_char *p;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "010|Logging not being performed, undo not possible");
		return (1);
	}

	if (ep->l_cur == 1) {
		msgq(sp, M_BERR, "011|No changes to undo");
		return (1);
	}

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);
	for (didop = 0;;) {
		--ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(DEBUG) && 0
		log_trace(sp, "log_backward", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_INIT:
			if (didop) {
				memmove(rp, p + sizeof(u_char), sizeof(MARK));
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_CURSOR_END:
			break;
		case LOG_LINE_APPEND:
		case LOG_LINE_INSERT:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (db_delete(sp, lno))
				goto err;
			++sp->rptlines[L_DELETED];
			break;
		case LOG_LINE_DELETE:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (db_insert(sp, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			++sp->rptlines[L_ADDED];
			break;
		case LOG_LINE_RESET_F:
			break;
		case LOG_LINE_RESET_B:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (db_set(sp, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			didop = 1;
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	return (1);
}

/*
 * Log_setline --
 *	Reset the line to its original appearance.
 *
 * XXX
 * There's a bug in this code due to our not logging cursor movements
 * unless a change was made.  If you do a change, move off the line,
 * then move back on and do a 'U', the line will be restored to the way
 * it was before the original change.
 *
 * PUBLIC: int log_setline __P((SCR *));
 */
int
log_setline(sp)
	SCR *sp;
{
	DBT key, data;
	EXF *ep;
	LMARK lm;
	MARK m;
	recno_t lno;
	u_char *p;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "012|Logging not being performed, undo not possible");
		return (1);
	}

	if (ep->l_cur == 1)
		return (1);

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);

	for (;;) {
		--ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(DEBUG) && 0
		log_trace(sp, "log_setline", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_INIT:
			memmove(&m, p + sizeof(u_char), sizeof(MARK));
			if (m.lno != sp->lno || ep->l_cur == 1) {
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_CURSOR_END:
			memmove(&m, p + sizeof(u_char), sizeof(MARK));
			if (m.lno != sp->lno) {
				++ep->l_cur;
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_LINE_APPEND:
		case LOG_LINE_INSERT:
		case LOG_LINE_DELETE:
		case LOG_LINE_RESET_F:
			break;
		case LOG_LINE_RESET_B:
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (lno == sp->lno &&
			    db_set(sp, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
		case LOG_MARK:
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	return (1);
}

/*
 * Log_forward --
 *	Roll the log forward one operation.
 *
 * PUBLIC: int log_forward __P((SCR *, MARK *));
 */
int
log_forward(sp, rp)
	SCR *sp;
	MARK *rp;
{
	DBT key, data;
	EXF *ep;
	LMARK lm;
	MARK m;
	recno_t lno;
	int didop;
	u_char *p;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
	    "013|Logging not being performed, roll-forward not possible");
		return (1);
	}

	if (ep->l_cur == ep->l_high) {
		msgq(sp, M_BERR, "014|No changes to re-do");
		return (1);
	}

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);
	for (didop = 0;;) {
		++ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(DEBUG) && 0
		log_trace(sp, "log_forward", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_END:
			if (didop) {
				++ep->l_cur;
				memmove(rp, p + sizeof(u_char), sizeof(MARK));
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_CURSOR_INIT:
			break;
		case LOG_LINE_APPEND:
		case LOG_LINE_INSERT:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (db_insert(sp, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			++sp->rptlines[L_ADDED];
			break;
		case LOG_LINE_DELETE:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (db_delete(sp, lno))
				goto err;
			++sp->rptlines[L_DELETED];
			break;
		case LOG_LINE_RESET_B:
			break;
		case LOG_LINE_RESET_F:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (db_set(sp, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			didop = 1;
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	return (1);
}

/*
 * log_err --
 *	Try and restart the log on failure, i.e. if we run out of memory.
 */
static void
log_err(sp, file, line)
	SCR *sp;
	char *file;
	int line;
{
	EXF *ep;

	msgq(sp, M_SYSERR, "015|%s/%d: log put error", tail(file), line);
	ep = sp->ep;
	(void)ep->log->close(ep->log);
	if (!log_init(sp, ep))
		msgq(sp, M_ERR, "267|Log restarted");
}

#if defined(DEBUG) && 0
static void
log_trace(sp, msg, rno, p)
	SCR *sp;
	char *msg;
	recno_t rno;
	u_char *p;
{
	LMARK lm;
	MARK m;
	recno_t lno;

	switch (*p) {
	case LOG_CURSOR_INIT:
		memmove(&m, p + sizeof(u_char), sizeof(MARK));
		TRACE(sp, "%lu: %s:  C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
		break;
	case LOG_CURSOR_END:
		memmove(&m, p + sizeof(u_char), sizeof(MARK));
		TRACE(sp, "%lu: %s:   C_END: %u/%u\n", rno, msg, m.lno, m.cno);
		break;
	case LOG_LINE_APPEND:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s:  APPEND: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_INSERT:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s:  INSERT: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_DELETE:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s:  DELETE: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_RESET_F:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s: RESET_F: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_RESET_B:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s: RESET_B: %lu\n", rno, msg, lno);
		break;
	case LOG_MARK:
		memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
		TRACE(sp,
		    "%lu: %s:    MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
		break;
	default:
		abort();
	}
}
#endif
@


1.3
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: log.c,v 1.2 1998/01/09 08:06:45 perry Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d14 1
d16 1
d18 3
@


1.1
log
@Initial revision
@
text
@d1 2
d6 2
d9 1
a9 27
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
d12 2
d15 1
a15 1
static const char sccsid[] = "@@(#)log.c	8.17 (Berkeley) 8/17/94";
a20 1
#include <sys/time.h>
a25 1
#include <signal.h>
a28 5
#include <termios.h>

#include "compat.h"
#include <db.h>
#include <regex.h>
d30 1
a30 1
#include "vi.h"
d67 2
a68 1
static int	log_cursor1 __P((SCR *, EXF *, int));
d75 1
a75 5
	msgq(sp, M_ERR, "Error: %s/%d: put log error: %s",		\
	    tail(__FILE__), __LINE__, strerror(errno));			\
	(void)ep->log->close(ep->log);					\
	if (!log_init(sp, ep))						\
		msgq(sp, M_ERR, "Log restarted");			\
d82 2
d91 3
d107 1
a107 1
		msgq(sp, M_ERR, "log db: %s", strerror(errno));
d118 2
d126 4
d148 2
d152 1
a152 1
log_cursor(sp, ep)
d154 1
d156 5
a160 1
{
d168 1
a168 1
		return (log_cursor1(sp, ep, LOG_CURSOR_END));
d180 1
a180 1
log_cursor1(sp, ep, type)
a181 1
	EXF *ep;
d185 1
d187 1
d213 2
d217 1
a217 1
log_line(sp, ep, lno, action)
a218 1
	EXF *ep;
d223 1
d227 1
d241 1
a241 1
		if (log_cursor1(sp, ep, LOG_CURSOR_INIT))
d253 1
a253 1
		if ((lp = file_rline(sp, ep, lno, &len)) == NULL) {
d255 1
a255 1
				GETLINE_ERR(sp, lno);
d262 1
a262 2
		if ((lp = file_gline(sp, ep, lno, &len)) == NULL) {
			GETLINE_ERR(sp, lno);
a263 1
		}
d313 2
d317 1
a317 1
log_mark(sp, ep, lmp)
a318 1
	EXF *ep;
d322 1
d324 1
d330 1
a330 1
		if (log_cursor1(sp, ep, LOG_CURSOR_INIT))
d359 2
d363 1
a363 1
log_backward(sp, ep, rp)
a364 1
	EXF *ep;
d368 1
d375 1
d378 1
a378 1
		    "Logging not being performed, undo not possible");
d383 1
a383 1
		msgq(sp, M_BERR, "No changes to undo");
d412 1
a412 1
			if (file_dline(sp, ep, lno))
d419 1
a419 1
			if (file_iline(sp, ep, lno, p + sizeof(u_char) +
d430 1
a430 1
			if (file_sline(sp, ep, lno, p + sizeof(u_char) +
d444 1
a444 1
			if (mark_set(sp, ep, lm.name, &m, 0))
d465 2
d469 1
a469 1
log_setline(sp, ep)
a470 1
	EXF *ep;
d473 1
d479 1
d482 1
a482 1
		    "Logging not being performed, undo not possible");
d525 1
a525 1
			    file_sline(sp, ep, lno, p + sizeof(u_char) +
d537 1
a537 1
			if (mark_set(sp, ep, lm.name, &m, 0))
d552 2
d556 1
a556 1
log_forward(sp, ep, rp)
a557 1
	EXF *ep;
d561 1
d568 1
d571 1
a571 1
		    "Logging not being performed, roll-forward not possible");
d576 1
a576 1
		msgq(sp, M_BERR, "No changes to re-do");
d606 1
a606 1
			if (file_iline(sp, ep, lno, p + sizeof(u_char) +
d615 1
a615 1
			if (file_dline(sp, ep, lno))
d624 1
a624 1
			if (file_sline(sp, ep, lno, p + sizeof(u_char) +
d638 1
a638 1
			if (mark_set(sp, ep, lm.name, &m, 0))
d648 19
@


1.1.1.1
log
@new public version of nvi
@
text
@@


1.1.1.2
log
@new public version of nvi
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)log.c	8.18 (Berkeley) 8/17/94";
@


1.1.1.2.2.1
log
@file log.c was added on branch netbsd-1-0 on 1994-08-17 19:25:00 +0000
@
text
@d1 698
@


1.1.1.2.2.2
log
@new public version of nvi
@
text
@a0 698
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@@(#)log.c	8.18 (Berkeley) 8/17/94";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <bitstring.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>

#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"

/*
 * The log consists of records, each containing a type byte and a variable
 * length byte string, as follows:
 *
 *	LOG_CURSOR_INIT		MARK
 *	LOG_CURSOR_END		MARK
 *	LOG_LINE_APPEND 	recno_t		char *
 *	LOG_LINE_DELETE		recno_t		char *
 *	LOG_LINE_INSERT		recno_t		char *
 *	LOG_LINE_RESET_F	recno_t		char *
 *	LOG_LINE_RESET_B	recno_t		char *
 *	LOG_MARK		LMARK
 *
 * We do before image physical logging.  This means that the editor layer
 * MAY NOT modify records in place, even if simply deleting or overwriting
 * characters.  Since the smallest unit of logging is a line, we're using
 * up lots of space.  This may eventually have to be reduced, probably by
 * doing logical logging, which is a much cooler database phrase.
 *
 * The implementation of the historic vi 'u' command, using roll-forward and
 * roll-back, is simple.  Each set of changes has a LOG_CURSOR_INIT record,
 * followed by a number of other records, followed by a LOG_CURSOR_END record.
 * LOG_LINE_RESET records come in pairs.  The first is a LOG_LINE_RESET_B
 * record, and is the line before the change.  The second is LOG_LINE_RESET_F,
 * and is the line after the change.  Roll-back is done by backing up to the
 * first LOG_CURSOR_INIT record before a change.  Roll-forward is done in a
 * similar fashion.
 *
 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
 * record for a line different from the current one.  It should be noted that
 * this means that a subsequent 'u' command will make a change based on the
 * new position of the log's cursor.  This is okay, and, in fact, historic vi
 * behaved that way.
 */

static int	log_cursor1 __P((SCR *, EXF *, int));
#if defined(DEBUG) && 0
static void	log_trace __P((SCR *, char *, recno_t, u_char *));
#endif

/* Try and restart the log on failure, i.e. if we run out of memory. */
#define	LOG_ERR {							\
	msgq(sp, M_ERR, "Error: %s/%d: put log error: %s",		\
	    tail(__FILE__), __LINE__, strerror(errno));			\
	(void)ep->log->close(ep->log);					\
	if (!log_init(sp, ep))						\
		msgq(sp, M_ERR, "Log restarted");			\
	return (1);							\
}

/*
 * log_init --
 *	Initialize the logging subsystem.
 */
int
log_init(sp, ep)
	SCR *sp;
	EXF *ep;
{
	/*
	 * Initialize the buffer.  The logging subsystem has its own
	 * buffers because the global ones are almost by definition
	 * going to be in use when the log runs.
	 */
	ep->l_lp = NULL;
	ep->l_len = 0;
	ep->l_cursor.lno = 1;		/* XXX Any valid recno. */
	ep->l_cursor.cno = 0;
	ep->l_high = ep->l_cur = 1;

	ep->log = dbopen(NULL, O_CREAT | O_NONBLOCK | O_RDWR,
	    S_IRUSR | S_IWUSR, DB_RECNO, NULL);
	if (ep->log == NULL) {
		msgq(sp, M_ERR, "log db: %s", strerror(errno));
		F_SET(ep, F_NOLOG);
		return (1);
	}

	return (0);
}

/*
 * log_end --
 *	Close the logging subsystem.
 */
int
log_end(sp, ep)
	SCR *sp;
	EXF *ep;
{
	if (ep->log != NULL) {
		(void)(ep->log->close)(ep->log);
		ep->log = NULL;
	}
	if (ep->l_lp != NULL) {
		free(ep->l_lp);
		ep->l_lp = NULL;
	}
	ep->l_len = 0;
	ep->l_cursor.lno = 1;		/* XXX Any valid recno. */
	ep->l_cursor.cno = 0;
	ep->l_high = ep->l_cur = 1;
	return (0);
}

/*
 * log_cursor --
 *	Log the current cursor position, starting an event.
 */
int
log_cursor(sp, ep)
	SCR *sp;
	EXF *ep;
{
	/*
	 * If any changes were made since the last cursor init,
	 * put out the ending cursor record.
	 */
	if (ep->l_cursor.lno == OOBLNO) {
		ep->l_cursor.lno = sp->lno;
		ep->l_cursor.cno = sp->cno;
		return (log_cursor1(sp, ep, LOG_CURSOR_END));
	}
	ep->l_cursor.lno = sp->lno;
	ep->l_cursor.cno = sp->cno;
	return (0);
}

/*
 * log_cursor1 --
 *	Actually push a cursor record out.
 */
static int
log_cursor1(sp, ep, type)
	SCR *sp;
	EXF *ep;
	int type;
{
	DBT data, key;

	BINC_RET(sp, ep->l_lp, ep->l_len, sizeof(u_char) + sizeof(MARK));
	ep->l_lp[0] = type;
	memmove(ep->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));

	key.data = &ep->l_cur;
	key.size = sizeof(recno_t);
	data.data = ep->l_lp;
	data.size = sizeof(u_char) + sizeof(MARK);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(DEBUG) && 0
	TRACE(sp, "%lu: %s: %u/%u\n", ep->l_cur,
	    type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
	    sp->lno, sp->cno);
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;

	return (0);
}

/*
 * log_line --
 *	Log a line change.
 */
int
log_line(sp, ep, lno, action)
	SCR *sp;
	EXF *ep;
	recno_t lno;
	u_int action;
{
	DBT data, key;
	size_t len;
	char *lp;

	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/*
	 * XXX
	 *
	 * Kluge for vi.  Clear the EXF undo flag so that the
	 * next 'u' command does a roll-back, regardless.
	 */
	F_CLR(ep, F_UNDO);

	/* Put out one initial cursor record per set of changes. */
	if (ep->l_cursor.lno != OOBLNO) {
		if (log_cursor1(sp, ep, LOG_CURSOR_INIT))
			return (1);
		ep->l_cursor.lno = OOBLNO;
	}

	/*
	 * Put out the changes.  If it's a LOG_LINE_RESET_B call, it's a
	 * special case, avoid the caches.  Also, if it fails and it's
	 * line 1, it just means that the user started with an empty file,
	 * so fake an empty length line.
	 */
	if (action == LOG_LINE_RESET_B) {
		if ((lp = file_rline(sp, ep, lno, &len)) == NULL) {
			if (lno != 1) {
				GETLINE_ERR(sp, lno);
				return (1);
			}
			len = 0;
			lp = "";
		}
	} else
		if ((lp = file_gline(sp, ep, lno, &len)) == NULL) {
			GETLINE_ERR(sp, lno);
			return (1);
		}
	BINC_RET(sp,
	    ep->l_lp, ep->l_len, len + sizeof(u_char) + sizeof(recno_t));
	ep->l_lp[0] = action;
	memmove(ep->l_lp + sizeof(u_char), &lno, sizeof(recno_t));
	memmove(ep->l_lp + sizeof(u_char) + sizeof(recno_t), lp, len);

	key.data = &ep->l_cur;
	key.size = sizeof(recno_t);
	data.data = ep->l_lp;
	data.size = len + sizeof(u_char) + sizeof(recno_t);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(DEBUG) && 0
	switch (action) {
	case LOG_LINE_APPEND:
		TRACE(sp, "%u: log_line: append: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_DELETE:
		TRACE(sp, "%lu: log_line: delete: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_INSERT:
		TRACE(sp, "%lu: log_line: insert: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_RESET_F:
		TRACE(sp, "%lu: log_line: reset_f: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_RESET_B:
		TRACE(sp, "%lu: log_line: reset_b: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	}
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;

	return (0);
}

/*
 * log_mark --
 *	Log a mark position.  For the log to work, we assume that there
 *	aren't any operations that just put out a log record -- this
 *	would mean that undo operations would only reset marks, and not
 *	cause any other change.
 */
int
log_mark(sp, ep, lmp)
	SCR *sp;
	EXF *ep;
	LMARK *lmp;
{
	DBT data, key;

	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/* Put out one initial cursor record per set of changes. */
	if (ep->l_cursor.lno != OOBLNO) {
		if (log_cursor1(sp, ep, LOG_CURSOR_INIT))
			return (1);
		ep->l_cursor.lno = OOBLNO;
	}

	BINC_RET(sp, ep->l_lp,
	    ep->l_len, sizeof(u_char) + sizeof(LMARK));
	ep->l_lp[0] = LOG_MARK;
	memmove(ep->l_lp + sizeof(u_char), lmp, sizeof(LMARK));

	key.data = &ep->l_cur;
	key.size = sizeof(recno_t);
	data.data = ep->l_lp;
	data.size = sizeof(u_char) + sizeof(LMARK);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(DEBUG) && 0
	TRACE(sp, "%lu: mark %c: %lu/%u\n",
	    ep->l_cur, lmp->name, lmp->lno, lmp->cno);
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;
	return (0);
}

/*
 * Log_backward --
 *	Roll the log backward one operation.
 */
int
log_backward(sp, ep, rp)
	SCR *sp;
	EXF *ep;
	MARK *rp;
{
	DBT key, data;
	LMARK lm;
	MARK m;
	recno_t lno;
	int didop;
	u_char *p;

	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "Logging not being performed, undo not possible");
		return (1);
	}

	if (ep->l_cur == 1) {
		msgq(sp, M_BERR, "No changes to undo");
		return (1);
	}

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);
	for (didop = 0;;) {
		--ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(DEBUG) && 0
		log_trace(sp, "log_backward", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_INIT:
			if (didop) {
				memmove(rp, p + sizeof(u_char), sizeof(MARK));
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_CURSOR_END:
			break;
		case LOG_LINE_APPEND:
		case LOG_LINE_INSERT:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (file_dline(sp, ep, lno))
				goto err;
			++sp->rptlines[L_DELETED];
			break;
		case LOG_LINE_DELETE:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (file_iline(sp, ep, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			++sp->rptlines[L_ADDED];
			break;
		case LOG_LINE_RESET_F:
			break;
		case LOG_LINE_RESET_B:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (file_sline(sp, ep, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			didop = 1;
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, ep, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	return (1);
}

/*
 * Log_setline --
 *	Reset the line to its original appearance.
 *
 * XXX
 * There's a bug in this code due to our not logging cursor movements
 * unless a change was made.  If you do a change, move off the line,
 * then move back on and do a 'U', the line will be restored to the way
 * it was before the original change.
 */
int
log_setline(sp, ep)
	SCR *sp;
	EXF *ep;
{
	DBT key, data;
	LMARK lm;
	MARK m;
	recno_t lno;
	u_char *p;

	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "Logging not being performed, undo not possible");
		return (1);
	}

	if (ep->l_cur == 1)
		return (1);

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);

	for (;;) {
		--ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(DEBUG) && 0
		log_trace(sp, "log_setline", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_INIT:
			memmove(&m, p + sizeof(u_char), sizeof(MARK));
			if (m.lno != sp->lno || ep->l_cur == 1) {
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_CURSOR_END:
			memmove(&m, p + sizeof(u_char), sizeof(MARK));
			if (m.lno != sp->lno) {
				++ep->l_cur;
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_LINE_APPEND:
		case LOG_LINE_INSERT:
		case LOG_LINE_DELETE:
		case LOG_LINE_RESET_F:
			break;
		case LOG_LINE_RESET_B:
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (lno == sp->lno &&
			    file_sline(sp, ep, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
		case LOG_MARK:
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, ep, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	return (1);
}

/*
 * Log_forward --
 *	Roll the log forward one operation.
 */
int
log_forward(sp, ep, rp)
	SCR *sp;
	EXF *ep;
	MARK *rp;
{
	DBT key, data;
	LMARK lm;
	MARK m;
	recno_t lno;
	int didop;
	u_char *p;

	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "Logging not being performed, roll-forward not possible");
		return (1);
	}

	if (ep->l_cur == ep->l_high) {
		msgq(sp, M_BERR, "No changes to re-do");
		return (1);
	}

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);
	for (didop = 0;;) {
		++ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(DEBUG) && 0
		log_trace(sp, "log_forward", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_END:
			if (didop) {
				++ep->l_cur;
				memmove(rp, p + sizeof(u_char), sizeof(MARK));
				F_CLR(ep, F_NOLOG);
				return (0);
			}
			break;
		case LOG_CURSOR_INIT:
			break;
		case LOG_LINE_APPEND:
		case LOG_LINE_INSERT:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (file_iline(sp, ep, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			++sp->rptlines[L_ADDED];
			break;
		case LOG_LINE_DELETE:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (file_dline(sp, ep, lno))
				goto err;
			++sp->rptlines[L_DELETED];
			break;
		case LOG_LINE_RESET_B:
			break;
		case LOG_LINE_RESET_F:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
			if (file_sline(sp, ep, lno, p + sizeof(u_char) +
			    sizeof(recno_t), data.size - sizeof(u_char) -
			    sizeof(recno_t)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			didop = 1;
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, ep, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	return (1);
}

#if defined(DEBUG) && 0
static void
log_trace(sp, msg, rno, p)
	SCR *sp;
	char *msg;
	recno_t rno;
	u_char *p;
{
	LMARK lm;
	MARK m;
	recno_t lno;

	switch (*p) {
	case LOG_CURSOR_INIT:
		memmove(&m, p + sizeof(u_char), sizeof(MARK));
		TRACE(sp, "%lu: %s:  C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
		break;
	case LOG_CURSOR_END:
		memmove(&m, p + sizeof(u_char), sizeof(MARK));
		TRACE(sp, "%lu: %s:   C_END: %u/%u\n", rno, msg, m.lno, m.cno);
		break;
	case LOG_LINE_APPEND:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s:  APPEND: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_INSERT:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s:  INSERT: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_DELETE:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s:  DELETE: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_RESET_F:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s: RESET_F: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_RESET_B:
		memmove(&lno, p + sizeof(u_char), sizeof(recno_t));
		TRACE(sp, "%lu: %s: RESET_B: %lu\n", rno, msg, lno);
		break;
	case LOG_MARK:
		memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
		TRACE(sp,
		    "%lu: %s:    MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
		break;
	default:
		abort();
	}
}
#endif
@


1.1.1.3
log
@import of nvi 1.66
@
text
@a3 2
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
d5 27
a31 1
 * See the LICENSE file for redistribution information.
a33 2
#include "config.h"

d35 1
a35 1
static const char sccsid[] = "@@(#)log.c	10.8 (Berkeley) 3/6/96";
d41 1
d47 1
d51 5
d57 1
a57 1
#include "common.h"
d94 1
a94 2
static int	log_cursor1 __P((SCR *, int));
static void	log_err __P((SCR *, char *, int));
d101 5
a105 1
	log_err(sp, __FILE__, __LINE__);				\
a111 2
 *
 * PUBLIC: int log_init __P((SCR *, EXF *));
a118 3
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 *
d132 1
a132 1
		msgq(sp, M_SYSERR, "009|Log file");
a142 2
 *
 * PUBLIC: int log_end __P((SCR *, EXF *));
a148 4
	/*
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 */
a166 2
 *
 * PUBLIC: int log_cursor __P((SCR *));
d169 1
a169 1
log_cursor(sp)
d171 1
a172 6
	EXF *ep;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

d180 1
a180 1
		return (log_cursor1(sp, LOG_CURSOR_END));
d192 1
a192 1
log_cursor1(sp, type)
d194 1
a197 1
	EXF *ep;
a198 1
	ep = sp->ep;
a223 2
 *
 * PUBLIC: int log_line __P((SCR *, recno_t, u_int));
d226 1
a226 1
log_line(sp, lno, action)
d228 1
a232 1
	EXF *ep;
a235 1
	ep = sp->ep;
d249 1
a249 1
		if (log_cursor1(sp, LOG_CURSOR_INIT))
d261 1
a261 1
		if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
d263 1
a263 1
				db_err(sp, lno);
d270 2
a271 1
		if (db_get(sp, lno, DBG_FATAL, &lp, &len))
d273 1
a322 2
 *
 * PUBLIC: int log_mark __P((SCR *, LMARK *));
d325 1
a325 1
log_mark(sp, lmp)
d327 1
a330 1
	EXF *ep;
a331 1
	ep = sp->ep;
d337 1
a337 1
		if (log_cursor1(sp, LOG_CURSOR_INIT))
a365 2
 *
 * PUBLIC: int log_backward __P((SCR *, MARK *));
d368 1
a368 1
log_backward(sp, rp)
d370 1
a373 1
	EXF *ep;
a379 1
	ep = sp->ep;
d382 1
a382 1
		    "010|Logging not being performed, undo not possible");
d387 1
a387 1
		msgq(sp, M_BERR, "011|No changes to undo");
d416 1
a416 1
			if (db_delete(sp, lno))
d423 1
a423 1
			if (db_insert(sp, lno, p + sizeof(u_char) +
d434 1
a434 1
			if (db_set(sp, lno, p + sizeof(u_char) +
d448 1
a448 1
			if (mark_set(sp, lm.name, &m, 0))
a468 2
 *
 * PUBLIC: int log_setline __P((SCR *));
d471 1
a471 1
log_setline(sp)
d473 1
a475 1
	EXF *ep;
a480 1
	ep = sp->ep;
d483 1
a483 1
		    "012|Logging not being performed, undo not possible");
d526 1
a526 1
			    db_set(sp, lno, p + sizeof(u_char) +
d538 1
a538 1
			if (mark_set(sp, lm.name, &m, 0))
a552 2
 *
 * PUBLIC: int log_forward __P((SCR *, MARK *));
d555 1
a555 1
log_forward(sp, rp)
d557 1
a560 1
	EXF *ep;
a566 1
	ep = sp->ep;
d569 1
a569 1
	    "013|Logging not being performed, roll-forward not possible");
d574 1
a574 1
		msgq(sp, M_BERR, "014|No changes to re-do");
d604 1
a604 1
			if (db_insert(sp, lno, p + sizeof(u_char) +
d613 1
a613 1
			if (db_delete(sp, lno))
d622 1
a622 1
			if (db_set(sp, lno, p + sizeof(u_char) +
d636 1
a636 1
			if (mark_set(sp, lm.name, &m, 0))
a645 19
}

/*
 * log_err --
 *	Try and restart the log on failure, i.e. if we run out of memory.
 */
static void
log_err(sp, file, line)
	SCR *sp;
	char *file;
	int line;
{
	EXF *ep;

	msgq(sp, M_SYSERR, "015|%s/%d: log put error", tail(file), line);
	ep = sp->ep;
	(void)ep->log->close(ep->log);
	if (!log_init(sp, ep))
		msgq(sp, M_ERR, "267|Log restarted");
@
