4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: log4.c,v 10.3 2002/06/08 21:00:33 skimo Exp";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
33 * The log consists of records, each containing a type byte and a variable
34 * length byte string, as follows:
36 * LOG_CURSOR_INIT MARK
38 * LOG_LINE_APPEND_F db_recno_t char *
39 * LOG_LINE_APPEND_B db_recno_t char *
40 * LOG_LINE_DELETE_F db_recno_t char *
41 * LOG_LINE_DELETE_B db_recno_t char *
42 * LOG_LINE_RESET_F db_recno_t char *
43 * LOG_LINE_RESET_B db_recno_t char *
46 * We do before image physical logging. This means that the editor layer
47 * MAY NOT modify records in place, even if simply deleting or overwriting
48 * characters. Since the smallest unit of logging is a line, we're using
49 * up lots of space. This may eventually have to be reduced, probably by
50 * doing logical logging, which is a much cooler database phrase.
52 * The implementation of the historic vi 'u' command, using roll-forward and
53 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
54 * followed by a number of other records, followed by a LOG_CURSOR_END record.
55 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
56 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
57 * and is the line after the change. Roll-back is done by backing up to the
58 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
61 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
62 * record for a line different from the current one. It should be noted that
63 * this means that a subsequent 'u' command will make a change based on the
64 * new position of the log's cursor. This is okay, and, in fact, historic vi
68 static int log_cursor1
__P((SCR
*, int));
72 * Initialize the logging subsystem.
74 * PUBLIC: int log_init __P((SCR *, EXF *));
77 log_init(SCR
*sp
, EXF
*ep
)
85 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
87 * Initialize the buffer. The logging subsystem has its own
88 * buffers because the global ones are almost by definition
89 * going to be in use when the log runs.
93 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
95 ep
->l_high
= ep
->l_cur
= 1;
97 if ((sp
->db_error
= ep
->env
->log_cursor(ep
->env
, &logc
, 0))
99 msgq(sp
, M_DBERR
, "env->log_cursor");
105 BINC_GOTO(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
106 memset(&data
, 0, sizeof(data
));
107 data
.data
= sp
->wp
->l_lp
;
108 data
.ulen
= sp
->wp
->l_len
;
109 data
.flags
= DB_DBT_USERMEM
;
110 switch ((sp
->db_error
=
111 logc
->get(logc
, &ep
->lsn_first
, &data
, DB_LAST
))) {
117 msgq(sp
, M_DBERR
, "logc->get");
123 MEMCPY(&ep
->lsn_cur
, &ep
->lsn_first
, 1);
124 MEMCPY(&ep
->lsn_high
, &ep
->lsn_first
, 1);
125 logc
->close(logc
, 0);
128 /*LOCK_INIT(sp->wp, ep);*/
135 * Close the logging subsystem.
137 * PUBLIC: int log_end __P((SCR *, EXF *));
140 log_end(SCR
*sp
, EXF
*ep
)
144 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
146 /*LOCK_END(sp->wp, ep);*/
147 if (sp
->wp
->l_lp
!= NULL
) {
152 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
153 ep
->l_cursor
.cno
= 0;
154 ep
->l_high
= ep
->l_cur
= 1;
160 * Log the current cursor position, starting an event.
162 * PUBLIC: int log_cursor __P((SCR *));
170 if (F_ISSET(ep
, F_NOLOG
))
174 * If any changes were made since the last cursor init,
175 * put out the ending cursor record.
177 if (ep
->l_cursor
.lno
== OOBLNO
) {
178 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
180 ep
->l_cursor
.lno
= sp
->lno
;
181 ep
->l_cursor
.cno
= sp
->cno
;
183 return (log_cursor1(sp
, LOG_CURSOR_END
));
185 ep
->l_cursor
.lno
= sp
->lno
;
186 ep
->l_cursor
.cno
= sp
->cno
;
192 * Actually push a cursor record out.
195 log_cursor1(SCR
*sp
, int type
)
203 if (type == LOG_CURSOR_INIT &&
204 LOCK_TRY(sp->wp, ep))
208 if (type
== LOG_CURSOR_INIT
&&
209 (sp
->db_error
= __vi_log_truncate(ep
)) != 0) {
210 msgq(sp
, M_DBERR
, "truncate");
214 __vi_cursor_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, type
,
215 ep
->l_cursor
.lno
, ep
->l_cursor
.cno
)) != 0) {
216 msgq(sp
, M_DBERR
, "cursor_log");
219 if (type
== LOG_CURSOR_END
) {
220 MEMCPY(&ep
->lsn_high
, &ep
->lsn_cur
, 1);
221 /* XXXX should not be needed */
222 ep
->env
->log_flush(ep
->env
, NULL
);
225 #if defined(DEBUG) && 0
226 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
227 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
230 /* Reset high water mark. */
231 ep
->l_high
= ++ep
->l_cur
;
234 if (type == LOG_CURSOR_END)
235 LOCK_UNLOCK(sp->wp, ep);
244 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
247 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
256 if (F_ISSET(ep
, F_NOLOG
))
262 * Kluge for vi. Clear the EXF undo flag so that the
263 * next 'u' command does a roll-back, regardless.
267 /* Put out one initial cursor record per set of changes. */
268 if (ep
->l_cursor
.lno
!= OOBLNO
) {
269 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
271 ep
->l_cursor
.lno
= OOBLNO
;
273 } /*else if (ep->l_win != sp->wp) {
274 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
279 __vi_change_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, action
,
281 msgq(sp
, M_DBERR
, "change_log");
285 #if defined(DEBUG) && 0
287 case LOG_LINE_APPEND_F
:
288 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
289 ep
->l_cur
, lno
, len
);
291 case LOG_LINE_APPEND_B
:
292 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
293 ep
->l_cur
, lno
, len
);
295 case LOG_LINE_DELETE_F
:
296 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
297 ep
->l_cur
, lno
, len
);
299 case LOG_LINE_DELETE_B
:
300 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
301 ep
->l_cur
, lno
, len
);
303 case LOG_LINE_RESET_F
:
304 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
305 ep
->l_cur
, lno
, len
);
307 case LOG_LINE_RESET_B
:
308 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
309 ep
->l_cur
, lno
, len
);
313 /* Reset high water mark. */
314 ep
->l_high
= ++ep
->l_cur
;
321 * Log a mark position. For the log to work, we assume that there
322 * aren't any operations that just put out a log record -- this
323 * would mean that undo operations would only reset marks, and not
324 * cause any other change.
326 * PUBLIC: int log_mark __P((SCR *, LMARK *));
329 log_mark(SCR
*sp
, LMARK
*lmp
)
335 if (F_ISSET(ep
, F_NOLOG
))
338 /* Put out one initial cursor record per set of changes. */
339 if (ep
->l_cursor
.lno
!= OOBLNO
) {
340 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
342 ep
->l_cursor
.lno
= OOBLNO
;
347 __vi_mark_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0,
349 msgq(sp
, M_DBERR
, "cursor_log");
353 #if defined(DEBUG) && 0
354 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
355 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
357 /* Reset high water mark. */
358 ep
->l_high
= ++ep
->l_cur
;
364 * Roll the log backward one operation.
366 * PUBLIC: int log_backward __P((SCR *, MARK *));
369 log_backward(SCR
*sp
, MARK
*rp
)
380 if (F_ISSET(ep
, F_NOLOG
)) {
382 "010|Logging not being performed, undo not possible");
386 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
387 msgq(sp
, M_BERR
, "011|No changes to undo");
390 return __vi_log_traverse(sp
, UNDO_BACKWARD
, rp
);
395 * Reset the line to its original appearance.
398 * There's a bug in this code due to our not logging cursor movements
399 * unless a change was made. If you do a change, move off the line,
400 * then move back on and do a 'U', the line will be restored to the way
401 * it was before the original change.
403 * PUBLIC: int log_setline __P((SCR *));
416 if (F_ISSET(ep
, F_NOLOG
)) {
418 "012|Logging not being performed, undo not possible");
422 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
423 msgq(sp
, M_BERR
, "011|No changes to undo");
426 return __vi_log_traverse(sp
, UNDO_SETLINE
, &m
);
431 * Roll the log forward one operation.
433 * PUBLIC: int log_forward __P((SCR *, MARK *));
436 log_forward(SCR
*sp
, MARK
*rp
)
447 if (F_ISSET(ep
, F_NOLOG
)) {
449 "013|Logging not being performed, roll-forward not possible");
453 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_high
) >= 0) {
454 msgq(sp
, M_BERR
, "014|No changes to re-do");
457 return __vi_log_traverse(sp
, UNDO_FORWARD
, rp
);