forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed.c
2834 lines (2542 loc) · 78.7 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".
* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
* Changed the program to use more of the #define's that are at the top of
* this file. Modified prntln() to print out a string instead of each
* individual character to make it easier to see if snooping a wizard who
* is in the editor. Got rid of putcntl() because of the change to strings
* instead of characters, and made a define with the name of putcntl()
* Incremented version to "6".
* Scott Grosch / Belgarath 08/10/91
* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
*
* ********--->> 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!
*/
/* A quick fix that hopefully does the job! -- Buddha */
#define PROMPT ":"
int version = 6; /* used only in the "set" function, for i.d. */
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <sys/types.h> /* need for netinet */
#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 "config.h"
#include "lint.h"
#include "regexp.h"
#include "interpret.h"
#include "object.h"
#include "exec.h"
#include "comm.h"
#include "comm1.h"
#include "mstring.h"
#include "mapping.h"
#include "simulate.h"
#include "inline_svalue.h"
/* define this if you don't like the ending dollar signs in ed, in n-mode */
#define NO_END_DOLLAR_SIGN
/*
* #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 UNSPEC_FAIL (ERR-6)
#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;
extern struct object *master_ob;
int EdErr = 0;
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 (char *);
#ifndef toupper
extern int toupper (int);
#endif
int doprnt (int, int);
int ins (char *);
int deflt (int, int);
int doglob (void);
int set (void);
int getlst (void);
int getone (void);
int ckglob (void);
void set_ed_buf (void);
void save_ed_buffer (void);
#ifdef ED_INDENT
static int indent_code (void);
#endif
static void print_help (int arg);
static void print_help2 (void);
static void count_blanks (int line);
static void _count_blanks (char *str, int blanks);
static void free_ed_buffer (void);
#define ED_BUFFER (command_giver->interactive->ed_buffer)
#define P_DIAG (ED_BUFFER->diag)
#define P_TRUNCFLG (ED_BUFFER->truncflg)
#define P_NONASCII (ED_BUFFER->nonascii)
#define P_NULLCHAR (ED_BUFFER->nullchar)
#define P_TRUNCATED (ED_BUFFER->truncated)
#define P_FNAME (ED_BUFFER->fname)
#define P_FCHANGED (ED_BUFFER->fchanged)
#define P_NOFNAME (ED_BUFFER->nofname)
#define P_MARK (ED_BUFFER->mark)
#define P_OLDPAT (ED_BUFFER->oldpat)
#define P_LINE0 (ED_BUFFER->Line0)
#define P_CURLN (ED_BUFFER->CurLn)
#define P_CURPTR (ED_BUFFER->CurPtr)
#define P_LASTLN (ED_BUFFER->LastLn)
#define P_LINE1 (ED_BUFFER->Line1)
#define P_LINE2 (ED_BUFFER->Line2)
#define P_NLINES (ED_BUFFER->nlines)
#define P_SHIFTWIDTH (ED_BUFFER->shiftwidth)
#define P_FLAGS (ED_BUFFER->flags)
#define P_APPENDING (ED_BUFFER->appending)
#define P_MORE (ED_BUFFER->moring)
#define P_LEADBLANKS (ED_BUFFER->leading_blanks)
#define P_CUR_AUTOIND (ED_BUFFER->cur_autoindent)
/* 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 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 TABINDENT_MASK 0x0400
#define P_TABINDENT ( P_FLAGS & TABINDENT_MASK )
#define SHIFTWIDTH_MASK 0x000f
#define ALL_FLAGS_MASK 0x07f0
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 */
struct closure *exit_func; /* function to call on exit */
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, },
{ "tabindent", ~FALSE, TABINDENT_MASK, },
{ "notabindent",~TABINDENT_MASK, FALSE, },
{ 0, 0, 0 },
};
/*-------------------------------------------------------------------------*/
extern LINE *getptr(int);
extern void prntln(char *, int, int), error(char *, ...);
regexp *optpat(void);
/*________ 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(int line, int glob)
{
if(glob)
return(ERR);
setCurLn( line );
P_APPENDING = 1;
if(P_NFLG)
(void)add_message("%6d. ",P_CURLN+1);
if (P_CUR_AUTOIND)
(void)add_message("%*s",P_LEADBLANKS,"");
set_prompt("*\b");
return 0;
}
void
more_append(char *str)
{
if(str[0] == '.' && str[1] == '\0')
{
P_APPENDING = 0;
set_prompt(PROMPT);
return;
}
if(P_NFLG)
(void)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++] = ' ';
(void)strncpy(inlin + P_LEADBLANKS, str, (size_t)(MAXLINE - P_LEADBLANKS));
inlin[MAXLINE-1] = '\0';
_count_blanks(inlin,0);
(void)add_message("%*s",P_LEADBLANKS,"");
if ( !*str && less_indent_flag )
return;
str = inlin;
}
if (strlen(str) >= MAXLINE) {
(void)add_message("[line too long, truncated]\n");
str[MAXLINE-1] = '\0';
}
(void)ins(str);
}
static void
count_blanks(int line)
{
_count_blanks(gettxtl(getptr(line)), 0);
}
static void
_count_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(int def1, int 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(int from, int 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(int line1, int line2)
{
int oldflags = P_FLAGS, p;
P_FLAGS |= LFLG_MASK;
p = doprnt(line1, line2);
P_FLAGS = oldflags;
return p;
}
/* doprnt.c */
int
doprnt(int from, int 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(char *str, int vflg, int len)
{
char *line, start[(MAXLINE << 1) + 2];
line = start;
if (len)
(void)add_message("%4d ", len);
while (*str && *str != NL)
{
if (*str < ' ' || *str >= DEL)
{
switch (*str)
{
case '\t':
*line++ = *str;
break;
case DEL:
*line++ = '^';
*line++ = '?';
break;
default:
*line++ = '^';
*line++ = (*str & 0x1F) | '@';
break;
}
}
else
*line++ = *str;
str++;
}
#if !defined(NO_END_DOLLAR_SIGN) || defined(lint)
if (vflg)
*line++ = '$';
#endif
*line = EOS;
(void)add_message("%s\n", start);
}
/* egets.c */
int
egets(char *str, int size, FILE *stream)
{
int c = 0, count;
char *cp;
for (count = 0, cp = str; size > count;)
{
c = getc(stream);
if(c == EOF)
{
*cp = EOS;
if(count)
(void)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)
{
(void)add_message("truncating line\n");
P_TRUNCATED++;
while((c = getc(stream)) != EOF)
if(c == NL)
break;
}
return(count);
} /* egets */
int
doread(int lin, char *fname)
{
FILE *fp;
int err;
unsigned long bytes;
unsigned int lines;
static char str[MAXLINE];
P_NONASCII = P_NULLCHAR = P_TRUNCATED = 0;
if (P_DIAG)
(void)add_message("\"%s\" ",fname);
if( (fp = fopen(fname, "r")) == NULL )
{
(void)add_message("'%s' isn't readable.\n", fname);
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++;
}
(void)fclose(fp);
if(err < 0)
return(err);
if (P_DIAG)
{
(void)add_message("%u lines %lu bytes",lines,bytes);
if(P_NONASCII)
(void)add_message(" [%d non-ascii]",P_NONASCII);
if(P_NULLCHAR)
(void)add_message(" [%d nul]",P_NULLCHAR);
if(P_TRUNCATED)
(void)add_message(" [%d lines truncated]",P_TRUNCATED);
(void)add_message("\n");
}
return( err );
} /* doread */
int
dowrite(int from, int to, char *fname, int apflg)
{
FILE *fp;
int lin, err;
unsigned int lines;
unsigned long bytes;
char *str;
LINE *lptr;
err = 0;
lines = 0;
bytes = 0;
(void)add_message("\"%s\" ",fname);
if((fp = fopen(fname,(apflg ? "a" : "w"))) == NULL)
{
(void)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)
{
(void)add_message("file write error\n");
err++;
break;
}
(void)fputc('\n', fp);
lptr = lptr->l_next;
}
(void)add_message("%u lines %lu bytes\n",lines,bytes);
(void)fclose(fp);
return( err );
} /* dowrite */
/* find.c */
int find(regexp *pat, int dir)
{
int i, num;
LINE *lin;
if (dir)
nextCurLn();
else
prevCurLn();
num = P_CURLN;
lin = P_CURPTR;
if (!pat)
return (ERR);
for(i = 0; i < P_LASTLN; i++ )
{
if(regexec( pat, gettxtl( lin ) ))
return(num);
if (EdErr) { EdErr = 0; break; }
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(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(int writeflg)
{
static char file[MAXFNAME];
char *cp;
char *file2;
struct svalue *ret;
if (*inptr == NL)
{
P_NOFNAME=TRUE;
file[0] = '/';
(void)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 (*file == '\0')
{
(void)add_message("Bad file name.\n");
return( NULL );
}
if (file[0] != '/')
{
push_string(file, STRING_MSTRING);
ret = apply_master_ob(M_MAKE_PATH_ABSOLUTE, 1);
if (ret == 0 || ret->type != T_STRING)
return NULL;
(void)strncpy(file, ret->u.string, sizeof file - 1);
}
file2 = check_valid_path(file, command_giver, "ed_start", writeflg);
if (!file2)
return NULL;
if (*file2 == '\0') {
add_message("No file name\n");
return NULL;
}
return file2;
} /* getfn */
int
getnum(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(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;