1 /* main.c: This file contains the main control and user-interface routines
2 for the ed line editor. */
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
33 All rights reserved.\n";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
43 * This program is based on the editor algorithm described in
44 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
45 * in Pascal," Addison-Wesley, 1981.
47 * The buffering algorithm is attributed to Rodney Ruddock of
48 * the University of Guelph, Guelph, Ontario.
50 * The cbc.c encryption code is adapted from
51 * the bdes program by Matt Bishop of Dartmouth College,
56 #include <sys/types.h>
58 #include <sys/ioctl.h>
75 char stdinbuf[1]; /* stdin buffer */
76 char *shcmd; /* shell command buffer */
77 int shcmdsz; /* shell command buffer size */
78 int shcmdi; /* shell command buffer index */
79 char *ibuf; /* ed command-line buffer */
80 int ibufsz; /* ed command-line buffer size */
81 char *ibufp; /* pointer to ed command-line buffer */
84 int des = 0; /* if set, use crypt(3) for i/o */
85 int garrulous = 0; /* if set, print all error messages */
86 int isbinary; /* if set, buffer contains ASCII NULs */
87 int isglobal; /* if set, doing a global command */
88 int modified; /* if set, buffer modified since last write */
89 int mutex = 0; /* if set, signals set "sigflags" */
90 int red = 0; /* if set, restrict shell/directory access */
91 int scripted = 0; /* if set, suppress diagnostics */
92 int sigflags = 0; /* if set, signals received while mutex set */
93 int sigactive = 0; /* if set, signal handlers are enabled */
95 char old_filename[PATH_MAX] = ""; /* default filename */
96 long current_addr; /* current address in editor buffer */
97 long addr_last; /* last address in editor buffer */
98 int lineno; /* script line number */
99 const char *prompt; /* command-line prompt */
100 const char *dps = "*"; /* default command-line prompt */
102 const char usage[] = "usage: %s [-] [-sx] [-p string] [file]\n";
104 /* ed: line editor */
106 main(int argc, char *argv[])
111 /* Avoid longjmp clobbering */
116 (void)setlocale(LC_ALL, "");
118 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
120 while ((c = getopt(argc, argv, "p:sx")) != -1)
122 case 'p': /* set prompt */
125 case 's': /* run script */
128 case 'x': /* use crypt */
132 fprintf(stderr, "crypt unavailable\n?\n");
137 fprintf(stderr, usage, red ? "red" : "ed");
142 if (argc && **argv == '-') {
151 /* assert: reliable signals! */
153 handle_winch(SIGWINCH);
154 if (isatty(0)) signal(SIGWINCH, handle_winch);
156 signal(SIGHUP, signal_hup);
157 signal(SIGQUIT, SIG_IGN);
158 signal(SIGINT, signal_int);
160 if ((status = sigsetjmp(env, 1)))
162 if ((status = setjmp(env)))
165 fputs("\n?\n", stderr);
166 errmsg = "interrupt";
169 sigactive = 1; /* enable signal handlers */
170 if (argc && **argv && is_legal_filename(*argv)) {
171 if (read_file(*argv, 0) < 0 && !isatty(0))
173 else if (**argv != '!')
174 if (strlcpy(old_filename, *argv, sizeof(old_filename))
175 >= sizeof(old_filename))
178 fputs("?\n", stderr);
180 errmsg = "invalid filename";
186 if (status < 0 && garrulous)
187 fprintf(stderr, "%s\n", errmsg);
189 printf("%s", prompt);
192 if ((n = get_tty_line()) < 0) {
196 if (modified && !scripted) {
197 fputs("?\n", stderr);
198 errmsg = "warning: file modified";
200 fprintf(stderr, garrulous ?
201 "script, line %d: %s\n" :
211 } else if (ibuf[n - 1] != '\n') {
213 errmsg = "unexpected end-of-file";
219 if ((status = extract_addr_range()) >= 0 &&
220 (status = exec_command()) >= 0)
222 (status = display_lines(current_addr, current_addr,
230 fputs("?\n", stderr); /* give warning */
231 errmsg = "warning: file modified";
233 fprintf(stderr, garrulous ?
234 "script, line %d: %s\n" :
241 fprintf(stderr, garrulous ?
242 "script, line %d: %s\n" : "",
245 fprintf(stderr, garrulous ? "%s\n" : "",
249 fputs("?\n", stderr);
251 fprintf(stderr, garrulous ?
252 "script, line %d: %s\n" : "",
262 long first_addr, second_addr, addr_cnt;
264 /* extract_addr_range: get line addresses from the command buffer until an
265 illegal address is seen; return status */
267 extract_addr_range(void)
272 first_addr = second_addr = current_addr;
273 while ((addr = next_addr()) >= 0) {
275 first_addr = second_addr;
277 if (*ibufp != ',' && *ibufp != ';')
279 else if (*ibufp++ == ';')
282 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
283 first_addr = second_addr;
284 return (addr == ERR) ? ERR : 0;
288 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
290 #define MUST_BE_FIRST() do { \
292 errmsg = "invalid address"; \
297 /* next_addr: return the next line address in the command buffer */
302 long addr = current_addr;
308 for (hd = ibufp;; first = 0)
309 switch (c = *ibufp) {
317 if (isdigit((unsigned char)*ibufp)) {
319 addr += (c == '-' || c == '^') ? -n : n;
320 } else if (!isspace((unsigned char)c))
321 addr += (c == '-' || c == '^') ? -1 : 1;
323 case '0': case '1': case '2':
324 case '3': case '4': case '5':
325 case '6': case '7': case '8': case '9':
333 addr = (c == '.') ? current_addr : addr_last;
338 if ((addr = get_matching_node_addr(
339 get_compiled_pattern(), c == '/')) < 0)
341 else if (c == *ibufp)
347 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
356 second_addr = (c == ';') ? current_addr : 1;
364 else if (addr < 0 || addr_last < addr) {
365 errmsg = "invalid address";
375 /* GET_THIRD_ADDR: get a legal address from the command buffer */
376 #define GET_THIRD_ADDR(addr) \
380 ol1 = first_addr, ol2 = second_addr; \
381 if (extract_addr_range() < 0) \
383 else if (addr_cnt == 0) { \
384 errmsg = "destination expected"; \
386 } else if (second_addr < 0 || addr_last < second_addr) { \
387 errmsg = "invalid address"; \
390 addr = second_addr; \
391 first_addr = ol1, second_addr = ol2; \
393 #else /* BACKWARDS */
394 /* GET_THIRD_ADDR: get a legal address from the command buffer */
395 #define GET_THIRD_ADDR(addr) \
399 ol1 = first_addr, ol2 = second_addr; \
400 if (extract_addr_range() < 0) \
402 if (second_addr < 0 || addr_last < second_addr) { \
403 errmsg = "invalid address"; \
406 addr = second_addr; \
407 first_addr = ol1, second_addr = ol2; \
412 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
413 #define GET_COMMAND_SUFFIX() { \
418 gflag |= GPR, ibufp++; \
421 gflag |= GLS, ibufp++; \
424 gflag |= GNP, ibufp++; \
430 if (*ibufp++ != '\n') { \
431 errmsg = "invalid command suffix"; \
438 #define SGG 001 /* complement previous global substitute suffix */
439 #define SGP 002 /* complement previous print suffix */
440 #define SGR 004 /* use last regex instead of last pat */
441 #define SGF 010 /* repeat last substitution */
443 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
445 long rows = 22; /* scroll length: ws_row - 2 */
447 /* exec_command: execute the next command in command buffer; return print
452 static pattern_t *pat = NULL;
453 static int sgflag = 0;
454 static long sgnum = 0;
465 switch(c = *ibufp++) {
467 GET_COMMAND_SUFFIX();
468 if (!isglobal) clear_undo_stack();
469 if (append_lines(second_addr) < 0)
473 if (check_addr_range(current_addr, current_addr) < 0)
475 GET_COMMAND_SUFFIX();
476 if (!isglobal) clear_undo_stack();
477 if (delete_lines(first_addr, second_addr) < 0 ||
478 append_lines(current_addr) < 0)
482 if (check_addr_range(current_addr, current_addr) < 0)
484 GET_COMMAND_SUFFIX();
485 if (!isglobal) clear_undo_stack();
486 if (delete_lines(first_addr, second_addr) < 0)
488 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
492 if (modified && !scripted)
497 errmsg = "unexpected address";
499 } else if (!isspace((unsigned char)*ibufp)) {
500 errmsg = "unexpected command suffix";
502 } else if ((fnp = get_filename()) == NULL)
504 GET_COMMAND_SUFFIX();
505 if (delete_lines(1, addr_last) < 0)
508 if (close_sbuf() < 0)
510 else if (open_sbuf() < 0)
512 if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
514 if (*fnp == '\0' && *old_filename == '\0') {
515 errmsg = "no current filename";
519 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
523 u_current_addr = u_addr_last = -1;
527 errmsg = "unexpected address";
529 } else if (!isspace((unsigned char)*ibufp)) {
530 errmsg = "unexpected command suffix";
532 } else if ((fnp = get_filename()) == NULL)
534 else if (*fnp == '!') {
535 errmsg = "invalid redirection";
538 GET_COMMAND_SUFFIX();
539 if (*fnp) strcpy(old_filename, fnp);
540 printf("%s\n", strip_escapes(old_filename));
547 errmsg = "cannot nest global commands";
549 } else if (check_addr_range(1, addr_last) < 0)
551 else if (build_active_list(c == 'g' || c == 'G') < 0)
553 else if ((n = (c == 'G' || c == 'V')))
554 GET_COMMAND_SUFFIX();
556 if (exec_global(n, gflag) < 0)
561 errmsg = "unexpected address";
564 GET_COMMAND_SUFFIX();
565 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
569 errmsg = "unexpected address";
572 GET_COMMAND_SUFFIX();
573 if ((garrulous = 1 - garrulous) && *errmsg)
574 fprintf(stderr, "%s\n", errmsg);
577 if (second_addr == 0) {
578 errmsg = "invalid address";
581 GET_COMMAND_SUFFIX();
582 if (!isglobal) clear_undo_stack();
583 if (append_lines(second_addr - 1) < 0)
587 if (check_addr_range(current_addr, current_addr + 1) < 0)
589 GET_COMMAND_SUFFIX();
590 if (!isglobal) clear_undo_stack();
591 if (first_addr != second_addr &&
592 join_lines(first_addr, second_addr) < 0)
597 if (second_addr == 0) {
598 errmsg = "invalid address";
601 GET_COMMAND_SUFFIX();
602 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
606 if (check_addr_range(current_addr, current_addr) < 0)
608 GET_COMMAND_SUFFIX();
609 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
614 if (check_addr_range(current_addr, current_addr) < 0)
616 GET_THIRD_ADDR(addr);
617 if (first_addr <= addr && addr < second_addr) {
618 errmsg = "invalid destination";
621 GET_COMMAND_SUFFIX();
622 if (!isglobal) clear_undo_stack();
623 if (move_lines(addr) < 0)
627 if (check_addr_range(current_addr, current_addr) < 0)
629 GET_COMMAND_SUFFIX();
630 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
635 if (check_addr_range(current_addr, current_addr) < 0)
637 GET_COMMAND_SUFFIX();
638 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
644 errmsg = "unexpected address";
647 GET_COMMAND_SUFFIX();
648 prompt = prompt ? NULL : optarg ? optarg : dps;
653 errmsg = "unexpected address";
656 GET_COMMAND_SUFFIX();
657 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
660 if (!isspace((unsigned char)*ibufp)) {
661 errmsg = "unexpected command suffix";
663 } else if (addr_cnt == 0)
664 second_addr = addr_last;
665 if ((fnp = get_filename()) == NULL)
667 GET_COMMAND_SUFFIX();
668 if (!isglobal) clear_undo_stack();
669 if (*old_filename == '\0' && *fnp != '!')
670 strcpy(old_filename, fnp);
672 if (*fnp == '\0' && *old_filename == '\0') {
673 errmsg = "no current filename";
677 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
679 else if (addr && addr != addr_last)
700 case '0': case '1': case '2': case '3': case '4':
701 case '5': case '6': case '7': case '8': case '9':
702 STRTOL(sgnum, ibufp);
704 sgflag &= ~GSG; /* override GSG */
708 errmsg = "invalid command suffix";
712 } while (sflags && *ibufp != '\n');
713 if (sflags && !pat) {
714 errmsg = "no previous substitution";
716 } else if (sflags & SGG)
717 sgnum = 0; /* override numeric arg */
718 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
719 errmsg = "invalid pattern delimiter";
724 if ((!sflags || (sflags & SGR)) &&
725 (tpat = get_compiled_pattern()) == NULL) {
728 } else if (tpat != pat) {
734 patlock = 1; /* reserve pattern */
737 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
746 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
750 sgflag |= GPR, ibufp++;
753 sgflag |= GLS, ibufp++;
756 sgflag |= GNP, ibufp++;
762 if (check_addr_range(current_addr, current_addr) < 0)
764 GET_COMMAND_SUFFIX();
765 if (!isglobal) clear_undo_stack();
766 if (search_and_replace(pat, sgflag, sgnum) < 0)
770 if (check_addr_range(current_addr, current_addr) < 0)
772 GET_THIRD_ADDR(addr);
773 GET_COMMAND_SUFFIX();
774 if (!isglobal) clear_undo_stack();
775 if (copy_lines(addr) < 0)
780 errmsg = "unexpected address";
783 GET_COMMAND_SUFFIX();
784 if (pop_undo_stack() < 0)
789 if ((n = *ibufp) == 'q' || n == 'Q') {
793 if (!isspace((unsigned char)*ibufp)) {
794 errmsg = "unexpected command suffix";
796 } else if ((fnp = get_filename()) == NULL)
798 if (addr_cnt == 0 && !addr_last)
799 first_addr = second_addr = 0;
800 else if (check_addr_range(1, addr_last) < 0)
802 GET_COMMAND_SUFFIX();
803 if (*old_filename == '\0' && *fnp != '!')
804 strcpy(old_filename, fnp);
806 if (*fnp == '\0' && *old_filename == '\0') {
807 errmsg = "no current filename";
811 if ((addr = write_file(*fnp ? fnp : old_filename,
812 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
814 else if (addr == addr_last)
816 else if (modified && !scripted && n == 'q')
821 errmsg = "unexpected address";
824 GET_COMMAND_SUFFIX();
829 errmsg = "crypt unavailable";
834 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
836 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
839 else if ('0' < *ibufp && *ibufp <= '9')
841 GET_COMMAND_SUFFIX();
842 if (display_lines(second_addr, min(addr_last,
843 second_addr + rows), gflag) < 0)
848 GET_COMMAND_SUFFIX();
849 printf("%ld\n", addr_cnt ? second_addr : addr_last);
853 errmsg = "unexpected address";
855 } else if ((sflags = get_shell_command()) < 0)
857 GET_COMMAND_SUFFIX();
858 if (sflags) printf("%s\n", shcmd + 1);
860 if (!scripted) printf("!\n");
864 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
866 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
868 || display_lines(second_addr, second_addr, 0) < 0)
872 errmsg = "unknown command";
879 /* check_addr_range: return status of address range check */
881 check_addr_range(long n, long m)
887 if (first_addr > second_addr || 1 > first_addr ||
888 second_addr > addr_last) {
889 errmsg = "invalid address";
896 /* get_matching_node_addr: return the address of the next line matching a
897 pattern in a given direction. wrap around begin/end of editor buffer if
900 get_matching_node_addr(pattern_t *pat, int dir)
903 long n = current_addr;
906 if (!pat) return ERR;
908 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
909 lp = get_addressed_line_node(n);
910 if ((s = get_sbuf_line(lp)) == NULL)
913 NUL_TO_NEWLINE(s, lp->len);
914 if (!regexec(pat, s, 0, NULL, 0))
917 } while (n != current_addr);
923 /* get_filename: return pointer to copy of filename in the command buffer */
927 static char *file = NULL;
928 static int filesz = 0;
932 if (*ibufp != '\n') {
934 if (*ibufp == '\n') {
935 errmsg = "invalid filename";
937 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
939 else if (*ibufp == '!') {
941 if ((n = get_shell_command()) < 0)
944 printf("%s\n", shcmd + 1);
946 } else if (n > PATH_MAX - 1) {
947 errmsg = "filename too long";
952 else if (*old_filename == '\0') {
953 errmsg = "no current filename";
957 REALLOC(file, filesz, PATH_MAX, NULL);
958 for (n = 0; *ibufp != '\n';)
959 file[n++] = *ibufp++;
961 return is_legal_filename(file) ? file : NULL;
965 /* get_shell_command: read a shell command from stdin; return substitution
968 get_shell_command(void)
970 static char *buf = NULL;
973 char *s; /* substitution char pointer */
978 errmsg = "shell access restricted";
980 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
982 REALLOC(buf, n, j + 1, ERR);
983 buf[i++] = '!'; /* prefix command w/ bang */
984 while (*ibufp != '\n')
987 REALLOC(buf, n, i + 2, ERR);
989 if (*ibufp++ == '\\')
994 REALLOC(buf, n, i + 1, ERR);
998 else if (shcmd == NULL || *(shcmd + 1) == '\0')
1000 else if (shcmd == NULL)
1003 errmsg = "no previous command";
1006 REALLOC(buf, n, i + shcmdi, ERR);
1007 for (s = shcmd + 1; s < shcmd + shcmdi;)
1013 if (*old_filename == '\0') {
1014 errmsg = "no current filename";
1017 j = strlen(s = strip_escapes(old_filename));
1018 REALLOC(buf, n, i + j, ERR);
1024 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1025 memcpy(shcmd, buf, i);
1026 shcmd[shcmdi = i] = '\0';
1027 return *s == '!' || *s == '%';
1031 /* append_lines: insert text from stdin to after line n; stop when either a
1032 single period is read or EOF; return status */
1034 append_lines(long n)
1037 const char *lp = ibuf;
1041 for (current_addr = n;;) {
1043 if ((l = get_tty_line()) < 0)
1045 else if (l == 0 || ibuf[l - 1] != '\n') {
1050 } else if (*(lp = ibufp) == '\0')
1053 while (*ibufp++ != '\n')
1057 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1063 if ((lp = put_sbuf_line(lp)) == NULL) {
1067 up->t = get_addressed_line_node(current_addr);
1068 else if ((up = push_undo_stack(UADD, current_addr,
1069 current_addr)) == NULL) {
1073 } while (lp != eot);
1081 /* join_lines: replace a range of lines with the joined text of those lines */
1083 join_lines(long from, long to)
1085 static char *buf = NULL;
1092 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1093 bp = get_addressed_line_node(from);
1094 for (; bp != ep; bp = bp->q_forw) {
1095 if ((s = get_sbuf_line(bp)) == NULL)
1097 REALLOC(buf, n, size + bp->len, ERR);
1098 memcpy(buf + size, s, bp->len);
1101 REALLOC(buf, n, size + 2, ERR);
1102 memcpy(buf + size, "\n", 2);
1103 if (delete_lines(from, to) < 0)
1105 current_addr = from - 1;
1107 if (put_sbuf_line(buf) == NULL ||
1108 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1118 /* move_lines: move a range of lines */
1120 move_lines(long addr)
1122 line_t *b1, *a1, *b2, *a2;
1123 long n = INC_MOD(second_addr, addr_last);
1124 long p = first_addr - 1;
1125 int done = (addr == first_addr - 1 || addr == second_addr);
1129 a2 = get_addressed_line_node(n);
1130 b2 = get_addressed_line_node(p);
1131 current_addr = second_addr;
1132 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1133 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1137 a1 = get_addressed_line_node(n);
1138 if (addr < first_addr) {
1139 b1 = get_addressed_line_node(p);
1140 b2 = get_addressed_line_node(addr);
1141 /* this get_addressed_line_node last! */
1143 b2 = get_addressed_line_node(addr);
1144 b1 = get_addressed_line_node(p);
1145 /* this get_addressed_line_node last! */
1148 REQUE(b2, b1->q_forw);
1149 REQUE(a1->q_back, a2);
1151 current_addr = addr + ((addr < first_addr) ?
1152 second_addr - first_addr + 1 : 0);
1155 unset_active_nodes(b2->q_forw, a2);
1162 /* copy_lines: copy a range of lines; return status */
1164 copy_lines(long addr)
1166 line_t *lp, *np = get_addressed_line_node(first_addr);
1168 long n = second_addr - first_addr + 1;
1171 current_addr = addr;
1172 if (first_addr <= addr && addr < second_addr) {
1173 n = addr - first_addr + 1;
1174 m = second_addr - addr;
1176 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1177 for (; n-- > 0; np = np->q_forw) {
1179 if ((lp = dup_line_node(np)) == NULL) {
1186 else if ((up = push_undo_stack(UADD, current_addr,
1187 current_addr)) == NULL) {
1198 /* delete_lines: delete a range of lines */
1200 delete_lines(long from, long to)
1205 if (push_undo_stack(UDEL, from, to) == NULL) {
1209 n = get_addressed_line_node(INC_MOD(to, addr_last));
1210 p = get_addressed_line_node(from - 1);
1211 /* this get_addressed_line_node last! */
1213 unset_active_nodes(p->q_forw, n);
1215 addr_last -= to - from + 1;
1216 current_addr = from - 1;
1223 /* display_lines: print a range of lines to stdout */
1225 display_lines(long from, long to, int gflag)
1232 errmsg = "invalid address";
1235 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1236 bp = get_addressed_line_node(from);
1237 for (; bp != ep; bp = bp->q_forw) {
1238 if ((s = get_sbuf_line(bp)) == NULL)
1240 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1247 #define MAXMARK 26 /* max number of marks */
1249 line_t *mark[MAXMARK]; /* line markers */
1250 int markno; /* line marker count */
1252 /* mark_line_node: set a line node mark */
1254 mark_line_node(line_t *lp, int n)
1256 if (!islower((unsigned char)n)) {
1257 errmsg = "invalid mark character";
1259 } else if (mark[n - 'a'] == NULL)
1266 /* get_marked_node_addr: return address of a marked line */
1268 get_marked_node_addr(int n)
1270 if (!islower((unsigned char)n)) {
1271 errmsg = "invalid mark character";
1274 return get_line_node_addr(mark[n - 'a']);
1278 /* unmark_line_node: clear line node mark */
1280 unmark_line_node(line_t *lp)
1284 for (i = 0; markno && i < MAXMARK; i++)
1285 if (mark[i] == lp) {
1292 /* dup_line_node: return a pointer to a copy of a line node */
1294 dup_line_node(line_t *lp)
1298 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1299 fprintf(stderr, "%s\n", strerror(errno));
1300 errmsg = "out of memory";
1303 np->seek = lp->seek;
1309 /* has_trailing_escape: return the parity of escapes preceding a character
1312 has_trailing_escape(char *s, char *t)
1314 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1318 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1320 strip_escapes(char *s)
1322 static char *file = NULL;
1323 static int filesz = 0;
1327 REALLOC(file, filesz, PATH_MAX, NULL);
1328 while (i < filesz - 1 /* Worry about a possible trailing escape */
1329 && (file[i++] = (*s == '\\') ? *++s : *s))
1336 signal_hup(int signo)
1339 sigflags |= (1 << (signo - 1));
1346 signal_int(int signo)
1349 sigflags |= (1 << (signo - 1));
1356 handle_hup(int signo)
1358 char *hup = NULL; /* hup filename */
1360 char ed_hup[] = "ed.hup";
1365 sigflags &= ~(1 << (signo - 1));
1366 if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 &&
1367 (s = getenv("HOME")) != NULL &&
1368 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1369 (hup = (char *) malloc(n + 10)) != NULL) {
1371 if (hup[n - 1] != '/')
1372 hup[n] = '/', hup[n+1] = '\0';
1373 strcat(hup, "ed.hup");
1374 write_file(hup, "w", 1, addr_last);
1381 handle_int(int signo)
1385 sigflags &= ~(1 << (signo - 1));
1386 #ifdef _POSIX_SOURCE
1387 siglongjmp(env, -1);
1394 int cols = 72; /* wrap column */
1397 handle_winch(int signo)
1399 int save_errno = errno;
1401 struct winsize ws; /* window size structure */
1403 sigflags &= ~(1 << (signo - 1));
1404 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1405 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1406 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1412 /* is_legal_filename: return a legal filename */
1414 is_legal_filename(char *s)
1416 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1417 errmsg = "shell access restricted";