1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
* LEVEE, or Captain Video; A vi clone
*
* Copyright (c) 1982-2007 David L Parsons
* All rights reserved.
*
* Redistribution and use in source and binary forms, without or
* without modification, are permitted provided that the above
* copyright notice and this paragraph are duplicated in all such
* forms and that any documentation, advertising materials, and
* other materials related to such distribution and use acknowledge
* that the software was developed by David L Parsons (orc@pell.chi.il.us).
* My name may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
#include "levee.h"
#include "extern.h"
#define BUFSZ sizeof(undo.coreblock)
#define AVAIL(x) ((x)<<1)
#define INDEX(x) ((1+x)>>1)
bool PROC
pushblock(u)
struct undostack *u;
{
if (u->blockp == 0)
if ((uwrite = OPEN_NEW(undobuf)) < 0)
return FALSE;
if (BUFSZ == WRITE_TEXT(uwrite, u->coreblock, BUFSZ)) {
u->blockp++;
u->ptr = 0;
return TRUE;
}
return FALSE;
}
bool PROC
pushw(u, i)
struct undostack *u;
int i;
{
if (u->ptr >= PAGESIZE && !pushblock(u))
return FALSE;
u->coreblock[u->ptr++] = i;
return TRUE;
}
bool PROC
pushmem(u, start, size)
struct undostack *u;
int start,size;
{
int chunk;
bool ok;
ok = TRUE;
while (ok && size > 0) {
chunk = min(size, AVAIL(PAGESIZE-u->ptr));
moveleft(&core[start], (char*)&u->coreblock[u->ptr], chunk);
size -= chunk;
start += chunk;
if (size > 0)
ok = pushblock(u);
else
u->ptr += INDEX(chunk);
}
return ok;
}
VOID PROC
zerostack(u)
struct undostack *u;
{
if (u->blockp > 0)
CLOSE_FILE(uwrite);
u->blockp = 0; /* initialize the stack */
u->ptr = 0;
}
bool PROC
uputcmd(u, size, start, cmd)
struct undostack *u;
int size,start;
char cmd;
{
return(pushw(u, size) && pushw(u, start) && pushw(u, cmd));
}
VOID PROC
insert_to_undo(u, start, size)
struct undostack *u;
int start,size;
{
if (uputcmd(u, size, start, U_DELC)) {
fixmarkers(start, size);
bufmax += size;
}
else
error();
}
/* delete stuff from the buffer && put it into the undo stack */
bool PROC
delete_to_undo(u, start, lump)
struct undostack *u;
int start, lump;
{
if (lump <= 0)
return TRUE;
else if (pushmem(u,start,lump) && uputcmd(u,lump,start,U_ADDC)) {
moveleft(&core[start+lump], &core[start], bufmax-(start+lump));
bufmax -= lump;
fixmarkers(start,-lump);
return TRUE;
}
else
return FALSE;
}
/* copy stuff into the undo buffer */
bool PROC
move_to_undo(u, start, lump)
struct undostack *u;
int start,lump;
{
return pushmem(u, start, lump) && uputcmd(u,lump,start,U_MOVEC);
}
bool PROC
popblock(u)
struct undostack *u;
{
if (u->blockp > 0) {
if (SEEK_POSITION(uread, (long)((--u->blockp)*BUFSZ), 0) < 0)
return FALSE;
if (BUFSZ == READ_TEXT(uread, u->coreblock, BUFSZ)) {
u->ptr = PAGESIZE;
return TRUE;
}
}
return FALSE;
}
bool PROC
popw(u, i)
struct undostack *u;
int *i;
{
if (u->ptr < 1 && !popblock(u))
return FALSE;
*i = u->coreblock[--u->ptr];
return TRUE;
}
bool PROC
popmem(u, start, size)
struct undostack *u;
int start, size;
{
int chunk, loc;
bool ok;
loc = start+size; /* running backwards */
ok = TRUE;
while (ok && size > 0) {
chunk = min(size, AVAIL(u->ptr));
size -= chunk;
loc -= chunk;
moveleft((char*)&u->coreblock[u->ptr-INDEX(chunk)], &core[loc], chunk);
if (size > 0)
ok = popblock(u);
else
u->ptr -= INDEX(chunk);
}
return(ok);
}
/* delete (I)nserted text */
bool PROC
takeout(save_undo,curp)
struct undostack *save_undo;
int *curp;
{
int lump;
return popw(&undo,curp) && popw(&undo,&lump)
&& delete_to_undo(save_undo,*curp,lump);
}
bool PROC
copyover(save_undo,curp)
struct undostack *save_undo;
int *curp;
{
int lump;
return popw(&undo, curp) && popw(&undo, &lump)
&& move_to_undo(save_undo, *curp, lump)
&& popmem(&undo, *curp, lump);
}
bool PROC
putin(save_undo,curp)
struct undostack *save_undo;
int *curp;
{
int lump;
if (popw(&undo,curp) && popw(&undo,&lump) && (bufmax+lump < SIZE)) {
insert_to_undo(save_undo, *curp, lump);
moveright(&core[*curp], &core[*curp+lump], bufmax-*curp);
if (popmem(&undo, *curp, lump))
return TRUE;
else
moveleft(&core[*curp+lump], &core[*curp], bufmax-*curp);
}
return FALSE;
}
/* driver for undo -- returns last address modified || -1 if error */
int PROC
fixcore(topp)
int *topp;
{
int curp;
static struct undostack save_undo;
bool closeio, ok;
int cch;
if (undo.blockp > 0 || undo.ptr > 0) {
closeio = (undo.blockp > 0);
if (closeio) { /* save diskfile */
CLOSE_FILE(uwrite); /* close current undo file */
rename(undobuf,undotmp);
uread = OPEN_OLD(undotmp); /* reopen it for reading */
if (uread < 0)
return -1;
}
*topp = SIZE+1;
curp = -MAGICNUMBER;
save_undo.blockp = save_undo.ptr = 0;
ok = TRUE;
while (ok && popw(&undo,&cch)) {
switch (cch) {
case U_ADDC : ok = putin(&save_undo, &curp); break;
case U_MOVEC: ok = copyover(&save_undo, &curp); break;
case U_DELC : ok = takeout(&save_undo, &curp); break;
}
if (curp < *topp)
*topp = curp;
}
if (curp >= 0)
undo = save_undo;
if (closeio) {
CLOSE_FILE(uread); /* Zap old buffer */
unlink(undotmp);
}
if (!ok)
error();
return(curp);
}
return ERR;
}
|