-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEDITOR.C
2523 lines (2174 loc) · 51.2 KB
/
EDITOR.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
/* ed_el.zip (Unix ed clone) obtained from SimTel collection, and adapted
* to Jnos by N5KNX, 12/95. A few minor bugs fixed.
* The concept is that if EDITOR is defined, we generate code to invoke
* either ed, or ted, depending on whether ED or TED was defined.
* TED takes up much less code space and only works from the console.
*
* Define STANDALONE & ED/TED for use (testing?) outside Jnos, eg,
* bcc -DSTANDALONE -DED/-DTED -O -G -Z -ml editor.c
*/
/*
* ed - standard editor
* ~~
* Authors: Brian Beattie, Kees Bot, and others
*
* Copyright 1987 Brian Beattie Rights Reserved.
* Permission to copy or distribute granted under the following conditions:
* 1). No charge may be made other than reasonable charges for reproduction.
* 2). This notice must remain intact.
* 3). No further restrictions may be added.
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* TurboC mods and cleanup 8/17/88 RAMontante.
* Further information (posting headers, etc.) at end of file.
* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
*/
#define edversion 401 /* used only in the "set" function, for i.d. */
#ifdef STANDALONE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <io.h>
#define mallocw malloc
#define tflush() fflush(stdout)
#define tputs(x) fputs(x,stdout)
#define tputc(c) putchar(c)
#define tprintf printf
#define pwait(ev)
#define dosigs 1
#define EDITOR 1
#else
#include "global.h"
#include "proc.h"
#include "socket.h"
#include "session.h"
#ifdef DBGLINKS
#include <dos.h>
#endif
#ifdef __TURBOC__
#include <signal.h>
#include <io.h>
#endif /* ifdef __TURBOC__ */
#endif /* STANDALONE */
#ifdef EDITOR
#ifdef TED
int ted2(char far *filepath);
extern int StatusLines;
#endif /* TED */
#ifdef ED
/*
* #defines for non-printing ASCII characters
*/
#define NUL 0x00 /* ^@ */
#define EOS 0x00 /* end of string */
/*#define SOH 0x01 /* ^A */
/*#define STX 0x02 /* ^B */
#define ETX 0x03 /* ^C */
/*#define EOT 0x04 /* ^D */
#define ENQ 0x05 /* ^E */
#define ACK 0x06 /* ^F */
#define BEL 0x07 /* ^G */
#define BS 0x08 /* ^H */
#define HT 0x09 /* ^I */
#define LF 0x0a /* ^J */
#define NL '\n'
#define VT 0x0b /* ^K */
#define FF 0x0c /* ^L */
#define CR 0x0d /* ^M */
#define SO 0x0e /* ^N */
#define SI 0x0f /* ^O */
#define DLE 0x10 /* ^P */
#define DC1 0x11 /* ^Q */
#define DC2 0x12 /* ^R */
#define DC3 0x13 /* ^S */
#define DC4 0x14 /* ^T */
#define NAK 0x15 /* ^U */
#define SYN 0x16 /* ^V */
#define ETB 0x17 /* ^W */
#define CAN 0x18 /* ^X */
#define EM 0x19 /* ^Y */
#define SUB 0x1a /* ^Z */
#define ESC 0x1b /* ^[ */
#define FS 0x1c /* ^\ */
#define GS 0x1d /* ^] */
#define RS 0x1e /* ^^ */
#define US 0x1f /* ^_ */
#define SP 0x20 /* space */
#define DEL 0x7f /* DEL*/
/* Definitions of meta-characters used in pattern matching
* routines. LITCHAR & NCCL are only used as token identifiers;
* all the others are also both token identifier and actual symbol
* used in the regular expression.
*/
#define BOL '^'
#define EOL '$'
#define ANY '.'
#define LITCHAR 'L'
#define ESCPE '\\'
#define CCL '[' /* Character class: [...] */
#define CCLEND ']'
#define NEGATE '^' /* n5knx: No collision with BOL, since only flags NCCL */
#define NCCL '!' /* Negative character class [^...] */
#define CLOSURE '*'
#define OR_SYM '|'
#define DITTO '&'
#define OPEN '('
#define CLOSE ')'
/* Largest permitted size for an expanded character class. (i.e. the class
* [a-z] will expand into 26 symbols; [a-z0-9] will expand into 36.)
*/
#define CLS_SIZE 128
#define TRUE 1
#define FALSE 0
#define ERR -2
#define FATAL (ERR-1)
#define CHANGED (ERR-2)
#define SET_FAIL (ERR-3)
#define SUB_FAIL (ERR-4)
#define BUFFER_SIZE 2048 /* stream-buffer size: == 1 hd cluster */
#define LINFREE 1 /* entry not in use */
#define LGLOB 2 /* line marked global */
#define MAXLINE 512 /* max number of chars per line */
#define MAXPAT 256 /* max number of chars per replacement pattern */
#define MAXFNAME 128 /* max file name size */
/** Global variables [made static to hide them from other modules!--n5knx] **/
/* Tokens are used to hold pattern templates. (see makepat()) */
typedef char BITMAP;
typedef struct token {
char tok;
char lchar;
BITMAP *bitmap;
struct token *next;
} TOKEN;
#define TOKSIZE sizeof (TOKEN)
struct line {
int l_stat; /* empty, mark */
struct line *l_prev;
struct line *l_next;
char l_buff[1];
};
typedef struct line LINE;
static int diag = 1; /* diagnostic-output? flag */
static char *paropen[9];
static char *parclose[9];
static int between, parnum;
static int truncflg = 1; /* truncate long line flag (not used) */
static int eightbit = 1; /* save eighth bit */
static int nonascii; /* count of non-ascii chars read */
static int nullchar; /* count of null chars read */
static int truncated; /* count of lines truncated */
static char fname[MAXFNAME];
static int fchanged; /* file-changed? flag */
static int nofname;
static int restricted; /* only access command-line file */
static int mark['z'-'a'+1];
static TOKEN *oldpat;
static LINE Line0;
static int CurLn = 0;
static int LastLn = 0;
static char inlin[MAXLINE];
static int Line1, Line2, nlines;
static int nflg; /* print line number flag */
static int lflg; /* print line in verbose mode */
static char *inptr; /* tty input buffer */
static struct tbl {
char *t_str;
int *t_ptr;
int t_val;
} *t, tbl[] = {
"number", &nflg, TRUE,
"nonumber", &nflg, FALSE,
"list", &lflg, TRUE,
"nolist", &lflg, FALSE,
"eightbit", &eightbit, TRUE,
"noeightbit", &eightbit, FALSE,
0
};
static char NOMEM[] = "insert error (out of memory)";
extern int Numrows, NumCols, StatusLines; /* from Jnos */
/*-------------------------------------------------------------------------*/
#if defined(ANSIPROTO) || defined(__TURBOC__)
static void prntln(char *, int, int);
static void putcntl(char);
static int doprnt(int, int);
static int esc(char **s);
static LINE *getptr(int);
static BITMAP *makebitmap(unsigned);
static int edsetbit(unsigned c, char *map, unsigned val);
static int testbit (unsigned c, char *map );
static int omatch(char **linp, TOKEN *pat, char *boln);
static char *match(char *lin, TOKEN *pat, char *boln);
static char *amatch(char *lin, TOKEN *pat, char* boln);
static void relink(LINE *, LINE *, LINE *, LINE *);
static int del(int, int);
static int ins(char *);
static int egets(char *str, int size, FILE *stream);
static char *gettxt(int);
static int doappend(int, int);
static char *catsub(char *, char *, char *, char *, char *);
static char *matchs(char *, TOKEN *, int);
static int deflt(int, int);
static TOKEN *makepat(char *arg, int delim);
static void unmakepat(TOKEN *head);
static char *maksub(char *sub, int subsz);
static TOKEN *optpat(void);
static char *dodash(int delim, char *src, char *map);
static char *getfn(void);
static int doread(int lin, char *fname);
static int dowrite(int from, int to, char *fname, int apflg);
static int join(int first, int last);
static int getnum(int first);
static int getlst(void);
static int dolst(int line1, int line2);
static int getone(void);
static int move(int num);
static int set(void);
static int getrhs(char *sub);
static int find(TOKEN *pat, int dir);
static int subst(TOKEN *pat, char *sub, int gflg, int pflag);
static int transfer(int num);
static int docmd(int glob);
static int ckglob(void);
static int doglob(void);
static void set_ed_buf(void);
static void cleanup(void);
#else /* !__TURBOC__ */
extern char *strcpy();
extern int *malloc();
extern LINE *getptr();
BITMAP *makebitmap();
extern char *gettxt();
extern char *catsub();
extern char *matchs();
char *maksub();
TOKEN *optpat();
#endif /* __TURBOC__ */
/*________ Macros ________________________________________________________*/
#ifndef max
# define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef toupper
# define toupper(c) ((c >= 'a' && c <= 'z') ? c-32 : c )
#endif
/* getpat -- Translate arg into a TOKEN string */
#define getpat(arg) makepat((arg), '\000')
#define nextln(l) ((l)+1 > LastLn ? 0 : (l)+1)
#define prevln(l) ((l)-1 < 0 ? LastLn : (l)-1)
#define clrbuf() del(1, LastLn)
#define Skip_White_Space {while (*inptr==SP || *inptr==HT) inptr++;}
#ifdef DBGLINKS
void
validatelinks(char *hdr)
{
LINE *nxt = &Line0;
int lnum=0;
nxt = nxt->l_next;
while(nxt != &Line0) {
lnum++;
if (strlen(nxt->l_buff) > MAXLINE) {
printf("%s: anomaly @ line %d/%p. Prev,Next=%p,%p\n",
hdr, lnum, nxt, nxt->l_prev, nxt->l_next);
break;
}
nxt = nxt->l_next;
}
}
#endif
/*________ functions ______________________________________________________*/
/*****************************************************************************
* BITMAP.C - makebitmap, edsetbit, testbit
* bit-map manipulation routines.
*
* Copyright (c) Allen I. Holub, all rights reserved. This program may
* for copied for personal, non-profit use only.
*/
static BITMAP *makebitmap( size )
unsigned size;
{
/* Make a bit map with "size" bits. The first entry in
* the map is an "unsigned int" representing the maximum
* bit. The map itself is concatenated to this integer.
* Return a pointer to a map on success, 0 if there's
* not enough memory.
*/
unsigned *map, numbytes;
numbytes = (size >> 3) + ((size & 0x07) ? 1 : 0 );
#ifdef DEBUG
tprintf("Making a %u bit map (%u bytes required)\n", size, numbytes);
#endif
if( (map=(unsigned *)calloc(numbytes + sizeof(unsigned),sizeof(char))) != (unsigned *)NULL)
*map = size;
return ((BITMAP *)map);
}
static int edsetbit( c, map, val )
unsigned c, val;
char *map;
{
/* Set bit c in the map to val.
* If c > map-size, 0 is returned, else 1 is returned.
*/
if( c >= *(unsigned *)map ) /* if c >= map size */
return 0;
map += sizeof(unsigned); /* skip past size */
if( val )
map[c >> 3] |= 1 << (c & 0x07);
else
map[c >> 3] &= ~(1 << (c & 0x07));
return( 1 );
}
static int testbit( c, map )
unsigned c;
char *map;
{
/* Return 1 if the bit corresponding to c in map is set.
* 0 if it is not.
*/
if( c >= *(unsigned *)map )
return 0;
map += sizeof(unsigned);
return(map[ c >> 3 ] & (1 << (c & 0x07)));
}
/********* end of BITMAP.C ************************************************/
/* omatch.c
*
* Match one pattern element, pointed at by pat, with the character at
* **linp. Return non-zero on match. Otherwise, return 0. *Linp is
* advanced to skip over the matched character; it is not advanced on
* failure. The amount of advance is 0 for patterns that match null
* strings, 1 otherwise. "boln" should point at the position that will
* match a BOL token.
*/
static int omatch(linp, pat, boln)
char **linp;
TOKEN *pat;
char *boln;
{
register int advance;
advance = -1;
if (**linp) {
switch (pat->tok) {
case LITCHAR:
if (**linp == pat->lchar)
advance = 1;
break;
case BOL:
if (*linp == boln) /* Replaced (*linp = boln) */
advance = 0;
break;
case ANY:
if (**linp != '\n')
advance = 1;
break;
case EOL:
if (**linp == '\n')
advance = 0;
break;
case CCL:
if( testbit( **linp, pat->bitmap))
advance = 1;
break;
case NCCL:
if (!testbit (**linp, pat->bitmap))
advance = 1;
break;
}
}
if (advance >= 0)
*linp += advance;
return (++advance);
}
static char *match(lin, pat, boln)
char *lin;
TOKEN *pat;
char *boln;
{
register char *bocl, *rval, *strstart;
if(pat == 0)
return 0;
strstart = lin;
while(pat) {
if(pat->tok == CLOSURE && pat->next) {
/* Process a closure:
* first skip over the closure token to the
* object to be repeated. This object can be
* a character class.
*/
pat = pat->next;
/* Now match as many occurrences of the
* closure pattern as possible.
*/
bocl = lin;
while( *lin && omatch(&lin, pat, boln))
;
/* 'Lin' now points to the character that
* made us fail. Now go on to process the
* rest of the string. A problem here is
* a character following the closure which
* could have been in the closure.
* For example, in the pattern "[a-z]*t" (which
* matches any lower-case word ending in a t),
* the final 't' will be sucked up in the while
* loop. So, if the match fails, we back up a
* notch and try to match the rest of the
* string again, repeating this process
* recursively until we get back to the
* beginning of the closure. The recursion
* goes, at most two levels deep.
*/
if((pat = pat->next)!=(TOKEN *)NULL)
{
int savbtwn=between;
int savprnm=parnum;
while(bocl <= lin) {
if ((rval = match(lin, pat, boln))!=NULL) {
return(rval); /* success */
} else {
--lin;
between=savbtwn;
parnum=savprnm;
}
}
return (0); /* match failed */
}
} else
if (pat->tok == OPEN) {
if (between || parnum>=9)
return 0;
paropen[parnum] = lin;
between = 1;
pat = pat->next;
} else
if (pat->tok == CLOSE) {
if (!between)
return 0;
parclose[parnum++] = lin;
between = 0;
pat = pat->next;
} else
if (omatch(&lin, pat, boln)) {
pat = pat->next;
} else {
return (0);
}
}
/* Note that omatch() advances lin to point at the next
* character to be matched. Consequently, when we reach
* the end of the template, lin will be pointing at the
* character following the last character matched. The
* exceptions are templates containing only a BOLN or EOLN
* token. In these cases omatch doesn't advance.
*
* A philosophical point should be mentioned here. Is $
* a position or a character? (i.e. does $ mean the EOL
* character itself or does it mean the character at the end
* of the line.) I decided here to make it mean the former,
* in order to make the behavior of match() consistent. If
* you give match the pattern ^$ (match all lines consisting
* only of an end of line) then, since something has to be
* returned, a pointer to the end of line character itself is
* returned.
*/
return ((char *)max(strstart , lin));
}
/* amatch.c
*
* Scans throught the pattern template looking for a match
* with lin. Each element of lin is compared with the template
* until either a mis-match is found or the end of the template
* is reached. In the former case a 0 is returned; in the latter,
* a pointer into lin (pointing to the character following the
* matched pattern) is returned.
*
* "lin" is a pointer to the line being searched.
* "pat" is a pointer to a template made by makepat().
* "boln" is a pointer into "lin" which points at the
* character at the beginning of the line.
*/
static char *amatch(lin, pat, boln)
char *lin;
TOKEN *pat;
char *boln;
{
between = parnum = 0;
lin = match(lin, pat, boln);
if (between)
return 0;
while (parnum<9) {
paropen[parnum] = parclose[parnum] = "";
parnum++;
}
return lin;
}
/* doappend.c */
static int doappend(line, glob)
int line, glob;
{
int stat;
char lin[MAXLINE];
if(glob)
return(ERR);
CurLn = line;
for (;;) {
#ifdef DBGLINKS
validatelinks("doappend");
#endif
if(nflg)
tprintf("%6d. ",CurLn+1);
#ifdef STANDALONE
if(fgets(lin, MAXLINE, stdin) == NULL)
#else
if (recvline(Curproc->input, lin, MAXLINE) == -1)
#endif
return( EOF );
if(lin[0] == '.' && lin[1] == '\n')
return(0);
stat = ins(lin);
if(stat < 0)
return( ERR );
}
}
/* catsub.c */
static char *catsub(from, to, sub, new, newend)
char *from, *to, *sub, *new, *newend;
{
char *cp, *cp2;
for(cp = new; *sub != EOS && cp < newend;) {
if(*sub == DITTO)
for(cp2 = from; cp2 < to;) {
*cp++ = *cp2++;
if(cp >= newend)
break;
}
else
if (*sub == ESCPE) {
sub++;
if ('1' <= *sub && *sub <= '9') {
char *parcl = parclose[*sub - '1'];
for (cp2 = paropen[*sub - '1']; cp2 < parcl;) {
*cp++ = *cp2++;
if (cp >= newend)
break;
}
} else
*cp++ = *sub;
} else
*cp++ = *sub;
sub++;
}
return(cp);
}
/* ckglob.c */
static int ckglob()
{
TOKEN *glbpat;
char c, delim, *lin;
int num;
LINE *ptr;
c = *inptr;
if(c != 'g' && c != 'v')
return(0);
if (deflt(1, LastLn) < 0)
return(ERR);
delim = *++inptr;
if(delim <= ' ')
return(ERR);
glbpat = optpat();
if(*inptr == delim)
inptr++;
for (num=1; num<=LastLn; num++) {
ptr = getptr(num);
ptr->l_stat &= ~LGLOB;
if (Line1 <= num && num <= Line2) {
lin = gettxt(num);
if(matchs(lin, glbpat, 0)) {
if (c=='g') ptr->l_stat |= LGLOB;
} else {
if (c=='v') ptr->l_stat |= LGLOB;
}
}
}
return(1);
}
/* deflt.c
* Set Line1 & Line2 (the command-range delimiters) if the file is
* empty; Test whether they have valid values.
*/
static int deflt(def1, def2)
int def1, def2;
{
if(nlines == 0) {
Line1 = def1;
Line2 = def2;
}
return ( (Line1>Line2 || Line1<=0) ? ERR : 0 );
}
/* del.c */
/* One of the calls to this function tests its return value for an error
* condition. But del doesn't return any error value, and it isn't obvious
* to me what errors might be detectable/reportable. To silence a warning
* message, I've added a constant return statement. -- RAM
*/
static int del(from, to)
int from, to;
{
LINE *first, *last, *next, *tmp;
#ifdef DBGLINKS
int lnum;
#endif
if(from < 1)
from = 1;
first = getptr(prevln(from));
last = getptr(nextln(to));
next = first->l_next;
#ifdef DBGLINKS
printf("del: first,last,next = %p,%p,%p (%d..%d) Cur=%d\n", first,last,next,from,to,CurLn);
lnum=from;
#endif
while(next != last && next != &Line0) {
tmp = next->l_next;
free(next);
next = tmp;
#ifdef DBGLINKS
printf("%d/%p ", ++lnum,next);
#endif
}
relink(first, last, first, last);
LastLn -= (to - from)+1;
CurLn = prevln(from);
return(0);
}
static int dolst(line1, line2)
int line1, line2;
{
int oldlflg=lflg, p;
lflg = 1;
p = doprnt(line1, line2);
lflg = oldlflg;
return p;
}
/* esc.c
* Map escape sequences into their equivalent symbols. Returns the
* correct ASCII character. If no escape prefix is present then s
* is untouched and *s is returned, otherwise **s is advanced to point
* at the escaped character and the translated character is returned.
*/
static int esc(s)
char **s;
{
register int rval;
if (**s != ESCPE) {
rval = **s;
} else {
(*s)++;
switch(toupper(**s)) {
case '\000':
rval = ESCPE; break;
case 'S':
rval = ' '; break;
case 'N':
rval = '\n'; break;
case 'T':
rval = '\t'; break;
case 'B':
rval = '\b'; break;
case 'R':
rval = '\r'; break;
default:
rval = **s; break;
}
}
return (rval);
}
/* dodash.c
* Expand the set pointed to by *src into dest.
* Stop at delim. Return 0 on error or size of
* character class on success. Update *src to
* point at delim. A set can have one element
* {x} or several elements ( {abcdefghijklmnopqrstuvwxyz}
* and {a-z} are equivalent ). Note that the dash
* notation is expanded as sequential numbers.
* This means (since we are using the ASCII character
* set) that a-Z will contain the entire alphabet
* plus the symbols: [\]^_`. The maximum number of
* characters in a character class is defined by maxccl.
*/
static char *dodash(delim, src, map)
int delim;
char *src, *map;
{
register int first, last;
char *start;
start = src;
while( *src && *src != delim ) {
if( *src != '-')
edsetbit( esc( &src ), map, 1 );
else
if( src == start || *(src + 1) == delim )
edsetbit( '-', map, 1 );
else {
src++;
if( *src < *(src - 2)) {
first = *src;
last = *(src - 2);
} else {
first = *(src - 2);
last = *src;
}
while( ++first <= last )
edsetbit( first, map, 1);
}
src++;
}
return( src );
}
/* doprnt.c */
static int doprnt(from, to)
int from, to;
{
from = (from < 1) ? 1 : from;
to = (to > LastLn) ? LastLn : to;
if(to != 0) {
for(CurLn = from; CurLn <= to; CurLn++)
prntln(gettxt(CurLn), lflg, (nflg ? CurLn : 0));
CurLn = to;
}
return(0);
}
static void prntln(str, vflg, lin)
char *str;
int vflg, lin;
{
if(lin)
tprintf("%7d ",lin);
while(*str && *str != NL) {
if(*str < ' ' || *str >= 0x7f) {
switch(*str) {
case '\t':
if(vflg)
putcntl(*str);
else
tputc(*str);
break;
case DEL:
tputc('^');
tputc('?');
break;
default:
putcntl(*str);
break;
}
} else
tputc(*str);
str++;
}
if(vflg)
tputc('$');
tputc('\n');
}
static void putcntl(c)
char c;
{
tputc('^');
tputc((c&31)|'@');
}
/* egets.c */
static int egets(str,size,stream)
char *str;
int size;
FILE *stream;
{
int c, count;
char *cp;
for(count = 0, cp = str; size > count;) {
c = getc(stream);
if(c == EOF) {
*cp++ = '\n';
*cp = EOS;
if(count)
tputs("[Incomplete last line]");
return(count);
}
if(c == NL) {
*cp++ = c;
*cp = EOS;
return(++count);
}
if(c > 127) {
if(!eightbit) /* if not saving eighth bit */
c = c&127; /* strip eigth bit */
nonascii++; /* count it */
}
if(c) {
*cp++ = c; /* not null, keep it */
count++;
} else
nullchar++; /* count nulls */
}
str[count-1] = EOS;
if(c != NL) {
tputs("truncating line");
truncated++;
while((c = getc(stream)) != EOF)
if(c == NL)
break;
}
return(count);
} /* egets */
static int doread(lin, fname)
int lin;
char *fname;
{
FILE *fp;
int err;
unsigned long bytes;
unsigned int lines;
static char str[MAXLINE];
err = 0;
nonascii = nullchar = truncated = 0;
if (diag) tprintf("\"%s\" ",fname);
if( (fp = fopen(fname, "r")) == NULL ) {
tputs(" isn't readable.");
return( ERR );
}
setvbuf(fp, NULL, _IOFBF, BUFFER_SIZE);
#if !defined(STANDALONE) && !defined(UNIX)
fseek(fp,0L,SEEK_END);
bytes = ftell(fp);
fseek(fp,0L,SEEK_SET);
if (bytes > (long)(availmem() - Memthresh)) {
tputs(NOMEM);
fclose(fp);
return( ERR );
}
#endif
CurLn = lin;
for(lines = 0, bytes = 0;(err = egets(str,MAXLINE,fp)) > 0;) {
pwait(NULL);
bytes += strlen(str);
if( (err = ins(str)) < 0) {
/*tputs(NOMEM);*/