Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*	$NetBSD: log1.c,v 1.7 2019/07/24 08:37:59 rin Exp $	*/
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#if 0
#ifndef lint
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 ";
#endif /* not lint */
#else
__RCSID("$NetBSD: log1.c,v 1.7 2019/07/24 08:37:59 rin Exp $");
#endif

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>

#include <bitstring.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

#include "common.h"

/*
 * The log consists of records, each containing a type byte and a variable
 * length byte string, as follows:
 *
 *	LOG_CURSOR_INIT		MARK
 *	LOG_CURSOR_END		MARK
 *	LOG_LINE_APPEND_F 	db_recno_t		char *
 *	LOG_LINE_APPEND_B 	db_recno_t		char *
 *	LOG_LINE_DELETE_F	db_recno_t		char *
 *	LOG_LINE_DELETE_B	db_recno_t		char *
 *	LOG_LINE_RESET_F	db_recno_t		char *
 *	LOG_LINE_RESET_B	db_recno_t		char *
 *	LOG_MARK		LMARK
 *
 * We do before image physical logging.  This means that the editor layer
 * MAY NOT modify records in place, even if simply deleting or overwriting
 * characters.  Since the smallest unit of logging is a line, we're using
 * up lots of space.  This may eventually have to be reduced, probably by
 * doing logical logging, which is a much cooler database phrase.
 *
 * The implementation of the historic vi 'u' command, using roll-forward and
 * roll-back, is simple.  Each set of changes has a LOG_CURSOR_INIT record,
 * followed by a number of other records, followed by a LOG_CURSOR_END record.
 * LOG_LINE_RESET records come in pairs.  The first is a LOG_LINE_RESET_B
 * record, and is the line before the change.  The second is LOG_LINE_RESET_F,
 * and is the line after the change.  Roll-back is done by backing up to the
 * first LOG_CURSOR_INIT record before a change.  Roll-forward is done in a
 * similar fashion.
 *
 * The 'U' command is implemented by rolling backward to a LOG_CURSOR_END
 * record for a line different from the current one.  It should be noted that
 * this means that a subsequent 'u' command will make a change based on the
 * new position of the log's cursor.  This is okay, and, in fact, historic vi
 * behaved that way.
 */

static int	log_cursor1 __P((SCR *, int));
static void	log_err __P((SCR *, const char *, int));
#if defined(LOGDEBUG) && defined(TRACE)
static void	log_trace __P((SCR *, const char *, db_recno_t, u_char *));
#endif

/* Try and restart the log on failure, i.e. if we run out of memory. */
#define	LOG_ERR {							\
	log_err(sp, __FILE__, __LINE__);				\
	return (1);							\
}

/* offset of CHAR_T string in log needs to be aligned on some systems
 * because it is passed to db_set as a string
 */
typedef struct {
    char    data[sizeof(u_char) /* type */ + sizeof(db_recno_t)];
    CHAR_T  str[1];
} log_t;
#define CHAR_T_OFFSET (offsetof(log_t, str))

/*
 * log_init --
 *	Initialize the logging subsystem.
 *
 * PUBLIC: int log_init __P((SCR *, EXF *));
 */
int
log_init(SCR *sp, EXF *ep)
{
	/*
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 *
	 * Initialize the buffer.  The logging subsystem has its own
	 * buffers because the global ones are almost by definition
	 * going to be in use when the log runs.
	 */
	sp->wp->l_lp = NULL;
	sp->wp->l_len = 0;
	ep->l_cursor.lno = 1;		/* XXX Any valid recno. */
	ep->l_cursor.cno = 0;
	ep->l_high = ep->l_cur = 1;

	ep->log = dbopen(NULL, O_CREAT | O_NONBLOCK | O_RDWR,
	    S_IRUSR | S_IWUSR, DB_RECNO, NULL);
	if (ep->log == NULL) {
		msgq(sp, M_SYSERR, "009|Log file");
		F_SET(ep, F_NOLOG);
		return (1);
	}

	ep->l_win = NULL;
	/*LOCK_INIT(sp->wp, ep);*/

	return (0);
}

/*
 * log_end --
 *	Close the logging subsystem.
 *
 * PUBLIC: int log_end __P((SCR *, EXF *));
 */
