remove generated compile, depcomp, missing and mkinstalldirs
[nvi.git] / common / log4.c
blob37d07f708bc9d3680083a187df4b6b4ff9b61ac9
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: log4.c,v 10.3 2002/06/08 21:00:33 skimo Exp $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "common.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
35 * LOG_CURSOR_END 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 *
42 * LOG_MARK LMARK
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
57 * similar fashion.
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
63 * behaved that way.
66 static int log_cursor1 __P((SCR *, int));
69 * log_init --
70 * Initialize the logging subsystem.
72 * PUBLIC: int log_init __P((SCR *, EXF *));
74 int
75 log_init(SCR *sp, EXF *ep)
77 DB_LOGC *logc;
78 DBT data;
79 size_t nlen;
82 * !!!
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.
89 sp->wp->l_lp = NULL;
90 sp->wp->l_len = 0;
91 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
92 ep->l_cursor.cno = 0;
93 ep->l_high = ep->l_cur = 1;
95 if ((sp->db_error = ep->env->log_cursor(ep->env, &logc, 0))
96 != 0) {
97 msgq(sp, M_DBERR, "env->log_cursor");
98 F_SET(ep, F_NOLOG);
99 return (1);
101 nlen = 1024;
102 retry:
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))) {
110 case ENOMEM:
111 nlen = data.size;
112 goto retry;
113 default:
114 alloc_err:
115 msgq(sp, M_DBERR, "logc->get");
116 F_SET(ep, F_NOLOG);
117 return (1);
118 case 0:
121 MEMCPY(&ep->lsn_cur, &ep->lsn_first, 1);
122 MEMCPY(&ep->lsn_high, &ep->lsn_first, 1);
123 logc->close(logc, 0);
125 ep->l_win = NULL;
126 /*LOCK_INIT(sp->wp, ep);*/
128 return (0);
132 * log_end --
133 * Close the logging subsystem.
135 * PUBLIC: int log_end __P((SCR *, EXF *));
138 log_end(SCR *sp, EXF *ep)
141 * !!!
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) {
146 free(sp->wp->l_lp);
147 sp->wp->l_lp = NULL;
149 sp->wp->l_len = 0;
150 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
151 ep->l_cursor.cno = 0;
152 ep->l_high = ep->l_cur = 1;
153 return (0);
157 * log_cursor --
158 * Log the current cursor position, starting an event.
160 * PUBLIC: int log_cursor __P((SCR *));
163 log_cursor(SCR *sp)
165 EXF *ep;
167 ep = sp->ep;
168 if (F_ISSET(ep, F_NOLOG))
169 return (0);
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)
177 return 0;
178 ep->l_cursor.lno = sp->lno;
179 ep->l_cursor.cno = sp->cno;
180 ep->l_win = NULL;
181 return (log_cursor1(sp, LOG_CURSOR_END));
183 ep->l_cursor.lno = sp->lno;
184 ep->l_cursor.cno = sp->cno;
185 return (0);
189 * log_cursor1 --
190 * Actually push a cursor record out.
192 static int
193 log_cursor1(SCR *sp, int type)
195 DBT data, key;
196 EXF *ep;
198 ep = sp->ep;
201 if (type == LOG_CURSOR_INIT &&
202 LOCK_TRY(sp->wp, ep))
203 return 1;
206 if (type == LOG_CURSOR_INIT &&
207 (sp->db_error = __vi_log_truncate(ep)) != 0) {
208 msgq(sp, M_DBERR, "truncate");
209 return 1;
211 if ((sp->db_error =
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");
215 return 1;
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",
226 sp->lno, sp->cno);
227 #endif
228 /* Reset high water mark. */
229 ep->l_high = ++ep->l_cur;
232 if (type == LOG_CURSOR_END)
233 LOCK_UNLOCK(sp->wp, ep);
235 return (0);
239 * log_line --
240 * Log a line change.
242 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
245 log_line(SCR *sp, db_recno_t lno, u_int action)
247 DBT data, key;
248 EXF *ep;
249 size_t len;
250 CHAR_T *lp;
251 db_recno_t lcur;
253 ep = sp->ep;
254 if (F_ISSET(ep, F_NOLOG))
255 return (0);
258 * XXX
260 * Kluge for vi. Clear the EXF undo flag so that the
261 * next 'u' command does a roll-back, regardless.
263 F_CLR(ep, F_UNDO);
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))
268 return (1);
269 ep->l_cursor.lno = OOBLNO;
270 ep->l_win = sp->wp;
271 } /*else if (ep->l_win != sp->wp) {
272 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
273 return 1;
276 if ((sp->db_error =
277 __vi_change_log(ep->env, NULL, &ep->lsn_cur, 0, action,
278 lno)) != 0) {
279 msgq(sp, M_DBERR, "change_log");
280 return 1;
283 #if defined(DEBUG) && 0
284 switch (action) {
285 case LOG_LINE_APPEND_F:
286 vtrace(sp, "%u: log_line: append_f: %lu {%u}\n",
287 ep->l_cur, lno, len);
288 break;
289 case LOG_LINE_APPEND_B:
290 vtrace(sp, "%u: log_line: append_b: %lu {%u}\n",
291 ep->l_cur, lno, len);
292 break;
293 case LOG_LINE_DELETE_F:
294 vtrace(sp, "%lu: log_line: delete_f: %lu {%u}\n",
295 ep->l_cur, lno, len);
296 break;
297 case LOG_LINE_DELETE_B:
298 vtrace(sp, "%lu: log_line: delete_b: %lu {%u}\n",
299 ep->l_cur, lno, len);
300 break;
301 case LOG_LINE_RESET_F:
302 vtrace(sp, "%lu: log_line: reset_f: %lu {%u}\n",
303 ep->l_cur, lno, len);
304 break;
305 case LOG_LINE_RESET_B:
306 vtrace(sp, "%lu: log_line: reset_b: %lu {%u}\n",
307 ep->l_cur, lno, len);
308 break;
310 #endif
311 /* Reset high water mark. */
312 ep->l_high = ++ep->l_cur;
314 return (0);
318 * log_mark --
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)
329 DBT data, key;
330 EXF *ep;
332 ep = sp->ep;
333 if (F_ISSET(ep, F_NOLOG))
334 return (0);
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))
339 return (1);
340 ep->l_cursor.lno = OOBLNO;
341 ep->l_win = sp->wp;
344 if ((sp->db_error =
345 __vi_mark_log(ep->env, NULL, &ep->lsn_cur, 0,
346 lmp)) != 0) {
347 msgq(sp, M_DBERR, "cursor_log");
348 return 1;
351 #if defined(DEBUG) && 0
352 vtrace(sp, "%lu: mark %c: %lu/%u\n",
353 ep->l_cur, lmp->name, lmp->lno, lmp->cno);
354 #endif
355 /* Reset high water mark. */
356 ep->l_high = ++ep->l_cur;
357 return (0);
361 * Log_backward --
362 * Roll the log backward one operation.
364 * PUBLIC: int log_backward __P((SCR *, MARK *));
367 log_backward(SCR *sp, MARK *rp)
369 EXF *ep;
370 LMARK lm;
371 MARK m;
372 db_recno_t lno;
373 int didop;
374 u_char *p;
375 size_t size;
377 ep = sp->ep;
378 if (F_ISSET(ep, F_NOLOG)) {
379 msgq(sp, M_ERR,
380 "010|Logging not being performed, undo not possible");
381 return (1);
384 if (log_compare(&ep->lsn_cur, &ep->lsn_first) <= 0) {
385 msgq(sp, M_BERR, "011|No changes to undo");
386 return (1);
388 return __vi_log_traverse(sp, UNDO_BACKWARD, rp);
392 * Log_setline --
393 * Reset the line to its original appearance.
395 * XXX
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 *));
404 log_setline(SCR *sp)
406 EXF *ep;
407 LMARK lm;
408 MARK m;
409 db_recno_t lno;
410 u_char *p;
411 size_t size;
413 ep = sp->ep;
414 if (F_ISSET(ep, F_NOLOG)) {
415 msgq(sp, M_ERR,
416 "012|Logging not being performed, undo not possible");
417 return (1);
420 if (log_compare(&ep->lsn_cur, &ep->lsn_first) <= 0) {
421 msgq(sp, M_BERR, "011|No changes to undo");
422 return (1);
424 return __vi_log_traverse(sp, UNDO_SETLINE, &m);
428 * Log_forward --
429 * Roll the log forward one operation.
431 * PUBLIC: int log_forward __P((SCR *, MARK *));
434 log_forward(SCR *sp, MARK *rp)
436 EXF *ep;
437 LMARK lm;
438 MARK m;
439 db_recno_t lno;
440 int didop;
441 u_char *p;
442 size_t size;
444 ep = sp->ep;
445 if (F_ISSET(ep, F_NOLOG)) {
446 msgq(sp, M_ERR,
447 "013|Logging not being performed, roll-forward not possible");
448 return (1);
451 if (log_compare(&ep->lsn_cur, &ep->lsn_high) >= 0) {
452 msgq(sp, M_BERR, "014|No changes to re-do");
453 return (1);
455 return __vi_log_traverse(sp, UNDO_FORWARD, rp);