1 /* $NetBSD: main.c,v 1.27 2014/03/31 12:55:46 christos 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.27 2014/03/31 12:55:46 christos 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 sigflags
= 0; /* if set, signals received while mutex set */
98 int sigactive
= 0; /* if set, signal handlers are enabled */
100 char old_filename
[MAXPATHLEN
+ 1] = ""; /* default filename */
101 long current_addr
; /* current address in editor buffer */
102 long addr_last
; /* last address in editor buffer */
103 int lineno
; /* script line number */
104 const char *prompt
; /* command-line prompt */
105 const char *dps
= "*"; /* default command-line prompt */
108 static const char usage
[] = "Usage: %s [-] [-sxE] [-p string] [name]\n";
110 /* ed: line editor */
112 main(int ac
, char *av
[])
116 volatile int argc
= ac
;
117 char ** volatile argv
= av
;
119 red
= (n
= strlen(argv
[0])) > 2 && argv
[0][n
- 3] == 'r';
121 while ((c
= getopt(argc
, argv
, "p:sxE")) != -1)
123 case 'p': /* set prompt */
126 case 's': /* run script */
129 case 'x': /* use crypt */
133 fprintf(stderr
, "crypt unavailable\n?\n");
141 fprintf(stderr
, usage
, getprogname());
147 if (argc
&& **argv
== '-') {
156 /* assert: reliable signals! */
158 handle_winch(SIGWINCH
);
159 if (isatty(0)) signal(SIGWINCH
, handle_winch
);
161 signal(SIGHUP
, signal_hup
);
162 signal(SIGQUIT
, SIG_IGN
);
163 signal(SIGINT
, signal_int
);
165 if ((status
= sigsetjmp(env
, 1)) != 0)
167 if ((status
= setjmp(env
)) != 0)
170 fputs("\n?\n", stderr
);
171 seterrmsg("interrupt");
174 sigactive
= 1; /* enable signal handlers */
175 if (argc
&& **argv
&& is_legal_filename(*argv
)) {
176 if (read_file(*argv
, 0) < 0 && !isatty(0))
178 else if (**argv
!= '!')
179 strlcpy(old_filename
, *argv
,
180 sizeof(old_filename
) - 2);
182 fputs("?\n", stderr
);
184 seterrmsg("invalid filename");
190 if (status
< 0 && garrulous
)
191 fprintf(stderr
, "%s\n", errmsg
);
193 printf("%s", prompt
);
196 if ((n
= get_tty_line()) < 0) {
200 if (modified
&& !scripted
) {
201 fputs("?\n", stderr
);
202 seterrmsg("warning: file modified");
206 "script, line %d: %s\n",
217 } else if (ibuf
[n
- 1] != '\n') {
219 seterrmsg("unexpected end-of-file");
225 if ((status
= extract_addr_range()) >= 0 &&
226 (status
= exec_command()) >= 0)
227 if (!status
|| (status
&&
228 (status
= display_lines(current_addr
, current_addr
,
236 fputs("?\n", stderr
); /* give warning */
237 seterrmsg("warning: file modified");
241 "script, line %d: %s\n",
251 "script, line %d: %s\n",
254 fprintf(stderr
, "%s\n", errmsg
);
259 fputs("?\n", stderr
);
262 fprintf(stderr
, "script, line %d: %s\n",
273 long first_addr
, second_addr
, addr_cnt
;
275 /* extract_addr_range: get line addresses from the command buffer until an
276 illegal address is seen; return status */
278 extract_addr_range(void)
283 first_addr
= second_addr
= current_addr
;
284 while ((addr
= next_addr()) >= 0) {
286 first_addr
= second_addr
;
288 if (*ibufp
!= ',' && *ibufp
!= ';')
290 else if (*ibufp
++ == ';')
293 if ((addr_cnt
= min(addr_cnt
, 2)) == 1 || second_addr
!= addr
)
294 first_addr
= second_addr
;
295 return (addr
== ERR
) ? ERR
: 0;
299 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') \
302 #define MUST_BE_FIRST() \
303 if (!first) { seterrmsg("invalid address"); return ERR; }
305 /* next_addr: return the next line address in the command buffer */
310 long addr
= current_addr
;
316 for (hd
= ibufp
;; first
= 0)
317 switch (c
= *ibufp
) {
325 if (isdigit((unsigned char)*ibufp
)) {
327 addr
+= (c
== '-' || c
== '^') ? -n
: n
;
328 } else if (!isspace((unsigned char)c
))
329 addr
+= (c
== '-' || c
== '^') ? -1 : 1;
331 case '0': case '1': case '2':
332 case '3': case '4': case '5':
333 case '6': case '7': case '8': case '9':
341 addr
= (c
== '.') ? current_addr
: addr_last
;
346 if ((addr
= get_matching_node_addr(
347 get_compiled_pattern(), c
== '/')) < 0)
349 else if (c
== *ibufp
)
355 if ((addr
= get_marked_node_addr((unsigned char)*ibufp
++)) < 0)
364 second_addr
= (c
== ';') ? current_addr
: 1;
372 else if (addr
< 0 || addr_last
< addr
) {
373 seterrmsg("invalid address");
383 /* GET_THIRD_ADDR: get a legal address from the command buffer */
384 #define GET_THIRD_ADDR(addr) \
388 ol1 = first_addr, ol2 = second_addr; \
389 if (extract_addr_range() < 0) \
391 else if (addr_cnt == 0) { \
392 seterrmsg("destination expected"); \
394 } else if (second_addr < 0 || addr_last < second_addr) { \
395 seterrmsg("invalid address"); \
398 addr = second_addr; \
399 first_addr = ol1, second_addr = ol2; \
401 #else /* BACKWARDS */
402 /* GET_THIRD_ADDR: get a legal address from the command buffer */
403 #define GET_THIRD_ADDR(addr) \
407 ol1 = first_addr, ol2 = second_addr; \
408 if (extract_addr_range() < 0) \
410 if (second_addr < 0 || addr_last < second_addr) { \
411 seterrmsg("invalid address"); \
414 addr = second_addr; \
415 first_addr = ol1, second_addr = ol2; \
420 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
421 #define GET_COMMAND_SUFFIX() { \
426 gflag |= GPR, ibufp++; \
429 gflag |= GLS, ibufp++; \
432 gflag |= GNP, ibufp++; \
438 if (*ibufp++ != '\n') { \
439 seterrmsg("invalid command suffix"); \
446 #define SGG 001 /* complement previous global substitute suffix */
447 #define SGP 002 /* complement previous print suffix */
448 #define SGR 004 /* use last regex instead of last pat */
449 #define SGF 010 /* repeat last substitution */
451 int patlock
= 0; /* if set, pattern not freed by get_compiled_pattern() */
453 long rows
= 22; /* scroll length: ws_row - 2 */
455 /* exec_command: execute the next command in command buffer; return print
460 static pattern_t
*pat
= NULL
;
461 static int sgflag
= 0;
462 static long sgnum
= 0;
473 switch(c
= *ibufp
++) {
475 GET_COMMAND_SUFFIX();
476 if (!isglobal
) clear_undo_stack();
477 if (append_lines(second_addr
) < 0)
481 if (check_addr_range(current_addr
, current_addr
) < 0)
483 GET_COMMAND_SUFFIX();
484 if (!isglobal
) clear_undo_stack();
485 if (delete_lines(first_addr
, second_addr
) < 0 ||
486 append_lines(current_addr
) < 0)
490 if (check_addr_range(current_addr
, current_addr
) < 0)
492 GET_COMMAND_SUFFIX();
493 if (!isglobal
) clear_undo_stack();
494 if (delete_lines(first_addr
, second_addr
) < 0)
496 else if ((addr
= INC_MOD(current_addr
, addr_last
)) != 0)
500 if (modified
&& !scripted
)
505 seterrmsg("unexpected address");
507 } else if (!isspace((unsigned char)*ibufp
)) {
508 seterrmsg("unexpected command suffix");
510 } else if ((fnp
= get_filename()) == NULL
)
512 GET_COMMAND_SUFFIX();
513 if (delete_lines(1, addr_last
) < 0)
516 if (close_sbuf() < 0)
518 else if (open_sbuf() < 0)
520 if (*fnp
&& *fnp
!= '!') strlcpy(old_filename
, fnp
,
521 sizeof(old_filename
) - 2);
523 if (*fnp
== '\0' && *old_filename
== '\0') {
524 seterrmsg("no current filename");
528 if (read_file(*fnp
? fnp
: old_filename
, 0) < 0)
532 u_current_addr
= u_addr_last
= -1;
536 seterrmsg("unexpected address");
538 } else if (!isspace((unsigned char)*ibufp
)) {
539 seterrmsg("unexpected command suffix");
541 } else if ((fnp
= get_filename()) == NULL
)
543 else if (*fnp
== '!') {
544 seterrmsg("invalid redirection");
547 GET_COMMAND_SUFFIX();
548 if (*fnp
) strlcpy(old_filename
, fnp
, sizeof(old_filename
) - 2);
549 printf("%s\n", strip_escapes(old_filename
));
556 seterrmsg("cannot nest global commands");
558 } else if (check_addr_range(1, addr_last
) < 0)
560 else if (build_active_list(c
== 'g' || c
== 'G') < 0)
562 else if ((n
= (c
== 'G' || c
== 'V')) != 0)
563 GET_COMMAND_SUFFIX();
565 if (exec_global(n
, gflag
) < 0)
570 seterrmsg("unexpected address");
573 GET_COMMAND_SUFFIX();
574 if (*errmsg
) fprintf(stderr
, "%s\n", errmsg
);
578 seterrmsg("unexpected address");
581 GET_COMMAND_SUFFIX();
582 if ((garrulous
= 1 - garrulous
) && *errmsg
)
583 fprintf(stderr
, "%s\n", errmsg
);
586 if (second_addr
== 0) {
587 seterrmsg("invalid address");
590 GET_COMMAND_SUFFIX();
591 if (!isglobal
) clear_undo_stack();
592 if (append_lines(second_addr
- 1) < 0)
596 if (check_addr_range(current_addr
, current_addr
+ 1) < 0)
598 GET_COMMAND_SUFFIX();
599 if (!isglobal
) clear_undo_stack();
600 if (first_addr
!= second_addr
&&
601 join_lines(first_addr
, second_addr
) < 0)
606 if (second_addr
== 0) {
607 seterrmsg("invalid address");
610 GET_COMMAND_SUFFIX();
611 if (mark_line_node(get_addressed_line_node(second_addr
), (unsigned char)c
) < 0)
615 if (check_addr_range(current_addr
, current_addr
) < 0)
617 GET_COMMAND_SUFFIX();
618 if (display_lines(first_addr
, second_addr
, gflag
| GLS
) < 0)
623 if (check_addr_range(current_addr
, current_addr
) < 0)
625 GET_THIRD_ADDR(addr
);
626 if (first_addr
<= addr
&& addr
< second_addr
) {
627 seterrmsg("invalid destination");
630 GET_COMMAND_SUFFIX();
631 if (!isglobal
) clear_undo_stack();
632 if (move_lines(addr
) < 0)
636 if (check_addr_range(current_addr
, current_addr
) < 0)
638 GET_COMMAND_SUFFIX();
639 if (display_lines(first_addr
, second_addr
, gflag
| GNP
) < 0)
644 if (check_addr_range(current_addr
, current_addr
) < 0)
646 GET_COMMAND_SUFFIX();
647 if (display_lines(first_addr
, second_addr
, gflag
| GPR
) < 0)
653 seterrmsg("unexpected address");
656 GET_COMMAND_SUFFIX();
657 prompt
= prompt
? NULL
: optarg
? optarg
: dps
;
662 seterrmsg("unexpected address");
665 GET_COMMAND_SUFFIX();
666 gflag
= (modified
&& !scripted
&& c
== 'q') ? EMOD
: EOF
;
669 if (!isspace((unsigned char)*ibufp
)) {
670 seterrmsg("unexpected command suffix");
672 } else if (addr_cnt
== 0)
673 second_addr
= addr_last
;
674 if ((fnp
= get_filename()) == NULL
)
676 GET_COMMAND_SUFFIX();
677 if (!isglobal
) clear_undo_stack();
678 if (*old_filename
== '\0' && *fnp
!= '!')
679 strlcpy(old_filename
, fnp
, sizeof(old_filename
) - 2);
681 if (*fnp
== '\0' && *old_filename
== '\0') {
682 seterrmsg("no current filename");
686 if ((addr
= read_file(*fnp
? fnp
: old_filename
, second_addr
)) < 0)
688 else if (addr
&& addr
!= addr_last
)
709 case '0': case '1': case '2': case '3': case '4':
710 case '5': case '6': case '7': case '8': case '9':
711 STRTOL(sgnum
, ibufp
);
713 sgflag
&= ~GSG
; /* override GSG */
717 seterrmsg("invalid command suffix");
721 } while (sflags
&& *ibufp
!= '\n');
722 if (sflags
&& !pat
) {
723 seterrmsg("no previous substitution");
725 } else if (sflags
& SGG
)
726 sgnum
= 0; /* override numeric arg */
727 if (*ibufp
!= '\n' && *(ibufp
+ 1) == '\n') {
728 seterrmsg("invalid pattern delimiter");
733 if ((!sflags
|| (sflags
& SGR
)) &&
734 (tpat
= get_compiled_pattern()) == NULL
) {
737 } else if (tpat
!= pat
) {
743 patlock
= 1; /* reserve pattern */
746 if (!sflags
&& extract_subst_tail(&sgflag
, &sgnum
) < 0)
755 sgflag
^= GPR
, sgflag
&= ~(GLS
| GNP
);
759 sgflag
|= GPR
, ibufp
++;
762 sgflag
|= GLS
, ibufp
++;
765 sgflag
|= GNP
, ibufp
++;
771 if (check_addr_range(current_addr
, current_addr
) < 0)
773 GET_COMMAND_SUFFIX();
774 if (!isglobal
) clear_undo_stack();
775 if (search_and_replace(pat
, sgflag
, sgnum
) < 0)
779 if (check_addr_range(current_addr
, current_addr
) < 0)
781 GET_THIRD_ADDR(addr
);
782 GET_COMMAND_SUFFIX();
783 if (!isglobal
) clear_undo_stack();
784 if (copy_lines(addr
) < 0)
789 seterrmsg("unexpected address");
792 GET_COMMAND_SUFFIX();
793 if (pop_undo_stack() < 0)
798 if ((n
= *ibufp
) == 'q' || n
== 'Q') {
802 if (!isspace((unsigned char)*ibufp
)) {
803 seterrmsg("unexpected command suffix");
805 } else if ((fnp
= get_filename()) == NULL
)
807 if (addr_cnt
== 0 && !addr_last
)
808 first_addr
= second_addr
= 0;
809 else if (check_addr_range(1, addr_last
) < 0)
811 GET_COMMAND_SUFFIX();
812 if (*old_filename
== '\0' && *fnp
!= '!')
813 strlcpy(old_filename
, fnp
, sizeof(old_filename
) - 2);
815 if (*fnp
== '\0' && *old_filename
== '\0') {
816 seterrmsg("no current filename");
820 if ((addr
= write_file(*fnp
? fnp
: old_filename
,
821 (c
== 'W') ? "a" : "w", first_addr
, second_addr
)) < 0)
823 else if (addr
== addr_last
)
825 else if (modified
&& !scripted
&& n
== 'q')
830 seterrmsg("unexpected address");
833 GET_COMMAND_SUFFIX();
837 seterrmsg("crypt unavailable");
843 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0)
845 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0)
848 else if ('0' < *ibufp
&& *ibufp
<= '9')
850 GET_COMMAND_SUFFIX();
851 if (display_lines(second_addr
, min(addr_last
,
852 second_addr
+ rows
), gflag
) < 0)
857 GET_COMMAND_SUFFIX();
858 printf("%ld\n", addr_cnt
? second_addr
: addr_last
);
862 seterrmsg("unexpected address");
864 } else if ((sflags
= get_shell_command()) < 0)
866 GET_COMMAND_SUFFIX();
867 if (sflags
) printf("%s\n", shcmd
+ 1);
869 if (!scripted
) printf("!\n");
873 if (check_addr_range(first_addr
= 1, current_addr
+ 1) < 0
875 if (check_addr_range(first_addr
= 1, current_addr
+ !isglobal
) < 0
877 || display_lines(second_addr
, second_addr
, 0) < 0)
881 seterrmsg("unknown command");
888 /* check_addr_range: return status of address range check */
890 check_addr_range(long n
, long m
)
896 if (first_addr
> second_addr
|| 1 > first_addr
||
897 second_addr
> addr_last
) {
898 seterrmsg("invalid address");
905 /* get_matching_node_addr: return the address of the next line matching a
906 pattern in a given direction. wrap around begin/end of editor buffer if
909 get_matching_node_addr(pattern_t
*pat
, int dir
)
912 long n
= current_addr
;
915 if (!pat
) return ERR
;
917 if ((n
= dir
? INC_MOD(n
, addr_last
) :
918 DEC_MOD(n
, addr_last
)) != 0) {
919 lp
= get_addressed_line_node(n
);
920 if ((s
= get_sbuf_line(lp
)) == NULL
)
923 NUL_TO_NEWLINE(s
, lp
->len
);
924 if (!regexec(pat
, s
, 0, NULL
, 0))
927 } while (n
!= current_addr
);
928 seterrmsg("no match");
933 /* get_filename: return pointer to copy of filename in the command buffer */
937 static char *file
= NULL
;
938 static int filesz
= 0;
942 if (*ibufp
!= '\n') {
944 if (*ibufp
== '\n') {
945 seterrmsg("invalid filename");
947 } else if ((ibufp
= get_extended_line(&n
, 1)) == NULL
)
949 else if (*ibufp
== '!') {
951 if ((n
= get_shell_command()) < 0)
953 if (n
) printf("%s\n", shcmd
+ 1);
955 } else if (n
- 1 > MAXPATHLEN
) {
956 seterrmsg("filename too long");
961 else if (*old_filename
== '\0') {
962 seterrmsg("no current filename");
966 REALLOC(file
, filesz
, MAXPATHLEN
+ 1, NULL
);
967 for (n
= 0; *ibufp
!= '\n';)
968 file
[n
++] = *ibufp
++;
970 return is_legal_filename(file
) ? file
: NULL
;
974 /* get_shell_command: read a shell command from stdin; return substitution
977 get_shell_command(void)
979 static char *buf
= NULL
;
982 char *s
; /* substitution char pointer */
987 seterrmsg("shell access restricted");
989 } else if ((s
= ibufp
= get_extended_line(&j
, 1)) == NULL
)
991 REALLOC(buf
, n
, j
+ 1, ERR
);
992 buf
[i
++] = '!'; /* prefix command w/ bang */
993 while (*ibufp
!= '\n')
996 REALLOC(buf
, n
, i
+ 2, ERR
);
998 if (*ibufp
++ == '\\')
1003 REALLOC(buf
, n
, i
+ 1, ERR
);
1004 buf
[i
++] = *ibufp
++;
1007 else if (shcmd
== NULL
|| *(shcmd
+ 1) == '\0')
1009 else if (shcmd
== NULL
)
1012 seterrmsg("no previous command");
1015 REALLOC(buf
, n
, i
+ shcmdi
, ERR
);
1016 for (s
= shcmd
+ 1; s
< shcmd
+ shcmdi
;)
1022 if (*old_filename
== '\0') {
1023 seterrmsg("no current filename");
1026 j
= strlen(s
= strip_escapes(old_filename
));
1027 REALLOC(buf
, n
, i
+ j
, ERR
);
1033 REALLOC(shcmd
, shcmdsz
, i
+ 1, ERR
);
1034 memcpy(shcmd
, buf
, i
);
1035 shcmd
[shcmdi
= i
] = '\0';
1036 return *s
== '!' || *s
== '%';
1040 /* append_lines: insert text from stdin to after line n; stop when either a
1041 single period is read or EOF; return status */
1043 append_lines(long n
)
1050 for (current_addr
= n
;;) {
1052 if ((l
= get_tty_line()) < 0)
1054 else if (l
== 0 || ibuf
[l
- 1] != '\n') {
1059 } else if (*(lp
= ibufp
) == '\0')
1062 while (*ibufp
++ != '\n')
1066 if (l
== 2 && lp
[0] == '.' && lp
[1] == '\n') {
1072 if ((lp
= put_sbuf_line(lp
)) == NULL
) {
1076 up
->t
= get_addressed_line_node(current_addr
);
1077 else if ((up
= push_undo_stack(UADD
, current_addr
,
1078 current_addr
)) == NULL
) {
1082 } while (lp
!= eot
);
1090 /* join_lines: replace a range of lines with the joined text of those lines */
1092 join_lines(long from
, long to
)
1094 static char *buf
= NULL
;
1101 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1102 bp
= get_addressed_line_node(from
);
1103 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1104 if ((s
= get_sbuf_line(bp
)) == NULL
)
1106 REALLOC(buf
, n
, size
+ bp
->len
, ERR
);
1107 memcpy(buf
+ size
, s
, bp
->len
);
1110 REALLOC(buf
, n
, size
+ 2, ERR
);
1111 memcpy(buf
+ size
, "\n", 2);
1112 if (delete_lines(from
, to
) < 0)
1114 current_addr
= from
- 1;
1116 if (put_sbuf_line(buf
) == NULL
||
1117 push_undo_stack(UADD
, current_addr
, current_addr
) == NULL
) {
1127 /* move_lines: move a range of lines */
1129 move_lines(long addr
)
1131 line_t
*b1
, *a1
, *b2
, *a2
;
1132 long n
= INC_MOD(second_addr
, addr_last
);
1133 long p
= first_addr
- 1;
1134 int done
= (addr
== first_addr
- 1 || addr
== second_addr
);
1138 a2
= get_addressed_line_node(n
);
1139 b2
= get_addressed_line_node(p
);
1140 current_addr
= second_addr
;
1141 } else if (push_undo_stack(UMOV
, p
, n
) == NULL
||
1142 push_undo_stack(UMOV
, addr
, INC_MOD(addr
, addr_last
)) == NULL
) {
1146 a1
= get_addressed_line_node(n
);
1147 if (addr
< first_addr
) {
1148 b1
= get_addressed_line_node(p
);
1149 b2
= get_addressed_line_node(addr
);
1150 /* this get_addressed_line_node last! */
1152 b2
= get_addressed_line_node(addr
);
1153 b1
= get_addressed_line_node(p
);
1154 /* this get_addressed_line_node last! */
1157 REQUE(b2
, b1
->q_forw
);
1158 REQUE(a1
->q_back
, a2
);
1160 current_addr
= addr
+ ((addr
< first_addr
) ?
1161 second_addr
- first_addr
+ 1 : 0);
1164 unset_active_nodes(b2
->q_forw
, a2
);
1171 /* copy_lines: copy a range of lines; return status */
1173 copy_lines(long addr
)
1175 line_t
*lp
, *np
= get_addressed_line_node(first_addr
);
1177 long n
= second_addr
- first_addr
+ 1;
1180 current_addr
= addr
;
1181 if (first_addr
<= addr
&& addr
< second_addr
) {
1182 n
= addr
- first_addr
+ 1;
1183 m
= second_addr
- addr
;
1185 for (; n
> 0; n
=m
, m
=0, np
= get_addressed_line_node(current_addr
+ 1))
1186 for (; n
-- > 0; np
= np
->q_forw
) {
1188 if ((lp
= dup_line_node(np
)) == NULL
) {
1195 else if ((up
= push_undo_stack(UADD
, current_addr
,
1196 current_addr
)) == NULL
) {
1207 /* delete_lines: delete a range of lines */
1209 delete_lines(long from
, long to
)
1214 if (push_undo_stack(UDEL
, from
, to
) == NULL
) {
1218 n
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1219 p
= get_addressed_line_node(from
- 1);
1220 /* this get_addressed_line_node last! */
1222 unset_active_nodes(p
->q_forw
, n
);
1224 addr_last
-= to
- from
+ 1;
1225 current_addr
= from
- 1;
1232 /* display_lines: print a range of lines to stdout */
1234 display_lines(long from
, long to
, int gflag
)
1241 seterrmsg("invalid address");
1244 ep
= get_addressed_line_node(INC_MOD(to
, addr_last
));
1245 bp
= get_addressed_line_node(from
);
1246 for (; bp
!= ep
; bp
= bp
->q_forw
) {
1247 if ((s
= get_sbuf_line(bp
)) == NULL
)
1249 if (put_tty_line(s
, bp
->len
, current_addr
= from
++, gflag
) < 0)
1256 #define MAXMARK 26 /* max number of marks */
1258 line_t
*mark
[MAXMARK
]; /* line markers */
1259 int markno
; /* line marker count */
1261 /* mark_line_node: set a line node mark */
1263 mark_line_node(line_t
*lp
, int n
)
1266 seterrmsg("invalid mark character");
1268 } else if (mark
[n
- 'a'] == NULL
)
1275 /* get_marked_node_addr: return address of a marked line */
1277 get_marked_node_addr(int n
)
1280 seterrmsg("invalid mark character");
1283 return get_line_node_addr(mark
[n
- 'a']);
1287 /* unmark_line_node: clear line node mark */
1289 unmark_line_node(line_t
*lp
)
1293 for (i
= 0; markno
&& i
< MAXMARK
; i
++)
1294 if (mark
[i
] == lp
) {
1301 /* dup_line_node: return a pointer to a copy of a line node */
1303 dup_line_node(line_t
*lp
)
1307 if ((np
= (line_t
*) malloc(sizeof(line_t
))) == NULL
) {
1308 fprintf(stderr
, "%s\n", strerror(errno
));
1309 seterrmsg("out of memory");
1312 np
->seek
= lp
->seek
;
1318 /* has_trailing_escape: return the parity of escapes preceding a character
1321 has_trailing_escape(char *s
, char *t
)
1323 return (s
== t
|| *(t
- 1) != '\\') ? 0 : !has_trailing_escape(s
, t
- 1);
1327 /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1329 strip_escapes(const char *s
)
1331 static char *file
= NULL
;
1332 static int filesz
= 0;
1336 REALLOC(file
, filesz
, MAXPATHLEN
+ 1, NULL
);
1337 while ((i
< (filesz
- 1)) &&
1338 (file
[i
++] = (*s
== '\\') != '\0' ? *++s
: *s
))
1340 file
[filesz
- 1] = '\0';
1346 signal_hup(int signo
)
1349 sigflags
|= (1 << (signo
- 1));
1350 else handle_hup(signo
);
1355 signal_int(int signo
)
1358 sigflags
|= (1 << (signo
- 1));
1359 else handle_int(signo
);
1364 handle_hup(int signo
)
1366 char *hup
= NULL
; /* hup filename */
1372 sigflags
&= ~(1 << (signo
- 1));
1373 if (addr_last
&& write_file("ed.hup", "w", 1, addr_last
) < 0 &&
1374 (s
= getenv("HOME")) != NULL
&&
1375 (n
= strlen(s
)) + 8 <= MAXPATHLEN
&& /* "ed.hup" + '/' */
1376 (hup
= (char *) malloc(n
+ 10)) != NULL
) {
1378 if (hup
[n
- 1] != '/')
1379 hup
[n
] = '/', hup
[n
+1] = '\0';
1380 strcat(hup
, "ed.hup");
1381 write_file(hup
, "w", 1, addr_last
);
1388 handle_int(int signo
)
1392 sigflags
&= ~(1 << (signo
- 1));
1393 #ifdef _POSIX_SOURCE
1394 siglongjmp(env
, -1);
1401 int cols
= 72; /* wrap column */
1404 handle_winch(int signo
)
1406 struct winsize ws
; /* window size structure */
1408 sigflags
&= ~(1 << (signo
- 1));
1409 if (ioctl(0, TIOCGWINSZ
, (char *) &ws
) >= 0) {
1410 if (ws
.ws_row
> 2) rows
= ws
.ws_row
- 2;
1411 if (ws
.ws_col
> 8) cols
= ws
.ws_col
- 8;
1416 /* is_legal_filename: return a legal filename */
1418 is_legal_filename(char *s
)
1420 if (red
&& (*s
== '!' || !strcmp(s
, "..") || strchr(s
, '/'))) {
1421 seterrmsg("shell access restricted");