-
Notifications
You must be signed in to change notification settings - Fork 1
/
ed.c
2281 lines (2032 loc) · 53.4 KB
/
ed.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 - 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.
* 4). Except meaningless ones.
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* TurboC mods and cleanup 8/17/88 RAMontante.
* Further information (posting headers, etc.) at end of file.
* RE stuff replaced with Spencerian version, sundry other bugfix+speedups
* Ian Phillipps. Version incremented to "5".
* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
*
* ********--->> INDENTATION ONLINE !!!! <<----------****************
* Indentation added by Ted Gaunt (aka Qixx) [email protected]
* help files added by Ted Gaunt
* '^' added by Ted Gaunt
* Note: this version compatible with v.3.0.34 (and probably all others too)
* but i've only tested it on v.3 please mail me if it works on your mud
* and if you like it!
*/
int version = 5; /* used only in the "set" function, for i.d. */
#include <stdio.h>
#include <string.h>
#ifdef __STDC__
#include <memory.h>
#endif
#include <sys/types.h> /* need for netinet */
/* #include <netinet/in.h> Included in comm.h */
#include <ctype.h>
/* Regexp is Henry Spencer's package. WARNING: regsub is modified to return
* a pointer to the \0 after the destination string, and this program refers
* to the "private" reganch field in the struct regexp.
*/
#include "lint.h"
#include "regexp.h"
#include "interpret.h"
#include "object.h"
#include "config.h"
#include "exec.h"
#include "comm.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 ESCAPE '\\'
#define TAB '\t' /* added by Qixx for indentation */
#define LB '{'
#define RB '}'
#define LC '('
#define RC ')'
#define LS '['
#define RS ']'
#define PP '\"'
#define EOL '\0'
#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 MEM_FAIL (ERR-5)
#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 256 /* max file name size */
/** Global variables **/
extern struct program *current_prog;
struct line {
int l_stat; /* empty, mark */
struct line *l_prev;
struct line *l_next;
char l_buff[1];
};
typedef struct line LINE;
extern struct object *command_giver;
void set_prompt PROT((char *));
#ifndef toupper
extern int toupper PROT((int));
#endif
int doprnt PROT((int, int));
int ins PROT((char *));
int deflt PROT((int, int));
static int strip_buff PROT((int line,char *buff2));
static void print_help PROT((int arg));
static void print_help2 PROT((void));
static void count_blanks PROT((int line));
static void _count_blanks PROT((char *str, int blanks));
#define P_DIAG (command_giver->interactive->ed_buffer->diag)
#define P_TRUNCFLG (command_giver->interactive->ed_buffer->truncflg)
#define P_NONASCII (command_giver->interactive->ed_buffer->nonascii)
#define P_NULLCHAR (command_giver->interactive->ed_buffer->nullchar)
#define P_TRUNCATED (command_giver->interactive->ed_buffer->truncated)
#define P_FNAME (command_giver->interactive->ed_buffer->fname)
#define P_FCHANGED (command_giver->interactive->ed_buffer->fchanged)
#define P_NOFNAME (command_giver->interactive->ed_buffer->nofname)
#define P_MARK (command_giver->interactive->ed_buffer->mark)
#define P_OLDPAT (command_giver->interactive->ed_buffer->oldpat)
#define P_LINE0 (command_giver->interactive->ed_buffer->Line0)
#define P_LINE0 (command_giver->interactive->ed_buffer->Line0)
#define P_CURLN (command_giver->interactive->ed_buffer->CurLn)
#define P_CURPTR (command_giver->interactive->ed_buffer->CurPtr)
#define P_LASTLN (command_giver->interactive->ed_buffer->LastLn)
#define P_LINE1 (command_giver->interactive->ed_buffer->Line1)
#define P_LINE2 (command_giver->interactive->ed_buffer->Line2)
#define P_NLINES (command_giver->interactive->ed_buffer->nlines)
#define P_SHIFTWIDTH (command_giver->interactive->ed_buffer->shiftwidth)
/* shiftwidth is meant to be a 4-bit-value that can be packed into an int
along with flags, therefore masks 0x1 ... 0x8 are reserved. */
#define P_FLAGS (command_giver->interactive->ed_buffer->flags)
#define NFLG_MASK 0x0010
#define P_NFLG ( P_FLAGS & NFLG_MASK )
#define LFLG_MASK 0x0020
#define P_LFLG ( P_FLAGS & LFLG_MASK )
#define PFLG_MASK 0x0040
#define P_PFLG ( P_FLAGS & PFLG_MASK )
#define EIGHTBIT_MASK 0x0080
#define P_EIGHTBIT ( P_FLAGS & EIGHTBIT_MASK )
#define AUTOINDFLG_MASK 0x0100
#define P_AUTOINDFLG ( P_FLAGS & AUTOINDFLG_MASK )
#define EXCOMPAT_MASK 0x0200
#define P_EXCOMPAT ( P_FLAGS & EXCOMPAT_MASK )
#define SHIFTWIDTH_MASK 0x000f
#define ALL_FLAGS_MASK 0x03f0
#define P_APPENDING (command_giver->interactive->ed_buffer->appending)
#define P_MORE (command_giver->interactive->ed_buffer->moring)
#define P_LEADBLANKS (command_giver->interactive->ed_buffer->leading_blanks)
#define P_CUR_AUTOIND (ED_BUFFER->cur_autoindent)
#define ED_BUFFER (command_giver->interactive->ed_buffer)
char inlin[MAXLINE];
char *inptr; /* tty input buffer */
struct ed_buffer {
int diag; /* diagnostic-output? flag */
int truncflg; /* truncate long line flag */
int nonascii; /* count of non-ascii chars read */
int nullchar; /* count of null chars read */
int truncated; /* count of lines truncated */
char fname[MAXFNAME];
int fchanged; /* file-changed? flag */
int nofname;
int mark['z'-'a'+1];
regexp *oldpat;
LINE Line0;
int CurLn;
LINE *CurPtr; /* CurLn and CurPtr must be kept in step */
int LastLn;
int Line1, Line2, nlines;
int flags;
#if 0
int eightbit; /* save eighth bit */
int nflg; /* print line number flag */
int lflg; /* print line in verbose mode */
int pflg; /* print current line after each command */
#endif
int appending;
int moring; /* used for the wait line of help */
char *exit_fn; /* Function to be called when player exits */
struct object *exit_ob; /* in this object */
int shiftwidth;
int leading_blanks;
int cur_autoindent;
};
struct tbl {
char *t_str;
int t_and_mask;
int t_or_mask;
} *t, tbl[] = {
"number", ~FALSE, NFLG_MASK,
"nonumber", ~NFLG_MASK, FALSE,
"list", ~FALSE, LFLG_MASK,
"nolist", ~LFLG_MASK, FALSE,
"print", ~FALSE, PFLG_MASK,
"noprint", ~PFLG_MASK, FALSE,
"eightbit", ~FALSE, EIGHTBIT_MASK,
"noeightbit", ~EIGHTBIT_MASK, FALSE,
"autoindent", ~FALSE, AUTOINDFLG_MASK,
"noautoindent", ~AUTOINDFLG_MASK, FALSE,
"excompatible", ~FALSE, EXCOMPAT_MASK,
"noexcompatible",~EXCOMPAT_MASK,FALSE,
0
};
/*-------------------------------------------------------------------------*/
#ifndef _AIX
extern char *strcpy(), *strncpy();
#endif
extern char *xalloc();
extern LINE *getptr();
extern char *gettxt();
extern char *gettxtl();
extern char *catsub();
extern void prntln(), putcntl(), error();
regexp *optpat();
/*________ Macros ________________________________________________________*/
#ifndef max
# define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
#define nextln(l) ((l)+1 > P_LASTLN ? 0 : (l)+1)
#define prevln(l) ((l)-1 < 0 ? P_LASTLN : (l)-1)
#define gettxtl(lin) ((lin)->l_buff)
#define gettxt(num) (gettxtl( getptr(num) ))
#define getnextptr(p) ((p)->l_next)
#define getprevptr(p) ((p)->l_prev)
#define setCurLn( lin ) ( P_CURPTR = getptr( P_CURLN = (lin) ) )
#define nextCurLn() ( P_CURLN = nextln(P_CURLN), P_CURPTR = getnextptr( P_CURPTR ) )
#define prevCurLn() ( P_CURLN = prevln(P_CURLN), P_CURPTR = getprevptr( P_CURPTR ) )
#define clrbuf() del(1, P_LASTLN)
#define Skip_White_Space {while (*inptr==SP || *inptr==HT) inptr++;}
#define relink(a, x, y, b) { (x)->l_prev = (a); (y)->l_next = (b); }
/*________ functions ______________________________________________________*/
/* append.c */
int append(line, glob)
int line, glob;
{
if(glob)
return(ERR);
setCurLn( line );
P_APPENDING = 1;
if(P_NFLG)
add_message("%6d. ",P_CURLN+1);
if (P_CUR_AUTOIND)
add_message("%*s",P_LEADBLANKS,"");
set_prompt("*\b");
return 0;
}
int more_append(str)
char *str;
{
if(str[0] == '.' && str[1] == '\0') {
P_APPENDING = 0;
set_prompt(":");
return(0);
}
if(P_NFLG)
add_message("%6d. ",P_CURLN+2);
if ( P_CUR_AUTOIND )
{
int i;
int less_indent_flag = 0;
while ( *str=='\004' || *str == '\013' )
{
str++;
P_LEADBLANKS-=P_SHIFTWIDTH;
if ( P_LEADBLANKS < 0 ) P_LEADBLANKS=0;
less_indent_flag=1;
}
for ( i=0; i < P_LEADBLANKS; ) inlin[i++]=' ';
strncpy(inlin+P_LEADBLANKS,str,MAXLINE-P_LEADBLANKS);
inlin[MAXLINE-1]='\0';
_count_blanks(inlin,0);
add_message("%*s",P_LEADBLANKS,"");
if ( !*str && less_indent_flag ) return 0;
str=inlin;
}
if( ins(str) < 0)
return( MEM_FAIL );
return 0;
}
static void count_blanks(line)
int line;
{
_count_blanks(gettxtl(getptr(line)), 0);
}
static void _count_blanks(str,blanks)
char *str;
int blanks;
{
for ( ; *str; str++ )
{
if ( *str == ' ' ) blanks++;
else if ( *str == '\t' ) blanks += 8 - blanks % 8 ;
else break;
}
P_LEADBLANKS = blanks<MAXLINE ? blanks : MAXLINE ;
}
/* ckglob.c */
int ckglob()
{
regexp *glbpat;
char c, delim, *lin;
int num;
LINE *ptr;
c = *inptr;
if(c != 'g' && c != 'v')
return(0);
if (deflt(1, P_LASTLN) < 0)
return(ERR);
delim = *++inptr;
if(delim <= ' ')
return(ERR);
glbpat = optpat();
if(*inptr == delim)
inptr++;
ptr = getptr(1);
for (num=1; num<=P_LASTLN; num++) {
ptr->l_stat &= ~LGLOB;
if (P_LINE1 <= num && num <= P_LINE2) {
/* we might have got a NULL pointer if the
supplied pattern was invalid */
if (glbpat) {
lin = gettxtl(ptr);
if(regexec(glbpat, lin )) {
if (c=='g') ptr->l_stat |= LGLOB;
} else {
if (c=='v') ptr->l_stat |= LGLOB;
}
}
ptr = getnextptr(ptr);
}
}
return(1);
}
/* deflt.c
* Set P_LINE1 & P_LINE2 (the command-range delimiters) if the file is
* empty; Test whether they have valid values.
*/
int deflt(def1, def2)
int def1, def2;
{
if(P_NLINES == 0) {
P_LINE1 = def1;
P_LINE2 = def2;
}
return ( (P_LINE1>P_LINE2 || P_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
* ... It could check to<=P_LASTLN ... igp
*/
int del(from, to)
int from, to;
{
LINE *first, *last, *next, *tmp;
if(from < 1)
from = 1;
first = getprevptr( getptr( from ) );
last = getnextptr( getptr( to ) );
next = first->l_next;
while(next != last && next != &P_LINE0) {
tmp = next->l_next;
free((char *)next);
next = tmp;
}
relink(first, last, first, last);
P_LASTLN -= (to - from)+1;
setCurLn( prevln(from) );
return(0);
}
int dolst(line1, line2)
int line1, line2;
{
int oldflags=P_FLAGS, p;
P_FLAGS |= LFLG_MASK;
p = doprnt(line1, line2);
P_FLAGS = oldflags;
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.
*/
int esc(s)
char **s;
{
register int rval;
if (**s != ESCAPE) {
rval = **s;
} else {
(*s)++;
switch(islower(**s) ? toupper(**s) : **s) {
case '\000':
rval = ESCAPE; 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);
}
/* doprnt.c */
int doprnt(from, to)
int from, to;
{
from = (from < 1) ? 1 : from;
to = (to > P_LASTLN) ? P_LASTLN : to;
if(to != 0) {
setCurLn( from );
while( P_CURLN <= to ) {
prntln( gettxtl( P_CURPTR ), P_LFLG, (P_NFLG ? P_CURLN : 0));
if( P_CURLN == to )
break;
nextCurLn();
}
}
return(0);
}
void prntln(str, vflg, lin)
char *str;
int vflg, lin;
{
if(lin)
add_message("%7d ",lin);
while(*str && *str != NL) {
if(*str < ' ' || *str >= 0x7f) {
switch(*str) {
case '\t':
if(vflg)
putcntl(*str);
else
add_message("%c", *str);
break;
case DEL:
add_message("^?");
break;
default:
putcntl(*str);
break;
}
} else
add_message("%c", *str);
str++;
}
if(vflg)
add_message("$");
add_message("\n");
}
void putcntl(c)
char c;
{
add_message("^%c",(c&31)|'@');
}
/* egets.c */
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 = EOS;
if(count)
add_message("[Incomplete last line]\n");
return(count);
}
else if(c == NL) {
*cp = EOS;
return(++count);
}
else if (c == 0)
P_NULLCHAR++; /* count nulls */
else {
if(c > 127) {
if(!P_EIGHTBIT) /* if not saving eighth bit */
c = c&127; /* strip eigth bit */
P_NONASCII++; /* count it */
}
*cp++ = c; /* not null, keep it */
count++;
}
}
str[count-1] = EOS;
if(c != NL) {
add_message("truncating line\n");
P_TRUNCATED++;
while((c = getc(stream)) != EOF)
if(c == NL)
break;
}
return(count);
} /* egets */
int doread(lin, fname)
int lin;
char *fname;
{
FILE *fp;
int err;
unsigned long bytes;
unsigned int lines;
static char str[MAXLINE];
err = 0;
P_NONASCII = P_NULLCHAR = P_TRUNCATED = 0;
if (P_DIAG) add_message("\"%s\" ",fname);
if( (fp = fopen(fname, "r")) == NULL ) {
add_message(" isn't readable.\n");
return( ERR );
}
setCurLn( lin );
for(lines = 0, bytes = 0;(err = egets(str,MAXLINE,fp)) > 0;) {
bytes += err;
if(ins(str) < 0) {
err = MEM_FAIL;
break;
}
lines++;
}
fclose(fp);
if(err < 0)
return(err);
if (P_DIAG) {
add_message("%u lines %u bytes",lines,bytes);
if(P_NONASCII)
add_message(" [%d non-ascii]",P_NONASCII);
if(P_NULLCHAR)
add_message(" [%d nul]",P_NULLCHAR);
if(P_TRUNCATED)
add_message(" [%d lines truncated]",P_TRUNCATED);
add_message("\n");
}
return( err );
} /* doread */
int dowrite(from, to, fname, apflg)
int from, to;
char *fname;
int apflg;
{
FILE *fp;
int lin, err;
unsigned int lines;
unsigned long bytes;
char *str;
LINE *lptr;
err = 0;
lines = bytes = 0;
add_message("\"%s\" ",fname);
if((fp = fopen(fname,(apflg?"a":"w"))) == NULL) {
add_message(" can't be opened for writing!\n");
return( ERR );
}
lptr = getptr(from);
for(lin = from; lin <= to; lin++) {
str = lptr->l_buff;
lines++;
bytes += strlen(str) + 1; /* str + '\n' */
if(fputs(str, fp) == EOF) {
add_message("file write error\n");
err++;
break;
}
fputc('\n', fp);
lptr = lptr->l_next;
}
add_message("%u lines %lu bytes\n",lines,bytes);
fclose(fp);
return( err );
} /* dowrite */
/* find.c */
int find(pat, dir)
regexp *pat;
int dir;
{
int i, num;
LINE *lin;
dir ? nextCurLn() : prevCurLn() ;
num = P_CURLN;
lin = P_CURPTR;
/* if the typed in pattern was invalid we have a NULL pointer! */
if (!pat) return ( ERR );
for(i=0; i<P_LASTLN; i++ ) {
if(regexec( pat, gettxtl( lin ) ))
return(num);
if( dir )
num = nextln(num), lin = getnextptr(lin);
else
num = prevln(num), lin = getprevptr(lin);
}
return ( ERR );
}
#if 0
/* findg.c by Ted Gaunt for global searches.... much like 'grep'
especially useful when line numbering is turned on.
*/
int findg(pat, dir)
regexp *pat;
int dir;
{
int i, num,count;
LINE *lin;
count=0;
num = P_CURLN;
lin = P_CURPTR;
for(i=0; i<P_LASTLN; i++ ) {
if(regexec( pat, gettxtl( lin ) ))
{prntln( gettxtl( lin ), P_LFLG, (P_NFLG ? P_CURLN : 0));
count++;}
if( dir )
num = nextln(num), lin = getnextptr(lin);
else
num = prevln(num), lin = getprevptr(lin);
nextCurLn();
}
if (!count)
return ( ERR );
else return (count);
}
#endif /* 0 */
/* getfn.c */
char *getfn(writeflg)
int writeflg;
{
static char file[MAXFNAME];
char *cp;
char *file2;
#ifndef COMPAT_MODE
struct svalue *ret;
#endif
if(*inptr == NL) {
P_NOFNAME=TRUE;
file[0] = '/';
strcpy(file+1, P_FNAME);
} else {
P_NOFNAME=FALSE;
Skip_White_Space;
cp = file;
while(*inptr && *inptr != NL && *inptr != SP && *inptr != HT)
*cp++ = *inptr++;
*cp = '\0';
}
if(strlen(file) == 0) {
add_message("bad file name\n");
return( NULL );
}
#ifdef COMPAT_MODE
file2 = check_file_name(file, writeflg);
#else
if (file[0] != '/') {
push_string(file, STRING_MALLOC);
ret = apply_master_ob("make_path_absolute", 1);
if (ret == 0 || ret->type != T_STRING)
return NULL;
strncpy(file, ret->u.string, sizeof file - 1);
}
file2 = check_valid_path(file, command_giver->eff_user,
"ed_start", writeflg);
#endif
if (!file2)
return( NULL );
strncpy(file, file2, MAXFNAME-1);
file[MAXFNAME-1] = 0;
if(strlen(file) == 0) {
add_message("no file name\n");
return(NULL);
}
return( file );
} /* getfn */
int getnum(first)
int first;
{
regexp *srchpat;
int num;
char c;
Skip_White_Space;
if(*inptr >= '0' && *inptr <= '9') { /* line number */
for(num = 0; *inptr >= '0' && *inptr <= '9'; ++inptr) {
num = (num * 10) + (*inptr - '0');
}
return num;
}
switch(c = *inptr) {
case '.':
inptr++;
return (P_CURLN);
case '$':
inptr++;
return (P_LASTLN);
case '/':
case '?':
srchpat = optpat();
if(*inptr == c)
inptr++;
return(find(srchpat,c == '/'?1:0));
#if 0
case '^': /* for grep-like searching */
case '&':
srchpat = optpat();
if(*inptr == c)
inptr++;
return(findg(srchpat,c == '^'?1:0));
#endif
case '-':
case '+':
return(first ? P_CURLN : 1);
case '\'':
inptr++;
if (*inptr < 'a' || *inptr > 'z')
return(EOF);
return P_MARK[ *inptr++ - 'a' ];
default:
return ( first ? EOF : 1 ); /* unknown address */
}
} /* getnum */
/* getone.c
* Parse a number (or arithmetic expression) off the command line.
*/
#define FIRST 1
#define NOTFIRST 0
int getone()
{
int c, i, num;
if((num = getnum(FIRST)) >= 0) {
for (;;) {
Skip_White_Space;
if(*inptr != '+' && *inptr != '-')
break; /* exit infinite loop */
c = *inptr++;
if((i = getnum(NOTFIRST)) < 0)
return ( i );
if(c == '+')
num += i;
else
num -= i;
}
}
return ( num>P_LASTLN ? ERR : num );
} /* getone */
int getlst()
{
int num;
P_LINE2 = 0;
for(P_NLINES = 0; (num = getone()) >= 0;)
{
P_LINE1 = P_LINE2;
P_LINE2 = num;
P_NLINES++;
if(*inptr != ',' && *inptr != ';')
break;
if(*inptr == ';')
setCurLn( num );
inptr++;
}
P_NLINES = min(P_NLINES, 2);
if(P_NLINES == 0)
P_LINE2 = P_CURLN;
if(P_NLINES <= 1)
P_LINE1 = P_LINE2;
return ( (num == ERR) ? num : P_NLINES );
} /* getlst */
/* getptr.c */
LINE *getptr(num)
int num;
{
LINE *ptr;
int j;
if (2*num>P_LASTLN && num<=P_LASTLN) { /* high line numbers */
ptr = P_LINE0.l_prev;
for (j = P_LASTLN; j>num; j--)
ptr = ptr->l_prev;
} else { /* low line numbers */
ptr = &P_LINE0;
for(j = 0; j < num; j++)
ptr = ptr->l_next;
}
return(ptr);
}
/* getrhs.c */
int getrhs(sub)
char *sub;
{
char delim = *inptr++;
char *outmax = sub + MAXPAT;
if( delim == NL || *inptr == NL) /* check for eol */
return( ERR );
while( *inptr != delim && *inptr != NL ) {
if ( sub > outmax )
return ERR;
if ( *inptr == ESCAPE ) {
switch ( *++inptr ) {
case 'r':
*sub++ = '\r';
inptr++;
break;
#if 0
case ESCAPE:
*sub++ = ESCAPE;
*sub++ = ESCAPE;
inptr++;
#endif
case 'n':
*sub++ = '\n';
inptr++;
break;
case 'b':
*sub++ = '\b';
inptr++;
break;
case 't':
*sub++ = '\t';
inptr++;
break;
case '0': {
int i=3;
*sub = 0;
do {
if (*++inptr<'0' || *inptr >'7')
break;
*sub = (*sub<<3) | (*inptr-'0');
} while (--i!=0);
sub++;
} break;
#if 0
default:
if ( *inptr != delim )
*sub++ = ESCAPE;
#else
case '&':
case '1':
case '2':
case '3':
case '4':
case '5':