int
log_end(SCR *sp, EXF *ep)
{
	/*
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 */
	/*LOCK_END(sp->wp, ep);*/
	if (ep->log != NULL) {
		(void)(ep->log->close)(ep->log);
		ep->log = NULL;
	}
	if (sp->wp->l_lp != NULL) {
		free(sp->wp->l_lp);
		sp->wp->l_lp = NULL;
	}
	sp->wp->l_len = 0;
	ep->l_cursor.lno = 1;		/* XXX Any valid recno. */
	ep->l_cursor.cno = 0;
	ep->l_high = ep->l_cur = 1;
	return (0);
}

/*
 * log_cursor --
 *	Log the current cursor position, starting an event.
 *
 * PUBLIC: int log_cursor __P((SCR *));
 */
int
log_cursor(SCR *sp)
{
	EXF *ep;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/*
	 * If any changes were made since the last cursor init,
	 * put out the ending cursor record.
	 */
	if (ep->l_cursor.lno == OOBLNO) {
		if (ep->l_win && ep->l_win != sp->wp)
			return 0;
		ep->l_cursor.lno = sp->lno;
		ep->l_cursor.cno = sp->cno;
		ep->l_win = NULL;
		return (log_cursor1(sp, LOG_CURSOR_END));
	}
	ep->l_cursor.lno = sp->lno;
	ep->l_cursor.cno = sp->cno;
	return (0);
}

/*
 * log_cursor1 --
 *	Actually push a cursor record out.
 */
static int
log_cursor1(SCR *sp, int type)
{
	DBT data, key;
	EXF *ep;

	ep = sp->ep;

	/*
	if (type == LOG_CURSOR_INIT &&
	    LOCK_TRY(sp->wp, ep))
		return 1;
	*/

	BINC_RETC(sp, sp->wp->l_lp, sp->wp->l_len, sizeof(u_char) + sizeof(MARK));
	sp->wp->l_lp[0] = type;
	memmove(sp->wp->l_lp + sizeof(u_char), &ep->l_cursor, sizeof(MARK));

	memset(&key, 0, sizeof(key));
	key.data = &ep->l_cur;
	key.size = sizeof(db_recno_t);
	memset(&data, 0, sizeof(data));
	data.data = sp->wp->l_lp;
	data.size = sizeof(u_char) + sizeof(MARK);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(LOGDEBUG) && defined(TRACE)
	vtrace("%lu: %s: %u/%u\n", ep->l_cur,
	    type == LOG_CURSOR_INIT ? "log_cursor_init" : "log_cursor_end",
	    sp->lno, sp->cno);
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;

	/*
	if (type == LOG_CURSOR_END)
		LOCK_UNLOCK(sp->wp, ep);
	*/
	return (0);
}

/*
 * log_line --
 *	Log a line change.
 *
 * PUBLIC: int log_line __P((SCR *, db_recno_t, u_int));
 */
