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
28 * @(#) Copyright (c) 1993 Andrew Moore, Talke Studio. All rights reserved.
29 * @(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp
30 * $FreeBSD: head/bin/ed/main.c 292455 2015-12-18 23:05:36Z pfg $
36 * This program is based on the editor algorithm described in
37 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
38 * in Pascal," Addison-Wesley, 1981.
40 * The buffering algorithm is attributed to Rodney Ruddock of
41 * the University of Guelph, Guelph, Ontario.
43 * The cbc.c encryption code is adapted from
44 * the bdes program by Matt Bishop of Dartmouth College,
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
61 static sigjmp_buf env
;
64 char stdinbuf
[1]; /* stdin buffer */
65 static char *shcmd
; /* shell command buffer */
66 static int shcmdsz
; /* shell command buffer size */
67 static int shcmdi
; /* shell command buffer index */
68 char *ibuf
; /* ed command-line buffer */
69 int ibufsz
; /* ed command-line buffer size */
70 char *ibufp
; /* pointer to ed command-line buffer */
73 int des
= 0; /* if set, use crypt(3) for i/o */
74 static int garrulous
= 0; /* if set, print all error messages */
75 int isbinary
; /* if set, buffer contains ASCII NULs */
76 int isglobal
; /* if set, doing a global command */
77 int modified
; /* if set, buffer modified since last write */
78 int mutex
= 0; /* if set, signals set "sigflags" */
79 static int red
= 0; /* if set, restrict shell/directory access */
80 int scripted
= 0; /* if set, suppress diagnostics */
81 int sigflags
= 0; /* if set, signals received while mutex set */
82 static int sigactive
= 0; /* if set, signal handlers are enabled */
84 static char old_filename
[PATH_MAX
] = ""; /* default filename */
85 long current_addr
; /* current address in editor buffer */
86 long addr_last
; /* last address in editor buffer */
87 int lineno
; /* script line number */
88 static const char *prompt
; /* command-line prompt */
89 static const char *dps
= "*"; /* default command-line prompt */
91 static const char *usage
= "usage: %s [-] [-sx] [-p string] [file]\n";
95 main(volatile int argc
, char ** volatile argv
)
100 setlocale(LC_ALL
, "");
102 red
= (n
= strlen(argv
[0])) > 2 && argv
[0][n
- 3] == 'r';
104 while ((c
= getopt(argc
, argv
, "p:sx")) != -1)
106 case 'p': /* set prompt */
109 case 's': /* run script */
112 case 'x': /* use crypt */
116 fprintf(stderr
, "crypt unavailable\n?\n");
121 fprintf(stderr
, usage
, red
? "red" : "ed");
126 if (argc
&& **argv
== '-') {
135 /* assert: reliable signals! */
137 handle_winch(SIGWINCH
);
138 if (isatty(0)) signal(SIGWINCH
, handle_winch
);
140 signal(SIGHUP
, signal_hup
);
141 signal(SIGQUIT
, SIG_IGN
);
142 signal(SIGINT
, signal_int
);
143 if ((status
= sigsetjmp(env
, 1))) {
144 fputs("\n?\n", stderr
);
145 errmsg
= "interrupt";
148 sigactive
= 1; /* enable signal handlers */
149 if (argc
&& **argv
&& is_legal_filename(*argv
)) {
150 if (read_file(*argv
, 0) < 0 && !isatty(0))
152 else if (**argv
!= '!')
153 if (strlcpy(old_filename
, *argv
, sizeof(old_filename
))
154 >= sizeof(old_filename
))
157 fputs("?\n", stderr
);
159 errmsg
= "invalid filename";
165 if (status
< 0 && garrulous
)
166 fprintf(stderr
, "%s\n", errmsg
);
168 printf("%s", prompt
);
171 if ((n
= get_tty_line()) < 0) {
175 if (modified
&& !scripted
) {
176 fputs("?\n", stderr
);
177 errmsg
= "warning: file modified";
181 "script, line %d: %s\n",
191 } else if (ibuf
[n
- 1] != '\n') {
193 errmsg
= "unexpected end-of-file";
199 if ((status
= extract_addr_range()) >= 0 &&
200 (status
= exec_command()) >= 0)
202 (status
= display_lines(current_addr
, current_addr
,
210 fputs("?\n", stderr
); /* give warning */
211 errmsg
= "warning: file modified";
214 fprintf(stderr
, "script, line %d: %s\n",
222 fprintf(stderr
, "script, line %d: %s\n",
224 } else if (garrulous
)
225 fprintf(stderr
, "%s\n", errmsg
);
228 fputs("?\n", stderr
);
231 fprintf(stderr
, "script, line %d: %s\n",
241 long first_addr
, second_addr
;
242 static long addr_cnt
;
244 /* extract_addr_range: get line addresses from the command buffer until an
245 illegal address is seen; return status */
247 extract_addr_range(void)
252 first_addr
= second_addr
= current_addr
;
253 while ((addr
= next_addr()) >= 0) {
255 first_addr
= second_addr
;
257 if (*ibufp
!= ',' && *ibufp
!= ';')
259 else if (*ibufp
++ == ';')
262 if ((addr_cnt
= min(addr_cnt
, 2)) == 1 || second_addr
!= addr
)
263 first_addr
= second_addr
;
264 return (addr
== ERR
) ? ERR
: 0;
268 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
270 #define MUST_BE_FIRST() do { \
272 errmsg = "invalid address"; \
277 /* next_addr: return the next line address in the command buffer */
282 long addr
= current_addr
;
288 for (hd
= ibufp
;; first
= 0)
289 switch (c
= *ibufp
) {
297 if (isdigit((unsigned char)*ibufp
)) {
299 addr
+= (c
== '-' || c
== '^') ? -n
: n
;
300 } else if (!isspace((unsigned char)c
))
301 addr
+= (c
== '-' || c
== '^') ? -1 : 1;
303 case '0': case '1': case '2':
304 case '3': case '4': case '5':
305 case '6': case '7': case '8': case '9':
313 addr
= (c
== '.') ? current_addr
: addr_last
;
318 if ((addr
= get_matching_node_addr(
319 get_compiled_pattern(), c
== '/')) < 0)
321 else if (c
== *ibufp
)
327 if ((addr
= get_marked_node_addr(*ibufp
++)) < 0)
336 second_addr
= (c
== ';') ? current_addr
: 1;
344 else if (addr
< 0 || addr_last
< addr
) {
345 errmsg
= "invalid address";
355 /* GET_THIRD_ADDR: get a legal address from the command buffer */
356 #define GET_THIRD_ADDR(addr) \
360 ol1 = first_addr, ol2 = second_addr; \
361 if (extract_addr_range() < 0) \
363 else if (addr_cnt == 0) { \
364 errmsg = "destination expected"; \
366 } else if (second_addr < 0 || addr_last < second_addr) { \
367 errmsg = "invalid address"; \
370 addr = second_addr; \
371 first_addr = ol1, second_addr = ol2; \
373 #else /* BACKWARDS */
374 /* GET_THIRD_ADDR: get a legal address from the command buffer */
375 #define GET_THIRD_ADDR(addr) \
379 ol1 = first_addr, ol2 = second_addr; \
380 if (extract_addr_range() < 0) \
382 if (second_addr < 0 || addr_last < second_addr) { \
383 errmsg = "invalid address"; \
386 addr = second_addr; \
387 first_addr = ol1, second_addr = ol2; \
392 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
393 #define GET_COMMAND_SUFFIX() { \
398 gflag |= GPR, ibufp++; \
401 gflag |= GLS, ibufp++; \
404 gflag |= GNP, ibufp++; \
410 if (*ibufp++ != '\n') { \
411 errmsg = "invalid command suffix"; \
418 #define SGG 001 /* complement previous global substitute suffix */
419 #define SGP 002 /* complement previous print suffix */
420 #define SGR 004 /* use last regex instead of last pat */
421 #define SGF 010 /* repeat last substitution */
423 int patlock
= 0; /* if set, pattern not freed by get_compiled_pattern() */
425 long rows
= 22; /* scroll length: ws_row - 2 */
427 /* exec_command: execute the next command in command buffer; return print
432 static pattern_t
*pat
= NULL
;
433 static int sgflag
= 0;
434 static long sgnum
= 0;
445 switch(c
= *ibufp
++) {
447 GET_COMMAND_SUFFIX();
448 if (!isglobal
) clear_undo_stack();
449 if (append_lines(second_addr
) < 0)
453 if (check_addr_range(current_addr
, current_addr
) < 0)
455 GET_COMMAND_SUFFIX();
456 if (!isglobal
) clear_undo_stack();
457 if (delete_lines(first_addr
, second_addr
) < 0 ||
458 append_lines(current_addr
) < 0)
462 if (check_addr_range(current_addr
, current_addr
) < 0)
464 GET_COMMAND_SUFFIX();
465 if (!isglobal
) clear_undo_stack();
466 if (delete_lines(first_addr
, second_addr
) < 0)
468 else if ((addr
= INC_MOD(current_addr
, addr_last
)) != 0)
472 if (modified
&& !scripted
)
477 errmsg
= "unexpected address";
479 } else if (!isspace((unsigned char)*ibufp
)) {
480 errmsg
= "unexpected command suffix";
482 } else if ((fnp
= get_filename()) == NULL
)
484 GET_COMMAND_SUFFIX();
485 if (delete_lines(1, addr_last
) < 0)
488 if (close_sbuf() < 0)
490 else if (open_sbuf() < 0)
492 if (*fnp
&& *fnp
!= '!')
493 strlcpy(old_filename
, fnp
, PATH_MAX
);
495 if (*fnp
== '\0' && *old_filename
== '\0') {
496 errmsg
= "no current filename";
500 if (read_file(*fnp
? fnp
: old_filename
, 0) < 0)
504 u_current_addr
= u_addr_last
= -1;
508 errmsg
= "unexpected address";
510 } else if (!isspace((unsigned char)*ibufp
)) {
511 errmsg
= "unexpected command suffix";
513 } else if ((fnp
= get_filename()) == NULL
)
515 else if (*fnp
== '!') {
516 errmsg
= "invalid redirection";
519 GET_COMMAND_SUFFIX();
521 strlcpy(old_filename
, fnp
, PATH_MAX
);
522 printf("%s\n", strip_escapes(old_filename
));
529 errmsg
= "cannot nest global commands";
531 } else if (check_addr_range(1, addr_last
) < 0)
533 else if (build_active_list(c
== 'g' || c
== 'G') < 0)
535 else if ((n
= (c
== 'G' || c
== 'V')))
536 GET_COMMAND_SUFFIX();
538 if (exec_global(n
, gflag
) < 0)
543 errmsg
= "unexpected address";
546 GET_COMMAND_SUFFIX();
547 if (*errmsg
) fprintf(stderr
, "%s\n", errmsg
);
551 errmsg
= "unexpected address";
554 GET_COMMAND_SUFFIX();
555 if ((garrulous
= 1 - garrulous
) && *errmsg
)
556 fprintf(stderr
, "%s\n", errmsg
);
559 if (second_addr
== 0) {
560 errmsg
= "invalid address";
563 GET_COMMAND_SUFFIX();
564 if (!isglobal
) clear_undo_stack();
565 if (append_lines(second_addr
- 1) < 0)
569 if (check_addr_range(current_addr
, current_addr
+ 1) < 0)
571 GET_COMMAND_SUFFIX();
572 if (!isglobal
) clear_undo_stack();
573 if (first_addr
!= second_addr
&&
574 join_lines(first_addr
, second_addr
) < 0)
579 if (second_addr
== 0) {
580 errmsg
= "invalid address";
583 GET_COMMAND_SUFFIX();
584 if (mark_line_node(get_addressed_line_node(second_addr
), c
) < 0)
588 if (check_addr_range(current_addr
, current_addr
) < 0)
590 GET_COMMAND_SUFFIX();
591 if (display_lines(first_addr
, second_addr
, gflag
| GLS
) < 0)
596 if (check_addr_range(current_addr
, current_addr
) < 0)
598 GET_THIRD_ADDR(addr
);
599 if (first_addr
<= addr
&& addr
< second_addr
) {
600 errmsg
= "invalid destination";
603 GET_COMMAND_SUFFIX();
604 if (!isglobal
) clear_undo_stack();
605 if (move_lines(addr
) < 0)
609 if (check_addr_range(current_addr
, current_addr
) < 0)
611 GET_COMMAND_SUFFIX();
612 if (display_lines(first_addr
, second_addr
, gflag
| GNP
) < 0)
617 if (check_addr_range(current_addr
, current_addr
) < 0)
619 GET_COMMAND_SUFFIX();
620 if (display_lines(first_addr
, second_addr
, gflag
| GPR
) < 0)
626 errmsg
= "unexpected address";
629 GET_COMMAND_SUFFIX();
630 prompt
= prompt
? NULL
: optarg
? optarg
: dps
;
635 errmsg
= "unexpected address";
638 GET_COMMAND_SUFFIX();
639 gflag
= (modified
&& !scripted
&& c
== 'q') ? EMOD
: EOF
;
642 if (!isspace((unsigned char)*ibufp
)) {
643 errmsg
= "unexpected command suffix";
645 } else if (addr_cnt
== 0)
646 second_addr
= addr_last
;
647 if ((fnp
= get_filename()) == NULL
)
649 GET_COMMAND_SUFFIX();
650 if (!isglobal
) clear_undo_stack();
651 if (*old_filename
== '\0' && *fnp
!= '!')
652 strlcpy(old_filename
, fnp
, PATH_MAX
);
654 if (*fnp
== '\0' && *old_filename
== '\0') {
655 errmsg
= "no current filename";
659 if ((addr
= read_file(*fnp
? fnp
: old_filename
, second_addr
)) < 0)
661 else if (addr
&& addr
!= addr_last
)
682 case '0': case '1': case '2': case '3': case '4':
683 case '5': case '6': case '7': case '8': case '9':
684 STRTOL(sgnum
, ibufp
);
686 sgflag
&= ~GSG
; /* override GSG */
690 errmsg
= "invalid command suffix";
694 } while (sflags
&& *ibufp
!= '\n');
695 if (sflags
&& !pat
) {
696 errmsg
= "no previous substitution";
698 } else if (sflags
& SGG
)
699 sgnum
= 0; /* override numeric arg */
700 if (*ibufp
!= '\n' && *(ibufp
+ 1) == '\n') {
701 errmsg
= "invalid pattern delimiter";
706 if ((!sflags
|| (sflags
& SGR
)) &&
707 (tpat
= get_compiled_pattern()) == NULL
) {
710 } else if (tpat
!= pat
) {
716 patlock
= 1; /* reserve pattern */
719 if (!sflags
&& extract_subst_tail(&sgflag
, &sgnum
) < 0)
728 sgflag
^= GPR
, sgflag
&= ~(GLS
| GNP
);
732 sgflag
|= GPR
, ibufp
++;
735 sgflag
|= GLS
, ibufp
++;
738 sgflag
|= GNP
, ibufp
++;
744 if (check_addr_range(current_addr
, current_addr
) < 0)
746 GET_COMMAND_SUFFIX();
747 if (!isglobal
) clear_undo_stack();
748 if (search_and_replace(pat
, sgflag
, sgnum
) < 0)
752 if (check_addr_range(current_addr
, current_addr
) < 0)
754 GET_THIRD_ADDR(addr
);
755 GET_COMMAND_SUFFIX();
756 if (!isglobal
) clear_undo_stack();
757 if (copy_lines(addr
) < 0)
762 errmsg
= "unexpected address";
765 GET_COMMAND_SUFFIX();
766 if (pop_undo_stack() < 0)
771 if ((n
= *ibufp
) == 'q' || n
== 'Q') {
775 if (!isspace((unsigned char)*ibufp
)) {
776 errmsg
= "unexpected command suffix";
778 } else if ((fnp
= get_filename()) == NULL
)
780 if (addr_cnt
== 0 && !addr_last
)
781 first_addr
= second_addr
= 0;
782 else if (check_addr_range(1, addr_last
) < 0)
784 GET_COMMAND_SUFFIX();
785 if (*old_filename
== '\0' && *fnp
!= '!')
786 strlcpy(old_filename
, fnp
, PATH_MAX
);
788 if (*fnp
== '\0' && *old_filename
== '\0') {
789 errmsg
= "no current filename";
793 if ((addr
= write_file(*fnp
? fnp
: old_filename
,
794 (c
== 'W') ? "a" : "w", first_addr
, second_addr
)) < 0)
796 else if (addr
== addr_last
)
798 else if (modified
&& !scripted
&& n
== 'q')
803 errmsg
= "unexpected address";
806 GET_COMMAND_SUFFIX();
811 errmsg
= "crypt unavailable";
816 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0)
818 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0)
821 else if ('0' < *ibufp
&& *ibufp
<= '9')
823 GET_COMMAND_SUFFIX();
824 if (display_lines(second_addr
, min(addr_last
,
825 second_addr
+ rows
), gflag
) < 0)
830 GET_COMMAND_SUFFIX();
831 printf("%ld\n", addr_cnt
? second_addr
: addr_last
);
835 errmsg
= "unexpected address";
837 } else if ((sflags
= get_shell_command()) < 0)
839 GET_COMMAND_SUFFIX();
840 if (sflags
) printf("%s\n", shcmd
+ 1);
842 if (!scripted
) printf("!\n");
846 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0
848 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0
850 || display_lines(second_addr
, second_addr
, 0) < 0)
854 errmsg
= "unknown command";
861 /* check_addr_range: return status of address range check */
863 check_addr_range(long n
, long m
)
869 if (first_addr
> second_addr
|| 1 > first_addr
||
870 second_addr
> addr_last
) {
871 errmsg
= "invalid address";
878 /* get_matching_node_addr: return the address of the next line matching a
879 pattern in a given direction. wrap around begin/end of editor buffer if
882 get_matching_node_addr(pattern_t
*pat
, int dir
)
885 long n
= current_addr
;
888 if (!pat
) return ERR
;
890 if ((n
= dir
? INC_MOD(n
, addr_last
) : DEC_MOD(n
, addr_last
))) {
891 lp
= get_addressed_line_node(n
);
892 if ((s
= get_sbuf_line(lp
)) == NULL
)
895 NUL_TO_NEWLINE(s
, lp
->len
);
896 if (!regexec(pat
, s
, 0, NULL
, 0))
899 } while (n
!= current_addr
);
905 /* get_filename: return pointer to copy of filename in the command buffer */
909 static char *file
= NULL
;
910 static int filesz
= 0;
914 if (*ibufp
!= '\n') {
916 if (*ibufp
== '\n') {
917 errmsg
= "invalid filename";
919 } else if ((ibufp
= get_extended_line(&n
, 1)) == NULL
)
921 else if (*ibufp
== '!') {
923 if ((n
= get_shell_command()) < 0)
926 printf("%s\n", shcmd
+ 1);
928 } else if (n
> PATH_MAX
- 1) {
929 errmsg
= "filename too long";
934 else if (*old_filename
== '\0') {
935 errmsg
= "no current filename";
939 REALLOC(file
, filesz
, PATH_MAX
, NULL
);
940 for (n
= 0; *ibufp
!= '\n';)
941 file
[n
++] = *ibufp
++;
943 return is_legal_filename(file
) ? file
: NULL
;
947 /* get_shell_command: read a shell command from stdin; return substitution
950 get_shell_command(void)
952 static char *buf
= NULL
;
955 char *s
; /* substitution char pointer */
960 errmsg
= "shell access restricted";
962 } else if ((s
= ibufp
= get_extended_line(&j
, 1)) == NULL
)
964 REALLOC(buf
, n
, j
+ 1, ERR
);
965 buf
[i
++] = '!'; /* prefix command w/ bang */
966 while (*ibufp
!= '\n')
969 REALLOC(buf
, n
, i
+ 2, ERR
);
971 if (*ibufp
++ == '\\')
976 REALLOC(buf
, n
, i
+ 1, ERR
);
980 else if (shcmd
== NULL
|| *(shcmd
+ 1) == '\0')
982 else if (shcmd
== NULL
)
985 errmsg
= "no previous command";
988 REALLOC(buf
, n
, i
+ shcmdi
, ERR
);
989 for (s
= shcmd
+ 1; s
< shcmd
+ shcmdi
;)
995 if (*old_filename
== '\0') {
996 errmsg
= "no current filename";
999 j
= strlen(s
= strip_escapes(old_filename
));
1000 REALLOC(buf
, n
, i
+ j
, ERR
);
1006 REALLOC(shcmd
, shcmdsz
, i
+ 1, ERR
);
1007 memcpy(shcmd
, buf
, i
);
1008 shcmd
[shcmdi
= i
] = '\0';
1009 return *s
== '!' || *s
== '%';
1013 /* append_lines: insert text from stdin to after line n; stop when either a
1014 single period is read or EOF; return status */
1016 append_lines(long n
)
1019 const char *lp
= ibuf
;
1023 for (current_addr
= n
;;) {
1025 if ((l
= get_tty_line()) < 0)
1027 else if (l
== 0 || ibuf
[l
- 1] != '\n') {
1032 } else if (*(lp
= ibufp
) == '\0')
1035 while (*ibufp
++ != '\n')
1039 if (l
== 2 && lp
[0] == '.' && lp
[1] == '\n') {
1045 if ((lp
= put_sbuf_line(lp
)) == NULL
) {
1049 up
->t
= get_addressed_line_node(current_addr
);
1050 else if ((up
= push_undo_stack(UADD
, current_addr
,
1051 current_addr
)) == NULL
) {
1055 } while (lp
!= eot
);
1063 /* join_lines: replace a range of lines with the joined text of those lines */
1065 join_lines(long from
, long to
)
1067 static char *buf
= NULL
;
1074 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1075 bp
= get_addressed_line_node(from
);
1076 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1077 if ((s
= get_sbuf_line(bp
)) == NULL
)
1079 REALLOC(buf
, n
, size
+ bp
->len
, ERR
);
1080 memcpy(buf
+ size
, s
, bp
->len
);
1083 REALLOC(buf
, n
, size
+ 2, ERR
);
1084 memcpy(buf
+ size
, "\n", 2);
1085 if (delete_lines(from
, to
) < 0)
1087 current_addr
= from
- 1;
1089 if (put_sbuf_line(buf
) == NULL
||
1090 push_undo_stack(UADD
, current_addr
, current_addr
) == NULL
) {
1100 /* move_lines: move a range of lines */
1102 move_lines(long addr
)
1104 line_t
*b1
, *a1
, *b2
, *a2
;
1105 long n
= INC_MOD(second_addr
, addr_last
);
1106 long p
= first_addr
- 1;
1107 int done
= (addr
== first_addr
- 1 || addr
== second_addr
);
1111 a2
= get_addressed_line_node(n
);
1112 b2
= get_addressed_line_node(p
);
1113 current_addr
= second_addr
;
1114 } else if (push_undo_stack(UMOV
, p
, n
) == NULL
||
1115 push_undo_stack(UMOV
, addr
, INC_MOD(addr
, addr_last
)) == NULL
) {
1119 a1
= get_addressed_line_node(n
);
1120 if (addr
< first_addr
) {
1121 b1
= get_addressed_line_node(p
);
1122 b2
= get_addressed_line_node(addr
);
1123 /* this get_addressed_line_node last! */
1125 b2
= get_addressed_line_node(addr
);
1126 b1
= get_addressed_line_node(p
);
1127 /* this get_addressed_line_node last! */
1130 REQUE(b2
, b1
->q_forw
);
1131 REQUE(a1
->q_back
, a2
);
1133 current_addr
= addr
+ ((addr
< first_addr
) ?
1134 second_addr
- first_addr
+ 1 : 0);
1137 unset_active_nodes(b2
->q_forw
, a2
);
1144 /* copy_lines: copy a range of lines; return status */
1146 copy_lines(long addr
)
1148 line_t
*lp
, *np
= get_addressed_line_node(first_addr
);
1150 long n
= second_addr
- first_addr
+ 1;
1153 current_addr
= addr
;
1154 if (first_addr
<= addr
&& addr
< second_addr
) {
1155 n
= addr
- first_addr
+ 1;
1156 m
= second_addr
- addr
;
1158 for (; n
> 0; n
=m
, m
=0, np
= get_addressed_line_node(current_addr
+ 1))
1159 for (; n
-- > 0; np
= np
->q_forw
) {
1161 if ((lp
= dup_line_node(np
)) == NULL
) {
1168 else if ((up
= push_undo_stack(UADD
, current_addr
,
1169 current_addr
)) == NULL
) {
1180 /* delete_lines: delete a range of lines */
1182 delete_lines(long from
, long to
)
1187 if (push_undo_stack(UDEL
, from
, to
) == NULL
) {
1191 n
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1192 p
= get_addressed_line_node(from
- 1);
1193 /* this get_addressed_line_node last! */
1195 unset_active_nodes(p
->q_forw
, n
);
1197 addr_last
-= to
- from
+ 1;
1198 current_addr
= from
- 1;
1205 /* display_lines: print a range of lines to stdout */
1207 display_lines(long from
, long to
, int gflag
)
1214 errmsg
= "invalid address";
1217 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1218 bp
= get_addressed_line_node(from
);
1219 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1220 if ((s
= get_sbuf_line(bp
)) == NULL
)
1222 if (put_tty_line(s
, bp
->len
, current_addr
= from
++, gflag
) < 0)
1229 #define MAXMARK 26 /* max number of marks */
1231 static line_t
*mark
[MAXMARK
]; /* line markers */
1232 static int markno
; /* line marker count */
1234 /* mark_line_node: set a line node mark */
1236 mark_line_node(line_t
*lp
, int n
)
1238 if (!islower((unsigned char)n
)) {
1239 errmsg
= "invalid mark character";
1241 } else if (mark
[n
- 'a'] == NULL
)
1248 /* get_marked_node_addr: return address of a marked line */
1250 get_marked_node_addr(int n
)
1252 if (!islower((unsigned char)n
)) {
1253 errmsg
= "invalid mark character";
1256 return get_line_node_addr(mark
[n
- 'a']);
1260 /* unmark_line_node: clear line node mark */
1262 unmark_line_node(line_t
*lp
)
1266 for (i
= 0; markno
&& i
< MAXMARK
; i
++)
1267 if (mark
[i
] == lp
) {
1274 /* dup_line_node: return a pointer to a copy of a line node */
1276 dup_line_node(line_t
*lp
)
1280 if ((np
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
1281 fprintf(stderr
, "%s\n", strerror(errno
));
1282 errmsg
= "out of memory";
1285 np
->seek
= lp
->seek
;
1291 /* has_trailing_escape: return the parity of escapes preceding a character
1294 has_trailing_escape(char *s
, char *t
)
1296 return (s
== t
|| *(t
- 1) != '\\') ? 0 : !has_trailing_escape(s
, t
- 1);
1300 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1302 strip_escapes(char *s
)
1304 static char *file
= NULL
;
1305 static int filesz
= 0;
1309 REALLOC(file
, filesz
, PATH_MAX
, NULL
);
1310 while (i
< filesz
- 1 /* Worry about a possible trailing escape */
1311 && (file
[i
++] = (*s
== '\\') ? *++s
: *s
))
1318 signal_hup(int signo
)
1321 sigflags
|= (1 << (signo
- 1));
1328 signal_int(int signo
)
1331 sigflags
|= (1 << (signo
- 1));
1338 handle_hup(int signo
)
1340 char *hup
= NULL
; /* hup filename */
1342 char ed_hup
[] = "ed.hup";
1347 sigflags
&= ~(1 << (signo
- 1));
1348 if (addr_last
&& write_file(ed_hup
, "w", 1, addr_last
) < 0 &&
1349 (s
= getenv("HOME")) != NULL
&&
1350 (n
= strlen(s
)) + 8 <= PATH_MAX
&& /* "ed.hup" + '/' */
1351 (hup
= (char *) malloc(n
+ 10)) != NULL
) {
1353 if (hup
[n
- 1] != '/')
1354 hup
[n
] = '/', hup
[n
+1] = '\0';
1355 strcat(hup
, "ed.hup");
1356 write_file(hup
, "w", 1, addr_last
);
1363 handle_int(int signo
)
1367 sigflags
&= ~(1 << (signo
- 1));
1368 siglongjmp(env
, -1);
1372 int cols
= 72; /* wrap column */
1375 handle_winch(int signo
)
1377 int save_errno
= errno
;
1379 struct winsize ws
; /* window size structure */
1381 sigflags
&= ~(1 << (signo
- 1));
1382 if (ioctl(0, TIOCGWINSZ
, (char *) &ws
) >= 0) {
1383 if (ws
.ws_row
> 2) rows
= ws
.ws_row
- 2;
1384 if (ws
.ws_col
> 8) cols
= ws
.ws_col
- 8;
1390 /* is_legal_filename: return a legal filename */
1392 is_legal_filename(char *s
)
1394 if (red
&& (*s
== '!' || !strcmp(s
, "..") || strchr(s
, '/'))) {
1395 errmsg
= "shell access restricted";