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>
41 * This program is based on the editor algorithm described in
42 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
43 * in Pascal," Addison-Wesley, 1981.
45 * The buffering algorithm is attributed to Rodney Ruddock of
46 * the University of Guelph, Guelph, Ontario.
50 #include <sys/types.h>
52 #include <sys/ioctl.h>
63 static sigjmp_buf env;
69 char stdinbuf[1]; /* stdin buffer */
70 static char *shcmd; /* shell command buffer */
71 static int shcmdsz; /* shell command buffer size */
72 static int shcmdi; /* shell command buffer index */
73 char *ibuf; /* ed command-line buffer */
74 int ibufsz; /* ed command-line buffer size */
75 char *ibufp; /* pointer to ed command-line buffer */
78 static int garrulous = 0; /* if set, print all error messages */
79 int isbinary; /* if set, buffer contains ASCII NULs */
80 int isglobal; /* if set, doing a global command */
81 int modified; /* if set, buffer modified since last write */
82 int mutex = 0; /* if set, signals set "sigflags" */
83 static int red = 0; /* if set, restrict shell/directory access */
84 int scripted = 0; /* if set, suppress diagnostics */
85 int sigflags = 0; /* if set, signals received while mutex set */
86 static int sigactive = 0; /* if set, signal handlers are enabled */
88 static char old_filename[PATH_MAX] = ""; /* default filename */
89 long current_addr; /* current address in editor buffer */
90 long addr_last; /* last address in editor buffer */
91 int lineno; /* script line number */
92 static const char *prompt; /* command-line prompt */
93 static const char *dps = "*"; /* default command-line prompt */
95 static const char *usage = "usage: %s [-] [-sx] [-p string] [file]\n";
99 main(volatile int argc, char ** volatile argv)
104 (void)setlocale(LC_ALL, "");
106 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
108 while ((c = getopt(argc, argv, "p:sx")) != -1)
110 case 'p': /* set prompt */
113 case 's': /* run script */
116 case 'x': /* use crypt */
117 fprintf(stderr, "crypt unavailable\n?\n");
121 fprintf(stderr, usage, red ? "red" : "ed");
126 if (argc && **argv == '-') {
135 /* assert: reliable signals! */
137 handle_winch(SIGWINCH);
138 if (isatty(0)) signal(SIGWINCH, handle_winch);
140 signal(SIGHUP, signal_hup);
141 signal(SIGQUIT, SIG_IGN);
142 signal(SIGINT, signal_int);
144 if ((status = sigsetjmp(env, 1)))
146 if ((status = setjmp(env)))
149 fputs("\n?\n", stderr);
150 errmsg = "interrupt";
153 sigactive = 1; /* enable signal handlers */
154 if (argc && **argv && is_legal_filename(*argv)) {
155 if (read_file(*argv, 0) < 0 && !isatty(0))
157 else if (**argv != '!')
158 if (strlcpy(old_filename, *argv, sizeof(old_filename))
159 >= sizeof(old_filename))
162 fputs("?\n", stderr);
164 errmsg = "invalid filename";
170 if (status < 0 && garrulous)
171 fprintf(stderr, "%s\n", errmsg);
173 printf("%s", prompt);
176 if ((n = get_tty_line()) < 0) {
180 if (modified && !scripted) {
181 fputs("?\n", stderr);
182 errmsg = "warning: file modified";
186 "script, line %d: %s\n",
196 } else if (ibuf[n - 1] != '\n') {
198 errmsg = "unexpected end-of-file";
204 if ((status = extract_addr_range()) >= 0 &&
205 (status = exec_command()) >= 0)
207 (status = display_lines(current_addr, current_addr,
215 fputs("?\n", stderr); /* give warning */
216 errmsg = "warning: file modified";
219 fprintf(stderr, "script, line %d: %s\n",
227 fprintf(stderr, "script, line %d: %s\n",
229 } else if (garrulous)
230 fprintf(stderr, "%s\n", errmsg);
233 fputs("?\n", stderr);
236 fprintf(stderr, "script, line %d: %s\n",
246 long first_addr, second_addr;
247 static long addr_cnt;
249 /* extract_addr_range: get line addresses from the command buffer until an
250 illegal address is seen; return status */
252 extract_addr_range(void)
257 first_addr = second_addr = current_addr;
258 while ((addr = next_addr()) >= 0) {
260 first_addr = second_addr;
262 if (*ibufp != ',' && *ibufp != ';')
264 else if (*ibufp++ == ';')
267 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
268 first_addr = second_addr;
269 return (addr == ERR) ? ERR : 0;
273 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
275 #define MUST_BE_FIRST() do { \
277 errmsg = "invalid address"; \
282 /* next_addr: return the next line address in the command buffer */
287 long addr = current_addr;
293 for (hd = ibufp;; first = 0)
294 switch (c = *ibufp) {
302 if (isdigit((unsigned char)*ibufp)) {
304 addr += (c == '-' || c == '^') ? -n : n;
305 } else if (!isspace((unsigned char)c))
306 addr += (c == '-' || c == '^') ? -1 : 1;
308 case '0': case '1': case '2':
309 case '3': case '4': case '5':
310 case '6': case '7': case '8': case '9':
318 addr = (c == '.') ? current_addr : addr_last;
323 if ((addr = get_matching_node_addr(
324 get_compiled_pattern(), c == '/')) < 0)
326 else if (c == *ibufp)
332 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
341 second_addr = (c == ';') ? current_addr : 1;
342 if ((addr = next_addr()) < 0)
350 else if (addr < 0 || addr_last < addr) {
351 errmsg = "invalid address";
361 /* GET_THIRD_ADDR: get a legal address from the command buffer */
362 #define GET_THIRD_ADDR(addr) \
366 ol1 = first_addr, ol2 = second_addr; \
367 if (extract_addr_range() < 0) \
369 else if (addr_cnt == 0) { \
370 errmsg = "destination expected"; \
372 } else if (second_addr < 0 || addr_last < second_addr) { \
373 errmsg = "invalid address"; \
376 addr = second_addr; \
377 first_addr = ol1, second_addr = ol2; \
379 #else /* BACKWARDS */
380 /* GET_THIRD_ADDR: get a legal address from the command buffer */
381 #define GET_THIRD_ADDR(addr) \
385 ol1 = first_addr, ol2 = second_addr; \
386 if (extract_addr_range() < 0) \
388 if (second_addr < 0 || addr_last < second_addr) { \
389 errmsg = "invalid address"; \
392 addr = second_addr; \
393 first_addr = ol1, second_addr = ol2; \
398 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
399 #define GET_COMMAND_SUFFIX() { \
404 gflag |= GPR, ibufp++; \
407 gflag |= GLS, ibufp++; \
410 gflag |= GNP, ibufp++; \
416 if (*ibufp++ != '\n') { \
417 errmsg = "invalid command suffix"; \
424 #define SGG 001 /* complement previous global substitute suffix */
425 #define SGP 002 /* complement previous print suffix */
426 #define SGR 004 /* use last regex instead of last pat */
427 #define SGF 010 /* repeat last substitution */
429 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
431 long rows = 22; /* scroll length: ws_row - 2 */
433 /* exec_command: execute the next command in command buffer; return print
438 static pattern_t *pat = NULL;
439 static int sgflag = 0;
440 static long sgnum = 0;
451 switch(c = *ibufp++) {
453 GET_COMMAND_SUFFIX();
454 if (!isglobal) clear_undo_stack();
455 if (append_lines(second_addr) < 0)
459 if (check_addr_range(current_addr, current_addr) < 0)
461 GET_COMMAND_SUFFIX();
462 if (!isglobal) clear_undo_stack();
463 if (delete_lines(first_addr, second_addr) < 0 ||
464 append_lines(current_addr) < 0)
468 if (check_addr_range(current_addr, current_addr) < 0)
470 GET_COMMAND_SUFFIX();
471 if (!isglobal) clear_undo_stack();
472 if (delete_lines(first_addr, second_addr) < 0)
474 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
478 if (modified && !scripted)
483 errmsg = "unexpected address";
485 } else if (!isspace((unsigned char)*ibufp)) {
486 errmsg = "unexpected command suffix";
488 } else if ((fnp = get_filename()) == NULL)
490 GET_COMMAND_SUFFIX();
491 if (delete_lines(1, addr_last) < 0)
494 if (close_sbuf() < 0)
496 else if (open_sbuf() < 0)
498 if (*fnp && *fnp != '!')
499 strlcpy(old_filename, fnp, PATH_MAX);
501 if (*fnp == '\0' && *old_filename == '\0') {
502 errmsg = "no current filename";
506 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
510 u_current_addr = u_addr_last = -1;
514 errmsg = "unexpected address";
516 } else if (!isspace((unsigned char)*ibufp)) {
517 errmsg = "unexpected command suffix";
519 } else if ((fnp = get_filename()) == NULL)
521 else if (*fnp == '!') {
522 errmsg = "invalid redirection";
525 GET_COMMAND_SUFFIX();
527 strlcpy(old_filename, fnp, PATH_MAX);
528 printf("%s\n", strip_escapes(old_filename));
535 errmsg = "cannot nest global commands";
537 } else if (check_addr_range(1, addr_last) < 0)
539 else if (build_active_list(c == 'g' || c == 'G') < 0)
541 else if ((n = (c == 'G' || c == 'V')))
542 GET_COMMAND_SUFFIX();
544 if (exec_global(n, gflag) < 0)
549 errmsg = "unexpected address";
552 GET_COMMAND_SUFFIX();
553 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
557 errmsg = "unexpected address";
560 GET_COMMAND_SUFFIX();
561 if ((garrulous = 1 - garrulous) && *errmsg)
562 fprintf(stderr, "%s\n", errmsg);
565 if (second_addr == 0) {
566 errmsg = "invalid address";
569 GET_COMMAND_SUFFIX();
570 if (!isglobal) clear_undo_stack();
571 if (append_lines(second_addr - 1) < 0)
575 if (check_addr_range(current_addr, current_addr + 1) < 0)
577 GET_COMMAND_SUFFIX();
578 if (!isglobal) clear_undo_stack();
579 if (first_addr != second_addr &&
580 join_lines(first_addr, second_addr) < 0)
585 if (second_addr == 0) {
586 errmsg = "invalid address";
589 GET_COMMAND_SUFFIX();
590 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
594 if (check_addr_range(current_addr, current_addr) < 0)
596 GET_COMMAND_SUFFIX();
597 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
602 if (check_addr_range(current_addr, current_addr) < 0)
604 GET_THIRD_ADDR(addr);
605 if (first_addr <= addr && addr < second_addr) {
606 errmsg = "invalid destination";
609 GET_COMMAND_SUFFIX();
610 if (!isglobal) clear_undo_stack();
611 if (move_lines(addr) < 0)
615 if (check_addr_range(current_addr, current_addr) < 0)
617 GET_COMMAND_SUFFIX();
618 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
623 if (check_addr_range(current_addr, current_addr) < 0)
625 GET_COMMAND_SUFFIX();
626 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
632 errmsg = "unexpected address";
635 GET_COMMAND_SUFFIX();
636 prompt = prompt ? NULL : optarg ? optarg : dps;
641 errmsg = "unexpected address";
644 GET_COMMAND_SUFFIX();
645 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
648 if (!isspace((unsigned char)*ibufp)) {
649 errmsg = "unexpected command suffix";
651 } else if (addr_cnt == 0)
652 second_addr = addr_last;
653 if ((fnp = get_filename()) == NULL)
655 GET_COMMAND_SUFFIX();
656 if (!isglobal) clear_undo_stack();
657 if (*old_filename == '\0' && *fnp != '!')
658 strlcpy(old_filename, fnp, PATH_MAX);
660 if (*fnp == '\0' && *old_filename == '\0') {
661 errmsg = "no current filename";
665 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
667 else if (addr && addr != addr_last)
688 case '0': case '1': case '2': case '3': case '4':
689 case '5': case '6': case '7': case '8': case '9':
690 STRTOL(sgnum, ibufp);
692 sgflag &= ~GSG; /* override GSG */
696 errmsg = "invalid command suffix";
700 } while (sflags && *ibufp != '\n');
701 if (sflags && !pat) {
702 errmsg = "no previous substitution";
704 } else if (sflags & SGG)
705 sgnum = 0; /* override numeric arg */
706 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
707 errmsg = "invalid pattern delimiter";
712 if ((!sflags || (sflags & SGR)) &&
713 (tpat = get_compiled_pattern()) == NULL) {
716 } else if (tpat != pat) {
722 patlock = 1; /* reserve pattern */
725 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
734 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
738 sgflag |= GPR, ibufp++;
741 sgflag |= GLS, ibufp++;
744 sgflag |= GNP, ibufp++;
750 if (check_addr_range(current_addr, current_addr) < 0)
752 GET_COMMAND_SUFFIX();
753 if (!isglobal) clear_undo_stack();
754 if (search_and_replace(pat, sgflag, sgnum) < 0)
758 if (check_addr_range(current_addr, current_addr) < 0)
760 GET_THIRD_ADDR(addr);
761 GET_COMMAND_SUFFIX();
762 if (!isglobal) clear_undo_stack();
763 if (copy_lines(addr) < 0)
768 errmsg = "unexpected address";
771 GET_COMMAND_SUFFIX();
772 if (pop_undo_stack() < 0)
777 if ((n = *ibufp) == 'q' || n == 'Q') {
781 if (!isspace((unsigned char)*ibufp)) {
782 errmsg = "unexpected command suffix";
784 } else if ((fnp = get_filename()) == NULL)
786 if (addr_cnt == 0 && !addr_last)
787 first_addr = second_addr = 0;
788 else if (check_addr_range(1, addr_last) < 0)
790 GET_COMMAND_SUFFIX();
791 if (*old_filename == '\0' && *fnp != '!')
792 strlcpy(old_filename, fnp, PATH_MAX);
794 if (*fnp == '\0' && *old_filename == '\0') {
795 errmsg = "no current filename";
799 if ((addr = write_file(*fnp ? fnp : old_filename,
800 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
802 else if (addr == addr_last && *fnp != '!')
804 else if (modified && !scripted && n == 'q')
809 errmsg = "unexpected address";
812 GET_COMMAND_SUFFIX();
813 errmsg = "crypt unavailable";
817 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
819 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
822 else if ('0' < *ibufp && *ibufp <= '9')
824 GET_COMMAND_SUFFIX();
825 if (display_lines(second_addr, min(addr_last,
826 second_addr + rows), gflag) < 0)
831 GET_COMMAND_SUFFIX();
832 printf("%ld\n", addr_cnt ? second_addr : addr_last);
836 errmsg = "unexpected address";
838 } else if ((sflags = get_shell_command()) < 0)
840 GET_COMMAND_SUFFIX();
841 if (sflags) printf("%s\n", shcmd + 1);
843 if (!scripted) printf("!\n");
847 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
849 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
851 || display_lines(second_addr, second_addr, 0) < 0)
855 errmsg = "unknown command";
862 /* check_addr_range: return status of address range check */
864 check_addr_range(long n, long m)
870 if (first_addr > second_addr || 1 > first_addr ||
871 second_addr > addr_last) {
872 errmsg = "invalid address";
879 /* get_matching_node_addr: return the address of the next line matching a
880 pattern in a given direction. wrap around begin/end of editor buffer if
883 get_matching_node_addr(pattern_t *pat, int dir)
886 long n = current_addr;
889 if (!pat) return ERR;
891 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
892 lp = get_addressed_line_node(n);
893 if ((s = get_sbuf_line(lp)) == NULL)
896 NUL_TO_NEWLINE(s, lp->len);
897 if (!regexec(pat, s, 0, NULL, 0))
900 } while (n != current_addr);
906 /* get_filename: return pointer to copy of filename in the command buffer */
910 static char *file = NULL;
911 static int filesz = 0;
915 if (*ibufp != '\n') {
917 if (*ibufp == '\n') {
918 errmsg = "invalid filename";
920 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
922 else if (*ibufp == '!') {
924 if ((n = get_shell_command()) < 0)
927 printf("%s\n", shcmd + 1);
929 } else if (n > PATH_MAX - 1) {
930 errmsg = "filename too long";
935 else if (*old_filename == '\0') {
936 errmsg = "no current filename";
940 REALLOC(file, filesz, PATH_MAX, NULL);
941 for (n = 0; *ibufp != '\n';)
942 file[n++] = *ibufp++;
944 return is_legal_filename(file) ? file : NULL;
948 /* get_shell_command: read a shell command from stdin; return substitution
951 get_shell_command(void)
953 static char *buf = NULL;
956 char *s; /* substitution char pointer */
961 errmsg = "shell access restricted";
963 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
965 REALLOC(buf, n, j + 1, ERR);
966 buf[i++] = '!'; /* prefix command w/ bang */
967 while (*ibufp != '\n')
970 REALLOC(buf, n, i + 2, ERR);
972 if (*ibufp++ == '\\')
977 REALLOC(buf, n, i + 1, ERR);
981 else if (shcmd == NULL || *(shcmd + 1) == '\0')
983 else if (shcmd == NULL)
986 errmsg = "no previous command";
989 REALLOC(buf, n, i + shcmdi, ERR);
990 for (s = shcmd + 1; s < shcmd + shcmdi;)
996 if (*old_filename == '\0') {
997 errmsg = "no current filename";
1000 j = strlen(s = strip_escapes(old_filename));
1001 REALLOC(buf, n, i + j, ERR);
1007 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1008 memcpy(shcmd, buf, i);
1009 shcmd[shcmdi = i] = '\0';
1010 return *s == '!' || *s == '%';
1014 /* append_lines: insert text from stdin to after line n; stop when either a
1015 single period is read or EOF; return status */
1017 append_lines(long n)
1020 const char *lp = ibuf;
1024 for (current_addr = n;;) {
1026 if ((l = get_tty_line()) < 0)
1028 else if (l == 0 || ibuf[l - 1] != '\n') {
1033 } else if (*(lp = ibufp) == '\0')
1036 while (*ibufp++ != '\n')
1040 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1046 if ((lp = put_sbuf_line(lp)) == NULL) {
1050 up->t = get_addressed_line_node(current_addr);
1051 else if ((up = push_undo_stack(UADD, current_addr,
1052 current_addr)) == NULL) {
1056 } while (lp != eot);
1064 /* join_lines: replace a range of lines with the joined text of those lines */
1066 join_lines(long from, long to)
1068 static char *buf = NULL;
1075 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1076 bp = get_addressed_line_node(from);
1077 for (; bp != ep; bp = bp->q_forw) {
1078 if ((s = get_sbuf_line(bp)) == NULL)
1080 REALLOC(buf, n, size + bp->len, ERR);
1081 memcpy(buf + size, s, bp->len);
1084 REALLOC(buf, n, size + 2, ERR);
1085 memcpy(buf + size, "\n", 2);
1086 if (delete_lines(from, to) < 0)
1088 current_addr = from - 1;
1090 if (put_sbuf_line(buf) == NULL ||
1091 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1101 /* move_lines: move a range of lines */
1103 move_lines(long addr)
1105 line_t *b1, *a1, *b2, *a2;
1106 long n = INC_MOD(second_addr, addr_last);
1107 long p = first_addr - 1;
1108 int done = (addr == first_addr - 1 || addr == second_addr);
1112 a2 = get_addressed_line_node(n);
1113 b2 = get_addressed_line_node(p);
1114 current_addr = second_addr;
1115 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1116 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1120 a1 = get_addressed_line_node(n);
1121 if (addr < first_addr) {
1122 b1 = get_addressed_line_node(p);
1123 b2 = get_addressed_line_node(addr);
1124 /* this get_addressed_line_node last! */
1126 b2 = get_addressed_line_node(addr);
1127 b1 = get_addressed_line_node(p);
1128 /* this get_addressed_line_node last! */
1131 REQUE(b2, b1->q_forw);
1132 REQUE(a1->q_back, a2);
1134 current_addr = addr + ((addr < first_addr) ?
1135 second_addr - first_addr + 1 : 0);
1138 unset_active_nodes(b2->q_forw, a2);
1145 /* copy_lines: copy a range of lines; return status */
1147 copy_lines(long addr)
1149 line_t *lp, *np = get_addressed_line_node(first_addr);
1151 long n = second_addr - first_addr + 1;
1154 current_addr = addr;
1155 if (first_addr <= addr && addr < second_addr) {
1156 n = addr - first_addr + 1;
1157 m = second_addr - addr;
1159 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1160 for (; n-- > 0; np = np->q_forw) {
1162 if ((lp = dup_line_node(np)) == NULL) {
1169 else if ((up = push_undo_stack(UADD, current_addr,
1170 current_addr)) == NULL) {
1181 /* delete_lines: delete a range of lines */
1183 delete_lines(long from, long to)
1188 if (push_undo_stack(UDEL, from, to) == NULL) {
1192 n = get_addressed_line_node(INC_MOD(to, addr_last));
1193 p = get_addressed_line_node(from - 1);
1194 /* this get_addressed_line_node last! */
1196 unset_active_nodes(p->q_forw, n);
1198 addr_last -= to - from + 1;
1199 current_addr = from - 1;
1206 /* display_lines: print a range of lines to stdout */
1208 display_lines(long from, long to, int gflag)
1215 errmsg = "invalid address";
1218 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1219 bp = get_addressed_line_node(from);
1220 for (; bp != ep; bp = bp->q_forw) {
1221 if ((s = get_sbuf_line(bp)) == NULL)
1223 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1230 #define MAXMARK 26 /* max number of marks */
1232 static line_t *mark[MAXMARK]; /* line markers */
1233 static int markno; /* line marker count */
1235 /* mark_line_node: set a line node mark */
1237 mark_line_node(line_t *lp, int n)
1239 if (!islower((unsigned char)n)) {
1240 errmsg = "invalid mark character";
1242 } else if (mark[n - 'a'] == NULL)
1249 /* get_marked_node_addr: return address of a marked line */
1251 get_marked_node_addr(int n)
1253 if (!islower((unsigned char)n)) {
1254 errmsg = "invalid mark character";
1257 return get_line_node_addr(mark[n - 'a']);
1261 /* unmark_line_node: clear line node mark */
1263 unmark_line_node(line_t *lp)
1267 for (i = 0; markno && i < MAXMARK; i++)
1268 if (mark[i] == lp) {
1275 /* dup_line_node: return a pointer to a copy of a line node */
1277 dup_line_node(line_t *lp)
1281 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1282 fprintf(stderr, "%s\n", strerror(errno));
1283 errmsg = "out of memory";
1286 np->seek = lp->seek;
1292 /* has_trailing_escape: return the parity of escapes preceding a character
1295 has_trailing_escape(char *s, char *t)
1297 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1301 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1303 strip_escapes(char *s)
1305 static char *file = NULL;
1306 static int filesz = 0;
1310 REALLOC(file, filesz, PATH_MAX, NULL);
1311 while (i < filesz - 1 /* Worry about a possible trailing escape */
1312 && (file[i++] = (*s == '\\') ? *++s : *s))
1319 signal_hup(int signo)
1322 sigflags |= (1 << (signo - 1));
1329 signal_int(int signo)
1332 sigflags |= (1 << (signo - 1));
1339 handle_hup(int signo)
1341 char *hup = NULL; /* hup filename */
1343 char ed_hup[] = "ed.hup";
1348 sigflags &= ~(1 << (signo - 1));
1349 if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 &&
1350 (s = getenv("HOME")) != NULL &&
1351 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1352 (hup = (char *) malloc(n + 10)) != NULL) {
1354 if (hup[n - 1] != '/')
1355 hup[n] = '/', hup[n+1] = '\0';
1356 strcat(hup, "ed.hup");
1357 write_file(hup, "w", 1, addr_last);
1364 handle_int(int signo)
1368 sigflags &= ~(1 << (signo - 1));
1369 #ifdef _POSIX_SOURCE
1370 siglongjmp(env, -1);
1377 int cols = 72; /* wrap column */
1380 handle_winch(int signo)
1382 int save_errno = errno;
1384 struct winsize ws; /* window size structure */
1386 sigflags &= ~(1 << (signo - 1));
1387 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1388 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1389 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1395 /* is_legal_filename: return a legal filename */
1397 is_legal_filename(char *s)
1399 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1400 errmsg = "shell access restricted";