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.20 2000/08/29 15:52:37 skimo Exp $ (Berkeley) $Date: 2000/08/29 15:52:37 $";
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");
112 //LOCK_INIT(sp->wp, ep);
119 * Close the logging subsystem.
121 * PUBLIC: int log_end __P((SCR *, EXF *));
130 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
132 //LOCK_END(sp->wp, ep);
133 if (ep
->log
!= NULL
) {
134 (void)(ep
->log
->close
)(ep
->log
,DB_NOSYNC
);
137 if (sp
->wp
->l_lp
!= NULL
) {
142 ep
->l_cursor
.lno
= 1; /* XXX Any valid recno. */
143 ep
->l_cursor
.cno
= 0;
144 ep
->l_high
= ep
->l_cur
= 1;
150 * Log the current cursor position, starting an event.
152 * PUBLIC: int log_cursor __P((SCR *));
161 if (F_ISSET(ep
, F_NOLOG
))
165 * If any changes were made since the last cursor init,
166 * put out the ending cursor record.
168 if (ep
->l_cursor
.lno
== OOBLNO
) {
169 if (ep
->l_win
&& 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 #if defined(DEBUG) && 0
215 vtrace(sp
, "%lu: %s: %u/%u\n", ep
->l_cur
,
216 type
== LOG_CURSOR_INIT
? "log_cursor_init" : "log_cursor_end",
219 /* Reset high water mark. */
220 ep
->l_high
= ++ep
->l_cur
;
223 if (type == LOG_CURSOR_END)
224 LOCK_UNLOCK(sp->wp, ep);
233 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
236 log_line(sp
, lno
, action
)
248 if (F_ISSET(ep
, F_NOLOG
))
254 * Kluge for vi. Clear the EXF undo flag so that the
255 * next 'u' command does a roll-back, regardless.
259 /* Put out one initial cursor record per set of changes. */
260 if (ep
->l_cursor
.lno
!= OOBLNO
) {
261 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
263 ep
->l_cursor
.lno
= OOBLNO
;
265 } /*else if (ep->l_win != sp->wp) {
266 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
271 * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
272 * special case, avoid the caches. Also, if it fails and it's
273 * line 1, it just means that the user started with an empty file,
274 * so fake an empty length line.
276 if (action
== LOG_LINE_RESET_B
) {
277 if (db_get(sp
, lno
, DBG_NOCACHE
, &lp
, &len
)) {
278 static CHAR_T nul
= 0;
287 if (db_get(sp
, lno
, DBG_FATAL
, &lp
, &len
))
290 sp
->wp
->l_lp
, sp
->wp
->l_len
,
291 len
* sizeof(CHAR_T
) + sizeof(u_char
) + sizeof(db_recno_t
));
292 sp
->wp
->l_lp
[0] = action
;
293 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), &lno
, sizeof(db_recno_t
));
294 MEMMOVEW(sp
->wp
->l_lp
+ sizeof(u_char
) + sizeof(db_recno_t
), lp
, len
);
297 memset(&key
, 0, sizeof(key
));
299 key
.size
= sizeof(db_recno_t
);
300 memset(&data
, 0, sizeof(data
));
301 data
.data
= sp
->wp
->l_lp
;
302 data
.size
= len
* sizeof(CHAR_T
) +
303 sizeof(u_char
) + sizeof(db_recno_t
);
304 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
307 #if defined(DEBUG) && 0
309 case LOG_LINE_APPEND
:
310 vtrace(sp
, "%u: log_line: append: %lu {%u}\n",
311 ep
->l_cur
, lno
, len
);
313 case LOG_LINE_DELETE
:
314 vtrace(sp
, "%lu: log_line: delete: %lu {%u}\n",
315 ep
->l_cur
, lno
, len
);
317 case LOG_LINE_INSERT
:
318 vtrace(sp
, "%lu: log_line: insert: %lu {%u}\n",
319 ep
->l_cur
, lno
, len
);
321 case LOG_LINE_RESET_F
:
322 vtrace(sp
, "%lu: log_line: reset_f: %lu {%u}\n",
323 ep
->l_cur
, lno
, len
);
325 case LOG_LINE_RESET_B
:
326 vtrace(sp
, "%lu: log_line: reset_b: %lu {%u}\n",
327 ep
->l_cur
, lno
, len
);
331 /* Reset high water mark. */
332 ep
->l_high
= ++ep
->l_cur
;
339 * Log a mark position. For the log to work, we assume that there
340 * aren't any operations that just put out a log record -- this
341 * would mean that undo operations would only reset marks, and not
342 * cause any other change.
344 * PUBLIC: int log_mark __P((SCR *, LMARK *));
355 if (F_ISSET(ep
, F_NOLOG
))
358 /* Put out one initial cursor record per set of changes. */
359 if (ep
->l_cursor
.lno
!= OOBLNO
) {
360 if (log_cursor1(sp
, LOG_CURSOR_INIT
))
362 ep
->l_cursor
.lno
= OOBLNO
;
366 BINC_RET(sp
, sp
->wp
->l_lp
,
367 sp
->wp
->l_len
, sizeof(u_char
) + sizeof(LMARK
));
368 sp
->wp
->l_lp
[0] = LOG_MARK
;
369 memmove(sp
->wp
->l_lp
+ sizeof(u_char
), lmp
, sizeof(LMARK
));
371 memset(&key
, 0, sizeof(key
));
372 key
.data
= &ep
->l_cur
;
373 key
.size
= sizeof(db_recno_t
);
374 memset(&data
, 0, sizeof(data
));
375 data
.data
= sp
->wp
->l_lp
;
376 data
.size
= sizeof(u_char
) + sizeof(LMARK
);
377 if (ep
->log
->put(ep
->log
, NULL
, &key
, &data
, 0) == -1)
380 #if defined(DEBUG) && 0
381 vtrace(sp
, "%lu: mark %c: %lu/%u\n",
382 ep
->l_cur
, lmp
->name
, lmp
->lno
, lmp
->cno
);
384 /* Reset high water mark. */
385 ep
->l_high
= ++ep
->l_cur
;
391 * Get a line from the log in log buffer.
394 vi_log_get(SCR
*sp
, db_recno_t
*lnop
, size_t *size
)
404 BINC_RET(sp
, sp
->wp
->l_lp
, sp
->wp
->l_len
, nlen
);
406 memset(&key
, 0, sizeof(key
));
407 key
.data
= lnop
; /* Initialize db request. */
408 key
.size
= sizeof(db_recno_t
);
409 memset(&data
, 0, sizeof(data
));
410 data
.data
= sp
->wp
->l_lp
;
411 data
.ulen
= sp
->wp
->l_len
;
412 data
.flags
= DB_DBT_USERMEM
;
413 switch (ep
->log
->get(ep
->log
, NULL
, &key
, &data
, 0)) {
427 * Roll the log backward one operation.
429 * PUBLIC: int log_backward __P((SCR *, MARK *));
445 if (F_ISSET(ep
, F_NOLOG
)) {
447 "010|Logging not being performed, undo not possible");
451 if (ep
->l_cur
== 1) {
452 msgq(sp
, M_BERR
, "011|No changes to undo");
456 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
457 ex_emsg(sp
, NULL
, EXM_LOCKED
);
463 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
467 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
469 #if defined(DEBUG) && 0
470 log_trace(sp
, "log_backward", ep
->l_cur
, data
.data
);
472 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
473 case LOG_CURSOR_INIT
:
475 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
483 case LOG_LINE_APPEND
:
484 case LOG_LINE_INSERT
:
486 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
487 if (db_delete(sp
, lno
))
489 ++sp
->rptlines
[L_DELETED
];
491 case LOG_LINE_DELETE
:
493 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
494 if (db_insert(sp
, lno
,
495 (CHAR_T
*)(p
+ sizeof(u_char
) +
497 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
500 ++sp
->rptlines
[L_ADDED
];
502 case LOG_LINE_RESET_F
:
504 case LOG_LINE_RESET_B
:
506 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
508 (CHAR_T
*)(p
+ sizeof(u_char
) +
510 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
513 if (sp
->rptlchange
!= lno
) {
514 sp
->rptlchange
= lno
;
515 ++sp
->rptlines
[L_CHANGED
];
520 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
523 if (mark_set(sp
, lm
.name
, &m
, 0))
531 err
: F_CLR(ep
, F_NOLOG
);
538 * Reset the line to its original appearance.
541 * There's a bug in this code due to our not logging cursor movements
542 * unless a change was made. If you do a change, move off the line,
543 * then move back on and do a 'U', the line will be restored to the way
544 * it was before the original change.
546 * PUBLIC: int log_setline __P((SCR *));
560 if (F_ISSET(ep
, F_NOLOG
)) {
562 "012|Logging not being performed, undo not possible");
569 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
570 ex_emsg(sp
, NULL
, EXM_LOCKED
);
575 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
579 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
581 #if defined(DEBUG) && 0
582 log_trace(sp
, "log_setline", ep
->l_cur
, data
.data
);
584 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
585 case LOG_CURSOR_INIT
:
586 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
587 if (m
.lno
!= sp
->lno
|| ep
->l_cur
== 1) {
594 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
595 if (m
.lno
!= sp
->lno
) {
602 case LOG_LINE_APPEND
:
603 case LOG_LINE_INSERT
:
604 case LOG_LINE_DELETE
:
605 case LOG_LINE_RESET_F
:
607 case LOG_LINE_RESET_B
:
608 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
609 if (lno
== sp
->lno
&&
610 db_set(sp
, lno
, (CHAR_T
*)(p
+ sizeof(u_char
) +
611 sizeof(db_recno_t
)), size
- sizeof(u_char
) -
614 if (sp
->rptlchange
!= lno
) {
615 sp
->rptlchange
= lno
;
616 ++sp
->rptlines
[L_CHANGED
];
619 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
622 if (mark_set(sp
, lm
.name
, &m
, 0))
630 err
: F_CLR(ep
, F_NOLOG
);
637 * Roll the log forward one operation.
639 * PUBLIC: int log_forward __P((SCR *, MARK *));
655 if (F_ISSET(ep
, F_NOLOG
)) {
657 "013|Logging not being performed, roll-forward not possible");
661 if (ep
->l_cur
== ep
->l_high
) {
662 msgq(sp
, M_BERR
, "014|No changes to re-do");
666 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
667 ex_emsg(sp
, NULL
, EXM_LOCKED
);
672 F_SET(ep
, F_NOLOG
); /* Turn off logging. */
676 if (vi_log_get(sp
, &ep
->l_cur
, &size
))
678 #if defined(DEBUG) && 0
679 log_trace(sp
, "log_forward", ep
->l_cur
, data
.data
);
681 switch (*(p
= (u_char
*)sp
->wp
->l_lp
)) {
685 memmove(rp
, p
+ sizeof(u_char
), sizeof(MARK
));
691 case LOG_CURSOR_INIT
:
693 /* XXXX LOG_LINE_APPEND and LOG_LINE_INSERT split
694 for now, because db_insert won't work for adding
697 case LOG_LINE_APPEND
:
699 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
701 if (db_append(sp
, 1, lno
,
702 (CHAR_T
*)(p
+ sizeof(u_char
) +
704 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
707 ++sp
->rptlines
[L_ADDED
];
709 case LOG_LINE_INSERT
:
711 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
712 if (db_insert(sp
, lno
,
713 (CHAR_T
*)(p
+ sizeof(u_char
) +
715 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
718 ++sp
->rptlines
[L_ADDED
];
720 case LOG_LINE_DELETE
:
722 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
723 if (db_delete(sp
, lno
))
725 ++sp
->rptlines
[L_DELETED
];
727 case LOG_LINE_RESET_B
:
729 case LOG_LINE_RESET_F
:
731 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
733 (CHAR_T
*)(p
+ sizeof(u_char
) +
735 (size
- sizeof(u_char
) - sizeof(db_recno_t
))
738 if (sp
->rptlchange
!= lno
) {
739 sp
->rptlchange
= lno
;
740 ++sp
->rptlines
[L_CHANGED
];
745 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
748 if (mark_set(sp
, lm
.name
, &m
, 0))
756 err
: F_CLR(ep
, F_NOLOG
);
763 * Try and restart the log on failure, i.e. if we run out of memory.
766 log_err(sp
, file
, line
)
773 msgq(sp
, M_SYSERR
, "015|%s/%d: log put error", tail(file
), line
);
775 (void)ep
->log
->close(ep
->log
, DB_NOSYNC
);
776 if (!log_init(sp
, ep
))
777 msgq(sp
, M_ERR
, "267|Log restarted");
780 #if defined(DEBUG) && 0
782 log_trace(sp
, msg
, rno
, p
)
793 case LOG_CURSOR_INIT
:
794 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
795 vtrace(sp
, "%lu: %s: C_INIT: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
798 memmove(&m
, p
+ sizeof(u_char
), sizeof(MARK
));
799 vtrace(sp
, "%lu: %s: C_END: %u/%u\n", rno
, msg
, m
.lno
, m
.cno
);
801 case LOG_LINE_APPEND
:
802 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
803 vtrace(sp
, "%lu: %s: APPEND: %lu\n", rno
, msg
, lno
);
805 case LOG_LINE_INSERT
:
806 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
807 vtrace(sp
, "%lu: %s: INSERT: %lu\n", rno
, msg
, lno
);
809 case LOG_LINE_DELETE
:
810 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
811 vtrace(sp
, "%lu: %s: DELETE: %lu\n", rno
, msg
, lno
);
813 case LOG_LINE_RESET_F
:
814 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
815 vtrace(sp
, "%lu: %s: RESET_F: %lu\n", rno
, msg
, lno
);
817 case LOG_LINE_RESET_B
:
818 memmove(&lno
, p
+ sizeof(u_char
), sizeof(db_recno_t
));
819 vtrace(sp
, "%lu: %s: RESET_B: %lu\n", rno
, msg
, lno
);
822 memmove(&lm
, p
+ sizeof(u_char
), sizeof(LMARK
));
824 "%lu: %s: MARK: %u/%u\n", rno
, msg
, lm
.lno
, lm
.cno
);