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.2 2003/06/17 04:22:49 dillon 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 char *prompt; /* command-line prompt */
94 char *dps = "*"; /* default command-line prompt */
96 const char usage[] = "usage: %s [-] [-sx] [-p string] [name]\n";
107 /* Avoid longjmp clobbering */
112 (void)setlocale(LC_ALL, "");
114 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
116 while ((c = getopt(argc, argv, "p:sx")) != -1)
118 case 'p': /* set prompt */
121 case 's': /* run script */
124 case 'x': /* use crypt */
128 fprintf(stderr, "crypt unavailable\n?\n");
133 fprintf(stderr, usage, argv[0]);
138 if (argc && **argv == '-') {
147 /* assert: reliable signals! */
149 handle_winch(SIGWINCH);
150 if (isatty(0)) signal(SIGWINCH, handle_winch);
152 signal(SIGHUP, signal_hup);
153 signal(SIGQUIT, SIG_IGN);
154 signal(SIGINT, signal_int);
156 if ((status = sigsetjmp(env, 1)))
158 if ((status = setjmp(env)))
161 fputs("\n?\n", stderr);
162 sprintf(errmsg, "interrupt");
165 sigactive = 1; /* enable signal handlers */
166 if (argc && **argv && is_legal_filename(*argv)) {
167 if (read_file(*argv, 0) < 0 && !isatty(0))
169 else if (**argv != '!')
170 if (strlcpy(old_filename, *argv, sizeof(old_filename))
171 >= sizeof(old_filename))
174 fputs("?\n", stderr);
176 sprintf(errmsg, "invalid filename");
182 if (status < 0 && garrulous)
183 fprintf(stderr, "%s\n", errmsg);
185 printf("%s", prompt);
188 if ((n = get_tty_line()) < 0) {
192 if (modified && !scripted) {
193 fputs("?\n", stderr);
194 sprintf(errmsg, "warning: file modified");
196 fprintf(stderr, garrulous ?
197 "script, line %d: %s\n" :
207 } else if (ibuf[n - 1] != '\n') {
209 sprintf(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 sprintf(errmsg, "warning: file modified");
229 fprintf(stderr, garrulous ?
230 "script, line %d: %s\n" :
237 fprintf(stderr, garrulous ?
238 "script, line %d: %s\n" : "",
241 fprintf(stderr, garrulous ? "%s\n" : "",
245 fputs("?\n", stderr);
247 fprintf(stderr, garrulous ?
248 "script, line %d: %s\n" : "",
258 long first_addr, second_addr, addr_cnt;
260 /* extract_addr_range: get line addresses from the command buffer until an
261 illegal address is seen; return status */
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() \
287 if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
289 /* next_addr: return the next line address in the command buffer */
294 long addr = current_addr;
300 for (hd = ibufp;; first = 0)
301 switch (c = *ibufp) {
309 if (isdigit((unsigned char)*ibufp)) {
311 addr += (c == '-' || c == '^') ? -n : n;
312 } else if (!isspace((unsigned char)c))
313 addr += (c == '-' || c == '^') ? -1 : 1;
315 case '0': case '1': case '2':
316 case '3': case '4': case '5':
317 case '6': case '7': case '8': case '9':
325 addr = (c == '.') ? current_addr : addr_last;
330 if ((addr = get_matching_node_addr(
331 get_compiled_pattern(), c == '/')) < 0)
333 else if (c == *ibufp)
339 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
348 second_addr = (c == ';') ? current_addr : 1;
356 else if (addr < 0 || addr_last < addr) {
357 sprintf(errmsg, "invalid address");
367 /* GET_THIRD_ADDR: get a legal address from the command buffer */
368 #define GET_THIRD_ADDR(addr) \
372 ol1 = first_addr, ol2 = second_addr; \
373 if (extract_addr_range() < 0) \
375 else if (addr_cnt == 0) { \
376 sprintf(errmsg, "destination expected"); \
378 } else if (second_addr < 0 || addr_last < second_addr) { \
379 sprintf(errmsg, "invalid address"); \
382 addr = second_addr; \
383 first_addr = ol1, second_addr = ol2; \
385 #else /* BACKWARDS */
386 /* GET_THIRD_ADDR: get a legal address from the command buffer */
387 #define GET_THIRD_ADDR(addr) \
391 ol1 = first_addr, ol2 = second_addr; \
392 if (extract_addr_range() < 0) \
394 if (second_addr < 0 || addr_last < second_addr) { \
395 sprintf(errmsg, "invalid address"); \
398 addr = second_addr; \
399 first_addr = ol1, second_addr = ol2; \
404 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
405 #define GET_COMMAND_SUFFIX() { \
410 gflag |= GPR, ibufp++; \
413 gflag |= GLS, ibufp++; \
416 gflag |= GNP, ibufp++; \
422 if (*ibufp++ != '\n') { \
423 sprintf(errmsg, "invalid command suffix"); \
430 #define SGG 001 /* complement previous global substitute suffix */
431 #define SGP 002 /* complement previous print suffix */
432 #define SGR 004 /* use last regex instead of last pat */
433 #define SGF 010 /* repeat last substitution */
435 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
437 long rows = 22; /* scroll length: ws_row - 2 */
439 /* exec_command: execute the next command in command buffer; return print
444 extern long u_current_addr;
445 extern long u_addr_last;
447 static pattern_t *pat = NULL;
448 static int sgflag = 0;
449 static long sgnum = 0;
460 switch(c = *ibufp++) {
462 GET_COMMAND_SUFFIX();
463 if (!isglobal) clear_undo_stack();
464 if (append_lines(second_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 ||
473 append_lines(current_addr) < 0)
477 if (check_addr_range(current_addr, current_addr) < 0)
479 GET_COMMAND_SUFFIX();
480 if (!isglobal) clear_undo_stack();
481 if (delete_lines(first_addr, second_addr) < 0)
483 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
487 if (modified && !scripted)
492 sprintf(errmsg, "unexpected address");
494 } else if (!isspace((unsigned char)*ibufp)) {
495 sprintf(errmsg, "unexpected command suffix");
497 } else if ((fnp = get_filename()) == NULL)
499 GET_COMMAND_SUFFIX();
500 if (delete_lines(1, addr_last) < 0)
503 if (close_sbuf() < 0)
505 else if (open_sbuf() < 0)
507 if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
509 if (*fnp == '\0' && *old_filename == '\0') {
510 sprintf(errmsg, "no current filename");
514 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
518 u_current_addr = u_addr_last = -1;
522 sprintf(errmsg, "unexpected address");
524 } else if (!isspace((unsigned char)*ibufp)) {
525 sprintf(errmsg, "unexpected command suffix");
527 } else if ((fnp = get_filename()) == NULL)
529 else if (*fnp == '!') {
530 sprintf(errmsg, "invalid redirection");
533 GET_COMMAND_SUFFIX();
534 if (*fnp) strcpy(old_filename, fnp);
535 printf("%s\n", strip_escapes(old_filename));
542 sprintf(errmsg, "cannot nest global commands");
544 } else if (check_addr_range(1, addr_last) < 0)
546 else if (build_active_list(c == 'g' || c == 'G') < 0)
548 else if ((n = (c == 'G' || c == 'V')))
549 GET_COMMAND_SUFFIX();
551 if (exec_global(n, gflag) < 0)
556 sprintf(errmsg, "unexpected address");
559 GET_COMMAND_SUFFIX();
560 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
564 sprintf(errmsg, "unexpected address");
567 GET_COMMAND_SUFFIX();
568 if ((garrulous = 1 - garrulous) && *errmsg)
569 fprintf(stderr, "%s\n", errmsg);
572 if (second_addr == 0) {
573 sprintf(errmsg, "invalid address");
576 GET_COMMAND_SUFFIX();
577 if (!isglobal) clear_undo_stack();
578 if (append_lines(second_addr - 1) < 0)
582 if (check_addr_range(current_addr, current_addr + 1) < 0)
584 GET_COMMAND_SUFFIX();
585 if (!isglobal) clear_undo_stack();
586 if (first_addr != second_addr &&
587 join_lines(first_addr, second_addr) < 0)
592 if (second_addr == 0) {
593 sprintf(errmsg, "invalid address");
596 GET_COMMAND_SUFFIX();
597 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
601 if (check_addr_range(current_addr, current_addr) < 0)
603 GET_COMMAND_SUFFIX();
604 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
609 if (check_addr_range(current_addr, current_addr) < 0)
611 GET_THIRD_ADDR(addr);
612 if (first_addr <= addr && addr < second_addr) {
613 sprintf(errmsg, "invalid destination");
616 GET_COMMAND_SUFFIX();
617 if (!isglobal) clear_undo_stack();
618 if (move_lines(addr) < 0)
622 if (check_addr_range(current_addr, current_addr) < 0)
624 GET_COMMAND_SUFFIX();
625 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
630 if (check_addr_range(current_addr, current_addr) < 0)
632 GET_COMMAND_SUFFIX();
633 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
639 sprintf(errmsg, "unexpected address");
642 GET_COMMAND_SUFFIX();
643 prompt = prompt ? NULL : optarg ? optarg : dps;
648 sprintf(errmsg, "unexpected address");
651 GET_COMMAND_SUFFIX();
652 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
655 if (!isspace((unsigned char)*ibufp)) {
656 sprintf(errmsg, "unexpected command suffix");
658 } else if (addr_cnt == 0)
659 second_addr = addr_last;
660 if ((fnp = get_filename()) == NULL)
662 GET_COMMAND_SUFFIX();
663 if (!isglobal) clear_undo_stack();
664 if (*old_filename == '\0' && *fnp != '!')
665 strcpy(old_filename, fnp);
667 if (*fnp == '\0' && *old_filename == '\0') {
668 sprintf(errmsg, "no current filename");
672 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
674 else if (addr && addr != addr_last)
695 case '0': case '1': case '2': case '3': case '4':
696 case '5': case '6': case '7': case '8': case '9':
697 STRTOL(sgnum, ibufp);
699 sgflag &= ~GSG; /* override GSG */
703 sprintf(errmsg, "invalid command suffix");
707 } while (sflags && *ibufp != '\n');
708 if (sflags && !pat) {
709 sprintf(errmsg, "no previous substitution");
711 } else if (sflags & SGG)
712 sgnum = 0; /* override numeric arg */
713 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
714 sprintf(errmsg, "invalid pattern delimiter");
719 if ((!sflags || (sflags & SGR)) &&
720 (tpat = get_compiled_pattern()) == NULL) {
723 } else if (tpat != pat) {
729 patlock = 1; /* reserve pattern */
732 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
741 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
745 sgflag |= GPR, ibufp++;
748 sgflag |= GLS, ibufp++;
751 sgflag |= GNP, ibufp++;
757 if (check_addr_range(current_addr, current_addr) < 0)
759 GET_COMMAND_SUFFIX();
760 if (!isglobal) clear_undo_stack();
761 if (search_and_replace(pat, sgflag, sgnum) < 0)
765 if (check_addr_range(current_addr, current_addr) < 0)
767 GET_THIRD_ADDR(addr);
768 GET_COMMAND_SUFFIX();
769 if (!isglobal) clear_undo_stack();
770 if (copy_lines(addr) < 0)
775 sprintf(errmsg, "unexpected address");
778 GET_COMMAND_SUFFIX();
779 if (pop_undo_stack() < 0)
784 if ((n = *ibufp) == 'q' || n == 'Q') {
788 if (!isspace((unsigned char)*ibufp)) {
789 sprintf(errmsg, "unexpected command suffix");
791 } else if ((fnp = get_filename()) == NULL)
793 if (addr_cnt == 0 && !addr_last)
794 first_addr = second_addr = 0;
795 else if (check_addr_range(1, addr_last) < 0)
797 GET_COMMAND_SUFFIX();
798 if (*old_filename == '\0' && *fnp != '!')
799 strcpy(old_filename, fnp);
801 if (*fnp == '\0' && *old_filename == '\0') {
802 sprintf(errmsg, "no current filename");
806 if ((addr = write_file(*fnp ? fnp : old_filename,
807 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
809 else if (addr == addr_last)
811 else if (modified && !scripted && n == 'q')
816 sprintf(errmsg, "unexpected address");
819 GET_COMMAND_SUFFIX();
823 sprintf(errmsg, "crypt unavailable");
829 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
831 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
834 else if ('0' < *ibufp && *ibufp <= '9')
836 GET_COMMAND_SUFFIX();
837 if (display_lines(second_addr, min(addr_last,
838 second_addr + rows), gflag) < 0)
843 GET_COMMAND_SUFFIX();
844 printf("%ld\n", addr_cnt ? second_addr : addr_last);
848 sprintf(errmsg, "unexpected address");
850 } else if ((sflags = get_shell_command()) < 0)
852 GET_COMMAND_SUFFIX();
853 if (sflags) printf("%s\n", shcmd + 1);
855 if (!scripted) printf("!\n");
859 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
861 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
863 || display_lines(second_addr, second_addr, 0) < 0)
867 sprintf(errmsg, "unknown command");
874 /* check_addr_range: return status of address range check */
876 check_addr_range(n, m)
883 if (first_addr > second_addr || 1 > first_addr ||
884 second_addr > addr_last) {
885 sprintf(errmsg, "invalid address");
892 /* get_matching_node_addr: return the address of the next line matching a
893 pattern in a given direction. wrap around begin/end of editor buffer if
896 get_matching_node_addr(pat, dir)
901 long n = current_addr;
904 if (!pat) return ERR;
906 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
907 lp = get_addressed_line_node(n);
908 if ((s = get_sbuf_line(lp)) == NULL)
911 NUL_TO_NEWLINE(s, lp->len);
912 if (!regexec(pat, s, 0, NULL, 0))
915 } while (n != current_addr);
916 sprintf(errmsg, "no match");
921 /* get_filename: return pointer to copy of filename in the command buffer */
925 static char *file = NULL;
926 static int filesz = 0;
930 if (*ibufp != '\n') {
932 if (*ibufp == '\n') {
933 sprintf(errmsg, "invalid filename");
935 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
937 else if (*ibufp == '!') {
939 if ((n = get_shell_command()) < 0)
942 printf("%s\n", shcmd + 1);
944 } else if (n > PATH_MAX - 1) {
945 sprintf(errmsg, "filename too long");
950 else if (*old_filename == '\0') {
951 sprintf(errmsg, "no current filename");
955 REALLOC(file, filesz, PATH_MAX, NULL);
956 for (n = 0; *ibufp != '\n';)
957 file[n++] = *ibufp++;
959 return is_legal_filename(file) ? file : NULL;
963 /* get_shell_command: read a shell command from stdin; return substitution
968 static char *buf = NULL;
971 char *s; /* substitution char pointer */
976 sprintf(errmsg, "shell access restricted");
978 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
980 REALLOC(buf, n, j + 1, ERR);
981 buf[i++] = '!'; /* prefix command w/ bang */
982 while (*ibufp != '\n')
985 REALLOC(buf, n, i + 2, ERR);
987 if (*ibufp++ == '\\')
992 REALLOC(buf, n, i + 1, ERR);
996 else if (shcmd == NULL || *(shcmd + 1) == '\0')
998 else if (shcmd == NULL)
1001 sprintf(errmsg, "no previous command");
1004 REALLOC(buf, n, i + shcmdi, ERR);
1005 for (s = shcmd + 1; s < shcmd + shcmdi;)
1011 if (*old_filename == '\0') {
1012 sprintf(errmsg, "no current filename");
1015 j = strlen(s = strip_escapes(old_filename));
1016 REALLOC(buf, n, i + j, ERR);
1022 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1023 memcpy(shcmd, buf, i);
1024 shcmd[shcmdi = i] = '\0';
1025 return *s == '!' || *s == '%';
1029 /* append_lines: insert text from stdin to after line n; stop when either a
1030 single period is read or EOF; return status */
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(from, to)
1086 static char *buf = NULL;
1093 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1094 bp = get_addressed_line_node(from);
1095 for (; bp != ep; bp = bp->q_forw) {
1096 if ((s = get_sbuf_line(bp)) == NULL)
1098 REALLOC(buf, n, size + bp->len, ERR);
1099 memcpy(buf + size, s, bp->len);
1102 REALLOC(buf, n, size + 2, ERR);
1103 memcpy(buf + size, "\n", 2);
1104 if (delete_lines(from, to) < 0)
1106 current_addr = from - 1;
1108 if (put_sbuf_line(buf) == NULL ||
1109 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1119 /* move_lines: move a range of lines */
1124 line_t *b1, *a1, *b2, *a2;
1125 long n = INC_MOD(second_addr, addr_last);
1126 long p = first_addr - 1;
1127 int done = (addr == first_addr - 1 || addr == second_addr);
1131 a2 = get_addressed_line_node(n);
1132 b2 = get_addressed_line_node(p);
1133 current_addr = second_addr;
1134 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1135 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1139 a1 = get_addressed_line_node(n);
1140 if (addr < first_addr) {
1141 b1 = get_addressed_line_node(p);
1142 b2 = get_addressed_line_node(addr);
1143 /* this get_addressed_line_node last! */
1145 b2 = get_addressed_line_node(addr);
1146 b1 = get_addressed_line_node(p);
1147 /* this get_addressed_line_node last! */
1150 REQUE(b2, b1->q_forw);
1151 REQUE(a1->q_back, a2);
1153 current_addr = addr + ((addr < first_addr) ?
1154 second_addr - first_addr + 1 : 0);
1157 unset_active_nodes(b2->q_forw, a2);
1164 /* copy_lines: copy a range of lines; return status */
1169 line_t *lp, *np = get_addressed_line_node(first_addr);
1171 long n = second_addr - first_addr + 1;
1174 current_addr = addr;
1175 if (first_addr <= addr && addr < second_addr) {
1176 n = addr - first_addr + 1;
1177 m = second_addr - addr;
1179 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1180 for (; n-- > 0; np = np->q_forw) {
1182 if ((lp = dup_line_node(np)) == NULL) {
1189 else if ((up = push_undo_stack(UADD, current_addr,
1190 current_addr)) == NULL) {
1201 /* delete_lines: delete a range of lines */
1203 delete_lines(from, to)
1209 if (push_undo_stack(UDEL, from, to) == NULL) {
1213 n = get_addressed_line_node(INC_MOD(to, addr_last));
1214 p = get_addressed_line_node(from - 1);
1215 /* this get_addressed_line_node last! */
1217 unset_active_nodes(p->q_forw, n);
1219 addr_last -= to - from + 1;
1220 current_addr = from - 1;
1227 /* display_lines: print a range of lines to stdout */
1229 display_lines(from, to, gflag)
1239 sprintf(errmsg, "invalid address");
1242 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1243 bp = get_addressed_line_node(from);
1244 for (; bp != ep; bp = bp->q_forw) {
1245 if ((s = get_sbuf_line(bp)) == NULL)
1247 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1254 #define MAXMARK 26 /* max number of marks */
1256 line_t *mark[MAXMARK]; /* line markers */
1257 int markno; /* line marker count */
1259 /* mark_line_node: set a line node mark */
1261 mark_line_node(lp, n)
1265 if (!islower((unsigned char)n)) {
1266 sprintf(errmsg, "invalid mark character");
1268 } else if (mark[n - 'a'] == NULL)
1275 /* get_marked_node_addr: return address of a marked line */
1277 get_marked_node_addr(n)
1280 if (!islower((unsigned char)n)) {
1281 sprintf(errmsg, "invalid mark character");
1284 return get_line_node_addr(mark[n - 'a']);
1288 /* unmark_line_node: clear line node mark */
1290 unmark_line_node(lp)
1295 for (i = 0; markno && i < MAXMARK; i++)
1296 if (mark[i] == lp) {
1303 /* dup_line_node: return a pointer to a copy of a line node */
1310 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1311 fprintf(stderr, "%s\n", strerror(errno));
1312 sprintf(errmsg, "out of memory");
1315 np->seek = lp->seek;
1321 /* has_trailing_escape: return the parity of escapes preceding a character
1324 has_trailing_escape(s, t)
1328 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1332 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1337 static char *file = NULL;
1338 static int filesz = 0;
1342 REALLOC(file, filesz, PATH_MAX, NULL);
1343 while (i < filesz - 1 /* Worry about a possible trailing escape */
1344 && (file[i++] = (*s == '\\') ? *++s : *s))
1355 sigflags |= (1 << (signo - 1));
1366 sigflags |= (1 << (signo - 1));
1376 char *hup = NULL; /* hup filename */
1382 sigflags &= ~(1 << (signo - 1));
1383 if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
1384 (s = getenv("HOME")) != NULL &&
1385 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1386 (hup = (char *) malloc(n + 10)) != NULL) {
1388 if (hup[n - 1] != '/')
1389 hup[n] = '/', hup[n+1] = '\0';
1390 strcat(hup, "ed.hup");
1391 write_file(hup, "w", 1, addr_last);
1403 sigflags &= ~(1 << (signo - 1));
1404 #ifdef _POSIX_SOURCE
1405 siglongjmp(env, -1);
1412 int cols = 72; /* wrap column */
1418 int save_errno = errno;
1420 struct winsize ws; /* window size structure */
1422 sigflags &= ~(1 << (signo - 1));
1423 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1424 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1425 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1431 /* is_legal_filename: return a legal filename */
1433 is_legal_filename(s)
1436 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1437 sprintf(errmsg, "shell access restricted");