log.c revision 1.1.1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1992, 1993, 1994
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos static const char sccsid[] = "Id: log.c,v 10.26 2002/03/02 23:12:13 skimo Exp (Berkeley) Date: 2002/03/02 23:12:13 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos #include <sys/stat.h>
19 1.1 christos
20 1.1 christos #include <bitstring.h>
21 1.1 christos #include <errno.h>
22 1.1 christos #include <fcntl.h>
23 1.1 christos #include <limits.h>
24 1.1 christos #include <stdio.h>
25 1.1 christos #include <stdlib.h>
26 1.1 christos #include <string.h>
27 1.1 christos
28 1.1 christos #include "common.h"
29 1.1 christos
30 1.1 christos /*
31 1.1 christos * The log consists of records, each containing a type byte and a variable
32 1.1 christos * length byte string, as follows:
33 1.1 christos *
34 1.1 christos * LOG_CURSOR_INIT MARK
35 1.1 christos * LOG_CURSOR_END MARK
36 1.1 christos * LOG_LINE_APPEND_F db_recno_t char *
37 1.1 christos * LOG_LINE_APPEND_B db_recno_t char *
38 1.1 christos * LOG_LINE_DELETE_F db_recno_t char *
39 1.1 christos * LOG_LINE_DELETE_B db_recno_t char *
40 1.1 christos * LOG_LINE_RESET_F db_recno_t char *
41 1.1 christos * LOG_LINE_RESET_B db_recno_t char *
42 1.1 christos * LOG_MARK LMARK
43 1.1 christos *
44 1.1 christos * We do before image physical logging. This means that the editor layer
45 1.1 christos * MAY NOT modify records in place, even if simply deleting or overwriting
46 1.1 christos * characters. Since the smallest unit of logging is a line, we're using
47 1.1 christos * up lots of space. This may eventually have to be reduced, probably by
48 1.1 christos * doing logical logging, which is a much cooler database phrase.
49 1.1 christos *
50 1.1 christos * The implementation of the historic vi 'u' command, using roll-forward and
51 1.1 christos * roll-back, is simple. Each set of changes has a LOG_CURSOR_INIT record,
52 1.1 christos * followed by a number of other records, followed by a LOG_CURSOR_END record.
53 1.1 christos * LOG_LINE_RESET records come in pairs. The first is a LOG_LINE_RESET_B
54 1.1 christos * record, and is the line before the change. The second is LOG_LINE_RESET_F,
55 1.1 christos * and is the line after the change. Roll-back is done by backing up to the
56 1.1 christos * first LOG_CURSOR_INIT record before a change. Roll-forward is done in a
57 1.1 christos * similar fashion.
58 1.1 christos *
59 1.1 christos * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
60 1.1 christos * record for a line different from the current one. It should be noted that
61 1.1 christos * this means that a subsequent 'u' command will make a change based on the
62 1.1 christos * new position of the log's cursor. This is okay, and, in fact, historic vi
63 1.1 christos * behaved that way.
64 1.1 christos */
65 1.1 christos
66 1.1 christos static int vi_log_get __P((SCR *sp, db_recno_t *lnop, size_t *size));
67 1.1 christos static int log_cursor1 __P((SCR *, int));
68 1.1 christos static void log_err __P((SCR *, char *, int));
69 1.1 christos #if defined(DEBUG) && 0
70 1.1 christos static void log_trace __P((SCR *, char *, db_recno_t, u_char *));
71 1.1 christos #endif
72 1.1 christos
73 1.1 christos /* Try and restart the log on failure, i.e. if we run out of memory. */
74 1.1 christos #define LOG_ERR { \
75 1.1 christos log_err(sp, __FILE__, __LINE__); \
76 1.1 christos return (1); \
77 1.1 christos }
78 1.1 christos
79 1.1 christos /* offset of CHAR_T string in log needs to be aligned on some systems
80 1.1 christos * because it is passed to db_set as a string
81 1.1 christos */
82 1.1 christos typedef struct {
83 1.1 christos char data[sizeof(u_char) /* type */ + sizeof(db_recno_t)];
84 1.1 christos CHAR_T str[1];
85 1.1 christos } log_t;
86 1.1 christos #define CHAR_T_OFFSET ((char *)(((log_t*)0)->str) - (char *)0)
87 1.1 christos
88 1.1 christos /*
89 1.1 christos * log_init --
90 1.1 christos * Initialize the logging subsystem.
91 1.1 christos *
92 1.1 christos * PUBLIC: int log_init __P((SCR *, EXF *));
93 1.1 christos */
94 1.1 christos int
95 1.1 christos log_init(SCR *sp, EXF *ep)
96 1.1 christos {
97 1.1 christos /*
98 1.1 christos * !!!
99 1.1 christos * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
100 1.1 christos *
101 1.1 christos * Initialize the buffer. The logging subsystem has its own
102 1.1 christos * buffers because the global ones are almost by definition
103 1.1 christos * going to be in use when the log runs.
104 1.1 christos */
105 1.1 christos sp->wp->l_lp = NULL;
106 1.1 christos sp->wp->l_len = 0;
107 1.1 christos ep->l_cursor.lno = 1; /* XXX Any valid recno. */
108 1.1 christos ep->l_cursor.cno = 0;
109 1.1 christos ep->l_high = ep->l_cur = 1;
110 1.1 christos
111 1.1 christos if (db_create(&ep->log, 0, 0) != 0 ||
112 1.1 christos db_open(ep->log, NULL, DB_RECNO,
113 1.1 christos DB_CREATE | VI_DB_THREAD, S_IRUSR | S_IWUSR) != 0) {
114 1.1 christos msgq(sp, M_SYSERR, "009|Log file");
115 1.1 christos F_SET(ep, F_NOLOG);
116 1.1 christos return (1);
117 1.1 christos }
118 1.1 christos
119 1.1 christos ep->l_win = NULL;
120 1.1 christos /*LOCK_INIT(sp->wp, ep);*/
121 1.1 christos
122 1.1 christos return (0);
123 1.1 christos }
124 1.1 christos
125 1.1 christos /*
126 1.1 christos * log_end --
127 1.1 christos * Close the logging subsystem.
128 1.1 christos *
129 1.1 christos * PUBLIC: int log_end __P((SCR *, EXF *));
130 1.1 christos */
131 1.1 christos int
132 1.1 christos log_end(SCR *sp, EXF *ep)
133 1.1 christos {
134 1.1 christos /*
135 1.1 christos * !!!
136 1.1 christos * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
137 1.1 christos */
138 1.1 christos /*LOCK_END(sp->wp, ep);*/
139 1.1 christos if (ep->log != NULL) {
140 1.1 christos (void)(ep->log->close)(ep->log, DB_NOSYNC);
141 1.1 christos ep->log = NULL;
142 1.1 christos }
143 1.1 christos if (sp->wp->l_lp != NULL) {
144 1.1 christos free(sp->wp->l_lp);
145 1.1 christos sp->wp->l_lp = NULL;
146 1.1 christos }
147 1.1 christos sp->wp->l_len = 0;
148 1.1 christos ep->l_cursor.lno = 1; /* XXX Any valid recno. */
149 1.1 christos ep->l_cursor.cno = 0;
150 1.1 christos ep->l_high = ep->l_cur = 1;
151 1.1 christos return (0);
152 1.1 christos }
153 1.1 christos
154 1.1 christos /*
155 1.1 christos * log_cursor --
156 1.1 christos * Log the current cursor position, starting an event.
157 1.1 christos *
158 1.1 christos * PUBLIC: int log_cursor __P((SCR *));
159 1.1 christos */
160 1.1 christos int
161 1.1 christos log_cursor(SCR *sp)
162 1.1 christos {
163 1.1 christos EXF *ep;
164 1.1 christos
165 1.1 christos ep = sp->ep;
166 1.1 christos if (F_ISSET(ep, F_NOLOG))
167 1.1 christos return (0);
168 1.1 christos
169 1.1 christos /*
170 1.1 christos * If any changes were made since the last cursor init,
171 1.1 christos * put out the ending cursor record.
172 1.1 christos */
173 1.1 christos if (ep->l_cursor.lno == OOBLNO) {
174 1.1 christos if (ep->l_win && ep->l_win != sp->wp)
175 1.1 christos return 0;
176 1.1 christos ep->l_cursor.lno = sp->lno;
177 1.1 christos ep->l_cursor.cno = sp->cno;
178 1.1 christos ep->l_win = NULL;
179 1.1 christos return (log_cursor1(sp, LOG_CURSOR_END));
180 1.1 christos }
181 1.1 christos ep->l_cursor.lno = sp->lno;
182 1.1 christos ep->l_cursor.cno = sp->cno;
183 1.1 christos return (0);
184 1.1 christos }
185 1.1 christos
186 1.1 christos /*
187 1.1 christos * log_cursor1 --
188 1.1 christos * Actually push a cursor record out.
189 1.1 christos */
190 1.1 christos static int
191 1.1 christos log_cursor1(SCR *sp, int type)
192 1.1 christos {
193 1.1 christos DBT data, key;
194 1.1 christos EXF *ep;
195 1.1 christos
196 1.1 christos ep = sp->ep;
197 1.1 christos
198 1.1 christos /*
199 1.1 christos if (type == LOG_CURSOR_INIT &&
200 1.1 christos LOCK_TRY(sp->wp, ep))
201 1.1 christos return 1;
202 1.1 christos */
203 1.1 christos
204 1.1 christos BINC_RETC(sp, sp->wp->l_lp, sp->wp->l_len, sizeof(u_char) + sizeof(MARK));
205 1.1 christos sp->wp->l_lp[0] = type;
206 1.1 christos memmove(sp->wp->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));
207 1.1 christos
208 1.1 christos memset(&key, 0, sizeof(key));
209 1.1 christos key.data = &ep->l_cur;
210 1.1 christos key.size = sizeof(db_recno_t);
211 1.1 christos memset(&data, 0, sizeof(data));
212 1.1 christos data.data = sp->wp->l_lp;
213 1.1 christos data.size = sizeof(u_char) + sizeof(MARK);
214 1.1 christos if (ep->log->put(ep->log, NULL, &key, &data, 0) == -1)
215 1.1 christos LOG_ERR;
216 1.1 christos
217 1.1 christos #if defined(DEBUG) && 0
218 1.1 christos vtrace(sp, "%lu: %s: %u/%u\n", ep->l_cur,
219 1.1 christos type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
220 1.1 christos sp->lno, sp->cno);
221 1.1 christos #endif
222 1.1 christos /* Reset high water mark. */
223 1.1 christos ep->l_high = ++ep->l_cur;
224 1.1 christos
225 1.1 christos /*
226 1.1 christos if (type == LOG_CURSOR_END)
227 1.1 christos LOCK_UNLOCK(sp->wp, ep);
228 1.1 christos */
229 1.1 christos return (0);
230 1.1 christos }
231 1.1 christos
232 1.1 christos /*
233 1.1 christos * log_line --
234 1.1 christos * Log a line change.
235 1.1 christos *
236 1.1 christos * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
237 1.1 christos */
238 1.1 christos int
239 1.1 christos log_line(SCR *sp, db_recno_t lno, u_int action)
240 1.1 christos {
241 1.1 christos DBT data, key;
242 1.1 christos EXF *ep;
243 1.1 christos size_t len;
244 1.1 christos CHAR_T *lp;
245 1.1 christos db_recno_t lcur;
246 1.1 christos
247 1.1 christos ep = sp->ep;
248 1.1 christos if (F_ISSET(ep, F_NOLOG))
249 1.1 christos return (0);
250 1.1 christos
251 1.1 christos /*
252 1.1 christos * XXX
253 1.1 christos *
254 1.1 christos * Kluge for vi. Clear the EXF undo flag so that the
255 1.1 christos * next 'u' command does a roll-back, regardless.
256 1.1 christos */
257 1.1 christos F_CLR(ep, F_UNDO);
258 1.1 christos
259 1.1 christos /* Put out one initial cursor record per set of changes. */
260 1.1 christos if (ep->l_cursor.lno != OOBLNO) {
261 1.1 christos if (log_cursor1(sp, LOG_CURSOR_INIT))
262 1.1 christos return (1);
263 1.1 christos ep->l_cursor.lno = OOBLNO;
264 1.1 christos ep->l_win = sp->wp;
265 1.1 christos } /*else if (ep->l_win != sp->wp) {
266 1.1 christos printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
267 1.1 christos return 1;
268 1.1 christos }*/
269 1.1 christos
270 1.1 christos switch (action) {
271 1.1 christos /* newly added for DB4 logging */
272 1.1 christos case LOG_LINE_APPEND_B:
273 1.1 christos case LOG_LINE_DELETE_F:
274 1.1 christos return 0;
275 1.1 christos }
276 1.1 christos
277 1.1 christos /*
278 1.1 christos * Put out the changes. If it's a LOG_LINE_RESET_B call, it's a
279 1.1 christos * special case, avoid the caches. Also, if it fails and it's
280 1.1 christos * line 1, it just means that the user started with an empty file,
281 1.1 christos * so fake an empty length line.
282 1.1 christos */
283 1.1 christos if (action == LOG_LINE_RESET_B) {
284 1.1 christos if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
285 1.1 christos static CHAR_T nul = 0;
286 1.1 christos if (lno != 1) {
287 1.1 christos db_err(sp, lno);
288 1.1 christos return (1);
289 1.1 christos }
290 1.1 christos len = 0;
291 1.1 christos lp = &nul;
292 1.1 christos }
293 1.1 christos } else
294 1.1 christos if (db_get(sp, lno, DBG_FATAL, &lp, &len))
295 1.1 christos return (1);
296 1.1 christos BINC_RETC(sp,
297 1.1 christos sp->wp->l_lp, sp->wp->l_len,
298 1.1 christos len * sizeof(CHAR_T) + CHAR_T_OFFSET);
299 1.1 christos sp->wp->l_lp[0] = action;
300 1.1 christos memmove(sp->wp->l_lp + sizeof(u_char), &lno, sizeof(db_recno_t));
301 1.1 christos MEMMOVEW(sp->wp->l_lp + CHAR_T_OFFSET, lp, len);
302 1.1 christos
303 1.1 christos lcur = ep->l_cur;
304 1.1 christos memset(&key, 0, sizeof(key));
305 1.1 christos key.data = &lcur;
306 1.1 christos key.size = sizeof(db_recno_t);
307 1.1 christos memset(&data, 0, sizeof(data));
308 1.1 christos data.data = sp->wp->l_lp;
309 1.1 christos data.size = len * sizeof(CHAR_T) + CHAR_T_OFFSET;
310 1.1 christos if (ep->log->put(ep->log, NULL, &key, &data, 0) == -1)
311 1.1 christos LOG_ERR;
312 1.1 christos
313 1.1 christos #if defined(DEBUG) && 0
314 1.1 christos switch (action) {
315 1.1 christos case LOG_LINE_APPEND_F:
316 1.1 christos vtrace(sp, "%u: log_line: append_f: %lu {%u}\n",
317 1.1 christos ep->l_cur, lno, len);
318 1.1 christos break;
319 1.1 christos case LOG_LINE_APPEND_B:
320 1.1 christos vtrace(sp, "%u: log_line: append_b: %lu {%u}\n",
321 1.1 christos ep->l_cur, lno, len);
322 1.1 christos break;
323 1.1 christos case LOG_LINE_DELETE_F:
324 1.1 christos vtrace(sp, "%lu: log_line: delete_f: %lu {%u}\n",
325 1.1 christos ep->l_cur, lno, len);
326 1.1 christos break;
327 1.1 christos case LOG_LINE_DELETE_B:
328 1.1 christos vtrace(sp, "%lu: log_line: delete_b: %lu {%u}\n",
329 1.1 christos ep->l_cur, lno, len);
330 1.1 christos break;
331 1.1 christos case LOG_LINE_RESET_F:
332 1.1 christos vtrace(sp, "%lu: log_line: reset_f: %lu {%u}\n",
333 1.1 christos ep->l_cur, lno, len);
334 1.1 christos break;
335 1.1 christos case LOG_LINE_RESET_B:
336 1.1 christos vtrace(sp, "%lu: log_line: reset_b: %lu {%u}\n",
337 1.1 christos ep->l_cur, lno, len);
338 1.1 christos break;
339 1.1 christos }
340 1.1 christos #endif
341 1.1 christos /* Reset high water mark. */
342 1.1 christos ep->l_high = ++ep->l_cur;
343 1.1 christos
344 1.1 christos return (0);
345 1.1 christos }
346 1.1 christos
347 1.1 christos /*
348 1.1 christos * log_mark --
349 1.1 christos * Log a mark position. For the log to work, we assume that there
350 1.1 christos * aren't any operations that just put out a log record -- this
351 1.1 christos * would mean that undo operations would only reset marks, and not
352 1.1 christos * cause any other change.
353 1.1 christos *
354 1.1 christos * PUBLIC: int log_mark __P((SCR *, LMARK *));
355 1.1 christos */
356 1.1 christos int
357 1.1 christos log_mark(SCR *sp, LMARK *lmp)
358 1.1 christos {
359 1.1 christos DBT data, key;
360 1.1 christos EXF *ep;
361 1.1 christos
362 1.1 christos ep = sp->ep;
363 1.1 christos if (F_ISSET(ep, F_NOLOG))
364 1.1 christos return (0);
365 1.1 christos
366 1.1 christos /* Put out one initial cursor record per set of changes. */
367 1.1 christos if (ep->l_cursor.lno != OOBLNO) {
368 1.1 christos if (log_cursor1(sp, LOG_CURSOR_INIT))
369 1.1 christos return (1);
370 1.1 christos ep->l_cursor.lno = OOBLNO;
371 1.1 christos ep->l_win = sp->wp;
372 1.1 christos }
373 1.1 christos
374 1.1 christos BINC_RETC(sp, sp->wp->l_lp,
375 1.1 christos sp->wp->l_len, sizeof(u_char) + sizeof(LMARK));
376 1.1 christos sp->wp->l_lp[0] = LOG_MARK;
377 1.1 christos memmove(sp->wp->l_lp + sizeof(u_char), lmp, sizeof(LMARK));
378 1.1 christos
379 1.1 christos memset(&key, 0, sizeof(key));
380 1.1 christos key.data = &ep->l_cur;
381 1.1 christos key.size = sizeof(db_recno_t);
382 1.1 christos memset(&data, 0, sizeof(data));
383 1.1 christos data.data = sp->wp->l_lp;
384 1.1 christos data.size = sizeof(u_char) + sizeof(LMARK);
385 1.1 christos if (ep->log->put(ep->log, NULL, &key, &data, 0) == -1)
386 1.1 christos LOG_ERR;
387 1.1 christos
388 1.1 christos #if defined(DEBUG) && 0
389 1.1 christos vtrace(sp, "%lu: mark %c: %lu/%u\n",
390 1.1 christos ep->l_cur, lmp->name, lmp->lno, lmp->cno);
391 1.1 christos #endif
392 1.1 christos /* Reset high water mark. */
393 1.1 christos ep->l_high = ++ep->l_cur;
394 1.1 christos return (0);
395 1.1 christos }
396 1.1 christos
397 1.1 christos /*
398 1.1 christos * vi_log_get --
399 1.1 christos * Get a line from the log in log buffer.
400 1.1 christos */
401 1.1 christos static int
402 1.1 christos vi_log_get(SCR *sp, db_recno_t *lnop, size_t *size)
403 1.1 christos {
404 1.1 christos DBT key, data;
405 1.1 christos size_t nlen;
406 1.1 christos EXF *ep;
407 1.1 christos
408 1.1 christos ep = sp->ep;
409 1.1 christos
410 1.1 christos nlen = 1024;
411 1.1 christos retry:
412 1.1 christos BINC_RETC(sp, sp->wp->l_lp, sp->wp->l_len, nlen);
413 1.1 christos
414 1.1 christos memset(&key, 0, sizeof(key));
415 1.1 christos key.data = lnop; /* Initialize db request. */
416 1.1 christos key.size = sizeof(db_recno_t);
417 1.1 christos memset(&data, 0, sizeof(data));
418 1.1 christos data.data = sp->wp->l_lp;
419 1.1 christos data.ulen = sp->wp->l_len;
420 1.1 christos data.flags = DB_DBT_USERMEM;
421 1.1 christos switch (ep->log->get(ep->log, NULL, &key, &data, 0)) {
422 1.1 christos case ENOMEM:
423 1.1 christos nlen = data.size;
424 1.1 christos goto retry;
425 1.1 christos case 0:
426 1.1 christos *size = data.size;
427 1.1 christos return 0;
428 1.1 christos default:
429 1.1 christos return 1;
430 1.1 christos }
431 1.1 christos }
432 1.1 christos
433 1.1 christos /*
434 1.1 christos * Log_backward --
435 1.1 christos * Roll the log backward one operation.
436 1.1 christos *
437 1.1 christos * PUBLIC: int log_backward __P((SCR *, MARK *));
438 1.1 christos */
439 1.1 christos int
440 1.1 christos log_backward(SCR *sp, MARK *rp)
441 1.1 christos {
442 1.1 christos EXF *ep;
443 1.1 christos LMARK lm;
444 1.1 christos MARK m;
445 1.1 christos db_recno_t lno;
446 1.1 christos int didop;
447 1.1 christos u_char *p;
448 1.1 christos size_t size;
449 1.1 christos
450 1.1 christos ep = sp->ep;
451 1.1 christos if (F_ISSET(ep, F_NOLOG)) {
452 1.1 christos msgq(sp, M_ERR,
453 1.1 christos "010|Logging not being performed, undo not possible");
454 1.1 christos return (1);
455 1.1 christos }
456 1.1 christos
457 1.1 christos if (ep->l_cur == 1) {
458 1.1 christos msgq(sp, M_BERR, "011|No changes to undo");
459 1.1 christos return (1);
460 1.1 christos }
461 1.1 christos
462 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
463 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
464 1.1 christos return 1;
465 1.1 christos }
466 1.1 christos ep->l_win = sp->wp;
467 1.1 christos
468 1.1 christos
469 1.1 christos F_SET(ep, F_NOLOG); /* Turn off logging. */
470 1.1 christos
471 1.1 christos for (didop = 0;;) {
472 1.1 christos --ep->l_cur;
473 1.1 christos if (vi_log_get(sp, &ep->l_cur, &size))
474 1.1 christos LOG_ERR;
475 1.1 christos #if defined(DEBUG) && 0
476 1.1 christos log_trace(sp, "log_backward", ep->l_cur, data.data);
477 1.1 christos #endif
478 1.1 christos switch (*(p = (u_char *)sp->wp->l_lp)) {
479 1.1 christos case LOG_CURSOR_INIT:
480 1.1 christos if (didop) {
481 1.1 christos memmove(rp, p + sizeof(u_char), sizeof(MARK));
482 1.1 christos F_CLR(ep, F_NOLOG);
483 1.1 christos ep->l_win = NULL;
484 1.1 christos return (0);
485 1.1 christos }
486 1.1 christos break;
487 1.1 christos case LOG_CURSOR_END:
488 1.1 christos break;
489 1.1 christos case LOG_LINE_APPEND_F:
490 1.1 christos didop = 1;
491 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
492 1.1 christos if (db_delete(sp, lno))
493 1.1 christos goto err;
494 1.1 christos ++sp->rptlines[L_DELETED];
495 1.1 christos break;
496 1.1 christos case LOG_LINE_DELETE_B:
497 1.1 christos didop = 1;
498 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
499 1.1 christos if (db_insert(sp, lno,
500 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
501 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
502 1.1 christos goto err;
503 1.1 christos ++sp->rptlines[L_ADDED];
504 1.1 christos break;
505 1.1 christos case LOG_LINE_RESET_F:
506 1.1 christos break;
507 1.1 christos case LOG_LINE_RESET_B:
508 1.1 christos didop = 1;
509 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
510 1.1 christos if (db_set(sp, lno,
511 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
512 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
513 1.1 christos goto err;
514 1.1 christos if (sp->rptlchange != lno) {
515 1.1 christos sp->rptlchange = lno;
516 1.1 christos ++sp->rptlines[L_CHANGED];
517 1.1 christos }
518 1.1 christos break;
519 1.1 christos case LOG_MARK:
520 1.1 christos didop = 1;
521 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
522 1.1 christos m.lno = lm.lno;
523 1.1 christos m.cno = lm.cno;
524 1.1 christos if (mark_set(sp, lm.name, &m, 0))
525 1.1 christos goto err;
526 1.1 christos break;
527 1.1 christos default:
528 1.1 christos abort();
529 1.1 christos }
530 1.1 christos }
531 1.1 christos
532 1.1 christos err: F_CLR(ep, F_NOLOG);
533 1.1 christos ep->l_win = NULL;
534 1.1 christos return (1);
535 1.1 christos }
536 1.1 christos
537 1.1 christos /*
538 1.1 christos * Log_setline --
539 1.1 christos * Reset the line to its original appearance.
540 1.1 christos *
541 1.1 christos * XXX
542 1.1 christos * There's a bug in this code due to our not logging cursor movements
543 1.1 christos * unless a change was made. If you do a change, move off the line,
544 1.1 christos * then move back on and do a 'U', the line will be restored to the way
545 1.1 christos * it was before the original change.
546 1.1 christos *
547 1.1 christos * PUBLIC: int log_setline __P((SCR *));
548 1.1 christos */
549 1.1 christos int
550 1.1 christos log_setline(SCR *sp)
551 1.1 christos {
552 1.1 christos EXF *ep;
553 1.1 christos LMARK lm;
554 1.1 christos MARK m;
555 1.1 christos db_recno_t lno;
556 1.1 christos u_char *p;
557 1.1 christos size_t size;
558 1.1 christos
559 1.1 christos ep = sp->ep;
560 1.1 christos if (F_ISSET(ep, F_NOLOG)) {
561 1.1 christos msgq(sp, M_ERR,
562 1.1 christos "012|Logging not being performed, undo not possible");
563 1.1 christos return (1);
564 1.1 christos }
565 1.1 christos
566 1.1 christos if (ep->l_cur == 1)
567 1.1 christos return (1);
568 1.1 christos
569 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
570 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
571 1.1 christos return 1;
572 1.1 christos }
573 1.1 christos ep->l_win = sp->wp;
574 1.1 christos
575 1.1 christos F_SET(ep, F_NOLOG); /* Turn off logging. */
576 1.1 christos
577 1.1 christos for (;;) {
578 1.1 christos --ep->l_cur;
579 1.1 christos if (vi_log_get(sp, &ep->l_cur, &size))
580 1.1 christos LOG_ERR;
581 1.1 christos #if defined(DEBUG) && 0
582 1.1 christos log_trace(sp, "log_setline", ep->l_cur, data.data);
583 1.1 christos #endif
584 1.1 christos switch (*(p = (u_char *)sp->wp->l_lp)) {
585 1.1 christos case LOG_CURSOR_INIT:
586 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
587 1.1 christos if (m.lno != sp->lno || ep->l_cur == 1) {
588 1.1 christos F_CLR(ep, F_NOLOG);
589 1.1 christos ep->l_win = NULL;
590 1.1 christos return (0);
591 1.1 christos }
592 1.1 christos break;
593 1.1 christos case LOG_CURSOR_END:
594 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
595 1.1 christos if (m.lno != sp->lno) {
596 1.1 christos ++ep->l_cur;
597 1.1 christos F_CLR(ep, F_NOLOG);
598 1.1 christos ep->l_win = NULL;
599 1.1 christos return (0);
600 1.1 christos }
601 1.1 christos break;
602 1.1 christos case LOG_LINE_APPEND_F:
603 1.1 christos case LOG_LINE_DELETE_B:
604 1.1 christos case LOG_LINE_RESET_F:
605 1.1 christos break;
606 1.1 christos case LOG_LINE_RESET_B:
607 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
608 1.1 christos if (lno == sp->lno &&
609 1.1 christos db_set(sp, lno, (CHAR_T *)(p + CHAR_T_OFFSET),
610 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
611 1.1 christos goto err;
612 1.1 christos if (sp->rptlchange != lno) {
613 1.1 christos sp->rptlchange = lno;
614 1.1 christos ++sp->rptlines[L_CHANGED];
615 1.1 christos }
616 1.1 christos case LOG_MARK:
617 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
618 1.1 christos m.lno = lm.lno;
619 1.1 christos m.cno = lm.cno;
620 1.1 christos if (mark_set(sp, lm.name, &m, 0))
621 1.1 christos goto err;
622 1.1 christos break;
623 1.1 christos default:
624 1.1 christos abort();
625 1.1 christos }
626 1.1 christos }
627 1.1 christos
628 1.1 christos err: F_CLR(ep, F_NOLOG);
629 1.1 christos ep->l_win = NULL;
630 1.1 christos return (1);
631 1.1 christos }
632 1.1 christos
633 1.1 christos /*
634 1.1 christos * Log_forward --
635 1.1 christos * Roll the log forward one operation.
636 1.1 christos *
637 1.1 christos * PUBLIC: int log_forward __P((SCR *, MARK *));
638 1.1 christos */
639 1.1 christos int
640 1.1 christos log_forward(SCR *sp, MARK *rp)
641 1.1 christos {
642 1.1 christos EXF *ep;
643 1.1 christos LMARK lm;
644 1.1 christos MARK m;
645 1.1 christos db_recno_t lno;
646 1.1 christos int didop;
647 1.1 christos u_char *p;
648 1.1 christos size_t size;
649 1.1 christos
650 1.1 christos ep = sp->ep;
651 1.1 christos if (F_ISSET(ep, F_NOLOG)) {
652 1.1 christos msgq(sp, M_ERR,
653 1.1 christos "013|Logging not being performed, roll-forward not possible");
654 1.1 christos return (1);
655 1.1 christos }
656 1.1 christos
657 1.1 christos if (ep->l_cur == ep->l_high) {
658 1.1 christos msgq(sp, M_BERR, "014|No changes to re-do");
659 1.1 christos return (1);
660 1.1 christos }
661 1.1 christos
662 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
663 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
664 1.1 christos return 1;
665 1.1 christos }
666 1.1 christos ep->l_win = sp->wp;
667 1.1 christos
668 1.1 christos F_SET(ep, F_NOLOG); /* Turn off logging. */
669 1.1 christos
670 1.1 christos for (didop = 0;;) {
671 1.1 christos ++ep->l_cur;
672 1.1 christos if (vi_log_get(sp, &ep->l_cur, &size))
673 1.1 christos LOG_ERR;
674 1.1 christos #if defined(DEBUG) && 0
675 1.1 christos log_trace(sp, "log_forward", ep->l_cur, data.data);
676 1.1 christos #endif
677 1.1 christos switch (*(p = (u_char *)sp->wp->l_lp)) {
678 1.1 christos case LOG_CURSOR_END:
679 1.1 christos if (didop) {
680 1.1 christos ++ep->l_cur;
681 1.1 christos memmove(rp, p + sizeof(u_char), sizeof(MARK));
682 1.1 christos F_CLR(ep, F_NOLOG);
683 1.1 christos ep->l_win = NULL;
684 1.1 christos return (0);
685 1.1 christos }
686 1.1 christos break;
687 1.1 christos case LOG_CURSOR_INIT:
688 1.1 christos break;
689 1.1 christos case LOG_LINE_APPEND_F:
690 1.1 christos didop = 1;
691 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
692 1.1 christos if (db_insert(sp, lno,
693 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
694 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
695 1.1 christos goto err;
696 1.1 christos ++sp->rptlines[L_ADDED];
697 1.1 christos break;
698 1.1 christos case LOG_LINE_DELETE_B:
699 1.1 christos didop = 1;
700 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
701 1.1 christos if (db_delete(sp, lno))
702 1.1 christos goto err;
703 1.1 christos ++sp->rptlines[L_DELETED];
704 1.1 christos break;
705 1.1 christos case LOG_LINE_RESET_B:
706 1.1 christos break;
707 1.1 christos case LOG_LINE_RESET_F:
708 1.1 christos didop = 1;
709 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
710 1.1 christos if (db_set(sp, lno,
711 1.1 christos (CHAR_T *)(p + CHAR_T_OFFSET),
712 1.1 christos (size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
713 1.1 christos goto err;
714 1.1 christos if (sp->rptlchange != lno) {
715 1.1 christos sp->rptlchange = lno;
716 1.1 christos ++sp->rptlines[L_CHANGED];
717 1.1 christos }
718 1.1 christos break;
719 1.1 christos case LOG_MARK:
720 1.1 christos didop = 1;
721 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
722 1.1 christos m.lno = lm.lno;
723 1.1 christos m.cno = lm.cno;
724 1.1 christos if (mark_set(sp, lm.name, &m, 0))
725 1.1 christos goto err;
726 1.1 christos break;
727 1.1 christos default:
728 1.1 christos abort();
729 1.1 christos }
730 1.1 christos }
731 1.1 christos
732 1.1 christos err: F_CLR(ep, F_NOLOG);
733 1.1 christos ep->l_win = NULL;
734 1.1 christos return (1);
735 1.1 christos }
736 1.1 christos
737 1.1 christos /*
738 1.1 christos * log_err --
739 1.1 christos * Try and restart the log on failure, i.e. if we run out of memory.
740 1.1 christos */
741 1.1 christos static void
742 1.1 christos log_err(SCR *sp, char *file, int line)
743 1.1 christos {
744 1.1 christos EXF *ep;
745 1.1 christos
746 1.1 christos msgq(sp, M_SYSERR, "015|%s/%d: log put error", tail(file), line);
747 1.1 christos ep = sp->ep;
748 1.1 christos (void)ep->log->close(ep->log, DB_NOSYNC);
749 1.1 christos if (!log_init(sp, ep))
750 1.1 christos msgq(sp, M_ERR, "267|Log restarted");
751 1.1 christos }
752 1.1 christos
753 1.1 christos #if defined(DEBUG) && 0
754 1.1 christos static void
755 1.1 christos log_trace(sp, msg, rno, p)
756 1.1 christos SCR *sp;
757 1.1 christos char *msg;
758 1.1 christos db_recno_t rno;
759 1.1 christos u_char *p;
760 1.1 christos {
761 1.1 christos LMARK lm;
762 1.1 christos MARK m;
763 1.1 christos db_recno_t lno;
764 1.1 christos
765 1.1 christos switch (*p) {
766 1.1 christos case LOG_CURSOR_INIT:
767 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
768 1.1 christos vtrace(sp, "%lu: %s: C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
769 1.1 christos break;
770 1.1 christos case LOG_CURSOR_END:
771 1.1 christos memmove(&m, p + sizeof(u_char), sizeof(MARK));
772 1.1 christos vtrace(sp, "%lu: %s: C_END: %u/%u\n", rno, msg, m.lno, m.cno);
773 1.1 christos break;
774 1.1 christos case LOG_LINE_APPEND_F:
775 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
776 1.1 christos vtrace(sp, "%lu: %s: APPEND_F: %lu\n", rno, msg, lno);
777 1.1 christos break;
778 1.1 christos case LOG_LINE_APPEND_B:
779 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
780 1.1 christos vtrace(sp, "%lu: %s: APPEND_B: %lu\n", rno, msg, lno);
781 1.1 christos break;
782 1.1 christos case LOG_LINE_DELETE_F:
783 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
784 1.1 christos vtrace(sp, "%lu: %s: DELETE_F: %lu\n", rno, msg, lno);
785 1.1 christos break;
786 1.1 christos case LOG_LINE_DELETE_B:
787 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
788 1.1 christos vtrace(sp, "%lu: %s: DELETE_B: %lu\n", rno, msg, lno);
789 1.1 christos break;
790 1.1 christos case LOG_LINE_RESET_F:
791 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
792 1.1 christos vtrace(sp, "%lu: %s: RESET_F: %lu\n", rno, msg, lno);
793 1.1 christos break;
794 1.1 christos case LOG_LINE_RESET_B:
795 1.1 christos memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
796 1.1 christos vtrace(sp, "%lu: %s: RESET_B: %lu\n", rno, msg, lno);
797 1.1 christos break;
798 1.1 christos case LOG_MARK:
799 1.1 christos memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
800 1.1 christos vtrace(sp,
801 1.1 christos "%lu: %s: MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
802 1.1 christos break;
803 1.1 christos default:
804 1.1 christos abort();
805 1.1 christos }
806 1.1 christos }
807 1.1 christos #endif
808