1 /* $NetBSD: main.c,v 1.28.8.2 2018/06/22 10:08:22 martin Exp $ */
3 /* main.c: This file contains the main control and user-interface routines
4 for the ed line editor. */
6 * Copyright (c) 1993 Andrew Moore, Talke Studio.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
34 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio.\
35 All rights reserved.");
40 static char *rcsid
= "@(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp";
42 __RCSID("$NetBSD: main.c,v 1.28.8.2 2018/06/22 10:08:22 martin Exp $");
49 * This program is based on the editor algorithm described in
50 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
51 * in Pascal," Addison-Wesley, 1981.
53 * The buffering algorithm is attributed to Rodney Ruddock of
54 * the University of Guelph, Guelph, Ontario.
56 * The cbc.c encryption code is adapted from
57 * the bdes program by Matt Bishop of Dartmouth College,
62 #include <sys/ioctl.h>
79 char stdinbuf
[1]; /* stdin buffer */
80 char *shcmd
; /* shell command buffer */
81 int shcmdsz
; /* shell command buffer size */
82 int shcmdi
; /* shell command buffer index */
83 char *ibuf
; /* ed command-line buffer */
84 int ibufsz
; /* ed command-line buffer size */
85 char *ibufp
; /* pointer to ed command-line buffer */
88 int des
= 0; /* if set, use crypt(3) for i/o */
89 int garrulous
= 0; /* if set, print all error messages */
90 int isbinary
; /* if set, buffer contains ASCII NULs */
91 int isglobal
; /* if set, doing a global command */
92 int modified
; /* if set, buffer modified since last write */
93 int mutex
= 0; /* if set, signals set "sigflags" */
94 int red
= 0; /* if set, restrict shell/directory access */
95 int ere
= 0; /* if set, use extended regexes */
96 int scripted
= 0; /* if set, suppress diagnostics */
97 int secure
= 0; /* is set, ! is not allowed */
98 int sigflags
= 0; /* if set, signals received while mutex set */
99 int sigactive
= 0; /* if set, signal handlers are enabled */
101 char old_filename
[MAXPATHLEN
+ 1] = ""; /* default filename */
102 long current_addr
; /* current address in editor buffer */
103 long addr_last
; /* last address in editor buffer */
104 int lineno
; /* script line number */
105 const char *prompt
; /* command-line prompt */
106 const char *dps
= "*"; /* default command-line prompt */
109 static const char usage
[] = "Usage: %s [-] [-ESsx] [-p string] [name]\n";
111 /* ed: line editor */
113 main(int ac
, char *av
[])
117 volatile int argc
= ac
;
118 char ** volatile argv
= av
;
120 red
= (n
= strlen(argv
[0])) > 2 && argv
[0][n
- 3] == 'r';
122 while ((c
= getopt(argc
, argv
, "p:sxES")) != -1)
124 case 'p': /* set prompt */
127 case 's': /* run script */
130 case 'x': /* use crypt */
134 fprintf(stderr
, "crypt unavailable\n?\n");
141 case 'S': /* ! is not allowed */
145 fprintf(stderr
, usage
, getprogname());
151 if (argc
&& **argv
== '-') {
160 /* assert: reliable signals! */
162 handle_winch(SIGWINCH
);
163 if (isatty(0)) signal(SIGWINCH
, handle_winch
);
165 signal(SIGHUP
, signal_hup
);
166 signal(SIGQUIT
, SIG_IGN
);
167 signal(SIGINT
, signal_int
);
169 if ((status
= sigsetjmp(env
, 1)) != 0)
171 if ((status
= setjmp(env
)) != 0)
174 fputs("\n?\n", stderr
);
175 seterrmsg("interrupt");
178 sigactive
= 1; /* enable signal handlers */
179 if (argc
&& **argv
&& is_legal_filename(*argv
)) {
180 if (read_file(*argv
, 0) < 0 && !isatty(0))
182 else if (**argv
!= '!')
183 strlcpy(old_filename
, *argv
,
184 sizeof(old_filename
) - 2);
186 fputs("?\n", stderr
);
188 seterrmsg("invalid filename");
194 if (status
< 0 && garrulous
)
195 fprintf(stderr
, "%s\n", errmsg
);
197 printf("%s", prompt
);
200 if ((n
= get_tty_line()) < 0) {
204 if (modified
&& !scripted
) {
205 fputs("?\n", stderr
);
206 seterrmsg("warning: file modified");
210 "script, line %d: %s\n",
221 } else if (ibuf
[n
- 1] != '\n') {
223 seterrmsg("unexpected end-of-file");
229 if ((status
= extract_addr_range()) >= 0 &&
230 (status
= exec_command()) >= 0) {
233 status
= display_lines(current_addr
, current_addr
,
243 fputs("?\n", stderr
); /* give warning */
244 seterrmsg("warning: file modified");
248 "script, line %d: %s\n",
258 "script, line %d: %s\n",
261 fprintf(stderr
, "%s\n", errmsg
);
266 fputs("?\n", stderr
);
269 fprintf(stderr
, "script, line %d: %s\n",
280 long first_addr
, second_addr
, addr_cnt
;
282 /* extract_addr_range: get line addresses from the command buffer until an
283 illegal address is seen; return status */
285 extract_addr_range(void)
290 first_addr
= second_addr
= current_addr
;
291 while ((addr
= next_addr()) >= 0) {
293 first_addr
= second_addr
;
295 if (*ibufp
!= ',' && *ibufp
!= ';')
297 else if (*ibufp
++ == ';')
300 if ((addr_cnt
= min(addr_cnt
, 2)) == 1 || second_addr
!= addr
)
301 first_addr
= second_addr
;
302 return (addr
== ERR
) ? ERR
: 0;
306 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') \
309 #define MUST_BE_FIRST() \
310 if (!first) { seterrmsg("invalid address"); return ERR; }
312 /* next_addr: return the next line address in the command buffer */
317 long addr
= current_addr
;
323 for (hd
= ibufp
;; first
= 0)
324 switch (c
= *ibufp
) {
332 if (isdigit((unsigned char)*ibufp
)) {
334 addr
+= (c
== '-' || c
== '^') ? -n
: n
;
335 } else if (!isspace((unsigned char)c
))
336 addr
+= (c
== '-' || c
== '^') ? -1 : 1;
338 case '0': case '1': case '2':
339 case '3': case '4': case '5':
340 case '6': case '7': case '8': case '9':
348 addr
= (c
== '.') ? current_addr
: addr_last
;
353 if ((addr
= get_matching_node_addr(
354 get_compiled_pattern(), c
== '/')) < 0)
356 else if (c
== *ibufp
)
362 if ((addr
= get_marked_node_addr((unsigned char)*ibufp
++)) < 0)
371 second_addr
= (c
== ';') ? current_addr
: 1;
379 else if (addr
< 0 || addr_last
< addr
) {
380 seterrmsg("invalid address");
390 /* GET_THIRD_ADDR: get a legal address from the command buffer */
391 #define GET_THIRD_ADDR(addr) \
395 ol1 = first_addr, ol2 = second_addr; \
396 if (extract_addr_range() < 0) \
398 else if (addr_cnt == 0) { \
399 seterrmsg("destination expected"); \
401 } else if (second_addr < 0 || addr_last < second_addr) { \
402 seterrmsg("invalid address"); \
405 addr = second_addr; \
406 first_addr = ol1, second_addr = ol2; \
408 #else /* BACKWARDS */
409 /* GET_THIRD_ADDR: get a legal address from the command buffer */
410 #define GET_THIRD_ADDR(addr) \
414 ol1 = first_addr, ol2 = second_addr; \
415 if (extract_addr_range() < 0) \
417 if (second_addr < 0 || addr_last < second_addr) { \
418 seterrmsg("invalid address"); \
421 addr = second_addr; \
422 first_addr = ol1, second_addr = ol2; \
427 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
428 #define GET_COMMAND_SUFFIX() { \
433 gflag |= GPR, ibufp++; \
436 gflag |= GLS, ibufp++; \
439 gflag |= GNP, ibufp++; \
445 if (*ibufp++ != '\n') { \
446 seterrmsg("invalid command suffix"); \
453 #define SGG 001 /* complement previous global substitute suffix */
454 #define SGP 002 /* complement previous print suffix */
455 #define SGR 004 /* use last regex instead of last pat */
456 #define SGF 010 /* repeat last substitution */
458 int patlock
= 0; /* if set, pattern not freed by get_compiled_pattern() */
460 long rows
= 22; /* scroll length: ws_row - 2 */
462 /* exec_command: execute the next command in command buffer; return print
467 static pattern_t
*pat
= NULL
;
468 static int sgflag
= 0;
469 static long sgnum
= 0;
480 switch(c
= *ibufp
++) {
482 GET_COMMAND_SUFFIX();
483 if (!isglobal
) clear_undo_stack();
484 if (append_lines(second_addr
) < 0)
488 if (check_addr_range(current_addr
, current_addr
) < 0)
490 GET_COMMAND_SUFFIX();
491 if (!isglobal
) clear_undo_stack();
492 if (delete_lines(first_addr
, second_addr
) < 0 ||
493 append_lines(current_addr
) < 0)
497 if (check_addr_range(current_addr
, current_addr
) < 0)
499 GET_COMMAND_SUFFIX();
500 if (!isglobal
) clear_undo_stack();
501 if (delete_lines(first_addr
, second_addr
) < 0)
503 else if ((addr
= INC_MOD(current_addr
, addr_last
)) != 0)
507 if (modified
&& !scripted
)
512 seterrmsg("unexpected address");
514 } else if (!isspace((unsigned char)*ibufp
)) {
515 seterrmsg("unexpected command suffix");
517 } else if ((fnp
= get_filename()) == NULL
)
519 GET_COMMAND_SUFFIX();
520 if (delete_lines(1, addr_last
) < 0)
523 if (close_sbuf() < 0)
525 else if (open_sbuf() < 0)
527 if (*fnp
&& *fnp
!= '!') strlcpy(old_filename
, fnp
,
528 sizeof(old_filename
) - 2);
530 if (*fnp
== '\0' && *old_filename
== '\0') {
531 seterrmsg("no current filename");
535 if (read_file(*fnp
? fnp
: old_filename
, 0) < 0)
539 u_current_addr
= u_addr_last
= -1;
543 seterrmsg("unexpected address");
545 } else if (!isspace((unsigned char)*ibufp
)) {
546 seterrmsg("unexpected command suffix");
548 } else if ((fnp
= get_filename()) == NULL
)
550 else if (*fnp
== '!') {
551 seterrmsg("invalid redirection");
554 GET_COMMAND_SUFFIX();
555 if (*fnp
) strlcpy(old_filename
, fnp
, sizeof(old_filename
) - 2);
556 printf("%s\n", strip_escapes(old_filename
));
563 seterrmsg("cannot nest global commands");
565 } else if (check_addr_range(1, addr_last
) < 0)
567 else if (build_active_list(c
== 'g' || c
== 'G') < 0)
569 else if ((n
= (c
== 'G' || c
== 'V')) != 0)
570 GET_COMMAND_SUFFIX();
572 if (exec_global(n
, gflag
) < 0)
577 seterrmsg("unexpected address");
580 GET_COMMAND_SUFFIX();
581 if (*errmsg
) fprintf(stderr
, "%s\n", errmsg
);
585 seterrmsg("unexpected address");
588 GET_COMMAND_SUFFIX();
589 if ((garrulous
= 1 - garrulous
) && *errmsg
)
590 fprintf(stderr
, "%s\n", errmsg
);
593 if (second_addr
== 0) {
594 seterrmsg("invalid address");
597 GET_COMMAND_SUFFIX();
598 if (!isglobal
) clear_undo_stack();
599 if (append_lines(second_addr
- 1) < 0)
603 if (check_addr_range(current_addr
, current_addr
+ 1) < 0)
605 GET_COMMAND_SUFFIX();
606 if (!isglobal
) clear_undo_stack();
607 if (first_addr
!= second_addr
&&
608 join_lines(first_addr
, second_addr
) < 0)
613 if (second_addr
== 0) {
614 seterrmsg("invalid address");
617 GET_COMMAND_SUFFIX();
618 if (mark_line_node(get_addressed_line_node(second_addr
), (unsigned char)c
) < 0)
622 if (check_addr_range(current_addr
, current_addr
) < 0)
624 GET_COMMAND_SUFFIX();
625 if (display_lines(first_addr
, second_addr
, gflag
| GLS
) < 0)
630 if (check_addr_range(current_addr
, current_addr
) < 0)
632 GET_THIRD_ADDR(addr
);
633 if (first_addr
<= addr
&& addr
< second_addr
) {
634 seterrmsg("invalid destination");
637 GET_COMMAND_SUFFIX();
638 if (!isglobal
) clear_undo_stack();
639 if (move_lines(addr
) < 0)
643 if (check_addr_range(current_addr
, current_addr
) < 0)
645 GET_COMMAND_SUFFIX();
646 if (display_lines(first_addr
, second_addr
, gflag
| GNP
) < 0)
651 if (check_addr_range(current_addr
, current_addr
) < 0)
653 GET_COMMAND_SUFFIX();
654 if (display_lines(first_addr
, second_addr
, gflag
| GPR
) < 0)
660 seterrmsg("unexpected address");
663 GET_COMMAND_SUFFIX();
664 prompt
= prompt
? NULL
: optarg
? optarg
: dps
;
669 seterrmsg("unexpected address");
672 GET_COMMAND_SUFFIX();
673 gflag
= (modified
&& !scripted
&& c
== 'q') ? EMOD
: EOF
;
676 if (!isspace((unsigned char)*ibufp
)) {
677 seterrmsg("unexpected command suffix");
679 } else if (addr_cnt
== 0)
680 second_addr
= addr_last
;
681 if ((fnp
= get_filename()) == NULL
)
683 GET_COMMAND_SUFFIX();
684 if (!isglobal
) clear_undo_stack();
685 if (*old_filename
== '\0' && *fnp
!= '!')
686 strlcpy(old_filename
, fnp
, sizeof(old_filename
) - 2);
688 if (*fnp
== '\0' && *old_filename
== '\0') {
689 seterrmsg("no current filename");
693 if ((addr
= read_file(*fnp
? fnp
: old_filename
, second_addr
)) < 0)
695 else if (addr
&& addr
!= addr_last
)
716 case '0': case '1': case '2': case '3': case '4':
717 case '5': case '6': case '7': case '8': case '9':
718 STRTOL(sgnum
, ibufp
);
720 sgflag
&= ~GSG
; /* override GSG */
724 seterrmsg("invalid command suffix");
728 } while (sflags
&& *ibufp
!= '\n');
729 if (sflags
&& !pat
) {
730 seterrmsg("no previous substitution");
732 } else if (sflags
& SGG
)
733 sgnum
= 0; /* override numeric arg */
734 if (*ibufp
!= '\n' && *(ibufp
+ 1) == '\n') {
735 seterrmsg("invalid pattern delimiter");
740 if ((!sflags
|| (sflags
& SGR
)) &&
741 (tpat
= get_compiled_pattern()) == NULL
) {
744 } else if (tpat
!= pat
) {
750 patlock
= 1; /* reserve pattern */
753 if (!sflags
&& extract_subst_tail(&sgflag
, &sgnum
) < 0)
762 sgflag
^= GPR
, sgflag
&= ~(GLS
| GNP
);
766 sgflag
|= GPR
, ibufp
++;
769 sgflag
|= GLS
, ibufp
++;
772 sgflag
|= GNP
, ibufp
++;
778 if (check_addr_range(current_addr
, current_addr
) < 0)
780 GET_COMMAND_SUFFIX();
781 if (!isglobal
) clear_undo_stack();
782 if (search_and_replace(pat
, sgflag
, sgnum
) < 0)
786 if (check_addr_range(current_addr
, current_addr
) < 0)
788 GET_THIRD_ADDR(addr
);
789 GET_COMMAND_SUFFIX();
790 if (!isglobal
) clear_undo_stack();
791 if (copy_lines(addr
) < 0)
796 seterrmsg("unexpected address");
799 GET_COMMAND_SUFFIX();
800 if (pop_undo_stack() < 0)
805 if ((n
= *ibufp
) == 'q' || n
== 'Q') {
809 if (!isspace((unsigned char)*ibufp
)) {
810 seterrmsg("unexpected command suffix");
812 } else if ((fnp
= get_filename()) == NULL
)
814 if (addr_cnt
== 0 && !addr_last
)
815 first_addr
= second_addr
= 0;
816 else if (check_addr_range(1, addr_last
) < 0)
818 GET_COMMAND_SUFFIX();
819 if (*old_filename
== '\0' && *fnp
!= '!')
820 strlcpy(old_filename
, fnp
, sizeof(old_filename
) - 2);
822 if (*fnp
== '\0' && *old_filename
== '\0') {
823 seterrmsg("no current filename");
827 if ((addr
= write_file(*fnp
? fnp
: old_filename
,
828 (c
== 'W') ? "a" : "w", first_addr
, second_addr
)) < 0)
830 else if (addr
== addr_last
)
832 else if (modified
&& !scripted
&& n
== 'q')
837 seterrmsg("unexpected address");
840 GET_COMMAND_SUFFIX();
844 seterrmsg("crypt unavailable");
850 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0)
852 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0)
855 else if ('0' < *ibufp
&& *ibufp
<= '9')
857 GET_COMMAND_SUFFIX();
858 if (display_lines(second_addr
, min(addr_last
,
859 second_addr
+ rows
), gflag
) < 0)
864 GET_COMMAND_SUFFIX();
865 printf("%ld\n", addr_cnt
? second_addr
: addr_last
);
869 seterrmsg("unexpected address");
872 if ((sflags
= get_shell_command()) < 0)
874 GET_COMMAND_SUFFIX();
875 if (sflags
) printf("%s\n", shcmd
+ 1);
877 if (!scripted
) printf("!\n");
881 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0
883 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0
885 || display_lines(second_addr
, second_addr
, 0) < 0)
889 seterrmsg("unknown command");
896 /* check_addr_range: return status of address range check */
898 check_addr_range(long n
, long m
)
904 if (first_addr
> second_addr
|| 1 > first_addr
||
905 second_addr
> addr_last
) {
906 seterrmsg("invalid address");
913 /* get_matching_node_addr: return the address of the next line matching a
914 pattern in a given direction. wrap around begin/end of editor buffer if
917 get_matching_node_addr(pattern_t
*pat
, int dir
)
920 long n
= current_addr
;
923 if (!pat
) return ERR
;
925 if ((n
= dir
? INC_MOD(n
, addr_last
) :
926 DEC_MOD(n
, addr_last
)) != 0) {
927 lp
= get_addressed_line_node(n
);
928 if ((s
= get_sbuf_line(lp
)) == NULL
)
931 NUL_TO_NEWLINE(s
, lp
->len
);
932 if (!regexec(pat
, s
, 0, NULL
, 0))
935 } while (n
!= current_addr
);
936 seterrmsg("no match");
941 /* get_filename: return pointer to copy of filename in the command buffer */
945 static char *file
= NULL
;
946 static int filesz
= 0;
950 if (*ibufp
!= '\n') {
952 if (*ibufp
== '\n') {
953 seterrmsg("invalid filename");
955 } else if ((ibufp
= get_extended_line(&n
, 1)) == NULL
)
957 else if (*ibufp
== '!') {
959 if ((n
= get_shell_command()) < 0)
961 if (n
) printf("%s\n", shcmd
+ 1);
963 } else if (n
- 1 > MAXPATHLEN
) {
964 seterrmsg("filename too long");
969 else if (*old_filename
== '\0') {
970 seterrmsg("no current filename");
974 REALLOC(file
, filesz
, MAXPATHLEN
+ 1, NULL
);
975 for (n
= 0; *ibufp
!= '\n';)
976 file
[n
++] = *ibufp
++;
978 return is_legal_filename(file
) ? file
: NULL
;
982 /* get_shell_command: read a shell command from stdin; return substitution
985 get_shell_command(void)
987 static char *buf
= NULL
;
990 char *s
; /* substitution char pointer */
995 seterrmsg("shell access restricted");
997 } else if ((s
= ibufp
= get_extended_line(&j
, 1)) == NULL
)
999 REALLOC(buf
, n
, j
+ 1, ERR
);
1000 buf
[i
++] = '!'; /* prefix command w/ bang */
1001 while (*ibufp
!= '\n')
1004 REALLOC(buf
, n
, i
+ 2, ERR
);
1006 if (*ibufp
++ == '\\')
1007 buf
[i
++] = *ibufp
++;
1011 REALLOC(buf
, n
, i
+ 1, ERR
);
1012 buf
[i
++] = *ibufp
++;
1015 else if (shcmd
== NULL
|| *(shcmd
+ 1) == '\0')
1017 else if (shcmd
== NULL
)
1020 seterrmsg("no previous command");
1023 REALLOC(buf
, n
, i
+ shcmdi
, ERR
);
1024 for (s
= shcmd
+ 1; s
< shcmd
+ shcmdi
;)
1030 if (*old_filename
== '\0') {
1031 seterrmsg("no current filename");
1034 j
= strlen(s
= strip_escapes(old_filename
));
1035 REALLOC(buf
, n
, i
+ j
, ERR
);
1041 REALLOC(shcmd
, shcmdsz
, i
+ 1, ERR
);
1042 memcpy(shcmd
, buf
, i
);
1043 shcmd
[shcmdi
= i
] = '\0';
1044 return *s
== '!' || *s
== '%';
1048 /* append_lines: insert text from stdin to after line n; stop when either a
1049 single period is read or EOF; return status */
1051 append_lines(long n
)
1058 for (current_addr
= n
;;) {
1060 if ((l
= get_tty_line()) < 0)
1062 else if (l
== 0 || ibuf
[l
- 1] != '\n') {
1067 } else if (*(lp
= ibufp
) == '\0')
1070 while (*ibufp
++ != '\n')
1074 if (l
== 2 && lp
[0] == '.' && lp
[1] == '\n') {
1080 if ((lp
= put_sbuf_line(lp
)) == NULL
) {
1084 up
->t
= get_addressed_line_node(current_addr
);
1085 else if ((up
= push_undo_stack(UADD
, current_addr
,
1086 current_addr
)) == NULL
) {
1090 } while (lp
!= eot
);
1098 /* join_lines: replace a range of lines with the joined text of those lines */
1100 join_lines(long from
, long to
)
1102 static char *buf
= NULL
;
1109 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1110 bp
= get_addressed_line_node(from
);
1111 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1112 if ((s
= get_sbuf_line(bp
)) == NULL
)
1114 REALLOC(buf
, n
, size
+ bp
->len
, ERR
);
1115 memcpy(buf
+ size
, s
, bp
->len
);
1118 REALLOC(buf
, n
, size
+ 2, ERR
);
1119 memcpy(buf
+ size
, "\n", 2);
1120 if (delete_lines(from
, to
) < 0)
1122 current_addr
= from
- 1;
1124 if (put_sbuf_line(buf
) == NULL
||
1125 push_undo_stack(UADD
, current_addr
, current_addr
) == NULL
) {
1135 /* move_lines: move a range of lines */
1137 move_lines(long addr
)
1139 line_t
*b1
, *a1
, *b2
, *a2
;
1140 long n
= INC_MOD(second_addr
, addr_last
);
1141 long p
= first_addr
- 1;
1142 int done
= (addr
== first_addr
- 1 || addr
== second_addr
);
1146 a2
= get_addressed_line_node(n
);
1147 b2
= get_addressed_line_node(p
);
1148 current_addr
= second_addr
;
1149 } else if (push_undo_stack(UMOV
, p
, n
) == NULL
||
1150 push_undo_stack(UMOV
, addr
, INC_MOD(addr
, addr_last
)) == NULL
) {
1154 a1
= get_addressed_line_node(n
);
1155 if (addr
< first_addr
) {
1156 b1
= get_addressed_line_node(p
);
1157 b2
= get_addressed_line_node(addr
);
1158 /* this get_addressed_line_node last! */
1160 b2
= get_addressed_line_node(addr
);
1161 b1
= get_addressed_line_node(p
);
1162 /* this get_addressed_line_node last! */
1165 REQUE(b2
, b1
->q_forw
);
1166 REQUE(a1
->q_back
, a2
);
1168 current_addr
= addr
+ ((addr
< first_addr
) ?
1169 second_addr
- first_addr
+ 1 : 0);
1172 unset_active_nodes(b2
->q_forw
, a2
);
1179 /* copy_lines: copy a range of lines; return status */
1181 copy_lines(long addr
)
1183 line_t
*lp
, *np
= get_addressed_line_node(first_addr
);
1185 long n
= second_addr
- first_addr
+ 1;
1188 current_addr
= addr
;
1189 if (first_addr
<= addr
&& addr
< second_addr
) {
1190 n
= addr
- first_addr
+ 1;
1191 m
= second_addr
- addr
;
1193 for (; n
> 0; n
=m
, m
=0, np
= get_addressed_line_node(current_addr
+ 1))
1194 for (; n
-- > 0; np
= np
->q_forw
) {
1196 if ((lp
= dup_line_node(np
)) == NULL
) {
1203 else if ((up
= push_undo_stack(UADD
, current_addr
,
1204 current_addr
)) == NULL
) {
1215 /* delete_lines: delete a range of lines */
1217 delete_lines(long from
, long to
)
1222 if (push_undo_stack(UDEL
, from
, to
) == NULL
) {
1226 n
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1227 p
= get_addressed_line_node(from
- 1);
1228 /* this get_addressed_line_node last! */
1230 unset_active_nodes(p
->q_forw
, n
);
1232 addr_last
-= to
- from
+ 1;
1233 current_addr
= from
- 1;
1240 /* display_lines: print a range of lines to stdout */
1242 display_lines(long from
, long to
, int gflag
)
1249 seterrmsg("invalid address");
1252 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1253 bp
= get_addressed_line_node(from
);
1254 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1255 if ((s
= get_sbuf_line(bp
)) == NULL
)
1257 if (put_tty_line(s
, bp
->len
, current_addr
= from
++, gflag
) < 0)
1264 #define MAXMARK 26 /* max number of marks */
1266 line_t
*mark
[MAXMARK
]; /* line markers */
1267 int markno
; /* line marker count */
1269 /* mark_line_node: set a line node mark */
1271 mark_line_node(line_t
*lp
, int n
)
1274 seterrmsg("invalid mark character");
1276 } else if (mark
[n
- 'a'] == NULL
)
1283 /* get_marked_node_addr: return address of a marked line */
1285 get_marked_node_addr(int n
)
1288 seterrmsg("invalid mark character");
1291 return get_line_node_addr(mark
[n
- 'a']);
1295 /* unmark_line_node: clear line node mark */
1297 unmark_line_node(line_t
*lp
)
1301 for (i
= 0; markno
&& i
< MAXMARK
; i
++)
1302 if (mark
[i
] == lp
) {
1309 /* dup_line_node: return a pointer to a copy of a line node */
1311 dup_line_node(line_t
*lp
)
1315 if ((np
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
1316 fprintf(stderr
, "%s\n", strerror(errno
));
1317 seterrmsg("out of memory");
1320 np
->seek
= lp
->seek
;
1326 /* has_trailing_escape: return the parity of escapes preceding a character
1329 has_trailing_escape(char *s
, char *t
)
1331 return (s
== t
|| *(t
- 1) != '\\') ? 0 : !has_trailing_escape(s
, t
- 1);
1335 /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1337 strip_escapes(const char *s
)
1339 static char *file
= NULL
;
1340 static int filesz
= 0;
1344 REALLOC(file
, filesz
, MAXPATHLEN
+ 1, NULL
);
1345 while ((i
< (filesz
- 1)) &&
1346 (file
[i
++] = (*s
== '\\') != '\0' ? *++s
: *s
))
1348 file
[filesz
- 1] = '\0';
1354 signal_hup(int signo
)
1357 sigflags
|= (1 << (signo
- 1));
1358 else handle_hup(signo
);
1363 signal_int(int signo
)
1366 sigflags
|= (1 << (signo
- 1));
1367 else handle_int(signo
);
1372 handle_hup(int signo
)
1374 char *hup
= NULL
; /* hup filename */
1380 sigflags
&= ~(1 << (signo
- 1));
1381 if (addr_last
&& write_file("ed.hup", "w", 1, addr_last
) < 0 &&
1382 (s
= getenv("HOME")) != NULL
&&
1383 (n
= strlen(s
)) + 8 <= MAXPATHLEN
&& /* "ed.hup" + '/' */
1384 (hup
= (char *) malloc(n
+ 10)) != NULL
) {
1386 if (hup
[n
- 1] != '/')
1387 hup
[n
] = '/', hup
[n
+1] = '\0';
1388 strcat(hup
, "ed.hup");
1389 write_file(hup
, "w", 1, addr_last
);
1396 handle_int(int signo
)
1400 sigflags
&= ~(1 << (signo
- 1));
1401 #ifdef _POSIX_SOURCE
1402 siglongjmp(env
, -1);
1409 int cols
= 72; /* wrap column */
1412 handle_winch(int signo
)
1414 struct winsize ws
; /* window size structure */
1416 sigflags
&= ~(1 << (signo
- 1));
1417 if (ioctl(0, TIOCGWINSZ
, (char *) &ws
) >= 0) {
1418 if (ws
.ws_row
> 2) rows
= ws
.ws_row
- 2;
1419 if (ws
.ws_col
> 8) cols
= ws
.ws_col
- 8;
1424 /* is_legal_filename: return a legal filename */
1426 is_legal_filename(char *s
)
1428 if (red
&& (*s
== '!' || !strcmp(s
, "..") || strchr(s
, '/'))) {
1429 seterrmsg("shell access restricted");