1 /* $OpenBSD: main.c,v 1.35 2013/01/15 20:26:28 espie Exp $ */
2 /* $NetBSD: main.c,v 1.3 1995/03/21 09:04:44 cgd Exp $ */
4 /* main.c: This file contains the main control and user-interface routines
5 for the ed line editor. */
7 * Copyright (c) 1993 Andrew Moore, Talke Studio.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This program is based on the editor algorithm described in
36 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
37 * in Pascal," Addison-Wesley, 1981.
39 * The buffering algorithm is attributed to Rodney Ruddock of
40 * the University of Guelph, Guelph, Ontario.
42 * The cbc.c encryption code is adapted from
43 * the bdes program by Matt Bishop of Dartmouth College,
48 #include <sys/ioctl.h>
66 char stdinbuf
[1]; /* stdin buffer */
67 char *shcmd
; /* shell command buffer */
68 int shcmdsz
; /* shell command buffer size */
69 int shcmdi
; /* shell command buffer index */
70 char *ibuf
; /* ed command-line buffer */
71 int ibufsz
; /* ed command-line buffer size */
72 char *ibufp
; /* pointer to ed command-line buffer */
75 int des
= 0; /* if set, use crypt(3) for i/o */
76 int garrulous
= 0; /* if set, print all error messages */
77 int isbinary
; /* if set, buffer contains ASCII NULs */
78 int isglobal
; /* if set, doing a global command */
79 int modified
; /* if set, buffer modified since last write */
80 int mutex
= 0; /* if set, signals set "sigflags" */
81 int red
= 0; /* if set, restrict shell/directory access */
82 int scripted
= 0; /* if set, suppress diagnostics */
83 int sigflags
= 0; /* if set, signals received while mutex set */
84 int interactive
= 0; /* if set, we are in interactive mode */
86 /* if set, signal handlers are enabled */
87 volatile sig_atomic_t sigactive
= 0;
89 char old_filename
[MAXPATHLEN
] = ""; /* default filename */
90 int current_addr
; /* current address in editor buffer */
91 int addr_last
; /* last address in editor buffer */
92 int lineno
; /* script line number */
93 char *prompt
; /* command-line prompt */
94 char *dps
= "*"; /* default command-line prompt */
96 char *usage
= "usage: %s [-] [-sx] [-p string] [file]\n";
98 char *home
; /* home directory */
103 strlcpy(errmsg
, s
, sizeof(errmsg
));
106 /* ed: line editor */
108 main(volatile int argc
, char ** volatile argv
)
113 home
= getenv("HOME");
115 red
= (n
= strlen(argv
[0])) > 2 && argv
[0][n
- 3] == 'r';
117 while ((c
= getopt(argc
, argv
, "p:sx")) != -1)
119 case 'p': /* set prompt */
122 case 's': /* run script */
125 case 'x': /* use crypt */
129 fprintf(stderr
, "crypt unavailable\n?\n");
134 fprintf(stderr
, usage
, argv
[0]);
139 if (argc
&& **argv
== '-') {
149 if (!(interactive
= isatty(0))) {
152 /* assert: pipes show up as fifo's when fstat'd */
153 if (fstat(STDIN_FILENO
, &sb
) || !S_ISFIFO(sb
.st_mode
)) {
154 if (lseek(STDIN_FILENO
, 0, SEEK_CUR
)) {
161 /* assert: reliable signals! */
163 if (isatty(STDIN_FILENO
)) {
164 handle_winch(SIGWINCH
);
165 signal(SIGWINCH
, handle_winch
);
168 signal(SIGHUP
, signal_hup
);
169 signal(SIGQUIT
, SIG_IGN
);
170 signal(SIGINT
, signal_int
);
172 if (status
= sigsetjmp(env
, 1))
174 if ((status
= setjmp(env
)) != 0)
177 fputs("\n?\n", stderr
);
178 seterrmsg("interrupt");
181 sigactive
= 1; /* enable signal handlers */
182 if (argc
&& **argv
&& is_legal_filename(*argv
)) {
183 if (read_file(*argv
, 0) < 0 && !interactive
)
185 else if (**argv
!= '!')
186 strlcpy(old_filename
, *argv
,
187 sizeof old_filename
);
189 fputs("?\n", stderr
);
191 seterrmsg("invalid filename");
197 if (status
< 0 && garrulous
)
198 fprintf(stderr
, "%s\n", errmsg
);
200 fputs(prompt
, stdout
);
203 if ((n
= get_tty_line()) < 0) {
207 if (modified
&& !scripted
) {
208 fputs("?\n", stderr
);
209 seterrmsg("warning: file modified");
211 fprintf(stderr
, garrulous
?
212 "script, line %d: %s\n" :
222 } else if (ibuf
[n
- 1] != '\n') {
224 seterrmsg("unexpected end-of-file");
230 if ((status
= extract_addr_range()) >= 0 &&
231 (status
= exec_command()) >= 0)
232 if (!status
|| (status
&&
233 (status
= display_lines(current_addr
, current_addr
,
241 fputs("?\n", stderr
); /* give warning */
242 seterrmsg("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", stderr
);
262 fprintf(stderr
, garrulous
?
263 "script, line %d: %s\n" : "",
273 int 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() \
301 while (isspace(*ibufp) && *ibufp != '\n') \
305 #define MUST_BE_FIRST() \
308 seterrmsg("invalid address"); \
314 /* next_addr: return the next line address in the command buffer */
319 int addr
= current_addr
;
325 for (hd
= ibufp
;; first
= 0)
326 switch ((c
= *ibufp
)) {
334 if (isdigit(*ibufp
)) {
336 addr
+= (c
== '-' || c
== '^') ? -n
: n
;
337 } else if (!isspace(c
))
338 addr
+= (c
== '-' || c
== '^') ? -1 : 1;
340 case '0': case '1': case '2':
341 case '3': case '4': case '5':
342 case '6': case '7': case '8': case '9':
350 addr
= (c
== '.') ? current_addr
: addr_last
;
355 if ((addr
= get_matching_node_addr(
356 get_compiled_pattern(), c
== '/')) < 0)
358 else if (c
== *ibufp
)
364 if ((addr
= get_marked_node_addr(*ibufp
++)) < 0)
373 second_addr
= (c
== ';') ? current_addr
: 1;
381 else if (addr
< 0 || addr_last
< addr
) {
382 seterrmsg("invalid address");
392 /* GET_THIRD_ADDR: get a legal address from the command buffer */
393 #define GET_THIRD_ADDR(addr) \
399 if (extract_addr_range() < 0) \
401 else if (addr_cnt == 0) { \
402 seterrmsg("destination expected"); \
404 } else if (second_addr < 0 || addr_last < second_addr) { \
405 seterrmsg("invalid address"); \
408 addr = second_addr; \
413 #else /* BACKWARDS */
414 /* GET_THIRD_ADDR: get a legal address from the command buffer */
415 #define GET_THIRD_ADDR(addr) \
421 if (extract_addr_range() < 0) \
423 if (second_addr < 0 || addr_last < second_addr) { \
424 seterrmsg("invalid address"); \
427 addr = second_addr; \
434 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
435 #define GET_COMMAND_SUFFIX() \
456 if (*ibufp++ != '\n') { \
457 seterrmsg("invalid command suffix"); \
463 #define SGG 001 /* complement previous global substitute suffix */
464 #define SGP 002 /* complement previous print suffix */
465 #define SGR 004 /* use last regex instead of last pat */
466 #define SGF 010 /* repeat last substitution */
468 int patlock
= 0; /* if set, pattern not freed by get_compiled_pattern() */
470 volatile sig_atomic_t rows
= 22; /* scroll length: ws_row - 2 */
471 volatile sig_atomic_t cols
= 72; /* wrap column */
473 /* exec_command: execute the next command in command buffer; return print
478 extern int u_current_addr
;
479 extern int u_addr_last
;
481 static pattern_t
*pat
= NULL
;
482 static int sgflag
= 0;
483 static int sgnum
= 0;
494 switch ((c
= *ibufp
++)) {
496 GET_COMMAND_SUFFIX();
497 if (!isglobal
) clear_undo_stack();
498 if (append_lines(second_addr
) < 0)
502 if (check_addr_range(current_addr
, current_addr
) < 0)
504 GET_COMMAND_SUFFIX();
505 if (!isglobal
) clear_undo_stack();
506 if (delete_lines(first_addr
, second_addr
) < 0 ||
507 append_lines(current_addr
) < 0)
511 if (check_addr_range(current_addr
, current_addr
) < 0)
513 GET_COMMAND_SUFFIX();
514 if (!isglobal
) clear_undo_stack();
515 if (delete_lines(first_addr
, second_addr
) < 0)
517 else if ((addr
= INC_MOD(current_addr
, addr_last
)) != 0)
521 if (modified
&& !scripted
)
526 seterrmsg("unexpected address");
528 } else if (!isspace(*ibufp
)) {
529 seterrmsg("unexpected command suffix");
531 } else if ((fnp
= get_filename()) == NULL
)
533 GET_COMMAND_SUFFIX();
534 if (delete_lines(1, addr_last
) < 0)
537 if (close_sbuf() < 0)
539 else if (open_sbuf() < 0)
541 if (*fnp
&& *fnp
!= '!')
542 strlcpy(old_filename
, fnp
, sizeof old_filename
);
544 if (*fnp
== '\0' && *old_filename
== '\0') {
545 seterrmsg("no current filename");
549 if (read_file(*fnp
? fnp
: old_filename
, 0) < 0)
553 u_current_addr
= u_addr_last
= -1;
557 seterrmsg("unexpected address");
559 } else if (!isspace(*ibufp
)) {
560 seterrmsg("unexpected command suffix");
562 } else if ((fnp
= get_filename()) == NULL
)
564 else if (*fnp
== '!') {
565 seterrmsg("invalid redirection");
568 GET_COMMAND_SUFFIX();
570 strlcpy(old_filename
, fnp
, sizeof old_filename
);
571 puts(strip_escapes(old_filename
));
578 seterrmsg("cannot nest global commands");
580 } else if (check_addr_range(1, addr_last
) < 0)
582 else if (build_active_list(c
== 'g' || c
== 'G') < 0)
584 else if ((n
= (c
== 'G' || c
== 'V')))
585 GET_COMMAND_SUFFIX();
587 if (exec_global(n
, gflag
) < 0)
592 seterrmsg("unexpected address");
595 GET_COMMAND_SUFFIX();
596 if (*errmsg
) fprintf(stderr
, "%s\n", errmsg
);
600 seterrmsg("unexpected address");
603 GET_COMMAND_SUFFIX();
604 if ((garrulous
= 1 - garrulous
) && *errmsg
)
605 fprintf(stderr
, "%s\n", errmsg
);
608 if (second_addr
== 0) {
609 seterrmsg("invalid address");
612 GET_COMMAND_SUFFIX();
613 if (!isglobal
) clear_undo_stack();
614 if (append_lines(second_addr
- 1) < 0)
618 if (check_addr_range(current_addr
, current_addr
+ 1) < 0)
620 GET_COMMAND_SUFFIX();
621 if (!isglobal
) clear_undo_stack();
622 if (first_addr
!= second_addr
&&
623 join_lines(first_addr
, second_addr
) < 0)
628 if (second_addr
== 0) {
629 seterrmsg("invalid address");
632 GET_COMMAND_SUFFIX();
633 if (mark_line_node(get_addressed_line_node(second_addr
), c
) < 0)
637 if (check_addr_range(current_addr
, current_addr
) < 0)
639 GET_COMMAND_SUFFIX();
640 if (display_lines(first_addr
, second_addr
, gflag
| GLS
) < 0)
645 if (check_addr_range(current_addr
, current_addr
) < 0)
647 GET_THIRD_ADDR(addr
);
648 if (first_addr
<= addr
&& addr
< second_addr
) {
649 seterrmsg("invalid destination");
652 GET_COMMAND_SUFFIX();
653 if (!isglobal
) clear_undo_stack();
654 if (move_lines(addr
) < 0)
658 if (check_addr_range(current_addr
, current_addr
) < 0)
660 GET_COMMAND_SUFFIX();
661 if (display_lines(first_addr
, second_addr
, gflag
| GNP
) < 0)
666 if (check_addr_range(current_addr
, current_addr
) < 0)
668 GET_COMMAND_SUFFIX();
669 if (display_lines(first_addr
, second_addr
, gflag
| GPR
) < 0)
675 seterrmsg("unexpected address");
678 GET_COMMAND_SUFFIX();
679 prompt
= prompt
? NULL
: optarg
? optarg
: dps
;
684 seterrmsg("unexpected address");
687 GET_COMMAND_SUFFIX();
688 gflag
= (modified
&& !scripted
&& c
== 'q') ? EMOD
: EOF
;
691 if (!isspace(*ibufp
)) {
692 seterrmsg("unexpected command suffix");
694 } else if (addr_cnt
== 0)
695 second_addr
= addr_last
;
696 if ((fnp
= get_filename()) == NULL
)
698 GET_COMMAND_SUFFIX();
699 if (!isglobal
) clear_undo_stack();
700 if (*old_filename
== '\0' && *fnp
!= '!')
701 strlcpy(old_filename
, fnp
, sizeof old_filename
);
703 if (*fnp
== '\0' && *old_filename
== '\0') {
704 seterrmsg("no current filename");
708 if ((addr
= read_file(*fnp
? fnp
: old_filename
,
711 else if (addr
&& addr
!= addr_last
)
732 case '0': case '1': case '2': case '3': case '4':
733 case '5': case '6': case '7': case '8': case '9':
734 STRTOI(sgnum
, ibufp
);
736 sgflag
&= ~GSG
; /* override GSG */
740 seterrmsg("invalid command suffix");
744 } while (sflags
&& *ibufp
!= '\n');
745 if (sflags
&& !pat
) {
746 seterrmsg("no previous substitution");
748 } else if (sflags
& SGG
)
749 sgnum
= 0; /* override numeric arg */
750 if (*ibufp
!= '\n' && *(ibufp
+ 1) == '\n') {
751 seterrmsg("invalid pattern delimiter");
756 if ((!sflags
|| (sflags
& SGR
)) &&
757 (tpat
= get_compiled_pattern()) == NULL
) {
760 } else if (tpat
!= pat
) {
766 patlock
= 1; /* reserve pattern */
769 if (!sflags
&& extract_subst_tail(&sgflag
, &sgnum
) < 0)
779 sgflag
&= ~(GLS
| GNP
);
799 if (check_addr_range(current_addr
, current_addr
) < 0)
801 GET_COMMAND_SUFFIX();
802 if (!isglobal
) clear_undo_stack();
803 if (search_and_replace(pat
, sgflag
, sgnum
) < 0)
807 if (check_addr_range(current_addr
, current_addr
) < 0)
809 GET_THIRD_ADDR(addr
);
810 GET_COMMAND_SUFFIX();
811 if (!isglobal
) clear_undo_stack();
812 if (copy_lines(addr
) < 0)
817 seterrmsg("unexpected address");
820 GET_COMMAND_SUFFIX();
821 if (pop_undo_stack() < 0)
826 if ((n
= *ibufp
) == 'q' || n
== 'Q') {
830 if (!isspace(*ibufp
)) {
831 seterrmsg("unexpected command suffix");
833 } else if ((fnp
= get_filename()) == NULL
)
835 if (addr_cnt
== 0 && !addr_last
)
836 first_addr
= second_addr
= 0;
837 else if (check_addr_range(1, addr_last
) < 0)
839 GET_COMMAND_SUFFIX();
840 if (*old_filename
== '\0' && *fnp
!= '!')
841 strlcpy(old_filename
, fnp
, sizeof old_filename
);
843 if (*fnp
== '\0' && *old_filename
== '\0') {
844 seterrmsg("no current filename");
848 if ((addr
= write_file(*fnp
? fnp
: old_filename
,
849 (c
== 'W') ? "a" : "w", first_addr
, second_addr
)) < 0)
851 else if (addr
== addr_last
)
853 else if (modified
&& !scripted
&& n
== 'q')
858 seterrmsg("unexpected address");
861 GET_COMMAND_SUFFIX();
865 seterrmsg("crypt unavailable");
872 if (check_addr_range(first_addr
, current_addr
+ 1) < 0)
874 if (check_addr_range(first_addr
, current_addr
+ !isglobal
) < 0)
877 else if ('0' < *ibufp
&& *ibufp
<= '9')
879 GET_COMMAND_SUFFIX();
880 if (display_lines(second_addr
, min(addr_last
,
881 second_addr
+ rows
), gflag
) < 0)
886 GET_COMMAND_SUFFIX();
887 printf("%d\n", addr_cnt
? second_addr
: addr_last
);
891 seterrmsg("unexpected address");
893 } else if ((sflags
= get_shell_command()) < 0)
895 GET_COMMAND_SUFFIX();
896 if (sflags
) printf("%s\n", shcmd
+ 1);
898 if (!scripted
) printf("!\n");
903 if (check_addr_range(first_addr
, current_addr
+ 1) < 0
905 if (check_addr_range(first_addr
, current_addr
+ !isglobal
) < 0
907 || display_lines(second_addr
, second_addr
, 0) < 0)
911 seterrmsg("unknown command");
918 /* check_addr_range: return status of address range check */
920 check_addr_range(int n
, int m
)
926 if (first_addr
> second_addr
|| 1 > first_addr
||
927 second_addr
> addr_last
) {
928 seterrmsg("invalid address");
935 /* get_matching_node_addr: return the address of the next line matching a
936 pattern in a given direction. wrap around begin/end of editor buffer if
939 get_matching_node_addr(pattern_t
*pat
, int dir
)
942 int n
= current_addr
;
945 if (!pat
) return ERR
;
947 if ((n
= dir
? INC_MOD(n
, addr_last
) : DEC_MOD(n
, addr_last
))) {
948 lp
= get_addressed_line_node(n
);
949 if ((s
= get_sbuf_line(lp
)) == NULL
)
952 NUL_TO_NEWLINE(s
, lp
->len
);
953 if (!regexec(pat
, s
, 0, NULL
, 0))
956 } while (n
!= current_addr
);
957 seterrmsg("no match");
962 /* get_filename: return pointer to copy of filename in the command buffer */
966 static char *file
= NULL
;
967 static int filesz
= 0;
970 if (*ibufp
!= '\n') {
972 if (*ibufp
== '\n') {
973 seterrmsg("invalid filename");
975 } else if ((ibufp
= get_extended_line(&n
, 1)) == NULL
)
977 else if (*ibufp
== '!') {
979 if ((n
= get_shell_command()) < 0)
981 if (n
) printf("%s\n", shcmd
+ 1);
983 } else if (n
>= MAXPATHLEN
) {
984 seterrmsg("filename too long");
989 else if (*old_filename
== '\0') {
990 seterrmsg("no current filename");
994 REALLOC(file
, filesz
, MAXPATHLEN
, NULL
);
995 for (n
= 0; *ibufp
!= '\n';)
996 file
[n
++] = *ibufp
++;
998 return is_legal_filename(file
) ? file
: NULL
;
1002 /* get_shell_command: read a shell command from stdin; return substitution
1005 get_shell_command(void)
1007 static char *buf
= NULL
;
1010 char *s
; /* substitution char pointer */
1015 seterrmsg("shell access restricted");
1017 } else if ((s
= ibufp
= get_extended_line(&j
, 1)) == NULL
)
1019 REALLOC(buf
, n
, j
+ 1, ERR
);
1020 buf
[i
++] = '!'; /* prefix command w/ bang */
1021 while (*ibufp
!= '\n')
1024 REALLOC(buf
, n
, i
+ 2, ERR
);
1026 if (*ibufp
++ == '\\')
1027 buf
[i
++] = *ibufp
++;
1031 REALLOC(buf
, n
, i
+ 1, ERR
);
1032 buf
[i
++] = *ibufp
++;
1035 else if (shcmd
== NULL
|| *(shcmd
+ 1) == '\0')
1037 else if (shcmd
== NULL
)
1040 seterrmsg("no previous command");
1043 REALLOC(buf
, n
, i
+ shcmdi
, ERR
);
1044 for (s
= shcmd
+ 1; s
< shcmd
+ shcmdi
;)
1050 if (*old_filename
== '\0') {
1051 seterrmsg("no current filename");
1054 j
= strlen(s
= strip_escapes(old_filename
));
1055 REALLOC(buf
, n
, i
+ j
, ERR
);
1061 REALLOC(shcmd
, shcmdsz
, i
+ 1, ERR
);
1062 memcpy(shcmd
, buf
, i
);
1063 shcmd
[shcmdi
= i
] = '\0';
1064 return *s
== '!' || *s
== '%';
1068 /* append_lines: insert text from stdin to after line n; stop when either a
1069 single period is read or EOF; return status */
1078 for (current_addr
= n
;;) {
1080 if ((l
= get_tty_line()) < 0)
1082 else if (l
== 0 || ibuf
[l
- 1] != '\n') {
1087 } else if (*(lp
= ibufp
) == '\0')
1090 while (*ibufp
++ != '\n')
1094 if (l
== 2 && lp
[0] == '.' && lp
[1] == '\n') {
1100 if ((lp
= put_sbuf_line(lp
)) == NULL
) {
1104 up
->t
= get_addressed_line_node(current_addr
);
1105 else if ((up
= push_undo_stack(UADD
, current_addr
,
1106 current_addr
)) == NULL
) {
1110 } while (lp
!= eot
);
1118 /* join_lines: replace a range of lines with the joined text of those lines */
1120 join_lines(int from
, int to
)
1122 static char *buf
= NULL
;
1129 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1130 bp
= get_addressed_line_node(from
);
1131 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1132 if ((s
= get_sbuf_line(bp
)) == NULL
)
1134 REALLOC(buf
, n
, size
+ bp
->len
, ERR
);
1135 memcpy(buf
+ size
, s
, bp
->len
);
1138 REALLOC(buf
, n
, size
+ 2, ERR
);
1139 memcpy(buf
+ size
, "\n", 2);
1140 if (delete_lines(from
, to
) < 0)
1142 current_addr
= from
- 1;
1144 if (put_sbuf_line(buf
) == NULL
||
1145 push_undo_stack(UADD
, current_addr
, current_addr
) == NULL
) {
1155 /* move_lines: move a range of lines */
1157 move_lines(int addr
)
1159 line_t
*b1
, *a1
, *b2
, *a2
;
1160 int n
= INC_MOD(second_addr
, addr_last
);
1161 int p
= first_addr
- 1;
1162 int done
= (addr
== first_addr
- 1 || addr
== second_addr
);
1166 a2
= get_addressed_line_node(n
);
1167 b2
= get_addressed_line_node(p
);
1168 current_addr
= second_addr
;
1169 } else if (push_undo_stack(UMOV
, p
, n
) == NULL
||
1170 push_undo_stack(UMOV
, addr
, INC_MOD(addr
, addr_last
)) == NULL
) {
1174 a1
= get_addressed_line_node(n
);
1175 if (addr
< first_addr
) {
1176 b1
= get_addressed_line_node(p
);
1177 b2
= get_addressed_line_node(addr
);
1178 /* this get_addressed_line_node last! */
1180 b2
= get_addressed_line_node(addr
);
1181 b1
= get_addressed_line_node(p
);
1182 /* this get_addressed_line_node last! */
1185 REQUE(b2
, b1
->q_forw
);
1186 REQUE(a1
->q_back
, a2
);
1188 current_addr
= addr
+ ((addr
< first_addr
) ?
1189 second_addr
- first_addr
+ 1 : 0);
1192 unset_active_nodes(b2
->q_forw
, a2
);
1199 /* copy_lines: copy a range of lines; return status */
1201 copy_lines(int addr
)
1203 line_t
*lp
, *np
= get_addressed_line_node(first_addr
);
1205 int n
= second_addr
- first_addr
+ 1;
1208 current_addr
= addr
;
1209 if (first_addr
<= addr
&& addr
< second_addr
) {
1210 n
= addr
- first_addr
+ 1;
1211 m
= second_addr
- addr
;
1213 for (; n
> 0; n
=m
, m
=0, np
= get_addressed_line_node(current_addr
+ 1))
1214 for (; n
-- > 0; np
= np
->q_forw
) {
1216 if ((lp
= dup_line_node(np
)) == NULL
) {
1223 else if ((up
= push_undo_stack(UADD
, current_addr
,
1224 current_addr
)) == NULL
) {
1235 /* delete_lines: delete a range of lines */
1237 delete_lines(int from
, int to
)
1242 if (push_undo_stack(UDEL
, from
, to
) == NULL
) {
1246 n
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1247 p
= get_addressed_line_node(from
- 1);
1248 /* this get_addressed_line_node last! */
1250 unset_active_nodes(p
->q_forw
, n
);
1252 addr_last
-= to
- from
+ 1;
1253 current_addr
= from
- 1;
1260 /* display_lines: print a range of lines to stdout */
1262 display_lines(int from
, int to
, int gflag
)
1269 seterrmsg("invalid address");
1272 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1273 bp
= get_addressed_line_node(from
);
1274 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1275 if ((s
= get_sbuf_line(bp
)) == NULL
)
1277 if (put_tty_line(s
, bp
->len
, current_addr
= from
++, gflag
) < 0)
1284 #define MAXMARK 26 /* max number of marks */
1286 line_t
*mark
[MAXMARK
]; /* line markers */
1287 int markno
; /* line marker count */
1289 /* mark_line_node: set a line node mark */
1291 mark_line_node(line_t
*lp
, int n
)
1294 seterrmsg("invalid mark character");
1296 } else if (mark
[n
- 'a'] == NULL
)
1303 /* get_marked_node_addr: return address of a marked line */
1305 get_marked_node_addr(int n
)
1308 seterrmsg("invalid mark character");
1311 return get_line_node_addr(mark
[n
- 'a']);
1315 /* unmark_line_node: clear line node mark */
1317 unmark_line_node(line_t
*lp
)
1321 for (i
= 0; markno
&& i
< MAXMARK
; i
++)
1322 if (mark
[i
] == lp
) {
1329 /* dup_line_node: return a pointer to a copy of a line node */
1331 dup_line_node(line_t
*lp
)
1335 if ((np
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
1337 seterrmsg("out of memory");
1340 np
->seek
= lp
->seek
;
1346 /* has_trailing_escape: return the parity of escapes preceding a character
1349 has_trailing_escape(char *s
, char *t
)
1351 return (s
== t
|| *(t
- 1) != '\\') ? 0 : !has_trailing_escape(s
, t
- 1);
1355 /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1357 strip_escapes(char *s
)
1359 static char *file
= NULL
;
1360 static int filesz
= 0;
1364 REALLOC(file
, filesz
, MAXPATHLEN
, NULL
);
1365 /* assert: no trailing escape */
1366 while ((file
[i
++] = (*s
== '\\') ? *++s
: *s
) != '\0' &&
1369 file
[MAXPATHLEN
-1] = '\0';
1375 signal_hup(int signo
)
1377 int save_errno
= errno
;
1380 sigflags
|= (1 << (signo
- 1));
1388 signal_int(int signo
)
1390 int save_errno
= errno
;
1393 sigflags
|= (1 << (signo
- 1));
1401 handle_hup(int signo
)
1403 char hup
[MAXPATHLEN
];
1407 sigflags
&= ~(1 << (signo
- 1));
1408 /* XXX signal race */
1409 if (addr_last
&& write_file("ed.hup", "w", 1, addr_last
) < 0 &&
1410 home
!= NULL
&& home
[0] == '/') {
1411 if (strlcpy(hup
, home
, sizeof(hup
)) < sizeof(hup
) &&
1412 strlcat(hup
, "/ed.hup", sizeof(hup
)) < sizeof(hup
))
1413 write_file(hup
, "w", 1, addr_last
);
1420 handle_int(int signo
)
1424 sigflags
&= ~(1 << (signo
- 1));
1425 #ifdef _POSIX_SOURCE
1426 siglongjmp(env
, -1);
1434 handle_winch(int signo
)
1436 int save_errno
= errno
;
1437 struct winsize ws
; /* window size structure */
1439 sigflags
&= ~(1 << (signo
- 1));
1440 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) >= 0) {
1442 rows
= ws
.ws_row
- 2;
1444 cols
= ws
.ws_col
- 8;
1450 /* is_legal_filename: return a legal filename */
1452 is_legal_filename(char *s
)
1454 if (red
&& (*s
== '!' || !strcmp(s
, "..") || strchr(s
, '/'))) {
1455 seterrmsg("shell access restricted");