forked from ratboy666/fbasic
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfbasic.c
More file actions
5798 lines (5225 loc) · 120 KB
/
Copy pathfbasic.c
File metadata and controls
5798 lines (5225 loc) · 120 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* fbasic.c
*
* A very simple BASIC interpreter. Quite compatible with Microsoft
* MBASIC (CP/M) and GWBASIC (MS-DOS).
*
* Requires garbage collecting malloc (no free's are done)
* There are three areas where memory can be reclaimed
* - random file buffers
* - string temporaries
* - variables that have been unlinked
* Currently uses the Boehm-Demers-Weiser conservative garbage
* collector.
*
* Building:
*
* gcc -DUNALIGNED_DOUBLES=1 -Os -o fbasic fbasic.c -lcurses -lm -lgc
*
* Note that the 64 bit executable is 60K bytes... the original
* MBASIC.COM is 20K, and is written in assembler, so we are not
* far off the mark.
*
* Changes: 2.2 - Zero-trip FOR/NEXT rather than one-trip
* - Reformat braces
* - WIDTH 255 as per MBASIC 5.21
* - Skip OPTION statements. We currently do
* OPTION BASE 0, and ZEROTRIP FOR/NEXT loops
*
* Planned Changes:
* - OPTION BASE (0) 1
* - OPTION ZEROTRIP (ON) OFF
* - Variables are auto-dimensioned to 10
* - OPTION CRLF ON (OFF)
* - Tolerate double-precision syntax
*
* Copyright (c) 2000, 2001, 2022, Fridtjof Weigel
* Under MIT License
*/
/* Standard headers */
/* Comment this out to build under Solaris */
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <curses.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/wait.h>
extern char **environ;
/* Garbage collecting malloc */
#include "gc.h"
/* FBASIC version */
#define VERSION "2.2"
/* Set to 1 if your machine supports unaligned access to doubles */
/* (should be set to 1 for maximum efficiency on Intel, must be set to 0
* on Sparc)
*/
#ifndef UNALIGNED_DOUBLES
#define UNALIGNED_DOUBLES 0
#endif
/* Set to 1 to use cr/lf, not just lf */
#ifndef CRLF
#define CRLF 0
#endif
/* Maximum array dimensions */
#define MAX_DIMS 64
/* Maximum variables to cache */
#define MAX_CACHE 5
/* Convenience defines */
#define FOREVER for(;;)
#define NOTHING
#define YES 1
#define NO 0
/* This program reads and write mis-aligned doubles. All such transfers
* are done through the R() and W() macros, to allow correction for
* systems that don't support this.
*/
#if UNALIGNED_DOUBLES
/* Unaligned double access permitted */
#define R(x) (*(double *)(x))
#define W(x,v) (*(double *)(x) = (v))
#else
/* Read unaligned double */
static double R(unsigned char *x) {
double v;
/* If the pointer is aligned on 8 byte boundary, access directly */
if ((sizeof(double) == 8) &&
(((long int)x & 0x7) == 0))
return *(double *)x;
memcpy(&v, x, sizeof(double));
return v;
}
/* Write unaligned double */
static void W(unsigned char *x, double v) {
/* If the pointer is aligned on 8 byte boundary, access directly */
if ((sizeof(double) == 8) &&
(((long int)x & 0x7) == 0))
*(double *)x = v;
else
memcpy(x, &v, sizeof(double));
}
#endif
/* 1K byte */
#define K * 1024
/* Maximum program size */
#define MAX_PROGRAM (256 K)
/* Maximum source line length */
#define MAX_SRC 1024
/* Maximum token size */
#define MAX_TKN (MAX_SRC * 9)
/* BASIC string (length + body) */
typedef struct {
int length;
char *storage;
} basic_string;
/* Token */
typedef struct {
char *name;
unsigned char tval;
} token;
/* Token values */
#define ABS 1
#define ASC 2
#define ATN 3
#define CHAIN 4
#define MERGE 5
#define ALL 6
#define CHR 7
#define CINT 8
#define CLEAR 9
#define CLOSE 10
#define COMMON 11
#define COS 12
#define CVL 13
#define CVD 14
#define CVI 15
#define CVS 16
#define DATA 17
#define DATE 18
#define DEF 19
#define BIN 20
#define FREEFILE 21
#define FREE 22
#define FIX 23
#define DELETE 24
#define DIM 25
#define END 26
#define ENVIRONS 27
#define ENVIRON 28
#define EOFF 29
#define ERASE 30
#define ERL 31
#define ERROR 32
#define ERRF 33
#define EXP 34
#define FIELD 35
#define AS 36
#define FOR 37
#define TO 38
#define STEP 39
#define GET 40
#define GOSUB 41
#define GOTO 42
#define HEX 43
#define IF 44
#define THEN 45
#define ELSE 46
#define INPUT 47
#define INSTR 48
#define INTF 49
#define KILL 50
#define LEFT 51
#define LEN 52
#define LET 53
#define LINE 54
#define LIST 55
#define LOF 56
#define LOG 57
#define LSET 58
#define MID 59
#define MKD 60
#define MKI 61
#define MKS 62
#define NAME 63
#define NEW 64
#define NEXT 65
#define OCT 66
#define ON 67
#define OPEN 68
#define CURSESF 69
#define LOCATE 70
#define CLS 71
#define BEEP 72
#define SLEEP 73
#define POS 74
#define PRINT 75
#define USING 76
#define PUT 77
#define RANDOMIZE 78
#define READ 79
#define REM 80
#define RESTORE 81
#define RETURN 82
#define RIGHT 83
#define RND 84
#define RSET 85
#define RUN 86
#define SAVE 87
#define SGN 88
#define SIN 89
#define SPACE 90
#define SPC 91
#define SQR 92
#define STOP 93
#define STRF 94
#define STRINGF 95
#define SWAP 96
#define SYSTEM 97
#define TAB 98
#define TAN 99
#define TIME 100
#define TIMER 101
#define TROFF 102
#define TRON 103
#define VAL 104
#define WEND 105
#define WHILE 106
#define WIDTH 107
#define WRITE 108
#define CHDIR 109
#define MKDIR 110
#define RMDIR 111
#define AUTO 112
#define CSNG 113
#define CONT 114
#define SEG 115
#define EDIT 116
#define CSRLIN 117
#define FILES 118
#define INKEY 119
#define INPUTS 120
#define INP 121
#define LLIST 122
#define LPOS 123
#define LPRINT 124
#define NULLF 125
#define OUT 126
#define PEEK 127
#define POKE 128
#define RENUM 129
#define RESUME 130
#define OFF 131
#define VARPTR 132
#define WAIT 133
#define EQUAL 134
#define PLUS 135
#define DASH 136
#define STAR 137
#define SLASH 138
#define HAT 139
#define COLON 140
#define HASH 141
#define COMMA 142
#define OPENP 143
#define CLOSEP 144
#define LOAD 145
#define LOC 146
#define AND 147
#define OR 148
#define NOT 149
#define XOR 150
#define IMP 151
#define EQV 152
#define MOD 153
#define NE 154
#define LE 155
#define GE 156
#define LT 157
#define GT 158
#define IDIV 159
#define SEMI 160
#define MKL 161
#define INT 162
#define SNG 163
#define FN 164
#define CDBL 165
#define STR 166
#define DBL 167
#define VARS 168
#define COLOR 169
#define LCASE 170
#define UCASE 171
#define LTRIM 172
#define RTRIM 173
#define INTERPRET 174
#define OPTION 175
/* Maximum token value */
#define TOKEN_MAX 200
/* Token table (name/value) */
static token token_table[] = {
{ "ABS(", ABS }, { "ASC(", ASC }, { "ATN(", ATN }, { "CHAIN", CHAIN },
{ "MERGE", MERGE }, { "ALL", ALL }, { "CHR$(", CHR }, { "CINT(", CINT },
{ "CLEAR", CLEAR }, { "CLOSE", CLOSE }, { "COMMON", COMMON },
{ "COS(", COS }, { "CVL(", CVL }, { "CVD(", CVD }, { "CVI(", CVI },
{ "CVS(", CVS }, { "DATA", DATA }, { "DATE$", DATE }, { "DEF", DEF },
{ "BIN$(", BIN }, { "FREEFILE", FREEFILE }, { "FREE", FREE },
{ "FIX(", FIX }, { "DELETE", DELETE }, { "DIM", DIM }, { "END", END },
{ "ENVIRON$(", ENVIRONS }, { "ENVIRON", ENVIRON }, { "EOF(", EOFF },
{ "ERASE", ERASE }, { "ERL", ERL }, { "ERROR", ERROR }, { "ERR", ERRF },
{ "EXP(", EXP }, { "FIELD", FIELD }, { "AS", AS }, { "FOR", FOR },
{ "TO", TO }, { "STEP", STEP }, { "GET", GET }, { "GOSUB", GOSUB },
{ "GOTO", GOTO }, { "HEX$(", HEX }, { "IF", IF }, { "THEN", THEN },
{ "ELSE", ELSE }, { "INPUT", INPUT }, { "INSTR(", INSTR },
{ "INT(", INTF }, { "KILL", KILL }, { "LEFT$(", LEFT }, { "LEN(", LEN },
{ "LET", LET }, { "LINE", LINE }, { "LIST", LIST }, { "LOF(", LOF },
{ "LOG(", LOG }, { "LSET", LSET }, { "MID$(", MID }, { "MKD$(", MKD },
{ "MKI$(", MKI }, { "MKS$(", MKS }, { "NAME", NAME }, { "NEW", NEW },
{ "NEXT", NEXT }, { "OCT$(", OCT }, { "ON", ON }, { "OPEN", OPEN },
{ "CURSES", CURSESF }, { "LOCATE", LOCATE }, { "CLS", CLS },
{ "BEEP", BEEP }, { "SLEEP", SLEEP }, { "POS(", POS },
{ "PRINT", PRINT }, { "?", PRINT }, { "USING", USING }, { "PUT", PUT },
{ "RANDOMIZE", RANDOMIZE }, { "READ", READ }, { "REM", REM },
{ "\'", REM }, { "RESTORE", RESTORE }, { "RETURN", RETURN },
{ "RIGHT$(", RIGHT }, { "RND(", RND }, { "RSET", RSET }, { "RUN", RUN },
{ "SAVE", SAVE }, { "SGN(", SGN }, { "SIN(", SIN },
{ "SPACE$(", SPACE }, { "SPC(", SPC }, { "SQR(", SQR },
{ "STOP", STOP }, { "STR$(", STRF }, { "STRING$(", STRINGF },
{ "SWAP", SWAP }, { "SYSTEM", SYSTEM }, { "TAB(", TAB },
{ "TAN(", TAN }, { "TIME$", TIME }, { "TIMER", TIMER },
{ "TROFF", TROFF }, { "TRON", TRON }, { "VAL(", VAL }, { "WEND", WEND },
{ "WHILE", WHILE }, { "WIDTH", WIDTH }, { "WRITE", WRITE },
{ "CHDIR", CHDIR }, { "MKDIR", MKDIR }, { "RMDIR", RMDIR },
{ "CONT", CONT }, { "SEG", SEG }, { "EDIT", EDIT }, { "CSRLIN", CSRLIN},
{ "FILES", FILES }, { "INKEY$", INKEY }, { "INPUT$(", INPUTS },
{ "INP(", INP }, { "LLIST", LLIST }, { "LPOS(", LPOS },
{ "LPRINT", LPRINT }, { "NULL", NULLF }, { "OUT", OUT },
{ "PEEK(", PEEK }, { "POKE", POKE }, { "RENUM", RENUM },
{ "RESUME", RESUME }, { "OFF", OFF }, { "VARPTR(", VARPTR },
{ "WAIT", WAIT }, { "=", EQUAL }, { "+", PLUS }, { "-", DASH },
{ "^", HAT }, { "**", HAT }, { "*", STAR }, { "/", SLASH },
{ ":", COLON }, { "#", HASH }, { ",", COMMA }, { "(", OPENP },
{ "[", OPENP }, { ")", CLOSEP }, { "]", CLOSEP }, { "LOAD", LOAD },
{ "LOC(", LOC }, { "AND", AND }, { "OR", OR },
{ "NOT", NOT }, { "XOR", XOR }, { "IMP", IMP }, { "EQV", EQV },
{ "MOD", MOD }, { "<>", NE }, { "><", NE }, { "<=", LE },
{ "=<", LE }, { ">=", GE }, { "=>", GE }, { "<", LT }, { ">", GT },
{ "\\", IDIV }, { ";", SEMI }, { "MKL$(", MKL }, { "INT", INT },
{ "SNG", SNG }, { "FN", FN }, { "STR", STR }, { "DBL", DBL },
{ "VARS", VARS }, { "COLOR", COLOR }, { "COLOUR", COLOR },
{ "CSNG(", CSNG }, { "CDBL(", CDBL }, { "AUTO", AUTO }, { "][", COMMA },
{ "LCASE$(", LCASE }, { "UCASE$(", UCASE },
{ "LTRIM$(", LTRIM }, { "RTRIM$(", RTRIM },
{ "INTERPRET", INTERPRET }, { "OPTION", OPTION },
{ NULL, 0 }
};
/* "High" tokens - variables, literals, end of line, etc. */
#define VARIABLE 255
#define STRING 254
#define START 253
#define LITERAL 252
#define EOL 251
#define EOP 250
#define CONSTANT 249
#define HEX_MODE 248
#define OCTAL_MODE 247
#define BINARY_MODE 246
#define IMPOSSIBLE 245
#define UNARY_MINUS 200
#define UNARY_PLUS 201
#define UNARY_NOT 202
/* Line number ranges (low..high and default) */
#define LOW_LN (0.0)
#define HIGH_LN (1.0e38)
#define DEFAULT_LN (-1.0)
/* Error values */
#define ERROR_SYNTAX 1 /* Syntax error */
#define ERROR_GOSUB 2 /* Gosub stack overflow */
#define ERROR_TYPE 3 /* Incorrect type */
#define ERROR_BREAK 4 /* Break */
#define ERROR_DATA 5 /* Out of data */
#define ERROR_NOLINE 6 /* Line number doesn't exist */
#define ERROR_WHILE 7 /* While stack overflow, nesting */
#define ERROR_FOR 8 /* For stack overflow, nesting */
#define ERROR_OM 9 /* Out of memory */
#define ERROR_REDIM 10 /* Redimension */
#define ERROR_MATH 11 /* Math error (/0, etc.) */
#define ERROR_DIM 12 /* Dimension */
#define ERROR_NOVAR 13 /* No such variable */
#define ERROR_IO 14 /* I/O error */
#define ERROR_NOEDIT 15 /* Can't edit */
#define ERROR_UNIMP 16 /* Not implemented */
/* Maximum nested gosubs, whiles and fors */
#define GOSUB_MAX 128
#define WHILE_MAX 128
#define FOR_MAX 128
/* File modes */
#define MODE_FREE 0
#define MODE_INPUT 1
#define MODE_OUTPUT 2
#define MODE_RANDOM 3
#define MODE_BINARY 4
#define MODE_APPEND 5
/* Special files */
#define CONSOLE -1
#define PRINTER -2
#define KEYBOARD -3
/* Maximum open files */
#define MAX_FD 256
/* File buffer descriptor */
struct filedesc {
char *buffer;
int mode;
int reclen;
int pos;
int width;
FILE *f;
};
/* Variable header (name, type, pointer to value) */
struct variable {
struct variable *next;
char *name;
int type;
int common;
void *descriptor;
};
/* Array header */
struct array {
int ndims;
double *dims;
int elems;
int type;
void *values;
};
/* Variable types */
#define NAME_ONLY 0
#define SIMPLE_SCALAR 1
#define SIMPLE_STRING 2
#define ARRAY 3
#define STRING_ARRAY 4
/* Global variables */
/* YES if editing not allowed (un-numbered program) */
static int noedit;
/* Width of console and line printer (file widths in descriptors) */
static int width, lwidth;
static int *pwidth;
/* Output has occured -- flush is needed */
static int cout_n;
/* For timer */
static time_t current_time;
static int ontimerflag;
static int secondsleft, ontimevalue;
static unsigned char *ontimer;
/* For error dispatch */
static double onerror;
static int inerror;
static unsigned char *current_loc;
/* Are we invoked as frun? */
static int frun;
/* NULL count for output */
static int nulls;
/* Global program arguments */
static int g_ac;
static char **g_av;
/* Curses */
static WINDOW *sc;
static int use_curses;
/* Our variables */
static struct variable *variables;
/* Current device description */
static int outdev;
static FILE *current_file;
static FILE *current_infile;
static int *ppos;
/* File descriptors */
static struct filedesc files[MAX_FD] = {{0}, };
/* Token buffer for tokenizing source code */
static unsigned char tkn_buf[MAX_TKN];
/* Source buffer */
static char src[MAX_SRC];
/* Our tokenized program */
static unsigned char *program;
/* Are we tracing line numbers ? */
static int ftrace;
/* Error code and error line */
static double erl;
static double err;
static double current_line;
static int running;
/* Expression operator stack */
static unsigned char opstack[MAX_SRC];
static int opstack_ptr;
/* Expression value stack */
static double valuestack[MAX_SRC / 2];
static int valuestack_ptr;
/* Error jump */
static jmp_buf jerror;
/* Local break jump (for breaking input) */
static int jbreakflag;
static jmp_buf jbreak;
/* Output to a string */
static char *coutp;
static int coutn;
static int coutmax;
/* Cache most recently used variables.
* The idea is to avoid lengthy searches through the variable list for
* variables used in a single segment.
* The variable cache must be flushed whenever names can be rebound
* (DEF functions).
*/
static struct variable *variable_cache[MAX_CACHE] = { 0, };
static int vci = 0;
/* GOSUB stack */
static unsigned char *gosubstack[GOSUB_MAX];
static int gosubstack_ptr;
/* WHILE stack */
static unsigned char *whilestack[WHILE_MAX];
static int whilestack_ptr;
/* flag indicating break (error) has occured */
static int breakflag;
/* DATA pointer */
static unsigned char *data;
static int pos; /* console position */
static int lpos; /* line printer position */
static int tab; /* tab-to location for print */
/* FOR stack */
static unsigned char *forstack[FOR_MAX];
static int forstack_ptr;
/* DEF SEG */
static int seg;
/* Variable types INT, SNG, STR, DBL, based on first character of name */
static char vtype[256] = { 0, };
/* User defined function pointers (DEF FNA..) */
static unsigned char *fns[256] = { 0, };
/* Numeric character sets */
static const char *b_set = "01";
static const char *o_set = "01234567";
static const char *t_set = "0123456789";
static const char *h_set = "0123456789ABCDEF";
/* Raise error */
static void error(int error) {
err = error;
erl = current_line;
longjmp(jerror, 1);
}
/* Allocate memory, pointers contained within */
static void *get_memory(int n) {
void *p;
p = GC_MALLOC(n);
if (p == NULL)
error(ERROR_OM);
return p;
}
/* Allocate memory, no pointers contained within */
static void *get_memory_atomic(int n) {
void *p;
p = GC_MALLOC_ATOMIC(n);
if (p == NULL)
error(ERROR_OM);
return p;
}
/* Reallocate memory */
static void *increase_memory(void *p, int n) {
p = GC_realloc(p, n);
if (p == NULL)
error(ERROR_OM);
return p;
}
/* Free memory */
static void free_memory(void *p) {
GC_free(p);
}
/* Force garbage collection */
static void garbage_collect(void) {
GC_gcollect();
}
#if 0
/* Return number of garbage collections */
static int n_gc(void) {
return GC_gc_no;
}
#endif
/* Return heap size, just data */
static int heap_size(void) {
return GC_get_heap_size() - MAX_PROGRAM;
}
/* Check filenumber range */
static void check_fn(int filenumber) {
if ((filenumber < 0) || (filenumber >= MAX_FD))
error(ERROR_IO);
}
/* Return free file */
static int freefile(void) {
int i;
for (i = 1; i < MAX_FD; ++i)
if (files[i].mode == MODE_FREE)
return i;
error(ERROR_IO);
return -1;
}
/* Open a file */
static void open_file(int mode, int filenumber, char *filename, int reclen) {
char *m = "r";
check_fn(filenumber);
if (files[filenumber].mode != MODE_FREE)
error(ERROR_IO);
switch (mode) {
case MODE_INPUT: m = "r"; break;
case MODE_OUTPUT: m = "w"; break;
case MODE_APPEND: m = "a+"; break;
case MODE_BINARY: m = "r+b"; break;
case MODE_RANDOM:
files[filenumber].reclen = reclen;
files[filenumber].buffer = get_memory_atomic(reclen);
m = "r+b";
break;
}
files[filenumber].f = fopen(filename, m);
if (files[filenumber].f == NULL) {
if (mode == MODE_RANDOM) {
free_memory(files[filenumber].buffer);
files[filenumber].buffer = NULL;
}
error(ERROR_IO);
}
files[filenumber].mode = mode;
files[filenumber].pos = 0;
files[filenumber].width = 0;
}
/* Close a file */
static void close_file(int filenumber) {
check_fn(filenumber);
if (files[filenumber].mode == MODE_FREE)
return;
if (files[filenumber].mode == MODE_RANDOM)
free_memory(files[filenumber].buffer);
files[filenumber].buffer = NULL;
files[filenumber].mode = MODE_FREE;
fclose(files[filenumber].f);
files[filenumber].f = NULL;
}
/* Return FILE* for file */
static FILE *pfile(int filenumber) {
FILE *f;
check_fn(filenumber);
f = files[filenumber].f;
if (f == NULL)
error(ERROR_IO);
return f;
}
/* Select file for i/o */
static void select_file(int filenumber) {
coutp = NULL;
if (outdev == filenumber)
return;
outdev = filenumber;
if (filenumber == CONSOLE) {
current_file = stdout;
current_infile = stdin;
ppos = &pos;
pwidth = &width;
} else if (filenumber == PRINTER) {
current_file = stderr;
current_infile = stdin;
ppos = &lpos;
pwidth = &lwidth;
} else {
current_file = pfile(filenumber);
current_infile = current_file;
ppos = &(files[filenumber].pos);
pwidth = &(files[filenumber].width);
}
}
/* Refresh for curses */
static void refresh_screen(void) {
if (use_curses)
refresh();
}
/* Flush output */
static void flush_out(void) {
if (cout_n) {
cout_n = 0;
fflush(stdout);
refresh_screen();
}
}
/* Check for BREAK */
static void check_break(void) {
if (breakflag) {
breakflag = 0;
error(ERROR_BREAK);
}
}
/* Get key */
static int get_key(void) {
unsigned char key;
flush_out();
while (read(1, &key, 1) < 0)
check_break();
check_break();
return key;
}
/* Get line */
static void get_line(char *s, int n) {
int x, y;
s[0] = '\0';
if (use_curses && (current_infile == stdin)) {
if (setjmp(jbreak) == 0) {
jbreakflag = YES;
/* getnstr(s,n) is not available on Solaris */
wgetnstr(sc, s, n);
/* Needed for Solaris - if an empty string is accepted
* by wgetnstr on Solaris, the cursor position is not
* changed.
*/
}
jbreakflag = NO;
echo();
nocbreak();
getyx(sc, y, x);
move(y, x);
refresh();
check_break();
return;
}
if (fgets(s, n, current_infile) == NULL) {
check_break();
error(ERROR_IO);
}
check_break();
}
/* Output character */
static void crlf(void);
static void cout(char c) {
if (coutp) {
if (coutn >= coutmax) {
coutmax += 1024;
coutp = increase_memory(coutp, coutmax);
}
coutp[coutn++] = c;
return;
}
++cout_n;
if (use_curses && (current_file == stdout)) {
if (addch(c) == ERR)
error(ERROR_IO);
/* WIDTH not effective in CURSES mode */
return;
}
if (putc(c, current_file) == EOF)
error(ERROR_IO);
if (c == 8)
--*ppos;
else if (c == 13)
*ppos = 0;
else if (c == 10)
*ppos = 0;
else if (c != 7)
++*ppos;
if ((*pwidth > 0) && (*ppos >= *pwidth))
crlf();
}
/* String out */
static void sout(char *s) {
while (*s)
cout(*s++);
}
/* ANSI sequence to clear screen and home cursor */
static void ansi_cls(void) {
sout("\x1b[1;1H\x1b[J");
}
/* Output return,newline */
static void crlf(void) {
int i;
#if CRLF
sout("\r\n");
#else
cout('\n');
#endif
for (i = 0; i < nulls; ++i)
cout(0);
}
/* Output return,newline for edit */
static void ncrlf(void) {
if (use_curses)
cout('\n');
else
sout("\r\n");
}
/* Execute program with pty */
static void pty(char *cmd) {
int i;
char data;
int master = 0, slave;
pid_t pid_sh;
#if 0
/* BSD style pty -- Linux 2.0 needs this */
char mastername[32], slavename[32];
char *s, *t;
int found;
found = NO;
for (s = "pqrs"; !found && *s; ++s) {
for (t = "0123456789abcdef"; !found && *t; ++t) {
sprintf(mastername, "/dev/pty%c%c", *s, *t);
sprintf(slavename, "/dev/tty%c%c", *s, *t);
if ((master = open(mastername, O_RDWR|O_NDELAY|O_SYNC))
>= 0)
found = YES;
}
}
if (!found) {
fprintf(stderr, "all ptys in use\n");
return;
}
#else
/* SVR4 style pty */
char *slavename;