-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathed.c
3160 lines (2800 loc) · 89.1 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) paradox@mcs.anl.gov
* 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!
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
* Dworkin rewrote the Indentation algorithm, originally for Amylaar's driver,
* around 5/20/1992.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
* Inspiral grabbed indentation code from Amylaar, hacked it into MudOS version
* 0.9.18.17, around 12/11/1993. Added some hacks to provide proper support
* for "//" commenting, and took out a PROMPT define.
*
* -------------------------------------------------------------------------
* Beek fixed Inspirals hacks, then later added the object_ed_* stuff.
* ------------------------------------------------------------------------
* Isaac Charles (Hamlet) did some reworks 2008/March
* 1) Many ED_OUTPUTV() things combined for less calls to the output stuff.
* (this will be important with future changes and shouldn't hurt now)
* 2) added an APPLY_RECEIVE_ED to send output within the user ob before
* outputting to the user.
* Prototype: mixed receive_ed(string msg, string fname);
* If return is 0, it is a "pass" and normal output occurs. If return
* is 1, we assume receive_ed() called receive() or something else
* clever and the text is taken care of. If return is a string,
* that string is sent to the user rather than the original.
* If fname is 0, the incoming string is help text. In this case,
* an array of strings may also be returned, which will be separated by
* "more" statements. As above, if 1 is returned, no output, and if 0
* is returned, normal output.
* 3) ed_start() and ed() now take an extra final argument telling
* how many lines on the user's screen. 'z' commands will then scroll
* that many lines (less 1 for the prompt). 'Z' commands scroll that many
* plus 5. Why? I dunno. Why was it double before? f_ed() and
* f_ed_start() do not require this arg.
* 5) Minor fixes: fixed indentation for foreach(), and for lines with only
* //.
* 6) Minor enhancements: z++ and z-- now work for double-forward or back.
* Added ED_INDENT_CASE (should 'case' be indented after switch?) and
* ED_INDENT_SPACES (how many spaces to indent with I) as defines in
* options.h. Their presence is optional. All additions to options.h
* are optional. Default behavior is mostly the same as the original ed.
*/
#include "std.h"
#include "ed.h"
#include "comm.h"
#include "file.h"
#include "master.h"
#ifndef ED_INDENT_SPACES
#define ED_INDENT_SPACES 4
#endif
#ifndef ED_TAB_WIDTH
#define ED_TAB_WIDTH 8
#endif
/* 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.
*/
/* if you don't want ed, define OLD_ED and remove ed() from func_spec.c */
#if defined(F_ED) || !defined(OLD_ED)
/** Global variables **/
static int version = 601;
static int EdErr = 0;
static int append (int, int);
static int more_append (const char *);
static int ckglob (void);
static int deflt (int, int);
static int del (int, int);
static int dolst (int, int);
/* This seems completely unused; -Beek
static int esc (char **);
*/
static void do_output(char *);
static int doprnt (int, int);
static int prntln (char *, char *, int, int);
static int egets (char *, int, FILE *);
static int doread (int, const char *);
static int dowrite (int, int, const char *, int);
static int find (regexp *, int);
static char *getfn (int);
static int getnum (int);
static int getone (void);
static int getlst (void);
static ed_line_t *getptr (int);
static int getrhs (char *);
static int ins (const char *);
static int join (int, int);
static int move (int);
static int transfer (int);
static regexp *optpat (void);
static int set (void);
static void set_ed_buf (void);
static int subst (regexp *, char *, int, int);
static int docmd (int);
static int doglob (void);
static void free_ed_buffer (object_t *);
static void shift (char *);
static void indent (char *);
static int indent_code (void);
static void report_status (int);
#ifndef OLD_ED
static char *object_ed_results (void);
static ed_buffer_t *add_ed_buffer (object_t *);
static void object_free_ed_buffer (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 ed_buffer_t *current_ed_buffer;
static object_t *current_editor; /* the object responsible */
outbuffer_t current_ed_results;
#define ED_BUFFER (current_ed_buffer)
#ifdef OLD_ED
#define P_NET_DEAD (command_giver->interactive->iflags & NET_DEAD)
#else
#define P_NET_DEAD 0 /* objects are never net dead :) */
#endif
#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_HELPOUT (ED_BUFFER->helpout)
/* 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_SHIFTWIDTH (ED_BUFFER->shiftwidth)
#define P_FLAGS (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 DPRINT_MASK 0x0400
#define P_DPRINT ( P_FLAGS & DPRINT_MASK )
#define VERBOSE_MASK 0x0800
#define P_VERBOSE ( P_FLAGS & VERBOSE_MASK )
#define SHIFTWIDTH_MASK 0x000f
#define ALL_FLAGS_MASK 0x0ff0
#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)
#define P_RESTRICT (ED_BUFFER->restricted)
static char inlin[ED_MAXLINE];
static char *inptr; /* tty input buffer */
static struct tbl {
const 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 },
{ "dprint", ~FALSE, DPRINT_MASK },
{ "nodprint", ~DPRINT_MASK, FALSE },
{ "verbose", ~FALSE, VERBOSE_MASK },
{ "noverbose", ~VERBOSE_MASK, FALSE },
};
/*________ 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 */
static int append (int line, int glob)
{
if (glob)
return SYNTAX_ERROR;
setCurLn(line);
P_APPENDING = 1;
if (P_NFLG)
ED_OUTPUTV(ED_DEST, "%6d. ", P_CURLN + 1);
if (P_CUR_AUTOIND)
ED_OUTPUTV(ED_DEST, "%*s", P_LEADBLANKS, "");
#ifdef OLD_ED
set_prompt("*\b");
#endif
return 0;
}
static int more_append (const char *str)
{
if (str[0] == '.' && str[1] == '\0') {
P_APPENDING = 0;
#ifdef OLD_ED
set_prompt(":");
#endif
return (0);
}
if (P_NFLG)
ED_OUTPUTV(ED_DEST, "%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, ED_MAXLINE - P_LEADBLANKS);
inlin[ED_MAXLINE - 1] = '\0';
_count_blanks(inlin, 0);
ED_OUTPUTV(ED_DEST, "%*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 (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 += ED_TAB_WIDTH - blanks % ED_TAB_WIDTH;
else
break;
}
P_LEADBLANKS = blanks < ED_MAXLINE ? blanks : ED_MAXLINE;
}
/* ckglob.c */
static int ckglob()
{
regexp *glbpat;
char c, delim, *lin;
int num;
ed_line_t *ptr;
c = *inptr;
if (c != 'g' && c != 'v')
return (0);
if (deflt(1, P_LASTLN) < 0)
return (BAD_LINE_RANGE);
delim = *++inptr;
if (delim <= ' ')
return (EDERR);
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.
*/
static int deflt (int def1, int def2)
{
if (P_NLINES == 0) {
P_LINE1 = def1;
P_LINE2 = def2;
}
return ((P_LINE1 > P_LINE2 || P_LINE1 <= 0) ? EDERR : 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
* ... Ok...and it corrects from (or to) > P_LASTLN also -- robo
*/
static int del (int from, int to)
{
ed_line_t *first, *last, *next, *tmp;
if (P_LASTLN == 0)
return (0); /* nothing to delete */
if (from > P_LASTLN)
from = P_LASTLN;
if (to > P_LASTLN)
to = P_LASTLN;
if (from < 1)
from = 1;
if (to < 1)
to = 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);
}
static int dolst (int line1, int 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.
*/
/* UNUSED ----
static int esc (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 */
static int doprnt (int from, int to)
{
#if (BUFFER_SIZE) > (ED_MAXLINE)
char outbf[BUFFER_SIZE];
#else
char outbf[ED_MAXLINE];
#endif
char tmp[ED_MAXLINE];
char *pos = outbf;
int count;
from = (from < 1) ? 1 : from;
to = (to > P_LASTLN) ? P_LASTLN : to;
if (to != 0) {
setCurLn(from);
while (P_CURLN <= to) {
count = prntln(gettxtl(P_CURPTR), tmp, P_LFLG,
(P_NFLG ? P_CURLN : 0));
if (P_NET_DEAD)
break;
#if (BUFFER_SIZE) > (ED_MAXLINE)
if((pos + count + 1) >= (outbf + BUFFER_SIZE)) {
#else
if((pos + count + 1) >= (outbf + ED_MAXLINE)) {
#endif
do_output(outbf);
pos = outbf;
}
if(pos > outbf)
*(pos++) = '\n';
strcpy(pos, tmp);
pos += count;
if(P_CURLN == to) {
do_output(outbf);
break;
}
nextCurLn();
}
}
return (0);
}
static void do_output(char *str) {
#ifdef RECEIVE_ED
svalue_t *ret;
copy_and_push_string(str);
copy_and_push_string(P_FNAME);
// One could argue that this should be safe_apply()
// Pro: ed continues to work with a runtiming receive_ed()
// Con: "I wrote a receive_ed()! Why does the driver ignore it??"
ret = apply(APPLY_RECEIVE_ED, ED_DEST, 2, ORIGIN_DRIVER);
if(!ret)
ED_OUTPUTV(ED_DEST, "%s\n", str);
else if(ret->type == T_NUMBER) {
// if 0, output ourselves, else they handled it, do nothing.
if(ret->u.number == 0) // "pass"
ED_OUTPUTV(ED_DEST, "%s\n", str);
}
else if(ret->type == T_STRING) {
ED_OUTPUTV(ED_DEST, "%s\n", ret->u.string);
}
#else
ED_OUTPUTV(ED_DEST, "%s\n", str);
#endif
}
static void free_ed_buffer (object_t * who)
{
clrbuf();
#ifdef OLD_ED
if (ED_BUFFER->write_fn) {
object_t *exit_ob = ED_BUFFER->exit_ob;
FREE(ED_BUFFER->write_fn);
free_object(&exit_ob, "ed EOF");
}
if (ED_BUFFER->exit_fn) {
char *exit_fn = ED_BUFFER->exit_fn;
object_t *exit_ob = ED_BUFFER->exit_ob;
if (P_OLDPAT)
FREE((char *) P_OLDPAT);
FREE((char *) ED_BUFFER);
who->interactive->ed_buffer = 0;
set_prompt("> ");
/* make this "safe" */
safe_apply(exit_fn, exit_ob, 0, ORIGIN_INTERNAL);
FREE(exit_fn);
free_object(&exit_ob, "ed EOF");
return;
}
#endif
if (P_OLDPAT)
FREE((char *) P_OLDPAT);
#ifdef OLD_ED
FREE((char *) ED_BUFFER);
who->interactive->ed_buffer = 0;
set_prompt("> ");
#else
object_free_ed_buffer();
#endif
return;
}
#define putcntl(X) *line++ = '^'; *line++ = (X) ? ((*str&31)|'@') : '?'
static int prntln (char * str, char * outstr, int vflg, int lineno)
{
char *line, start[ED_MAXLINE + 100]; //need space for the bloody tabs
line = start;
if (lineno) {
sprintf(line, "%3d ", lineno);
line += 5;
}
while (*str && *str != NL) {
if ((line - start) >= (ED_MAXLINE-1)) {
line = start + ED_MAXLINE - 33;
line += sprintf(line, "messing up end of a long line\n");
break;
}
if (*str < ' ' || *str >= DEL) {
switch (*str) {
case '\t':
/* have to be smart about this or the indentor will fail */
*line++ = ' ';
while ((line - start) % ED_TAB_WIDTH)
*line++ = ' ';
break;
case DEL:
putcntl(0);
break;
default:
putcntl(1);
break;
}
} else
*line++ = *str;
str++;
}
#ifndef NO_END_DOLLAR_SIGN
if (vflg)
*line++ = '$';
#endif
*line = EOS;
strcpy(outstr, start);
return (line - start);
}
/* egets.c */
static 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)
ED_OUTPUT(ED_DEST, "[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) {
ED_OUTPUT(ED_DEST, "truncating line\n");
P_TRUNCATED++;
while ((c = getc(stream)) != EOF)
if (c == NL)
break;
}
return (count);
} /* egets */
static int doread (int lin, const char * fname)
{
FILE *fp;
int err;
unsigned int bytes;
unsigned int lines;
static char str[ED_MAXLINE];
err = 0;
P_NONASCII = P_NULLCHAR = P_TRUNCATED = 0;
if (P_VERBOSE)
ED_OUTPUTV(ED_DEST, "\"%s\" ", fname);
if ((fp = fopen(fname, "r")) == NULL) {
ED_OUTPUT(ED_DEST, " isn't readable.\n");
return EDERR;
}
setCurLn(lin);
for (lines = 0, bytes = 0; (err = egets(str, ED_MAXLINE, fp)) > 0;) {
bytes += err;
if (ins(str) < 0) {
err = MEM_FAIL;
break;
}
lines++;
}
fclose(fp);
if (err < 0)
return (err);
if (P_VERBOSE) {
ED_OUTPUTV(ED_DEST, "%u lines %u bytes", lines, bytes);
if (P_NONASCII)
ED_OUTPUTV(ED_DEST, " [%d non-ascii]", P_NONASCII);
if (P_NULLCHAR)
ED_OUTPUTV(ED_DEST, " [%d nul]", P_NULLCHAR);
if (P_TRUNCATED)
ED_OUTPUTV(ED_DEST, " [%d lines truncated]", P_TRUNCATED);
ED_OUTPUT(ED_DEST, "\n");
}
return (err);
} /* doread */
static int dowrite (int from, int to, const char * fname, int apflg)
{
FILE *fp;
int lin, err;
unsigned int lines;
unsigned int bytes;
char *str;
ed_line_t *lptr;
err = 0;
lines = bytes = 0;
#ifdef OLD_ED
if (ED_BUFFER->write_fn) {
svalue_t *res;
push_malloced_string(add_slash(fname));
push_number(0);
res = safe_apply(ED_BUFFER->write_fn, ED_BUFFER->exit_ob, 2, ORIGIN_INTERNAL);
if (IS_ZERO(res))
return (EDERR);
}
#endif
if (!P_RESTRICT)
ED_OUTPUTV(ED_DEST, "\"/%s\" ", fname);
if ((fp = fopen(fname, (apflg ? "a" : "w"))) == NULL) {
if (!P_RESTRICT)
ED_OUTPUT(ED_DEST, " can't be opened for writing!\n");
else
ED_OUTPUT(ED_DEST, "Couldn't open file for writing!\n");
return EDERR;
}
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) {
ED_OUTPUT(ED_DEST, "file write error\n");
err++;
break;
}
fputc('\n', fp);
lptr = lptr->l_next;
}
if (!P_RESTRICT)
ED_OUTPUTV(ED_DEST, "%u lines %lu bytes\n", lines, bytes);
fclose(fp);
#ifdef OLD_ED
if (ED_BUFFER->write_fn) {
push_malloced_string(add_slash(fname));
push_number(1);
safe_apply(ED_BUFFER->write_fn, ED_BUFFER->exit_ob, 2, ORIGIN_INTERNAL);
}
#endif
return (err);
} /* dowrite */
/* find.c */
static int find (regexp * pat, int dir)
{
int i, num;
ed_line_t *lin;
if (!pat)
return (EDERR);
dir ? nextCurLn() : prevCurLn();
num = P_CURLN;
lin = P_CURPTR;
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);
}
/* Put us back to where we came from */
dir ? prevCurLn() : nextCurLn();
return (SEARCH_FAILED);
} /* find() */
/* getfn.c */
static char *getfn (int writeflg)
{
static char file[MAXFNAME];
char *cp;
const char *file2;
svalue_t *ret;
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) {
ED_OUTPUT(ED_DEST, "Bad file name.\n");
return (0);
}
if (file[0] != '/') {
copy_and_push_string(file);
ret = apply_master_ob(APPLY_MAKE_PATH_ABSOLUTE, 1);
if ((ret == 0) || (ret == (svalue_t *)-1) || ret->type != T_STRING)
return NULL;
strncpy(file, ret->u.string, sizeof file - 1);
file[MAXFNAME - 1] = '\0';
}
/* valid_read/valid_write done here */
file2 = check_valid_path(file, current_editor, "ed_start", writeflg);
if (!file2)
return (NULL);
strncpy(file, file2, MAXFNAME - 1);
file[MAXFNAME - 1] = 0;
if (*file == 0) {
ED_OUTPUT(ED_DEST, "no file name\n");
return (NULL);
}
return (file);
} /* getfn */
static 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));
case '-':
case '+':
return (first ? P_CURLN : 1);
case '\'':
inptr++;
if (*inptr < 'a' || *inptr > 'z')
return MARK_A_TO_Z;
return P_MARK[*inptr++ - 'a'];
default:
return (first ? NO_LINE_RANGE : 1); /* unknown address */
}
} /* getnum */
/* getone.c
* Parse a number (or arithmetic expression) off the command line.
*/
#define FIRST 1
#define NOTFIRST 0
static 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;
if (num < 1) num = 1;
}
}
}
return (num > P_LASTLN ? BAD_LINE_NUMBER : num);
} /* getone */
static int getlst()
{
int num;
P_LINE2 = 0;
for (P_NLINES = 0; (num = getone()) >= 0 || (num == BAD_LINE_NUMBER);) {
/* if it's out of bounds, go to the end of the file. */
if (num == BAD_LINE_NUMBER)
num = P_LASTLN;
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 < 0) ? num : P_NLINES);
} /* getlst */
/* getptr.c */
static ed_line_t *getptr (int num)
{
ed_line_t *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;