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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
* gap.c
*
* Anthony's Editor November 95
*
* Copyright 1993, 1995 by Anthony Howe. All rights reserved. No warranty.
*/
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "header.h"
typedef struct t_undo {
short u_set;
t_point u_point;
t_point u_gap;
t_point u_egap;
} t_undo;
static t_undo ubuf;
/*
* Enlarge the gap by n characters.
* Note that the position of the gap cannot change.
*/
int
growgap(n)
t_point n;
{
t_char *new;
t_point buflen, newlen, xgap, xegap;
assert(buf <= gap);
assert(gap <= egap);
assert(egap <= ebuf);
xgap = gap - buf;
xegap = egap - buf;
buflen = ebuf - buf;
newlen = buflen + n * sizeof (t_char);
if (buflen == 0) {
if (newlen < 0 || MAX_SSIZE_T < newlen)
fatal(f_alloc);
new = (t_char*) malloc((size_t) newlen);
if (new == NULL)
/* Cannot edit a file without a buffer. */
fatal(f_alloc);
} else {
if (newlen < 0 || MAX_SSIZE_T < newlen) {
msg(m_alloc);
return (FALSE);
}
new = (t_char*) realloc(buf, (size_t) newlen);
if (new == NULL) {
/* Report non-fatal error. */
msg(m_alloc);
return (FALSE);
}
}
/* Relocate pointers in new buffer and append the new
* extension to the end of the gap.
*/
buf = new;
gap = buf + xgap;
ebuf = buf + buflen;
egap = buf + newlen;
while (xegap < buflen--)
*--egap = *--ebuf;
ebuf = buf + newlen;
assert(buf < ebuf); /* Buffer must exist. */
assert(buf <= gap);
assert(gap < egap); /* Gap must grow only. */
assert(egap <= ebuf);
return (TRUE);
}
/*
*
*/
t_point
movegap(offset)
t_point offset;
{
t_char *p = ptr(offset);
while (p < gap)
*--egap = *--gap;
while (egap < p)
*gap++ = *egap++;
assert(gap <= egap);
assert(buf <= gap);
assert(egap <= ebuf);
return (pos(egap));
}
/*
* Return pointer on range buf <= ptr(offset) <= ebuf.
*/
t_char *
ptr(offset)
register t_point offset;
{
register t_char *cp;
if (offset < 0)
return (buf);
cp = buf + offset;
if (gap <= cp)
cp += (unsigned long)(egap - gap);
if (ebuf <= cp)
return (ebuf);
return (cp);
}
/*
* Return offset on range 0 <= pos(pointer) <= pos(ebuf).
*/
t_point
pos(cp)
register t_char *cp;
{
register t_point offset;
offset = (t_point)(cp - buf);
if (cp < buf)
return (0);
if (ebuf <= cp)
cp = ebuf;
if (egap <= cp)
offset -= (unsigned long)(egap - gap);
return (offset);
}
/*
* Return the current region.
*/
void
getregion(rp)
t_region *rp;
{
if (marker == NOMARK) {
rp->left = rp->right = point;
} else if (point <= marker) {
rp->left = point;
rp->right = marker-1;
} else {
rp->left = marker;
rp->right = point-1;
}
}
int
posix_file(fn)
char *fn;
{
if (fn[0] == '\0' || fn[0] == '_')
return (FALSE);
#ifdef DRIVE_COLON
if (fn[1] == ':') {
if (!isalpha(fn[0]))
return (FALSE);
fn += 2;
}
#endif /* DRIVE_COLON */
for (; *fn != '\0'; ++fn) {
if (!isalnum(*fn) && *fn != '.' && *fn != '_' && *fn != '-'
#ifdef EITHER_SLASH
&& *fn != '/' && *fn != '\\')
#else
&& *fn != '/')
#endif /* EITHER_SLASH */
return (FALSE);
}
return (TRUE);
}
/*
*
*/
int
save(fn)
char *fn;
{
int fd;
t_point llen, rlen;
t_char *lhalf, *rhalf;
if (!posix_file(fn)) {
msg(m_badname);
return (FALSE);
}
if ((fd = open(fn, O_WRONLY|O_CREAT|O_TRUNC, FILE_MODE)) < 0) {
msg(m_open, fn);
return (FALSE);
}
if (marker == NOMARK || point == marker) {
lhalf = buf;
llen = (t_point) (gap - buf);
rhalf = egap;
rlen = (t_point) (ebuf - egap);
} else {
/* When saving only a block of text, we'll move the gap
* to the start of the block, and set the left-half to
* zero length. We should never see the error message
* in the saved block of text.
*/
llen = 0;
lhalf = (t_char *) m_error;
if (point < marker) {
(void) movegap(point);
rlen = marker - point;
} else {
(void) movegap(marker);
rlen = point - marker;
}
rhalf = egap;
}
if (write(fd, lhalf, (size_t) llen) != llen
|| write(fd, rhalf, (size_t) rlen) != rlen) {
msg(m_write, fn);
return (FALSE);
}
if (close(fd) != 0) {
msg(m_close, fn);
return (FALSE);
}
if (lhalf != (t_char *) m_error)
modified = FALSE;
msg(m_saved, fn, llen + rlen);
return (TRUE);
}
/*
*
*/
int
load(fn)
char *fn;
{
int fd;
SSIZE_T len;
struct stat sb;
if (stat(fn, &sb) < 0) {
msg(m_stat, fn);
return (FALSE);
}
if (MAX_SSIZE_T < sb.st_size) {
msg(m_toobig, fn);
return (FALSE);
}
if (egap-gap < sb.st_size * sizeof (t_char) && !growgap(sb.st_size))
return (FALSE);
if ((fd = open(fn, O_RDONLY)) < 0) {
msg(m_open, fn);
return (FALSE);
}
point = movegap(point);
undoset();
len = read(fd, gap, (size_t) sb.st_size);
if (close(fd) != 0) {
msg(m_close, fn);
return (FALSE);
}
if (len < 0) {
msg(m_read, fn);
return (FALSE);
}
gap += len;
modified = TRUE;
msg(m_loaded, fn, len);
return (TRUE);
}
/*
* Record a new undo location.
*/
void
undoset()
{
ubuf.u_set = TRUE;
ubuf.u_point = point;
ubuf.u_gap = gap - buf;
ubuf.u_egap = ebuf - egap;
}
/*
* Undo.
*/
void
undo()
{
t_undo tmp;
if (ubuf.u_set) {
memcpy(&tmp, &ubuf, sizeof (t_undo));
undoset();
point = tmp.u_point;
gap = buf + tmp.u_gap;
egap = ebuf - tmp.u_egap;
} else {
msg(m_undo);
}
}
|