1 /* $NetBSD: log4.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: log4.c,v 10.3 2002/06/08 21:00:33 skimo Exp ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
32 * The log consists of records, each containing a type byte and a variable
33 * length byte string, as follows:
35 * LOG_CURSOR_INIT MARK
37 * LOG_LINE_APPEND_F db_recno_t char *
38 * LOG_LINE_APPEND_B db_recno_t char *
39 * LOG_LINE_DELETE_F db_recno_t char *
40 * LOG_LINE_DELETE_B db_recno_t char *
41 * LOG_LINE_RESET_F db_recno_t char *
42 * LOG_LINE_RESET_B db_recno_t char *
45 * We do before image physical logging. This means that the editor layer
46 * MAY NOT modify records in place, even if simply deleting or overwriting
47 * characters. Since the smallest unit of logging is a line, we're using
48 * up lots of space. This may eventually have to be reduced, probably by
49 * doing logical logging, which is a much cooler database phrase.
51 * The implementation of the historic vi 'u' command, using roll-forward and
52 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
53 * followed by a number of other records, followed by a LOG_CURSOR_END record.
54 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
55 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
56 * and is the line after the change. Roll-back is done by backing up to the
57 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
60 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
61 * record for a line different from the current one. It should be noted that
62 * this means that a subsequent 'u' command will make a change based on the
63 * new position of the log's cursor. This is okay, and, in fact, historic vi
67 static int log_cursor1
__P((SCR
*, int));
71 * Initialize the logging subsystem.
73 * PUBLIC: int log_init __P((SCR *, EXF *));
76 log_init(SCR
*sp
, EXF
*ep
)
84 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
86 * Initialize the buffer. The logging subsystem has its own
87 * buffers because the global ones are almost by definition
88 * going to be in use when the log runs.
92 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
94 ep
->l_high
= ep
->l_cur
= 1;
96 if ((sp
->db_error
= ep
->env
->log_cursor(ep
->env
, &logc
, 0))
98 msgq(sp
, M_DBERR
, "env->log_cursor");
104 BINC_GOTO(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
105 memset(&data
, 0, sizeof(data
));
106 data
.data
= sp
->wp
->l_lp
;
107 data
.ulen
= sp
->wp
->l_len
;
108 data
.flags
= DB_DBT_USERMEM
;
109 switch ((sp
->db_error
=
110 logc
->get(logc
, &ep
->lsn_first
, &data
, DB_LAST
))) {
116 msgq(sp
, M_DBERR
, "logc->get");
122 MEMCPY(&ep
->lsn_cur
, &ep
->lsn_first
, 1);
123 MEMCPY(&ep
->lsn_high
, &ep
->lsn_first
, 1);
124 logc
->close(logc
, 0);
127 /*LOCK_INIT(sp->wp, ep);*/
134 * Close the logging subsystem.
136 * PUBLIC: int log_end __P((SCR *, EXF *));
139 log_end(SCR
*sp
, EXF
*ep
)
143 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
145 /*LOCK_END(sp->wp, ep);*/
146 if (sp
->wp
->l_lp
!= NULL
) {
151 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
152 ep
->l_cursor
.cno
= 0;
153 ep
->l_high
= ep
->l_cur
= 1;
159 * Log the current cursor position, starting an event.
161 * PUBLIC: int log_cursor __P((SCR *));
169 if (F_ISSET(ep
, F_NOLOG
))
173 * If any changes were made since the last cursor init,
174 * put out the ending cursor record.
176 if (ep
->l_cursor
.lno
== OOBLNO
) {
177 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
179 ep
->l_cursor
.lno
= sp
->lno
;
180 ep
->l_cursor
.cno
= sp
->cno
;
182 return (log_cursor1(sp
, LOG_CURSOR_END
));
184 ep
->l_cursor
.lno
= sp
->lno
;
185 ep
->l_cursor
.cno
= sp
->cno
;
191 * Actually push a cursor record out.
194 log_cursor1(SCR
*sp
, int type
)
202 if (type == LOG_CURSOR_INIT &&
203 LOCK_TRY(sp->wp, ep))
207 if (type
== LOG_CURSOR_INIT
&&
208 (sp
->db_error
= __vi_log_truncate(ep
)) != 0) {
209 msgq(sp
, M_DBERR
, "truncate");
213 __vi_cursor_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, type
,
214 ep
->l_cursor
.lno
, ep
->l_cursor
.cno
)) != 0) {
215 msgq(sp
, M_DBERR
, "cursor_log");
218 if (type
== LOG_CURSOR_END
) {
219 MEMCPY(&ep
->lsn_high
, &ep
->lsn_cur
, 1);
220 /* XXXX should not be needed */
221 ep
->env
->log_flush(ep
->env
, NULL
);
224 #if defined(DEBUG) && 0
225 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
226 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
229 /* Reset high water mark. */
230 ep
->l_high
= ++ep
->l_cur
;
233 if (type == LOG_CURSOR_END)
234 LOCK_UNLOCK(sp->wp, ep);
243 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
246 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
255 if (F_ISSET(ep
, F_NOLOG
))
261 * Kluge for vi. Clear the EXF undo flag so that the
262 * next 'u' command does a roll-back, regardless.
266 /* Put out one initial cursor record per set of changes. */
267 if (ep
->l_cursor
.lno
!= OOBLNO
) {
268 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
270 ep
->l_cursor
.lno
= OOBLNO
;
272 } /*else if (ep->l_win != sp->wp) {
273 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
278 __vi_change_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, action
,
280 msgq(sp
, M_DBERR
, "change_log");
284 #if defined(DEBUG) && 0
286 case LOG_LINE_APPEND_F
:
287 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
288 ep
->l_cur
, lno
, len
);
290 case LOG_LINE_APPEND_B
:
291 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
292 ep
->l_cur
, lno
, len
);
294 case LOG_LINE_DELETE_F
:
295 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
296 ep
->l_cur
, lno
, len
);
298 case LOG_LINE_DELETE_B
:
299 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
300 ep
->l_cur
, lno
, len
);
302 case LOG_LINE_RESET_F
:
303 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
304 ep
->l_cur
, lno
, len
);
306 case LOG_LINE_RESET_B
:
307 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
308 ep
->l_cur
, lno
, len
);
312 /* Reset high water mark. */
313 ep
->l_high
= ++ep
->l_cur
;
320 * Log a mark position. For the log to work, we assume that there
321 * aren't any operations that just put out a log record -- this
322 * would mean that undo operations would only reset marks, and not
323 * cause any other change.
325 * PUBLIC: int log_mark __P((SCR *, LMARK *));
328 log_mark(SCR
*sp
, LMARK
*lmp
)
334 if (F_ISSET(ep
, F_NOLOG
))
337 /* Put out one initial cursor record per set of changes. */
338 if (ep
->l_cursor
.lno
!= OOBLNO
) {
339 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
341 ep
->l_cursor
.lno
= OOBLNO
;
346 __vi_mark_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0,
348 msgq(sp
, M_DBERR
, "cursor_log");
352 #if defined(DEBUG) && 0
353 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
354 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
356 /* Reset high water mark. */
357 ep
->l_high
= ++ep
->l_cur
;
363 * Roll the log backward one operation.
365 * PUBLIC: int log_backward __P((SCR *, MARK *));
368 log_backward(SCR
*sp
, MARK
*rp
)
379 if (F_ISSET(ep
, F_NOLOG
)) {
381 "010|Logging not being performed, undo not possible");
385 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
386 msgq(sp
, M_BERR
, "011|No changes to undo");
389 return __vi_log_traverse(sp
, UNDO_BACKWARD
, rp
);
394 * Reset the line to its original appearance.
397 * There's a bug in this code due to our not logging cursor movements
398 * unless a change was made. If you do a change, move off the line,
399 * then move back on and do a 'U', the line will be restored to the way
400 * it was before the original change.
402 * PUBLIC: int log_setline __P((SCR *));
415 if (F_ISSET(ep
, F_NOLOG
)) {
417 "012|Logging not being performed, undo not possible");
421 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
422 msgq(sp
, M_BERR
, "011|No changes to undo");
425 return __vi_log_traverse(sp
, UNDO_SETLINE
, &m
);
430 * Roll the log forward one operation.
432 * PUBLIC: int log_forward __P((SCR *, MARK *));
435 log_forward(SCR
*sp
, MARK
*rp
)
446 if (F_ISSET(ep
, F_NOLOG
)) {
448 "013|Logging not being performed, roll-forward not possible");
452 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_high
) >= 0) {
453 msgq(sp
, M_BERR
, "014|No changes to re-do");
456 return __vi_log_traverse(sp
, UNDO_FORWARD
, rp
);