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: src/bin/ed/main.c,v 1.14.2.4 2001/08/01 02:36:03 obrien Exp $
31 * $DragonFly: src/bin/ed/main.c,v 1.8 2005/03/09 02:38:51 drhodus Exp $
37 * This program is based on the editor algorithm described in
38 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
39 * in Pascal," Addison-Wesley, 1981.
41 * The buffering algorithm is attributed to Rodney Ruddock of
42 * the University of Guelph, Guelph, Ontario.
44 * The cbc.c encryption code is adapted from
45 * the bdes program by Matt Bishop of Dartmouth College,
50 #include <sys/types.h>
52 #include <sys/ioctl.h>
69 char stdinbuf
[1]; /* stdin buffer */
70 char *shcmd
; /* shell command buffer */
71 int shcmdsz
; /* shell command buffer size */
72 int shcmdi
; /* shell command buffer index */
73 char *ibuf
; /* ed command-line buffer */
74 int ibufsz
; /* ed command-line buffer size */
75 char *ibufp
; /* pointer to ed command-line buffer */
78 int des
= 0; /* if set, use crypt(3) for i/o */
79 int garrulous
= 0; /* if set, print all error messages */
80 int isbinary
; /* if set, buffer contains ASCII NULs */
81 int isglobal
; /* if set, doing a global command */
82 int modified
; /* if set, buffer modified since last write */
83 int mutex
= 0; /* if set, signals set "sigflags" */
84 int red
= 0; /* if set, restrict shell/directory access */
85 int scripted
= 0; /* if set, suppress diagnostics */
86 int sigflags
= 0; /* if set, signals received while mutex set */
87 int sigactive
= 0; /* if set, signal handlers are enabled */
89 char old_filename
[PATH_MAX
] = ""; /* default filename */
90 long current_addr
; /* current address in editor buffer */
91 long addr_last
; /* last address in editor buffer */
92 int lineno
; /* script line number */
93 const char *prompt
; /* command-line prompt */
94 const char *dps
= "*"; /* default command-line prompt */
96 const char usage
[] = "usage: %s [-] [-sx] [-p string] [name]\n";
100 main(int argc
, char **argv
)
105 /* Avoid longjmp clobbering */
110 setlocale(LC_ALL
, "");
112 red
= (n
= strlen(argv
[0])) > 2 && argv
[0][n
- 3] == 'r';
114 while ((c
= getopt(argc
, argv
, "p:sx")) != -1)
116 case 'p': /* set prompt */
119 case 's': /* run script */
122 case 'x': /* use crypt */
127 fprintf(stderr
, "crypt unavailable\n?\n");
132 fprintf(stderr
, usage
, argv
[0]);
137 if (argc
&& **argv
== '-') {
146 /* assert: reliable signals! */
148 handle_winch(SIGWINCH
);
149 if (isatty(0)) signal(SIGWINCH
, handle_winch
);
151 signal(SIGHUP
, signal_hup
);
152 signal(SIGQUIT
, SIG_IGN
);
153 signal(SIGINT
, signal_int
);
155 if ((status
= sigsetjmp(env
, 1)))
157 if ((status
= setjmp(env
)))
160 fputs("\n?\n", stderr
);
161 sprintf(errmsg
, "interrupt");
164 sigactive
= 1; /* enable signal handlers */
165 if (argc
&& **argv
&& is_legal_filename(*argv
)) {
166 if (read_file(*argv
, 0) < 0 && !isatty(0))
168 else if (**argv
!= '!')
169 if (strlcpy(old_filename
, *argv
, sizeof(old_filename
))
170 >= sizeof(old_filename
))
173 fputs("?\n", stderr
);
175 sprintf(errmsg
, "invalid filename");
181 if (status
< 0 && garrulous
)
182 fprintf(stderr
, "%s\n", errmsg
);
184 printf("%s", prompt
);
187 if ((n
= get_tty_line()) < 0) {
191 if (modified
&& !scripted
) {
192 fputs("?\n", stderr
);
193 sprintf(errmsg
, "warning: file modified");
195 fprintf(stderr
, garrulous
?
196 "script, line %d: %s\n" :
206 } else if (ibuf
[n
- 1] != '\n') {
208 sprintf(errmsg
, "unexpected end-of-file");
214 if ((status
= extract_addr_range()) >= 0 &&
215 (status
= exec_command()) >= 0)
217 (status
= display_lines(current_addr
, current_addr
,
225 fputs("?\n", stderr
); /* give warning */
226 sprintf(errmsg
, "warning: file modified");
228 fprintf(stderr
, garrulous
?
229 "script, line %d: %s\n" :
236 fprintf(stderr
, garrulous
?
237 "script, line %d: %s\n" : "",
240 fprintf(stderr
, garrulous
? "%s\n" : "",
244 fputs("?\n", stderr
);
246 fprintf(stderr
, garrulous
?
247 "script, line %d: %s\n" : "",
257 long first_addr
, second_addr
, addr_cnt
;
259 /* extract_addr_range: get line addresses from the command buffer until an
260 illegal address is seen; return status */
262 extract_addr_range(void)
267 first_addr
= second_addr
= current_addr
;
268 while ((addr
= next_addr()) >= 0) {
270 first_addr
= second_addr
;
272 if (*ibufp
!= ',' && *ibufp
!= ';')
274 else if (*ibufp
++ == ';')
277 if ((addr_cnt
= min(addr_cnt
, 2)) == 1 || second_addr
!= addr
)
278 first_addr
= second_addr
;
279 return (addr
== ERR
) ? ERR
: 0;
283 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
285 #define MUST_BE_FIRST() \
286 if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
288 /* next_addr: return the next line address in the command buffer */
293 long addr
= current_addr
;
299 for (hd
= ibufp
;; first
= 0)
300 switch (c
= *ibufp
) {
308 if (isdigit((unsigned char)*ibufp
)) {
310 addr
+= (c
== '-' || c
== '^') ? -n
: n
;
311 } else if (!isspace((unsigned char)c
))
312 addr
+= (c
== '-' || c
== '^') ? -1 : 1;
314 case '0': case '1': case '2':
315 case '3': case '4': case '5':
316 case '6': case '7': case '8': case '9':
324 addr
= (c
== '.') ? current_addr
: addr_last
;
329 if ((addr
= get_matching_node_addr(
330 get_compiled_pattern(), c
== '/')) < 0)
332 else if (c
== *ibufp
)
338 if ((addr
= get_marked_node_addr(*ibufp
++)) < 0)
347 second_addr
= (c
== ';') ? current_addr
: 1;
355 else if (addr
< 0 || addr_last
< addr
) {
356 sprintf(errmsg
, "invalid address");
366 /* GET_THIRD_ADDR: get a legal address from the command buffer */
367 #define GET_THIRD_ADDR(addr) \
371 ol1 = first_addr, ol2 = second_addr; \
372 if (extract_addr_range() < 0) \
374 else if (addr_cnt == 0) { \
375 sprintf(errmsg, "destination expected"); \
377 } else if (second_addr < 0 || addr_last < second_addr) { \
378 sprintf(errmsg, "invalid address"); \
381 addr = second_addr; \
382 first_addr = ol1, second_addr = ol2; \
384 #else /* BACKWARDS */
385 /* GET_THIRD_ADDR: get a legal address from the command buffer */
386 #define GET_THIRD_ADDR(addr) \
390 ol1 = first_addr, ol2 = second_addr; \
391 if (extract_addr_range() < 0) \
393 if (second_addr < 0 || addr_last < second_addr) { \
394 sprintf(errmsg, "invalid address"); \
397 addr = second_addr; \
398 first_addr = ol1, second_addr = ol2; \
403 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
404 #define GET_COMMAND_SUFFIX() { \
409 gflag |= GPR, ibufp++; \
412 gflag |= GLS, ibufp++; \
415 gflag |= GNP, ibufp++; \
421 if (*ibufp++ != '\n') { \
422 sprintf(errmsg, "invalid command suffix"); \
429 #define SGG 001 /* complement previous global substitute suffix */
430 #define SGP 002 /* complement previous print suffix */
431 #define SGR 004 /* use last regex instead of last pat */
432 #define SGF 010 /* repeat last substitution */
434 int patlock
= 0; /* if set, pattern not freed by get_compiled_pattern() */
436 long rows
= 22; /* scroll length: ws_row - 2 */
438 /* exec_command: execute the next command in command buffer; return print
443 static pattern_t
*pat
= NULL
;
444 static int sgflag
= 0;
445 static long sgnum
= 0;
456 switch(c
= *ibufp
++) {
458 GET_COMMAND_SUFFIX();
459 if (!isglobal
) clear_undo_stack();
460 if (append_lines(second_addr
) < 0)
464 if (check_addr_range(current_addr
, current_addr
) < 0)
466 GET_COMMAND_SUFFIX();
467 if (!isglobal
) clear_undo_stack();
468 if (delete_lines(first_addr
, second_addr
) < 0 ||
469 append_lines(current_addr
) < 0)
473 if (check_addr_range(current_addr
, current_addr
) < 0)
475 GET_COMMAND_SUFFIX();
476 if (!isglobal
) clear_undo_stack();
477 if (delete_lines(first_addr
, second_addr
) < 0)
479 else if ((addr
= INC_MOD(current_addr
, addr_last
)) != 0)
483 if (modified
&& !scripted
)
488 sprintf(errmsg
, "unexpected address");
490 } else if (!isspace((unsigned char)*ibufp
)) {
491 sprintf(errmsg
, "unexpected command suffix");
493 } else if ((fnp
= get_filename()) == NULL
)
495 GET_COMMAND_SUFFIX();
496 if (delete_lines(1, addr_last
) < 0)
499 if (close_sbuf() < 0)
501 else if (open_sbuf() < 0)
503 if (*fnp
&& *fnp
!= '!') strcpy(old_filename
, fnp
);
505 if (*fnp
== '\0' && *old_filename
== '\0') {
506 sprintf(errmsg
, "no current filename");
510 if (read_file(*fnp
? fnp
: old_filename
, 0) < 0)
514 u_current_addr
= u_addr_last
= -1;
518 sprintf(errmsg
, "unexpected address");
520 } else if (!isspace((unsigned char)*ibufp
)) {
521 sprintf(errmsg
, "unexpected command suffix");
523 } else if ((fnp
= get_filename()) == NULL
)
525 else if (*fnp
== '!') {
526 sprintf(errmsg
, "invalid redirection");
529 GET_COMMAND_SUFFIX();
530 if (*fnp
) strcpy(old_filename
, fnp
);
531 printf("%s\n", strip_escapes(old_filename
));
538 sprintf(errmsg
, "cannot nest global commands");
540 } else if (check_addr_range(1, addr_last
) < 0)
542 else if (build_active_list(c
== 'g' || c
== 'G') < 0)
544 else if ((n
= (c
== 'G' || c
== 'V')))
545 GET_COMMAND_SUFFIX();
547 if (exec_global(n
, gflag
) < 0)
552 sprintf(errmsg
, "unexpected address");
555 GET_COMMAND_SUFFIX();
556 if (*errmsg
) fprintf(stderr
, "%s\n", errmsg
);
560 sprintf(errmsg
, "unexpected address");
563 GET_COMMAND_SUFFIX();
564 if ((garrulous
= 1 - garrulous
) && *errmsg
)
565 fprintf(stderr
, "%s\n", errmsg
);
568 if (second_addr
== 0) {
569 sprintf(errmsg
, "invalid address");
572 GET_COMMAND_SUFFIX();
573 if (!isglobal
) clear_undo_stack();
574 if (append_lines(second_addr
- 1) < 0)
578 if (check_addr_range(current_addr
, current_addr
+ 1) < 0)
580 GET_COMMAND_SUFFIX();
581 if (!isglobal
) clear_undo_stack();
582 if (first_addr
!= second_addr
&&
583 join_lines(first_addr
, second_addr
) < 0)
588 if (second_addr
== 0) {
589 sprintf(errmsg
, "invalid address");
592 GET_COMMAND_SUFFIX();
593 if (mark_line_node(get_addressed_line_node(second_addr
), c
) < 0)
597 if (check_addr_range(current_addr
, current_addr
) < 0)
599 GET_COMMAND_SUFFIX();
600 if (display_lines(first_addr
, second_addr
, gflag
| GLS
) < 0)
605 if (check_addr_range(current_addr
, current_addr
) < 0)
607 GET_THIRD_ADDR(addr
);
608 if (first_addr
<= addr
&& addr
< second_addr
) {
609 sprintf(errmsg
, "invalid destination");
612 GET_COMMAND_SUFFIX();
613 if (!isglobal
) clear_undo_stack();
614 if (move_lines(addr
) < 0)
618 if (check_addr_range(current_addr
, current_addr
) < 0)
620 GET_COMMAND_SUFFIX();
621 if (display_lines(first_addr
, second_addr
, gflag
| GNP
) < 0)
626 if (check_addr_range(current_addr
, current_addr
) < 0)
628 GET_COMMAND_SUFFIX();
629 if (display_lines(first_addr
, second_addr
, gflag
| GPR
) < 0)
635 sprintf(errmsg
, "unexpected address");
638 GET_COMMAND_SUFFIX();
639 prompt
= prompt
? NULL
: optarg
? optarg
: dps
;
644 sprintf(errmsg
, "unexpected address");
647 GET_COMMAND_SUFFIX();
648 gflag
= (modified
&& !scripted
&& c
== 'q') ? EMOD
: EOF
;
651 if (!isspace((unsigned char)*ibufp
)) {
652 sprintf(errmsg
, "unexpected command suffix");
654 } else if (addr_cnt
== 0)
655 second_addr
= addr_last
;
656 if ((fnp
= get_filename()) == NULL
)
658 GET_COMMAND_SUFFIX();
659 if (!isglobal
) clear_undo_stack();
660 if (*old_filename
== '\0' && *fnp
!= '!')
661 strcpy(old_filename
, fnp
);
663 if (*fnp
== '\0' && *old_filename
== '\0') {
664 sprintf(errmsg
, "no current filename");
668 if ((addr
= read_file(*fnp
? fnp
: old_filename
, second_addr
)) < 0)
670 else if (addr
&& addr
!= addr_last
)
691 case '0': case '1': case '2': case '3': case '4':
692 case '5': case '6': case '7': case '8': case '9':
693 STRTOL(sgnum
, ibufp
);
695 sgflag
&= ~GSG
; /* override GSG */
699 sprintf(errmsg
, "invalid command suffix");
703 } while (sflags
&& *ibufp
!= '\n');
704 if (sflags
&& !pat
) {
705 sprintf(errmsg
, "no previous substitution");
707 } else if (sflags
& SGG
)
708 sgnum
= 0; /* override numeric arg */
709 if (*ibufp
!= '\n' && *(ibufp
+ 1) == '\n') {
710 sprintf(errmsg
, "invalid pattern delimiter");
715 if ((!sflags
|| (sflags
& SGR
)) &&
716 (tpat
= get_compiled_pattern()) == NULL
) {
719 } else if (tpat
!= pat
) {
725 patlock
= 1; /* reserve pattern */
728 if (!sflags
&& extract_subst_tail(&sgflag
, &sgnum
) < 0)
737 sgflag
^= GPR
, sgflag
&= ~(GLS
| GNP
);
741 sgflag
|= GPR
, ibufp
++;
744 sgflag
|= GLS
, ibufp
++;
747 sgflag
|= GNP
, ibufp
++;
753 if (check_addr_range(current_addr
, current_addr
) < 0)
755 GET_COMMAND_SUFFIX();
756 if (!isglobal
) clear_undo_stack();
757 if (search_and_replace(pat
, sgflag
, sgnum
) < 0)
761 if (check_addr_range(current_addr
, current_addr
) < 0)
763 GET_THIRD_ADDR(addr
);
764 GET_COMMAND_SUFFIX();
765 if (!isglobal
) clear_undo_stack();
766 if (copy_lines(addr
) < 0)
771 sprintf(errmsg
, "unexpected address");
774 GET_COMMAND_SUFFIX();
775 if (pop_undo_stack() < 0)
780 if ((n
= *ibufp
) == 'q' || n
== 'Q') {
784 if (!isspace((unsigned char)*ibufp
)) {
785 sprintf(errmsg
, "unexpected command suffix");
787 } else if ((fnp
= get_filename()) == NULL
)
789 if (addr_cnt
== 0 && !addr_last
)
790 first_addr
= second_addr
= 0;
791 else if (check_addr_range(1, addr_last
) < 0)
793 GET_COMMAND_SUFFIX();
794 if (*old_filename
== '\0' && *fnp
!= '!')
795 strcpy(old_filename
, fnp
);
797 if (*fnp
== '\0' && *old_filename
== '\0') {
798 sprintf(errmsg
, "no current filename");
802 if ((addr
= write_file(*fnp
? fnp
: old_filename
,
803 (c
== 'W') ? "a" : "w", first_addr
, second_addr
)) < 0)
805 else if (addr
== addr_last
)
807 else if (modified
&& !scripted
&& n
== 'q')
812 sprintf(errmsg
, "unexpected address");
815 GET_COMMAND_SUFFIX();
819 sprintf(errmsg
, "crypt unavailable");
824 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0)
826 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0)
829 else if ('0' < *ibufp
&& *ibufp
<= '9')
831 GET_COMMAND_SUFFIX();
832 if (display_lines(second_addr
, min(addr_last
,
833 second_addr
+ rows
), gflag
) < 0)
838 GET_COMMAND_SUFFIX();
839 printf("%ld\n", addr_cnt
? second_addr
: addr_last
);
843 sprintf(errmsg
, "unexpected address");
845 } else if ((sflags
= get_shell_command()) < 0)
847 GET_COMMAND_SUFFIX();
848 if (sflags
) printf("%s\n", shcmd
+ 1);
850 if (!scripted
) printf("!\n");
854 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0
856 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0
858 || display_lines(second_addr
, second_addr
, 0) < 0)
862 sprintf(errmsg
, "unknown command");
869 /* check_addr_range: return status of address range check */
871 check_addr_range(long n
, long m
)
877 if (first_addr
> second_addr
|| 1 > first_addr
||
878 second_addr
> addr_last
) {
879 sprintf(errmsg
, "invalid address");
886 /* get_matching_node_addr: return the address of the next line matching a
887 pattern in a given direction. wrap around begin/end of editor buffer if
890 get_matching_node_addr(pattern_t
*pat
, int dir
)
893 long n
= current_addr
;
896 if (!pat
) return ERR
;
898 if ((n
= dir
? INC_MOD(n
, addr_last
) : DEC_MOD(n
, addr_last
))) {
899 lp
= get_addressed_line_node(n
);
900 if ((s
= get_sbuf_line(lp
)) == NULL
)
903 NUL_TO_NEWLINE(s
, lp
->len
);
904 if (!regexec(pat
, s
, 0, NULL
, 0))
907 } while (n
!= current_addr
);
908 sprintf(errmsg
, "no match");
913 /* get_filename: return pointer to copy of filename in the command buffer */
917 static char *file
= NULL
;
918 static int filesz
= 0;
922 if (*ibufp
!= '\n') {
924 if (*ibufp
== '\n') {
925 sprintf(errmsg
, "invalid filename");
927 } else if ((ibufp
= get_extended_line(&n
, 1)) == NULL
)
929 else if (*ibufp
== '!') {
931 if ((n
= get_shell_command()) < 0)
934 printf("%s\n", shcmd
+ 1);
936 } else if (n
> PATH_MAX
- 1) {
937 sprintf(errmsg
, "filename too long");
942 else if (*old_filename
== '\0') {
943 sprintf(errmsg
, "no current filename");
947 REALLOC(file
, filesz
, PATH_MAX
, NULL
);
948 for (n
= 0; *ibufp
!= '\n';)
949 file
[n
++] = *ibufp
++;
951 return is_legal_filename(file
) ? file
: NULL
;
955 /* get_shell_command: read a shell command from stdin; return substitution
958 get_shell_command(void)
960 static char *buf
= NULL
;
963 char *s
; /* substitution char pointer */
968 sprintf(errmsg
, "shell access restricted");
970 } else if ((s
= ibufp
= get_extended_line(&j
, 1)) == NULL
)
972 REALLOC(buf
, n
, j
+ 1, ERR
);
973 buf
[i
++] = '!'; /* prefix command w/ bang */
974 while (*ibufp
!= '\n')
977 REALLOC(buf
, n
, i
+ 2, ERR
);
979 if (*ibufp
++ == '\\')
984 REALLOC(buf
, n
, i
+ 1, ERR
);
988 else if (shcmd
== NULL
|| *(shcmd
+ 1) == '\0')
990 else if (shcmd
== NULL
)
993 sprintf(errmsg
, "no previous command");
996 REALLOC(buf
, n
, i
+ shcmdi
, ERR
);
997 for (s
= shcmd
+ 1; s
< shcmd
+ shcmdi
;)
1003 if (*old_filename
== '\0') {
1004 sprintf(errmsg
, "no current filename");
1007 j
= strlen(s
= strip_escapes(old_filename
));
1008 REALLOC(buf
, n
, i
+ j
, ERR
);
1014 REALLOC(shcmd
, shcmdsz
, i
+ 1, ERR
);
1015 memcpy(shcmd
, buf
, i
);
1016 shcmd
[shcmdi
= i
] = '\0';
1017 return *s
== '!' || *s
== '%';
1021 /* append_lines: insert text from stdin to after line n; stop when either a
1022 single period is read or EOF; return status */
1024 append_lines(long n
)
1031 for (current_addr
= n
;;) {
1033 if ((l
= get_tty_line()) < 0)
1035 else if (l
== 0 || ibuf
[l
- 1] != '\n') {
1040 } else if (*(lp
= ibufp
) == '\0')
1043 while (*ibufp
++ != '\n')
1047 if (l
== 2 && lp
[0] == '.' && lp
[1] == '\n') {
1053 if ((lp
= put_sbuf_line(lp
)) == NULL
) {
1057 up
->t
= get_addressed_line_node(current_addr
);
1058 else if ((up
= push_undo_stack(UADD
, current_addr
,
1059 current_addr
)) == NULL
) {
1063 } while (lp
!= eot
);
1071 /* join_lines: replace a range of lines with the joined text of those lines */
1073 join_lines(long from
, long to
)
1075 static char *buf
= NULL
;
1082 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1083 bp
= get_addressed_line_node(from
);
1084 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1085 if ((s
= get_sbuf_line(bp
)) == NULL
)
1087 REALLOC(buf
, n
, size
+ bp
->len
, ERR
);
1088 memcpy(buf
+ size
, s
, bp
->len
);
1091 REALLOC(buf
, n
, size
+ 2, ERR
);
1092 memcpy(buf
+ size
, "\n", 2);
1093 if (delete_lines(from
, to
) < 0)
1095 current_addr
= from
- 1;
1097 if (put_sbuf_line(buf
) == NULL
||
1098 push_undo_stack(UADD
, current_addr
, current_addr
) == NULL
) {
1108 /* move_lines: move a range of lines */
1110 move_lines(long addr
)
1112 line_t
*b1
, *a1
, *b2
, *a2
;
1113 long n
= INC_MOD(second_addr
, addr_last
);
1114 long p
= first_addr
- 1;
1115 int done
= (addr
== first_addr
- 1 || addr
== second_addr
);
1119 a2
= get_addressed_line_node(n
);
1120 b2
= get_addressed_line_node(p
);
1121 current_addr
= second_addr
;
1122 } else if (push_undo_stack(UMOV
, p
, n
) == NULL
||
1123 push_undo_stack(UMOV
, addr
, INC_MOD(addr
, addr_last
)) == NULL
) {
1127 a1
= get_addressed_line_node(n
);
1128 if (addr
< first_addr
) {
1129 b1
= get_addressed_line_node(p
);
1130 b2
= get_addressed_line_node(addr
);
1131 /* this get_addressed_line_node last! */
1133 b2
= get_addressed_line_node(addr
);
1134 b1
= get_addressed_line_node(p
);
1135 /* this get_addressed_line_node last! */
1138 REQUE(b2
, b1
->q_forw
);
1139 REQUE(a1
->q_back
, a2
);
1141 current_addr
= addr
+ ((addr
< first_addr
) ?
1142 second_addr
- first_addr
+ 1 : 0);
1145 unset_active_nodes(b2
->q_forw
, a2
);
1152 /* copy_lines: copy a range of lines; return status */
1154 copy_lines(long addr
)
1156 line_t
*lp
, *np
= get_addressed_line_node(first_addr
);
1158 long n
= second_addr
- first_addr
+ 1;
1161 current_addr
= addr
;
1162 if (first_addr
<= addr
&& addr
< second_addr
) {
1163 n
= addr
- first_addr
+ 1;
1164 m
= second_addr
- addr
;
1166 for (; n
> 0; n
=m
, m
=0, np
= get_addressed_line_node(current_addr
+ 1))
1167 for (; n
-- > 0; np
= np
->q_forw
) {
1169 if ((lp
= dup_line_node(np
)) == NULL
) {
1176 else if ((up
= push_undo_stack(UADD
, current_addr
,
1177 current_addr
)) == NULL
) {
1188 /* delete_lines: delete a range of lines */
1190 delete_lines(long from
, long to
)
1195 if (push_undo_stack(UDEL
, from
, to
) == NULL
) {
1199 n
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1200 p
= get_addressed_line_node(from
- 1);
1201 /* this get_addressed_line_node last! */
1203 unset_active_nodes(p
->q_forw
, n
);
1205 addr_last
-= to
- from
+ 1;
1206 current_addr
= from
- 1;
1213 /* display_lines: print a range of lines to stdout */
1215 display_lines(long from
, long to
, int gflag
)
1222 sprintf(errmsg
, "invalid address");
1225 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1226 bp
= get_addressed_line_node(from
);
1227 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1228 if ((s
= get_sbuf_line(bp
)) == NULL
)
1230 if (put_tty_line(s
, bp
->len
, current_addr
= from
++, gflag
) < 0)
1237 #define MAXMARK 26 /* max number of marks */
1239 line_t
*mark
[MAXMARK
]; /* line markers */
1240 int markno
; /* line marker count */
1242 /* mark_line_node: set a line node mark */
1244 mark_line_node(line_t
*lp
, int n
)
1246 if (!islower((unsigned char)n
)) {
1247 sprintf(errmsg
, "invalid mark character");
1249 } else if (mark
[n
- 'a'] == NULL
)
1256 /* get_marked_node_addr: return address of a marked line */
1258 get_marked_node_addr(int n
)
1260 if (!islower((unsigned char)n
)) {
1261 sprintf(errmsg
, "invalid mark character");
1264 return get_line_node_addr(mark
[n
- 'a']);
1268 /* unmark_line_node: clear line node mark */
1270 unmark_line_node(line_t
*lp
)
1274 for (i
= 0; markno
&& i
< MAXMARK
; i
++)
1275 if (mark
[i
] == lp
) {
1282 /* dup_line_node: return a pointer to a copy of a line node */
1284 dup_line_node(line_t
*lp
)
1288 if ((np
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
1289 fprintf(stderr
, "%s\n", strerror(errno
));
1290 sprintf(errmsg
, "out of memory");
1293 np
->seek
= lp
->seek
;
1299 /* has_trailing_escape: return the parity of escapes preceding a character
1302 has_trailing_escape(char *s
, char *t
)
1304 return (s
== t
|| *(t
- 1) != '\\') ? 0 : !has_trailing_escape(s
, t
- 1);
1308 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1310 strip_escapes(const char *s
)
1312 static char *file
= NULL
;
1313 static int filesz
= 0;
1317 REALLOC(file
, filesz
, PATH_MAX
, NULL
);
1318 while (i
< filesz
- 1 /* Worry about a possible trailing escape */
1319 && (file
[i
++] = (*s
== '\\') ? *++s
: *s
))
1326 signal_hup(int signo
)
1329 sigflags
|= (1 << (signo
- 1));
1336 signal_int(int signo
)
1339 sigflags
|= (1 << (signo
- 1));
1346 handle_hup(int signo
)
1348 char *hup
= NULL
; /* hup filename */
1354 sigflags
&= ~(1 << (signo
- 1));
1355 if (addr_last
&& write_file("ed.hup", "w", 1, addr_last
) < 0 &&
1356 (s
= getenv("HOME")) != NULL
&&
1357 (n
= strlen(s
)) + 8 <= PATH_MAX
&& /* "ed.hup" + '/' */
1358 (hup
= (char *) malloc(n
+ 10)) != NULL
) {
1360 if (hup
[n
- 1] != '/')
1361 hup
[n
] = '/', hup
[n
+1] = '\0';
1362 strcat(hup
, "ed.hup");
1363 write_file(hup
, "w", 1, addr_last
);
1370 handle_int(int signo
)
1374 sigflags
&= ~(1 << (signo
- 1));
1375 #ifdef _POSIX_SOURCE
1376 siglongjmp(env
, -1);
1383 int cols
= 72; /* wrap column */
1386 handle_winch(int signo
)
1388 int save_errno
= errno
;
1390 struct winsize ws
; /* window size structure */
1392 sigflags
&= ~(1 << (signo
- 1));
1393 if (ioctl(0, TIOCGWINSZ
, (char *) &ws
) >= 0) {
1394 if (ws
.ws_row
> 2) rows
= ws
.ws_row
- 2;
1395 if (ws
.ws_col
> 8) cols
= ws
.ws_col
- 8;
1401 /* is_legal_filename: return a legal filename */
1403 is_legal_filename(char *s
)
1405 if (red
&& (*s
== '!' || !strcmp(s
, "..") || strchr(s
, '/'))) {
1406 sprintf(errmsg
, "shell access restricted");