1 /* $NetBSD: log4.c,v 1.3 2014/01/26 21:43:45 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.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: log4.c,v 10.3 2002/06/08 21:00:33 skimo Exp ";
19 __RCSID("$NetBSD: log4.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
37 * The log consists of records, each containing a type byte and a variable
38 * length byte string, as follows:
40 * LOG_CURSOR_INIT MARK
42 * LOG_LINE_APPEND_F db_recno_t char *
43 * LOG_LINE_APPEND_B db_recno_t char *
44 * LOG_LINE_DELETE_F db_recno_t char *
45 * LOG_LINE_DELETE_B db_recno_t char *
46 * LOG_LINE_RESET_F db_recno_t char *
47 * LOG_LINE_RESET_B db_recno_t char *
50 * We do before image physical logging. This means that the editor layer
51 * MAY NOT modify records in place, even if simply deleting or overwriting
52 * characters. Since the smallest unit of logging is a line, we're using
53 * up lots of space. This may eventually have to be reduced, probably by
54 * doing logical logging, which is a much cooler database phrase.
56 * The implementation of the historic vi 'u' command, using roll-forward and
57 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
58 * followed by a number of other records, followed by a LOG_CURSOR_END record.
59 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
60 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
61 * and is the line after the change. Roll-back is done by backing up to the
62 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
65 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
66 * record for a line different from the current one. It should be noted that
67 * this means that a subsequent 'u' command will make a change based on the
68 * new position of the log's cursor. This is okay, and, in fact, historic vi
72 static int log_cursor1
__P((SCR
*, int));
76 * Initialize the logging subsystem.
78 * PUBLIC: int log_init __P((SCR *, EXF *));
81 log_init(SCR
*sp
, EXF
*ep
)
89 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
91 * Initialize the buffer. The logging subsystem has its own
92 * buffers because the global ones are almost by definition
93 * going to be in use when the log runs.
97 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
99 ep
->l_high
= ep
->l_cur
= 1;
101 if ((sp
->db_error
= ep
->env
->log_cursor(ep
->env
, &logc
, 0))
103 msgq(sp
, M_DBERR
, "env->log_cursor");
109 BINC_GOTO(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
110 memset(&data
, 0, sizeof(data
));
111 data
.data
= sp
->wp
->l_lp
;
112 data
.ulen
= sp
->wp
->l_len
;
113 data
.flags
= DB_DBT_USERMEM
;
114 switch ((sp
->db_error
=
115 logc
->get(logc
, &ep
->lsn_first
, &data
, DB_LAST
))) {
121 msgq(sp
, M_DBERR
, "logc->get");
127 MEMCPY(&ep
->lsn_cur
, &ep
->lsn_first
, 1);
128 MEMCPY(&ep
->lsn_high
, &ep
->lsn_first
, 1);
129 logc
->close(logc
, 0);
132 /*LOCK_INIT(sp->wp, ep);*/
139 * Close the logging subsystem.
141 * PUBLIC: int log_end __P((SCR *, EXF *));
144 log_end(SCR
*sp
, EXF
*ep
)
148 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
150 /*LOCK_END(sp->wp, ep);*/
151 if (sp
->wp
->l_lp
!= NULL
) {
156 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
157 ep
->l_cursor
.cno
= 0;
158 ep
->l_high
= ep
->l_cur
= 1;
164 * Log the current cursor position, starting an event.
166 * PUBLIC: int log_cursor __P((SCR *));
174 if (F_ISSET(ep
, F_NOLOG
))
178 * If any changes were made since the last cursor init,
179 * put out the ending cursor record.
181 if (ep
->l_cursor
.lno
== OOBLNO
) {
182 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
184 ep
->l_cursor
.lno
= sp
->lno
;
185 ep
->l_cursor
.cno
= sp
->cno
;
187 return (log_cursor1(sp
, LOG_CURSOR_END
));
189 ep
->l_cursor
.lno
= sp
->lno
;
190 ep
->l_cursor
.cno
= sp
->cno
;
196 * Actually push a cursor record out.
199 log_cursor1(SCR
*sp
, int type
)
207 if (type == LOG_CURSOR_INIT &&
208 LOCK_TRY(sp->wp, ep))
212 if (type
== LOG_CURSOR_INIT
&&
213 (sp
->db_error
= __vi_log_truncate(ep
)) != 0) {
214 msgq(sp
, M_DBERR
, "truncate");
218 __vi_cursor_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, type
,
219 ep
->l_cursor
.lno
, ep
->l_cursor
.cno
)) != 0) {
220 msgq(sp
, M_DBERR
, "cursor_log");
223 if (type
== LOG_CURSOR_END
) {
224 MEMCPY(&ep
->lsn_high
, &ep
->lsn_cur
, 1);
225 /* XXXX should not be needed */
226 ep
->env
->log_flush(ep
->env
, NULL
);
229 #if defined(DEBUG) && 0
230 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
231 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
234 /* Reset high water mark. */
235 ep
->l_high
= ++ep
->l_cur
;
238 if (type == LOG_CURSOR_END)
239 LOCK_UNLOCK(sp->wp, ep);
248 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
251 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
260 if (F_ISSET(ep
, F_NOLOG
))
266 * Kluge for vi. Clear the EXF undo flag so that the
267 * next 'u' command does a roll-back, regardless.
271 /* Put out one initial cursor record per set of changes. */
272 if (ep
->l_cursor
.lno
!= OOBLNO
) {
273 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
275 ep
->l_cursor
.lno
= OOBLNO
;
277 } /*else if (ep->l_win != sp->wp) {
278 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
283 __vi_change_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, action
,
285 msgq(sp
, M_DBERR
, "change_log");
289 #if defined(DEBUG) && 0
291 case LOG_LINE_APPEND_F
:
292 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
293 ep
->l_cur
, lno
, len
);
295 case LOG_LINE_APPEND_B
:
296 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
297 ep
->l_cur
, lno
, len
);
299 case LOG_LINE_DELETE_F
:
300 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
301 ep
->l_cur
, lno
, len
);
303 case LOG_LINE_DELETE_B
:
304 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
305 ep
->l_cur
, lno
, len
);
307 case LOG_LINE_RESET_F
:
308 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
309 ep
->l_cur
, lno
, len
);
311 case LOG_LINE_RESET_B
:
312 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
313 ep
->l_cur
, lno
, len
);
317 /* Reset high water mark. */
318 ep
->l_high
= ++ep
->l_cur
;
325 * Log a mark position. For the log to work, we assume that there
326 * aren't any operations that just put out a log record -- this
327 * would mean that undo operations would only reset marks, and not
328 * cause any other change.
330 * PUBLIC: int log_mark __P((SCR *, LMARK *));
333 log_mark(SCR
*sp
, LMARK
*lmp
)
339 if (F_ISSET(ep
, F_NOLOG
))
342 /* Put out one initial cursor record per set of changes. */
343 if (ep
->l_cursor
.lno
!= OOBLNO
) {
344 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
346 ep
->l_cursor
.lno
= OOBLNO
;
351 __vi_mark_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0,
353 msgq(sp
, M_DBERR
, "cursor_log");
357 #if defined(DEBUG) && 0
358 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
359 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
361 /* Reset high water mark. */
362 ep
->l_high
= ++ep
->l_cur
;
368 * Roll the log backward one operation.
370 * PUBLIC: int log_backward __P((SCR *, MARK *));
373 log_backward(SCR
*sp
, MARK
*rp
)
384 if (F_ISSET(ep
, F_NOLOG
)) {
386 "010|Logging not being performed, undo not possible");
390 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
391 msgq(sp
, M_BERR
, "011|No changes to undo");
394 return __vi_log_traverse(sp
, UNDO_BACKWARD
, rp
);
399 * Reset the line to its original appearance.
402 * There's a bug in this code due to our not logging cursor movements
403 * unless a change was made. If you do a change, move off the line,
404 * then move back on and do a 'U', the line will be restored to the way
405 * it was before the original change.
407 * PUBLIC: int log_setline __P((SCR *));
420 if (F_ISSET(ep
, F_NOLOG
)) {
422 "012|Logging not being performed, undo not possible");
426 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
427 msgq(sp
, M_BERR
, "011|No changes to undo");
430 return __vi_log_traverse(sp
, UNDO_SETLINE
, &m
);
435 * Roll the log forward one operation.
437 * PUBLIC: int log_forward __P((SCR *, MARK *));
440 log_forward(SCR
*sp
, MARK
*rp
)
451 if (F_ISSET(ep
, F_NOLOG
)) {
453 "013|Logging not being performed, roll-forward not possible");
457 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_high
) >= 0) {
458 msgq(sp
, M_BERR
, "014|No changes to re-do");
461 return __vi_log_traverse(sp
, UNDO_FORWARD
, rp
);