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: log.c,v 10.18 2000/07/22 14:52:36 skimo Exp $ (Berkeley) $Date: 2000/07/22 14:52:36 $";
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 db_recno_t char *
37 * LOG_LINE_DELETE db_recno_t char *
38 * LOG_LINE_INSERT db_recno_t char *
39 * LOG_LINE_RESET_F db_recno_t char *
40 * LOG_LINE_RESET_B db_recno_t char *
43 * We do before image physical logging. This means that the editor layer
44 * MAY NOT modify records in place, even if simply deleting or overwriting
45 * characters. Since the smallest unit of logging is a line, we're using
46 * up lots of space. This may eventually have to be reduced, probably by
47 * doing logical logging, which is a much cooler database phrase.
49 * The implementation of the historic vi 'u' command, using roll-forward and
50 * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
51 * followed by a number of other records, followed by a LOG_CURSOR_END record.
52 * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
53 * record, and is the line before the change. The second is LOG_LINE_RESET_F,
54 * and is the line after the change. Roll-back is done by backing up to the
55 * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
58 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
59 * record for a line different from the current one. It should be noted that
60 * this means that a subsequent 'u' command will make a change based on the
61 * new position of the log's cursor. This is okay, and, in fact, historic vi
65 static int vi_log_get
__P((SCR
*sp
, db_recno_t
*lnop
, size_t *size
));
66 static int log_cursor1
__P((SCR
*, int));
67 static void log_err
__P((SCR
*, char *, int));
68 #if defined(DEBUG) && 0
69 static void log_trace
__P((SCR
*, char *, db_recno_t
, u_char
*));
72 /* Try and restart the log on failure, i.e. if we run out of memory. */
74 log_err(sp, __FILE__, __LINE__); \
80 * Initialize the logging subsystem.
82 * PUBLIC: int log_init __P((SCR *, EXF *));
91 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
93 * Initialize the buffer. The logging subsystem has its own
94 * buffers because the global ones are almost by definition
95 * going to be in use when the log runs.
99 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
100 ep
->l_cursor
.cno
= 0;
101 ep
->l_high
= ep
->l_cur
= 1;
103 if (db_create(&ep
->log
, sp
->gp
->env
, 0) != 0 ||
104 ep
->log
->open(ep
->log
, NULL
, NULL
, DB_RECNO
, DB_CREATE
| DB_THREAD
,
105 S_IRUSR
| S_IWUSR
) != 0) {
106 msgq(sp
, M_SYSERR
, "009|Log file");
111 //LOCK_INIT(sp->wp, ep);
118 * Close the logging subsystem.
120 * PUBLIC: int log_end __P((SCR *, EXF *));
129 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
131 //LOCK_END(sp->wp, ep);
132 if (ep
->log
!= NULL
) {
133 (void)(ep
->log
->close
)(ep
->log
,DB_NOSYNC
);
136 if (sp
->wp
->l_lp
!= NULL
) {
141 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
142 ep
->l_cursor
.cno
= 0;
143 ep
->l_high
= ep
->l_cur
= 1;
149 * Log the current cursor position, starting an event.
151 * PUBLIC: int log_cursor __P((SCR *));
160 if (F_ISSET(ep
, F_NOLOG
))
164 * If any changes were made since the last cursor init,
165 * put out the ending cursor record.
167 if (ep
->l_cursor
.lno
== OOBLNO
) {
168 printf("own: %p, this: %p\n", ep
->l_win
, sp
->wp
);
169 if (ep
->l_win
!= sp
->wp
)
171 ep
->l_cursor
.lno
= sp
->lno
;
172 ep
->l_cursor
.cno
= sp
->cno
;
174 return (log_cursor1(sp
, LOG_CURSOR_END
));
176 ep
->l_cursor
.lno
= sp
->lno
;
177 ep
->l_cursor
.cno
= sp
->cno
;
183 * Actually push a cursor record out.
186 log_cursor1(sp
, type
)
196 if (type == LOG_CURSOR_INIT &&
197 LOCK_TRY(sp->wp, ep))
201 BINC_RET(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, sizeof(u_char
) + sizeof(MARK
));
202 sp
->wp
->l_lp
[0] = type
;
203 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &ep
->l_cursor
, sizeof(MARK
));
205 memset(&key
, 0, sizeof(key
));
206 key
.data
= &ep
->l_cur
;
207 key
.size
= sizeof(db_recno_t
);
208 memset(&data
, 0, sizeof(data
));
209 data
.data
= sp
->wp
->l_lp
;
210 data
.size
= sizeof(u_char
) + sizeof(MARK
);
211 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
214 puts(type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end");
215 #if defined(DEBUG) && 0
216 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
217 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
220 /* Reset high water mark. */
221 ep
->l_high
= ++ep
->l_cur
;
224 if (type == LOG_CURSOR_END)
225 LOCK_UNLOCK(sp->wp, ep);
234 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
237 log_line(sp
, lno
, action
)
249 if (F_ISSET(ep
, F_NOLOG
))
255 * Kluge for vi. Clear the EXF undo flag so that the
256 * next 'u' command does a roll-back, regardless.
260 /* Put out one initial cursor record per set of changes. */
261 if (ep
->l_cursor
.lno
!= OOBLNO
) {
263 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
265 ep
->l_cursor
.lno
= OOBLNO
;
267 } else if (ep
->l_win
!= sp
->wp
) {
268 printf("log_line own: %p, this: %p\n", ep
->l_win
, sp
->wp
);
273 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
274 * special case, avoid the caches. Also, if it fails and it's
275 * line 1, it just means that the user started with an empty file,
276 * so fake an empty length line.
278 if (action
== LOG_LINE_RESET_B
) {
279 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
280 static CHAR_T nul
= 0;
289 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
292 sp
->wp
->l_lp
, sp
->wp
->l_len
,
293 len
* sizeof(CHAR_T
) + sizeof(u_char
) + sizeof(db_recno_t
));
294 sp
->wp
->l_lp
[0] = action
;
295 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
296 MEMMOVEW(sp
->wp
->l_lp
+ sizeof(u_char
) + sizeof(db_recno_t
), lp
, len
);
299 memset(&key
, 0, sizeof(key
));
301 key
.size
= sizeof(db_recno_t
);
302 memset(&data
, 0, sizeof(data
));
303 data
.data
= sp
->wp
->l_lp
;
304 data
.size
= len
* sizeof(CHAR_T
) +
305 sizeof(u_char
) + sizeof(db_recno_t
);
306 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
309 #if defined(DEBUG) && 0
311 case LOG_LINE_APPEND
:
312 vtrace(sp
, "%u: log_line: append: %lu {%u}\n",
313 ep
->l_cur
, lno
, len
);
315 case LOG_LINE_DELETE
:
316 vtrace(sp
, "%lu: log_line: delete: %lu {%u}\n",
317 ep
->l_cur
, lno
, len
);
319 case LOG_LINE_INSERT
:
320 vtrace(sp
, "%lu: log_line: insert: %lu {%u}\n",
321 ep
->l_cur
, lno
, len
);
323 case LOG_LINE_RESET_F
:
324 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
325 ep
->l_cur
, lno
, len
);
327 case LOG_LINE_RESET_B
:
328 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
329 ep
->l_cur
, lno
, len
);
333 /* Reset high water mark. */
334 ep
->l_high
= ++ep
->l_cur
;
341 * Log a mark position. For the log to work, we assume that there
342 * aren't any operations that just put out a log record -- this
343 * would mean that undo operations would only reset marks, and not
344 * cause any other change.
346 * PUBLIC: int log_mark __P((SCR *, LMARK *));
357 if (F_ISSET(ep
, F_NOLOG
))
360 /* Put out one initial cursor record per set of changes. */
361 if (ep
->l_cursor
.lno
!= OOBLNO
) {
363 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
365 ep
->l_cursor
.lno
= OOBLNO
;
369 BINC_RET(sp
, sp
->wp
->l_lp
,
370 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
371 sp
->wp
->l_lp
[0] = LOG_MARK
;
372 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
374 memset(&key
, 0, sizeof(key
));
375 key
.data
= &ep
->l_cur
;
376 key
.size
= sizeof(db_recno_t
);
377 memset(&data
, 0, sizeof(data
));
378 data
.data
= sp
->wp
->l_lp
;
379 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
380 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
383 #if defined(DEBUG) && 0
384 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
385 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
387 /* Reset high water mark. */
388 ep
->l_high
= ++ep
->l_cur
;
394 * Get a line from the log in log buffer.
397 vi_log_get(SCR
*sp
, db_recno_t
*lnop
, size_t *size
)
407 BINC_RET(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
409 memset(&key
, 0, sizeof(key
));
410 key
.data
= lnop
; /* Initialize db request. */
411 key
.size
= sizeof(db_recno_t
);
412 memset(&data
, 0, sizeof(data
));
413 data
.data
= sp
->wp
->l_lp
;
414 data
.ulen
= sp
->wp
->l_len
;
415 data
.flags
= DB_DBT_USERMEM
;
416 switch (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0)) {
430 * Roll the log backward one operation.
432 * PUBLIC: int log_backward __P((SCR *, MARK *));
448 if (F_ISSET(ep
, F_NOLOG
)) {
450 "010|Logging not being performed, undo not possible");
454 if (ep
->l_cur
== 1) {
455 msgq(sp
, M_BERR
, "011|No changes to undo");
459 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
463 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
465 #if defined(DEBUG) && 0
466 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
468 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
469 case LOG_CURSOR_INIT
:
471 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
478 case LOG_LINE_APPEND
:
479 case LOG_LINE_INSERT
:
481 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
482 if (db_delete(sp
, lno
))
484 ++sp
->rptlines
[L_DELETED
];
486 case LOG_LINE_DELETE
:
488 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
489 if (db_insert(sp
, lno
,
490 (CHAR_T
*)(p
+ sizeof(u_char
) +
492 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
495 ++sp
->rptlines
[L_ADDED
];
497 case LOG_LINE_RESET_F
:
499 case LOG_LINE_RESET_B
:
501 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
503 (CHAR_T
*)(p
+ sizeof(u_char
) +
505 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
508 if (sp
->rptlchange
!= lno
) {
509 sp
->rptlchange
= lno
;
510 ++sp
->rptlines
[L_CHANGED
];
515 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
518 if (mark_set(sp
, lm
.name
, &m
, 0))
526 err
: F_CLR(ep
, F_NOLOG
);
532 * Reset the line to its original appearance.
535 * There's a bug in this code due to our not logging cursor movements
536 * unless a change was made. If you do a change, move off the line,
537 * then move back on and do a 'U', the line will be restored to the way
538 * it was before the original change.
540 * PUBLIC: int log_setline __P((SCR *));
554 if (F_ISSET(ep
, F_NOLOG
)) {
556 "012|Logging not being performed, undo not possible");
563 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
567 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
569 #if defined(DEBUG) && 0
570 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
572 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
573 case LOG_CURSOR_INIT
:
574 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
575 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
581 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
582 if (m
.lno
!= sp
->lno
) {
588 case LOG_LINE_APPEND
:
589 case LOG_LINE_INSERT
:
590 case LOG_LINE_DELETE
:
591 case LOG_LINE_RESET_F
:
593 case LOG_LINE_RESET_B
:
594 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
595 if (lno
== sp
->lno
&&
596 db_set(sp
, lno
, (CHAR_T
*)(p
+ sizeof(u_char
) +
597 sizeof(db_recno_t
)), size
- sizeof(u_char
) -
600 if (sp
->rptlchange
!= lno
) {
601 sp
->rptlchange
= lno
;
602 ++sp
->rptlines
[L_CHANGED
];
605 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
608 if (mark_set(sp
, lm
.name
, &m
, 0))
616 err
: F_CLR(ep
, F_NOLOG
);
622 * Roll the log forward one operation.
624 * PUBLIC: int log_forward __P((SCR *, MARK *));
640 if (F_ISSET(ep
, F_NOLOG
)) {
642 "013|Logging not being performed, roll-forward not possible");
646 if (ep
->l_cur
== ep
->l_high
) {
647 msgq(sp
, M_BERR
, "014|No changes to re-do");
651 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
655 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
657 #if defined(DEBUG) && 0
658 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
660 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
664 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
669 case LOG_CURSOR_INIT
:
671 /* XXXX LOG_LINE_APPEND and LOG_LINE_INSERT split
672 for now, because db_insert won't work for adding
675 case LOG_LINE_APPEND
:
677 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
679 if (db_append(sp
, 1, lno
,
680 (CHAR_T
*)(p
+ sizeof(u_char
) +
682 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
685 ++sp
->rptlines
[L_ADDED
];
687 case LOG_LINE_INSERT
:
689 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
690 if (db_insert(sp
, lno
,
691 (CHAR_T
*)(p
+ sizeof(u_char
) +
693 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
696 ++sp
->rptlines
[L_ADDED
];
698 case LOG_LINE_DELETE
:
700 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
701 if (db_delete(sp
, lno
))
703 ++sp
->rptlines
[L_DELETED
];
705 case LOG_LINE_RESET_B
:
707 case LOG_LINE_RESET_F
:
709 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
711 (CHAR_T
*)(p
+ sizeof(u_char
) +
713 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
716 if (sp
->rptlchange
!= lno
) {
717 sp
->rptlchange
= lno
;
718 ++sp
->rptlines
[L_CHANGED
];
723 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
726 if (mark_set(sp
, lm
.name
, &m
, 0))
734 err
: F_CLR(ep
, F_NOLOG
);
740 * Try and restart the log on failure, i.e. if we run out of memory.
743 log_err(sp
, file
, line
)
750 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
752 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
753 if (!log_init(sp
, ep
))
754 msgq(sp
, M_ERR
, "267|Log restarted");
757 #if defined(DEBUG) && 0
759 log_trace(sp
, msg
, rno
, p
)
770 case LOG_CURSOR_INIT
:
771 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
772 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
775 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
776 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
778 case LOG_LINE_APPEND
:
779 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
780 vtrace(sp
, "%lu: %s: APPEND: %lu\n", rno
, msg
, lno
);
782 case LOG_LINE_INSERT
:
783 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
784 vtrace(sp
, "%lu: %s: INSERT: %lu\n", rno
, msg
, lno
);
786 case LOG_LINE_DELETE
:
787 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
788 vtrace(sp
, "%lu: %s: DELETE: %lu\n", rno
, msg
, lno
);
790 case LOG_LINE_RESET_F
:
791 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
792 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
794 case LOG_LINE_RESET_B
:
795 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
796 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
799 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
801 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);