GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mg/undo.c Lines: 0 225 0.0 %
Date: 2016-12-06 Branches: 0 152 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: undo.c,v 1.57 2015/12/11 20:21:23 mmcc Exp $ */
2
/*
3
 * This file is in the public domain
4
 */
5
6
#include <sys/queue.h>
7
#include <signal.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
12
#include "def.h"
13
#include "kbd.h"
14
15
#define MAX_FREE_RECORDS	32
16
17
/*
18
 * Local variables
19
 */
20
static struct undoq		 undo_free;
21
static int			 undo_free_num;
22
static int			 boundary_flag = TRUE;
23
static int			 undo_enable_flag = TRUE;
24
25
/*
26
 * Local functions
27
 */
28
static int find_dot(struct line *, int);
29
static int find_lo(int, struct line **, int *, int *);
30
static struct undo_rec *new_undo_record(void);
31
static int drop_oldest_undo_record(void);
32
33
/*
34
 * find_dot, find_lo()
35
 *
36
 * Find an absolute dot in the buffer from a line/offset pair, and vice-versa.
37
 *
38
 * Since lines can be deleted while they are referenced by undo record, we
39
 * need to have an absolute dot to have something reliable.
40
 */
41
static int
42
find_dot(struct line *lp, int off)
43
{
44
	int	 count = 0;
45
	struct line	*p;
46
47
	for (p = curbp->b_headp; p != lp; p = lforw(p)) {
48
		if (count != 0) {
49
			if (p == curbp->b_headp) {
50
				dobeep();
51
				ewprintf("Error: Undo stuff called with a"
52
				    "nonexistent line");
53
				return (FALSE);
54
			}
55
		}
56
		count += llength(p) + 1;
57
	}
58
	count += off;
59
60
	return (count);
61
}
62
63
static int
64
find_lo(int pos, struct line **olp, int *offset, int *lnum)
65
{
66
	struct line *p;
67
	int lineno;
68
69
	p = curbp->b_headp;
70
	lineno = 0;
71
	while (pos > llength(p)) {
72
		pos -= llength(p) + 1;
73
		if ((p = lforw(p)) == curbp->b_headp) {
74
			*olp = NULL;
75
			*offset = 0;
76
			return (FALSE);
77
		}
78
		lineno++;
79
	}
80
	*olp = p;
81
	*offset = pos;
82
	*lnum = lineno;
83
84
	return (TRUE);
85
}
86
87
static struct undo_rec *
88
new_undo_record(void)
89
{
90
	struct undo_rec *rec;
91
92
	rec = TAILQ_FIRST(&undo_free);
93
	if (rec != NULL) {
94
		/* Remove it from the free-list */
95
		TAILQ_REMOVE(&undo_free, rec, next);
96
		undo_free_num--;
97
	} else {
98
		if ((rec = malloc(sizeof(*rec))) == NULL)
99
			panic("Out of memory in undo code (record)");
100
	}
101
	memset(rec, 0, sizeof(struct undo_rec));
102
103
	return (rec);
104
}
105
106
void
107
free_undo_record(struct undo_rec *rec)
108
{
109
	static int initialised = 0;
110
111
	/*
112
	 * On the first run, do initialisation of the free list.
113
	 */
114
	if (initialised == 0) {
115
		TAILQ_INIT(&undo_free);
116
		initialised = 1;
117
	}
118
	free(rec->content);
119
	rec->content = NULL;
120
	if (undo_free_num >= MAX_FREE_RECORDS) {
121
		free(rec);
122
		return;
123
	}
124
	undo_free_num++;
125
126
	TAILQ_INSERT_HEAD(&undo_free, rec, next);
127
}
128
129
/*
130
 * Drop the oldest undo record in our list. Return 1 if we could remove it,
131
 * 0 if the undo list was empty.
132
 */
133
static int
134
drop_oldest_undo_record(void)
135
{
136
	struct undo_rec *rec;
137
138
	rec = TAILQ_LAST(&curbp->b_undo, undoq);
139
	if (rec != NULL) {
140
		undo_free_num--;
141
		TAILQ_REMOVE(&curbp->b_undo, rec, next);
142
		free_undo_record(rec);
143
		return (1);
144
	}
145
	return (0);
146
}
147
148
static int
149
lastrectype(void)
150
{
151
	struct undo_rec *rec;
152
153
	if ((rec = TAILQ_FIRST(&curbp->b_undo)) != NULL)
154
		return (rec->type);
155
	return (0);
156
}
157
158
/*
159
 * Returns TRUE if undo is enabled, FALSE otherwise.
160
 */
