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
30 static char * const copyright =
31 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
32 All rights reserved.\n";
37 static char * const rcsid = "@(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp";
39 static char * const rcsid =
40 "$FreeBSD: src/bin/ed/main.c,v 1.14.2.4 2001/08/01 02:36:03 obrien Exp $";
47 * This program is based on the editor algorithm described in
48 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
49 * in Pascal," Addison-Wesley, 1981.
51 * The buffering algorithm is attributed to Rodney Ruddock of
52 * the University of Guelph, Guelph, Ontario.
54 * The cbc.c encryption code is adapted from
55 * the bdes program by Matt Bishop of Dartmouth College,
60 #include <sys/types.h>
62 #include <sys/ioctl.h>
79 char stdinbuf[1]; /* stdin buffer */
80 char *shcmd; /* shell command buffer */
81 int shcmdsz; /* shell command buffer size */
82 int shcmdi; /* shell command buffer index */
83 char *ibuf; /* ed command-line buffer */
84 int ibufsz; /* ed command-line buffer size */
85 char *ibufp; /* pointer to ed command-line buffer */
88 int des = 0; /* if set, use crypt(3) for i/o */
89 int garrulous = 0; /* if set, print all error messages */
90 int isbinary; /* if set, buffer contains ASCII NULs */
91 int isglobal; /* if set, doing a global command */
92 int modified; /* if set, buffer modified since last write */
93 int mutex = 0; /* if set, signals set "sigflags" */
94 int red = 0; /* if set, restrict shell/directory access */
95 int scripted = 0; /* if set, suppress diagnostics */
96 int sigflags = 0; /* if set, signals received while mutex set */
97 int sigactive = 0; /* if set, signal handlers are enabled */
99 char old_filename[PATH_MAX] = ""; /* default filename */
100 long current_addr; /* current address in editor buffer */
101 long addr_last; /* last address in editor buffer */
102 int lineno; /* script line number */
103 char *prompt; /* command-line prompt */
104 char *dps = "*"; /* default command-line prompt */
106 const char usage[] = "usage: %s [-] [-sx] [-p string] [name]\n";
108 /* ed: line editor */
117 /* Avoid longjmp clobbering */
122 (void)setlocale(LC_ALL, "");
124 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
126 while ((c = getopt(argc, argv, "p:sx")) != -1)
128 case 'p': /* set prompt */
131 case 's': /* run script */
134 case 'x': /* use crypt */
138 fprintf(stderr, "crypt unavailable\n?\n");
143 fprintf(stderr, usage, argv[0]);
148 if (argc && **argv == '-') {
157 /* assert: reliable signals! */
159 handle_winch(SIGWINCH);
160 if (isatty(0)) signal(SIGWINCH, handle_winch);
162 signal(SIGHUP, signal_hup);
163 signal(SIGQUIT, SIG_IGN);
164 signal(SIGINT, signal_int);
166 if ((status = sigsetjmp(env, 1)))
168 if ((status = setjmp(env)))
171 fputs("\n?\n", stderr);
172 sprintf(errmsg, "interrupt");
175 sigactive = 1; /* enable signal handlers */
176 if (argc && **argv && is_legal_filename(*argv)) {
177 if (read_file(*argv, 0) < 0 && !isatty(0))
179 else if (**argv != '!')
180 if (strlcpy(old_filename, *argv, sizeof(old_filename))
181 >= sizeof(old_filename))
184 fputs("?\n", stderr);
186 sprintf(errmsg, "invalid filename");
192 if (status < 0 && garrulous)
193 fprintf(stderr, "%s\n", errmsg);
195 printf("%s", prompt);
198 if ((n = get_tty_line()) < 0) {
202 if (modified && !scripted) {
203 fputs("?\n", stderr);
204 sprintf(errmsg, "warning: file modified");
206 fprintf(stderr, garrulous ?
207 "script, line %d: %s\n" :
217 } else if (ibuf[n - 1] != '\n') {
219 sprintf(errmsg, "unexpected end-of-file");
225 if ((status = extract_addr_range()) >= 0 &&
226 (status = exec_command()) >= 0)
228 (status = display_lines(current_addr, current_addr,
236 fputs("?\n", stderr); /* give warning */
237 sprintf(errmsg, "warning: file modified");
239 fprintf(stderr, garrulous ?
240 "script, line %d: %s\n" :
247 fprintf(stderr, garrulous ?
248 "script, line %d: %s\n" : "",
251 fprintf(stderr, garrulous ? "%s\n" : "",
255 fputs("?\n", stderr);
257 fprintf(stderr, garrulous ?
258 "script, line %d: %s\n" : "",
268 long first_addr, second_addr, addr_cnt;
270 /* extract_addr_range: get line addresses from the command buffer until an
271 illegal address is seen; return status */
278 first_addr = second_addr = current_addr;
279 while ((addr = next_addr()) >= 0) {
281 first_addr = second_addr;
283 if (*ibufp != ',' && *ibufp != ';')
285 else if (*ibufp++ == ';')
288 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
289 first_addr = second_addr;
290 return (addr == ERR) ? ERR : 0;
294 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
296 #define MUST_BE_FIRST() \
297 if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
299 /* next_addr: return the next line address in the command buffer */
304 long addr = current_addr;
310 for (hd = ibufp;; first = 0)
311 switch (c = *ibufp) {
319 if (isdigit((unsigned char)*ibufp)) {
321 addr += (c == '-' || c == '^') ? -n : n;
322 } else if (!isspace((unsigned char)c))
323 addr += (c == '-' || c == '^') ? -1 : 1;
325 case '0': case '1': case '2':
326 case '3': case '4': case '5':
327 case '6': case '7': case '8': case '9':
335 addr = (c == '.') ? current_addr : addr_last;
340 if ((addr = get_matching_node_addr(
341 get_compiled_pattern(), c == '/')) < 0)
343 else if (c == *ibufp)
349 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
358 second_addr = (c == ';') ? current_addr : 1;
366 else if (addr < 0 || addr_last < addr) {
367 sprintf(errmsg, "invalid address");
377 /* GET_THIRD_ADDR: get a legal address from the command buffer */
378 #define GET_THIRD_ADDR(addr) \
382 ol1 = first_addr, ol2 = second_addr; \
383 if (extract_addr_range() < 0) \
385 else if (addr_cnt == 0) { \
386 sprintf(errmsg, "destination expected"); \
388 } else if (second_addr < 0 || addr_last < second_addr) { \
389 sprintf(errmsg, "invalid address"); \
392 addr = second_addr; \
393 first_addr = ol1, second_addr = ol2; \
395 #else /* BACKWARDS */
396 /* GET_THIRD_ADDR: get a legal address from the command buffer */
397 #define GET_THIRD_ADDR(addr) \
401 ol1 = first_addr, ol2 = second_addr; \
402 if (extract_addr_range() < 0) \
404 if (second_addr < 0 || addr_last < second_addr) { \
405 sprintf(errmsg, "invalid address"); \
408 addr = second_addr; \
409 first_addr = ol1, second_addr = ol2; \
414 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
415 #define GET_COMMAND_SUFFIX() { \
420 gflag |= GPR, ibufp++; \
423 gflag |= GLS, ibufp++; \
426 gflag |= GNP, ibufp++; \
432 if (*ibufp++ != '\n') { \
433 sprintf(errmsg, "invalid command suffix"); \
440 #define SGG 001 /* complement previous global substitute suffix */
441 #define SGP 002 /* complement previous print suffix */
442 #define SGR 004 /* use last regex instead of last pat */
443 #define SGF 010 /* repeat last substitution */
445 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
447 long rows = 22; /* scroll length: ws_row - 2 */
449 /* exec_command: execute the next command in command buffer; return print
454 extern long u_current_addr;
455 extern long u_addr_last;
457 static pattern_t *pat = NULL;
458 static int sgflag = 0;
459 static long sgnum = 0;
470 switch(c = *ibufp++) {
472 GET_COMMAND_SUFFIX();
473 if (!isglobal) clear_undo_stack();
474 if (append_lines(second_addr) < 0)
478 if (check_addr_range(current_addr, current_addr) < 0)
480 GET_COMMAND_SUFFIX();
481 if (!isglobal) clear_undo_stack();
482 if (delete_lines(first_addr, second_addr) < 0 ||
483 append_lines(current_addr) < 0)
487 if (check_addr_range(current_addr, current_addr) < 0)
489 GET_COMMAND_SUFFIX();
490 if (!isglobal) clear_undo_stack();
491 if (delete_lines(first_addr, second_addr) < 0)
493 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
497 if (modified && !scripted)
502 sprintf(errmsg, "unexpected address");
504 } else if (!isspace((unsigned char)*ibufp)) {
505 sprintf(errmsg, "unexpected command suffix");
507 } else if ((fnp = get_filename()) == NULL)
509 GET_COMMAND_SUFFIX();
510 if (delete_lines(1, addr_last) < 0)
513 if (close_sbuf() < 0)
515 else if (open_sbuf() < 0)
517 if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
519 if (*fnp == '\0' && *old_filename == '\0') {
520 sprintf(errmsg, "no current filename");
524 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
528 u_current_addr = u_addr_last = -1;
532 sprintf(errmsg, "unexpected address");
534 } else if (!isspace((unsigned char)*ibufp)) {
535 sprintf(errmsg, "unexpected command suffix");
537 } else if ((fnp = get_filename()) == NULL)
539 else if (*fnp == '!') {
540 sprintf(errmsg, "invalid redirection");
543 GET_COMMAND_SUFFIX();
544 if (*fnp) strcpy(old_filename, fnp);
545 printf("%s\n", strip_escapes(old_filename));
552 sprintf(errmsg, "cannot nest global commands");
554 } else if (check_addr_range(1, addr_last) < 0)
556 else if (build_active_list(c == 'g' || c == 'G') < 0)
558 else if ((n = (c == 'G' || c == 'V')))
559 GET_COMMAND_SUFFIX();
561 if (exec_global(n, gflag) < 0)
566 sprintf(errmsg, "unexpected address");
569 GET_COMMAND_SUFFIX();
570 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
574 sprintf(errmsg, "unexpected address");
577 GET_COMMAND_SUFFIX();
578 if ((garrulous = 1 - garrulous) && *errmsg)
579 fprintf(stderr, "%s\n", errmsg);
582 if (second_addr == 0) {
583 sprintf(errmsg, "invalid address");
586 GET_COMMAND_SUFFIX();
587 if (!isglobal) clear_undo_stack();
588 if (append_lines(second_addr - 1) < 0)
592 if (check_addr_range(current_addr, current_addr + 1) < 0)
594 GET_COMMAND_SUFFIX();
595 if (!isglobal) clear_undo_stack();
596 if (first_addr != second_addr &&
597 join_lines(first_addr, second_addr) < 0)
602 if (second_addr == 0) {
603 sprintf(errmsg, "invalid address");
606 GET_COMMAND_SUFFIX();
607 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
611 if (check_addr_range(current_addr, current_addr) < 0)
613 GET_COMMAND_SUFFIX();
614 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
619 if (check_addr_range(current_addr, current_addr) < 0)
621 GET_THIRD_ADDR(addr);
622 if (first_addr <= addr && addr < second_addr) {
623 sprintf(errmsg, "invalid destination");
626 GET_COMMAND_SUFFIX();
627 if (!isglobal) clear_undo_stack();
628 if (move_lines(addr) < 0)
632 if (check_addr_range(current_addr, current_addr) < 0)
634 GET_COMMAND_SUFFIX();
635 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
640 if (check_addr_range(current_addr, current_addr) < 0)
642 GET_COMMAND_SUFFIX();
643 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
649 sprintf(errmsg, "unexpected address");
652 GET_COMMAND_SUFFIX();
653 prompt = prompt ? NULL : optarg ? optarg : dps;
658 sprintf(errmsg, "unexpected address");
661 GET_COMMAND_SUFFIX();
662 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
665 if (!isspace((unsigned char)*ibufp)) {
666 sprintf(errmsg, "unexpected command suffix");
668 } else if (addr_cnt == 0)
669 second_addr = addr_last;
670 if ((fnp = get_filename()) == NULL)
672 GET_COMMAND_SUFFIX();
673 if (!isglobal) clear_undo_stack();
674 if (*old_filename == '\0' && *fnp != '!')
675 strcpy(old_filename, fnp);
677 if (*fnp == '\0' && *old_filename == '\0') {
678 sprintf(errmsg, "no current filename");
682 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
684 else if (addr && addr != addr_last)
705 case '0': case '1': case '2': case '3': case '4':
706 case '5': case '6': case '7': case '8': case '9':
707 STRTOL(sgnum, ibufp);
709 sgflag &= ~GSG; /* override GSG */
713 sprintf(errmsg, "invalid command suffix");
717 } while (sflags && *ibufp != '\n');
718 if (sflags && !pat) {
719 sprintf(errmsg, "no previous substitution");
721 } else if (sflags & SGG)
722 sgnum = 0; /* override numeric arg */
723 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
724 sprintf(errmsg, "invalid pattern delimiter");
729 if ((!sflags || (sflags & SGR)) &&
730 (tpat = get_compiled_pattern()) == NULL) {
733 } else if (tpat != pat) {
739 patlock = 1; /* reserve pattern */
742 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
751 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
755 sgflag |= GPR, ibufp++;
758 sgflag |= GLS, ibufp++;
761 sgflag |= GNP, ibufp++;
767 if (check_addr_range(current_addr, current_addr) < 0)
769 GET_COMMAND_SUFFIX();
770 if (!isglobal) clear_undo_stack();
771 if (search_and_replace(pat, sgflag, sgnum) < 0)
775 if (check_addr_range(current_addr, current_addr) < 0)
777 GET_THIRD_ADDR(addr);
778 GET_COMMAND_SUFFIX();
779 if (!isglobal) clear_undo_stack();
780 if (copy_lines(addr) < 0)
785 sprintf(errmsg, "unexpected address");
788 GET_COMMAND_SUFFIX();
789 if (pop_undo_stack() < 0)
794 if ((n = *ibufp) == 'q' || n == 'Q') {
798 if (!isspace((unsigned char)*ibufp)) {
799 sprintf(errmsg, "unexpected command suffix");
801 } else if ((fnp = get_filename()) == NULL)
803 if (addr_cnt == 0 && !addr_last)
804 first_addr = second_addr = 0;
805 else if (check_addr_range(1, addr_last) < 0)
807 GET_COMMAND_SUFFIX();
808 if (*old_filename == '\0' && *fnp != '!')
809 strcpy(old_filename, fnp);
811 if (*fnp == '\0' && *old_filename == '\0') {
812 sprintf(errmsg, "no current filename");
816 if ((addr = write_file(*fnp ? fnp : old_filename,
817 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
819 else if (addr == addr_last)
821 else if (modified && !scripted && n == 'q')
826 sprintf(errmsg, "unexpected address");
829 GET_COMMAND_SUFFIX();
833 sprintf(errmsg, "crypt unavailable");
839 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
841 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
844 else if ('0' < *ibufp && *ibufp <= '9')
846 GET_COMMAND_SUFFIX();
847 if (display_lines(second_addr, min(addr_last,
848 second_addr + rows), gflag) < 0)
853 GET_COMMAND_SUFFIX();
854 printf("%ld\n", addr_cnt ? second_addr : addr_last);
858 sprintf(errmsg, "unexpected address");
860 } else if ((sflags = get_shell_command()) < 0)
862 GET_COMMAND_SUFFIX();
863 if (sflags) printf("%s\n", shcmd + 1);
865 if (!scripted) printf("!\n");
869 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
871 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
873 || display_lines(second_addr, second_addr, 0) < 0)
877 sprintf(errmsg, "unknown command");
884 /* check_addr_range: return status of address range check */
886 check_addr_range(n, m)
893 if (first_addr > second_addr || 1 > first_addr ||
894 second_addr > addr_last) {
895 sprintf(errmsg, "invalid address");
902 /* get_matching_node_addr: return the address of the next line matching a
903 pattern in a given direction. wrap around begin/end of editor buffer if
906 get_matching_node_addr(pat, dir)
911 long n = current_addr;
914 if (!pat) return ERR;
916 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
917 lp = get_addressed_line_node(n);
918 if ((s = get_sbuf_line(lp)) == NULL)
921 NUL_TO_NEWLINE(s, lp->len);
922 if (!regexec(pat, s, 0, NULL, 0))
925 } while (n != current_addr);
926 sprintf(errmsg, "no match");
931 /* get_filename: return pointer to copy of filename in the command buffer */
935 static char *file = NULL;
936 static int filesz = 0;
940 if (*ibufp != '\n') {
942 if (*ibufp == '\n') {
943 sprintf(errmsg, "invalid filename");
945 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
947 else if (*ibufp == '!') {
949 if ((n = get_shell_command()) < 0)
952 printf("%s\n", shcmd + 1);
954 } else if (n > PATH_MAX - 1) {
955 sprintf(errmsg, "filename too long");
960 else if (*old_filename == '\0') {
961 sprintf(errmsg, "no current filename");
965 REALLOC(file, filesz, PATH_MAX, NULL);
966 for (n = 0; *ibufp != '\n';)
967 file[n++] = *ibufp++;
969 return is_legal_filename(file) ? file : NULL;
973 /* get_shell_command: read a shell command from stdin; return substitution
978 static char *buf = NULL;
981 char *s; /* substitution char pointer */
986 sprintf(errmsg, "shell access restricted");
988 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
990 REALLOC(buf, n, j + 1, ERR);
991 buf[i++] = '!'; /* prefix command w/ bang */
992 while (*ibufp != '\n')
995 REALLOC(buf, n, i + 2, ERR);
997 if (*ibufp++ == '\\')
1002 REALLOC(buf, n, i + 1, ERR);
1003 buf[i++] = *ibufp++;
1006 else if (shcmd == NULL || *(shcmd + 1) == '\0')
1008 else if (shcmd == NULL)
1011 sprintf(errmsg, "no previous command");
1014 REALLOC(buf, n, i + shcmdi, ERR);
1015 for (s = shcmd + 1; s < shcmd + shcmdi;)
1021 if (*old_filename == '\0') {
1022 sprintf(errmsg, "no current filename");
1025 j = strlen(s = strip_escapes(old_filename));
1026 REALLOC(buf, n, i + j, ERR);
1032 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1033 memcpy(shcmd, buf, i);
1034 shcmd[shcmdi = i] = '\0';
1035 return *s == '!' || *s == '%';
1039 /* append_lines: insert text from stdin to after line n; stop when either a
1040 single period is read or EOF; return status */
1050 for (current_addr = n;;) {
1052 if ((l = get_tty_line()) < 0)
1054 else if (l == 0 || ibuf[l - 1] != '\n') {
1059 } else if (*(lp = ibufp) == '\0')
1062 while (*ibufp++ != '\n')
1066 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1072 if ((lp = put_sbuf_line(lp)) == NULL) {
1076 up->t = get_addressed_line_node(current_addr);
1077 else if ((up = push_undo_stack(UADD, current_addr,
1078 current_addr)) == NULL) {
1082 } while (lp != eot);
1090 /* join_lines: replace a range of lines with the joined text of those lines */
1092 join_lines(from, to)
1096 static char *buf = NULL;
1103 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1104 bp = get_addressed_line_node(from);
1105 for (; bp != ep; bp = bp->q_forw) {
1106 if ((s = get_sbuf_line(bp)) == NULL)
1108 REALLOC(buf, n, size + bp->len, ERR);
1109 memcpy(buf + size, s, bp->len);
1112 REALLOC(buf, n, size + 2, ERR);
1113 memcpy(buf + size, "\n", 2);
1114 if (delete_lines(from, to) < 0)
1116 current_addr = from - 1;
1118 if (put_sbuf_line(buf) == NULL ||
1119 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1129 /* move_lines: move a range of lines */
1134 line_t *b1, *a1, *b2, *a2;
1135 long n = INC_MOD(second_addr, addr_last);
1136 long p = first_addr - 1;
1137 int done = (addr == first_addr - 1 || addr == second_addr);
1141 a2 = get_addressed_line_node(n);
1142 b2 = get_addressed_line_node(p);
1143 current_addr = second_addr;
1144 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1145 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1149 a1 = get_addressed_line_node(n);
1150 if (addr < first_addr) {
1151 b1 = get_addressed_line_node(p);
1152 b2 = get_addressed_line_node(addr);
1153 /* this get_addressed_line_node last! */
1155 b2 = get_addressed_line_node(addr);
1156 b1 = get_addressed_line_node(p);
1157 /* this get_addressed_line_node last! */
1160 REQUE(b2, b1->q_forw);
1161 REQUE(a1->q_back, a2);
1163 current_addr = addr + ((addr < first_addr) ?
1164 second_addr - first_addr + 1 : 0);
1167 unset_active_nodes(b2->q_forw, a2);
1174 /* copy_lines: copy a range of lines; return status */
1179 line_t *lp, *np = get_addressed_line_node(first_addr);
1181 long n = second_addr - first_addr + 1;
1184 current_addr = addr;
1185 if (first_addr <= addr && addr < second_addr) {
1186 n = addr - first_addr + 1;
1187 m = second_addr - addr;
1189 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1190 for (; n-- > 0; np = np->q_forw) {
1192 if ((lp = dup_line_node(np)) == NULL) {
1199 else if ((up = push_undo_stack(UADD, current_addr,
1200 current_addr)) == NULL) {
1211 /* delete_lines: delete a range of lines */
1213 delete_lines(from, to)
1219 if (push_undo_stack(UDEL, from, to) == NULL) {
1223 n = get_addressed_line_node(INC_MOD(to, addr_last));
1224 p = get_addressed_line_node(from - 1);
1225 /* this get_addressed_line_node last! */
1227 unset_active_nodes(p->q_forw, n);
1229 addr_last -= to - from + 1;
1230 current_addr = from - 1;
1237 /* display_lines: print a range of lines to stdout */
1239 display_lines(from, to, gflag)
1249 sprintf(errmsg, "invalid address");
1252 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1253 bp = get_addressed_line_node(from);
1254 for (; bp != ep; bp = bp->q_forw) {
1255 if ((s = get_sbuf_line(bp)) == NULL)
1257 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1264 #define MAXMARK 26 /* max number of marks */
1266 line_t *mark[MAXMARK]; /* line markers */
1267 int markno; /* line marker count */
1269 /* mark_line_node: set a line node mark */
1271 mark_line_node(lp, n)
1275 if (!islower((unsigned char)n)) {
1276 sprintf(errmsg, "invalid mark character");
1278 } else if (mark[n - 'a'] == NULL)
1285 /* get_marked_node_addr: return address of a marked line */
1287 get_marked_node_addr(n)
1290 if (!islower((unsigned char)n)) {
1291 sprintf(errmsg, "invalid mark character");
1294 return get_line_node_addr(mark[n - 'a']);
1298 /* unmark_line_node: clear line node mark */
1300 unmark_line_node(lp)
1305 for (i = 0; markno && i < MAXMARK; i++)
1306 if (mark[i] == lp) {
1313 /* dup_line_node: return a pointer to a copy of a line node */
1320 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1321 fprintf(stderr, "%s\n", strerror(errno));
1322 sprintf(errmsg, "out of memory");
1325 np->seek = lp->seek;
1331 /* has_trailing_escape: return the parity of escapes preceding a character
1334 has_trailing_escape(s, t)
1338 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1342 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1347 static char *file = NULL;
1348 static int filesz = 0;
1352 REALLOC(file, filesz, PATH_MAX, NULL);
1353 while (i < filesz - 1 /* Worry about a possible trailing escape */
1354 && (file[i++] = (*s == '\\') ? *++s : *s))
1365 sigflags |= (1 << (signo - 1));
1376 sigflags |= (1 << (signo - 1));
1386 char *hup = NULL; /* hup filename */
1392 sigflags &= ~(1 << (signo - 1));
1393 if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
1394 (s = getenv("HOME")) != NULL &&
1395 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1396 (hup = (char *) malloc(n + 10)) != NULL) {
1398 if (hup[n - 1] != '/')
1399 hup[n] = '/', hup[n+1] = '\0';
1400 strcat(hup, "ed.hup");
1401 write_file(hup, "w", 1, addr_last);
1413 sigflags &= ~(1 << (signo - 1));
1414 #ifdef _POSIX_SOURCE
1415 siglongjmp(env, -1);
1422 int cols = 72; /* wrap column */
1428 int save_errno = errno;
1430 struct winsize ws; /* window size structure */
1432 sigflags &= ~(1 << (signo - 1));
1433 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1434 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1435 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1441 /* is_legal_filename: return a legal filename */
1443 is_legal_filename(s)
1446 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1447 sprintf(errmsg, "shell access restricted");