-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg.c
12005 lines (10353 loc) · 240 KB
/
g.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
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
/*
* ----------( G E D I T O R )----------
*
* Copyright (C) 1995, Jeremy Hall
*
* Please send comments, fixes, problems etc to: [email protected]
*
* Last delta: 95/10/20 15:24:55
*
* To compile G for UNIX
* ---------------------
*
* HP-UX 9/10
* cc -O -Ae -s +Oall +Oaggressive +ESlit -D_XPG4 +u4 -o g -n \
* -Wl,-a,archive g.c -lcur_colr
* ICL DRS/NX sparc/Intel & UnixWare
* cc -Xc -O g.c -dn -s -o g -lcurses; mcs -d g
* Solaris Intel
* cc -Xc -xO3 -Di386=1 g.c -dn -s -o g -lcurses; mcs -d g
* Solaris Sparc
* cc -Xc -xO3 -dalign g.c -dn -s -o g -lcurses; mcs -d g
* IBM AIX 3.25/4.12
* cc -O3 -qflag=e:e -qcompact -qro -qroconst -s -o g g.c -lcurses
* DG/UX AViiON 4.1 Motorola
* cc -Xc -O2 -D_USING_SYSV4_OR_DGUX g.c -dn -s -o g -lcurses; mcs -d g
* Pyramid DC/OSx
* cc -Xc -WM,-mips2 -WM,-O3 -Wf,-Cs g.c -dn -s -o g -lcurses; mcs -d g
* SCO 5
* cc -O -Xc -s -o g g.c -lcurses; mcs -d g
* Linux 1.2.8
* gcc -O2 -m486 -s -I/usr/include/ncurses g.c -o g -lncurses
*
* add -DDEBUG to enable checks, and -DCOLOUR=0 for some older UNIX's.
*
* To compile G for DOS (Watcom C 10, 16bit real mode)
* --------------------
*
* Startup code: wasm crt0 -bt=DOS -ml -2r_
*
* 386+ version: wcl -DDOS=1 -4 -7 -fp3 -ml -zp2 -oaosilm -s @g.lnk \
* -fe=g.exe crt0.obj g.c
*
* 286 version: wcl -DDOS=1 -DASM86=0 -2 -7 -fp2 -ml -zp2 -oaosilm -s \
* @g.lnk -fe=g.exe crt0.obj g.c
*
* 8086 version: wcl -DDOS=1 -DASM86=0 -0 -7 -ml -zp2 -oaosilm -s \
* @g.lnk -fe=g.exe crt0.obj g.c
*
* The above do: inline floating point, 2 byte struct alignment,
* large model, optimize for small size, and no stack check.
* g.lnk just has "option eliminate" in to get rid of unreferenced
* segments.
* crt0.asm is optional; for use with Watcom C only. Its 2k smaller and
* much faster than the default (uses stosd instead of stosb to zero
* the BSS region for example). If not used, -k8192 must be added to
* set the stack size.
*
* g.c also compiles OK with Microsoft Visual C/C++ and Borland, with
* a couple of trivial changes - look for: MSVC in comments.
* The 16 bit DOS version needs the large memory model and 8K of stack.
*
* Status
* ------
* Although G is FreeWare - I would greatly appreciate a short e-mail if
* you use it and will be very interested in any comments or suggestions.
* Thanks!
* You are also welcome to email questions or problems to me at the address
* below, but I can't guarantee an instant response.
*
*
* Please forward any source changes for ports, bug fixes, or modifications
* to me so that improvements may be passed on to all in future releases.
*
* You may copy G as often as you wish or give it to anyone who wants it.
* Please do not ever sell G although you may of course charge a small
* amount of money to cover materials and handling.
*
* G IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND. I CANNOT BE
* HELD RESPONSIBLE FOR ANY DAMAGE OR LOSS, CONSEQUENTIAL OR OTHERWISE,
* ARISING OUT OF THE USE OF, OR INABILITY TO USE, G.
*
* History
* -------
* First implementation of context editor kernel by Roger McCalman and
* Robert Ash, October 1981 - February 1982, at Aberystwyth University.
* Original in 'B' on Honeywell-6000/GCOS/TSS.
* All further development by Jeremy Hall. Reading, England.
* Ported to UNIX (Amdahl UTS/V), August 1984 - May 1985. In the
* process it was translated to 'C' and re-written to include an
* in-core file-system, Regular Expressions, arithmetic etc,
* Ported to ICL DRS300 (iAPX286) CDOS 4.1, January 1987.
* Screen editor implemented April 1987 ...
* Ported to ICL DRS300 Unix (DRS/NX - SVR2 286), October 1987.
* .. SUN 3 Unix BSD 4.2, February 1988, (CE only).
* .. DRS300 CDOS 5.2, July 1988.
* .. Tulip (386) Unix SVR3, September 1988.
* .. DRS300 Unix (DRSNX - SVR3 386), March 1989.
* Kernal fully compiled with separate syntax analyser, July 1989.
* Last CDOS build, March 1989 and CDOS support removed, 1/09/89.
* Ported to IBM PC MSDOS 3.3, Colour VGA etc, (ANSI C), January 1990.
* .. UNIX SVR4 - sparc (ANSI C), i486, May 1990.
* .. Sun 4 SunOS (sparc), October 1991.
* .. UNIX SVR4.2 December 1992.
* Private malloc removed, linked VSAM finally complete, xenix support
* removed, special code for early terminal emulators removed, C++ port
* complete. All Jan 1993. Now compiles cleanly with C++ and ANSI C.
* Ported to DEC VAX/VMS 5.0, Feb 1993 (incomplete)
* .. Borland Turbo C with PDCurses 2.0 on DOS, Dec 1993.
* .. Coherant 3.2 64k i/d, hence CE only.
* .. Pyramid (MIPS 4400), DC/OSx 1.1, Mar 1994.
* Support for non-ANSI C comilers removed, now back in a single file.
* Ported to Solaris 2.3, Mar 1994.
* .. Watcom C/C++ 10 on DOS, Aug 1994.
* .. HP-UX (PA-RISC), Jan 1995
* .. IBM AIX (RS6000), Jan 1995
* .. DG/UX 5.4R3.10 AViiON mc88110, July 1995
* .. SCO 5, Aug 1995
* .. Linux 1.2.8, Sep 1995
* .. UnixWare, Sep 1995
* First distributed on the Internet, Sep 1995
*
* DOS version now uses a very small and fast subset of curses, mostly
* in assembler, included within this module. Memory moves and fill
* now done with dword instructions where appropriate. Screen attributes
* unfortunately now rather fixed, but screen updates very fast.
*/
#ifdef DOS
#define UNIX 0
#else
#define UNIX 1
#define DOS 0
#define _XOPEN_SOURCE 1
#define _POSIX_SOURCE 1
#endif
#undef NEVER
#ifndef TINY_G
#if DOS
#define TINY_G 1 /* small buffers etc for real mode DOS */
#else
#define TINY_G 0
#endif
#endif
#define FULL_G (TINY_G == 0)
#if DOS
#ifndef ASM86
#ifdef __WATCOMC__
#define ASM86 1
#else
#define ASM86 0
#endif
#endif
#else
#define near /* Empty */
#define ASM86 0
#endif
#if ASM86
#define B_SIZE 2
#define B_COLS h_inc
#else
#define B_SIZE 1
#define B_COLS COLS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h> /* comment this out for MSVC, or for Borland use io.h */
#if DOS
#include <direct.h>
#endif
#if UNIX /* not ANSI */
#include <sys/mman.h>
#ifndef caddr_t
typedef void * caddr_t;
#endif
#endif
/*
* Manifests for the George Editor.
*/
#define NUMERIC '0': case '1': case '2': case '3': case '4':\
case '5': case '6': case '7': case '8': case '9'
#define PNUMERIC '1': case '2': case '3': case '4':\
case '5': case '6': case '7': case '8': case '9'
#define XNUMERIC NUMERIC:\
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F'
#define EOS 0x00
#define SPACE 0x20
#define BSLASH 0x5C
#define SLASH 0x2F
#define GRAVE 0x60
#define SQUOTE 0x27
#define DQUOTE 0x22
#define TAB 0x09
#define ESC 0x1B
#define DEL 0x7F
#define LFEED 0x0A
#define CNTRL_MASK 037U
#define BYTE_MASK 0377U
#define ASCII_MASK 0177U
#define ODD_MASK 01U
#define delim(c) (g_map[c] & 01U)
#define comsep(c) (g_map[c] & 02U)
#define isdigit(c) (g_map[c] & 010U)
#define isupper(c) (g_map[c] & 020U)
#define se_b4key(c) (g_map[c] & 040U)
#define se_jkey(c) (g_map[c] & 0100U)
#define se_b1key(c) (g_map[c] & 0200U)
#define wordch(c) (g_map[c] & (04U | 010U | 020U))
#if DOS
#define path_sep(c) (c == SLASH || c == BSLASH)
#else
#define path_sep(c) (c == SLASH)
#endif
/* upper case is done by g_map */
#define tolower( c ) (isupper( c ) ? c + ('a' - 'A') : c)
/* Standard one treats Formfeed etc as space too */
#define isspace(c) ((c) == SPACE)
/* "punctuation" includes control characters here */
#define punctch(c) (!(wordch(c)||isspace(c)))
/* determine if char is valid ASCII */
#define isascii(c) (!((c) & ~ASCII_MASK))
/* convert char to ASCII 7 bit */
#define toascii(c) ((c) & ASCII_MASK)
/* control character literals */
#define CNTRL(c) (c & CNTRL_MASK)
#define iscntrl(c) (c < SPACE || c >= DEL)
#define isprint(c) (c >= SPACE && c < DEL)
/* case conversion */
#define u_star(p) u_map[ *(p) ]
#define TAB_WIDTH 8
#define YES 1
#define NO 0
#define G_OK 0
#define G_FAIL 1
#define byte unsigned char
#define ushort unsigned short
#define word unsigned int
#define real double
#define repeat for(;;)
#define elif else if
#define private static
#define csc const * const
#define cssc const ** const
#define skip_space(s) while( isspace( *(s) ) ) ++(s)
#define nullstr(s) (*(s) == EOS)
#define heap(t) (t*)getvec( sizeof( t ) )
#define getbuf(len) (char*)getvec( len )
#define equal(s1,s2) (strcmp( s1, s2 ) == 0)
#if DOS
#define equal1(s,c) (*(word*)s == c)
#else
#define equal1(s,c) (s[0] == c && s[1] == EOS)
#endif
/* length of temp strings */
#define STR_LEN 256
typedef char string[STR_LEN];
/* history, del/ins record stacks */
typedef int* stack;
#define plural(n) (n > 1 ? let_s : empty)
/* path/file names */
#if DOS
typedef char FNAME[_MAX_PATH];
#else
typedef char FNAME[STR_LEN];
#endif
/* Length of system provided date string */
#define DATE_LEN 24
#if ASM86
/* smaller or faster versions of standard functions */
extern void wfill( void *start, short val, int len );
extern void cwmovelr( void *dst, const void *src, short len );
extern void movelr( void *dst, const void *src, short len );
extern char *mmovelr( void *dst, const void *src, short len );
extern byte *cwmmovelr( void *dst, const void *src, short len );
extern void tmovelr( void *dst, const void *src, short len );
extern void movelrz( void *dst, const void *src, short len );
extern void cmovelr( void *dst, const void *src );
extern char *mcmovelr( void *dst, const void *src );
extern void zmovelr( void *dst, const void *src );
extern char *mzmovelr( void *dst, const void *src );
extern void wmovelr( void *dst, const void *src, short len );
extern void bmovelr( void *dst, const void *src, short len );
extern void wmoverl( void *dst, const void *src, short len );
extern void moverl( void *dst, const void *src, short len );
extern void space_fill( void *start, short len );
extern byte *mspace_fill( void *start, short len );
extern void bzero( void *start, short len );
extern int ecmp( const void *s1, const void *s2, ushort n );
extern int size( const void *s );
extern char *get_eos( const char *s );
extern char *tab_fill( void *start );
extern char *mmovelr4( void *dst, const void *src );
extern void movelr5( void *dst, const void *src );
extern char *mmovelr5( void *dst, const void *src );
extern void bios_wait( void );
extern void bmoverl( void *dst, const void *src, short len );
#pragma aux bios_wait = "xor ah,ah" "int 16h" modify [ah] ;
#pragma aux get_eos = "xor al,al" "or cx,-1" "repne scasb" "dec di" \
parm [es di] value [es di] modify [cx al] ;
#pragma aux wfill = "rep stosw" parm [es di] [ax] [cx] ;
#pragma aux movelr = "rep movsb" parm [es di] [ds si] [cx] ;
#pragma aux mmovelr = "rep movsb" parm [es di] [ds si] [cx] value [es di] ;
#pragma aux cwmovelr = "shr cx,1" "rep movsw" "jnc nocp" "movsb" "nocp:" \
parm [es di] [ds si] [cx] ;
#pragma aux cwmmovelr = "shr cx,1" "rep movsw" "jnc nocp" "movsb" "nocp:" \
parm [es di] [ds si] [cx] value [es di] ;
#pragma aux movelrz = "rep movsb" "xor al,al" "stosb" \
parm [es di] [ds si] [cx] modify [al] ;
#pragma aux tmovelr = "cmp cx,0" "jbe nocp" "rep movsb" "nocp:" \
parm [es di] [ds si] [cx] ;
#pragma aux zmovelr = "nc: lodsb" "stosb" "test al,al" "jnz nc" \
parm [es di] [ds si] modify [al] ;
#pragma aux mzmovelr = "nc: lodsb" "stosb" "test al,al" "jnz nc" "dec di" \
parm [es di] [ds si] modify [al] value [es di] ;
#pragma aux cmovelr = "or cx,-1" "xor al,al" "repne scasb" "dec di" \
"nc: lodsb" "stosb" "test al,al" "jnz nc" \
parm [es di] [ds si] modify [cx al] ;
#pragma aux mcmovelr = "or cx,-1" "xor al,al" "repne scasb" "dec di" \
"nc: lodsb" "stosb" "test al,al" "jnz nc" "dec di" \
parm [es di] [ds si] modify [cx al] value [es di] ;
#pragma aux bmovelr = "rep movsd" parm [es di] [ds si] [cx] ;
#pragma aux mmovelr4 = "movsd" parm [es di] [ds si] value [es di] ;
#pragma aux movelr5 = "movsd" "movsb" parm [es di] [ds si] ;
#pragma aux mmovelr5 = "movsd" "movsb" parm [es di] [ds si] value [es di] ;
#pragma aux bzero = "xor eax,eax" "rep stosd" parm [es di] [cx] modify [ax] ;
#pragma aux bmoverl = "mov dx,cx" "shl dx,2" "add di,dx" "add si,dx" \
"sub di,4" "sub si,4" "std" "rep movsd" "cld" \
parm [es di] [ds si] [cx] modify [dx] ;
#pragma aux tab_fill = "mov eax,20202020h" "stosd" "stosd" \
parm [es di] modify [ax] value [es di] ;
#pragma aux wmovelr = "rep movsw" parm [es di] [ds si] [cx] ;
#pragma aux moverl = "add di,cx" "add si,cx" "dec di" "dec si" \
"std" "rep movsb" "cld" parm [es di] [ds si] [cx] ;
#pragma aux wmoverl = "add di,cx" "add di,cx" "add si,cx" "add si,cx" \
"sub di,2" "sub si,2" "std" "rep movsw" "cld" parm [es di] [ds si] [cx] ;
#pragma aux space_fill = "mov al,32" "rep stosb" parm [es di] [cx] modify [al] ;
#pragma aux mspace_fill = "mov al,32" "rep stosb" \
parm [es di] [cx] modify [al] value [es di] ;
#pragma aux ecmp = "xor ax,ax" "or cx,cx" "repe cmpsb" "jne neq" "inc ax" \
"neq:" parm [es di] [ds si] [cx] value [ax] ;
#pragma aux size = "xor al,al" "or cx,-1" "repnz scasb" "not cx" \
parm [es di] value [cx] modify [al] ;
#define save_jbuf(d,s) wmovelr( d, s, 13 )
#else
#if DOS
#include <i86.h> /* use bios.h for MSVC and Borland */
#endif
private void
wfill( void *s, const short v, int len )
{
ushort *p = s;
while( len-- )
*p++ = v;
}
#define movelr(a,b,n) (void)memcpy( (void*)(a), (const void*)(b), (n) )
#define moverl(a,b,n) (void)memmove( (void*)(a), (const void*)(b), (n) )
#define movelr5(a,b) (void)memcpy( (void*)(a), (const void*)(b), 5 )
#define mmovelr(a,b,n) (memcpy( (void*)(a), (const void*)(b), n ), (void*)((char*)(a) + n))
#define cwmovelr(a,b,n) (void)memcpy( (void*)(a), (const void*)(b), (n) )
#define cwmmovelr(a,b,n) (memcpy( (void*)(a), (const void*)(b), n ), (void*)((char*)(a) + n))
#define mmovelr4(a,b) (memcpy( (void*)(a), (const void*)(b), 4 ), (void*)((char*)(a) + 4))
#define mmovelr5(a,b) (memcpy( (void*)(a), (const void*)(b), 5 ), (void*)((char*)(a) + 5))
#define tmovelr(a,b,n) if( n > 0 ) (void)memcpy( (void*)(a), (const void*)(b), n )
#define movelrz(a,b,n) (void)memcpy( (void*)(a), (const void*)(b), n ); a[n] = EOS;
#define zmovelr(a,b) (void)strcpy( (void*)(a), (const void*)(b) )
#define mzmovelr(a,b) (strcpy( (void*)a, (const void*)b ), get_eos( a ))
#define cmovelr(a,b) (void)strcat( (void*)(a), (const void*)(b) )
#define mcmovelr(a,b) (strcat( (void*)a, (const void*)b ), get_eos( a ))
#define wmovelr(a,b,n) (void)memcpy( (void*)(a), (const void*)(b), (n) << 1 )
#define wmoverl(a,b,n) (void)memmove( (void*)(a), (const void*)(b), (n) << 1 )
#define bzero(s,n) (void)memset( (char*)(s), EOS, (n) << 1 )
#define space_fill(s,n) (void)memset( (char*)(s), SPACE, n )
#define mspace_fill(s,n) (memset( (char*)s, SPACE, n ), s + n)
#define tab_fill(s) (space_fill( s, TAB_WIDTH ), s + TAB_WIDTH)
#define ecmp(s1,s2,n) (memcmp(s1, s2, n) == 0)
#define size(s) (strlen(s) + 1)
#define get_eos(s) strchr( s, EOS )
#define bmovelr wmovelr
#define bmoverl wmoverl
#define save_jbuf(d,s) movelr( d, s, sizeof( jmp_buf ) )
#endif
/*
* FCB for internal files.
*/
typedef struct _pp
{
byte *base; /* Memory address of page */
word rec; /* Record number of last record */
ushort linked, /* Shared page index */
end_pos; /* One past last byte */
}
PAGE_PTR;
typedef struct _unit
{
PAGE_PTR *list; /* page table */
byte *rec_start; /* address for next r/w op */
struct _unit *link_u; /* linked file descriptor */
word rec_num, /* current GE record */
eof_rec, /* location of EOF */
page, /* index of current page */
eof_page, /* last active page */
list_end; /* last page in table */
char read; /* read/write */
}
UNIT;
/* Report the current record pointer of the file */
#define vstell(fp) (fp->rec_num)
/* Report the current size of the file (for read files only) */
#define vssizeof(fp) (fp->eof_rec)
/* Reset file pointers */
#define vsrewind(fp) (fp->rec_num=fp->page=0,fp->rec_start=fp->list->base)
/* Determine if a file is primary and therefore linked */
#define isprimary(fp) (fp->link_u != NULL)
/* vsam page size & allocation unit */
#if TINY_G
#define PPP 200 /* Entries per page */
#define BLOCK_SIZE 4096 /* R/W block size */
#else
#define PPP 360
#define BLOCK_SIZE 8192
#endif
#define PPB 8 /* Pages per allocation unit */
#define PP_SIZE sizeof( PAGE_PTR ) /* should be 10/12 bytes */
#define PAGE_SIZE (PP_SIZE * PPP) /* Data page size */
#define PBLOCK (PPP * PPB) /* Sizeof allocation unit */
/* Max length of text line */
#if TINY_G
#define E_BUFF_LEN 1022
#else
#define E_BUFF_LEN (PAGE_SIZE - 3)
#endif
#define E_BUFF_SIZE (E_BUFF_LEN + 2)
typedef char LINE[E_BUFF_SIZE];
/* Macro list element */
#define MAC_NAME_LEN 4
typedef struct _ml
{
const struct _ml *next;
const char *text;
char name[MAC_NAME_LEN+1];
char par_sub;
byte nargs;
}
MACRO;
/* Entire save area, holds variables and files */
typedef struct
{
char *in_rec, *out_rec;
int in_rec_len, out_rec_len, in_rec_num,
g_rec, g_eof, i_eor, i_col, e_col, o_rec;
}
SAVE_AREA;
/* General purpose file list element */
typedef struct _fl
{
struct _fl *next, *prev;
UNIT *old_u; /* Previous file in list */
SAVE_AREA save; /* Previous kernel state */
FNAME name; /* File name */
char disp; /* Access mode letter */
byte trans; /* File is transient file */
}
FILE_LIST;
/* A variable for the calculator */
typedef struct _tk
{
union { real r; long i; }
opval, /* operand and temporary values */
litval; /* literal values */
struct _tk *next, /* when compiled or on free list */
*snext; /* when stacked */
const char *errp; /* position of start of token for error reports */
word id; /* operator, var name or LITERAL */
char fp; /* int or real operand */
char l_fp; /* int or real literal */
char group; /* Basic type selected in outer loop */
} /* Note one wasted byte */
TOKEN;
/* G_err manifests */
#define SYN_EXPR 0
#define NO_DOT 1
#define Y_LENGTHS 2
#define B_BOFFILE 3
#define EMPTY_USE_MERGE 4
#define NO_BACK 5
#define DIV_CHECK 6
#define I_REPEAT 7
#define XIT_S_M_T 8
#define I_OPT 9
#define DIG_OR_END 10
#define S_STR_LEN 11
#define END_OF_FILE 12
#define END_OF_LINE 13
#define STR_N_F 14
#define B_BOFLINE 15
#define BAD_NUM 16
#define N_T_S 17
#define TYPE_ERR 18
#define VERIFY_FAIL 19
#define W_IN_MERGE 20
#define INT_OPT 21
#define I_COMMAND 22
#define NO_MAC_NAM 23
#define RE_BACKREF 24
#define NO_SUBS_CH 25
#define NO_COUNT 26
#define NO_M_COMM 27
#define M_NAME_NF 28
#define ARG_NUM_INV 29
#define SM_SE 30
#define SYS_COM_FAIL 31
#define COMM_TOO_LONG 32
#define SAV_ON_STACK 33
#define NO_SMUT 34
#define M_DELIM 35
#define NO_ENV 36
#define UP_DELIM 37
#define BREAK_KEY 38
#define FILE_ERROR 39
#define NO_PREV_RE 40
#define MISSING_BRA 41
#define RE_TOO_MANY_BRA 42
#define RE_END_RANGE 43
#define RE_CLOSE_CURLY 44
#define RE_1_GT_2 45
#define EOF_INSERT 46
#define LINE_TOO_LONG 47
#define RE_CCL_IMB 48
#define RE_TOO_LONG 49
#define NO_RE_READ 50
#define NO_TEMP_FILE 51
#define TRANS_IN_USE 52
#define RHS_TOO_LONG 53
#define ILL_RHS_STR 54
#define CLOSE_SAVE 55
#define CLOSE_MERGE 56
#define HEX_INV 57
/* Standard files */
#define vdu stderr
#define kbd_fd 0
#define vdu_fd 2
/* screen action on return from CE */
#define SE_DISP 1
#define SE_WAIT 2
/* terminating condition codes */
#define NO_OPT 0
#define OP_EOF 1
#define RECS 2
#define MRECS 3
#define OR_END 4
#define R_TIMES 5
#define R_END 6
#define L_LON 7
#define L_LOFF 8
#define OP_CALC 0x0100
#define STR_IGNORE 0x0200
#define STR_NEGATE 0x0400
#define OP_WHILE 0x1000
#define OP_UNTIL 0x2000
#define LOOP_MASK 0xF000
/* string options for find_string */
#define STR_END 'B': case 'C': case 'G': case 'F': case 'S': case 'R': case 'r'
/* Drive options */
#define D_LINE_USER 0
#define D_SE_HOME 1
#define D_USE_FILE 2
#define D_SE_AUTO 3
/* calculator options */
#define C_ENDP 0 /* calculator is to evaluate an endpoint */
#define C_SIDE 3 /* Used for side effects only */
#define C_REPEAT 4 /* Interactive mode, display result */
/* length of portions */
#define L_LEN 76
/* Possible GE delimiters */
#define DELIM '/': case ':': case '=': case '?': case '$': case '%': \
case '&': case '+': case '>': case '<': case '[': case ']': \
case '\'': case '\"': case '`': case DEL
/* Structures for parsed options/endpoints */
typedef struct _option
{
word q; /* the primary option (#,R,C,S etc) */
int v; /* from #, N, or {} */
TOKEN *e; /* compiled numeric expressions */
string s; /* search string */
}
OPTION;
typedef struct _verb
{
struct _verb *next, *cpar; /* end of loop/cond clause */
const char *errp; /* pointer to start of command for g_err */
OPTION o1; /* inter-record component */
OPTION o2; /* intra-record component */
char comm; /* the command itself (T,P etc) */
char dot; /* line component flag */
}
VERB;
/* record nested command lists to implement co-routine SE/CE */
typedef struct _verb_list
{
struct _verb_list *next, *prev;
VERB *prog;
int save_depth;
}
VERB_LIST;
#if DOS
/*
* Mini Curses for G, no stdscr, write direct to screen
*/
#include <bios.h>
#define bios_byte(offs) (*((byte *) offs))
#define bios_word(offs) (*((ushort *) offs))
typedef ushort chtype; /* 8-bit attr + 8-bit char */
private word near LINES, near COLS; /* terminal width/height */
#define ACS_HLINE 0xC4
#define ACS_VLINE 0xB3
#define ACS_ULCORNER 0xDA
#define ACS_LLCORNER 0xC0
#define ACS_URCORNER 0xBF
#define ACS_LRCORNER 0xD9
/*
* Function and Keypad Key Definitions.
*/
#define KEY_DOWN 0x5000 /* Down arrow key */
#define KEY_UP 0x4800 /* Up arrow key */
#define KEY_LEFT 0x4B00 /* Left arrow key */
#define KEY_RIGHT 0x4D00 /* Right arrow key */
#define KEY_HOME 0x4700 /* home key */
#define KEY_NPAGE 0x5100 /* next page */
#define KEY_PPAGE 0x4900 /* previous page */
#define KEY_BTAB 0x0F00 /* Back tab key */
#define KEY_IC 0x5200 /* insert char or enter ins mode */
#define KEY_END 0x4F00 /* end key */
#define KEY_DC 0x5300 /* delete character */
#define KEY_SLEFT 0x7300 /* shifted left arrow key */
#define KEY_SRIGHT 0x7400 /* shifted right arrow */
#define KEY_F0 0x3A00 /* 10 function keys */
#define KEY_F(n) ((0x3A+(n))<<8)
#define ERR 0 /* general error flag */
private void clrtoeol( void );
private void deleteln( void );
private ushort curs_getc( void );
private void insertln( void );
private void initscr( void );
private void curs_chins( void );
private void napms( const unsigned long );
#define init_pair(a,b,c) /* Empty */
#define endwin() bios_gotoxy( (byte)(LINES - 2), 0 )
#define raw() /* Empty */
#define noraw() /* Empty */
#define attrset(a) /* Empty */
#define refresh() /* Empty */
#define move(y,x) ( curs_row = (y), curs_col = (x) )
#define rgetc() curs_getc()
#define getch() curs_getc()
#define insch( c ) curs_chins()
#define keypad(w,flag) /* Empty */
#define nonl() /* Empty */
#define noecho() /* Empty */
#define kbd_check(c) (c=_bios_keybrd(_KEYBRD_READY)?rgetc():ERR)
#define erase() berase( v_base, LINES * B_COLS )
#else
/* specials for UNIX curses.h */
#define PERFORMANCE 1
#define CURS_PERFORMANCE
#define NCC 8 /* kludge for termio.h (_XOPEN_SOURCE on SVR4.2) */
#ifndef L_ctermid
#define L_ctermid 1 /* so curses defines SYSV and not index and bcopy */
#endif
#ifdef _HPUX_SOURCE
#include <curses_colr/curses.h>
#else
#include <curses.h>
#endif
/* specials for AIX */
#ifndef ACS_HLINE
#define ACS_HLINE '-'
#define ACS_VLINE '|'
#define ACS_ULCORNER '+'
#define ACS_URCORNER '+'
#define ACS_LLCORNER '+'
#define ACS_LRCORNER '+'
#define wtimeout(w,t) ((w)->_nodelay = (t))
private int rgetc( void ) { refresh(); return getch(); }
#else
#define rgetc() getch()
#endif
#ifndef COLOUR
#ifdef COLOR_PAIR
#define COLOUR 1
#else
#define COLOUR 0
#endif
#endif
#endif
/* Manifests for the Screen Editor */
/* Action codes */
#define NEXT_LINE 1 /* Move window down one record */
#define PREV_LINE 2 /* Move window up one record */
#define NEXT_PAGE 3 /* Move window down one 20 line page */
#define PREV_PAGE 4 /* Move window up one page */
#define MOVE_TOF 5 /* Move to start of file */
#define MOVE_EOF 6 /* Move to end of file */
#define MOVE_ABS 7 /* Move direct to absolute line */
#define SE_ENTER 8 /* Do T.#0 and fill buffer */
#define SE_LEAVE 9 /* Leave S.E and write ALL text back */
#define PEEK_LINE 10 /* Push line direct from file to stk */
/* Screen and command buffer definition */
#define STATUS_LINE 0
#define COMMAND_LINE 1
#define TEMPLATE_LINE 2
#define FIRST_LINE 3
#define MATCH_LINE 6
/* Screen attributes */
#if DOS
/* colour & mono */
#define found_col 0x4F00 /* matched text */
#define cntrl_col 0x1F00 /* control characters */
#define eof_col 0x0F00 /* EOF marker */
#define scale_col 0x0E00 /* the scale line */
#define status_col 0x0B00 /* the status line */
#define norm_col 0x0A00 /* normal text */
#define query_col 0x0C00 /* query */
#define marg_col 0x0D00 /* margins */
#define found_ctrl 0x4900 /* matched binary */
#define norm_space 0x0A20 /* normal text space char */
#else
/* colour */
#define FOUND_COL COLOR_PAIR(1) /* matched text */
#define CNTRL_COL COLOR_PAIR(2) /* control characters */
#define EOF_COL COLOR_PAIR(6) /* EOF marker */
#define SCALE_COL COLOR_PAIR(4) /* the scale line */
#define STATUS_COL COLOR_PAIR(5) /* the status line */
#define NORM_COL COLOR_PAIR(3) /* normal text */
#define QUERY_COL COLOR_PAIR(7) /* query */
#define MARG_COL COLOR_PAIR(8) /* margins */
#define FOUND_CTRL COLOR_PAIR(9) /* matched binary */
/* monochrome */
#ifndef A_NORMAL
#define A_NORMAL 0
#endif
#define M_FOUND_COL A_REVERSE /* matched text */
#define M_CNTRL_COL A_REVERSE /* control characters */
#define M_EOF_COL A_BOLD /* EOF marker */
#define M_SCALE_COL A_NORMAL /* the scale line */
#define M_STATUS_COL A_NORMAL /* the status line */
#define M_NORM_COL A_NORMAL /* normal text */
#define M_QUERY_COL A_BOLD /* query */
#define M_MARG_COL A_BOLD /* margins */
#define M_FOUND_CTRL A_REVERSE /* matched binary */
#if COLOUR
private chtype found_col = FOUND_COL, /* matched text */
cntrl_col = CNTRL_COL, /* control characters */
eof_col = EOF_COL, /* EOF marker */
scale_col = SCALE_COL, /* the scale line */
status_col = STATUS_COL, /* the status line */
norm_col = NORM_COL, /* normal text */
query_col = QUERY_COL, /* query */
marg_col = MARG_COL, /* margins */
found_ctrl = FOUND_CTRL; /* matched binary */
#else
private chtype found_col = M_FOUND_COL, /* matched text */
cntrl_col = M_CNTRL_COL, /* control characters */
eof_col = M_EOF_COL, /* EOF marker */
scale_col = M_SCALE_COL, /* the scale line */
status_col = M_STATUS_COL, /* the status line */
norm_col = M_NORM_COL, /* normal text */
query_col = M_QUERY_COL, /* query */
marg_col = M_MARG_COL, /* margins */
found_ctrl = M_FOUND_CTRL; /* matched binary */
#endif
#endif
/* directions for deletes etc */
#define LEFT 1
#define RIGHT 2
/* current screen line */
#define CURSOR_ROW ((row==COMMAND_LINE?text_row:row) - FIRST_LINE)
/* line number of start of screen */
#define START_OF_PAGE (o_rec+1)
/* current file position */
#define FILE_LINE (START_OF_PAGE+row-FIRST_LINE)
#define HFILE_LINE (START_OF_PAGE+CURSOR_ROW)
#define FILE_COL (col + offset)
/* address of cursor in screen buffer */
#define BUF(c) (&s_buf[row][c])
#ifndef kbd_check
#ifdef __DGUX__
#define kbd_check(c) (nodelay(stdscr,1), c = rgetc(), nodelay(stdscr,0))
#else
#define kbd_check(c) (wtimeout(stdscr,0), c = rgetc(), wtimeout(stdscr,-1))
#endif
#endif
/* peek at length of object at top of a stack */
#define pop_length(s) ( *(short*)(s + 1) )
/* keyboard sequence actions */
typedef enum
{
A_C_UP, A_FILE_MOVE, A_C_DOWN, A_C_LEFT, A_EXP_MODE, A_DEL_C, A_C_HOME,
A_B_TAB, A_PAGE_SHIFT, A_C_STAY, A_DEL_REST, A_C_EOL, A_C_TOS, A_C_SOL,
A_C_BOS, A_JUSTIFY, A_RWX_FILE, A_EXIT_EDITOR, A_W_LEFT, A_C_RIGHT,
A_W_RIGHT, A_H_TAB, A_SEARCH, A_CHARACTER, A_C_RETURN, A_OPEN_LINE,
A_DEL_LINE, A_REST_LINE, A_HELP, A_HIST, A_REPEAT, A_MISC_CE, A_YANK,
A_COMMAND, A_FINDC, A_BLOCK, A_REDRAW
}
ACTION;
/* user query types */
typedef enum
{
Q_EDIT, Q_RAW, Q_YORN, Q_BLOCK
}
Q_MODE;
/*
* Cursor to end of line.
*/
#define c_eol() set_col( eor[row] )
/*
* Cursor right one character.
*/
#define c_right() set_col( FILE_COL + 1 )
/*
* Cursor to start of line.
*/
#define c_sol() col = offset = 0
/*
* Sync screen & file
*/
#define se_sync() file_move( - START_OF_PAGE )
#if UNIX /* for smooth scrolling */
#define se_insertln() { idlok( stdscr, YES ); insertln(); ++idlpending; }
#define se_deleteln() { idlok( stdscr, YES ); deleteln(); ++idlpending; }
#else
#define se_insertln() insertln()
#define se_deleteln() deleteln()
#endif
private int George( const VERB * );
private void G_compile( VERB **, const char * );
private int Disk_to_mem( char csc, UNIT * const, const int );
private void c_comm_u( void );
private void Xit( VERB csc );
private void term( void );
private void message( char csc );
private void inform( char csc );
private void se_execute( const ACTION, const int );
private void alter_end( int, const int );
private void Quit( void );
private void Exit( void );
private void Drive( const int );
#ifdef __WATCOMC__
#pragma aux main aborts;
#pragma aux Quit aborts;
#pragma aux Exit aborts;
#pragma aux g_err aborts;
#pragma aux g_intr aborts;
#pragma aux se_error aborts;