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
28 * @(#) Copyright (c) 1993 Andrew Moore, Talke Studio. All rights reserved.
29 * @(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp
30 * $FreeBSD: src/bin/ed/main.c,v 1.14.2.4 2001/08/01 02:36:03 obrien Exp $
31 * $DragonFly: src/bin/ed/main.c,v 1.4 2004/03/19 17:47:48 cpressey Exp $
37 * This program is based on the editor algorithm described in
38 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
39 * in Pascal," Addison-Wesley, 1981.
41 * The buffering algorithm is attributed to Rodney Ruddock of
42 * the University of Guelph, Guelph, Ontario.
44 * The cbc.c encryption code is adapted from
45 * the bdes program by Matt Bishop of Dartmouth College,
50 #include <sys/types.h>
52 #include <sys/ioctl.h>
69 char stdinbuf[1]; /* stdin buffer */
70 char *shcmd; /* shell command buffer */
71 int shcmdsz; /* shell command buffer size */
72 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 int des = 0; /* if set, use crypt(3) for i/o */
79 int garrulous = 0; /* if set, print all error messages */
80 int isbinary; /* if set, buffer contains ASCII NULs */
81 int isglobal; /* if set, doing a global command */
82 int modified; /* if set, buffer modified since last write */
83 int mutex = 0; /* if set, signals set "sigflags" */
84 int red = 0; /* if set, restrict shell/directory access */
85 int scripted = 0; /* if set, suppress diagnostics */
86 int sigflags = 0; /* if set, signals received while mutex set */
87 int sigactive = 0; /* if set, signal handlers are enabled */
89 char old_filename[PATH_MAX] = ""; /* default filename */
90 long current_addr; /* current address in editor buffer */
91 long addr_last; /* last address in editor buffer */
92 int lineno; /* script line number */
93 const char *prompt; /* command-line prompt */
94 const char *dps = "*"; /* default command-line prompt */
96 const char usage[] = "usage: %s [-] [-sx] [-p string] [name]\n";
100 main(int argc, char **argv)
105 /* Avoid longjmp clobbering */
110 (void)setlocale(LC_ALL, "");
112 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
114 while ((c = getopt(argc, argv, "p:sx")) != -1)
116 case 'p': /* set prompt */
119 case 's': /* run script */
122 case 'x': /* use crypt */
126 fprintf(stderr, "crypt unavailable\n?\n");
131 fprintf(stderr, usage, argv[0]);
136 if (argc && **argv == '-') {
145 /* assert: reliable signals! */
147 handle_winch(SIGWINCH);
148 if (isatty(0)) signal(SIGWINCH, handle_winch);
150 signal(SIGHUP, signal_hup);
151 signal(SIGQUIT, SIG_IGN);
152 signal(SIGINT, signal_int);
154 if ((status = sigsetjmp(env, 1)))
156 if ((status = setjmp(env)))
159 fputs("\n?\n", stderr);
160 sprintf(errmsg, "interrupt");
163 sigactive = 1; /* enable signal handlers */
164 if (argc && **argv && is_legal_filename(*argv)) {
165 if (read_file(*argv, 0) < 0 && !isatty(0))
167 else if (**argv != '!')
168 if (strlcpy(old_filename, *argv, sizeof(old_filename))
169 >= sizeof(old_filename))
172 fputs("?\n", stderr);
174 sprintf(errmsg, "invalid filename");
180 if (status < 0 && garrulous)
181 fprintf(stderr, "%s\n", errmsg);
183 printf("%s", prompt);
186 if ((n = get_tty_line()) < 0) {
190 if (modified && !scripted) {
191 fputs("?\n", stderr);
192 sprintf(errmsg, "warning: file modified");
194 fprintf(stderr, garrulous ?
195 "script, line %d: %s\n" :
205 } else if (ibuf[n - 1] != '\n') {
207 sprintf(errmsg, "unexpected end-of-file");
213 if ((status = extract_addr_range()) >= 0 &&
214 (status = exec_command()) >= 0)
216 (status = display_lines(current_addr, current_addr,
224 fputs("?\n", stderr); /* give warning */
225 sprintf(errmsg, "warning: file modified");
227 fprintf(stderr, garrulous ?
228 "script, line %d: %s\n" :
235 fprintf(stderr, garrulous ?
236 "script, line %d: %s\n" : "",
239 fprintf(stderr, garrulous ? "%s\n" : "",
243 fputs("?\n", stderr);
245 fprintf(stderr, garrulous ?
246 "script, line %d: %s\n" : "",
256 long first_addr, second_addr, addr_cnt;
258 /* extract_addr_range: get line addresses from the command buffer until an
259 illegal address is seen; return status */
261 extract_addr_range(void)
266 first_addr = second_addr = current_addr;
267 while ((addr = next_addr()) >= 0) {
269 first_addr = second_addr;
271 if (*ibufp != ',' && *ibufp != ';')
273 else if (*ibufp++ == ';')
276 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
277 first_addr = second_addr;
278 return (addr == ERR) ? ERR : 0;
282 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
284 #define MUST_BE_FIRST() \
285 if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
287 /* next_addr: return the next line address in the command buffer */
292 long addr = current_addr;
298 for (hd = ibufp;; first = 0)
299 switch (c = *ibufp) {
307 if (isdigit((unsigned char)*ibufp)) {
309 addr += (c == '-' || c == '^') ? -n : n;
310 } else if (!isspace((unsigned char)c))
311 addr += (c == '-' || c == '^') ? -1 : 1;
313 case '0': case '1': case '2':
314 case '3': case '4': case '5':
315 case '6': case '7': case '8': case '9':
323 addr = (c == '.') ? current_addr : addr_last;
328 if ((addr = get_matching_node_addr(
329 get_compiled_pattern(), c == '/')) < 0)
331 else if (c == *ibufp)
337 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
346 second_addr = (c == ';') ? current_addr : 1;
354 else if (addr < 0 || addr_last < addr) {
355 sprintf(errmsg, "invalid address");
365 /* GET_THIRD_ADDR: get a legal address from the command buffer */
366 #define GET_THIRD_ADDR(addr) \
370 ol1 = first_addr, ol2 = second_addr; \
371 if (extract_addr_range() < 0) \
373 else if (addr_cnt == 0) { \
374 sprintf(errmsg, "destination expected"); \
376 } else if (second_addr < 0 || addr_last < second_addr) { \
377 sprintf(errmsg, "invalid address"); \
380 addr = second_addr; \
381 first_addr = ol1, second_addr = ol2; \
383 #else /* BACKWARDS */
384 /* GET_THIRD_ADDR: get a legal address from the command buffer */
385 #define GET_THIRD_ADDR(addr) \
389 ol1 = first_addr, ol2 = second_addr; \
390 if (extract_addr_range() < 0) \
392 if (second_addr < 0 || addr_last < second_addr) { \
393 sprintf(errmsg, "invalid address"); \
396 addr = second_addr; \
397 first_addr = ol1, second_addr = ol2; \
402 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
403 #define GET_COMMAND_SUFFIX() { \
408 gflag |= GPR, ibufp++; \
411 gflag |= GLS, ibufp++; \
414 gflag |= GNP, ibufp++; \
420 if (*ibufp++ != '\n') { \
421 sprintf(errmsg, "invalid command suffix"); \
428 #define SGG 001 /* complement previous global substitute suffix */
429 #define SGP 002 /* complement previous print suffix */
430 #define SGR 004 /* use last regex instead of last pat */
431 #define SGF 010 /* repeat last substitution */
433 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
435 long rows = 22; /* scroll length: ws_row - 2 */
437 /* exec_command: execute the next command in command buffer; return print
442 extern long u_current_addr;
443 extern long u_addr_last;
445 static pattern_t *pat = NULL;
446 static int sgflag = 0;
447 static long sgnum = 0;
458 switch(c = *ibufp++) {
460 GET_COMMAND_SUFFIX();
461 if (!isglobal) clear_undo_stack();
462 if (append_lines(second_addr) < 0)
466 if (check_addr_range(current_addr, current_addr) < 0)
468 GET_COMMAND_SUFFIX();
469 if (!isglobal) clear_undo_stack();
470 if (delete_lines(first_addr, second_addr) < 0 ||
471 append_lines(current_addr) < 0)
475 if (check_addr_range(current_addr, current_addr) < 0)
477 GET_COMMAND_SUFFIX();
478 if (!isglobal) clear_undo_stack();
479 if (delete_lines(first_addr, second_addr) < 0)
481 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
485 if (modified && !scripted)
490 sprintf(errmsg, "unexpected address");
492 } else if (!isspace((unsigned char)*ibufp)) {
493 sprintf(errmsg, "unexpected command suffix");
495 } else if ((fnp = get_filename()) == NULL)
497 GET_COMMAND_SUFFIX();
498 if (delete_lines(1, addr_last) < 0)
501 if (close_sbuf() < 0)
503 else if (open_sbuf() < 0)
505 if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
507 if (*fnp == '\0' && *old_filename == '\0') {
508 sprintf(errmsg, "no current filename");
512 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
516 u_current_addr = u_addr_last = -1;
520 sprintf(errmsg, "unexpected address");
522 } else if (!isspace((unsigned char)*ibufp)) {
523 sprintf(errmsg, "unexpected command suffix");
525 } else if ((fnp = get_filename()) == NULL)
527 else if (*fnp == '!') {
528 sprintf(errmsg, "invalid redirection");
531 GET_COMMAND_SUFFIX();
532 if (*fnp) strcpy(old_filename, fnp);
533 printf("%s\n", strip_escapes(old_filename));
540 sprintf(errmsg, "cannot nest global commands");
542 } else if (check_addr_range(1, addr_last) < 0)
544 else if (build_active_list(c == 'g' || c == 'G') < 0)
546 else if ((n = (c == 'G' || c == 'V')))
547 GET_COMMAND_SUFFIX();
549 if (exec_global(n, gflag) < 0)
554 sprintf(errmsg, "unexpected address");
557 GET_COMMAND_SUFFIX();
558 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
562 sprintf(errmsg, "unexpected address");
565 GET_COMMAND_SUFFIX();
566 if ((garrulous = 1 - garrulous) && *errmsg)
567 fprintf(stderr, "%s\n", errmsg);
570 if (second_addr == 0) {
571 sprintf(errmsg, "invalid address");
574 GET_COMMAND_SUFFIX();
575 if (!isglobal) clear_undo_stack();
576 if (append_lines(second_addr - 1) < 0)
580 if (check_addr_range(current_addr, current_addr + 1) < 0)
582 GET_COMMAND_SUFFIX();
583 if (!isglobal) clear_undo_stack();
584 if (first_addr != second_addr &&
585 join_lines(first_addr, second_addr) < 0)
590 if (second_addr == 0) {
591 sprintf(errmsg, "invalid address");
594 GET_COMMAND_SUFFIX();
595 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
599 if (check_addr_range(current_addr, current_addr) < 0)
601 GET_COMMAND_SUFFIX();
602 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
607 if (check_addr_range(current_addr, current_addr) < 0)
609 GET_THIRD_ADDR(addr);
610 if (first_addr <= addr && addr < second_addr) {
611 sprintf(errmsg, "invalid destination");
614 GET_COMMAND_SUFFIX();
615 if (!isglobal) clear_undo_stack();
616 if (move_lines(addr) < 0)
620 if (check_addr_range(current_addr, current_addr) < 0)
622 GET_COMMAND_SUFFIX();
623 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
628 if (check_addr_range(current_addr, current_addr) < 0)
630 GET_COMMAND_SUFFIX();
631 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
637 sprintf(errmsg, "unexpected address");
640 GET_COMMAND_SUFFIX();
641 prompt = prompt ? NULL : optarg ? optarg : dps;
646 sprintf(errmsg, "unexpected address");
649 GET_COMMAND_SUFFIX();
650 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
653 if (!isspace((unsigned char)*ibufp)) {
654 sprintf(errmsg, "unexpected command suffix");
656 } else if (addr_cnt == 0)
657 second_addr = addr_last;
658 if ((fnp = get_filename()) == NULL)
660 GET_COMMAND_SUFFIX();
661 if (!isglobal) clear_undo_stack();
662 if (*old_filename == '\0' && *fnp != '!')
663 strcpy(old_filename, fnp);
665 if (*fnp == '\0' && *old_filename == '\0') {
666 sprintf(errmsg, "no current filename");
670 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
672 else if (addr && addr != addr_last)
693 case '0': case '1': case '2': case '3': case '4':
694 case '5': case '6': case '7': case '8': case '9':
695 STRTOL(sgnum, ibufp);
697 sgflag &= ~GSG; /* override GSG */
701 sprintf(errmsg, "invalid command suffix");
705 } while (sflags && *ibufp != '\n');
706 if (sflags && !pat) {
707 sprintf(errmsg, "no previous substitution");
709 } else if (sflags & SGG)
710 sgnum = 0; /* override numeric arg */
711 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
712 sprintf(errmsg, "invalid pattern delimiter");
717 if ((!sflags || (sflags & SGR)) &&
718 (tpat = get_compiled_pattern()) == NULL) {
721 } else if (tpat != pat) {
727 patlock = 1; /* reserve pattern */
730 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
739 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
743 sgflag |= GPR, ibufp++;
746 sgflag |= GLS, ibufp++;
749 sgflag |= GNP, ibufp++;
755 if (check_addr_range(current_addr, current_addr) < 0)
757 GET_COMMAND_SUFFIX();
758 if (!isglobal) clear_undo_stack();
759 if (search_and_replace(pat, sgflag, sgnum) < 0)
763 if (check_addr_range(current_addr, current_addr) < 0)
765 GET_THIRD_ADDR(addr);
766 GET_COMMAND_SUFFIX();
767 if (!isglobal) clear_undo_stack();
768 if (copy_lines(addr) < 0)
773 sprintf(errmsg, "unexpected address");
776 GET_COMMAND_SUFFIX();
777 if (pop_undo_stack() < 0)
782 if ((n = *ibufp) == 'q' || n == 'Q') {
786 if (!isspace((unsigned char)*ibufp)) {
787 sprintf(errmsg, "unexpected command suffix");
789 } else if ((fnp = get_filename()) == NULL)
791 if (addr_cnt == 0 && !addr_last)
792 first_addr = second_addr = 0;
793 else if (check_addr_range(1, addr_last) < 0)
795 GET_COMMAND_SUFFIX();
796 if (*old_filename == '\0' && *fnp != '!')
797 strcpy(old_filename, fnp);
799 if (*fnp == '\0' && *old_filename == '\0') {
800 sprintf(errmsg, "no current filename");
804 if ((addr = write_file(*fnp ? fnp : old_filename,
805 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
807 else if (addr == addr_last)
809 else if (modified && !scripted && n == 'q')
814 sprintf(errmsg, "unexpected address");
817 GET_COMMAND_SUFFIX();
821 sprintf(errmsg, "crypt unavailable");
827 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
829 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
832 else if ('0' < *ibufp && *ibufp <= '9')
834 GET_COMMAND_SUFFIX();
835 if (display_lines(second_addr, min(addr_last,
836 second_addr + rows), gflag) < 0)
841 GET_COMMAND_SUFFIX();
842 printf("%ld\n", addr_cnt ? second_addr : addr_last);
846 sprintf(errmsg, "unexpected address");
848 } else if ((sflags = get_shell_command()) < 0)
850 GET_COMMAND_SUFFIX();
851 if (sflags) printf("%s\n", shcmd + 1);
853 if (!scripted) printf("!\n");
857 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
859 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
861 || display_lines(second_addr, second_addr, 0) < 0)
865 sprintf(errmsg, "unknown command");
872 /* check_addr_range: return status of address range check */
874 check_addr_range(long n, long m)
880 if (first_addr > second_addr || 1 > first_addr ||
881 second_addr > addr_last) {
882 sprintf(errmsg, "invalid address");
889 /* get_matching_node_addr: return the address of the next line matching a
890 pattern in a given direction. wrap around begin/end of editor buffer if
893 get_matching_node_addr(pattern_t *pat, int dir)
896 long n = current_addr;
899 if (!pat) return ERR;
901 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
902 lp = get_addressed_line_node(n);
903 if ((s = get_sbuf_line(lp)) == NULL)
906 NUL_TO_NEWLINE(s, lp->len);
907 if (!regexec(pat, s, 0, NULL, 0))
910 } while (n != current_addr);
911 sprintf(errmsg, "no match");
916 /* get_filename: return pointer to copy of filename in the command buffer */
920 static char *file = NULL;
921 static int filesz = 0;
925 if (*ibufp != '\n') {
927 if (*ibufp == '\n') {
928 sprintf(errmsg, "invalid filename");
930 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
932 else if (*ibufp == '!') {
934 if ((n = get_shell_command()) < 0)
937 printf("%s\n", shcmd + 1);
939 } else if (n > PATH_MAX - 1) {
940 sprintf(errmsg, "filename too long");
945 else if (*old_filename == '\0') {
946 sprintf(errmsg, "no current filename");
950 REALLOC(file, filesz, PATH_MAX, NULL);
951 for (n = 0; *ibufp != '\n';)
952 file[n++] = *ibufp++;
954 return is_legal_filename(file) ? file : NULL;
958 /* get_shell_command: read a shell command from stdin; return substitution
961 get_shell_command(void)
963 static char *buf = NULL;
966 char *s; /* substitution char pointer */
971 sprintf(errmsg, "shell access restricted");
973 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
975 REALLOC(buf, n, j + 1, ERR);
976 buf[i++] = '!'; /* prefix command w/ bang */
977 while (*ibufp != '\n')
980 REALLOC(buf, n, i + 2, ERR);
982 if (*ibufp++ == '\\')
987 REALLOC(buf, n, i + 1, ERR);
991 else if (shcmd == NULL || *(shcmd + 1) == '\0')
993 else if (shcmd == NULL)
996 sprintf(errmsg, "no previous command");
999 REALLOC(buf, n, i + shcmdi, ERR);
1000 for (s = shcmd + 1; s < shcmd + shcmdi;)
1006 if (*old_filename == '\0') {
1007 sprintf(errmsg, "no current filename");
1010 j = strlen(s = strip_escapes(old_filename));
1011 REALLOC(buf, n, i + j, ERR);
1017 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1018 memcpy(shcmd, buf, i);
1019 shcmd[shcmdi = i] = '\0';
1020 return *s == '!' || *s == '%';
1024 /* append_lines: insert text from stdin to after line n; stop when either a
1025 single period is read or EOF; return status */
1027 append_lines(long n)
1034 for (current_addr = n;;) {
1036 if ((l = get_tty_line()) < 0)
1038 else if (l == 0 || ibuf[l - 1] != '\n') {
1043 } else if (*(lp = ibufp) == '\0')
1046 while (*ibufp++ != '\n')
1050 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1056 if ((lp = put_sbuf_line(lp)) == NULL) {
1060 up->t = get_addressed_line_node(current_addr);
1061 else if ((up = push_undo_stack(UADD, current_addr,
1062 current_addr)) == NULL) {
1066 } while (lp != eot);
1074 /* join_lines: replace a range of lines with the joined text of those lines */
1076 join_lines(long from, long to)
1078 static char *buf = NULL;
1085 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1086 bp = get_addressed_line_node(from);
1087 for (; bp != ep; bp = bp->q_forw) {
1088 if ((s = get_sbuf_line(bp)) == NULL)
1090 REALLOC(buf, n, size + bp->len, ERR);
1091 memcpy(buf + size, s, bp->len);
1094 REALLOC(buf, n, size + 2, ERR);
1095 memcpy(buf + size, "\n", 2);
1096 if (delete_lines(from, to) < 0)
1098 current_addr = from - 1;
1100 if (put_sbuf_line(buf) == NULL ||
1101 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1111 /* move_lines: move a range of lines */
1113 move_lines(long addr)
1115 line_t *b1, *a1, *b2, *a2;
1116 long n = INC_MOD(second_addr, addr_last);
1117 long p = first_addr - 1;
1118 int done = (addr == first_addr - 1 || addr == second_addr);
1122 a2 = get_addressed_line_node(n);
1123 b2 = get_addressed_line_node(p);
1124 current_addr = second_addr;
1125 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1126 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1130 a1 = get_addressed_line_node(n);
1131 if (addr < first_addr) {
1132 b1 = get_addressed_line_node(p);
1133 b2 = get_addressed_line_node(addr);
1134 /* this get_addressed_line_node last! */
1136 b2 = get_addressed_line_node(addr);
1137 b1 = get_addressed_line_node(p);
1138 /* this get_addressed_line_node last! */
1141 REQUE(b2, b1->q_forw);
1142 REQUE(a1->q_back, a2);
1144 current_addr = addr + ((addr < first_addr) ?
1145 second_addr - first_addr + 1 : 0);
1148 unset_active_nodes(b2->q_forw, a2);
1155 /* copy_lines: copy a range of lines; return status */
1157 copy_lines(long addr)
1159 line_t *lp, *np = get_addressed_line_node(first_addr);
1161 long n = second_addr - first_addr + 1;
1164 current_addr = addr;
1165 if (first_addr <= addr && addr < second_addr) {
1166 n = addr - first_addr + 1;
1167 m = second_addr - addr;
1169 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1170 for (; n-- > 0; np = np->q_forw) {
1172 if ((lp = dup_line_node(np)) == NULL) {
1179 else if ((up = push_undo_stack(UADD, current_addr,
1180 current_addr)) == NULL) {
1191 /* delete_lines: delete a range of lines */
1193 delete_lines(long from, long to)
1198 if (push_undo_stack(UDEL, from, to) == NULL) {
1202 n = get_addressed_line_node(INC_MOD(to, addr_last));
1203 p = get_addressed_line_node(from - 1);
1204 /* this get_addressed_line_node last! */
1206 unset_active_nodes(p->q_forw, n);
1208 addr_last -= to - from + 1;
1209 current_addr = from - 1;
1216 /* display_lines: print a range of lines to stdout */
1218 display_lines(long from, long to, int gflag)
1225 sprintf(errmsg, "invalid address");
1228 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1229 bp = get_addressed_line_node(from);
1230 for (; bp != ep; bp = bp->q_forw) {
1231 if ((s = get_sbuf_line(bp)) == NULL)
1233 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1240 #define MAXMARK 26 /* max number of marks */
1242 line_t *mark[MAXMARK]; /* line markers */
1243 int markno; /* line marker count */
1245 /* mark_line_node: set a line node mark */
1247 mark_line_node(line_t *lp, int n)
1249 if (!islower((unsigned char)n)) {
1250 sprintf(errmsg, "invalid mark character");
1252 } else if (mark[n - 'a'] == NULL)
1259 /* get_marked_node_addr: return address of a marked line */
1261 get_marked_node_addr(int n)
1263 if (!islower((unsigned char)n)) {
1264 sprintf(errmsg, "invalid mark character");
1267 return get_line_node_addr(mark[n - 'a']);
1271 /* unmark_line_node: clear line node mark */
1273 unmark_line_node(line_t *lp)
1277 for (i = 0; markno && i < MAXMARK; i++)
1278 if (mark[i] == lp) {
1285 /* dup_line_node: return a pointer to a copy of a line node */
1287 dup_line_node(line_t *lp)
1291 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1292 fprintf(stderr, "%s\n", strerror(errno));
1293 sprintf(errmsg, "out of memory");
1296 np->seek = lp->seek;
1302 /* has_trailing_escape: return the parity of escapes preceding a character
1305 has_trailing_escape(char *s, char *t)
1307 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1311 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1313 strip_escapes(const char *s)
1315 static char *file = NULL;
1316 static int filesz = 0;
1320 REALLOC(file, filesz, PATH_MAX, NULL);
1321 while (i < filesz - 1 /* Worry about a possible trailing escape */
1322 && (file[i++] = (*s == '\\') ? *++s : *s))
1329 signal_hup(int signo)
1332 sigflags |= (1 << (signo - 1));
1339 signal_int(int signo)
1342 sigflags |= (1 << (signo - 1));
1349 handle_hup(int signo)
1351 char *hup = NULL; /* hup filename */
1357 sigflags &= ~(1 << (signo - 1));
1358 if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
1359 (s = getenv("HOME")) != NULL &&
1360 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1361 (hup = (char *) malloc(n + 10)) != NULL) {
1363 if (hup[n - 1] != '/')
1364 hup[n] = '/', hup[n+1] = '\0';
1365 strcat(hup, "ed.hup");
1366 write_file(hup, "w", 1, addr_last);
1373 handle_int(int signo)
1377 sigflags &= ~(1 << (signo - 1));
1378 #ifdef _POSIX_SOURCE
1379 siglongjmp(env, -1);
1386 int cols = 72; /* wrap column */
1389 handle_winch(int signo)
1391 int save_errno = errno;
1393 struct winsize ws; /* window size structure */
1395 sigflags &= ~(1 << (signo - 1));
1396 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1397 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1398 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1404 /* is_legal_filename: return a legal filename */
1406 is_legal_filename(char *s)
1408 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1409 sprintf(errmsg, "shell access restricted");