/* undo.c: This file contains the undo routines for the ed line editor */1/*-2* Copyright (c) 1993 Andrew Moore, Talke Studio.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/cdefs.h>28#include "ed.h"293031#define USIZE 100 /* undo stack size */32static undo_t *ustack = NULL; /* undo stack */33static long usize = 0; /* stack size variable */34static long u_p = 0; /* undo stack pointer */3536/* push_undo_stack: return pointer to initialized undo node */37undo_t *38push_undo_stack(int type, long from, long to)39{40undo_t *t;4142#if defined(sun) || defined(NO_REALLOC_NULL)43if (ustack == NULL &&44(ustack = (undo_t *) malloc((usize = USIZE) * sizeof(undo_t))) == NULL) {45fprintf(stderr, "%s\n", strerror(errno));46errmsg = "out of memory";47return NULL;48}49#endif50t = ustack;51if (u_p < usize ||52(t = (undo_t *) realloc(ustack, (usize += USIZE) * sizeof(undo_t))) != NULL) {53ustack = t;54ustack[u_p].type = type;55ustack[u_p].t = get_addressed_line_node(to);56ustack[u_p].h = get_addressed_line_node(from);57return ustack + u_p++;58}59/* out of memory - release undo stack */60fprintf(stderr, "%s\n", strerror(errno));61errmsg = "out of memory";62clear_undo_stack();63free(ustack);64ustack = NULL;65usize = 0;66return NULL;67}686970/* USWAP: swap undo nodes */71#define USWAP(x,y) { \72undo_t utmp; \73utmp = x, x = y, y = utmp; \74}757677long u_current_addr = -1; /* if >= 0, undo enabled */78long u_addr_last = -1; /* if >= 0, undo enabled */7980/* pop_undo_stack: undo last change to the editor buffer */81int82pop_undo_stack(void)83{84long n;85long o_current_addr = current_addr;86long o_addr_last = addr_last;8788if (u_current_addr == -1 || u_addr_last == -1) {89errmsg = "nothing to undo";90return ERR;91} else if (u_p)92modified = 1;93get_addressed_line_node(0); /* this get_addressed_line_node last! */94SPL1();95for (n = u_p; n-- > 0;) {96switch(ustack[n].type) {97case UADD:98REQUE(ustack[n].h->q_back, ustack[n].t->q_forw);99break;100case UDEL:101REQUE(ustack[n].h->q_back, ustack[n].h);102REQUE(ustack[n].t, ustack[n].t->q_forw);103break;104case UMOV:105case VMOV:106REQUE(ustack[n - 1].h, ustack[n].h->q_forw);107REQUE(ustack[n].t->q_back, ustack[n - 1].t);108REQUE(ustack[n].h, ustack[n].t);109n--;110break;111default:112/*NOTREACHED*/113;114}115ustack[n].type ^= 1;116}117/* reverse undo stack order */118for (n = u_p; n-- > (u_p + 1)/ 2;)119USWAP(ustack[n], ustack[u_p - 1 - n]);120if (isglobal)121clear_active_list();122current_addr = u_current_addr, u_current_addr = o_current_addr;123addr_last = u_addr_last, u_addr_last = o_addr_last;124SPL0();125return 0;126}127128129/* clear_undo_stack: clear the undo stack */130void131clear_undo_stack(void)132{133line_t *lp, *ep, *tl;134135while (u_p--)136if (ustack[u_p].type == UDEL) {137ep = ustack[u_p].t->q_forw;138for (lp = ustack[u_p].h; lp != ep; lp = tl) {139unmark_line_node(lp);140tl = lp->q_forw;141free(lp);142}143}144u_p = 0;145u_current_addr = current_addr;146u_addr_last = addr_last;147}148149150