Remove building with NOCRYPTO option
[minix.git] / bin / ed / main.c
blobaa6801e2b14c982484f1124354ecae6952d84756
1 /* $NetBSD: main.c,v 1.28.8.2 2018/06/22 10:08:22 martin Exp $ */
3 /* main.c: This file contains the main control and user-interface routines
4 for the ed line editor. */
5 /*-
6 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __COPYRIGHT(
34 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio.\
35 All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char *rcsid = "@(#)main.c,v 1.1 1994/02/01 00:34:42 alm Exp";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.28.8.2 2018/06/22 10:08:22 martin Exp $");
43 #endif
44 #endif /* not lint */
47 * CREDITS
49 * This program is based on the editor algorithm described in
50 * Brian W. Kernighan and P. J. Plauger's book "Software Tools
51 * in Pascal," Addison-Wesley, 1981.
53 * The buffering algorithm is attributed to Rodney Ruddock of
54 * the University of Guelph, Guelph, Ontario.
56 * The cbc.c encryption code is adapted from
57 * the bdes program by Matt Bishop of Dartmouth College,
58 * Hanover, NH.
62 #include <sys/ioctl.h>
63 #include <sys/wait.h>
64 #include <termios.h>
65 #include <ctype.h>
66 #include <setjmp.h>
67 #include <pwd.h>
69 #include "ed.h"
72 #ifdef _POSIX_SOURCE
73 sigjmp_buf env;
74 #else
75 jmp_buf env;
76 #endif
78 /* static buffers */
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 */
87 /* global flags */
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 ere = 0; /* if set, use extended regexes */
96 int scripted = 0; /* if set, suppress diagnostics */
97 int secure = 0; /* is set, ! is not allowed */
98 int sigflags = 0; /* if set, signals received while mutex set */
99 int sigactive = 0; /* if set, signal handlers are enabled */
101 char old_filename[MAXPATHLEN + 1] = ""; /* default filename */
102 long current_addr; /* current address in editor buffer */
103 long addr_last; /* last address in editor buffer */
104 int lineno; /* script line number */
105 const char *prompt; /* command-line prompt */
106 const char *dps = "*"; /* default command-line prompt */
109 static const char usage[] = "Usage: %s [-] [-ESsx] [-p string] [name]\n";
111 /* ed: line editor */
113 main(int ac, char *av[])
115 int c, n;
116 long status = 0;
117 volatile int argc = ac;
118 char ** volatile argv = av;
120 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
121 top:
122 while ((c = getopt(argc, argv, "p:sxES")) != -1)
123 switch(c) {
124 case 'p': /* set prompt */
125 prompt = optarg;
126 break;
127 case 's': /* run script */
128 scripted = 1;
129 break;
130 case 'x': /* use crypt */
131 #ifdef DES
132 des = get_keyword();
133 #else
134 fprintf(stderr, "crypt unavailable\n?\n");
135 #endif
136 break;
138 case 'E':
139 ere = REG_EXTENDED;
140 break;
141 case 'S': /* ! is not allowed */
142 secure = 1;
143 break;
144 default:
145 fprintf(stderr, usage, getprogname());
146 exit(1);
147 /* NOTREACHED */
149 argv += optind;
150 argc -= optind;
151 if (argc && **argv == '-') {
152 scripted = 1;
153 if (argc > 1) {
154 optind = 1;
155 goto top;
157 argv++;
158 argc--;
160 /* assert: reliable signals! */
161 #ifdef SIGWINCH
162 handle_winch(SIGWINCH);
163 if (isatty(0)) signal(SIGWINCH, handle_winch);
164 #endif
165 signal(SIGHUP, signal_hup);
166 signal(SIGQUIT, SIG_IGN);
167 signal(SIGINT, signal_int);
168 #ifdef _POSIX_SOURCE
169 if ((status = sigsetjmp(env, 1)) != 0)
170 #else
171 if ((status = setjmp(env)) != 0)
172 #endif
174 fputs("\n?\n", stderr);
175 seterrmsg("interrupt");
176 } else {
177 init_buffers();
178 sigactive = 1; /* enable signal handlers */
179 if (argc && **argv && is_legal_filename(*argv)) {
180 if (read_file(*argv, 0) < 0 && !isatty(0))
181 quit(2);
182 else if (**argv != '!')
183 strlcpy(old_filename, *argv,
184 sizeof(old_filename) - 2);
185 } else if (argc) {
186 fputs("?\n", stderr);
187 if (**argv == '\0')
188 seterrmsg("invalid filename");
189 if (!isatty(0))
190 quit(2);
193 for (;;) {
194 if (status < 0 && garrulous)
195 fprintf(stderr, "%s\n", errmsg);
196 if (prompt) {
197 printf("%s", prompt);
198 fflush(stdout);
200 if ((n = get_tty_line()) < 0) {
201 status = ERR;
202 continue;
203 } else if (n == 0) {
204 if (modified && !scripted) {
205 fputs("?\n", stderr);
206 seterrmsg("warning: file modified");
207 if (!isatty(0)) {
208 if (garrulous) {
209 fprintf(stderr,
210 "script, line %d: %s\n",
211 lineno, errmsg);
213 quit(2);
215 clearerr(stdin);
216 modified = 0;
217 status = EMOD;
218 continue;
219 } else
220 quit(0);
221 } else if (ibuf[n - 1] != '\n') {
222 /* discard line */
223 seterrmsg("unexpected end-of-file");
224 clearerr(stdin);
225 status = ERR;
226 continue;
228 isglobal = 0;
229 if ((status = extract_addr_range()) >= 0 &&
230 (status = exec_command()) >= 0) {
231 if (status == 0)
232 continue;
233 status = display_lines(current_addr, current_addr,
234 status);
235 if (status >= 0)
236 continue;
238 switch (status) {
239 case EOF:
240 quit(0);
241 case EMOD:
242 modified = 0;
243 fputs("?\n", stderr); /* give warning */
244 seterrmsg("warning: file modified");
245 if (!isatty(0)) {
246 if (garrulous) {
247 fprintf(stderr,
248 "script, line %d: %s\n",
249 lineno, errmsg);
251 quit(2);
253 break;
254 case FATAL:
255 if (garrulous) {
256 if (!isatty(0)) {
257 fprintf(stderr,
258 "script, line %d: %s\n",
259 lineno, errmsg);
260 } else {
261 fprintf(stderr, "%s\n", errmsg);
264 quit(3);
265 default:
266 fputs("?\n", stderr);
267 if (!isatty(0)) {
268 if (garrulous) {
269 fprintf(stderr, "script, line %d: %s\n",
270 lineno, errmsg);
272 quit(2);
274 break;
277 /* NOTREACHED */
280 long first_addr, second_addr, addr_cnt;
282 /* extract_addr_range: get line addresses from the command buffer until an
283 illegal address is seen; return status */
285 extract_addr_range(void)
287 long addr;
289 addr_cnt = 0;
290 first_addr = second_addr = current_addr;
291 while ((addr = next_addr()) >= 0) {
292 addr_cnt++;
293 first_addr = second_addr;
294 second_addr = addr;
295 if (*ibufp != ',' && *ibufp != ';')
296 break;
297 else if (*ibufp++ == ';')
298 current_addr = addr;
300 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
301 first_addr = second_addr;
302 return (addr == ERR) ? ERR : 0;
306 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') \
307 ibufp++
309 #define MUST_BE_FIRST() \
310 if (!first) { seterrmsg("invalid address"); return ERR; }
312 /* next_addr: return the next line address in the command buffer */
313 long
314 next_addr(void)
316 char *hd;
317 long addr = current_addr;
318 long n;
319 int first = 1;
320 int c;
322 SKIP_BLANKS();
323 for (hd = ibufp;; first = 0)
324 switch (c = *ibufp) {
325 case '+':
326 case '\t':
327 case ' ':
328 case '-':
329 case '^':
330 ibufp++;
331 SKIP_BLANKS();
332 if (isdigit((unsigned char)*ibufp)) {
333 STRTOL(n, ibufp);
334 addr += (c == '-' || c == '^') ? -n : n;
335 } else if (!isspace((unsigned char)c))
336 addr += (c == '-' || c == '^') ? -1 : 1;
337 break;
338 case '0': case '1': case '2':
339 case '3': case '4': case '5':
340 case '6': case '7': case '8': case '9':
341 MUST_BE_FIRST();
342 STRTOL(addr, ibufp);
343 break;
344 case '.':
345 case '$':
346 MUST_BE_FIRST();
347 ibufp++;
348 addr = (c == '.') ? current_addr : addr_last;
349 break;
350 case '/':
351 case '?':
352 MUST_BE_FIRST();
353 if ((addr = get_matching_node_addr(
354 get_compiled_pattern(), c == '/')) < 0)
355 return ERR;
356 else if (c == *ibufp)
357 ibufp++;
358 break;
359 case '\'':
360 MUST_BE_FIRST();
361 ibufp++;
362 if ((addr = get_marked_node_addr((unsigned char)*ibufp++)) < 0)
363 return ERR;
364 break;
365 case '%':
366 case ',':
367 case ';':
368 if (first) {
369 ibufp++;
370 addr_cnt++;
371 second_addr = (c == ';') ? current_addr : 1;
372 addr = addr_last;
373 break;
375 /* FALL THROUGH */
376 default:
377 if (ibufp == hd)
378 return EOF;
379 else if (addr < 0 || addr_last < addr) {
380 seterrmsg("invalid address");
381 return ERR;
382 } else
383 return addr;
385 /* NOTREACHED */
389 #ifdef BACKWARDS
390 /* GET_THIRD_ADDR: get a legal address from the command buffer */
391 #define GET_THIRD_ADDR(addr) \
393 long ol1, ol2; \
395 ol1 = first_addr, ol2 = second_addr; \
396 if (extract_addr_range() < 0) \
397 return ERR; \
398 else if (addr_cnt == 0) { \
399 seterrmsg("destination expected"); \
400 return ERR; \
401 } else if (second_addr < 0 || addr_last < second_addr) { \
402 seterrmsg("invalid address"); \
403 return ERR; \
405 addr = second_addr; \
406 first_addr = ol1, second_addr = ol2; \
408 #else /* BACKWARDS */
409 /* GET_THIRD_ADDR: get a legal address from the command buffer */
410 #define GET_THIRD_ADDR(addr) \
412 long ol1, ol2; \
414 ol1 = first_addr, ol2 = second_addr; \
415 if (extract_addr_range() < 0) \
416 return ERR; \
417 if (second_addr < 0 || addr_last < second_addr) { \
418 seterrmsg("invalid address"); \
419 return ERR; \
421 addr = second_addr; \
422 first_addr = ol1, second_addr = ol2; \
424 #endif
427 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
428 #define GET_COMMAND_SUFFIX() { \
429 int done = 0; \
430 do { \
431 switch(*ibufp) { \
432 case 'p': \
433 gflag |= GPR, ibufp++; \
434 break; \
435 case 'l': \
436 gflag |= GLS, ibufp++; \
437 break; \
438 case 'n': \
439 gflag |= GNP, ibufp++; \
440 break; \
441 default: \
442 done++; \
444 } while (!done); \
445 if (*ibufp++ != '\n') { \
446 seterrmsg("invalid command suffix"); \
447 return ERR; \
452 /* sflags */
453 #define SGG 001 /* complement previous global substitute suffix */
454 #define SGP 002 /* complement previous print suffix */
455 #define SGR 004 /* use last regex instead of last pat */
456 #define SGF 010 /* repeat last substitution */
458 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */
460 long rows = 22; /* scroll length: ws_row - 2 */
462 /* exec_command: execute the next command in command buffer; return print
463 request, if any */
465 exec_command(void)
467 static pattern_t *pat = NULL;
468 static int sgflag = 0;
469 static long sgnum = 0;
471 pattern_t *tpat;
472 char *fnp;
473 int gflag = 0;
474 int sflags = 0;
475 long addr = 0;
476 int n = 0;
477 int c;
479 SKIP_BLANKS();
480 switch(c = *ibufp++) {
481 case 'a':
482 GET_COMMAND_SUFFIX();
483 if (!isglobal) clear_undo_stack();
484 if (append_lines(second_addr) < 0)
485 return ERR;
486 break;
487 case 'c':
488 if (check_addr_range(current_addr, current_addr) < 0)
489 return ERR;
490 GET_COMMAND_SUFFIX();
491 if (!isglobal) clear_undo_stack();
492 if (delete_lines(first_addr, second_addr) < 0 ||
493 append_lines(current_addr) < 0)
494 return ERR;
495 break;
496 case 'd':
497 if (check_addr_range(current_addr, current_addr) < 0)
498 return ERR;
499 GET_COMMAND_SUFFIX();
500 if (!isglobal) clear_undo_stack();
501 if (delete_lines(first_addr, second_addr) < 0)
502 return ERR;
503 else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
504 current_addr = addr;
505 break;
506 case 'e':
507 if (modified && !scripted)
508 return EMOD;
509 /* fall through */
510 case 'E':
511 if (addr_cnt > 0) {
512 seterrmsg("unexpected address");
513 return ERR;
514 } else if (!isspace((unsigned char)*ibufp)) {
515 seterrmsg("unexpected command suffix");
516 return ERR;
517 } else if ((fnp = get_filename()) == NULL)
518 return ERR;
519 GET_COMMAND_SUFFIX();
520 if (delete_lines(1, addr_last) < 0)
521 return ERR;
522 clear_undo_stack();
523 if (close_sbuf() < 0)
524 return ERR;
525 else if (open_sbuf() < 0)
526 return FATAL;
527 if (*fnp && *fnp != '!') strlcpy(old_filename, fnp,
528 sizeof(old_filename) - 2);
529 #ifdef BACKWARDS
530 if (*fnp == '\0' && *old_filename == '\0') {
531 seterrmsg("no current filename");
532 return ERR;
534 #endif
535 if (read_file(*fnp ? fnp : old_filename, 0) < 0)
536 return ERR;
537 clear_undo_stack();
538 modified = 0;
539 u_current_addr = u_addr_last = -1;
540 break;
541 case 'f':
542 if (addr_cnt > 0) {
543 seterrmsg("unexpected address");
544 return ERR;
545 } else if (!isspace((unsigned char)*ibufp)) {
546 seterrmsg("unexpected command suffix");
547 return ERR;
548 } else if ((fnp = get_filename()) == NULL)
549 return ERR;
550 else if (*fnp == '!') {
551 seterrmsg("invalid redirection");
552 return ERR;
554 GET_COMMAND_SUFFIX();
555 if (*fnp) strlcpy(old_filename, fnp, sizeof(old_filename) - 2);
556 printf("%s\n", strip_escapes(old_filename));
557 break;
558 case 'g':
559 case 'v':
560 case 'G':
561 case 'V':
562 if (isglobal) {
563 seterrmsg("cannot nest global commands");
564 return ERR;
565 } else if (check_addr_range(1, addr_last) < 0)
566 return ERR;
567 else if (build_active_list(c == 'g' || c == 'G') < 0)
568 return ERR;
569 else if ((n = (c == 'G' || c == 'V')) != 0)
570 GET_COMMAND_SUFFIX();
571 isglobal++;
572 if (exec_global(n, gflag) < 0)
573 return ERR;
574 break;
575 case 'h':
576 if (addr_cnt > 0) {
577 seterrmsg("unexpected address");
578 return ERR;
580 GET_COMMAND_SUFFIX();
581 if (*errmsg) fprintf(stderr, "%s\n", errmsg);
582 break;
583 case 'H':
584 if (addr_cnt > 0) {
585 seterrmsg("unexpected address");
586 return ERR;
588 GET_COMMAND_SUFFIX();
589 if ((garrulous = 1 - garrulous) && *errmsg)
590 fprintf(stderr, "%s\n", errmsg);
591 break;
592 case 'i':
593 if (second_addr == 0) {
594 seterrmsg("invalid address");
595 return ERR;
597 GET_COMMAND_SUFFIX();
598 if (!isglobal) clear_undo_stack();
599 if (append_lines(second_addr - 1) < 0)
600 return ERR;
601 break;
602 case 'j':
603 if (check_addr_range(current_addr, current_addr + 1) < 0)
604 return ERR;
605 GET_COMMAND_SUFFIX();
606 if (!isglobal) clear_undo_stack();
607 if (first_addr != second_addr &&
608 join_lines(first_addr, second_addr) < 0)
609 return ERR;
610 break;
611 case 'k':
612 c = *ibufp++;
613 if (second_addr == 0) {
614 seterrmsg("invalid address");
615 return ERR;
617 GET_COMMAND_SUFFIX();
618 if (mark_line_node(get_addressed_line_node(second_addr), (unsigned char)c) < 0)
619 return ERR;
620 break;
621 case 'l':
622 if (check_addr_range(current_addr, current_addr) < 0)
623 return ERR;
624 GET_COMMAND_SUFFIX();
625 if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
626 return ERR;
627 gflag = 0;
628 break;
629 case 'm':
630 if (check_addr_range(current_addr, current_addr) < 0)
631 return ERR;
632 GET_THIRD_ADDR(addr);
633 if (first_addr <= addr && addr < second_addr) {
634 seterrmsg("invalid destination");
635 return ERR;
637 GET_COMMAND_SUFFIX();
638 if (!isglobal) clear_undo_stack();
639 if (move_lines(addr) < 0)
640 return ERR;
641 break;
642 case 'n':
643 if (check_addr_range(current_addr, current_addr) < 0)
644 return ERR;
645 GET_COMMAND_SUFFIX();
646 if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
647 return ERR;
648 gflag = 0;
649 break;
650 case 'p':
651 if (check_addr_range(current_addr, current_addr) < 0)
652 return ERR;
653 GET_COMMAND_SUFFIX();
654 if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
655 return ERR;
656 gflag = 0;
657 break;
658 case 'P':
659 if (addr_cnt > 0) {
660 seterrmsg("unexpected address");
661 return ERR;
663 GET_COMMAND_SUFFIX();
664 prompt = prompt ? NULL : optarg ? optarg : dps;
665 break;
666 case 'q':
667 case 'Q':
668 if (addr_cnt > 0) {
669 seterrmsg("unexpected address");
670 return ERR;
672 GET_COMMAND_SUFFIX();
673 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF;
674 break;
675 case 'r':
676 if (!isspace((unsigned char)*ibufp)) {
677 seterrmsg("unexpected command suffix");
678 return ERR;
679 } else if (addr_cnt == 0)
680 second_addr = addr_last;
681 if ((fnp = get_filename()) == NULL)
682 return ERR;
683 GET_COMMAND_SUFFIX();
684 if (!isglobal) clear_undo_stack();
685 if (*old_filename == '\0' && *fnp != '!')
686 strlcpy(old_filename, fnp, sizeof(old_filename) - 2);
687 #ifdef BACKWARDS
688 if (*fnp == '\0' && *old_filename == '\0') {
689 seterrmsg("no current filename");
690 return ERR;
692 #endif
693 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
694 return ERR;
695 else if (addr && addr != addr_last)
696 modified = 1;
697 break;
698 case 's':
699 do {
700 switch(*ibufp) {
701 case '\n':
702 sflags |=SGF;
703 break;
704 case 'g':
705 sflags |= SGG;
706 ibufp++;
707 break;
708 case 'p':
709 sflags |= SGP;
710 ibufp++;
711 break;
712 case 'r':
713 sflags |= SGR;
714 ibufp++;
715 break;
716 case '0': case '1': case '2': case '3': case '4':
717 case '5': case '6': case '7': case '8': case '9':
718 STRTOL(sgnum, ibufp);
719 sflags |= SGF;
720 sgflag &= ~GSG; /* override GSG */
721 break;
722 default:
723 if (sflags) {
724 seterrmsg("invalid command suffix");
725 return ERR;
728 } while (sflags && *ibufp != '\n');
729 if (sflags && !pat) {
730 seterrmsg("no previous substitution");
731 return ERR;
732 } else if (sflags & SGG)
733 sgnum = 0; /* override numeric arg */
734 if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
735 seterrmsg("invalid pattern delimiter");
736 return ERR;
738 tpat = pat;
739 SPL1();
740 if ((!sflags || (sflags & SGR)) &&
741 (tpat = get_compiled_pattern()) == NULL) {
742 SPL0();
743 return ERR;
744 } else if (tpat != pat) {
745 if (pat) {
746 regfree(pat);
747 free(pat);
749 pat = tpat;
750 patlock = 1; /* reserve pattern */
752 SPL0();
753 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
754 return ERR;
755 else if (isglobal)
756 sgflag |= GLB;
757 else
758 sgflag &= ~GLB;
759 if (sflags & SGG)
760 sgflag ^= GSG;
761 if (sflags & SGP)
762 sgflag ^= GPR, sgflag &= ~(GLS | GNP);
763 do {
764 switch(*ibufp) {
765 case 'p':
766 sgflag |= GPR, ibufp++;
767 break;
768 case 'l':
769 sgflag |= GLS, ibufp++;
770 break;
771 case 'n':
772 sgflag |= GNP, ibufp++;
773 break;
774 default:
775 n++;
777 } while (!n);
778 if (check_addr_range(current_addr, current_addr) < 0)
779 return ERR;
780 GET_COMMAND_SUFFIX();
781 if (!isglobal) clear_undo_stack();
782 if (search_and_replace(pat, sgflag, sgnum) < 0)
783 return ERR;
784 break;
785 case 't':
786 if (check_addr_range(current_addr, current_addr) < 0)
787 return ERR;
788 GET_THIRD_ADDR(addr);
789 GET_COMMAND_SUFFIX();
790 if (!isglobal) clear_undo_stack();
791 if (copy_lines(addr) < 0)
792 return ERR;
793 break;
794 case 'u':
795 if (addr_cnt > 0) {
796 seterrmsg("unexpected address");
797 return ERR;
799 GET_COMMAND_SUFFIX();
800 if (pop_undo_stack() < 0)
801 return ERR;
802 break;
803 case 'w':
804 case 'W':
805 if ((n = *ibufp) == 'q' || n == 'Q') {
806 gflag = EOF;
807 ibufp++;
809 if (!isspace((unsigned char)*ibufp)) {
810 seterrmsg("unexpected command suffix");
811 return ERR;
812 } else if ((fnp = get_filename()) == NULL)
813 return ERR;
814 if (addr_cnt == 0 && !addr_last)
815 first_addr = second_addr = 0;
816 else if (check_addr_range(1, addr_last) < 0)
817 return ERR;
818 GET_COMMAND_SUFFIX();
819 if (*old_filename == '\0' && *fnp != '!')
820 strlcpy(old_filename, fnp, sizeof(old_filename) - 2);
821 #ifdef BACKWARDS
822 if (*fnp == '\0' && *old_filename == '\0') {
823 seterrmsg("no current filename");
824 return ERR;
826 #endif
827 if ((addr = write_file(*fnp ? fnp : old_filename,
828 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
829 return ERR;
830 else if (addr == addr_last)
831 modified = 0;
832 else if (modified && !scripted && n == 'q')
833 gflag = EMOD;
834 break;
835 case 'x':
836 if (addr_cnt > 0) {
837 seterrmsg("unexpected address");
838 return ERR;
840 GET_COMMAND_SUFFIX();
841 #ifdef DES
842 des = get_keyword();
843 #else
844 seterrmsg("crypt unavailable");
845 return ERR;
846 #endif
847 break;
848 case 'z':
849 #ifdef BACKWARDS
850 if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
851 #else
852 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
853 #endif
854 return ERR;
855 else if ('0' < *ibufp && *ibufp <= '9')
856 STRTOL(rows, ibufp);
857 GET_COMMAND_SUFFIX();
858 if (display_lines(second_addr, min(addr_last,
859 second_addr + rows), gflag) < 0)
860 return ERR;
861 gflag = 0;
862 break;
863 case '=':
864 GET_COMMAND_SUFFIX();
865 printf("%ld\n", addr_cnt ? second_addr : addr_last);
866 break;
867 case '!':
868 if (addr_cnt > 0) {
869 seterrmsg("unexpected address");
870 return ERR;
872 if ((sflags = get_shell_command()) < 0)
873 return ERR;
874 GET_COMMAND_SUFFIX();
875 if (sflags) printf("%s\n", shcmd + 1);
876 system(shcmd + 1);
877 if (!scripted) printf("!\n");
878 break;
879 case '\n':
880 #ifdef BACKWARDS
881 if (check_addr_range(first_addr = 1, current_addr + 1) < 0
882 #else
883 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
884 #endif
885 || display_lines(second_addr, second_addr, 0) < 0)
886 return ERR;
887 break;
888 default:
889 seterrmsg("unknown command");
890 return ERR;
892 return gflag;
896 /* check_addr_range: return status of address range check */
898 check_addr_range(long n, long m)
900 if (addr_cnt == 0) {
901 first_addr = n;
902 second_addr = m;
904 if (first_addr > second_addr || 1 > first_addr ||
905 second_addr > addr_last) {
906 seterrmsg("invalid address");
907 return ERR;
909 return 0;
913 /* get_matching_node_addr: return the address of the next line matching a
914 pattern in a given direction. wrap around begin/end of editor buffer if
915 necessary */
916 long
917 get_matching_node_addr(pattern_t *pat, int dir)
919 char *s;
920 long n = current_addr;
921 line_t *lp;
923 if (!pat) return ERR;
924 do {
925 if ((n = dir ? INC_MOD(n, addr_last) :
926 DEC_MOD(n, addr_last)) != 0) {
927 lp = get_addressed_line_node(n);
928 if ((s = get_sbuf_line(lp)) == NULL)
929 return ERR;
930 if (isbinary)
931 NUL_TO_NEWLINE(s, lp->len);
932 if (!regexec(pat, s, 0, NULL, 0))
933 return n;
935 } while (n != current_addr);
936 seterrmsg("no match");
937 return ERR;
941 /* get_filename: return pointer to copy of filename in the command buffer */
942 char *
943 get_filename(void)
945 static char *file = NULL;
946 static int filesz = 0;
948 int n;
950 if (*ibufp != '\n') {
951 SKIP_BLANKS();
952 if (*ibufp == '\n') {
953 seterrmsg("invalid filename");
954 return NULL;
955 } else if ((ibufp = get_extended_line(&n, 1)) == NULL)
956 return NULL;
957 else if (*ibufp == '!') {
958 ibufp++;
959 if ((n = get_shell_command()) < 0)
960 return NULL;
961 if (n) printf("%s\n", shcmd + 1);
962 return shcmd;
963 } else if (n - 1 > MAXPATHLEN) {
964 seterrmsg("filename too long");
965 return NULL;
968 #ifndef BACKWARDS
969 else if (*old_filename == '\0') {
970 seterrmsg("no current filename");
971 return NULL;
973 #endif
974 REALLOC(file, filesz, MAXPATHLEN + 1, NULL);
975 for (n = 0; *ibufp != '\n';)
976 file[n++] = *ibufp++;
977 file[n] = '\0';
978 return is_legal_filename(file) ? file : NULL;
982 /* get_shell_command: read a shell command from stdin; return substitution
983 status */
985 get_shell_command(void)
987 static char *buf = NULL;
988 static int n = 0;
990 char *s; /* substitution char pointer */
991 int i = 0;
992 int j = 0;
994 if (red || secure) {
995 seterrmsg("shell access restricted");
996 return ERR;
997 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
998 return ERR;
999 REALLOC(buf, n, j + 1, ERR);
1000 buf[i++] = '!'; /* prefix command w/ bang */
1001 while (*ibufp != '\n')
1002 switch (*ibufp) {
1003 default:
1004 REALLOC(buf, n, i + 2, ERR);
1005 buf[i++] = *ibufp;
1006 if (*ibufp++ == '\\')
1007 buf[i++] = *ibufp++;
1008 break;
1009 case '!':
1010 if (s != ibufp) {
1011 REALLOC(buf, n, i + 1, ERR);
1012 buf[i++] = *ibufp++;
1014 #ifdef BACKWARDS
1015 else if (shcmd == NULL || *(shcmd + 1) == '\0')
1016 #else
1017 else if (shcmd == NULL)
1018 #endif
1020 seterrmsg("no previous command");
1021 return ERR;
1022 } else {
1023 REALLOC(buf, n, i + shcmdi, ERR);
1024 for (s = shcmd + 1; s < shcmd + shcmdi;)
1025 buf[i++] = *s++;
1026 s = ibufp++;
1028 break;
1029 case '%':
1030 if (*old_filename == '\0') {
1031 seterrmsg("no current filename");
1032 return ERR;
1034 j = strlen(s = strip_escapes(old_filename));
1035 REALLOC(buf, n, i + j, ERR);
1036 while (j--)
1037 buf[i++] = *s++;
1038 s = ibufp++;
1039 break;
1041 REALLOC(shcmd, shcmdsz, i + 1, ERR);
1042 memcpy(shcmd, buf, i);
1043 shcmd[shcmdi = i] = '\0';
1044 return *s == '!' || *s == '%';
1048 /* append_lines: insert text from stdin to after line n; stop when either a
1049 single period is read or EOF; return status */
1051 append_lines(long n)
1053 int l;
1054 char *lp = ibuf;
1055 char *eot;
1056 undo_t *up = NULL;
1058 for (current_addr = n;;) {
1059 if (!isglobal) {
1060 if ((l = get_tty_line()) < 0)
1061 return ERR;
1062 else if (l == 0 || ibuf[l - 1] != '\n') {
1063 clearerr(stdin);
1064 return l ? EOF : 0;
1066 lp = ibuf;
1067 } else if (*(lp = ibufp) == '\0')
1068 return 0;
1069 else {
1070 while (*ibufp++ != '\n')
1072 l = ibufp - lp;
1074 if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1075 return 0;
1077 eot = lp + l;
1078 SPL1();
1079 do {
1080 if ((lp = put_sbuf_line(lp)) == NULL) {
1081 SPL0();
1082 return ERR;
1083 } else if (up)
1084 up->t = get_addressed_line_node(current_addr);
1085 else if ((up = push_undo_stack(UADD, current_addr,
1086 current_addr)) == NULL) {
1087 SPL0();
1088 return ERR;
1090 } while (lp != eot);
1091 modified = 1;
1092 SPL0();
1094 /* NOTREACHED */
1098 /* join_lines: replace a range of lines with the joined text of those lines */
1100 join_lines(long from, long to)
1102 static char *buf = NULL;
1103 static int n;
1105 char *s;
1106 int size = 0;
1107 line_t *bp, *ep;
1109 ep = get_addressed_line_node(INC_MOD(to, addr_last));
1110 bp = get_addressed_line_node(from);
1111 for (; bp != ep; bp = bp->q_forw) {
1112 if ((s = get_sbuf_line(bp)) == NULL)
1113 return ERR;
1114 REALLOC(buf, n, size + bp->len, ERR);
1115 memcpy(buf + size, s, bp->len);
1116 size += bp->len;
1118 REALLOC(buf, n, size + 2, ERR);
1119 memcpy(buf + size, "\n", 2);
1120 if (delete_lines(from, to) < 0)
1121 return ERR;
1122 current_addr = from - 1;
1123 SPL1();
1124 if (put_sbuf_line(buf) == NULL ||
1125 push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1126 SPL0();
1127 return ERR;
1129 modified = 1;
1130 SPL0();
1131 return 0;
1135 /* move_lines: move a range of lines */
1137 move_lines(long addr)
1139 line_t *b1, *a1, *b2, *a2;
1140 long n = INC_MOD(second_addr, addr_last);
1141 long p = first_addr - 1;
1142 int done = (addr == first_addr - 1 || addr == second_addr);
1144 SPL1();
1145 if (done) {
1146 a2 = get_addressed_line_node(n);
1147 b2 = get_addressed_line_node(p);
1148 current_addr = second_addr;
1149 } else if (push_undo_stack(UMOV, p, n) == NULL ||
1150 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) {
1151 SPL0();
1152 return ERR;
1153 } else {
1154 a1 = get_addressed_line_node(n);
1155 if (addr < first_addr) {
1156 b1 = get_addressed_line_node(p);
1157 b2 = get_addressed_line_node(addr);
1158 /* this get_addressed_line_node last! */
1159 } else {
1160 b2 = get_addressed_line_node(addr);
1161 b1 = get_addressed_line_node(p);
1162 /* this get_addressed_line_node last! */
1164 a2 = b2->q_forw;
1165 REQUE(b2, b1->q_forw);
1166 REQUE(a1->q_back, a2);
1167 REQUE(b1, a1);
1168 current_addr = addr + ((addr < first_addr) ?
1169 second_addr - first_addr + 1 : 0);
1171 if (isglobal)
1172 unset_active_nodes(b2->q_forw, a2);
1173 modified = 1;
1174 SPL0();
1175 return 0;
1179 /* copy_lines: copy a range of lines; return status */
1181 copy_lines(long addr)
1183 line_t *lp, *np = get_addressed_line_node(first_addr);
1184 undo_t *up = NULL;
1185 long n = second_addr - first_addr + 1;
1186 long m = 0;
1188 current_addr = addr;
1189 if (first_addr <= addr && addr < second_addr) {
1190 n = addr - first_addr + 1;
1191 m = second_addr - addr;
1193 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1194 for (; n-- > 0; np = np->q_forw) {
1195 SPL1();
1196 if ((lp = dup_line_node(np)) == NULL) {
1197 SPL0();
1198 return ERR;
1200 add_line_node(lp);
1201 if (up)
1202 up->t = lp;
1203 else if ((up = push_undo_stack(UADD, current_addr,
1204 current_addr)) == NULL) {
1205 SPL0();
1206 return ERR;
1208 modified = 1;
1209 SPL0();
1211 return 0;
1215 /* delete_lines: delete a range of lines */
1217 delete_lines(long from, long to)
1219 line_t *n, *p;
1221 SPL1();
1222 if (push_undo_stack(UDEL, from, to) == NULL) {
1223 SPL0();
1224 return ERR;
1226 n = get_addressed_line_node(INC_MOD(to, addr_last));
1227 p = get_addressed_line_node(from - 1);
1228 /* this get_addressed_line_node last! */
1229 if (isglobal)
1230 unset_active_nodes(p->q_forw, n);
1231 REQUE(p, n);
1232 addr_last -= to - from + 1;
1233 current_addr = from - 1;
1234 modified = 1;
1235 SPL0();
1236 return 0;
1240 /* display_lines: print a range of lines to stdout */
1242 display_lines(long from, long to, int gflag)
1244 line_t *bp;
1245 line_t *ep;
1246 char *s;
1248 if (!from) {
1249 seterrmsg("invalid address");
1250 return ERR;
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)
1256 return ERR;
1257 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0)
1258 return ERR;
1260 return 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(line_t *lp, int n)
1273 if (!islower(n)) {
1274 seterrmsg("invalid mark character");
1275 return ERR;
1276 } else if (mark[n - 'a'] == NULL)
1277 markno++;
1278 mark[n - 'a'] = lp;
1279 return 0;
1283 /* get_marked_node_addr: return address of a marked line */
1284 long
1285 get_marked_node_addr(int n)
1287 if (!islower(n)) {
1288 seterrmsg("invalid mark character");
1289 return ERR;
1291 return get_line_node_addr(mark[n - 'a']);
1295 /* unmark_line_node: clear line node mark */
1296 void
1297 unmark_line_node(line_t *lp)
1299 int i;
1301 for (i = 0; markno && i < MAXMARK; i++)
1302 if (mark[i] == lp) {
1303 mark[i] = NULL;
1304 markno--;
1309 /* dup_line_node: return a pointer to a copy of a line node */
1310 line_t *
1311 dup_line_node(line_t *lp)
1313 line_t *np;
1315 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1316 fprintf(stderr, "%s\n", strerror(errno));
1317 seterrmsg("out of memory");
1318 return NULL;
1320 np->seek = lp->seek;
1321 np->len = lp->len;
1322 return np;
1326 /* has_trailing_escape: return the parity of escapes preceding a character
1327 in a string */
1329 has_trailing_escape(char *s, char *t)
1331 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1335 /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1336 char *
1337 strip_escapes(const char *s)
1339 static char *file = NULL;
1340 static int filesz = 0;
1342 int i = 0;
1344 REALLOC(file, filesz, MAXPATHLEN + 1, NULL);
1345 while ((i < (filesz - 1)) &&
1346 (file[i++] = (*s == '\\') != '\0' ? *++s : *s))
1347 s++;
1348 file[filesz - 1] = '\0';
1349 return file;
1353 void
1354 signal_hup(int signo)
1356 if (mutex)
1357 sigflags |= (1 << (signo - 1));
1358 else handle_hup(signo);
1362 void
1363 signal_int(int signo)
1365 if (mutex)
1366 sigflags |= (1 << (signo - 1));
1367 else handle_int(signo);
1371 void
1372 handle_hup(int signo)
1374 char *hup = NULL; /* hup filename */
1375 char *s;
1376 int n;
1378 if (!sigactive)
1379 quit(1);
1380 sigflags &= ~(1 << (signo - 1));
1381 if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
1382 (s = getenv("HOME")) != NULL &&
1383 (n = strlen(s)) + 8 <= MAXPATHLEN && /* "ed.hup" + '/' */
1384 (hup = (char *) malloc(n + 10)) != NULL) {
1385 strcpy(hup, s);
1386 if (hup[n - 1] != '/')
1387 hup[n] = '/', hup[n+1] = '\0';
1388 strcat(hup, "ed.hup");
1389 write_file(hup, "w", 1, addr_last);
1391 quit(2);
1395 void
1396 handle_int(int signo)
1398 if (!sigactive)
1399 quit(1);
1400 sigflags &= ~(1 << (signo - 1));
1401 #ifdef _POSIX_SOURCE
1402 siglongjmp(env, -1);
1403 #else
1404 longjmp(env, -1);
1405 #endif
1409 int cols = 72; /* wrap column */
1411 void
1412 handle_winch(int signo)
1414 struct winsize ws; /* window size structure */
1416 sigflags &= ~(1 << (signo - 1));
1417 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1418 if (ws.ws_row > 2) rows = ws.ws_row - 2;
1419 if (ws.ws_col > 8) cols = ws.ws_col - 8;
1424 /* is_legal_filename: return a legal filename */
1426 is_legal_filename(char *s)
1428 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1429 seterrmsg("shell access restricted");
1430 return 0;
1432 return 1;