forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathed.c
More file actions
2198 lines (1852 loc) · 43 KB
/
Copy pathed.c
File metadata and controls
2198 lines (1852 loc) · 43 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
/* Copyright 1987 Brian Beattie Rights Reserved.
*
* Permission to copy and/or distribute granted under the
* following conditions:
*
* 1). No charge may be made other than resonable charges
* for reproduction.
*
* 2). This notice must remain intact.
*
* 3). No further restrictions may be added.
*
*/
/* This program used to be in many little pieces, with this makefile:
.SUFFIXES: .c .s
CFLAGS = -F
OBJS = append.s catsub.s ckglob.s deflt.s del.s docmd.s doglob.s\
doprnt.s doread.s dowrite.s ed.s egets.s find.s getfn.s getlst.s\
getnum.s getone.s getptr.s getrhs.s gettxt.s ins.s join.s maksub.s\
move.s optpat.s set.s setbuf.s subst.s getpat.s matchs.s amatch.s\
unmkpat.s omatch.s makepat.s bitmap.s dodash.s esc.s System.s
ed: $(OBJS)
cc -T. -i -o ed $(OBJS)
*/
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
/****************************/
/* tools.h */
/*
* #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 */
#define TRUE 1
#define FALSE 0
#define ERR -2
/* 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 ESCAPE '\\'
#define CCL '[' /* Character class: [...] */
#define CCLEND ']'
#define NEGATE '^'
#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
/*
* 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)
/*
* An absolute maximun for strings.
*/
#define MAXSTR 132 /* Maximum numbers of characters in a line */
/* Macros */
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)
#define toupper(c) (c>='a'&&c<='z'?c-32:c)
/* ed.h */
#define FATAL (ERR-1)
struct line {
int l_stat; /* empty, mark */
struct line *l_prev;
struct line *l_next;
char l_buff[1];
};
typedef struct line LINE;
#define LINFREE 1 /* entry not in use */
#define LGLOB 2 /* line marked global */
/* max number of chars per line */
#define MAXLINE (sizeof(int) == 2 ? 256 : 8192)
#define MAXPAT 256 /* max number of chars per replacement
* pattern */
/* max file name size */
#define MAXFNAME (sizeof(int) == 2 ? 256 : 1024)
extern LINE line0;
extern int curln, lastln, line1, line2, nlines;
extern int nflg; /* print line number flag */
extern int lflg; /* print line in verbose mode */
extern char *inptr; /* tty input buffer */
extern char linbuf[], *linptr; /* current line */
extern int truncflg; /* truncate long line flag */
extern int eightbit; /* save eighth bit */
extern int nonascii; /* count of non-ascii chars read */
extern int nullchar; /* count of null chars read */
extern int truncated; /* count of lines truncated */
extern int fchanged; /* file changed */
#define nextln(l) ((l)+1 > lastln ? 0 : (l)+1)
#define prevln(l) ((l)-1 < 0 ? lastln : (l)-1)
/* amatch.c */
/* #include <stdio.h> */
/* #include "tools.h" */
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(static char *match, (char *lin, TOKEN *pat, char *boln));
_PROTOTYPE(char *amatch, (char *lin, TOKEN *pat, char *boln));
_PROTOTYPE(int append, (int line, int glob));
_PROTOTYPE(BITMAP *makebitmap, (unsigned size));
_PROTOTYPE(int setbit, (unsigned c, char *map, unsigned val));
_PROTOTYPE(int testbit, (unsigned c, char *map));
_PROTOTYPE(char *catsub, (char *from, char *to, char *sub, char *new, char *newend));
_PROTOTYPE(int ckglob, (void));
_PROTOTYPE(int deflt, (int def1, int def2));
_PROTOTYPE(int del, (int from, int to));
_PROTOTYPE(int docmd, (int glob));
_PROTOTYPE(int dolst, (int line1, int line2));
_PROTOTYPE(char *dodash, (int delim, char *src, char *map));
_PROTOTYPE(int doglob, (void));
_PROTOTYPE(int doprnt, (int from, int to));
_PROTOTYPE(void prntln, (char *str, int vflg, int lin));
_PROTOTYPE(void putcntl, (int c, FILE *stream));
_PROTOTYPE(int doread, (int lin, char *fname));
_PROTOTYPE(int dowrite, (int from, int to, char *fname, int apflg));
_PROTOTYPE(void intr, (int sig));
_PROTOTYPE(int egets, (char *str, int size, FILE *stream));
_PROTOTYPE(int esc, (char **s));
_PROTOTYPE(int find, (TOKEN *pat, int dir));
_PROTOTYPE(char *getfn, (void));
_PROTOTYPE(int getlst, (void));
_PROTOTYPE(int getnum, (int first));
_PROTOTYPE(int getone, (void));
_PROTOTYPE(TOKEN *getpat, (char *arg));
_PROTOTYPE(LINE *getptr, (int num));
_PROTOTYPE(int getrhs, (char *sub));
_PROTOTYPE(char *gettxt, (int num));
_PROTOTYPE(int ins, (char *str));
_PROTOTYPE(int System, (char *c));
_PROTOTYPE(int join, (int first, int last));
_PROTOTYPE(TOKEN *makepat, (char *arg, int delim));
_PROTOTYPE(char *maksub, (char *sub, int subsz));
_PROTOTYPE(char *matchs, (char *line, TOKEN *pat, int ret_endp));
_PROTOTYPE(int move, (int num));
_PROTOTYPE(int transfer, (int num));
_PROTOTYPE(int omatch, (char **linp, TOKEN *pat, char *boln));
_PROTOTYPE(TOKEN *optpat, (void));
_PROTOTYPE(int set, (void));
_PROTOTYPE(int show, (void));
_PROTOTYPE(void relink, (LINE *a, LINE *x, LINE *y, LINE *b));
_PROTOTYPE(void clrbuf, (void));
_PROTOTYPE(void set_buf, (void));
_PROTOTYPE(int subst, (TOKEN *pat, char *sub, int gflg, int pflag));
_PROTOTYPE(void unmakepat, (TOKEN *head));
/* 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.
*/
char *paropen[9], *parclose[9];
int between, parnum;
char *amatch(lin, pat, boln)
char *lin;
TOKEN *pat;
char *boln;
{
between = 0;
parnum = 0;
lin = match(lin, pat, boln);
if (between) return 0;
while (parnum < 9) {
paropen[parnum] = parclose[parnum] = "";
parnum++;
}
return lin;
}
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 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) {
int savbtwn = between;
int savprnm = parnum;
while (bocl <= lin) {
if (rval = match(lin, pat, boln)) {
/* Success */
return(rval);
} 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));
}
/* append.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
int append(line, glob)
int line, glob;
{
int stat;
char lin[MAXLINE];
if (glob) return(ERR);
curln = line;
while (1) {
if (nflg) printf("%6d. ", curln + 1);
if (fgets(lin, MAXLINE, stdin) == NULL) return(EOF);
if (lin[0] == '.' && lin[1] == '\n') return (0);
stat = ins(lin);
if (stat < 0) return(ERR);
}
}
/* bitmap.c */
/*
* BITMAP.C - makebitmap, setbit, testbit
* bit-map manipulation routines.
*
* Copyright (c) Allen I. Holub, all rights reserved. This program may
* for copied for personal, non-profit use only.
*
*/
#ifdef DEBUG
/* #include <stdio.h> */
#endif
/* #include "tools.h" */
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
printf("Making a %d bit map (%d bytes required)\n", size, numbytes);
#endif
if (map = (unsigned *) malloc(numbytes + sizeof(unsigned))) {
*map = size;
memset(map + 1, 0, numbytes);
}
return((BITMAP *) map);
}
int setbit(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;
}
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)));
}
/* catsub.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
extern char *paropen[9], *parclose[9];
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 == ESCAPE) {
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 */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
int ckglob()
{
TOKEN *glbpat;
char c, delim;
char lin[MAXLINE];
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++;
ptr = getptr(1);
for (num = 1; num <= lastln; num++) {
ptr->l_stat &= ~LGLOB;
if (line1 <= num && num <= line2) {
strcpy(lin, ptr->l_buff);
strcat(lin, "\n");
if (matchs(lin, glbpat, 0)) {
if (c == 'g') ptr->l_stat |= LGLOB;
} else {
if (c == 'v') ptr->l_stat |= LGLOB;
}
}
ptr = ptr->l_next;
}
return(1);
}
/* deflt.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
int deflt(def1, def2)
int def1, def2;
{
if (nlines == 0) {
line1 = def1;
line2 = def2;
}
if (line1 > line2 || line1 <= 0) return(ERR);
return(0);
}
/* del.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
int del(from, to)
int from, to;
{
LINE *first, *last, *next, *tmp;
if (from < 1) from = 1;
first = getptr(prevln(from));
last = getptr(nextln(to));
next = first->l_next;
while (next != last && next != &line0) {
tmp = next->l_next;
free((char *) next);
next = tmp;
}
relink(first, last, first, last);
lastln -= (to - from) + 1;
curln = prevln(from);
return(0);
}
/* docmd.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
char fname[MAXFNAME];
int fchanged;
extern int nofname;
extern int mark[];
int docmd(glob)
int glob;
{
static char rhs[MAXPAT];
TOKEN *subpat;
int c, err, line3;
int apflg, pflag, gflag;
int nchng;
char *fptr;
pflag = FALSE;
while (*inptr == SP && *inptr == HT) inptr++;
c = *inptr++;
switch (c) {
case NL:
if (nlines == 0) {
if ((line2 = nextln(curln)) == 0) return(ERR);
}
curln = line2;
return(1);
break;
case '=': printf("%d\n", line2); break;
case 'a':
if (*inptr != NL || nlines > 1) return(ERR);
if (append(line1, glob) < 0) return(ERR);;
fchanged = TRUE;
break;
case 'c':
if (*inptr != NL) return(ERR);
if (deflt(curln, curln) < 0) return(ERR);
if (del(line1, line2) < 0) return(ERR);
if (append(curln, glob) < 0) return (ERR);
fchanged = TRUE;
break;
case 'd':
if (*inptr != NL) return(ERR);
if (deflt(curln, curln) < 0) return(ERR);
if (del(line1, line2) < 0) return(ERR);
if (nextln(curln) != 0) curln = nextln(curln);
fchanged = TRUE;
break;
case 'e':
if (nlines > 0) return(ERR);
if (fchanged) {
fchanged = FALSE;
return(ERR);
}
/* FALL THROUGH */
case 'E':
if (nlines > 0) return(ERR);
if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);
if ((fptr = getfn()) == NULL) return(ERR);
clrbuf();
if ((err = doread(0, fptr)) < 0) return(err);
strcpy(fname, fptr);
fchanged = FALSE;
break;
case 'f':
if (nlines > 0) return(ERR);
if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);
if ((fptr = getfn()) == NULL) return(ERR);
if (nofname)
printf("%s\n", fname);
else
strcpy(fname, fptr);
break;
case 'i':
if (*inptr != NL || nlines > 1) return(ERR);
if (append(prevln(line1), glob) < 0) return(ERR);
fchanged = TRUE;
break;
case 'j':
if (*inptr != NL || deflt(curln, curln + 1) < 0) return(ERR);
if (join(line1, line2) < 0) return(ERR);
break;
case 'k':
while (*inptr == ' ' || *inptr == HT) inptr++;
if (*inptr < 'a' || *inptr > 'z') return ERR;
c = *inptr++;
if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);
mark[c - 'a'] = line1;
break;
case 'l':
if (*inptr != NL) return(ERR);
if (deflt(curln, curln) < 0) return (ERR);
if (dolst(line1, line2) < 0) return (ERR);
break;
case 'm':
if ((line3 = getone()) < 0) return(ERR);
if (deflt(curln, curln) < 0) return (ERR);
if (move(line3) < 0) return (ERR);
fchanged = TRUE;
break;
case 'P':
case 'p':
if (*inptr != NL) return(ERR);
if (deflt(curln, curln) < 0) return (ERR);
if (doprnt(line1, line2) < 0) return (ERR);
break;
case 'q':
if (fchanged) {
fchanged = FALSE;
return(ERR);
}
/* FALL THROUGH */
case 'Q':
if (*inptr == NL && nlines == 0 && !glob)
return(EOF);
else
return(ERR);
case 'r':
if (nlines > 1) return(ERR);
if (nlines == 0) line2 = lastln;
if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);
if ((fptr = getfn()) == NULL) return(ERR);
if ((err = doread(line2, fptr)) < 0) return(err);
fchanged = TRUE;
break;
case 's':
if (*inptr == 'e') return(set());
while (*inptr == SP || *inptr == HT) inptr++;
if ((subpat = optpat()) == NULL) return (ERR);
if ((gflag = getrhs(rhs)) < 0) return (ERR);
if (*inptr == 'p') pflag++;
if (deflt(curln, curln) < 0) return (ERR);
if ((nchng = subst(subpat, rhs, gflag, pflag)) < 0) return (ERR);
if (nchng) fchanged = TRUE;
break;
case 't':
if ((line3 = getone()) < 0) return(ERR);
if (deflt(curln, curln) < 0) return (ERR);
if (transfer(line3) < 0) return (ERR);
fchanged = TRUE;
break;
case 'W':
case 'w':
apflg = (c == 'W');
if (*inptr != ' ' && *inptr != HT && *inptr != NL) return(ERR);
if ((fptr = getfn()) == NULL) return(ERR);
if (deflt(1, lastln) < 0) return(ERR);
if (dowrite(line1, line2, fptr, apflg) < 0) return (ERR);
fchanged = FALSE;
break;
case 'x':
if (*inptr == NL && nlines == 0 && !glob) {
if ((fptr = getfn()) == NULL) return(ERR);
if (dowrite(1, lastln, fptr, 0) >= 0) return (EOF);
}
return(ERR);
case 'z':
if (deflt(curln, curln) < 0) return(ERR);
switch (*inptr) {
case '-':
if (doprnt(line1 - 21, line1) < 0) return(ERR);
break;
case '.':
if (doprnt(line1 - 11, line1 + 10) < 0) return(ERR);
break;
case '+':
case '\n':
if (doprnt(line1, line1 + 21) < 0) return(ERR);
break;
}
break;
default: return(ERR);
}
return(0);
}
int dolst(line1, line2)
int line1, line2;
{
int oldlflg = lflg, p;
lflg = 1;
p = doprnt(line1, line2);
lflg = oldlflg;
return p;
}
/* dodash.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* 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.
*/
char *dodash(delim, src, map)
int delim;
char *src, *map;
{
register int first, last;
char *start;
start = src;
while (*src && *src != delim) {
if (*src != '-') setbit(esc(&src), map, 1);
else if (src == start || *(src + 1) == delim)
setbit('-', map, 1);
else {
src++;
if (*src < *(src - 2)) {
first = *src;
last = *(src - 2);
} else {
first = *(src - 2);
last = *src;
}
while (++first <= last) setbit(first, map, 1);
}
src++;
}
return(src);
}
/* doglob.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
int doglob()
{
int lin, stat;
char *cmd;
LINE *ptr;
cmd = inptr;
while (1) {
ptr = getptr(1);
for (lin = 1; lin <= lastln; lin++) {
if (ptr->l_stat & LGLOB) break;
ptr = ptr->l_next;
}
if (lin > lastln) break;
ptr->l_stat &= ~LGLOB;
curln = lin;
inptr = cmd;
if ((stat = getlst()) < 0) return(stat);
if ((stat = docmd(1)) < 0) return (stat);
}
return(curln);
}
/* doprnt.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
int doprnt(from, to)
int from, to;
{
int i;
LINE *lptr;
from = from < 1 ? 1 : from;
to = to > lastln ? lastln : to;
if (to != 0) {
lptr = getptr(from);
for (i = from; i <= to; i++) {
prntln(lptr->l_buff, lflg, (nflg ? i : 0));
lptr = lptr->l_next;
}
curln = to;
}
return(0);
}
void prntln(str, vflg, lin)
char *str;
int vflg, lin;
{
if (lin) printf("%7d ", lin);
while (*str && *str != NL) {
if (*str < ' ' || *str >= 0x7f) {
switch (*str) {
case '\t':
if (vflg)
putcntl(*str, stdout);
else
putc(*str, stdout);
break;
case DEL:
putc('^', stdout);
putc('?', stdout);
break;
default:
putcntl(*str, stdout);
break;
}
} else
putc(*str, stdout);
str++;
}
if (vflg) putc('$', stdout);
putc('\n', stdout);
}
void putcntl(c, stream)
char c;
FILE *stream;
{
putc('^', stream);
putc((c & 31) | '@', stream);
}
/* doread.c */
/* #include <stdio.h> */
/* #include "tools.h" */
/* #include "ed.h" */
extern int diag;
int doread(lin, fname)
int lin;
char *fname;
{
FILE *fp;
int err;
long bytes;
int lines;
static char str[MAXLINE];
err = 0;
nonascii = nullchar = truncated = 0;
if (diag) printf("\"%s\" ", fname);
if ((fp = fopen(fname, "r")) == NULL) {
printf("file open err\n");
return(ERR);
}
curln = lin;
for (lines = 0, bytes = 0; (err = egets(str, MAXLINE, fp)) > 0;) {
bytes += strlen(str);
if (ins(str) < 0) {
printf("file insert error\n");
err++;
break;
}
lines++;
}
fclose(fp);
if (err < 0) return(err);
if (diag) {