Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / log4.c
blob4388d463775894af902958b38fc7da39b5d7b93c
1 /* $NetBSD$ */
3 /*-
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.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: log4.c,v 10.3 2002/06/08 21:00:33 skimo Exp";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "common.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
37 * LOG_CURSOR_END 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 *
44 * LOG_MARK LMARK
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
59 * similar fashion.
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
65 * behaved that way.
68 static int log_cursor1 __P((SCR *, int));
71 * log_init --
72 * Initialize the logging subsystem.
74 * PUBLIC: int log_init __P((SCR *, EXF *));
76 int
77 log_init(SCR *sp, EXF *ep)
79 DB_LOGC *logc;
80 DBT data;
81 size_t nlen;
84 * !!!
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.
91 sp->wp->l_lp = NULL;
92 sp->wp->l_len = 0;
93 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
94 ep->l_cursor.cno = 0;
95 ep->l_high = ep->l_cur = 1;
97 if ((sp->db_error = ep->env->log_cursor(ep->env, &logc, 0))
98 != 0) {
99 msgq(sp, M_DBERR, "env->log_cursor");
100 F_SET(ep, F_NOLOG);
101 return (1);
103 nlen = 1024;
104 retry:
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))) {
112 case ENOMEM:
113 nlen = data.size;
114 goto retry;
115 default:
116 alloc_err:
117 msgq(sp, M_DBERR, "logc->get");
118 F_SET(ep, F_NOLOG);
119 return (1);
120 case 0:
123 MEMCPY(&ep->lsn_cur, &ep->lsn_first, 1);
124 MEMCPY(&ep->lsn_high, &ep->lsn_first, 1);
125 logc->close(logc, 0);
127 ep->l_win = NULL;
128 /*LOCK_INIT(sp->wp, ep);*/
130 return (0);
134 * log_end --
135 * Close the logging subsystem.
137 * PUBLIC: int log_end __P((SCR *, EXF *));
140 log_end(SCR *sp, EXF *ep)
143 * !!!
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) {
148 free(sp->wp->l_lp);
149 sp->wp->l_lp = NULL;
151 sp->wp->l_len = 0;
152 ep->l_cursor.lno = 1; /* XXX Any valid recno. */
153 ep->l_cursor.cno = 0;
154 ep->l_high = ep->l_cur = 1;
155 return (0);
159 * log_cursor --
160 * Log the current cursor position, starting an event.
162 * PUBLIC: int log_cursor __P((SCR *));
165 log_cursor(SCR *sp)
167 EXF *ep;
169 ep = sp->ep;
170 if (F_ISSET(ep, F_NOLOG))
171 return (0);
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)
179 return 0;
180 ep->l_cursor.lno = sp->lno;
181 ep->l_cursor.cno = sp->cno;
182 ep->l_win = NULL;
183 return (log_cursor1(sp, LOG_CURSOR_END));
185 ep->l_cursor.lno = sp->lno;
186 ep->l_cursor.cno = sp->cno;
187 return (0);
191 * log_cursor1 --
192 * Actually push a cursor record out.
194 static int
195 log_cursor1(SCR *sp, int type)
197 DBT data, key;
198 EXF *ep;
200 ep = sp->ep;
203 if (type == LOG_CURSOR_INIT &&
204 LOCK_TRY(sp->wp, ep))
205 return 1;
208 if (type == LOG_CURSOR_INIT &&
209 (sp->db_error = __vi_log_truncate(ep)) != 0) {
210 msgq(sp, M_DBERR, "truncate");
211 return 1;
213 if ((sp->db_error =
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");
217 return 1;
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",
228 sp->lno, sp->cno);
229 #endif
230 /* Reset high water mark. */
231 ep->l_high = ++ep->l_cur;
234 if (type == LOG_CURSOR_END)
235 LOCK_UNLOCK(sp->wp, ep);
237 return (0);
241 * log_line --
242 * Log a line change.
244 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
247 log_line(SCR *sp, db_recno_t lno, u_int action)
249 DBT data, key;
250 EXF *ep;
251 size_t len;
252 CHAR_T *lp;
253 db_recno_t lcur;
255 ep = sp->ep;
256 if (F_ISSET(ep, F_NOLOG))
257 return (0);
260 * XXX
262 * Kluge for vi. Clear the EXF undo flag so that the
263 * next 'u' command does a roll-back, regardless.
265 F_CLR(ep, F_UNDO);
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))
270 return (1);
271 ep->l_cursor.lno = OOBLNO;
272 ep->l_win = sp->wp;
273 } /*else if (ep->l_win != sp->wp) {
274 printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
275 return 1;
278 if ((sp->db_error =
279 __vi_change_log(ep->env, NULL, &ep->lsn_cur, 0, action,
280 lno)) != 0) {
281 msgq(sp, M_DBERR, "change_log");
282 return 1;
285 #if defined(DEBUG) && 0
286 switch (action) {
287 case LOG_LINE_APPEND_F:
288 vtrace(sp, "%u: log_line: append_f: %lu {%u}\n",
289 ep->l_cur, lno, len);
290 break;
291 case LOG_LINE_APPEND_B:
292 vtrace(sp, "%u: log_line: append_b: %lu {%u}\n",
293 ep->l_cur, lno, len);
294 break;
295 case LOG_LINE_DELETE_F:
296 vtrace(sp, "%lu: log_line: delete_f: %lu {%u}\n",
297 ep->l_cur, lno, len);
298 break;
299 case LOG_LINE_DELETE_B:
300 vtrace(sp, "%lu: log_line: delete_b: %lu {%u}\n",
301 ep->l_cur, lno, len);
302 break;
303 case LOG_LINE_RESET_F:
304 vtrace(sp, "%lu: log_line: reset_f: %lu {%u}\n",
305 ep->l_cur, lno, len);
306 break;
307 case LOG_LINE_RESET_B:
308 vtrace(sp, "%lu: log_line: reset_b: %lu {%u}\n",
309 ep->l_cur, lno, len);
310 break;
312 #endif
313 /* Reset high water mark. */
314 ep->l_high = ++ep->l_cur;
316 return (0);
320 * log_mark --
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)
331 DBT data, key;
332 EXF *ep;
334 ep = sp->ep;
335 if (F_ISSET(ep, F_NOLOG))
336 return (0);
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))
341 return (1);
342 ep->l_cursor.lno = OOBLNO;
343 ep->l_win = sp->wp;
346 if ((sp->db_error =
347 __vi_mark_log(ep->env, NULL, &ep->lsn_cur, 0,
348 lmp)) != 0) {
349 msgq(sp, M_DBERR, "cursor_log");
350 return 1;
353 #if defined(DEBUG) && 0
354 vtrace(sp, "%lu: mark %c: %lu/%u\n",
355 ep->l_cur, lmp->name, lmp->lno, lmp->cno);
356 #endif
357 /* Reset high water mark. */
358 ep->l_high = ++ep->l_cur;
359 return (0);
363 * Log_backward --
364 * Roll the log backward one operation.
366 * PUBLIC: int log_backward __P((SCR *, MARK *));
369 log_backward(SCR *sp, MARK *rp)
371 EXF *ep;
372 LMARK lm;
373 MARK m;
374 db_recno_t lno;
375 int didop;
376 u_char *p;
377 size_t size;
379 ep = sp->ep;
380 if (F_ISSET(ep, F_NOLOG)) {
381 msgq(sp, M_ERR,
382 "010|Logging not being performed, undo not possible");
383 return (1);
386 if (log_compare(&ep->lsn_cur, &ep->lsn_first) <= 0) {
387 msgq(sp, M_BERR, "011|No changes to undo");
388 return (1);
390 return __vi_log_traverse(sp, UNDO_BACKWARD, rp);
394 * Log_setline --
395 * Reset the line to its original appearance.
397 * XXX
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 *));
406 log_setline(SCR *sp)
408 EXF *ep;
409 LMARK lm;
410 MARK m;
411 db_recno_t lno;
412 u_char *p;
413 size_t size;
415 ep = sp->ep;
416 if (F_ISSET(ep, F_NOLOG)) {
417 msgq(sp, M_ERR,
418 "012|Logging not being performed, undo not possible");
419 return (1);
422 if (log_compare(&ep->lsn_cur, &ep->lsn_first) <= 0) {
423 msgq(sp, M_BERR, "011|No changes to undo");
424 return (1);
426 return __vi_log_traverse(sp, UNDO_SETLINE, &m);
430 * Log_forward --
431 * Roll the log forward one operation.
433 * PUBLIC: int log_forward __P((SCR *, MARK *));
436 log_forward(SCR *sp, MARK *rp)
438 EXF *ep;
439 LMARK lm;
440 MARK m;
441 db_recno_t lno;
442 int didop;
443 u_char *p;
444 size_t size;
446 ep = sp->ep;
447 if (F_ISSET(ep, F_NOLOG)) {
448 msgq(sp, M_ERR,
449 "013|Logging not being performed, roll-forward not possible");
450 return (1);
453 if (log_compare(&ep->lsn_cur, &ep->lsn_high) >= 0) {
454 msgq(sp, M_BERR, "014|No changes to re-do");
455 return (1);
457 return __vi_log_traverse(sp, UNDO_FORWARD, rp);