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