xref: /freebsd/bin/ed/undo.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 /* undo.c: This file contains the undo routines for the ed line editor */
2 /*-
3  * Copyright (c) 1993 Andrew Moore, Talke Studio.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "ed.h"
32 
33 
34 #define USIZE 100				/* undo stack size */
35 static undo_t *ustack = NULL;			/* undo stack */
36 static long usize = 0;				/* stack size variable */
37 static long u_p = 0;				/* undo stack pointer */
38 
39 /* push_undo_stack: return pointer to initialized undo node */
40 undo_t *
41 push_undo_stack(int type, long from, long to)
42 {
43 	undo_t *t;
44 
45 #if defined(sun) || defined(NO_REALLOC_NULL)
46 	if (ustack == NULL &&
47 	    (ustack = (undo_t *) malloc((usize = USIZE) * sizeof(undo_t))) == NULL) {
48 		fprintf(stderr, "%s\n", strerror(errno));
49 		errmsg = "out of memory";
50 		return NULL;
51 	}
52 #endif
53 	t = ustack;
54 	if (u_p < usize ||
55 	    (t = (undo_t *) realloc(ustack, (usize += USIZE) * sizeof(undo_t))) != NULL) {
56 		ustack = t;
57 		ustack[u_p].type = type;
58 		ustack[u_p].t = get_addressed_line_node(to);
59 		ustack[u_p].h = get_addressed_line_node(from);
60 		return ustack + u_p++;
61 	}
62 	/* out of memory - release undo stack */
63 	fprintf(stderr, "%s\n", strerror(errno));
64 	errmsg = "out of memory";
65 	clear_undo_stack();
66 	free(ustack);
67 	ustack = NULL;
68 	usize = 0;
69 	return NULL;
70 }
71 
72 
73 /* USWAP: swap undo nodes */
74 #define USWAP(x,y) { \
75 	undo_t utmp; \
76 	utmp = x, x = y, y = utmp; \
77 }
78 
79 
80 long u_current_addr = -1;	/* if >= 0, undo enabled */
81 long u_addr_last = -1;		/* if >= 0, undo enabled */
82 
83 /* pop_undo_stack: undo last change to the editor buffer */
84 int
85 pop_undo_stack(void)
86 {
87 	long n;
88 	long o_current_addr = current_addr;
89 	long o_addr_last = addr_last;
90 
91 	if (u_current_addr == -1 || u_addr_last == -1) {
92 		errmsg = "nothing to undo";
93 		return ERR;
94 	} else if (u_p)
95 		modified = 1;
96 	get_addressed_line_node(0);	/* this get_addressed_line_node last! */
97 	SPL1();
98 	for (n = u_p; n-- > 0;) {
99 		switch(ustack[n].type) {
100 		case UADD:
101 			REQUE(ustack[n].h->q_back, ustack[n].t->q_forw);
102 			break;
103 		case UDEL:
104 			REQUE(ustack[n].h->q_back, ustack[n].h);
105 			REQUE(ustack[n].t, ustack[n].t->q_forw);
106 			break;
107 		case UMOV:
108 		case VMOV:
109 			REQUE(ustack[n - 1].h, ustack[n].h->q_forw);
110 			REQUE(ustack[n].t->q_back, ustack[n - 1].t);
111 			REQUE(ustack[n].h, ustack[n].t);
112 			n--;
113 			break;
114 		default:
115 			/*NOTREACHED*/
116 			;
117 		}
118 		ustack[n].type ^= 1;
119 	}
120 	/* reverse undo stack order */
121 	for (n = u_p; n-- > (u_p + 1)/ 2;)
122 		USWAP(ustack[n], ustack[u_p - 1 - n]);
123 	if (isglobal)
124 		clear_active_list();
125 	current_addr = u_current_addr, u_current_addr = o_current_addr;
126 	addr_last = u_addr_last, u_addr_last = o_addr_last;
127 	SPL0();
128 	return 0;
129 }
130 
131 
132 /* clear_undo_stack: clear the undo stack */
133 void
134 clear_undo_stack(void)
135 {
136 	line_t *lp, *ep, *tl;
137 
138 	while (u_p--)
139 		if (ustack[u_p].type == UDEL) {
140 			ep = ustack[u_p].t->q_forw;
141 			for (lp = ustack[u_p].h; lp != ep; lp = tl) {
142 				unmark_line_node(lp);
143 				tl = lp->q_forw;
144 				free(lp);
145 			}
146 		}
147 	u_p = 0;
148 	u_current_addr = current_addr;
149 	u_addr_last = addr_last;
150 }
151