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: head/bin/ed/main.c 241720 2012-10-19 05:43:38Z ed $
36 * This program is based on the editor algorithm described in
37 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
38 * in Pascal," Addison-Wesley, 1981.
40 * The buffering algorithm is attributed to Rodney Ruddock of
41 * the University of Guelph, Guelph, Ontario.
43 * The cbc.c encryption code is adapted from
44 * the bdes program by Matt Bishop of Dartmouth College,
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
62 static sigjmp_buf env;
68 char stdinbuf[1]; /* stdin buffer */
69 static char *shcmd; /* shell command buffer */
70 static int shcmdsz; /* shell command buffer size */
71 static int shcmdi; /* shell command buffer index */
72 char *ibuf; /* ed command-line buffer */
73 int ibufsz; /* ed command-line buffer size */
74 char *ibufp; /* pointer to ed command-line buffer */
77 int des = 0; /* if set, use crypt(3) for i/o */
78 static int garrulous = 0; /* if set, print all error messages */
79 int isbinary; /* if set, buffer contains ASCII NULs */
80 int isglobal; /* if set, doing a global command */
81 int modified; /* if set, buffer modified since last write */
82 int mutex = 0; /* if set, signals set "sigflags" */
83 static int red = 0; /* if set, restrict shell/directory access */
84 int scripted = 0; /* if set, suppress diagnostics */
85 int sigflags = 0; /* if set, signals received while mutex set */
86 static int sigactive = 0; /* if set, signal handlers are enabled */
88 static char old_filename[PATH_MAX] = ""; /* default filename */
89 long current_addr; /* current address in editor buffer */
90 long addr_last; /* last address in editor buffer */
91 int lineno; /* script line number */
92 static const char *prompt; /* command-line prompt */
93 static const char *dps = "*"; /* default command-line prompt */
95 static const char *usage = "usage: %s [-] [-sx] [-p string] [file]\n";
99 main(volatile int argc, char ** volatile argv)
104 setlocale(LC_ALL, "");
106 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
108 while ((c = getopt(argc, argv, "p:sx")) != -1)
110 case 'p': /* set prompt */
113 case 's': /* run script */
116 case 'x': /* use crypt */
120 fprintf(stderr, "crypt unavailable\n?\n");
125 fprintf(stderr, usage, red ? "red" : "ed");
130 if (argc && **argv == '-') {
139 /* assert: reliable signals! */
141 handle_winch(SIGWINCH);
142 if (isatty(0)) signal(SIGWINCH, handle_winch);
144 signal(SIGHUP, signal_hup);
145 signal(SIGQUIT, SIG_IGN);
146 signal(SIGINT, signal_int);
148 if ((status = sigsetjmp(env, 1)))
150 if ((status = setjmp(env)))
153 fputs("\n?\n", stderr);
154 errmsg = "interrupt";
157 sigactive = 1; /* enable signal handlers */
158 if (argc && **argv && is_legal_filename(*argv)) {
159 if (read_file(*argv, 0) < 0 && !isatty(0))
161 else if (**argv != '!')
162 if (strlcpy(old_filename, *argv, sizeof(old_filename))
163 >= sizeof(old_filename))
166 fputs("?\n", stderr);
168 errmsg = "invalid filename";
174 if (status < 0 && garrulous)
175 fprintf(stderr, "%s\n", errmsg);
177 printf("%s", prompt);
180 if ((n = get_tty_line()) < 0) {
184 if (modified && !scripted) {
185 fputs("?\n", stderr);
186 errmsg = "warning: file modified";
190 "script, line %d: %s\n",
200 } else if (ibuf[n - 1] != '\n') {
202 errmsg = "unexpected end-of-file";
208 if ((status = extract_addr_range()) >= 0 &&
209 (status = exec_command()) >= 0)
211 (status = display_lines(current_addr, current_addr,
219 fputs("?\n", stderr); /* give warning */
220 errmsg = "warning: file modified";
223 fprintf(stderr, "script, line %d: %s\n",
231 fprintf(stderr, "script, line %d: %s\n",
233 } else if (garrulous)
234 fprintf(stderr, "%s\n", errmsg);
237 fputs("?\n", stderr);
240 fprintf(stderr, "script, line %d: %s\n",
250 long first_addr, second_addr;
251 static long addr_cnt;
253 /* extract_addr_range: get line addresses from the command buffer until an
254 illegal address is seen; return status */
256 extract_addr_range(void)
261 first_addr = second_addr = current_addr;
262 while ((addr = next_addr()) >= 0) {
264 first_addr = second_addr;
266 if (*ibufp != ',' && *ibufp != ';')
268 else if (*ibufp++ == ';')
271 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
272 first_addr = second_addr;
273 return (addr == ERR) ? ERR : 0;
277 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++
279 #define MUST_BE_FIRST() do { \
281 errmsg = "invalid address"; \
286 /* next_addr: return the next line address in the command buffer */
291 long addr = current_addr;
297 for (hd = ibufp;; first = 0)
298 switch (c = *ibufp) {
306 if (isdigit((unsigned char)*ibufp)) {
308 addr += (c == '-' || c == '^') ? -n : n;
309 } else if (!isspace((unsigned char)c))
310 addr += (c == '-' || c == '^') ? -1 : 1;
312 case '0': case '1': case '2':
313 case '3': case '4': case '5':
314 case '6': case '7': case '8': case '9':
322 addr = (c == '.') ? current_addr : addr_last;
327 if ((addr = get_matching_node_addr(
328 get_compiled_pattern(), c == '/')) < 0)
330 else if (c == *ibufp)
336 if ((addr = get_marked_node_addr(*ibufp++)) < 0)
345 second_addr = (c == ';') ? current_addr : 1;
353 else if (addr < 0 || addr_last < addr) {
354 errmsg = "invalid address";
364 /* GET_THIRD_ADDR: get a legal address from the command buffer */
365 #define GET_THIRD_ADDR(addr) \
369 ol1 = first_addr, ol2 = second_addr; \
370 if (extract_addr_range() < 0) \
372 else if (addr_cnt == 0) { \
373 errmsg = "destination expected"; \
375 } else if (second_addr < 0 || addr_last < second_addr) { \
376 errmsg = "invalid address"; \
379 addr = second_addr; \
380 first_addr = ol1, second_addr = ol2; \
382 #else /* BACKWARDS */
383 /* GET_THIRD_ADDR: get a legal address from the command buffer */
384 #define GET_THIRD_ADDR(addr) \
388 ol1 = first_addr, ol2 = second_addr; \
389 if (extract_addr_range() < 0) \
391 if (second_addr < 0 || addr_last < second_addr) { \
392 errmsg = "invalid address"; \
395 addr = second_addr; \
396 first_addr = ol1, second_addr = ol2; \
401 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
402 #define GET_COMMAND_SUFFIX() { \
407 gflag |= GPR, ibufp++; \
410 gflag |= GLS, ibufp++; \
413 gflag |= GNP, ibufp++; \
419 if (*ibufp++ != '\n') { \
420 errmsg = "invalid command suffix"; \
427 #define SGG 001 /* complement previous global substitute suffix */
428 #define SGP 002 /* complement previous print suffix */
429 #define SGR 004 /* use last regex instead of last pat */
430 #define SGF 010 /* repeat last substitution */
432 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
434 long rows = 22; /* scroll length: ws_row - 2 */
436 /* exec_command: execute the next command in command buffer; return print
441 static pattern_t *pat = NULL;
442 static int sgflag = 0;
443 static long sgnum = 0;
454 switch(c = *ibufp++) {
456 GET_COMMAND_SUFFIX();
457 if (!isglobal) clear_undo_stack();
458 if (append_lines(second_addr) < 0)
462 if (check_addr_range(current_addr, current_addr) < 0)
464 GET_COMMAND_SUFFIX();
465 if (!isglobal) clear_undo_stack();
466 if (delete_lines(first_addr, second_addr) < 0 ||
467 append_lines(current_addr) < 0)
471 if (check_addr_range(current_addr, current_addr) < 0)
473 GET_COMMAND_SUFFIX();
474 if (!isglobal) clear_undo_stack();
475 if (delete_lines(first_addr, second_addr) < 0)
477 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
481 if (modified && !scripted)
486 errmsg = "unexpected address";
488 } else if (!isspace((unsigned char)*ibufp)) {
489 errmsg = "unexpected command suffix";
491 } else if ((fnp = get_filename()) == NULL)
493 GET_COMMAND_SUFFIX();
494 if (delete_lines(1, addr_last) < 0)
497 if (close_sbuf() < 0)
499 else if (open_sbuf() < 0)
501 if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
503 if (*fnp == '\0' && *old_filename == '\0') {
504 errmsg = "no current filename";
508 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
512 u_current_addr = u_addr_last = -1;
516 errmsg = "unexpected address";
518 } else if (!isspace((unsigned char)*ibufp)) {
519 errmsg = "unexpected command suffix";
521 } else if ((fnp = get_filename()) == NULL)
523 else if (*fnp == '!') {
524 errmsg = "invalid redirection";
527 GET_COMMAND_SUFFIX();
528 if (*fnp) strcpy(old_filename, fnp);
529 printf("%s\n", strip_escapes(old_filename));
536 errmsg = "cannot nest global commands";
538 } else if (check_addr_range(1, addr_last) < 0)
540 else if (build_active_list(c == 'g' || c == 'G') < 0)
542 else if ((n = (c == 'G' || c == 'V')))
543 GET_COMMAND_SUFFIX();
545 if (exec_global(n, gflag) < 0)
550 errmsg = "unexpected address";
553 GET_COMMAND_SUFFIX();
554 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
558 errmsg = "unexpected address";
561 GET_COMMAND_SUFFIX();
562 if ((garrulous = 1 - garrulous) && *errmsg)
563 fprintf(stderr, "%s\n", errmsg);
566 if (second_addr == 0) {
567 errmsg = "invalid address";
570 GET_COMMAND_SUFFIX();
571 if (!isglobal) clear_undo_stack();
572 if (append_lines(second_addr - 1) < 0)
576 if (check_addr_range(current_addr, current_addr + 1) < 0)
578 GET_COMMAND_SUFFIX();
579 if (!isglobal) clear_undo_stack();
580 if (first_addr != second_addr &&
581 join_lines(first_addr, second_addr) < 0)
586 if (second_addr == 0) {
587 errmsg = "invalid address";
590 GET_COMMAND_SUFFIX();
591 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
595 if (check_addr_range(current_addr, current_addr) < 0)
597 GET_COMMAND_SUFFIX();
598 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
603 if (check_addr_range(current_addr, current_addr) < 0)
605 GET_THIRD_ADDR(addr);
606 if (first_addr <= addr && addr < second_addr) {
607 errmsg = "invalid destination";
610 GET_COMMAND_SUFFIX();
611 if (!isglobal) clear_undo_stack();
612 if (move_lines(addr) < 0)
616 if (check_addr_range(current_addr, current_addr) < 0)
618 GET_COMMAND_SUFFIX();
619 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
624 if (check_addr_range(current_addr, current_addr) < 0)
626 GET_COMMAND_SUFFIX();
627 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
633 errmsg = "unexpected address";
636 GET_COMMAND_SUFFIX();
637 prompt = prompt ? NULL : optarg ? optarg : dps;
642 errmsg = "unexpected address";
645 GET_COMMAND_SUFFIX();
646 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
649 if (!isspace((unsigned char)*ibufp)) {
650 errmsg = "unexpected command suffix";
652 } else if (addr_cnt == 0)
653 second_addr = addr_last;
654 if ((fnp = get_filename()) == NULL)
656 GET_COMMAND_SUFFIX();
657 if (!isglobal) clear_undo_stack();
658 if (*old_filename == '\0' && *fnp != '!')
659 strcpy(old_filename, fnp);
661 if (*fnp == '\0' && *old_filename == '\0') {
662 errmsg = "no current filename";
666 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
668 else if (addr && addr != addr_last)
689 case '0': case '1': case '2': case '3': case '4':
690 case '5': case '6': case '7': case '8': case '9':
691 STRTOL(sgnum, ibufp);
693 sgflag &= ~GSG; /* override GSG */
697 errmsg = "invalid command suffix";
701 } while (sflags && *ibufp != '\n');
702 if (sflags && !pat) {
703 errmsg = "no previous substitution";
705 } else if (sflags & SGG)
706 sgnum = 0; /* override numeric arg */
707 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
708 errmsg = "invalid pattern delimiter";
713 if ((!sflags || (sflags & SGR)) &&
714 (tpat = get_compiled_pattern()) == NULL) {
717 } else if (tpat != pat) {
723 patlock = 1; /* reserve pattern */
726 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
735 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
739 sgflag |= GPR, ibufp++;
742 sgflag |= GLS, ibufp++;
745 sgflag |= GNP, ibufp++;
751 if (check_addr_range(current_addr, current_addr) < 0)
753 GET_COMMAND_SUFFIX();
754 if (!isglobal) clear_undo_stack();
755 if (search_and_replace(pat, sgflag, sgnum) < 0)
759 if (check_addr_range(current_addr, current_addr) < 0)
761 GET_THIRD_ADDR(addr);
762 GET_COMMAND_SUFFIX();
763 if (!isglobal) clear_undo_stack();
764 if (copy_lines(addr) < 0)
769 errmsg = "unexpected address";
772 GET_COMMAND_SUFFIX();
773 if (pop_undo_stack() < 0)
778 if ((n = *ibufp) == 'q' || n == 'Q') {
782 if (!isspace((unsigned char)*ibufp)) {
783 errmsg = "unexpected command suffix";
785 } else if ((fnp = get_filename()) == NULL)
787 if (addr_cnt == 0 && !addr_last)
788 first_addr = second_addr = 0;
789 else if (check_addr_range(1, addr_last) < 0)
791 GET_COMMAND_SUFFIX();
792 if (*old_filename == '\0' && *fnp != '!')
793 strcpy(old_filename, fnp);
795 if (*fnp == '\0' && *old_filename == '\0') {
796 errmsg = "no current filename";
800 if ((addr = write_file(*fnp ? fnp : old_filename,
801 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
803 else if (addr == addr_last)
805 else if (modified && !scripted && n == 'q')
810 errmsg = "unexpected address";
813 GET_COMMAND_SUFFIX();
818 errmsg = "crypt unavailable";
823 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
825 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
828 else if ('0' < *ibufp && *ibufp <= '9')
830 GET_COMMAND_SUFFIX();
831 if (display_lines(second_addr, min(addr_last,
832 second_addr + rows), gflag) < 0)
837 GET_COMMAND_SUFFIX();
838 printf("%ld\n", addr_cnt ? second_addr : addr_last);
842 errmsg = "unexpected address";
844 } else if ((sflags = get_shell_command()) < 0)
846 GET_COMMAND_SUFFIX();
847 if (sflags) printf("%s\n", shcmd + 1);
849 if (!scripted) printf("!\n");
853 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
855 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
857 || display_lines(second_addr, second_addr, 0) < 0)
861 errmsg = "unknown command";
868 /* check_addr_range: return status of address range check */
870 check_addr_range(long n, long m)
876 if (first_addr > second_addr || 1 > first_addr ||
877 second_addr > addr_last) {
878 errmsg = "invalid address";
885 /* get_matching_node_addr: return the address of the next line matching a
886 pattern in a given direction. wrap around begin/end of editor buffer if
889 get_matching_node_addr(pattern_t *pat, int dir)
892 long n = current_addr;
895 if (!pat) return ERR;
897 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) {
898 lp = get_addressed_line_node(n);
899 if ((s = get_sbuf_line(lp)) == NULL)
902 NUL_TO_NEWLINE(s, lp->len);
903 if (!regexec(pat, s, 0, NULL, 0))
906 } while (n != current_addr);
912 /* get_filename: return pointer to copy of filename in the command buffer */
916 static char *file = NULL;
917 static int filesz = 0;
921 if (*ibufp != '\n') {
923 if (*ibufp == '\n') {
924 errmsg = "invalid filename";
926 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
928 else if (*ibufp == '!') {
930 if ((n = get_shell_command()) < 0)
933 printf("%s\n", shcmd + 1);
935 } else if (n > PATH_MAX - 1) {
936 errmsg = "filename too long";
941 else if (*old_filename == '\0') {
942 errmsg = "no current filename";
946 REALLOC(file, filesz, PATH_MAX, NULL);
947 for (n = 0; *ibufp != '\n';)
948 file[n++] = *ibufp++;
950 return is_legal_filename(file) ? file : NULL;
954 /* get_shell_command: read a shell command from stdin; return substitution
957 get_shell_command(void)
959 static char *buf = NULL;
962 char *s; /* substitution char pointer */
967 errmsg = "shell access restricted";
969 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
971 REALLOC(buf, n, j + 1, ERR);
972 buf[i++] = '!'; /* prefix command w/ bang */
973 while (*ibufp != '\n')
976 REALLOC(buf, n, i + 2, ERR);
978 if (*ibufp++ == '\\')
983 REALLOC(buf, n, i + 1, ERR);
987 else if (shcmd == NULL || *(shcmd + 1) == '\0')
989 else if (shcmd == NULL)
992 errmsg = "no previous command";
995 REALLOC(buf, n, i + shcmdi, ERR);
996 for (s = shcmd + 1; s < shcmd + shcmdi;)
1002 if (*old_filename == '\0') {
1003 errmsg = "no current filename";
1006 j = strlen(s = strip_escapes(old_filename));
1007 REALLOC(buf, n, i + j, ERR);
1013 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1014 memcpy(shcmd, buf, i);
1015 shcmd[shcmdi = i] = '\0';
1016 return *s == '!' || *s == '%';
1020 /* append_lines: insert text from stdin to after line n; stop when either a
1021 single period is read or EOF; return status */
1023 append_lines(long n)
1026 const char *lp = ibuf;
1030 for (current_addr = n;;) {
1032 if ((l = get_tty_line()) < 0)
1034 else if (l == 0 || ibuf[l - 1] != '\n') {
1039 } else if (*(lp = ibufp) == '\0')
1042 while (*ibufp++ != '\n')
1046 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1052 if ((lp = put_sbuf_line(lp)) == NULL) {
1056 up->t = get_addressed_line_node(current_addr);
1057 else if ((up = push_undo_stack(UADD, current_addr,
1058 current_addr)) == NULL) {
1062 } while (lp != eot);
1070 /* join_lines: replace a range of lines with the joined text of those lines */
1072 join_lines(long from, long to)
1074 static char *buf = NULL;
1081 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1082 bp = get_addressed_line_node(from);
1083 for (; bp != ep; bp = bp->q_forw) {
1084 if ((s = get_sbuf_line(bp)) == NULL)
1086 REALLOC(buf, n, size + bp->len, ERR);
1087 memcpy(buf + size, s, bp->len);
1090 REALLOC(buf, n, size + 2, ERR);
1091 memcpy(buf + size, "\n", 2);
1092 if (delete_lines(from, to) < 0)
1094 current_addr = from - 1;
1096 if (put_sbuf_line(buf) == NULL ||
1097 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1107 /* move_lines: move a range of lines */
1109 move_lines(long addr)
1111 line_t *b1, *a1, *b2, *a2;
1112 long n = INC_MOD(second_addr, addr_last);
1113 long p = first_addr - 1;
1114 int done = (addr == first_addr - 1 || addr == second_addr);
1118 a2 = get_addressed_line_node(n);
1119 b2 = get_addressed_line_node(p);
1120 current_addr = second_addr;
1121 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1122 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1126 a1 = get_addressed_line_node(n);
1127 if (addr < first_addr) {
1128 b1 = get_addressed_line_node(p);
1129 b2 = get_addressed_line_node(addr);
1130 /* this get_addressed_line_node last! */
1132 b2 = get_addressed_line_node(addr);
1133 b1 = get_addressed_line_node(p);
1134 /* this get_addressed_line_node last! */
1137 REQUE(b2, b1->q_forw);
1138 REQUE(a1->q_back, a2);
1140 current_addr = addr + ((addr < first_addr) ?
1141 second_addr - first_addr + 1 : 0);
1144 unset_active_nodes(b2->q_forw, a2);
1151 /* copy_lines: copy a range of lines; return status */
1153 copy_lines(long addr)
1155 line_t *lp, *np = get_addressed_line_node(first_addr);
1157 long n = second_addr - first_addr + 1;
1160 current_addr = addr;
1161 if (first_addr <= addr && addr < second_addr) {
1162 n = addr - first_addr + 1;
1163 m = second_addr - addr;
1165 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1166 for (; n-- > 0; np = np->q_forw) {
1168 if ((lp = dup_line_node(np)) == NULL) {
1175 else if ((up = push_undo_stack(UADD, current_addr,
1176 current_addr)) == NULL) {
1187 /* delete_lines: delete a range of lines */
1189 delete_lines(long from, long to)
1194 if (push_undo_stack(UDEL, from, to) == NULL) {
1198 n = get_addressed_line_node(INC_MOD(to, addr_last));
1199 p = get_addressed_line_node(from - 1);
1200 /* this get_addressed_line_node last! */
1202 unset_active_nodes(p->q_forw, n);
1204 addr_last -= to - from + 1;
1205 current_addr = from - 1;
1212 /* display_lines: print a range of lines to stdout */
1214 display_lines(long from, long to, int gflag)
1221 errmsg = "invalid address";
1224 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1225 bp = get_addressed_line_node(from);
1226 for (; bp != ep; bp = bp->q_forw) {
1227 if ((s = get_sbuf_line(bp)) == NULL)
1229 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1236 #define MAXMARK 26 /* max number of marks */
1238 static line_t *mark[MAXMARK]; /* line markers */
1239 static int markno; /* line marker count */
1241 /* mark_line_node: set a line node mark */
1243 mark_line_node(line_t *lp, int n)
1245 if (!islower((unsigned char)n)) {
1246 errmsg = "invalid mark character";
1248 } else if (mark[n - 'a'] == NULL)
1255 /* get_marked_node_addr: return address of a marked line */
1257 get_marked_node_addr(int n)
1259 if (!islower((unsigned char)n)) {
1260 errmsg = "invalid mark character";
1263 return get_line_node_addr(mark[n - 'a']);
1267 /* unmark_line_node: clear line node mark */
1269 unmark_line_node(line_t *lp)
1273 for (i = 0; markno && i < MAXMARK; i++)
1274 if (mark[i] == lp) {
1281 /* dup_line_node: return a pointer to a copy of a line node */
1283 dup_line_node(line_t *lp)
1287 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1288 fprintf(stderr, "%s\n", strerror(errno));
1289 errmsg = "out of memory";
1292 np->seek = lp->seek;
1298 /* has_trailing_escape: return the parity of escapes preceding a character
1301 has_trailing_escape(char *s, char *t)
1303 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1307 /* strip_escapes: return copy of escaped string of at most length PATH_MAX */
1309 strip_escapes(char *s)
1311 static char *file = NULL;
1312 static int filesz = 0;
1316 REALLOC(file, filesz, PATH_MAX, NULL);
1317 while (i < filesz - 1 /* Worry about a possible trailing escape */
1318 && (file[i++] = (*s == '\\') ? *++s : *s))
1325 signal_hup(int signo)
1328 sigflags |= (1 << (signo - 1));
1335 signal_int(int signo)
1338 sigflags |= (1 << (signo - 1));
1345 handle_hup(int signo)
1347 char *hup = NULL; /* hup filename */
1349 char ed_hup[] = "ed.hup";
1354 sigflags &= ~(1 << (signo - 1));
1355 if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 &&
1356 (s = getenv("HOME")) != NULL &&
1357 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */
1358 (hup = (char *) malloc(n + 10)) != NULL) {
1360 if (hup[n - 1] != '/')
1361 hup[n] = '/', hup[n+1] = '\0';
1362 strcat(hup, "ed.hup");
1363 write_file(hup, "w", 1, addr_last);
1370 handle_int(int signo)
1374 sigflags &= ~(1 << (signo - 1));
1375 #ifdef _POSIX_SOURCE
1376 siglongjmp(env, -1);
1383 int cols = 72; /* wrap column */
1386 handle_winch(int signo)
1388 int save_errno = errno;
1390 struct winsize ws; /* window size structure */
1392 sigflags &= ~(1 << (signo - 1));
1393 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1394 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1395 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1401 /* is_legal_filename: return a legal filename */
1403 is_legal_filename(char *s)
1405 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1406 errmsg = "shell access restricted";