161
int
162
undo_enabled(void)
163
{
164
	return (undo_enable_flag);
165
}
166
167
/*
168
 * undo_enable: toggle undo_enable.
169
 * Returns the previous value of the flag.
170
 */
171
int
172
undo_enable(int f, int n)
173
{
174
	int pon = undo_enable_flag;
175
176
	if (f & (FFARG | FFRAND))
177
		undo_enable_flag = n > 0;
178
	else
179
		undo_enable_flag = !undo_enable_flag;
180
181
	if (!(f & FFRAND))
182
		ewprintf("Undo %sabled", undo_enable_flag ? "en" : "dis");
183
184
	return (pon);
185
}
186
187
/*
188
 * If undo is enabled, then:
189
 *  Toggle undo boundary recording.
190
 *  If called with an argument, (n > 0) => enable. Otherwise disable.
191
 * In either case, add an undo boundary
192
 * If undo is disabled, this function has no effect.
193
 */
194
int
195
undo_boundary_enable(int f, int n)
196
{
197
	int bon = boundary_flag;
198
199
	if (!undo_enable_flag)
200
		return (FALSE);
201
202
	undo_add_boundary(FFRAND, 1);
203
204
	if (f & (FFARG | FFRAND))
205
		boundary_flag = n > 0;
206
	else
207
		boundary_flag = !boundary_flag;
208
209
	if (!(f & FFRAND))
210
		ewprintf("Undo boundaries %sabled",
211
		    boundary_flag ? "en" : "dis");
212
213
	return (bon);
214
}
215
216
/*
217
 * Record an undo boundary, unless boundary_flag == FALSE.
218
 * Does nothing if previous undo entry is already a boundary or 'modified' flag.
219
 */
220
int
221
undo_add_boundary(int f, int n)
222
{
223
	struct undo_rec *rec;
224
	int last;
225
226
	if (boundary_flag == FALSE)
227
		return (FALSE);
228
229
	last = lastrectype();
230
	if (last == BOUNDARY || last == MODIFIED)
231
		return (TRUE);
232
233
	rec = new_undo_record();
234
	rec->type = BOUNDARY;
235
236
	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
237
238
	return (TRUE);
239
}
240
241
/*
242
 * Record an undo "modified" boundary
243
 */
244
void
245
undo_add_modified(void)
246
{
247
	struct undo_rec *rec, *trec;
248
249
	TAILQ_FOREACH_SAFE(rec, &curbp->b_undo, next, trec)
250
		if (rec->type == MODIFIED) {
251
			TAILQ_REMOVE(&curbp->b_undo, rec, next);
252
			free_undo_record(rec);
253
		}
254
255
	rec = new_undo_record();
256
	rec->type = MODIFIED;
257
258
	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
259
260
	return;
261
}
262
263
int
264
undo_add_insert(struct line *lp, int offset, int size)
265
{
266
	struct region	reg;
267
	struct	undo_rec *rec;
268
	int	pos;
269
270
	if (!undo_enable_flag)
271
		return (TRUE);
272
	reg.r_linep = lp;
273
	reg.r_offset = offset;
274
	reg.r_size = size;
275
276
	pos = find_dot(lp, offset);
277
278
	/*
279
	 * We try to reuse the last undo record to `compress' things.
280
	 */
281
	rec = TAILQ_FIRST(&curbp->b_undo);
282
	if (rec != NULL && rec->type == INSERT) {
283
		if (rec->pos + rec->region.r_size == pos) {
284
			rec->region.r_size += reg.r_size;
285
			return (TRUE);
286
		}
287
	}
288
289
	/*
290
	 * We couldn't reuse the last undo record, so prepare a new one.
291
	 */
292
	rec = new_undo_record();
293
	rec->pos = pos;
294
	rec->type = INSERT;
295
	memmove(&rec->region, &reg, sizeof(struct region));
296
	rec->content = NULL;
297
298
	undo_add_boundary(FFRAND, 1);
299
300
	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
301
302
	return (TRUE);
303
}
304
305
/*
306
 * This of course must be done _before_ the actual deletion is done.
307
 */
