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.
52 #include <sys/types.h>
54 #include <sys/ioctl.h>
65 static sigjmp_buf env;
71 char stdinbuf[1]; /* stdin buffer */
72 static char *shcmd; /* shell command buffer */
73 static int shcmdsz; /* shell command buffer size */
74 static int shcmdi; /* shell command buffer index */
75 char *ibuf; /* ed command-line buffer */
76 int ibufsz; /* ed command-line buffer size */
77 char *ibufp; /* pointer to ed command-line buffer */
80 static int garrulous = 0; /* if set, print all error messages */
81 int isbinary; /* if set, buffer contains ASCII NULs */
82 int isglobal; /* if set, doing a global command */
83 int modified; /* if set, buffer modified since last write */
84 int mutex = 0; /* if set, signals set "sigflags" */
85 static int red = 0; /* if set, restrict shell/directory access */
86 int scripted = 0; /* if set, suppress diagnostics */
87 int sigflags = 0; /* if set, signals received while mutex set */
88 static int sigactive = 0; /* if set, signal handlers are enabled */
90 static char old_filename[PATH_MAX] = ""; /* default filename */
91 long current_addr; /* current address in editor buffer */
92 long addr_last; /* last address in editor buffer */
93 int lineno; /* script line number */
94 static const char *prompt; /* command-line prompt */
95 static const char *dps = "*"; /* default command-line prompt */
97 static const char *usage = "usage: %s [-] [-sx] [-p string] [file]\n";
101 main(volatile int argc, char ** volatile argv)
106 (void)setlocale(LC_ALL, "");
108 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
110 while ((c = getopt(argc, argv, "p:sx")) != -1)
112 case 'p': /* set prompt */
115 case 's': /* run script */
118 case 'x': /* use crypt */
119 fprintf(stderr, "crypt unavailable\n?\n");
123 fprintf(stderr, usage, red ? "red" : "ed");
128 if (argc && **argv == '-') {
137 /* assert: reliable signals! */
139 handle_winch(SIGWINCH);
140 if (isatty(0)) signal(SIGWINCH, handle_winch);
142 signal(SIGHUP, signal_hup);
143 signal(SIGQUIT, SIG_IGN);
144 signal(SIGINT, signal_int);
146 if ((status = sigsetjmp(env, 1)))
148 if ((status = setjmp(env)))
151 fputs("\n?\n", stderr);
152 errmsg = "interrupt";
155 sigactive = 1; /* enable signal handlers */
156 if (argc && **argv && is_legal_filename(*argv)) {
157 if (read_file(*argv, 0) < 0 && !isatty(0))
159 else if (**argv != '!')
160 if (strlcpy(old_filename, *argv, sizeof(old_filename))
161 >= sizeof(old_filename))
164 fputs("?\n", stderr);
166 errmsg = "invalid filename";
172 if (status < 0 && garrulous)
173 fprintf(stderr, "%s\n", errmsg);
175 printf("%s", prompt);
178 if ((n = get_tty_line()) < 0) {
182 if (modified && !scripted) {
183 fputs("?\n", stderr);
184 errmsg = "warning: file modified";
188 "script, line %d: %s\n",
198 } else if (ibuf[n - 1] != '\n') {
200 errmsg = "unexpected end-of-file";
206 if ((status = extract_addr_range()) >= 0 &&
207 (status = exec_command()) >= 0)
209 (status = display_lines(current_addr, current_addr,
217 fputs("?\n", stderr); /* give warning */
218 errmsg = "warning: file modified";
221 fprintf(stderr, "script, line %d: %s\n",
229 fprintf(stderr, "script, line %d: %s\n",
231 } else if (garrulous)
232 fprintf(stderr, "%s\n", errmsg);
235 fputs("?\n", stderr);
238 fprintf(stderr, "script, line %d: %s\n",
248 long first_addr, second_addr;
249 static long addr_cnt;
251 /* extract_addr_range: get line addresses from the command buffer until an
252 illegal address is seen; return status */
254 extract_addr_range(void)
259 first_addr = second_addr = current_addr;
260 while ((addr = next_addr()) >= 0) {
262 first_addr = second_addr;
264 if (*ibufp != ',' && *ibufp != ';')
266 else if (*ibufp++ == ';')
269 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
270 first_addr = second_addr;
271 return (addr == ERR) ? ERR : 0;
275 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
277 #define MUST_BE_FIRST() do { \
279 errmsg = "invalid address"; \
284 /* next_addr: return the next line address in the command buffer */
289 long addr = current_addr;
295 for (hd = ibufp;; first = 0)
296 switch (c = *ibufp) {
304 if (isdigit((unsigned char)*ibufp)) {
306 addr += (c == '-' || c == '^') ? -n : n;
307 } else if (!isspace((unsigned char)c))
308 addr += (c == '-' || c == '^') ? -1 : 1;
310 case '0': case '1': case '2':
311 case '3': case '4': case '5':
312 case '6': case '7': case '8': case '9':
320 addr = (c == '.') ? current_addr : addr_last;
325 if ((addr = get_matching_node_addr(
326 get_compiled_pattern(), c == '/')) < 0)
328 else if (c == *ibufp)
334 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
343 second_addr = (c == ';') ? current_addr : 1;
344 if ((addr = next_addr()) < 0)
352 else if (addr < 0 || addr_last < addr) {
353 errmsg = "invalid address";
363 /* GET_THIRD_ADDR: get a legal address from the command buffer */
364 #define GET_THIRD_ADDR(addr) \
368 ol1 = first_addr, ol2 = second_addr; \
369 if (extract_addr_range() < 0) \
371 else if (addr_cnt == 0) { \
372 errmsg = "destination expected"; \
374 } else if (second_addr < 0 || addr_last < second_addr) { \
375 errmsg = "invalid address"; \
378 addr = second_addr; \
379 first_addr = ol1, second_addr = ol2; \
381 #else /* BACKWARDS */
382 /* GET_THIRD_ADDR: get a legal address from the command buffer */
383 #define GET_THIRD_ADDR(addr) \
387 ol1 = first_addr, ol2 = second_addr; \
388 if (extract_addr_range() < 0) \
390 if (second_addr < 0 || addr_last < second_addr) { \
391 errmsg = "invalid address"; \
394 addr = second_addr; \
395 first_addr = ol1, second_addr = ol2; \
400 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
401 #define GET_COMMAND_SUFFIX() { \
406 gflag |= GPR, ibufp++; \
409 gflag |= GLS, ibufp++; \
412 gflag |= GNP, ibufp++; \
418 if (*ibufp++ != '\n') { \
419 errmsg = "invalid command suffix"; \
426 #define SGG 001 /* complement previous global substitute suffix */
427 #define SGP 002 /* complement previous print suffix */
428 #define SGR 004 /* use last regex instead of last pat */
429 #define SGF 010 /* repeat last substitution */
431 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
433 long rows = 22; /* scroll length: ws_row - 2 */
435 /* exec_command: execute the next command in command buffer; return print
440 static pattern_t *pat = NULL;
441 static int sgflag = 0;
442 static long sgnum = 0;
453 switch(c = *ibufp++) {
455 GET_COMMAND_SUFFIX();
456 if (!isglobal) clear_undo_stack();
457 if (append_lines(second_addr) < 0)
461 if (check_addr_range(current_addr, current_addr) < 0)
463 GET_COMMAND_SUFFIX();
464 if (!isglobal) clear_undo_stack();
465 if (delete_lines(first_addr, second_addr) < 0 ||
466 append_lines(current_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)
476 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
480 if (modified && !scripted)
485 errmsg = "unexpected address";
487 } else if (!isspace((unsigned char)*ibufp)) {
488 errmsg = "unexpected command suffix";
490 } else if ((fnp = get_filename()) == NULL)
492 GET_COMMAND_SUFFIX();
493 if (delete_lines(1, addr_last) < 0)
496 if (close_sbuf() < 0)
498 else if (open_sbuf() < 0)
500 if (*fnp && *fnp != '!')
501 strlcpy(old_filename, fnp, PATH_MAX);
503 if (*fnp == '\0' && *old_filename == '\0') {
504 errmsg = "no current filename";
508 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
512 u_current_addr = u_addr_last = -1;
516 errmsg = "unexpected address";
518 } else if (!isspace((unsigned char)*ibufp)) {
519 errmsg = "unexpected command suffix";
521 } else if ((fnp = get_filename()) == NULL)
523 else if (*fnp == '!') {
524 errmsg = "invalid redirection";
527 GET_COMMAND_SUFFIX();
529 strlcpy(old_filename, fnp, PATH_MAX);
530 printf("%s\n", strip_escapes(old_filename));
537 errmsg = "cannot nest global commands";
539 } else if (check_addr_range(1, addr_last) < 0)
541 else if (build_active_list(c == 'g' || c == 'G') < 0)
543 else if ((n = (c == 'G' || c == 'V')))
544 GET_COMMAND_SUFFIX();
546 if (exec_global(n, gflag) < 0)
551 errmsg = "unexpected address";
554 GET_COMMAND_SUFFIX();
555 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
559 errmsg = "unexpected address";
562 GET_COMMAND_SUFFIX();
563 if ((garrulous = 1 - garrulous) && *errmsg)
564 fprintf(stderr, "%s\n", errmsg);
567 if (second_addr == 0) {
568 errmsg = "invalid address";
571 GET_COMMAND_SUFFIX();
572 if (!isglobal) clear_undo_stack();
573 if (append_lines(second_addr - 1) < 0)
577 if (check_addr_range(current_addr, current_addr + 1) < 0)
579 GET_COMMAND_SUFFIX();
580 if (!isglobal) clear_undo_stack();
581 if (first_addr != second_addr &&
582 join_lines(first_addr, second_addr) < 0)
587 if (second_addr == 0) {
588 errmsg = "invalid address";
591 GET_COMMAND_SUFFIX();
592 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
596 if (check_addr_range(current_addr, current_addr) < 0)
598 GET_COMMAND_SUFFIX();
599 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
604 if (check_addr_range(current_addr, current_addr) < 0)
606 GET_THIRD_ADDR(addr);
607 if (first_addr <= addr && addr < second_addr) {
608 errmsg = "invalid destination";
611 GET_COMMAND_SUFFIX();
612 if (!isglobal) clear_undo_stack();
613 if (move_lines(addr) < 0)
617 if (check_addr_range(current_addr, current_addr) < 0)
619 GET_COMMAND_SUFFIX();
620 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
625 if (check_addr_range(current_addr, current_addr) < 0)
627 GET_COMMAND_SUFFIX();
628 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
634 errmsg = "unexpected address";
637 GET_COMMAND_SUFFIX();
638 prompt = prompt ? NULL : optarg ? optarg : dps;
643 errmsg = "unexpected address";
646 GET_COMMAND_SUFFIX();
647 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
650 if (!isspace((unsigned char)*ibufp)) {
651 errmsg = "unexpected command suffix";
653 } else if (addr_cnt == 0)
654 second_addr = addr_last;
655 if ((fnp = get_filename()) == NULL)
657 GET_COMMAND_SUFFIX();
658 if (!isglobal) clear_undo_stack();
659 if (*old_filename == '\0' && *fnp != '!')
660 strlcpy(old_filename, fnp, PATH_MAX);
662 if (*fnp == '\0' && *old_filename == '\0') {
663 errmsg = "no current filename";
667 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
669 else if (addr && addr != addr_last)
690 case '0': case '1': case '2': case '3': case '4':
691 case '5': case '6': case '7': case '8': case '9':
692 STRTOL(sgnum, ibufp);
694 sgflag &= ~GSG; /* override GSG */
698 errmsg = "invalid command suffix";
702 } while (sflags && *ibufp != '\n');
703 if (sflags && !pat) {
704 errmsg = "no previous substitution";
706 } else if (sflags & SGG)
707 sgnum = 0; /* override numeric arg */
708 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
709 errmsg = "invalid pattern delimiter";
714 if ((!sflags || (sflags & SGR)) &&
715 (tpat = get_compiled_pattern()) == NULL) {
718 } else if (tpat != pat) {
724 patlock = 1; /* reserve pattern */
727 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
736 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
740 sgflag |= GPR, ibufp++;
743 sgflag |= GLS, ibufp++;
746 sgflag |= GNP, ibufp++;
752 if (check_addr_range(current_addr, current_addr) < 0)
754 GET_COMMAND_SUFFIX();
755 if (!isglobal) clear_undo_stack();
756 if (search_and_replace(pat, sgflag, sgnum) < 0)
760 if (check_addr_range(current_addr, current_addr) < 0)
762 GET_THIRD_ADDR(addr);
763 GET_COMMAND_SUFFIX();
764 if (!isglobal) clear_undo_stack();
765 if (copy_lines(addr) < 0)
770 errmsg = "unexpected address";
773 GET_COMMAND_SUFFIX();
774 if (pop_undo_stack() < 0)
779 if ((n = *ibufp) == 'q' || n == 'Q') {
783 if (!isspace((unsigned char)*ibufp)) {
784 errmsg = "unexpected command suffix";
786 } else if ((fnp = get_filename()) == NULL)
788 if (addr_cnt == 0 && !addr_last)
789 first_addr = second_addr = 0;
790 else if (check_addr_range(1, addr_last) < 0)
792 GET_COMMAND_SUFFIX();
793 if (*old_filename == '\0' && *fnp != '!')
794 strlcpy(old_filename, fnp, PATH_MAX);
796 if (*fnp == '\0' && *old_filename == '\0') {
797 errmsg = "no current filename";
801 if ((addr = write_file(*fnp ? fnp : old_filename,
802 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
804 else if (addr == addr_last && *fnp != '!')
806 else if (modified && !scripted && n == 'q')
811 errmsg = "unexpected address";
814 GET_COMMAND_SUFFIX();
815 errmsg = "crypt unavailable";
819 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
821 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
824 else if ('0' < *ibufp && *ibufp <= '9')
826 GET_COMMAND_SUFFIX();
827 if (display_lines(second_addr, min(addr_last,
828 second_addr + rows), gflag) < 0)
833 GET_COMMAND_SUFFIX();
834 printf("%ld\n", addr_cnt ? second_addr : addr_last);
838 errmsg = "unexpected address";
840 } else if ((sflags = get_shell_command()) < 0)
842 GET_COMMAND_SUFFIX();
843 if (sflags) printf("%s\n", shcmd + 1);
845 if (!scripted) printf("!\n");
849 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
851 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
853 || display_lines(second_addr, second_addr, 0) < 0)
857 errmsg = "unknown command";
864 /* check_addr_range: return status of address range check */
866 check_addr_range(long n, long m)
872 if (first_addr > second_addr || 1 > first_addr ||
873 second_addr > addr_last) {
874 errmsg = "invalid address";
881 /* get_matching_node_addr: return the address of the next line matching a
882 pattern in a given direction. wrap around begin/end of editor buffer if
885 get_matching_node_addr(pattern_t *pat, int dir)
888 long n = current_addr;
891 if (!pat) return ERR;
893 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
894 lp = get_addressed_line_node(n);
895 if ((s = get_sbuf_line(lp)) == NULL)
898 NUL_TO_NEWLINE(s, lp->len);
899 if (!regexec(pat, s, 0, NULL, 0))
902 } while (n != current_addr);
908 /* get_filename: return pointer to copy of filename in the command buffer */
912 static char *file = NULL;
913 static int filesz = 0;
917 if (*ibufp != '\n') {
919 if (*ibufp == '\n') {
920 errmsg = "invalid filename";
922 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
924 else if (*ibufp == '!') {
926 if ((n = get_shell_command()) < 0)
929 printf("%s\n", shcmd + 1);
931 } else if (n > PATH_MAX - 1) {
932 errmsg = "filename too long";
937 else if (*old_filename == '\0') {
938 errmsg = "no current filename";
942 REALLOC(file, filesz, PATH_MAX, NULL);
943 for (n = 0; *ibufp != '\n';)
944 file[n++] = *ibufp++;
946 return is_legal_filename(file) ? file : NULL;
950 /* get_shell_command: read a shell command from stdin; return substitution
953 get_shell_command(void)
955 static char *buf = NULL;
958 char *s; /* substitution char pointer */
963 errmsg = "shell access restricted";
965 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
967 REALLOC(buf, n, j + 1, ERR);
968 buf[i++] = '!'; /* prefix command w/ bang */
969 while (*ibufp != '\n')
972 REALLOC(buf, n, i + 2, ERR);
974 if (*ibufp++ == '\\')
979 REALLOC(buf, n, i + 1, ERR);
983 else if (shcmd == NULL || *(shcmd + 1) == '\0')
985 else if (shcmd == NULL)
988 errmsg = "no previous command";
991 REALLOC(buf, n, i + shcmdi, ERR);
992 for (s = shcmd + 1; s < shcmd + shcmdi;)
998 if (*old_filename == '\0') {
999 errmsg = "no current filename";
1002 j = strlen(s = strip_escapes(old_filename));
1003 REALLOC(buf, n, i + j, ERR);
1009 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1010 memcpy(shcmd, buf, i);
1011 shcmd[shcmdi = i] = '\0';
1012 return *s == '!' || *s == '%';
1016 /* append_lines: insert text from stdin to after line n; stop when either a
1017 single period is read or EOF; return status */
1019 append_lines(long n)
1022 const char *lp = ibuf;
1026 for (current_addr = n;;) {
1028 if ((l = get_tty_line()) < 0)
1030 else if (l == 0 || ibuf[l - 1] != '\n') {
1035 } else if (*(lp = ibufp) == '\0')
1038 while (*ibufp++ != '\n')
1042 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1048 if ((lp = put_sbuf_line(lp)) == NULL) {
1052 up->t = get_addressed_line_node(current_addr);
1053 else if ((up = push_undo_stack(UADD, current_addr,
1054 current_addr)) == NULL) {
1058 } while (lp != eot);
1066 /* join_lines: replace a range of lines with the joined text of those lines */
1068 join_lines(long from, long to)
1070 static char *buf = NULL;
1077 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1078 bp = get_addressed_line_node(from);
1079 for (; bp != ep; bp = bp->q_forw) {
1080 if ((s = get_sbuf_line(bp)) == NULL)
1082 REALLOC(buf, n, size + bp->len, ERR);
1083 memcpy(buf + size, s, bp->len);
1086 REALLOC(buf, n, size + 2, ERR);
1087 memcpy(buf + size, "\n", 2);
1088 if (delete_lines(from, to) < 0)
1090 current_addr = from - 1;
1092 if (put_sbuf_line(buf) == NULL ||
1093 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1103 /* move_lines: move a range of lines */
1105 move_lines(long addr)
1107 line_t *b1, *a1, *b2, *a2;
1108 long n = INC_MOD(second_addr, addr_last);
1109 long p = first_addr - 1;
1110 int done = (addr == first_addr - 1 || addr == second_addr);
1114 a2 = get_addressed_line_node(n);
1115 b2 = get_addressed_line_node(p);
1116 current_addr = second_addr;
1117 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1118 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1122 a1 = get_addressed_line_node(n);
1123 if (addr < first_addr) {
1124 b1 = get_addressed_line_node(p);
1125 b2 = get_addressed_line_node(addr);
1126 /* this get_addressed_line_node last! */
1128 b2 = get_addressed_line_node(addr);
1129 b1 = get_addressed_line_node(p);
1130 /* this get_addressed_line_node last! */
1133 REQUE(b2, b1->q_forw);
1134 REQUE(a1->q_back, a2);
1136 current_addr = addr + ((addr < first_addr) ?
1137 second_addr - first_addr + 1 : 0);
1140 unset_active_nodes(b2->q_forw, a2);
1147 /* copy_lines: copy a range of lines; return status */
1149 copy_lines(long addr)
1151 line_t *lp, *np = get_addressed_line_node(first_addr);
1153 long n = second_addr - first_addr + 1;
1156 current_addr = addr;
1157 if (first_addr <= addr && addr < second_addr) {
1158 n = addr - first_addr + 1;
1159 m = second_addr - addr;
1161 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1162 for (; n-- > 0; np = np->q_forw) {
1164 if ((lp = dup_line_node(np)) == NULL) {
1171 else if ((up = push_undo_stack(UADD, current_addr,
1172 current_addr)) == NULL) {
1183 /* delete_lines: delete a range of lines */
1185 delete_lines(long from, long to)
1190 if (push_undo_stack(UDEL, from, to) == NULL) {
1194 n = get_addressed_line_node(INC_MOD(to, addr_last));
1195 p = get_addressed_line_node(from - 1);
1196 /* this get_addressed_line_node last! */
1198 unset_active_nodes(p->q_forw, n);
1200 addr_last -= to - from + 1;
1201 current_addr = from - 1;
1208 /* display_lines: print a range of lines to stdout */
1210 display_lines(long from, long to, int gflag)
1217 errmsg = "invalid address";
1220 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1221 bp = get_addressed_line_node(from);
1222 for (; bp != ep; bp = bp->q_forw) {
1223 if ((s = get_sbuf_line(bp)) == NULL)
1225 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1232 #define MAXMARK 26 /* max number of marks */
1234 static line_t *mark[MAXMARK]; /* line markers */
1235 static int markno; /* line marker count */
1237 /* mark_line_node: set a line node mark */
1239 mark_line_node(line_t *lp, int n)
1241 if (!islower((unsigned char)n)) {
1242 errmsg = "invalid mark character";
1244 } else if (mark[n - 'a'] == NULL)
1251 /* get_marked_node_addr: return address of a marked line */
1253 get_marked_node_addr(int n)
1255 if (!islower((unsigned char)n)) {
1256 errmsg = "invalid mark character";
1259 return get_line_node_addr(mark[n - 'a']);
1263 /* unmark_line_node: clear line node mark */
1265 unmark_line_node(line_t *lp)
1269 for (i = 0; markno && i < MAXMARK; i++)
1270 if (mark[i] == lp) {
1277 /* dup_line_node: return a pointer to a copy of a line node */
1279 dup_line_node(line_t *lp)
1283 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1284 fprintf(stderr, "%s\n", strerror(errno));
1285 errmsg = "out of memory";
1288 np->seek = lp->seek;
1294 /* has_trailing_escape: return the parity of escapes preceding a character
1297 has_trailing_escape(char *s, char *t)
1299 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1303 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1305 strip_escapes(char *s)
1307 static char *file = NULL;
1308 static int filesz = 0;
1312 REALLOC(file, filesz, PATH_MAX, NULL);
1313 while (i < filesz - 1 /* Worry about a possible trailing escape */
1314 && (file[i++] = (*s == '\\') ? *++s : *s))
1321 signal_hup(int signo)
1324 sigflags |= (1 << (signo - 1));
1331 signal_int(int signo)
1334 sigflags |= (1 << (signo - 1));
1341 handle_hup(int signo)
1343 char *hup = NULL; /* hup filename */
1345 char ed_hup[] = "ed.hup";
1350 sigflags &= ~(1 << (signo - 1));
1351 if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 &&
1352 (s = getenv("HOME")) != NULL &&
1353 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1354 (hup = (char *) malloc(n + 10)) != NULL) {
1356 if (hup[n - 1] != '/')
1357 hup[n] = '/', hup[n+1] = '\0';
1358 strcat(hup, "ed.hup");
1359 write_file(hup, "w", 1, addr_last);
1366 handle_int(int signo)
1370 sigflags &= ~(1 << (signo - 1));
1371 #ifdef _POSIX_SOURCE
1372 siglongjmp(env, -1);
1379 int cols = 72; /* wrap column */
1382 handle_winch(int signo)
1384 int save_errno = errno;
1386 struct winsize ws; /* window size structure */
1388 sigflags &= ~(1 << (signo - 1));
1389 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1390 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1391 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1397 /* is_legal_filename: return a legal filename */
1399 is_legal_filename(char *s)
1401 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1402 errmsg = "shell access restricted";