Catch up with the latest LWKT msgport updating.
[dragonfly/vkernel-mp.git] / bin / ed / undo.c
blob08475b366e59f79cc050e072c289181dc55213c4
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.
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.
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.
27 * @(#)undo.c,v 1.1 1994/02/01 00:34:44 alm Exp
28 * $FreeBSD: src/bin/ed/undo.c,v 1.12 2002/06/30 05:13:53 obrien Exp $
29 * $DragonFly: src/bin/ed/undo.c,v 1.4 2007/04/06 23:36:54 pavalos Exp $
32 #include "ed.h"
35 #define USIZE 100 /* undo stack size */
36 undo_t *ustack = NULL; /* undo stack */
37 long usize = 0; /* stack size variable */
38 long u_p = 0; /* undo stack pointer */
40 /* push_undo_stack: return pointer to initialized undo node */
41 undo_t *
42 push_undo_stack(int type, long from, long to)
44 undo_t *t;
46 #if defined(sun) || defined(NO_REALLOC_NULL)
47 if (ustack == NULL &&
48 (ustack = (undo_t *) malloc((usize = USIZE) * sizeof(undo_t))) == NULL) {
49 fprintf(stderr, "%s\n", strerror(errno));
50 errmsg = "out of memory";
51 return NULL;
53 #endif
54 t = ustack;
55 if (u_p < usize ||
56 (t = (undo_t *) realloc(ustack, (usize += USIZE) * sizeof(undo_t))) != NULL) {
57 ustack = t;
58 ustack[u_p].type = type;
59 ustack[u_p].t = get_addressed_line_node(to);
60 ustack[u_p].h = get_addressed_line_node(from);
61 return ustack + u_p++;
63 /* out of memory - release undo stack */
64 fprintf(stderr, "%s\n", strerror(errno));
65 errmsg = "out of memory";
66 clear_undo_stack();
67 free(ustack);
68 ustack = NULL;
69 usize = 0;
70 return NULL;
74 /* USWAP: swap undo nodes */
75 #define USWAP(x,y) { \
76 undo_t utmp; \
77 utmp = x, x = y, y = utmp; \
81 long u_current_addr = -1; /* if >= 0, undo enabled */
82 long u_addr_last = -1; /* if >= 0, undo enabled */
84 /* pop_undo_stack: undo last change to the editor buffer */
85 int
86 pop_undo_stack(void)
88 long n;
89 long o_current_addr = current_addr;
90 long o_addr_last = addr_last;
92 if (u_current_addr == -1 || u_addr_last == -1) {
93 errmsg = "nothing to undo";
94 return ERR;
95 } else if (u_p)
96 modified = 1;
97 get_addressed_line_node(0); /* this get_addressed_line_node last! */
98 SPL1();
99 for (n = u_p; n-- > 0;) {
100 switch(ustack[n].type) {
101 case UADD:
102 REQUE(ustack[n].h->q_back, ustack[n].t->q_forw);
103 break;
104 case UDEL:
105 REQUE(ustack[n].h->q_back, ustack[n].h);
106 REQUE(ustack[n].t, ustack[n].t->q_forw);
107 break;
108 case UMOV:
109 case VMOV:
110 REQUE(ustack[n - 1].h, ustack[n].h->q_forw);
111 REQUE(ustack[n].t->q_back, ustack[n - 1].t);
112 REQUE(ustack[n].h, ustack[n].t);
113 n--;
114 break;
115 default:
116 /*NOTREACHED*/
119 ustack[n].type ^= 1;
121 /* reverse undo stack order */
122 for (n = u_p; n-- > (u_p + 1)/ 2;)
123 USWAP(ustack[n], ustack[u_p - 1 - n]);
124 if (isglobal)
125 clear_active_list();
126 current_addr = u_current_addr, u_current_addr = o_current_addr;
127 addr_last = u_addr_last, u_addr_last = o_addr_last;
128 SPL0();
129 return 0;
133 /* clear_undo_stack: clear the undo stack */
134 void
135 clear_undo_stack(void)
137 line_t *lp, *ep, *tl;
139 while (u_p--)
140 if (ustack[u_p].type == UDEL) {
141 ep = ustack[u_p].t->q_forw;
142 for (lp = ustack[u_p].h; lp != ep; lp = tl) {
143 unmark_line_node(lp);
144 tl = lp->q_forw;
145 free(lp);
148 u_p = 0;
149 u_current_addr = current_addr;
150 u_addr_last = addr_last;