int
log_line(SCR *sp, db_recno_t lno, u_int action)
{
	DBT data, key;
	EXF *ep;
	size_t len;
	CHAR_T *lp;
	db_recno_t lcur;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/*
	 * XXX
	 *
	 * Kluge for vi.  Clear the EXF undo flag so that the
	 * next 'u' command does a roll-back, regardless.
	 */
	F_CLR(ep, F_UNDO);

	/* Put out one initial cursor record per set of changes. */
	if (ep->l_cursor.lno != OOBLNO) {
		if (log_cursor1(sp, LOG_CURSOR_INIT))
			return (1);
		ep->l_cursor.lno = OOBLNO;
		ep->l_win = sp->wp;
	} /*else if (ep->l_win != sp->wp) {
		printf("log_line own: %p, this: %p\n", ep->l_win, sp->wp);
		return 1;
	}*/

	switch (action) {
	/* newly added for DB4 logging */
	case LOG_LINE_APPEND_B:
	case LOG_LINE_DELETE_F:
		return 0;
	}

	/*
	 * Put out the changes.  If it's a LOG_LINE_RESET_B call, it's a
	 * special case, avoid the caches.  Also, if it fails and it's
	 * line 1, it just means that the user started with an empty file,
	 * so fake an empty length line.
	 */
	if (action == LOG_LINE_RESET_B) {
		if (db_get(sp, lno, DBG_NOCACHE, &lp, &len)) {
			static CHAR_T nul = 0;
			if (lno != 1) {
				db_err(sp, lno);
				return (1);
			}
			len = 0;
			lp = &nul;
		}
	} else
		if (db_get(sp, lno, DBG_FATAL, &lp, &len))
			return (1);
	BINC_RETC(sp,
	    sp->wp->l_lp, sp->wp->l_len, 
	    len * sizeof(CHAR_T) + CHAR_T_OFFSET);
	sp->wp->l_lp[0] = action;
	memmove(sp->wp->l_lp + sizeof(u_char), &lno, sizeof(db_recno_t));
	MEMMOVEW(sp->wp->l_lp + CHAR_T_OFFSET, lp, len);

	lcur = ep->l_cur;
	memset(&key, 0, sizeof(key));
	key.data = &lcur;
	key.size = sizeof(db_recno_t);
	memset(&data, 0, sizeof(data));
	data.data = sp->wp->l_lp;
	data.size = len * sizeof(CHAR_T) + CHAR_T_OFFSET;
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(LOGDEBUG) && defined(TRACE)
	switch (action) {
	case LOG_LINE_APPEND_F:
		vtrace("%u: log_line: append_f: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_APPEND_B:
		vtrace("%u: log_line: append_b: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_DELETE_F:
		vtrace("%lu: log_line: delete_f: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_DELETE_B:
		vtrace("%lu: log_line: delete_b: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_RESET_F:
		vtrace("%lu: log_line: reset_f: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	case LOG_LINE_RESET_B:
		vtrace("%lu: log_line: reset_b: %lu {%u}\n",
		    ep->l_cur, lno, len);
		break;
	}
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;

	return (0);
}

/*
 * log_mark --
 *	Log a mark position.  For the log to work, we assume that there
 *	aren't any operations that just put out a log record -- this
 *	would mean that undo operations would only reset marks, and not
 *	cause any other change.
 *
 * PUBLIC: int log_mark __P((SCR *, LMARK *));
 */
int
log_mark(SCR *sp, LMARK *lmp)
{
	DBT data, key;
	EXF *ep;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG))
		return (0);

	/* Put out one initial cursor record per set of changes. */
	if (ep->l_cursor.lno != OOBLNO) {
		if (log_cursor1(sp, LOG_CURSOR_INIT))
			return (1);
		ep->l_cursor.lno = OOBLNO;
		ep->l_win = sp->wp;
	}

	BINC_RETC(sp, sp->wp->l_lp,
	    sp->wp->l_len, sizeof(u_char) + sizeof(LMARK));
	sp->wp->l_lp[0] = LOG_MARK;
	memmove(sp->wp->l_lp + sizeof(u_char), lmp, sizeof(LMARK));

	memset(&key, 0, sizeof(key));
	key.data = &ep->l_cur;
	key.size = sizeof(db_recno_t);
	memset(&data, 0, sizeof(data));
	data.data = sp->wp->l_lp;
	data.size = sizeof(u_char) + sizeof(LMARK);
	if (ep->log->put(ep->log, &key, &data, 0) == -1)
		LOG_ERR;

#if defined(LOGDEBUG) && defined(TRACE)
	vtrace("%lu: mark %c: %lu/%u\n",
	    ep->l_cur, lmp->name, lmp->lno, lmp->cno);
#endif
	/* Reset high water mark. */
	ep->l_high = ++ep->l_cur;
	return (0);
}

/*
 * Log_backward --
 *	Roll the log backward one operation.
 *
 * PUBLIC: int log_backward __P((SCR *, MARK *));
 */
int
log_backward(SCR *sp, MARK *rp)
{
	DBT key, data;
	EXF *ep;
	LMARK lm;
	MARK m;
	db_recno_t lno;
	int didop;
	u_char *p;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "010|Logging not being performed, undo not possible");
		return (1);
	}

	if (ep->l_cur == 1) {
		msgq(sp, M_BERR, "011|No changes to undo");
		return (1);
	}

	if (ep->l_win && ep->l_win != sp->wp) {
		ex_emsg(sp, NULL, EXM_LOCKED);
		return 1;
	}
	ep->l_win = sp->wp;
		

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);
	for (didop = 0;;) {
		--ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(LOGDEBUG) && defined(TRACE)
		log_trace(sp, "log_backward", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_INIT:
			if (didop) {
				memmove(rp, p + sizeof(u_char), sizeof(MARK));
				F_CLR(ep, F_NOLOG);
				ep->l_win = NULL;
				return (0);
			}
			break;
		case LOG_CURSOR_END:
			break;
		case LOG_LINE_APPEND_F:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (db_delete(sp, lno))
				goto err;
			++sp->rptlines[L_DELETED];
			break;
		case LOG_LINE_DELETE_B:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (db_insert(sp, lno, 
			    (CHAR_T *)(p + CHAR_T_OFFSET),
			    (data.size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
				goto err;
			++sp->rptlines[L_ADDED];
			break;
		case LOG_LINE_RESET_F:
			break;
		case LOG_LINE_RESET_B:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (db_set(sp, lno, 
			    (CHAR_T *)(p + CHAR_T_OFFSET),
			    (data.size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			didop = 1;
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	ep->l_win = NULL;
	return (1);
}

/*
 * Log_setline --
 *	Reset the line to its original appearance.
 *
 * XXX
 * There's a bug in this code due to our not logging cursor movements
 * unless a change was made.  If you do a change, move off the line,
 * then move back on and do a 'U', the line will be restored to the way
 * it was before the original change.
 *
 * PUBLIC: int log_setline __P((SCR *));
 */
int
log_setline(SCR *sp)
{
	DBT key, data;
	EXF *ep;
	LMARK lm;
	MARK m;
	db_recno_t lno;
	u_char *p;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
		    "012|Logging not being performed, undo not possible");
		return (1);
	}

	if (ep->l_cur == 1)
		return (1);

	if (ep->l_win && ep->l_win != sp->wp) {
		ex_emsg(sp, NULL, EXM_LOCKED);
		return 1;
	}
	ep->l_win = sp->wp;

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);

	for (;;) {
		--ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(LOGDEBUG) && defined(TRACE)
		log_trace(sp, "log_setline", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_INIT:
			memmove(&m, p + sizeof(u_char), sizeof(MARK));
			if (m.lno != sp->lno || ep->l_cur == 1) {
				F_CLR(ep, F_NOLOG);
				ep->l_win = NULL;
				return (0);
			}
			break;
		case LOG_CURSOR_END:
			memmove(&m, p + sizeof(u_char), sizeof(MARK));
			if (m.lno != sp->lno) {
				++ep->l_cur;
				F_CLR(ep, F_NOLOG);
				ep->l_win = NULL;
				return (0);
			}
			break;
		case LOG_LINE_APPEND_F:
		case LOG_LINE_DELETE_B:
		case LOG_LINE_RESET_F:
			break;
		case LOG_LINE_RESET_B:
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (lno == sp->lno &&
			    db_set(sp, lno, (CHAR_T *)(p + CHAR_T_OFFSET),
				(data.size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	ep->l_win = NULL;
	return (1);
}

/*
 * Log_forward --
 *	Roll the log forward one operation.
 *
 * PUBLIC: int log_forward __P((SCR *, MARK *));
 */
int
log_forward(SCR *sp, MARK *rp)
{
	DBT key, data;
	EXF *ep;
	LMARK lm;
	MARK m;
	db_recno_t lno;
	int didop;
	u_char *p;

	ep = sp->ep;
	if (F_ISSET(ep, F_NOLOG)) {
		msgq(sp, M_ERR,
	    "013|Logging not being performed, roll-forward not possible");
		return (1);
	}

	if (ep->l_cur == ep->l_high) {
		msgq(sp, M_BERR, "014|No changes to re-do");
		return (1);
	}

	if (ep->l_win && ep->l_win != sp->wp) {
		ex_emsg(sp, NULL, EXM_LOCKED);
		return 1;
	}
	ep->l_win = sp->wp;

	F_SET(ep, F_NOLOG);		/* Turn off logging. */

	key.data = &ep->l_cur;		/* Initialize db request. */
	key.size = sizeof(recno_t);
	for (didop = 0;;) {
		++ep->l_cur;
		if (ep->log->get(ep->log, &key, &data, 0))
			LOG_ERR;
#if defined(LOGDEBUG) && defined(TRACE)
		log_trace(sp, "log_forward", ep->l_cur, data.data);
#endif
		switch (*(p = (u_char *)data.data)) {
		case LOG_CURSOR_END:
			if (didop) {
				++ep->l_cur;
				memmove(rp, p + sizeof(u_char), sizeof(MARK));
				F_CLR(ep, F_NOLOG);
				ep->l_win = NULL;
				return (0);
			}
			break;
		case LOG_CURSOR_INIT:
			break;
		case LOG_LINE_APPEND_F:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (db_insert(sp, lno, 
			    (CHAR_T *)(p + CHAR_T_OFFSET),
			    (data.size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
				goto err;
			++sp->rptlines[L_ADDED];
			break;
		case LOG_LINE_DELETE_B:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (db_delete(sp, lno))
				goto err;
			++sp->rptlines[L_DELETED];
			break;
		case LOG_LINE_RESET_B:
			break;
		case LOG_LINE_RESET_F:
			didop = 1;
			memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
			if (db_set(sp, lno, 
			    (CHAR_T *)(p + CHAR_T_OFFSET),
			    (data.size - CHAR_T_OFFSET) / sizeof(CHAR_T)))
				goto err;
			if (sp->rptlchange != lno) {
				sp->rptlchange = lno;
				++sp->rptlines[L_CHANGED];
			}
			break;
		case LOG_MARK:
			didop = 1;
			memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
			m.lno = lm.lno;
			m.cno = lm.cno;
			if (mark_set(sp, lm.name, &m, 0))
				goto err;
			break;
		default:
			abort();
		}
	}

err:	F_CLR(ep, F_NOLOG);
	ep->l_win = NULL;
	return (1);
}

/*
 * log_err --
 *	Try and restart the log on failure, i.e. if we run out of memory.
 */
static void
log_err(SCR *sp, const char *file, int line)
{
	EXF *ep;

	msgq(sp, M_SYSERR, "015|%s/%d: log put error", tail(file), line);
	ep = sp->ep;
	(void)ep->log->close(ep->log);
	if (!log_init(sp, ep))
		msgq(sp, M_ERR, "267|Log restarted");
}

#if defined(LOGDEBUG) && defined(TRACE)
static void
log_trace(sp, msg, rno, p)
	SCR *sp;
	const char *msg;
	db_recno_t rno;
	u_char *p;
{
	LMARK lm;
	MARK m;
	db_recno_t lno;

	switch (*p) {
	case LOG_CURSOR_INIT:
		memmove(&m, p + sizeof(u_char), sizeof(MARK));
		vtrace("%lu: %s:  C_INIT: %u/%u\n", rno, msg, m.lno, m.cno);
		break;
	case LOG_CURSOR_END:
		memmove(&m, p + sizeof(u_char), sizeof(MARK));
		vtrace("%lu: %s:   C_END: %u/%u\n", rno, msg, m.lno, m.cno);
		break;
	case LOG_LINE_APPEND_F:
		memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
		vtrace("%lu: %s:  APPEND_F: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_APPEND_B:
		memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
		vtrace("%lu: %s:  APPEND_B: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_DELETE_F:
		memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
		vtrace("%lu: %s:  DELETE_F: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_DELETE_B:
		memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
		vtrace("%lu: %s:  DELETE_B: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_RESET_F:
		memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
		vtrace("%lu: %s: RESET_F: %lu\n", rno, msg, lno);
		break;
	case LOG_LINE_RESET_B:
		memmove(&lno, p + sizeof(u_char), sizeof(db_recno_t));
		vtrace("%lu: %s: RESET_B: %lu\n", rno, msg, lno);
		break;
	case LOG_MARK:
		memmove(&lm, p + sizeof(u_char), sizeof(LMARK));
		vtrace("%lu: %s:    MARK: %u/%u\n", rno, msg, lm.lno, lm.cno);
		break;
	default:
		abort();
	}
}
#endif