308
int
309
undo_add_delete(struct line *lp, int offset, int size, int isreg)
310
{
311
	struct region	reg;
312
	struct	undo_rec *rec;
313
	int	pos;
314
315
	if (!undo_enable_flag)
316
		return (TRUE);
317
318
	reg.r_linep = lp;
319
	reg.r_offset = offset;
320
	reg.r_size = size;
321
322
	pos = find_dot(lp, offset);
323
324
	if (offset == llength(lp))	/* if it's a newline... */
325
		undo_add_boundary(FFRAND, 1);
326
	else if ((rec = TAILQ_FIRST(&curbp->b_undo)) != NULL) {
327
		/*
328
		 * Separate this command from the previous one if we're not
329
		 * just before the previous record...
330
		 */
331
		if (!isreg && rec->type == DELETE) {
332
			if (rec->pos - rec->region.r_size != pos)
333
				undo_add_boundary(FFRAND, 1);
334
		}
335
	}
336
	rec = new_undo_record();
337
	rec->pos = pos;
338
	if (isreg)
339
		rec->type = DELREG;
340
	else
341
		rec->type = DELETE;
342
	memmove(&rec->region, &reg, sizeof(struct region));
343
	do {
344
		rec->content = malloc(reg.r_size + 1);
345
	} while ((rec->content == NULL) && drop_oldest_undo_record());
346
347
	if (rec->content == NULL)
348
		panic("Out of memory");
349
350
	region_get_data(&reg, rec->content, reg.r_size);
351
352
	if (isreg || lastrectype() != DELETE)
353
		undo_add_boundary(FFRAND, 1);
354
355
	TAILQ_INSERT_HEAD(&curbp->b_undo, rec, next);
356
357
	return (TRUE);
358
}
359
360
/*
361
 * This of course must be called before the change takes place.
362
 */
363
int
364
undo_add_change(struct line *lp, int offset, int size)
365
{
366
	if (!undo_enable_flag)
367
		return (TRUE);
368
	undo_add_boundary(FFRAND, 1);
369
	boundary_flag = FALSE;
370
	undo_add_delete(lp, offset, size, 0);
371
	undo_add_insert(lp, offset, size);
372
	boundary_flag = TRUE;
373
	undo_add_boundary(FFRAND, 1);
374
375
	return (TRUE);
376
}
377
378
/*
379
 * Show the undo records for the current buffer in a new buffer.
380
 */
381
/* ARGSUSED */
382
int
383
undo_dump(int f, int n)
384
{
385
	struct	 undo_rec *rec;
386
	struct buffer	*bp;
387
	struct mgwin	*wp;
388
	char	 buf[4096], tmp[1024];
389
	int	 num;
390
391
	/*
392
	 * Prepare the buffer for insertion.
393
	 */
394
	if ((bp = bfind("*undo*", TRUE)) == NULL)
395
		return (FALSE);
396
	bp->b_flag |= BFREADONLY;
397
	bclear(bp);
398
	if ((wp = popbuf(bp, WNONE)) == NULL)
399
		return (FALSE);
400
401
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
402
		if (wp->w_bufp == bp) {
403
			wp->w_dotp = bp->b_headp;
404
			wp->w_doto = 0;
405
		}
406
	}
407
408
	num = 0;
409
	TAILQ_FOREACH(rec, &curbp->b_undo, next) {
410
		num++;
411
		snprintf(buf, sizeof(buf),
412
		    "%d:\t %s at %d ", num,
413
		    (rec->type == DELETE) ? "DELETE":
414
		    (rec->type == DELREG) ? "DELREGION":
415
		    (rec->type == INSERT) ? "INSERT":
416
		    (rec->type == BOUNDARY) ? "----" :
417
		    (rec->type == MODIFIED) ? "MODIFIED": "UNKNOWN",
418
		    rec->pos);
419
420
		if (rec->content) {
421
			(void)strlcat(buf, "\"", sizeof(buf));
422
			snprintf(tmp, sizeof(tmp), "%.*s", rec->region.r_size,
423
			    rec->content);
424
			(void)strlcat(buf, tmp, sizeof(buf));
425
			(void)strlcat(buf, "\"", sizeof(buf));
426
		}
427
		snprintf(tmp, sizeof(tmp), " [%d]", rec->region.r_size);
428
		if (strlcat(buf, tmp, sizeof(buf)) >= sizeof(buf)) {
429
			dobeep();
430
			ewprintf("Undo record too large. Aborted.");
431
			return (FALSE);
432
		}
433
		addlinef(bp, "%s", buf);
434
	}
435
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
436
		if (wp->w_bufp == bp) {
437
			wp->w_dotline = num+1;
438
			wp->w_rflag |= WFFULL;
439
		}
440
	}
441
	return (TRUE);
