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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
33 All rights reserved.\n";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
43 * This program is based on the editor algorithm described in
44 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
45 * in Pascal," Addison-Wesley, 1981.
47 * The buffering algorithm is attributed to Rodney Ruddock of
48 * the University of Guelph, Guelph, Ontario.
50 * The cbc.c encryption code is adapted from
51 * the bdes program by Matt Bishop of Dartmouth College,
56 #include <sys/types.h>
58 #include <sys/ioctl.h>
69 static sigjmp_buf env;
75 char stdinbuf[1]; /* stdin buffer */
76 static char *shcmd; /* shell command buffer */
77 static int shcmdsz; /* shell command buffer size */
78 static int shcmdi; /* shell command buffer index */
79 char *ibuf; /* ed command-line buffer */
80 int ibufsz; /* ed command-line buffer size */
81 char *ibufp; /* pointer to ed command-line buffer */
84 int des = 0; /* if set, use crypt(3) for i/o */
85 static int garrulous = 0; /* if set, print all error messages */
86 int isbinary; /* if set, buffer contains ASCII NULs */
87 int isglobal; /* if set, doing a global command */
88 int modified; /* if set, buffer modified since last write */
89 int mutex = 0; /* if set, signals set "sigflags" */
90 static int red = 0; /* if set, restrict shell/directory access */
91 int scripted = 0; /* if set, suppress diagnostics */
92 int sigflags = 0; /* if set, signals received while mutex set */
93 static int sigactive = 0; /* if set, signal handlers are enabled */
95 static char old_filename[PATH_MAX] = ""; /* default filename */
96 long current_addr; /* current address in editor buffer */
97 long addr_last; /* last address in editor buffer */
98 int lineno; /* script line number */
99 static const char *prompt; /* command-line prompt */
100 static const char *dps = "*"; /* default command-line prompt */
102 static const char *usage = "usage: %s [-] [-sx] [-p string] [file]\n";
104 /* ed: line editor */
106 main(volatile int argc, char ** volatile argv)
111 (void)setlocale(LC_ALL, "");
113 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
115 while ((c = getopt(argc, argv, "p:sx")) != -1)
117 case 'p': /* set prompt */
120 case 's': /* run script */
123 case 'x': /* use crypt */
127 fprintf(stderr, "crypt unavailable\n?\n");
132 fprintf(stderr, usage, red ? "red" : "ed");
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 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 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 errmsg = "warning: file modified";
197 "script, line %d: %s\n",
207 } else if (ibuf[n - 1] != '\n') {
209 errmsg = "unexpected end-of-file";
215 if ((status = extract_addr_range()) >= 0 &&
216 (status = exec_command()) >= 0)
218 (status = display_lines(current_addr, current_addr,
226 fputs("?\n", stderr); /* give warning */
227 errmsg = "warning: file modified";
230 fprintf(stderr, "script, line %d: %s\n",
238 fprintf(stderr, "script, line %d: %s\n",
240 } else if (garrulous)
241 fprintf(stderr, "%s\n", errmsg);
244 fputs("?\n", stderr);
247 fprintf(stderr, "script, line %d: %s\n",
257 long first_addr, second_addr;
258 static long addr_cnt;
260 /* extract_addr_range: get line addresses from the command buffer until an
261 illegal address is seen; return status */
263 extract_addr_range(void)
268 first_addr = second_addr = current_addr;
269 while ((addr = next_addr()) >= 0) {
271 first_addr = second_addr;
273 if (*ibufp != ',' && *ibufp != ';')
275 else if (*ibufp++ == ';')
278 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
279 first_addr = second_addr;
280 return (addr == ERR) ? ERR : 0;
284 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
286 #define MUST_BE_FIRST() do { \
288 errmsg = "invalid address"; \
293 /* next_addr: return the next line address in the command buffer */
298 long addr = current_addr;
304 for (hd = ibufp;; first = 0)
305 switch (c = *ibufp) {
313 if (isdigit((unsigned char)*ibufp)) {
315 addr += (c == '-' || c == '^') ? -n : n;
316 } else if (!isspace((unsigned char)c))
317 addr += (c == '-' || c == '^') ? -1 : 1;
319 case '0': case '1': case '2':
320 case '3': case '4': case '5':
321 case '6': case '7': case '8': case '9':
329 addr = (c == '.') ? current_addr : addr_last;
334 if ((addr = get_matching_node_addr(
335 get_compiled_pattern(), c == '/')) < 0)
337 else if (c == *ibufp)
343 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
352 second_addr = (c == ';') ? current_addr : 1;
353 if ((addr = next_addr()) < 0)
361 else if (addr < 0 || addr_last < addr) {
362 errmsg = "invalid address";
372 /* GET_THIRD_ADDR: get a legal address from the command buffer */
373 #define GET_THIRD_ADDR(addr) \
377 ol1 = first_addr, ol2 = second_addr; \
378 if (extract_addr_range() < 0) \
380 else if (addr_cnt == 0) { \
381 errmsg = "destination expected"; \
383 } else if (second_addr < 0 || addr_last < second_addr) { \
384 errmsg = "invalid address"; \
387 addr = second_addr; \
388 first_addr = ol1, second_addr = ol2; \
390 #else /* BACKWARDS */
391 /* GET_THIRD_ADDR: get a legal address from the command buffer */
392 #define GET_THIRD_ADDR(addr) \
396 ol1 = first_addr, ol2 = second_addr; \
397 if (extract_addr_range() < 0) \
399 if (second_addr < 0 || addr_last < second_addr) { \
400 errmsg = "invalid address"; \
403 addr = second_addr; \
404 first_addr = ol1, second_addr = ol2; \
409 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
410 #define GET_COMMAND_SUFFIX() { \
415 gflag |= GPR, ibufp++; \
418 gflag |= GLS, ibufp++; \
421 gflag |= GNP, ibufp++; \
427 if (*ibufp++ != '\n') { \
428 errmsg = "invalid command suffix"; \
435 #define SGG 001 /* complement previous global substitute suffix */
436 #define SGP 002 /* complement previous print suffix */
437 #define SGR 004 /* use last regex instead of last pat */
438 #define SGF 010 /* repeat last substitution */
440 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
442 long rows = 22; /* scroll length: ws_row - 2 */
444 /* exec_command: execute the next command in command buffer; return print
449 static pattern_t *pat = NULL;
450 static int sgflag = 0;
451 static long sgnum = 0;
462 switch(c = *ibufp++) {
464 GET_COMMAND_SUFFIX();
465 if (!isglobal) clear_undo_stack();
466 if (append_lines(second_addr) < 0)
470 if (check_addr_range(current_addr, current_addr) < 0)
472 GET_COMMAND_SUFFIX();
473 if (!isglobal) clear_undo_stack();
474 if (delete_lines(first_addr, second_addr) < 0 ||
475 append_lines(current_addr) < 0)
479 if (check_addr_range(current_addr, current_addr) < 0)
481 GET_COMMAND_SUFFIX();
482 if (!isglobal) clear_undo_stack();
483 if (delete_lines(first_addr, second_addr) < 0)
485 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
489 if (modified && !scripted)
494 errmsg = "unexpected address";
496 } else if (!isspace((unsigned char)*ibufp)) {
497 errmsg = "unexpected command suffix";
499 } else if ((fnp = get_filename()) == NULL)
501 GET_COMMAND_SUFFIX();
502 if (delete_lines(1, addr_last) < 0)
505 if (close_sbuf() < 0)
507 else if (open_sbuf() < 0)
509 if (*fnp && *fnp != '!')
510 strlcpy(old_filename, fnp, PATH_MAX);
512 if (*fnp == '\0' && *old_filename == '\0') {
513 errmsg = "no current filename";
517 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
521 u_current_addr = u_addr_last = -1;
525 errmsg = "unexpected address";
527 } else if (!isspace((unsigned char)*ibufp)) {
528 errmsg = "unexpected command suffix";
530 } else if ((fnp = get_filename()) == NULL)
532 else if (*fnp == '!') {
533 errmsg = "invalid redirection";
536 GET_COMMAND_SUFFIX();
538 strlcpy(old_filename, fnp, PATH_MAX);
539 printf("%s\n", strip_escapes(old_filename));
546 errmsg = "cannot nest global commands";
548 } else if (check_addr_range(1, addr_last) < 0)
550 else if (build_active_list(c == 'g' || c == 'G') < 0)
552 else if ((n = (c == 'G' || c == 'V')))
553 GET_COMMAND_SUFFIX();
555 if (exec_global(n, gflag) < 0)
560 errmsg = "unexpected address";
563 GET_COMMAND_SUFFIX();
564 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
568 errmsg = "unexpected address";
571 GET_COMMAND_SUFFIX();
572 if ((garrulous = 1 - garrulous) && *errmsg)
573 fprintf(stderr, "%s\n", errmsg);
576 if (second_addr == 0) {
577 errmsg = "invalid address";
580 GET_COMMAND_SUFFIX();
581 if (!isglobal) clear_undo_stack();
582 if (append_lines(second_addr - 1) < 0)
586 if (check_addr_range(current_addr, current_addr + 1) < 0)
588 GET_COMMAND_SUFFIX();
589 if (!isglobal) clear_undo_stack();
590 if (first_addr != second_addr &&
591 join_lines(first_addr, second_addr) < 0)
596 if (second_addr == 0) {
597 errmsg = "invalid address";
600 GET_COMMAND_SUFFIX();
601 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
605 if (check_addr_range(current_addr, current_addr) < 0)
607 GET_COMMAND_SUFFIX();
608 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
613 if (check_addr_range(current_addr, current_addr) < 0)
615 GET_THIRD_ADDR(addr);
616 if (first_addr <= addr && addr < second_addr) {
617 errmsg = "invalid destination";
620 GET_COMMAND_SUFFIX();
621 if (!isglobal) clear_undo_stack();
622 if (move_lines(addr) < 0)
626 if (check_addr_range(current_addr, current_addr) < 0)
628 GET_COMMAND_SUFFIX();
629 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
634 if (check_addr_range(current_addr, current_addr) < 0)
636 GET_COMMAND_SUFFIX();
637 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
643 errmsg = "unexpected address";
646 GET_COMMAND_SUFFIX();
647 prompt = prompt ? NULL : optarg ? optarg : dps;
652 errmsg = "unexpected address";
655 GET_COMMAND_SUFFIX();
656 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
659 if (!isspace((unsigned char)*ibufp)) {
660 errmsg = "unexpected command suffix";
662 } else if (addr_cnt == 0)
663 second_addr = addr_last;
664 if ((fnp = get_filename()) == NULL)
666 GET_COMMAND_SUFFIX();
667 if (!isglobal) clear_undo_stack();
668 if (*old_filename == '\0' && *fnp != '!')
669 strlcpy(old_filename, fnp, PATH_MAX);
671 if (*fnp == '\0' && *old_filename == '\0') {
672 errmsg = "no current filename";
676 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
678 else if (addr && addr != addr_last)
699 case '0': case '1': case '2': case '3': case '4':
700 case '5': case '6': case '7': case '8': case '9':
701 STRTOL(sgnum, ibufp);
703 sgflag &= ~GSG; /* override GSG */
707 errmsg = "invalid command suffix";
711 } while (sflags && *ibufp != '\n');
712 if (sflags && !pat) {
713 errmsg = "no previous substitution";
715 } else if (sflags & SGG)
716 sgnum = 0; /* override numeric arg */
717 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
718 errmsg = "invalid pattern delimiter";
723 if ((!sflags || (sflags & SGR)) &&
724 (tpat = get_compiled_pattern()) == NULL) {
727 } else if (tpat != pat) {
733 patlock = 1; /* reserve pattern */
736 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
745 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
749 sgflag |= GPR, ibufp++;
752 sgflag |= GLS, ibufp++;
755 sgflag |= GNP, ibufp++;
761 if (check_addr_range(current_addr, current_addr) < 0)
763 GET_COMMAND_SUFFIX();
764 if (!isglobal) clear_undo_stack();
765 if (search_and_replace(pat, sgflag, sgnum) < 0)
769 if (check_addr_range(current_addr, current_addr) < 0)
771 GET_THIRD_ADDR(addr);
772 GET_COMMAND_SUFFIX();
773 if (!isglobal) clear_undo_stack();
774 if (copy_lines(addr) < 0)
779 errmsg = "unexpected address";
782 GET_COMMAND_SUFFIX();
783 if (pop_undo_stack() < 0)
788 if ((n = *ibufp) == 'q' || n == 'Q') {
792 if (!isspace((unsigned char)*ibufp)) {
793 errmsg = "unexpected command suffix";
795 } else if ((fnp = get_filename()) == NULL)
797 if (addr_cnt == 0 && !addr_last)
798 first_addr = second_addr = 0;
799 else if (check_addr_range(1, addr_last) < 0)
801 GET_COMMAND_SUFFIX();
802 if (*old_filename == '\0' && *fnp != '!')
803 strlcpy(old_filename, fnp, PATH_MAX);
805 if (*fnp == '\0' && *old_filename == '\0') {
806 errmsg = "no current filename";
810 if ((addr = write_file(*fnp ? fnp : old_filename,
811 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
813 else if (addr == addr_last && *fnp != '!')
815 else if (modified && !scripted && n == 'q')
820 errmsg = "unexpected address";
823 GET_COMMAND_SUFFIX();
828 errmsg = "crypt unavailable";
833 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
835 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
838 else if ('0' < *ibufp && *ibufp <= '9')
840 GET_COMMAND_SUFFIX();
841 if (display_lines(second_addr, min(addr_last,
842 second_addr + rows), gflag) < 0)
847 GET_COMMAND_SUFFIX();
848 printf("%ld\n", addr_cnt ? second_addr : addr_last);
852 errmsg = "unexpected address";
854 } else if ((sflags = get_shell_command()) < 0)
856 GET_COMMAND_SUFFIX();
857 if (sflags) printf("%s\n", shcmd + 1);
859 if (!scripted) printf("!\n");
863 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
865 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
867 || display_lines(second_addr, second_addr, 0) < 0)
871 errmsg = "unknown command";
878 /* check_addr_range: return status of address range check */
880 check_addr_range(long n, long m)
886 if (first_addr > second_addr || 1 > first_addr ||
887 second_addr > addr_last) {
888 errmsg = "invalid address";
895 /* get_matching_node_addr: return the address of the next line matching a
896 pattern in a given direction. wrap around begin/end of editor buffer if
899 get_matching_node_addr(pattern_t *pat, int dir)
902 long n = current_addr;
905 if (!pat) return ERR;
907 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
908 lp = get_addressed_line_node(n);
909 if ((s = get_sbuf_line(lp)) == NULL)
912 NUL_TO_NEWLINE(s, lp->len);
913 if (!regexec(pat, s, 0, NULL, 0))
916 } while (n != current_addr);
922 /* get_filename: return pointer to copy of filename in the command buffer */
926 static char *file = NULL;
927 static int filesz = 0;
931 if (*ibufp != '\n') {
933 if (*ibufp == '\n') {
934 errmsg = "invalid filename";
936 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
938 else if (*ibufp == '!') {
940 if ((n = get_shell_command()) < 0)
943 printf("%s\n", shcmd + 1);
945 } else if (n > PATH_MAX - 1) {
946 errmsg = "filename too long";
951 else if (*old_filename == '\0') {
952 errmsg = "no current filename";
956 REALLOC(file, filesz, PATH_MAX, NULL);
957 for (n = 0; *ibufp != '\n';)
958 file[n++] = *ibufp++;
960 return is_legal_filename(file) ? file : NULL;
964 /* get_shell_command: read a shell command from stdin; return substitution
967 get_shell_command(void)
969 static char *buf = NULL;
972 char *s; /* substitution char pointer */
977 errmsg = "shell access restricted";
979 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
981 REALLOC(buf, n, j + 1, ERR);
982 buf[i++] = '!'; /* prefix command w/ bang */
983 while (*ibufp != '\n')
986 REALLOC(buf, n, i + 2, ERR);
988 if (*ibufp++ == '\\')
993 REALLOC(buf, n, i + 1, ERR);
997 else if (shcmd == NULL || *(shcmd + 1) == '\0')
999 else if (shcmd == NULL)
1002 errmsg = "no previous command";
1005 REALLOC(buf, n, i + shcmdi, ERR);
1006 for (s = shcmd + 1; s < shcmd + shcmdi;)
1012 if (*old_filename == '\0') {
1013 errmsg = "no current filename";
1016 j = strlen(s = strip_escapes(old_filename));
1017 REALLOC(buf, n, i + j, ERR);
1023 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1024 memcpy(shcmd, buf, i);
1025 shcmd[shcmdi = i] = '\0';
1026 return *s == '!' || *s == '%';
1030 /* append_lines: insert text from stdin to after line n; stop when either a
1031 single period is read or EOF; return status */
1033 append_lines(long n)
1036 const char *lp = ibuf;
1040 for (current_addr = n;;) {
1042 if ((l = get_tty_line()) < 0)
1044 else if (l == 0 || ibuf[l - 1] != '\n') {
1049 } else if (*(lp = ibufp) == '\0')
1052 while (*ibufp++ != '\n')
1056 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1062 if ((lp = put_sbuf_line(lp)) == NULL) {
1066 up->t = get_addressed_line_node(current_addr);
1067 else if ((up = push_undo_stack(UADD, current_addr,
1068 current_addr)) == NULL) {
1072 } while (lp != eot);
1080 /* join_lines: replace a range of lines with the joined text of those lines */
1082 join_lines(long from, long to)
1084 static char *buf = NULL;
1091 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1092 bp = get_addressed_line_node(from);
1093 for (; bp != ep; bp = bp->q_forw) {
1094 if ((s = get_sbuf_line(bp)) == NULL)
1096 REALLOC(buf, n, size + bp->len, ERR);
1097 memcpy(buf + size, s, bp->len);
1100 REALLOC(buf, n, size + 2, ERR);
1101 memcpy(buf + size, "\n", 2);
1102 if (delete_lines(from, to) < 0)
1104 current_addr = from - 1;
1106 if (put_sbuf_line(buf) == NULL ||
1107 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1117 /* move_lines: move a range of lines */
1119 move_lines(long addr)
1121 line_t *b1, *a1, *b2, *a2;
1122 long n = INC_MOD(second_addr, addr_last);
1123 long p = first_addr - 1;
1124 int done = (addr == first_addr - 1 || addr == second_addr);
1128 a2 = get_addressed_line_node(n);
1129 b2 = get_addressed_line_node(p);
1130 current_addr = second_addr;
1131 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1132 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1136 a1 = get_addressed_line_node(n);
1137 if (addr < first_addr) {
1138 b1 = get_addressed_line_node(p);
1139 b2 = get_addressed_line_node(addr);
1140 /* this get_addressed_line_node last! */
1142 b2 = get_addressed_line_node(addr);
1143 b1 = get_addressed_line_node(p);
1144 /* this get_addressed_line_node last! */
1147 REQUE(b2, b1->q_forw);
1148 REQUE(a1->q_back, a2);
1150 current_addr = addr + ((addr < first_addr) ?
1151 second_addr - first_addr + 1 : 0);
1154 unset_active_nodes(b2->q_forw, a2);
1161 /* copy_lines: copy a range of lines; return status */
1163 copy_lines(long addr)
1165 line_t *lp, *np = get_addressed_line_node(first_addr);
1167 long n = second_addr - first_addr + 1;
1170 current_addr = addr;
1171 if (first_addr <= addr && addr < second_addr) {
1172 n = addr - first_addr + 1;
1173 m = second_addr - addr;
1175 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1176 for (; n-- > 0; np = np->q_forw) {
1178 if ((lp = dup_line_node(np)) == NULL) {
1185 else if ((up = push_undo_stack(UADD, current_addr,
1186 current_addr)) == NULL) {
1197 /* delete_lines: delete a range of lines */
1199 delete_lines(long from, long to)
1204 if (push_undo_stack(UDEL, from, to) == NULL) {
1208 n = get_addressed_line_node(INC_MOD(to, addr_last));
1209 p = get_addressed_line_node(from - 1);
1210 /* this get_addressed_line_node last! */
1212 unset_active_nodes(p->q_forw, n);
1214 addr_last -= to - from + 1;
1215 current_addr = from - 1;
1222 /* display_lines: print a range of lines to stdout */
1224 display_lines(long from, long to, int gflag)
1231 errmsg = "invalid address";
1234 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1235 bp = get_addressed_line_node(from);
1236 for (; bp != ep; bp = bp->q_forw) {
1237 if ((s = get_sbuf_line(bp)) == NULL)
1239 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1246 #define MAXMARK 26 /* max number of marks */
1248 static line_t *mark[MAXMARK]; /* line markers */
1249 static int markno; /* line marker count */
1251 /* mark_line_node: set a line node mark */
1253 mark_line_node(line_t *lp, int n)
1255 if (!islower((unsigned char)n)) {
1256 errmsg = "invalid mark character";
1258 } else if (mark[n - 'a'] == NULL)
1265 /* get_marked_node_addr: return address of a marked line */
1267 get_marked_node_addr(int n)
1269 if (!islower((unsigned char)n)) {
1270 errmsg = "invalid mark character";
1273 return get_line_node_addr(mark[n - 'a']);
1277 /* unmark_line_node: clear line node mark */
1279 unmark_line_node(line_t *lp)
1283 for (i = 0; markno && i < MAXMARK; i++)
1284 if (mark[i] == lp) {
1291 /* dup_line_node: return a pointer to a copy of a line node */
1293 dup_line_node(line_t *lp)
1297 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1298 fprintf(stderr, "%s\n", strerror(errno));
1299 errmsg = "out of memory";
1302 np->seek = lp->seek;
1308 /* has_trailing_escape: return the parity of escapes preceding a character
1311 has_trailing_escape(char *s, char *t)
1313 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1317 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1319 strip_escapes(char *s)
1321 static char *file = NULL;
1322 static int filesz = 0;
1326 REALLOC(file, filesz, PATH_MAX, NULL);
1327 while (i < filesz - 1 /* Worry about a possible trailing escape */
1328 && (file[i++] = (*s == '\\') ? *++s : *s))
1335 signal_hup(int signo)
1338 sigflags |= (1 << (signo - 1));
1345 signal_int(int signo)
1348 sigflags |= (1 << (signo - 1));
1355 handle_hup(int signo)
1357 char *hup = NULL; /* hup filename */
1359 char ed_hup[] = "ed.hup";
1364 sigflags &= ~(1 << (signo - 1));
1365 if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 &&
1366 (s = getenv("HOME")) != NULL &&
1367 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1368 (hup = (char *) malloc(n + 10)) != NULL) {
1370 if (hup[n - 1] != '/')
1371 hup[n] = '/', hup[n+1] = '\0';
1372 strcat(hup, "ed.hup");
1373 write_file(hup, "w", 1, addr_last);
1380 handle_int(int signo)
1384 sigflags &= ~(1 << (signo - 1));
1385 #ifdef _POSIX_SOURCE
1386 siglongjmp(env, -1);
1393 int cols = 72; /* wrap column */
1396 handle_winch(int signo)
1398 int save_errno = errno;
1400 struct winsize ws; /* window size structure */
1402 sigflags &= ~(1 << (signo - 1));
1403 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1404 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1405 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1411 /* is_legal_filename: return a legal filename */
1413 is_legal_filename(char *s)
1415 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1416 errmsg = "shell access restricted";