2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: log4.c,v 10.3 2002/06/08 21:00:33 skimo Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
31 * The log consists of records, each containing a type byte and a variable
32 * length byte string, as follows:
34 * LOG_CURSOR_INIT MARK
36 * LOG_LINE_APPEND_F db_recno_t char *
37 * LOG_LINE_APPEND_B db_recno_t char *
38 * LOG_LINE_DELETE_F db_recno_t char *
39 * LOG_LINE_DELETE_B db_recno_t char *
40 * LOG_LINE_RESET_F db_recno_t char *
41 * LOG_LINE_RESET_B db_recno_t char *
44 * We do before image physical logging. This means that the editor layer
45 * MAY NOT modify records in place, even if simply deleting or overwriting
46 * characters. Since the smallest unit of logging is a line, we're using
47 * up lots of space. This may eventually have to be reduced, probably by
48 * doing logical logging, which is a much cooler database phrase.
50 * The implementation of the historic vi 'u' command, using roll-forward and
51 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
52 * followed by a number of other records, followed by a LOG_CURSOR_END record.
53 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
54 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
55 * and is the line after the change. Roll-back is done by backing up to the
56 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
59 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
60 * record for a line different from the current one. It should be noted that
61 * this means that a subsequent 'u' command will make a change based on the
62 * new position of the log's cursor. This is okay, and, in fact, historic vi
66 static int log_cursor1
__P((SCR
*, int));
70 * Initialize the logging subsystem.
72 * PUBLIC: int log_init __P((SCR *, EXF *));
75 log_init(SCR
*sp
, EXF
*ep
)
83 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
85 * Initialize the buffer. The logging subsystem has its own
86 * buffers because the global ones are almost by definition
87 * going to be in use when the log runs.
91 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
93 ep
->l_high
= ep
->l_cur
= 1;
95 if ((sp
->db_error
= ep
->env
->log_cursor(ep
->env
, &logc
, 0))
97 msgq(sp
, M_DBERR
, "env->log_cursor");
103 BINC_GOTO(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
104 memset(&data
, 0, sizeof(data
));
105 data
.data
= sp
->wp
->l_lp
;
106 data
.ulen
= sp
->wp
->l_len
;
107 data
.flags
= DB_DBT_USERMEM
;
108 switch ((sp
->db_error
=
109 logc
->get(logc
, &ep
->lsn_first
, &data
, DB_LAST
))) {
115 msgq(sp
, M_DBERR
, "logc->get");
121 MEMCPY(&ep
->lsn_cur
, &ep
->lsn_first
, 1);
122 MEMCPY(&ep
->lsn_high
, &ep
->lsn_first
, 1);
123 logc
->close(logc
, 0);
126 /*LOCK_INIT(sp->wp, ep);*/
133 * Close the logging subsystem.
135 * PUBLIC: int log_end __P((SCR *, EXF *));
138 log_end(SCR
*sp
, EXF
*ep
)
142 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
144 /*LOCK_END(sp->wp, ep);*/
145 if (sp
->wp
->l_lp
!= NULL
) {
150 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
151 ep
->l_cursor
.cno
= 0;
152 ep
->l_high
= ep
->l_cur
= 1;
158 * Log the current cursor position, starting an event.
160 * PUBLIC: int log_cursor __P((SCR *));
168 if (F_ISSET(ep
, F_NOLOG
))
172 * If any changes were made since the last cursor init,
173 * put out the ending cursor record.
175 if (ep
->l_cursor
.lno
== OOBLNO
) {
176 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
)
178 ep
->l_cursor
.lno
= sp
->lno
;
179 ep
->l_cursor
.cno
= sp
->cno
;
181 return (log_cursor1(sp
, LOG_CURSOR_END
));
183 ep
->l_cursor
.lno
= sp
->lno
;
184 ep
->l_cursor
.cno
= sp
->cno
;
190 * Actually push a cursor record out.
193 log_cursor1(SCR
*sp
, int type
)
201 if (type == LOG_CURSOR_INIT &&
202 LOCK_TRY(sp->wp, ep))
206 if (type
== LOG_CURSOR_INIT
&&
207 (sp
->db_error
= __vi_log_truncate(ep
)) != 0) {
208 msgq(sp
, M_DBERR
, "truncate");
212 __vi_cursor_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, type
,
213 ep
->l_cursor
.lno
, ep
->l_cursor
.cno
)) != 0) {
214 msgq(sp
, M_DBERR
, "cursor_log");
217 if (type
== LOG_CURSOR_END
) {
218 MEMCPY(&ep
->lsn_high
, &ep
->lsn_cur
, 1);
219 /* XXXX should not be needed */
220 ep
->env
->log_flush(ep
->env
, NULL
);
223 #if defined(DEBUG) && 0
224 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
225 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
228 /* Reset high water mark. */
229 ep
->l_high
= ++ep
->l_cur
;
232 if (type == LOG_CURSOR_END)
233 LOCK_UNLOCK(sp->wp, ep);
242 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
245 log_line(SCR
*sp
, db_recno_t lno
, u_int action
)
254 if (F_ISSET(ep
, F_NOLOG
))
260 * Kluge for vi. Clear the EXF undo flag so that the
261 * next 'u' command does a roll-back, regardless.
265 /* Put out one initial cursor record per set of changes. */
266 if (ep
->l_cursor
.lno
!= OOBLNO
) {
267 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
269 ep
->l_cursor
.lno
= OOBLNO
;
271 } /*else if (ep->l_win != sp->wp) {
272 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
277 __vi_change_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0, action
,
279 msgq(sp
, M_DBERR
, "change_log");
283 #if defined(DEBUG) && 0
285 case LOG_LINE_APPEND_F
:
286 vtrace(sp
, "%u: log_line: append_f: %lu {%u}\n",
287 ep
->l_cur
, lno
, len
);
289 case LOG_LINE_APPEND_B
:
290 vtrace(sp
, "%u: log_line: append_b: %lu {%u}\n",
291 ep
->l_cur
, lno
, len
);
293 case LOG_LINE_DELETE_F
:
294 vtrace(sp
, "%lu: log_line: delete_f: %lu {%u}\n",
295 ep
->l_cur
, lno
, len
);
297 case LOG_LINE_DELETE_B
:
298 vtrace(sp
, "%lu: log_line: delete_b: %lu {%u}\n",
299 ep
->l_cur
, lno
, len
);
301 case LOG_LINE_RESET_F
:
302 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
303 ep
->l_cur
, lno
, len
);
305 case LOG_LINE_RESET_B
:
306 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
307 ep
->l_cur
, lno
, len
);
311 /* Reset high water mark. */
312 ep
->l_high
= ++ep
->l_cur
;
319 * Log a mark position. For the log to work, we assume that there
320 * aren't any operations that just put out a log record -- this
321 * would mean that undo operations would only reset marks, and not
322 * cause any other change.
324 * PUBLIC: int log_mark __P((SCR *, LMARK *));
327 log_mark(SCR
*sp
, LMARK
*lmp
)
333 if (F_ISSET(ep
, F_NOLOG
))
336 /* Put out one initial cursor record per set of changes. */
337 if (ep
->l_cursor
.lno
!= OOBLNO
) {
338 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
340 ep
->l_cursor
.lno
= OOBLNO
;
345 __vi_mark_log(ep
->env
, NULL
, &ep
->lsn_cur
, 0,
347 msgq(sp
, M_DBERR
, "cursor_log");
351 #if defined(DEBUG) && 0
352 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
353 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
355 /* Reset high water mark. */
356 ep
->l_high
= ++ep
->l_cur
;
362 * Roll the log backward one operation.
364 * PUBLIC: int log_backward __P((SCR *, MARK *));
367 log_backward(SCR
*sp
, MARK
*rp
)
378 if (F_ISSET(ep
, F_NOLOG
)) {
380 "010|Logging not being performed, undo not possible");
384 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
385 msgq(sp
, M_BERR
, "011|No changes to undo");
388 return __vi_log_traverse(sp
, UNDO_BACKWARD
, rp
);
393 * Reset the line to its original appearance.
396 * There's a bug in this code due to our not logging cursor movements
397 * unless a change was made. If you do a change, move off the line,
398 * then move back on and do a 'U', the line will be restored to the way
399 * it was before the original change.
401 * PUBLIC: int log_setline __P((SCR *));
414 if (F_ISSET(ep
, F_NOLOG
)) {
416 "012|Logging not being performed, undo not possible");
420 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_first
) <= 0) {
421 msgq(sp
, M_BERR
, "011|No changes to undo");
424 return __vi_log_traverse(sp
, UNDO_SETLINE
, &m
);
429 * Roll the log forward one operation.
431 * PUBLIC: int log_forward __P((SCR *, MARK *));
434 log_forward(SCR
*sp
, MARK
*rp
)
445 if (F_ISSET(ep
, F_NOLOG
)) {
447 "013|Logging not being performed, roll-forward not possible");
451 if (log_compare(&ep
->lsn_cur
, &ep
->lsn_high
) >= 0) {
452 msgq(sp
, M_BERR
, "014|No changes to re-do");
455 return __vi_log_traverse(sp
, UNDO_FORWARD
, rp
);