forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed.c
4022 lines (3424 loc) · 104 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
/*---------------------------------------------------------------------------
* Gamedriver - ed compatible 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.
*
* Regexp stuff replaced with Spencerian version, sundry other bugfix+speedups
* by Ian Phillipps. Regexps replaced again to transparently use PCRE
* instead of Spencer's regexps by Lars Duening.
*
* help files and '^' added by Ted Gaunt.
* Tab conversion added by Andreas Klauer.
*
* Original indentation algorithm replaced with adapted version from DGD
* editor by Dworkin (Felix A. Croes), 920510.
* The code may be used freely as long as its author and its origin from
* DGD are clearly stated.
*---------------------------------------------------------------------------
* Per interactive user there can be one active ed session; stored as
* a pointer to the ed_buffer in the interactive structure. The lines of
* the text are stored linewise in a double-linked ring.
*
* TODO: Make it possible to attach an editor to any object, and let
* TODO:: the editor communicate via efuns - no direct io.
* TODO: In the long run, maybe only offer primitives and let the command
* TODO:: interpretation be done by the mudlib. This makes it easier
* TODO:: to write non-ed like editors.
*---------------------------------------------------------------------------
*/
#define ED_VERSION 6 /* used only in outputs for id */
#include "driver.h"
#include "typedefs.h"
#include <stdio.h>
#include <ctype.h>
#include "ed.h"
#include "actions.h"
#include "comm.h"
#include "filestat.h"
#include "gcollect.h"
#include "interpret.h"
#include "lex.h"
#include "main.h"
#include "mregex.h"
#include "mstrings.h"
#include "object.h"
#include "simulate.h"
#include "stdstrings.h"
#include "svalue.h"
#include "xalloc.h"
#include "i-current_object.h"
#include "../mudlib/sys/regexp.h"
/*-------------------------------------------------------------------------*/
/* Default TAB size */
# define DEFAULT_TABSIZE 8
/* #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 '\\'
/* Characters used in the indentation code */
#define TAB '\t'
#define LB '{'
#define RB '}'
#define LC '('
#define RC ')'
#define LS '['
#define RS ']'
#define PP '\"'
#define EOL '\0'
/*-------------------------------------------------------------------------*/
#ifndef FALSE
# define TRUE 1
# define FALSE 0
#endif
/* Return codes */
#define ED_OK FALSE
#define ERR -2
#undef FATAL /* (ERR-1) */
#define CHANGED (ERR-2)
#define SET_FAIL (ERR-3)
#define SUB_FAIL (ERR-4)
#define MEM_FAIL (ERR-5)
/* Sizes and limits */
#define BUFFER_SIZE 2048 /* stream-buffer size, a multible of a disk block */
#define MAXLINE 2048 /* max number of chars per line */
#define MAXPAT 256 /* max number of chars per replacement pattern */
#define MAXFNAME MAXPATHLEN /* max file name size */
/*-------------------------------------------------------------------------*/
/* The whole text is stored linewise in a double-linked ring(!) of 'line'
* structures. The text of every line is appended to the end of its
* structure.
*/
struct line
{
int l_stat; /* Status of the line */
struct line *l_prev; /* previous line */
struct line *l_next; /* next line */
char l_buff[1]; /* the line's text */
};
typedef struct line LINE;
/* Bitflags of line.l_stat */
#define LINFREE 0x01 /* entry not in use */
#define LGLOB 0x02 /* line marked global, e.g. by a match-pattern */
/*-------------------------------------------------------------------------*/
/* The ed_buffer holds all the information for one editor session.
*/
typedef struct ed_buffer_s ed_buffer_t;
struct ed_buffer_s
{
input_t input; /* It's an input handler. */
Bool diag; /* True: diagnostic-output?*/
Bool truncflg; /* True: truncate long line flag
* Note: not used anywhere */
int nonascii; /* count of non-ascii chars read */
int nullchar; /* count of null chars read */
int truncated; /* count of lines truncated */
string_t *fname; /* name of the file */
Bool fchanged; /* True: file-changed */
int nofname;
int mark['z'-'a'+1];
regexp_t *oldpat;
LINE Line0; /* anchor of the line buffer */
int CurLn; /* number of current line */
LINE *CurPtr; /* CurLn and CurPtr must be kept in sync */
int LastLn; /* number of last line */
int Line1; /* command linerange: first line */
int Line2; /* command linerange: last line */
int nlines;
int flags; /* flags */
Bool appending;
int moring; /* used for the wait line of help */
int shiftwidth; /* shiftwidth, in the range 0..15 */
int leading_blanks; /* Current number of leading blanks when
using autoindentation. */
int cur_autoindent;
int lastcmd; /* The last command */
string_t *exit_fn; /* Function to be called when player exits */
/* TODO: Make this a callback */
object_t *exit_ob; /* Object holding <exit_fn> */
};
/* ed_buffer.flag values
* The lower 4 bits are reserved to hold the shiftwidth value then
* the settings are stored/retrieved.
*/
#define SHIFTWIDTH_MASK 0x000f
#define NFLG_MASK 0x0010 /* True: number lines */
#define LFLG_MASK 0x0020 /* True: mark tabs and line ends */
#define PFLG_MASK 0x0040
#define EIGHTBIT_MASK 0x0080 /* True: keep 8th bit */
#define AUTOINDFLG_MASK 0x0100 /* True: autoindent */
#define EXCOMPAT_MASK 0x0200
#define TABINDENT_MASK 0x0400
#define SMALLNUMBER_MASK 0x0800 /* True: short line numbers */
#define ALL_FLAGS_MASK 0x0ff0
/* Macros handling the current ed_buffer for less typing */
#define ED_BUFFER (current_ed_buffer)
#define EXTERN_ED_BUFFER(ih) ((ed_buffer_t*) (ih))
#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_NFLG ( P_FLAGS & NFLG_MASK )
#define P_LFLG ( P_FLAGS & LFLG_MASK )
#define P_PFLG ( P_FLAGS & PFLG_MASK )
#define P_EIGHTBIT ( P_FLAGS & EIGHTBIT_MASK )
#define P_AUTOINDFLG ( P_FLAGS & AUTOINDFLG_MASK )
#define P_EXCOMPAT ( P_FLAGS & EXCOMPAT_MASK )
#define P_TABINDENT ( P_FLAGS & TABINDENT_MASK )
#define P_SMALLNUMBER ( P_FLAGS & SMALLNUMBER_MASK )
#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_PROMPT (ED_BUFFER->prompt)
#define P_LASTCMD (ED_BUFFER->lastcmd)
/*-------------------------------------------------------------------------*/
/* The editor options are described by this structure: */
struct tbl
{
char *t_str; /* option name, NULL marks the table end */
int t_and_mask; /* unset mask */
int t_or_mask; /* set mask */
};
static struct tbl 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 }
, { "smallnumber", ~FALSE, SMALLNUMBER_MASK }
, { "nosmallnumber", ~SMALLNUMBER_MASK, FALSE }
, { NULL }
};
/*-------------------------------------------------------------------------*/
static ed_buffer_t *current_ed_buffer;
/* The current ed_buffer
*/
static char inlin[MAXLINE];
static char *inptr;
/* Command input buffer
*/
static int ed_tmp;
/* Temporary used by some macros
*/
/*-------------------------------------------------------------------------*/
/* Some 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( (lin) )), P_CURLN = (lin) )
#define setCurLn( lin ) ( (P_CURPTR = getptr( ed_tmp = (lin) )), P_CURLN = ed_tmp )
#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); }
/*-------------------------------------------------------------------------*/
/* Forward declarations */
static int doprnt(int, int);
static int ins(char *);
static int deflt(int, int);
static void print_help(char arg);
static void print_help2(void);
static void count_blanks(int line);
static void _count_blanks(char *str, int blanks);
static LINE *getptr(int num);
static void putcntl(char c);
static void prntln(char *str, Bool vflg, int lin);
static regexp_t *optpat(void);
/*-------------------------------------------------------------------------*/
size_t
ed_buffer_size (input_t *ih)
/* Return the size of the memory allocated for the <buffer>
*/
{
ed_buffer_t *buffer = (ed_buffer_t*) ih;
size_t sum;
long line;
LINE *pLine;
if (!buffer)
return 0;
sum = sizeof(*buffer);
for (line = 1, pLine = buffer->Line0.l_next
; line < buffer->LastLn
; line++, pLine = pLine->l_next)
sum += sizeof(*pLine) + strlen(pLine->l_buff);
return sum;
} /* ed_buffer_size() */
/*-------------------------------------------------------------------------*/
static INLINE void
set_ed_prompt (ed_buffer_t * ed_buffer, string_t * prompt)
/* Reference and set string <prompt> as new prompt in <ed_buffer>.
* The prompt svalue must already have been initialized as T_STRING.
*/
{
free_mstring(ed_buffer->input.prompt.u.str);
ed_buffer->input.prompt.u.str = ref_mstring(prompt);
} /* set_ed_prompt() */
/*-------------------------------------------------------------------------*/
static int
append (int line, Bool glob)
/* Start appending (or inserting) after <line>: set the current line
* and print/set the prompt.
* Return success.
*/
{
if (glob)
return(ERR);
_setCurLn( line );
P_APPENDING = TRUE;
if (P_NFLG)
add_message(P_SMALLNUMBER ? "%3d " : "%6d. ",P_CURLN+1);
if (P_CUR_AUTOIND)
add_message("%*s", P_LEADBLANKS, "");
set_ed_prompt(ED_BUFFER, STR_ED_APPEND_PROMPT);
return ED_OK;
}
/*-------------------------------------------------------------------------*/
static INLINE int /* only used once */
more_append (char *str)
/* User entered a new line <str> in appending mode.
* Terminate mode if it is just '.', else append it to the text.
* In autoindentation mode, recompute the indentation. Outdentation can
* be forced by entering Ctrl-D or CR as the first characters of <str>.
* Return success code.
*/
{
if(str[0] == '.' && str[1] == '\0')
{
P_APPENDING = FALSE;
set_ed_prompt(ED_BUFFER, STR_ED_PROMPT);
return ED_OK;
}
if (P_NFLG)
add_message(P_SMALLNUMBER ? "%3d " : "%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++]=' ';
xstrncpy(inlin+P_LEADBLANKS, str, (size_t)(MAXLINE-P_LEADBLANKS));
inlin[MAXLINE-1] = '\0';
_count_blanks(inlin, 0);
add_message("%*s", P_LEADBLANKS, "");
if (!*str && less_indent_flag)
return ED_OK;
str = inlin;
}
if (ins(str) < 0)
return MEM_FAIL;
return ED_OK;
}
/*-------------------------------------------------------------------------*/
static void
count_blanks (int line)
/* Count the leading blanks in line <line> and set .leadingblanks to
* the value.
*/
{
_count_blanks(gettxtl(getptr(line)), 0);
}
/*-------------------------------------------------------------------------*/
static void
_count_blanks (char * str, int blanks)
/* Count the leading blanks of <str>, add them to <blanks> and set the
* result as .leadingblanks.
*/
{
for ( ; *str; str++ )
{
if ( *str == ' ' ) blanks++;
else if ( *str == '\t' ) blanks += 8 - blanks % 8 ;
else break;
}
P_LEADBLANKS = blanks < MAXLINE ? blanks : MAXLINE ;
}
/*-------------------------------------------------------------------------*/
static INLINE int /* only used once */
ckglob (void)
/* Check if the command starting at <inptr> has a global modifier of the
* forms 'g/<expr>/' or 'v/<expr>/'.
* If yes, mark all matching lines with LGLOB and return TRUE,
* else return FALSE.
* Return an error code on failure.
*/
{
regexp_t *glbpat;
char c, delim, *lin;
int num;
LINE *ptr;
c = *inptr;
if (c != 'g' && c != 'v')
return FALSE;
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)
{
int rc;
lin = gettxtl(ptr);
rc = rx_exec_str(glbpat, lin, lin);
if (rc < 0)
{
add_message("ed: %s\n", rx_error_message(rc, glbpat) );
return ERR;
}
if (rc > 0)
{
if (c=='g') ptr->l_stat |= LGLOB;
}
else
{
if (c=='v') ptr->l_stat |= LGLOB;
}
}
}
ptr = getnextptr(ptr);
}
return TRUE;
}
/*-------------------------------------------------------------------------*/
static int
deflt (int def1, int def2)
/* Set P_LINE1 & P_LINE2 (the command-range delimiters) if none of them
* was supplied with the command; test whether they have valid values.
* Return success code.
*/
{
if(P_NLINES == 0) {
P_LINE1 = def1;
P_LINE2 = def2;
}
return ( (P_LINE1 > P_LINE2 || P_LINE1 <= 0) ? ERR : ED_OK );
}
/*-------------------------------------------------------------------------*/
static int
del (int from, int to)
/* Delete the lines in the range <from> to <to> inclusive. The range is
* automatically limited by the text start/end.
* Always returns ED_OK.
*/
{
LINE *first, *last, *next, *tmp;
if (from < 1)
from = 1;
first = getprevptr(getptr(from));
P_CURLN = prevln(from);
P_CURPTR = first;
last = getnextptr(getptr(to));
next = first->l_next;
while(next != last && next != &P_LINE0)
{
tmp = next->l_next;
xfree((char *)next);
next = tmp;
}
relink(first, last, first, last);
P_LASTLN -= (to - from)+1;
return ED_OK;
}
/*-------------------------------------------------------------------------*/
static INLINE int /* only used once */
dolst (int line1, int line2)
/* Print the range <line1> to <line2> with tab characters printed
* symbolically and line end marked with '$'.
* Returns success (currently always ED_OK).
*/
{
int oldflags = P_FLAGS;
int p;
P_FLAGS |= LFLG_MASK;
p = doprnt(line1, line2);
P_FLAGS = oldflags;
return p;
}
/*-------------------------------------------------------------------------*/
#if 0 /* unused */
int
esc (char **s)
/* 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.
*/
{
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;
}
#endif
/*-------------------------------------------------------------------------*/
static int
doprnt (int from, int to)
/* Print the text in the range <from> to <to>, using the current
* settings of P_LFLG and P_NFLG.
* Return ED_OK.
*/
{
from = (from < 1) ? 1 : from;
to = (to > P_LASTLN) ? P_LASTLN : to;
if (to != 0 && from <= P_LASTLN)
{
_setCurLn( from );
while( P_CURLN <= to )
{
prntln( gettxtl( P_CURPTR ), P_LFLG, (P_NFLG ? P_CURLN : 0));
if( P_CURLN == to )
break;
nextCurLn();
}
}
return ED_OK;
}
/*-------------------------------------------------------------------------*/
static void
prntln (char *str, Bool vflg, int lin)
/* Print the line <str>, optionally prepended by the number <lin> (if not 0).
* If <vflg> is true, tab characters are printed symbolically and line end
* marked with '$'.
*/
{
if (lin)
add_message(P_SMALLNUMBER ? "%3d " : "%7d " ,lin);
while(*str && *str != NL)
{
if (((unsigned char)*str) < ' ')
{
switch(*str)
{
case '\t':
if (vflg)
putcntl(*str);
else
add_message("%c", *str);
break;
case DEL:
add_message("^?");
break;
default:
putcntl(*str);
break;
}
str++;
}
else
{
char *start;
start = str;
do str++; while(((unsigned char)*str) >= ' ');
if (*str)
add_message("%.*s", (int)(str - start), start);
else
add_message("%s", start);
}
}
if (vflg)
add_message("$");
add_message("\n");
}
/*-------------------------------------------------------------------------*/
static void
putcntl (char c)
/* Print the control character <c> symbolically (e.g. '^C' for a Control-C).
*/
{
add_message("^%c",(c&31)|'@');
}
/*-------------------------------------------------------------------------*/
static INLINE int /* only used once */
egets (char *str, int size, FILE * stream)
/* Safe version of fgets(): get a line of text from <stream> into <str>,
* but at max <size> characters, and return the number characters read.
*/
{
int c, count;
char *cp;
/* assert(size); if there is any chance of size == 0 */
count = 0;
cp = str;
do {
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++ = (char)c; /* not null, keep it */
count++;
}
} while (size > 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() */
/*-------------------------------------------------------------------------*/
static int
doread (int lin, string_t *fname)
/* Read the file <fname> and insert the lines at line <lin>
* Return success code.
*/
{
FILE *fp;
int err;
unsigned long bytes;
unsigned int lines;
char str[MAXLINE];
char *native;
err = 0;
P_NONASCII = P_NULLCHAR = P_TRUNCATED = 0;
if (P_DIAG) add_message("\"%s\" ",get_txt(fname));
native = convert_path_to_native(get_txt(fname), mstrsize(fname));
if (!native)
{
add_message("Could not encode path '%s'.\n", get_txt(fname));
return ERR ;
}
if ((fp = fopen(native, "r")) == NULL )
{
if (!P_DIAG) add_message("\"%s\" ",get_txt(fname));
add_message(" isn't readable.\n");
return ERR ;
}
FCOUNT_READ(native);
_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 %lu 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 */
/*-------------------------------------------------------------------------*/
static int
dowrite (int from, int to, string_t *fname, Bool apflg)
/* Write the lines <from> to <to> into the file <fname>. If the <apflg>
* is true, the file is opened for appending, else it is written
* from scratch.
* Return success code.
*/
{
FILE *fp;
int lin, err;
unsigned int lines;
unsigned long bytes;
char *str;
LINE *lptr;
char *native;
err = 0;
lines = bytes = 0;
add_message("\"%s\" ",get_txt(fname));
native = convert_path_to_native(get_txt(fname), mstrsize(fname));
if (!native)
{
add_message("Could not encode path '%s'.\n", get_txt(fname));
return ERR ;
}
if ((fp = fopen(native,(apflg?"a":"w"))) == NULL)
{
add_message(" can't be opened for writing!\n");
return ERR;
}
FCOUNT_WRITE(native);
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 */
/*-------------------------------------------------------------------------*/
static INLINE int /* only used once */
find (regexp_t *pat, Bool dir)
/* Find the <pat>tern in the text, starting 'after' the current line.
* If <dir> is false, the search is carried out forward, else backwards.
* Return the number of the line where the pattern is found first, else
* the result code ERR.
*/
{
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++ )
{
int rc;
char *line_start = gettxtl(lin);
rc = rx_exec_str(pat, line_start, line_start);
if (rc < 0)
{
add_message("ed: %s\n", rx_error_message(rc, pat) );
return ERR;
}
if (rc)
return(num);
if( dir )
num = nextln(num), lin = getnextptr(lin);
else
num = prevln(num), lin = getprevptr(lin);
}
return ERR;