This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
forked from koeppl/bvi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dosio.c
464 lines (413 loc) · 9.3 KB
/
dosio.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* DOSIO.C - file I/O and alloc subroutines for MSDOS - BVI
*
* 1996-02-28 V 1.0.0
* 1998-04-12 V 1.0.1
* 1999-01-14 V 1.1.0
* 1999-04-27 V 1.1.1
* 1999-07-02 V 1.2.0 beta
* 1999-09-01 V 1.2.0 final
* 2000-05-02 V 1.3.0 alpha
*
* NOTE: Edit this file with tabstop=4 !
*
* Copyright 1996-2002 by Gerhard Buergmann
* Gerhard.Buergmann@puon.at
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* See file COPYING for information on distribution conditions.
*/
#include <conio.h>
#include <alloc.h>
#include <fcntl.h>
#include <bios.h>
#include <io.h>
#include <mem.h>
#include "bvi.h"
#include "set.h"
char *terminal = "ansi";
extern maxy;
struct WINDOW scr;
int stdscr = 0;
int COLS = 80;
int LINES = 25;
int ECHO = TRUE;
int NODEL = FALSE;
static struct stat buf;
int filemode;
int inch(void);
void
attrset(int attr)
{
switch(attr) {
case A_NORMAL: textcolor(P(P_CO) & 0x07);
textbackground((P(P_CO) & 0xf0) >> 4);
break;
case A_BOLD: textcolor((P(P_CO) & 0x07) | 0x08);
textbackground((P(P_CO) & 0xf0) >> 4);
break;
case A_REVERSE: textcolor((P(P_CO) & 0xf0) >> 4);
textbackground(P(P_CO) & 0x0f);
break;
case A_BLINK: textcolor((P(P_CO) & 0xf0) >> 4);
textbackground(P(P_CO) & 0x0f);
break;
}
}
int
getch()
{
int x;
if (NODEL) if (!kbhit()) return(ERR);
x = bioskey(0);
if ((x & 0xff) == 0) return(x);
else {
x &= 0xff;
if (ECHO) putch(x);
return(x); }
}
void
delch()
{
int x, y;
x = wherex(); y = wherey();
movetext(x + 1, y, COLS - x, y, x, y);
gotoxy(COLS, y);
putch(' ');
gotoxy(x, y);
}
void
insch(int c)
{
char line[80];
int x, y;
x = wherex(); y = wherey();
gettext(x, y, COLS - x, y, line);
puttext(x + 1, y, COLS - x + 1, y, line);
putch(c);
}
int
inch()
{
return peek(0xb800, (wherex() - 1) + (wherey() - 1) * COLS);
}
int
mvinch(int y, int x)
{
gotoxy(x + 1, y + 1);
return inch();
}
/*********** Save the patched file ********************/
int
save(char *fname, PTR start, PTR end, int flags)
{
int fd;
char string[255];
char *newstr;
off_t written;
off_t filesize;
unsigned n, to_write;
if (!fname) {
emsg("No file|No current filename");
return 0;
}
if (stat(fname, &buf) == -1) {
newstr = "[New file] ";
} else {
if (S_ISDIR(buf.st_mode)) {
sprintf(string, "\"%s\" Is a directory", fname);
msg(string);
return 0;
}
newstr = "";
}
if (filemode == PARTIAL) flags = O_RDWR;
if ((fd = open(fname, flags, 0666)) < 0) {
sysemsg(fname);
return 0;
}
if (filemode == PARTIAL) {
if (block_read) {
filesize = block_read;
sprintf(string, "\"%s\" range %lu-%lu", fname,
(unsigned long)block_begin,
(unsigned long)(block_begin - 1 + filesize));
if (lseek(fd, block_begin, SEEK_SET) < 0) {
sysemsg(fname);
return 0;
}
} else {
msg("Null range");
return 0;
}
} else {
filesize = end - start + 1L;
sprintf(string, "\"%s\" %s%lu@bytes", fname, newstr, (long)filesize);
}
written = 0;
do {
to_write = (filesize - written) > 0xfffe ? 0xfffe : (filesize - written);
if ((n = write(fd, start + written, to_write)) == 0xffff) {
sysemsg(fname);
close(fd);
return(0L);
}
written += (off_t)n;
} while (written < filesize);
close(fd);
edits = 0;
msg(string);
return 1;
}
off_t
load(char *fname)
{
int fd = -1;
char string[MAXCMD];
unsigned chunk, n;
buf.st_size = filesize = 0L;
if (fname != NULL) {
if (stat(fname, &buf) == -1) {
filemode = NEW;
} else if (S_ISDIR(buf.st_mode)) {
filemode = DIRECTORY;
} else if (S_ISREG(buf.st_mode)) {
if ((fd = open(fname, O_RDONLY|O_BINARY)) > 0) {
filemode = REGULAR;
if (access(fname, 2)) {
P(P_RO) = TRUE;
params[P_RO].flags |= P_CHANGED;
}
} else {
sysemsg(fname);
filemode = ERROR;
}
}
} else {
filemode = NEW;
}
if (mem != NULL) farfree(mem);
if (block_flag) {
memsize = block_size + 1000;
} else if (filemode == REGULAR) {
memsize = buf.st_size + 100;
} else {
memsize = 1000;
}
if (farcoreleft() < memsize) {
move(maxy, 0);
endwin();
printf("\n\nOut of memory\n");
exit(0);
}
mem = (char huge *)farmalloc(memsize);
clear_marks();
if (block_flag && (filemode == REGULAR)) {
if (lseek(fd, block_begin, SEEK_SET) < 0) {
sysemsg(fname);
filemode = ERROR;
} else {
chunk = block_size > 0xfffe ? 0xfffe : block_size;
do {
if ((n = read(fd, mem + filesize, chunk)) == 0xffff) {
sysemsg(fname);
filemode = ERROR;
break;
}
filesize += (off_t)n;
} while (filesize < buf.st_size);
if ((filesize == 0) {
sprintf(string, "\"%s\" No such range: %lu-%lu", fname,
(unsigned long)block_begin, (unsigned long)(block_end));
} else {
sprintf(string, "\"%s\" range %lu-%lu", fname,
(unsigned long)block_begin,
(unsigned long)(block_begin + filesize - 1));
}
filemode = PARTIAL;
block_read = filesize;
msg(string);
P(P_OF) = block_begin;
params[P_OF].flags |= P_CHANGED;
}
} else if (filemode == REGULAR) {
chunk = buf.st_size > 0xfffe ? 0xfffe : buf.st_size;
do {
if ((n = read(fd, mem + filesize, chunk)) == 0xffff) {
sysemsg(fname);
filemode = ERROR;
break;
}
filesize += (off_t)n;
} while (filesize < buf.st_size);
}
if (fd > 0) close(fd);
if (filemode != REGULAR) {
filesize = 0L;
}
if (fname != NULL) {
switch (filemode) {
case NEW:
sprintf(string, "\"%s\" [New File]", fname);
break;
case REGULAR:
sprintf(string, "\"%s\" %s%lu bytes", fname,
P(P_RO) ? "[Read only] " : "", (long)filesize);
break;
case DIRECTORY:
sprintf(string, "\"%s\" Directory", fname);
break;
}
if (filemode != ERROR) msg(string);
}
pagepos = mem;
maxpos = (PTR)(mem + filesize);
loc = HEX;
x = AnzAdd; y = 0;
repaint();
return(filesize);
}
int
addfile(char *fname)
{
int fd;
off_t oldsize;
unsigned chunk, n;
if (stat(fname, &buf)) {
sysemsg(fname);
return 1;
}
if ((fd = open(fname, O_RDONLY)) == -1) {
sysemsg(fname);
return 1;
}
oldsize = filesize;
if (enlarge(buf.st_size)) return 1;
chunk = buf.st_size > 0xfffe ? 0xfffe : buf.st_size;
do {
if ((n = read(fd, mem + filesize, chunk)) == 0xffff) {
sysemsg(fname);
filemode = ERROR;
return 1;
}
filesize += (off_t)n;
} while (filesize < buf.st_size);
maxpos = mem + filesize;
close(fd);
setpage(mem + oldsize);
return 0;
}
void
bvi_init(char *dir)
{
char *poi;
char *initstr;
char rcpath[255];
shell = getenv("COMSPEC");
if (shell == NULL || *shell == '\0')
shell = "COMMAND.COM";
strcpy(rcpath, dir);
poi = strrchr(rcpath, '\\');
*poi = '\0';
strcat(rcpath, "\\BVI.RC");
if ((initstr = getenv("BVIINIT")) != NULL) {
docmdline(initstr);
}
read_rc("BVI.RC");
read_rc(rcpath);
}
int
enlarge(off_t add)
{
PTR newmem;
off_t savecur, savepag, savemax, saveundo;
savecur = curpos - mem;
savepag = pagepos - mem;
savemax = maxpos - mem;
saveundo = undo_start - mem;
newmem = (PTR)farrealloc(mem, memsize + add);
if (newmem == NULL) {
emsg("Out of memory");
return 1;
}
mem = newmem;
memsize += add;
curpos = mem + savecur;
pagepos = mem + savepag;
maxpos = mem + savemax;
undo_start = mem + saveundo;
current = curpos + 1;
return 0;
}
void
do_shell()
{
system("");
}
off_t
alloc_buf(off_t n, char **buffer)
{
if ((*buffer = (char *)farrealloc(*buffer, n)) == NULL) {
emsg("No buffer space available");
return 0L;
}
return n;
}
void
d_memmove(PTR dest, PTR src, off_t n)
{
unsigned len;
long chunk;
PTR source;
PTR destin;
chunk = n;
if (dest < src) {
/* copy forward */
source = src;
destin = dest;
while (chunk > 0L) {
len = chunk > 0x7ffe ? 0x7ffe : chunk;
movmem(source, destin, len);
chunk -= len;
source += len;
destin += len;
}
} else {
/* copy backward */
source = src + n;
destin = dest + n;
while (chunk > 0L) {
len = chunk > 0x7ffe ? 0x7ffe : chunk;
chunk -= len;
source -= len;
destin -= len;
movmem(source, destin, len);
}
}
}
void
d_memcpy(PTR dest, PTR src, off_t n)
{
unsigned len;
long chunk;
PTR source;
PTR destin;
source = src;
destin = dest;
chunk = n;
while (chunk > 0L) {
len = chunk > 0x7ffe ? 0x7ffe : chunk;
movmem(source, destin, len);
chunk -= len;
source += len;
destin += len;
}
}