442
}
443
444
/*
445
 * After the user did action1, then action2, then action3:
446
 *
447
 *	[action3] <--- Undoptr
448
 *	[action2]
449
 *	[action1]
450
 *	 ------
451
 *	 [undo]
452
 *
453
 * After undo:
454
 *
455
 *	[undo of action3]
456
 *	[action2] <--- Undoptr
457
 *	[action1]
458
 *	 ------
459
 *	 [undo]
460
 *
461
 * After another undo:
462
 *
463
 *
464
 *	[undo of action2]
465
 *	[undo of action3]
466
 *	[action1]  <--- Undoptr
467
 *	 ------
468
 *	 [undo]
469
 *
470
 * Note that the "undo of actionX" have no special meaning. Only when
471
 * we undo a deletion, the insertion will be recorded just as if it
472
 * was typed on the keyboard. Resulting in the inverse operation being
473
 * saved in the list.
474
 *
475
 * If undoptr reaches the bottom of the list, or if we moved between
476
 * two undo actions, we make it point back at the topmost record. This is
477
 * how we handle redoing.
478
 */
479
/* ARGSUSED */
480
int
481
undo(int f, int n)
482
{
483
	struct undo_rec	*ptr, *nptr;
484
	int		 done, rval;
485
	struct line	*lp;
486
	int		 offset, save;
487
	static int	 nulled = FALSE;
488
	int		 lineno;
489
490
	if (n < 0)
491
		return (FALSE);
492
493
	ptr = curbp->b_undoptr;
494
495
	/* first invocation, make ptr point back to the top of the list */
496
	if ((ptr == NULL && nulled == TRUE) ||  rptcount == 0) {
497
		ptr = TAILQ_FIRST(&curbp->b_undo);
498
		nulled = TRUE;
499
	}
500
501
	rval = TRUE;
502
	while (n--) {
503
		/* if we have a spurious boundary, free it and move on.... */
504
		while (ptr && ptr->type == BOUNDARY) {
505
			nptr = TAILQ_NEXT(ptr, next);
506
			TAILQ_REMOVE(&curbp->b_undo, ptr, next);
507
			free_undo_record(ptr);
508
			ptr = nptr;
509
		}
510
		/*
511
		 * Ptr is NULL, but on the next run, it will point to the
512
		 * top again, redoing all stuff done in the buffer since
513
		 * its creation.
514
		 */
515
		if (ptr == NULL) {
516
			dobeep();
517
			ewprintf("No further undo information");
518
			rval = FALSE;
519
			nulled = TRUE;
520
			break;
521
		}
522
		nulled = FALSE;
523
524
		/*
525
		 * Loop while we don't get a boundary specifying we've
526
		 * finished the current action...
527
		 */
528
529
		undo_add_boundary(FFRAND, 1);
530
531
		save = boundary_flag;
532
		boundary_flag = FALSE;
533
534
		done = 0;
535
		do {
536
			/*
537
			 * Move to where this has to apply
538
			 *
539
			 * Boundaries (and the modified flag)  are put as
540
			 * position 0 (to save lookup time in find_dot)
541
			 * so we must not move there...
542
			 */
543
			if (ptr->type != BOUNDARY && ptr->type != MODIFIED) {
544
				if (find_lo(ptr->pos, &lp,
545
				    &offset, &lineno) == FALSE) {
546
					dobeep();
547
					ewprintf("Internal error in Undo!");
548
					rval = FALSE;
549
					break;
550
				}
551
				curwp->w_dotp = lp;
552
				curwp->w_doto = offset;
553
				curwp->w_markline = curwp->w_dotline;
554
				curwp->w_dotline = lineno;
555
			}
556
557
			/*
558
			 * Do operation^-1
559
			 */
560
			switch (ptr->type) {
561
			case INSERT:
562
				ldelete(ptr->region.r_size, KNONE);
563
				break;
564
			case DELETE:
565
				lp = curwp->w_dotp;
566
				offset = curwp->w_doto;
567
				region_put_data(ptr->content,
568
				    ptr->region.r_size);
569
				curwp->w_dotp = lp;
570
				curwp->w_doto = offset;
571
				break;
572
			case DELREG:
573
				region_put_data(ptr->content,
574
				    ptr->region.r_size);
575
				break;
576
			case BOUNDARY:
577
				done = 1;
578
				break;
579
			case MODIFIED:
580
				curbp->b_flag &= ~BFCHG;
581
				break;
582
			default:
583
				break;
584
			}
585
586
			/* And move to next record */
587
			ptr = TAILQ_NEXT(ptr, next);
588
		} while (ptr != NULL && !done);
589
590
		boundary_flag = save;
591
		undo_add_boundary(FFRAND, 1);
592
593
		ewprintf("Undo!");
594
	}
595
596
	curbp->b_undoptr = ptr;
597
598
	return (rval);
599
}