]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/ed/main.c
1 /* main.c: This file contains the main control and user-interface routines
2 for the ed line editor. */
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright
[] =
32 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
33 All rights reserved.\n";
40 * This program is based on the editor algorithm described in
41 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
42 * in Pascal," Addison-Wesley, 1981.
44 * The buffering algorithm is attributed to Rodney Ruddock of
45 * the University of Guelph, Guelph, Ontario.
47 * The cbc.c encryption code is adapted from
48 * the bdes program by Matt Bishop of Dartmouth College,
53 #include <sys/types.h>
55 #include <sys/ioctl.h>
63 #include <get_compat.h>
65 #define COMPAT_MODE(a,b) (1)
66 #endif /* __APPLE__ */
78 char stdinbuf
[1]; /* stdin buffer */
79 char *shcmd
; /* shell command buffer */
80 int shcmdsz
; /* shell command buffer size */
81 int shcmdi
; /* shell command buffer index */
82 char *ibuf
; /* ed command-line buffer */
83 int ibufsz
; /* ed command-line buffer size */
84 char *ibufp
; /* pointer to ed command-line buffer */
87 int des
= 0; /* if set, use crypt(3) for i/o */
88 int garrulous
= 0; /* if set, print all error messages */
89 int isbinary
; /* if set, buffer contains ASCII NULs */
90 int isglobal
; /* if set, doing a global command */
91 int modified
; /* if set, buffer modified since last write */
92 int mutex
= 0; /* if set, signals set "sigflags" */
93 int red
= 0; /* if set, restrict shell/directory access */
94 int scripted
= 0; /* if set, suppress diagnostics */
95 int sigflags
= 0; /* if set, signals received while mutex set */
96 int sigactive
= 0; /* if set, signal handlers are enabled */
97 int posixly_correct
= 0; /* if set, POSIX behavior as per */
98 /* http://www.opengroup.org/onlinepubs/009695399/utilities/ed.html */
100 char old_filename
[PATH_MAX
+ 1] = ""; /* default filename */
101 long current_addr
; /* current address in editor buffer */
102 long addr_last
; /* last address in editor buffer */
103 int lineno
; /* script line number */
104 const char *prompt
; /* command-line prompt */
105 const char *dps
= "*"; /* default command-line prompt */
107 const char usage
[] = "usage: %s [-] [-sx] [-p string] [file]\n";
109 /* ed: line editor */
111 main(int argc
, char *argv
[])
116 /* Avoid longjmp clobbering */
121 (void)setlocale(LC_ALL
, "");
123 posixly_correct
= COMPAT_MODE("bin/ed", "Unix2003");
125 red
= argv
[0]!=NULL
&& (n
= strlen(argv
[0])) > 2 && argv
[0][n
- 3] == 'r';
127 while ((c
= getopt(argc
, argv
, "p:sx")) != -1)
129 case 'p': /* set prompt */
132 case 's': /* run script */
135 case 'x': /* use crypt */
139 fprintf(stderr
, "crypt unavailable\n?\n");
144 fprintf(stderr
, usage
, red
? "red" : "ed");
149 if (argc
>0 && *argv
&& !strcmp(*argv
, "-")) {
158 /* assert: reliable signals! */
160 handle_winch(SIGWINCH
);
161 if (isatty(0)) signal(SIGWINCH
, handle_winch
);
163 signal(SIGHUP
, signal_hup
);
164 signal(SIGQUIT
, SIG_IGN
);
165 signal(SIGINT
, signal_int
);
167 if ((status
= sigsetjmp(env
, 1)))
169 if ((status
= setjmp(env
)))
172 fputs("\n?\n", stderr
);
173 errmsg
= "interrupt";
176 sigactive
= 1; /* enable signal handlers */
177 if (argc
>0 && *argv
&& is_legal_filename(*argv
)) {
178 if (read_file(*argv
, 0) < 0 && !isatty(0))
180 else if (**argv
!= '!') {
181 if (strlen(*argv
) < sizeof(old_filename
)) {
182 strcpy(old_filename
, *argv
);
184 fprintf(stderr
, "%s: filename too long\n", *argv
);
189 fputs("?\n", stdout
);
191 errmsg
= "invalid filename";
197 if (status
< 0 && garrulous
)
198 fprintf(stderr
, "%s\n", errmsg
);
200 printf("%s", prompt
);
203 if ((n
= get_tty_line()) < 0) {
207 if (modified
&& !scripted
) {
208 fputs("?\n", stderr
);
209 errmsg
= "warning: file modified";
211 fprintf(stderr
, garrulous
?
212 "script, line %d: %s\n" :
222 } else if (ibuf
[n
- 1] != '\n') {
224 errmsg
= "unexpected end-of-file";
230 if ((status
= extract_addr_range()) >= 0 &&
231 (status
= exec_command()) >= 0)
233 (status
= display_lines(current_addr
, current_addr
,
241 fputs("?\n", stderr
); /* give warning */
242 errmsg
= "warning: file modified";
244 fprintf(stderr
, garrulous
?
245 "script, line %d: %s\n" :
252 fprintf(stderr
, garrulous
?
253 "script, line %d: %s\n" : "",
256 fprintf(stderr
, garrulous
? "%s\n" : "",
260 fputs("?\n", stdout
);
262 fprintf(stderr
, garrulous
?
263 "script, line %d: %s\n" : "",
273 long first_addr
, second_addr
, addr_cnt
;
275 /* extract_addr_range: get line addresses from the command buffer until an
276 illegal address is seen; return status */
278 extract_addr_range(void)
283 first_addr
= second_addr
= current_addr
;
284 while ((addr
= next_addr()) >= 0) {
286 first_addr
= second_addr
;
288 if (*ibufp
!= ',' && *ibufp
!= ';')
290 else if (*ibufp
++ == ';')
293 if ((addr_cnt
= min(addr_cnt
, 2)) == 1 || second_addr
!= addr
)
294 first_addr
= second_addr
;
295 return (addr
== ERR
) ? ERR
: 0;
299 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
301 #define MUST_BE_FIRST() do { \
303 errmsg = "invalid address"; \
308 /* next_addr: return the next line address in the command buffer */
313 long addr
= current_addr
;
320 for (hd
= ibufp
;; first
= 0)
321 switch (c
= *ibufp
) {
329 if (isdigit((unsigned char)*ibufp
)) {
331 addr
+= (c
== '-' || c
== '^') ? -n
: n
;
332 } else if (!isspace((unsigned char)c
))
333 addr
+= (c
== '-' || c
== '^') ? -1 : 1;
335 case '0': case '1': case '2':
336 case '3': case '4': case '5':
337 case '6': case '7': case '8': case '9':
339 if (first
|| !add
) /* don't add for ,\d or ;\d */
348 addr
= (c
== '.') ? current_addr
: addr_last
;
354 if ((addr
= get_matching_node_addr(
355 get_compiled_pattern(), c
== '/')) < 0)
357 else if (c
== *ibufp
)
364 if ((addr
= get_marked_node_addr(*ibufp
++)) < 0)
374 second_addr
= (c
== ';') ? current_addr
: 1;
382 else if (addr
< 0 || addr_last
< addr
) {
383 errmsg
= "invalid address";
392 /* GET_THIRD_ADDR: get a legal address from the command buffer */
393 #define GET_THIRD_ADDR(addr) \
397 ol1 = first_addr, ol2 = second_addr; \
398 if (extract_addr_range() < 0) \
400 else if (!posixly_correct && addr_cnt == 0) { \
401 errmsg = "destination expected"; \
403 } else if (second_addr < 0 || addr_last < second_addr) { \
404 errmsg = "invalid address"; \
407 addr = second_addr; \
408 first_addr = ol1, second_addr = ol2; \
412 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
413 #define GET_COMMAND_SUFFIX() { \
418 gflag |= GPR, ibufp++; \
421 gflag |= GLS, ibufp++; \
424 gflag |= GNP, ibufp++; \
430 if (*ibufp++ != '\n') { \
431 errmsg = "invalid command suffix"; \
438 #define SGG 001 /* complement previous global substitute suffix */
439 #define SGP 002 /* complement previous print suffix */
440 #define SGR 004 /* use last regex instead of last pat */
441 #define SGF 010 /* repeat last substitution */
443 int patlock
= 0; /* if set, pattern not freed by get_compiled_pattern() */
445 long rows
= 22; /* scroll length: ws_row - 2 */
447 /* exec_command: execute the next command in command buffer; return print
452 static pattern_t
*pat
= NULL
;
453 static int sgflag
= 0;
454 static long sgnum
= 0;
465 switch(c
= *ibufp
++) {
467 GET_COMMAND_SUFFIX();
468 if (!isglobal
) clear_undo_stack();
469 if (append_lines(second_addr
) < 0)
475 if (check_addr_range(current_addr
, current_addr
) < 0)
477 GET_COMMAND_SUFFIX();
478 if (!isglobal
) clear_undo_stack();
479 if (delete_lines(first_addr
, second_addr
) < 0 ||
480 append_lines(current_addr
) < 0)
482 if (current_addr
== 0 && addr_last
!= 0)
486 if (check_addr_range(current_addr
, current_addr
) < 0)
488 GET_COMMAND_SUFFIX();
489 if (!isglobal
) clear_undo_stack();
490 if (delete_lines(first_addr
, second_addr
) < 0)
492 else if ((addr
= INC_MOD(current_addr
, addr_last
)) != 0)
494 if (current_addr
== 0 && addr_last
!= 0)
498 if (modified
&& !scripted
)
503 errmsg
= "unexpected address";
505 } else if (!isspace((unsigned char)*ibufp
)) {
506 errmsg
= "unexpected command suffix";
508 } else if ((fnp
= get_filename()) == NULL
)
510 GET_COMMAND_SUFFIX();
511 if (delete_lines(1, addr_last
) < 0)
514 if (close_sbuf() < 0)
516 else if (open_sbuf() < 0)
518 if (*fnp
&& *fnp
!= '!') {
519 if (strlen(fnp
) < sizeof(old_filename
)) {
520 strcpy(old_filename
, fnp
);
522 fprintf(stderr
, "%s: filename too long\n", fnp
);
527 if (!posixly_correct
&& *fnp
== '\0' && *old_filename
== '\0') {
528 errmsg
= "no current filename";
531 if (read_file(*fnp
? fnp
: old_filename
, 0) < 0)
535 u_current_addr
= u_addr_last
= -1;
539 errmsg
= "unexpected address";
541 } else if (!isspace((unsigned char)*ibufp
)) {
542 errmsg
= "unexpected command suffix";
544 } else if ((fnp
= get_filename()) == NULL
)
546 else if (*fnp
== '!') {
547 errmsg
= "invalid redirection";
550 GET_COMMAND_SUFFIX();
552 if (strlen(fnp
) < sizeof(old_filename
)) {
553 strcpy(old_filename
, fnp
);
555 fprintf(stderr
, "%s: filename too long\n", fnp
);
559 printf("%s\n", strip_escapes(old_filename
));
566 errmsg
= "cannot nest global commands";
568 } else if (check_addr_range(1, addr_last
) < 0)
570 else if (build_active_list(c
== 'g' || c
== 'G') < 0)
572 else if ((n
= (c
== 'G' || c
== 'V')))
573 GET_COMMAND_SUFFIX();
575 n
= exec_global(n
, gflag
);
583 errmsg
= "unexpected address";
586 GET_COMMAND_SUFFIX();
587 if (*errmsg
) fprintf(stderr
, "%s\n", errmsg
);
591 errmsg
= "unexpected address";
594 GET_COMMAND_SUFFIX();
595 if ((garrulous
= 1 - garrulous
) && *errmsg
)
596 fprintf(stderr
, "%s\n", errmsg
);
599 if (second_addr
== 0) {
602 GET_COMMAND_SUFFIX();
603 if (!isglobal
) clear_undo_stack();
604 if (append_lines(second_addr
- 1) < 0)
606 if (u_addr_last
== addr_last
) /* nothing inserted */
607 current_addr
= second_addr
;
610 if (check_addr_range(current_addr
, current_addr
+ 1) < 0)
612 GET_COMMAND_SUFFIX();
613 if (!isglobal
) clear_undo_stack();
614 if (first_addr
!= second_addr
&&
615 join_lines(first_addr
, second_addr
) < 0)
620 if (second_addr
== 0) {
621 errmsg
= "invalid address";
624 GET_COMMAND_SUFFIX();
625 if (mark_line_node(get_addressed_line_node(second_addr
), c
) < 0)
629 if (check_addr_range(current_addr
, current_addr
) < 0)
631 GET_COMMAND_SUFFIX();
632 if (display_lines(first_addr
, second_addr
, gflag
| GLS
) < 0)
637 if (check_addr_range(current_addr
, current_addr
) < 0)
639 GET_THIRD_ADDR(addr
);
640 if (first_addr
<= addr
&& addr
< second_addr
) {
641 errmsg
= "invalid destination";
644 GET_COMMAND_SUFFIX();
645 if (!isglobal
) clear_undo_stack();
646 if (move_lines(addr
) < 0)
650 if (check_addr_range(current_addr
, current_addr
) < 0)
652 GET_COMMAND_SUFFIX();
653 if (display_lines(first_addr
, second_addr
, gflag
| GNP
) < 0)
658 if (check_addr_range(current_addr
, current_addr
) < 0)
660 GET_COMMAND_SUFFIX();
661 if (display_lines(first_addr
, second_addr
, gflag
| GPR
) < 0)
667 errmsg
= "unexpected address";
670 GET_COMMAND_SUFFIX();
671 prompt
= prompt
? NULL
: optarg
? optarg
: dps
;
676 errmsg
= "unexpected address";
679 GET_COMMAND_SUFFIX();
680 gflag
= (modified
&& !scripted
&& c
== 'q') ? EMOD
: EOF
;
683 if (!isspace((unsigned char)*ibufp
)) {
684 errmsg
= "unexpected command suffix";
686 } else if (addr_cnt
== 0)
687 second_addr
= addr_last
;
688 if ((fnp
= get_filename()) == NULL
)
690 GET_COMMAND_SUFFIX();
691 if (!isglobal
) clear_undo_stack();
692 if (*old_filename
== '\0' && *fnp
!= '!') {
693 if (strlen(fnp
) < sizeof(old_filename
)) {
694 strcpy(old_filename
, fnp
);
696 fprintf(stderr
, "%s: filename too long\n", fnp
);
700 if (!posixly_correct
&& *fnp
== '\0' && *old_filename
== '\0') {
701 errmsg
= "no current filename";
704 if ((addr
= read_file(*fnp
? fnp
: old_filename
, second_addr
)) < 0)
706 else if (addr
&& addr
!= addr_last
)
711 * POSIX requires ed to process srabcr123r as
712 * replace abc with 123 delimiter='r'
714 if (!posixly_correct
) do {
731 case '0': case '1': case '2': case '3': case '4':
732 case '5': case '6': case '7': case '8': case '9':
733 STRTOL(sgnum
, ibufp
);
735 sgflag
&= ~GSG
; /* override GSG */
739 errmsg
= "invalid command suffix";
743 } while (sflags
&& *ibufp
!= '\n');
744 if (sflags
&& !pat
) {
745 errmsg
= "no previous substitution";
747 } else if (sflags
& SGG
)
748 sgnum
= 0; /* override numeric arg */
749 if (*ibufp
!= '\n' && *(ibufp
+ 1) == '\n') {
750 errmsg
= "invalid pattern delimiter";
755 if ((!sflags
|| (sflags
& SGR
)) &&
756 (tpat
= get_compiled_pattern()) == NULL
) {
759 } else if (tpat
!= pat
) {
765 patlock
= 1; /* reserve pattern */
768 if (!sflags
&& extract_subst_tail(&sgflag
, &sgnum
) < 0)
777 sgflag
^= GPR
, sgflag
&= ~(GLS
| GNP
);
781 sgflag
|= GPR
, ibufp
++;
784 sgflag
|= GLS
, ibufp
++;
787 sgflag
|= GNP
, ibufp
++;
793 if (check_addr_range(current_addr
, current_addr
) < 0)
795 GET_COMMAND_SUFFIX();
796 if (!isglobal
) clear_undo_stack();
797 if (search_and_replace(pat
, sgflag
, sgnum
) < 0)
801 if (check_addr_range(current_addr
, current_addr
) < 0)
803 GET_THIRD_ADDR(addr
);
804 GET_COMMAND_SUFFIX();
805 if (!isglobal
) clear_undo_stack();
806 if (copy_lines(addr
) < 0)
811 errmsg
= "unexpected address";
814 GET_COMMAND_SUFFIX();
815 if (pop_undo_stack() < 0)
820 if ((n
= *ibufp
) == 'q' || n
== 'Q') {
824 if (!isspace((unsigned char)*ibufp
)) {
825 errmsg
= "unexpected command suffix";
827 } else if ((fnp
= get_filename()) == NULL
)
829 if (addr_cnt
== 0 && !addr_last
)
830 first_addr
= second_addr
= 0;
831 else if (check_addr_range(1, addr_last
) < 0)
833 GET_COMMAND_SUFFIX();
834 if (*old_filename
== '\0' && *fnp
!= '!') {
835 if (strlen(fnp
) < sizeof(old_filename
)) {
836 strcpy(old_filename
, fnp
);
838 fprintf(stderr
, "%s: filename too long\n", fnp
);
842 if (!posixly_correct
&& *fnp
== '\0' && *old_filename
== '\0') {
843 errmsg
= "no current filename";
846 if ((addr
= write_file(*fnp
? fnp
: old_filename
,
847 (c
== 'W') ? "a" : "w", first_addr
, second_addr
)) < 0)
849 else if (addr
== addr_last
)
851 else if (modified
&& !scripted
&& n
== 'q')
856 errmsg
= "unexpected address";
859 GET_COMMAND_SUFFIX();
864 errmsg
= "crypt unavailable";
868 if (check_addr_range(first_addr
= 1, current_addr
+ (posixly_correct
? !isglobal
: 1)) < 0)
870 else if ('0' < *ibufp
&& *ibufp
<= '9')
872 GET_COMMAND_SUFFIX();
873 if (display_lines(second_addr
, min(addr_last
,
874 second_addr
+ rows
), gflag
) < 0)
879 GET_COMMAND_SUFFIX();
880 printf("%ld\n", addr_cnt
? second_addr
: addr_last
);
884 errmsg
= "unexpected address";
886 } else if ((sflags
= get_shell_command()) < 0)
888 GET_COMMAND_SUFFIX();
889 if (sflags
) printf("%s\n", shcmd
+ 1);
892 if (!scripted
) printf("!\n");
895 if (check_addr_range(first_addr
= 1, current_addr
+ (posixly_correct
? !isglobal
: 1)) < 0
896 || display_lines(second_addr
, second_addr
, 0) < 0)
900 errmsg
= "unknown command";
907 /* check_addr_range: return status of address range check */
909 check_addr_range(long n
, long m
)
915 if (first_addr
> second_addr
|| 1 > first_addr
||
916 second_addr
> addr_last
) {
917 errmsg
= "invalid address";
924 /* get_matching_node_addr: return the address of the next line matching a
925 pattern in a given direction. wrap around begin/end of editor buffer if
928 get_matching_node_addr(pattern_t
*pat
, int dir
)
931 long n
= current_addr
;
934 if (!pat
) return ERR
;
936 if ((n
= dir
? INC_MOD(n
, addr_last
) : DEC_MOD(n
, addr_last
))) {
937 lp
= get_addressed_line_node(n
);
938 if ((s
= get_sbuf_line(lp
)) == NULL
)
941 NUL_TO_NEWLINE(s
, lp
->len
);
942 if (!regexec(pat
, s
, 0, NULL
, 0))
945 } while (n
!= current_addr
);
951 /* get_filename: return pointer to copy of filename in the command buffer */
955 static char *file
= NULL
;
956 static int filesz
= 0;
960 if (*ibufp
!= '\n') {
962 if (*ibufp
== '\n') {
963 errmsg
= "invalid filename";
965 } else if ((ibufp
= get_extended_line(&n
, 1)) == NULL
)
967 else if (*ibufp
== '!') {
969 if ((n
= get_shell_command()) < 0)
972 printf("%s\n", shcmd
+ 1);
974 } else if (n
> PATH_MAX
) {
975 errmsg
= "filename too long";
979 else if (posixly_correct
&& *old_filename
== '\0') {
980 errmsg
= "no current filename";
983 REALLOC(file
, filesz
, PATH_MAX
+ 1, NULL
);
984 for (n
= 0; *ibufp
!= '\n';)
985 file
[n
++] = *ibufp
++;
987 return is_legal_filename(file
) ? file
: NULL
;
991 /* get_shell_command: read a shell command from stdin; return substitution
994 get_shell_command(void)
996 static char *buf
= NULL
;
999 char *s
; /* substitution char pointer */
1004 errmsg
= "shell access restricted";
1006 } else if ((s
= ibufp
= get_extended_line(&j
, 1)) == NULL
)
1008 REALLOC(buf
, n
, j
+ 1, ERR
);
1009 buf
[i
++] = '!'; /* prefix command w/ bang */
1010 while (*ibufp
!= '\n')
1013 REALLOC(buf
, n
, i
+ 2, ERR
);
1015 if (*ibufp
++ == '\\')
1016 buf
[i
++] = *ibufp
++;
1020 REALLOC(buf
, n
, i
+ 1, ERR
);
1021 buf
[i
++] = *ibufp
++;
1023 else if (shcmd
== NULL
|| (!posixly_correct
&& *(shcmd
+ 1) == '\0'))
1025 errmsg
= "no previous command";
1028 REALLOC(buf
, n
, i
+ shcmdi
, ERR
);
1029 for (s
= shcmd
+ 1; s
< shcmd
+ shcmdi
;)
1035 if (*old_filename
== '\0') {
1036 errmsg
= "no current filename";
1039 j
= strlen(s
= strip_escapes(old_filename
));
1040 REALLOC(buf
, n
, i
+ j
, ERR
);
1046 REALLOC(shcmd
, shcmdsz
, i
+ 1, ERR
);
1047 memcpy(shcmd
, buf
, i
);
1048 shcmd
[shcmdi
= i
] = '\0';
1049 return *s
== '!' || *s
== '%';
1053 /* append_lines: insert text from stdin to after line n; stop when either a
1054 single period is read or EOF; return status */
1056 append_lines(long n
)
1059 const char *lp
= ibuf
;
1063 for (current_addr
= n
;;) {
1065 if ((l
= get_tty_line()) < 0)
1067 else if (l
== 0 || ibuf
[l
- 1] != '\n') {
1072 } else if (*(lp
= ibufp
) == '\0')
1075 while (*ibufp
++ != '\n')
1079 if (l
== 2 && lp
[0] == '.' && lp
[1] == '\n') {
1085 if ((lp
= put_sbuf_line(lp
)) == NULL
) {
1089 up
->t
= get_addressed_line_node(current_addr
);
1090 else if ((up
= push_undo_stack(UADD
, current_addr
,
1091 current_addr
)) == NULL
) {
1095 } while (lp
!= eot
);
1103 /* join_lines: replace a range of lines with the joined text of those lines */
1105 join_lines(long from
, long to
)
1107 static char *buf
= NULL
;
1114 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1115 bp
= get_addressed_line_node(from
);
1116 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1117 if ((s
= get_sbuf_line(bp
)) == NULL
)
1119 REALLOC(buf
, n
, size
+ bp
->len
, ERR
);
1120 memcpy(buf
+ size
, s
, bp
->len
);
1123 REALLOC(buf
, n
, size
+ 2, ERR
);
1124 memcpy(buf
+ size
, "\n", 2);
1125 if (delete_lines(from
, to
) < 0)
1127 current_addr
= from
- 1;
1129 if (put_sbuf_line(buf
) == NULL
||
1130 push_undo_stack(UADD
, current_addr
, current_addr
) == NULL
) {
1135 if (current_addr
== 0)
1142 /* move_lines: move a range of lines */
1144 move_lines(long addr
)
1146 line_t
*b1
, *a1
, *b2
, *a2
;
1147 long n
= INC_MOD(second_addr
, addr_last
);
1148 long p
= first_addr
- 1;
1149 int done
= (addr
== first_addr
- 1 || addr
== second_addr
);
1153 a2
= get_addressed_line_node(n
);
1154 b2
= get_addressed_line_node(p
);
1155 current_addr
= second_addr
;
1156 } else if (push_undo_stack(UMOV
, p
, n
) == NULL
||
1157 push_undo_stack(UMOV
, addr
, INC_MOD(addr
, addr_last
)) == NULL
) {
1161 a1
= get_addressed_line_node(n
);
1162 if (addr
< first_addr
) {
1163 b1
= get_addressed_line_node(p
);
1164 b2
= get_addressed_line_node(addr
);
1165 /* this get_addressed_line_node last! */
1167 b2
= get_addressed_line_node(addr
);
1168 b1
= get_addressed_line_node(p
);
1169 /* this get_addressed_line_node last! */
1172 REQUE(b2
, b1
->q_forw
);
1173 REQUE(a1
->q_back
, a2
);
1175 current_addr
= addr
+ ((addr
< first_addr
) ?
1176 second_addr
- first_addr
+ 1 : 0);
1179 unset_active_nodes(b2
->q_forw
, a2
);
1186 /* copy_lines: copy a range of lines; return status */
1188 copy_lines(long addr
)
1190 line_t
*lp
, *np
= get_addressed_line_node(first_addr
);
1192 long n
= second_addr
- first_addr
+ 1;
1195 current_addr
= addr
;
1196 if (first_addr
<= addr
&& addr
< second_addr
) {
1197 n
= addr
- first_addr
+ 1;
1198 m
= second_addr
- addr
;
1200 for (; n
> 0; n
=m
, m
=0, np
= get_addressed_line_node(current_addr
+ 1))
1201 for (; n
-- > 0; np
= np
->q_forw
) {
1203 if ((lp
= dup_line_node(np
)) == NULL
) {
1210 else if ((up
= push_undo_stack(UADD
, current_addr
,
1211 current_addr
)) == NULL
) {
1222 /* delete_lines: delete a range of lines */
1224 delete_lines(long from
, long to
)
1229 if (push_undo_stack(UDEL
, from
, to
) == NULL
) {
1233 n
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1234 p
= get_addressed_line_node(from
- 1);
1235 /* this get_addressed_line_node last! */
1237 unset_active_nodes(p
->q_forw
, n
);
1239 addr_last
-= to
- from
+ 1;
1240 current_addr
= from
- 1;
1247 /* display_lines: print a range of lines to stdout */
1249 display_lines(long from
, long to
, int gflag
)
1256 errmsg
= "invalid address";
1259 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1260 bp
= get_addressed_line_node(from
);
1261 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1262 if ((s
= get_sbuf_line(bp
)) == NULL
)
1264 if (put_tty_line(s
, bp
->len
, current_addr
= from
++, gflag
) < 0)
1271 #define MAXMARK 26 /* max number of marks */
1273 line_t
*mark
[MAXMARK
]; /* line markers */
1274 int markno
; /* line marker count */
1276 /* mark_line_node: set a line node mark */
1278 mark_line_node(line_t
*lp
, int n
)
1280 if (!islower((unsigned char)n
)) {
1281 errmsg
= "invalid mark character";
1283 } else if (mark
[n
- 'a'] == NULL
)
1290 /* get_marked_node_addr: return address of a marked line */
1292 get_marked_node_addr(int n
)
1294 if (!islower((unsigned char)n
)) {
1295 errmsg
= "invalid mark character";
1298 return get_line_node_addr(mark
[n
- 'a']);
1301 /* Used by search and replace */
1303 replace_marks(line_t
*old
, line_t
*new)
1306 for (i
=0; markno
&& i
<MAXMARK
; i
++)
1312 /* unmark_line_node: clear line node mark */
1314 unmark_line_node(line_t
*lp
)
1318 for (i
= 0; markno
&& i
< MAXMARK
; i
++)
1319 if (mark
[i
] == lp
) {
1326 /* dup_line_node: return a pointer to a copy of a line node */
1328 dup_line_node(line_t
*lp
)
1332 if ((np
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
1333 fprintf(stderr
, "%s\n", strerror(errno
));
1334 errmsg
= "out of memory";
1337 np
->seek
= lp
->seek
;
1343 /* has_trailing_escape: return the parity of escapes preceding a character
1346 has_trailing_escape(char *s
, char *t
)
1348 return (s
== t
|| *(t
- 1) != '\\') ? 0 : !has_trailing_escape(s
, t
- 1);
1352 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1354 strip_escapes(char *s
)
1356 static char *file
= NULL
;
1357 static int filesz
= 0;
1361 REALLOC(file
, filesz
, PATH_MAX
, NULL
);
1362 while (i
< filesz
- 1 /* Worry about a possible trailing escape */
1363 && (file
[i
++] = (*s
== '\\') ? *++s
: *s
))
1370 signal_hup(int signo
)
1373 sigflags
|= (1 << (signo
- 1));
1380 signal_int(int signo
)
1383 sigflags
|= (1 << (signo
- 1));
1390 handle_hup(int signo
)
1392 char *hup
= NULL
; /* hup filename */
1394 char ed_hup
[] = "ed.hup";
1399 sigflags
&= ~(1 << (signo
- 1));
1400 if (addr_last
&& write_file(ed_hup
, "w", 1, addr_last
) < 0 &&
1401 (s
= getenv("HOME")) != NULL
&&
1402 (n
= strlen(s
)) + 8 <= PATH_MAX
&& /* "ed.hup" + '/' */
1403 (hup
= (char *) malloc(n
+ 10)) != NULL
) {
1405 if (hup
[n
- 1] != '/')
1406 hup
[n
] = '/', hup
[n
+1] = '\0';
1407 strcat(hup
, "ed.hup");
1408 write_file(hup
, "w", 1, addr_last
);
1415 handle_int(int signo
)
1419 sigflags
&= ~(1 << (signo
- 1));
1420 #ifdef _POSIX_SOURCE
1421 siglongjmp(env
, -1);
1428 int cols
= 72; /* wrap column */
1431 handle_winch(int signo
)
1433 int save_errno
= errno
;
1435 struct winsize ws
; /* window size structure */
1437 sigflags
&= ~(1 << (signo
- 1));
1438 if (ioctl(0, TIOCGWINSZ
, (char *) &ws
) >= 0) {
1439 if (ws
.ws_row
> 2) rows
= ws
.ws_row
- 2;
1440 if (ws
.ws_col
> 8) cols
= ws
.ws_col
- 8;
1446 /* is_legal_filename: return a legal filename */
1448 is_legal_filename(char *s
)
1450 if (red
&& (*s
== '!' || !strcmp(s
, "..") || strchr(s
, '/'))) {
1451 errmsg
= "shell access restricted";