xref: /netbsd/bin/ed/main.c (revision bf9ec67e)
1 /*	$NetBSD: main.c,v 1.13 2002/01/23 19:07:33 atatat Exp $	*/
2 
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.
8  *
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.
17  *
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.
29  */
30 
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __COPYRIGHT(
34 "@(#) Copyright (c) 1993 Andrew Moore, Talke Studio. \n\
35  All rights reserved.\n");
36 #endif /* not lint */
37 
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.13 2002/01/23 19:07:33 atatat Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  * CREDITS
48  *
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.
52  *
53  *	The buffering algorithm is attributed to Rodney Ruddock of
54  *	the University of Guelph, Guelph, Ontario.
55  *
56  *	The cbc.c encryption code is adapted from
57  *	the bdes program by Matt Bishop of Dartmouth College,
58  *	Hanover, NH.
59  *
60  */
61 
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>
68 
69 #include "ed.h"
70 
71 
72 #ifdef _POSIX_SOURCE
73 sigjmp_buf env;
74 #else
75 jmp_buf env;
76 #endif
77 
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 */
86 
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 sigflags = 0;		/* if set, signals received while mutex set */
98 int sigactive = 0;		/* if set, signal handlers are enabled */
99 
100 char old_filename[MAXPATHLEN + 1] = "";	/* default filename */
101 long current_addr;		/* current address in editor buffer */
102 long addr_last;			/* last address in editor buffer */
103 int lineno;			/* script line number */
104 char *prompt;			/* command-line prompt */
105 char *dps = "*";		/* default command-line prompt */
106 
107 char *usage = "usage: %s [-] [-sxE] [-p string] [name]\n";
108 
109 int main __P((int, char *[]));
110 
111 /* ed: line editor */
112 int
113 main(argc, argv)
114 	int argc;
115 	char *argv[];
116 {
117 	int c, n;
118 	long status = 0;
119 #ifdef __GNUC__
120 	(void) &argc;
121 	(void) &argv;
122 #endif
123 
124 	red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r';
125 top:
126 	while ((c = getopt(argc, argv, "p:sxE")) != -1)
127 		switch(c) {
128 		case 'p':				/* set prompt */
129 			prompt = optarg;
130 			break;
131 		case 's':				/* run script */
132 			scripted = 1;
133 			break;
134 		case 'x':				/* use crypt */
135 #ifdef DES
136 			des = get_keyword();
137 #else
138 			fprintf(stderr, "crypt unavailable\n?\n");
139 #endif
140 			break;
141 
142 		case 'E':
143 			ere = REG_EXTENDED;
144 			break;
145 		default:
146 			fprintf(stderr, usage, argv[0]);
147 			exit(1);
148 			/* NOTREACHED */
149 		}
150 	argv += optind;
151 	argc -= optind;
152 	if (argc && **argv == '-') {
153 		scripted = 1;
154 		if (argc > 1) {
155 			optind = 1;
156 			goto top;
157 		}
158 		argv++;
159 		argc--;
160 	}
161 	/* assert: reliable signals! */
162 #ifdef SIGWINCH
163 	handle_winch(SIGWINCH);
164 	if (isatty(0)) signal(SIGWINCH, handle_winch);
165 #endif
166 	signal(SIGHUP, signal_hup);
167 	signal(SIGQUIT, SIG_IGN);
168 	signal(SIGINT, signal_int);
169 #ifdef _POSIX_SOURCE
170 	if ((status = sigsetjmp(env, 1)) != 0)
171 #else
172 	if ((status = setjmp(env)) != 0)
173 #endif
174 	{
175 		fputs("\n?\n", stderr);
176 		sprintf(errmsg, "interrupt");
177 	} else {
178 		init_buffers();
179 		sigactive = 1;			/* enable signal handlers */
180 		if (argc && **argv && is_legal_filename(*argv)) {
181 			if (read_file(*argv, 0) < 0 && !isatty(0))
182 				quit(2);
183 			else if (**argv != '!')
184 				strcpy(old_filename, *argv);
185 		} else if (argc) {
186 			fputs("?\n", stderr);
187 			if (**argv == '\0')
188 				sprintf(errmsg, "invalid filename");
189 			if (!isatty(0))
190 				quit(2);
191 		}
192 	}
193 	for (;;) {
194 		if (status < 0 && garrulous)
195 			fprintf(stderr, "%s\n", errmsg);
196 		if (prompt) {
197 			printf("%s", prompt);
198 			fflush(stdout);
199 		}
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 				sprintf(errmsg, "warning: file modified");
207 				if (!isatty(0)) {
208 					fprintf(stderr, garrulous ?
209 					    "script, line %d: %s\n" :
210 					    "", lineno, errmsg);
211 					quit(2);
212 				}
213 				clearerr(stdin);
214 				modified = 0;
215 				status = EMOD;
216 				continue;
217 			} else
218 				quit(0);
219 		} else if (ibuf[n - 1] != '\n') {
220 			/* discard line */
221 			sprintf(errmsg, "unexpected end-of-file");
222 			clearerr(stdin);
223 			status = ERR;
224 			continue;
225 		}
226 		isglobal = 0;
227 		if ((status = extract_addr_range()) >= 0 &&
228 		    (status = exec_command()) >= 0)
229 			if (!status || (status &&
230 			    (status = display_lines(current_addr, current_addr,
231 			        status))) >= 0)
232 				continue;
233 		switch (status) {
234 		case EOF:
235 			quit(0);
236 		case EMOD:
237 			modified = 0;
238 			fputs("?\n", stderr);		/* give warning */
239 			sprintf(errmsg, "warning: file modified");
240 			if (!isatty(0)) {
241 				fprintf(stderr, garrulous ?
242 				    "script, line %d: %s\n" :
243 				    "", lineno, errmsg);
244 				quit(2);
245 			}
246 			break;
247 		case FATAL:
248 			if (!isatty(0))
249 				fprintf(stderr, garrulous ?
250 				    "script, line %d: %s\n" : "",
251 				    lineno, errmsg);
252 			else
253 				fprintf(stderr, garrulous ? "%s\n" : "",
254 				    errmsg);
255 			quit(3);
256 		default:
257 			fputs("?\n", stderr);
258 			if (!isatty(0)) {
259 				fprintf(stderr, garrulous ?
260 				    "script, line %d: %s\n" : "",
261 				    lineno, errmsg);
262 				quit(2);
263 			}
264 			break;
265 		}
266 	}
267 	/* NOTREACHED */
268 }
269 
270 long first_addr, second_addr, addr_cnt;
271 
272 /* extract_addr_range: get line addresses from the command buffer until an
273    illegal address is seen; return status */
274 int
275 extract_addr_range()
276 {
277 	long addr;
278 
279 	addr_cnt = 0;
280 	first_addr = second_addr = current_addr;
281 	while ((addr = next_addr()) >= 0) {
282 		addr_cnt++;
283 		first_addr = second_addr;
284 		second_addr = addr;
285 		if (*ibufp != ',' && *ibufp != ';')
286 			break;
287 		else if (*ibufp++ == ';')
288 			current_addr = addr;
289 	}
290 	if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
291 		first_addr = second_addr;
292 	return (addr == ERR) ? ERR : 0;
293 }
294 
295 
296 #define	SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') \
297 	ibufp++
298 
299 #define MUST_BE_FIRST() \
300 	if (!first) { sprintf(errmsg, "invalid address"); return ERR; }
301 
302 /*  next_addr: return the next line address in the command buffer */
303 long
304 next_addr()
305 {
306 	char *hd;
307 	long addr = current_addr;
308 	long n;
309 	int first = 1;
310 	int c;
311 
312 	SKIP_BLANKS();
313 	for (hd = ibufp;; first = 0)
314 		switch (c = *ibufp) {
315 		case '+':
316 		case '\t':
317 		case ' ':
318 		case '-':
319 		case '^':
320 			ibufp++;
321 			SKIP_BLANKS();
322 			if (isdigit((unsigned char)*ibufp)) {
323 				STRTOL(n, ibufp);
324 				addr += (c == '-' || c == '^') ? -n : n;
325 			} else if (!isspace(c))
326 				addr += (c == '-' || c == '^') ? -1 : 1;
327 			break;
328 		case '0': case '1': case '2':
329 		case '3': case '4': case '5':
330 		case '6': case '7': case '8': case '9':
331 			MUST_BE_FIRST();
332 			STRTOL(addr, ibufp);
333 			break;
334 		case '.':
335 		case '$':
336 			MUST_BE_FIRST();
337 			ibufp++;
338 			addr = (c == '.') ? current_addr : addr_last;
339 			break;
340 		case '/':
341 		case '?':
342 			MUST_BE_FIRST();
343 			if ((addr = get_matching_node_addr(
344 			    get_compiled_pattern(), c == '/')) < 0)
345 				return ERR;
346 			else if (c == *ibufp)
347 				ibufp++;
348 			break;
349 		case '\'':
350 			MUST_BE_FIRST();
351 			ibufp++;
352 			if ((addr = get_marked_node_addr(*ibufp++)) < 0)
353 				return ERR;
354 			break;
355 		case '%':
356 		case ',':
357 		case ';':
358 			if (first) {
359 				ibufp++;
360 				addr_cnt++;
361 				second_addr = (c == ';') ? current_addr : 1;
362 				addr = addr_last;
363 				break;
364 			}
365 			/* FALL THROUGH */
366 		default:
367 			if (ibufp == hd)
368 				return EOF;
369 			else if (addr < 0 || addr_last < addr) {
370 				sprintf(errmsg, "invalid address");
371 				return ERR;
372 			} else
373 				return addr;
374 		}
375 	/* NOTREACHED */
376 }
377 
378 
379 #ifdef BACKWARDS
380 /* GET_THIRD_ADDR: get a legal address from the command buffer */
381 #define GET_THIRD_ADDR(addr) \
382 { \
383 	long ol1, ol2; \
384 \
385 	ol1 = first_addr, ol2 = second_addr; \
386 	if (extract_addr_range() < 0) \
387 		return ERR; \
388 	else if (addr_cnt == 0) { \
389 		sprintf(errmsg, "destination expected"); \
390 		return ERR; \
391 	} else if (second_addr < 0 || addr_last < second_addr) { \
392 		sprintf(errmsg, "invalid address"); \
393 		return ERR; \
394 	} \
395 	addr = second_addr; \
396 	first_addr = ol1, second_addr = ol2; \
397 }
398 #else	/* BACKWARDS */
399 /* GET_THIRD_ADDR: get a legal address from the command buffer */
400 #define GET_THIRD_ADDR(addr) \
401 { \
402 	long ol1, ol2; \
403 \
404 	ol1 = first_addr, ol2 = second_addr; \
405 	if (extract_addr_range() < 0) \
406 		return ERR; \
407 	if (second_addr < 0 || addr_last < second_addr) { \
408 		sprintf(errmsg, "invalid address"); \
409 		return ERR; \
410 	} \
411 	addr = second_addr; \
412 	first_addr = ol1, second_addr = ol2; \
413 }
414 #endif
415 
416 
417 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */
418 #define GET_COMMAND_SUFFIX() { \
419 	int done = 0; \
420 	do { \
421 		switch(*ibufp) { \
422 		case 'p': \
423 			gflag |= GPR, ibufp++; \
424 			break; \
425 		case 'l': \
426 			gflag |= GLS, ibufp++; \
427 			break; \
428 		case 'n': \
429 			gflag |= GNP, ibufp++; \
430 			break; \
431 		default: \
432 			done++; \
433 		} \
434 	} while (!done); \
435 	if (*ibufp++ != '\n') { \
436 		sprintf(errmsg, "invalid command suffix"); \
437 		return ERR; \
438 	} \
439 }
440 
441 
442 /* sflags */
443 #define SGG 001		/* complement previous global substitute suffix */
444 #define SGP 002		/* complement previous print suffix */
445 #define SGR 004		/* use last regex instead of last pat */
446 #define SGF 010		/* repeat last substitution */
447 
448 int patlock = 0;	/* if set, pattern not freed by get_compiled_pattern() */
449 
450 long rows = 22;		/* scroll length: ws_row - 2 */
451 
452 /* exec_command: execute the next command in command buffer; return print
453    request, if any */
454 int
455 exec_command()
456 {
457 	static pattern_t *pat = NULL;
458 	static int sgflag = 0;
459 	static long sgnum = 0;
460 
461 	pattern_t *tpat;
462 	char *fnp;
463 	int gflag = 0;
464 	int sflags = 0;
465 	long addr = 0;
466 	int n = 0;
467 	int c;
468 
469 	SKIP_BLANKS();
470 	switch(c = *ibufp++) {
471 	case 'a':
472 		GET_COMMAND_SUFFIX();
473 		if (!isglobal) clear_undo_stack();
474 		if (append_lines(second_addr) < 0)
475 			return ERR;
476 		break;
477 	case 'c':
478 		if (check_addr_range(current_addr, current_addr) < 0)
479 			return ERR;
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)
484 			return ERR;
485 		break;
486 	case 'd':
487 		if (check_addr_range(current_addr, current_addr) < 0)
488 			return ERR;
489 		GET_COMMAND_SUFFIX();
490 		if (!isglobal) clear_undo_stack();
491 		if (delete_lines(first_addr, second_addr) < 0)
492 			return ERR;
493 		else if ((addr = INC_MOD(current_addr, addr_last)) != 0)
494 			current_addr = addr;
495 		break;
496 	case 'e':
497 		if (modified && !scripted)
498 			return EMOD;
499 		/* fall through */
500 	case 'E':
501 		if (addr_cnt > 0) {
502 			sprintf(errmsg, "unexpected address");
503 			return ERR;
504 		} else if (!isspace((unsigned char)*ibufp)) {
505 			sprintf(errmsg, "unexpected command suffix");
506 			return ERR;
507 		} else if ((fnp = get_filename()) == NULL)
508 			return ERR;
509 		GET_COMMAND_SUFFIX();
510 		if (delete_lines(1, addr_last) < 0)
511 			return ERR;
512 		clear_undo_stack();
513 		if (close_sbuf() < 0)
514 			return ERR;
515 		else if (open_sbuf() < 0)
516 			return FATAL;
517 		if (*fnp && *fnp != '!') strcpy(old_filename, fnp);
518 #ifdef BACKWARDS
519 		if (*fnp == '\0' && *old_filename == '\0') {
520 			sprintf(errmsg, "no current filename");
521 			return ERR;
522 		}
523 #endif
524 		if (read_file(*fnp ? fnp : old_filename, 0) < 0)
525 			return ERR;
526 		clear_undo_stack();
527 		modified = 0;
528 		u_current_addr = u_addr_last = -1;
529 		break;
530 	case 'f':
531 		if (addr_cnt > 0) {
532 			sprintf(errmsg, "unexpected address");
533 			return ERR;
534 		} else if (!isspace((unsigned char)*ibufp)) {
535 			sprintf(errmsg, "unexpected command suffix");
536 			return ERR;
537 		} else if ((fnp = get_filename()) == NULL)
538 			return ERR;
539 		else if (*fnp == '!') {
540 			sprintf(errmsg, "invalid redirection");
541 			return ERR;
542 		}
543 		GET_COMMAND_SUFFIX();
544 		if (*fnp) strcpy(old_filename, fnp);
545 		printf("%s\n", strip_escapes(old_filename));
546 		break;
547 	case 'g':
548 	case 'v':
549 	case 'G':
550 	case 'V':
551 		if (isglobal) {
552 			sprintf(errmsg, "cannot nest global commands");
553 			return ERR;
554 		} else if (check_addr_range(1, addr_last) < 0)
555 			return ERR;
556 		else if (build_active_list(c == 'g' || c == 'G') < 0)
557 			return ERR;
558 		else if ((n = (c == 'G' || c == 'V')) != 0)
559 			GET_COMMAND_SUFFIX();
560 		isglobal++;
561 		if (exec_global(n, gflag) < 0)
562 			return ERR;
563 		break;
564 	case 'h':
565 		if (addr_cnt > 0) {
566 			sprintf(errmsg, "unexpected address");
567 			return ERR;
568 		}
569 		GET_COMMAND_SUFFIX();
570 		if (*errmsg) fprintf(stderr, "%s\n", errmsg);
571 		break;
572 	case 'H':
573 		if (addr_cnt > 0) {
574 			sprintf(errmsg, "unexpected address");
575 			return ERR;
576 		}
577 		GET_COMMAND_SUFFIX();
578 		if ((garrulous = 1 - garrulous) && *errmsg)
579 			fprintf(stderr, "%s\n", errmsg);
580 		break;
581 	case 'i':
582 		if (second_addr == 0) {
583 			sprintf(errmsg, "invalid address");
584 			return ERR;
585 		}
586 		GET_COMMAND_SUFFIX();
587 		if (!isglobal) clear_undo_stack();
588 		if (append_lines(second_addr - 1) < 0)
589 			return ERR;
590 		break;
591 	case 'j':
592 		if (check_addr_range(current_addr, current_addr + 1) < 0)
593 			return ERR;
594 		GET_COMMAND_SUFFIX();
595 		if (!isglobal) clear_undo_stack();
596 		if (first_addr != second_addr &&
597 		    join_lines(first_addr, second_addr) < 0)
598 			return ERR;
599 		break;
600 	case 'k':
601 		c = *ibufp++;
602 		if (second_addr == 0) {
603 			sprintf(errmsg, "invalid address");
604 			return ERR;
605 		}
606 		GET_COMMAND_SUFFIX();
607 		if (mark_line_node(get_addressed_line_node(second_addr), c) < 0)
608 			return ERR;
609 		break;
610 	case 'l':
611 		if (check_addr_range(current_addr, current_addr) < 0)
612 			return ERR;
613 		GET_COMMAND_SUFFIX();
614 		if (display_lines(first_addr, second_addr, gflag | GLS) < 0)
615 			return ERR;
616 		gflag = 0;
617 		break;
618 	case 'm':
619 		if (check_addr_range(current_addr, current_addr) < 0)
620 			return ERR;
621 		GET_THIRD_ADDR(addr);
622 		if (first_addr <= addr && addr < second_addr) {
623 			sprintf(errmsg, "invalid destination");
624 			return ERR;
625 		}
626 		GET_COMMAND_SUFFIX();
627 		if (!isglobal) clear_undo_stack();
628 		if (move_lines(addr) < 0)
629 			return ERR;
630 		break;
631 	case 'n':
632 		if (check_addr_range(current_addr, current_addr) < 0)
633 			return ERR;
634 		GET_COMMAND_SUFFIX();
635 		if (display_lines(first_addr, second_addr, gflag | GNP) < 0)
636 			return ERR;
637 		gflag = 0;
638 		break;
639 	case 'p':
640 		if (check_addr_range(current_addr, current_addr) < 0)
641 			return ERR;
642 		GET_COMMAND_SUFFIX();
643 		if (display_lines(first_addr, second_addr, gflag | GPR) < 0)
644 			return ERR;
645 		gflag = 0;
646 		break;
647 	case 'P':
648 		if (addr_cnt > 0) {
649 			sprintf(errmsg, "unexpected address");
650 			return ERR;
651 		}
652 		GET_COMMAND_SUFFIX();
653 		prompt = prompt ? NULL : optarg ? optarg : dps;
654 		break;
655 	case 'q':
656 	case 'Q':
657 		if (addr_cnt > 0) {
658 			sprintf(errmsg, "unexpected address");
659 			return ERR;
660 		}
661 		GET_COMMAND_SUFFIX();
662 		gflag =  (modified && !scripted && c == 'q') ? EMOD : EOF;
663 		break;
664 	case 'r':
665 		if (!isspace((unsigned char)*ibufp)) {
666 			sprintf(errmsg, "unexpected command suffix");
667 			return ERR;
668 		} else if (addr_cnt == 0)
669 			second_addr = addr_last;
670 		if ((fnp = get_filename()) == NULL)
671 			return ERR;
672 		GET_COMMAND_SUFFIX();
673 		if (!isglobal) clear_undo_stack();
674 		if (*old_filename == '\0' && *fnp != '!')
675 			strcpy(old_filename, fnp);
676 #ifdef BACKWARDS
677 		if (*fnp == '\0' && *old_filename == '\0') {
678 			sprintf(errmsg, "no current filename");
679 			return ERR;
680 		}
681 #endif
682 		if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0)
683 			return ERR;
684 		else if (addr && addr != addr_last)
685 			modified = 1;
686 		break;
687 	case 's':
688 		do {
689 			switch(*ibufp) {
690 			case '\n':
691 				sflags |=SGF;
692 				break;
693 			case 'g':
694 				sflags |= SGG;
695 				ibufp++;
696 				break;
697 			case 'p':
698 				sflags |= SGP;
699 				ibufp++;
700 				break;
701 			case 'r':
702 				sflags |= SGR;
703 				ibufp++;
704 				break;
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);
708 				sflags |= SGF;
709 				sgflag &= ~GSG;		/* override GSG */
710 				break;
711 			default:
712 				if (sflags) {
713 					sprintf(errmsg, "invalid command suffix");
714 					return ERR;
715 				}
716 			}
717 		} while (sflags && *ibufp != '\n');
718 		if (sflags && !pat) {
719 			sprintf(errmsg, "no previous substitution");
720 			return ERR;
721 		} else if (sflags & SGG)
722 			sgnum = 0;		/* override numeric arg */
723 		if (*ibufp != '\n' && *(ibufp + 1) == '\n') {
724 			sprintf(errmsg, "invalid pattern delimiter");
725 			return ERR;
726 		}
727 		tpat = pat;
728 		SPL1();
729 		if ((!sflags || (sflags & SGR)) &&
730 		    (tpat = get_compiled_pattern()) == NULL) {
731 		 	SPL0();
732 			return ERR;
733 		} else if (tpat != pat) {
734 			if (pat) {
735 				regfree(pat);
736 				free(pat);
737 			}
738 			pat = tpat;
739 			patlock = 1;		/* reserve pattern */
740 		}
741 		SPL0();
742 		if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0)
743 			return ERR;
744 		else if (isglobal)
745 			sgflag |= GLB;
746 		else
747 			sgflag &= ~GLB;
748 		if (sflags & SGG)
749 			sgflag ^= GSG;
750 		if (sflags & SGP)
751 			sgflag ^= GPR, sgflag &= ~(GLS | GNP);
752 		do {
753 			switch(*ibufp) {
754 			case 'p':
755 				sgflag |= GPR, ibufp++;
756 				break;
757 			case 'l':
758 				sgflag |= GLS, ibufp++;
759 				break;
760 			case 'n':
761 				sgflag |= GNP, ibufp++;
762 				break;
763 			default:
764 				n++;
765 			}
766 		} while (!n);
767 		if (check_addr_range(current_addr, current_addr) < 0)
768 			return ERR;
769 		GET_COMMAND_SUFFIX();
770 		if (!isglobal) clear_undo_stack();
771 		if (search_and_replace(pat, sgflag, sgnum) < 0)
772 			return ERR;
773 		break;
774 	case 't':
775 		if (check_addr_range(current_addr, current_addr) < 0)
776 			return ERR;
777 		GET_THIRD_ADDR(addr);
778 		GET_COMMAND_SUFFIX();
779 		if (!isglobal) clear_undo_stack();
780 		if (copy_lines(addr) < 0)
781 			return ERR;
782 		break;
783 	case 'u':
784 		if (addr_cnt > 0) {
785 			sprintf(errmsg, "unexpected address");
786 			return ERR;
787 		}
788 		GET_COMMAND_SUFFIX();
789 		if (pop_undo_stack() < 0)
790 			return ERR;
791 		break;
792 	case 'w':
793 	case 'W':
794 		if ((n = *ibufp) == 'q' || n == 'Q') {
795 			gflag = EOF;
796 			ibufp++;
797 		}
798 		if (!isspace((unsigned char)*ibufp)) {
799 			sprintf(errmsg, "unexpected command suffix");
800 			return ERR;
801 		} else if ((fnp = get_filename()) == NULL)
802 			return ERR;
803 		if (addr_cnt == 0 && !addr_last)
804 			first_addr = second_addr = 0;
805 		else if (check_addr_range(1, addr_last) < 0)
806 			return ERR;
807 		GET_COMMAND_SUFFIX();
808 		if (*old_filename == '\0' && *fnp != '!')
809 			strcpy(old_filename, fnp);
810 #ifdef BACKWARDS
811 		if (*fnp == '\0' && *old_filename == '\0') {
812 			sprintf(errmsg, "no current filename");
813 			return ERR;
814 		}
815 #endif
816 		if ((addr = write_file(*fnp ? fnp : old_filename,
817 		    (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0)
818 			return ERR;
819 		else if (addr == addr_last)
820 			modified = 0;
821 		else if (modified && !scripted && n == 'q')
822 			gflag = EMOD;
823 		break;
824 	case 'x':
825 		if (addr_cnt > 0) {
826 			sprintf(errmsg, "unexpected address");
827 			return ERR;
828 		}
829 		GET_COMMAND_SUFFIX();
830 #ifdef DES
831 		des = get_keyword();
832 #else
833 		sprintf(errmsg, "crypt unavailable");
834 		return ERR;
835 #endif
836 		break;
837 	case 'z':
838 #ifdef BACKWARDS
839 		if (check_addr_range(first_addr = 1, current_addr + 1) < 0)
840 #else
841 		if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0)
842 #endif
843 			return ERR;
844 		else if ('0' < *ibufp && *ibufp <= '9')
845 			STRTOL(rows, ibufp);
846 		GET_COMMAND_SUFFIX();
847 		if (display_lines(second_addr, min(addr_last,
848 		    second_addr + rows), gflag) < 0)
849 			return ERR;
850 		gflag = 0;
851 		break;
852 	case '=':
853 		GET_COMMAND_SUFFIX();
854 		printf("%ld\n", addr_cnt ? second_addr : addr_last);
855 		break;
856 	case '!':
857 		if (addr_cnt > 0) {
858 			sprintf(errmsg, "unexpected address");
859 			return ERR;
860 		} else if ((sflags = get_shell_command()) < 0)
861 			return ERR;
862 		GET_COMMAND_SUFFIX();
863 		if (sflags) printf("%s\n", shcmd + 1);
864 		system(shcmd + 1);
865 		if (!scripted) printf("!\n");
866 		break;
867 	case '\n':
868 #ifdef BACKWARDS
869 		if (check_addr_range(first_addr = 1, current_addr + 1) < 0
870 #else
871 		if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0
872 #endif
873 		 || display_lines(second_addr, second_addr, 0) < 0)
874 			return ERR;
875 		break;
876 	default:
877 		sprintf(errmsg, "unknown command");
878 		return ERR;
879 	}
880 	return gflag;
881 }
882 
883 
884 /* check_addr_range: return status of address range check */
885 int
886 check_addr_range(n, m)
887 	long n, m;
888 {
889 	if (addr_cnt == 0) {
890 		first_addr = n;
891 		second_addr = m;
892 	}
893 	if (first_addr > second_addr || 1 > first_addr ||
894 	    second_addr > addr_last) {
895 		sprintf(errmsg, "invalid address");
896 		return ERR;
897 	}
898 	return 0;
899 }
900 
901 
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
904    necessary */
905 long
906 get_matching_node_addr(pat, dir)
907 	pattern_t *pat;
908 	int dir;
909 {
910 	char *s;
911 	long n = current_addr;
912 	line_t *lp;
913 
914 	if (!pat) return ERR;
915 	do {
916 		if ((n = dir ? INC_MOD(n, addr_last) :
917 		    DEC_MOD(n, addr_last)) != 0) {
918 			lp = get_addressed_line_node(n);
919 			if ((s = get_sbuf_line(lp)) == NULL)
920 				return ERR;
921 			if (isbinary)
922 				NUL_TO_NEWLINE(s, lp->len);
923 			if (!regexec(pat, s, 0, NULL, 0))
924 				return n;
925 		}
926 	} while (n != current_addr);
927 	sprintf(errmsg, "no match");
928 	return  ERR;
929 }
930 
931 
932 /* get_filename: return pointer to copy of filename in the command buffer */
933 char *
934 get_filename()
935 {
936 	static char *file = NULL;
937 	static int filesz = 0;
938 
939 	int n;
940 
941 	if (*ibufp != '\n') {
942 		SKIP_BLANKS();
943 		if (*ibufp == '\n') {
944 			sprintf(errmsg, "invalid filename");
945 			return NULL;
946 		} else if ((ibufp = get_extended_line(&n, 1)) == NULL)
947 			return NULL;
948 		else if (*ibufp == '!') {
949 			ibufp++;
950 			if ((n = get_shell_command()) < 0)
951 				return NULL;
952 			if (n) printf("%s\n", shcmd + 1);
953 			return shcmd;
954 		} else if (n - 1 > MAXPATHLEN) {
955 			sprintf(errmsg, "filename too long");
956 			return  NULL;
957 		}
958 	}
959 #ifndef BACKWARDS
960 	else if (*old_filename == '\0') {
961 		sprintf(errmsg, "no current filename");
962 		return  NULL;
963 	}
964 #endif
965 	REALLOC(file, filesz, MAXPATHLEN + 1, NULL);
966 	for (n = 0; *ibufp != '\n';)
967 		file[n++] = *ibufp++;
968 	file[n] = '\0';
969 	return is_legal_filename(file) ? file : NULL;
970 }
971 
972 
973 /* get_shell_command: read a shell command from stdin; return substitution
974    status */
975 int
976 get_shell_command()
977 {
978 	static char *buf = NULL;
979 	static int n = 0;
980 
981 	char *s;			/* substitution char pointer */
982 	int i = 0;
983 	int j = 0;
984 
985 	if (red) {
986 		sprintf(errmsg, "shell access restricted");
987 		return ERR;
988 	} else if ((s = ibufp = get_extended_line(&j, 1)) == NULL)
989 		return ERR;
990 	REALLOC(buf, n, j + 1, ERR);
991 	buf[i++] = '!';			/* prefix command w/ bang */
992 	while (*ibufp != '\n')
993 		switch (*ibufp) {
994 		default:
995 			REALLOC(buf, n, i + 2, ERR);
996 			buf[i++] = *ibufp;
997 			if (*ibufp++ == '\\')
998 				buf[i++] = *ibufp++;
999 			break;
1000 		case '!':
1001 			if (s != ibufp) {
1002 				REALLOC(buf, n, i + 1, ERR);
1003 				buf[i++] = *ibufp++;
1004 			}
1005 #ifdef BACKWARDS
1006 			else if (shcmd == NULL || *(shcmd + 1) == '\0')
1007 #else
1008 			else if (shcmd == NULL)
1009 #endif
1010 			{
1011 				sprintf(errmsg, "no previous command");
1012 				return ERR;
1013 			} else {
1014 				REALLOC(buf, n, i + shcmdi, ERR);
1015 				for (s = shcmd + 1; s < shcmd + shcmdi;)
1016 					buf[i++] = *s++;
1017 				s = ibufp++;
1018 			}
1019 			break;
1020 		case '%':
1021 			if (*old_filename  == '\0') {
1022 				sprintf(errmsg, "no current filename");
1023 				return ERR;
1024 			}
1025 			j = strlen(s = strip_escapes(old_filename));
1026 			REALLOC(buf, n, i + j, ERR);
1027 			while (j--)
1028 				buf[i++] = *s++;
1029 			s = ibufp++;
1030 			break;
1031 		}
1032 	REALLOC(shcmd, shcmdsz, i + 1, ERR);
1033 	memcpy(shcmd, buf, i);
1034 	shcmd[shcmdi = i] = '\0';
1035 	return *s == '!' || *s == '%';
1036 }
1037 
1038 
1039 /* append_lines: insert text from stdin to after line n; stop when either a
1040    single period is read or EOF; return status */
1041 int
1042 append_lines(n)
1043 	long n;
1044 {
1045 	int l;
1046 	char *lp = ibuf;
1047 	char *eot;
1048 	undo_t *up = NULL;
1049 
1050 	for (current_addr = n;;) {
1051 		if (!isglobal) {
1052 			if ((l = get_tty_line()) < 0)
1053 				return ERR;
1054 			else if (l == 0 || ibuf[l - 1] != '\n') {
1055 				clearerr(stdin);
1056 				return  l ? EOF : 0;
1057 			}
1058 			lp = ibuf;
1059 		} else if (*(lp = ibufp) == '\0')
1060 			return 0;
1061 		else {
1062 			while (*ibufp++ != '\n')
1063 				;
1064 			l = ibufp - lp;
1065 		}
1066 		if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1067 			return 0;
1068 		}
1069 		eot = lp + l;
1070 		SPL1();
1071 		do {
1072 			if ((lp = put_sbuf_line(lp)) == NULL) {
1073 				SPL0();
1074 				return ERR;
1075 			} else if (up)
1076 				up->t = get_addressed_line_node(current_addr);
1077 			else if ((up = push_undo_stack(UADD, current_addr,
1078 			    current_addr)) == NULL) {
1079 				SPL0();
1080 				return ERR;
1081 			}
1082 		} while (lp != eot);
1083 		modified = 1;
1084 		SPL0();
1085 	}
1086 	/* NOTREACHED */
1087 }
1088 
1089 
1090 /* join_lines: replace a range of lines with the joined text of those lines */
1091 int
1092 join_lines(from, to)
1093 	long from;
1094 	long to;
1095 {
1096 	static char *buf = NULL;
1097 	static int n;
1098 
1099 	char *s;
1100 	int size = 0;
1101 	line_t *bp, *ep;
1102 
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)
1107 			return ERR;
1108 		REALLOC(buf, n, size + bp->len, ERR);
1109 		memcpy(buf + size, s, bp->len);
1110 		size += bp->len;
1111 	}
1112 	REALLOC(buf, n, size + 2, ERR);
1113 	memcpy(buf + size, "\n", 2);
1114 	if (delete_lines(from, to) < 0)
1115 		return ERR;
1116 	current_addr = from - 1;
1117 	SPL1();
1118 	if (put_sbuf_line(buf) == NULL ||
1119 	    push_undo_stack(UADD, current_addr, current_addr) == NULL) {
1120 		SPL0();
1121 		return ERR;
1122 	}
1123 	modified = 1;
1124 	SPL0();
1125 	return 0;
1126 }
1127 
1128 
1129 /* move_lines: move a range of lines */
1130 int
1131 move_lines(addr)
1132 	long addr;
1133 {
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);
1138 
1139 	SPL1();
1140 	if (done) {
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) {
1146 		SPL0();
1147 		return ERR;
1148 	} else {
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! */
1154 		} else {
1155 			b2 = get_addressed_line_node(addr);
1156 			b1 = get_addressed_line_node(p);
1157 					/* this get_addressed_line_node last! */
1158 		}
1159 		a2 = b2->q_forw;
1160 		REQUE(b2, b1->q_forw);
1161 		REQUE(a1->q_back, a2);
1162 		REQUE(b1, a1);
1163 		current_addr = addr + ((addr < first_addr) ?
1164 		    second_addr - first_addr + 1 : 0);
1165 	}
1166 	if (isglobal)
1167 		unset_active_nodes(b2->q_forw, a2);
1168 	modified = 1;
1169 	SPL0();
1170 	return 0;
1171 }
1172 
1173 
1174 /* copy_lines: copy a range of lines; return status */
1175 int
1176 copy_lines(addr)
1177 	long addr;
1178 {
1179 	line_t *lp, *np = get_addressed_line_node(first_addr);
1180 	undo_t *up = NULL;
1181 	long n = second_addr - first_addr + 1;
1182 	long m = 0;
1183 
1184 	current_addr = addr;
1185 	if (first_addr <= addr && addr < second_addr) {
1186 		n =  addr - first_addr + 1;
1187 		m = second_addr - addr;
1188 	}
1189 	for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1))
1190 		for (; n-- > 0; np = np->q_forw) {
1191 			SPL1();
1192 			if ((lp = dup_line_node(np)) == NULL) {
1193 				SPL0();
1194 				return ERR;
1195 			}
1196 			add_line_node(lp);
1197 			if (up)
1198 				up->t = lp;
1199 			else if ((up = push_undo_stack(UADD, current_addr,
1200 			    current_addr)) == NULL) {
1201 				SPL0();
1202 				return ERR;
1203 			}
1204 			modified = 1;
1205 			SPL0();
1206 		}
1207 	return 0;
1208 }
1209 
1210 
1211 /* delete_lines: delete a range of lines */
1212 int
1213 delete_lines(from, to)
1214 	long from, to;
1215 {
1216 	line_t *n, *p;
1217 
1218 	SPL1();
1219 	if (push_undo_stack(UDEL, from, to) == NULL) {
1220 		SPL0();
1221 		return ERR;
1222 	}
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! */
1226 	if (isglobal)
1227 		unset_active_nodes(p->q_forw, n);
1228 	REQUE(p, n);
1229 	addr_last -= to - from + 1;
1230 	current_addr = from - 1;
1231 	modified = 1;
1232 	SPL0();
1233 	return 0;
1234 }
1235 
1236 
1237 /* display_lines: print a range of lines to stdout */
1238 int
1239 display_lines(from, to, gflag)
1240 	long from;
1241 	long to;
1242 	int gflag;
1243 {
1244 	line_t *bp;
1245 	line_t *ep;
1246 	char *s;
1247 
1248 	if (!from) {
1249 		sprintf(errmsg, "invalid address");
1250 		return ERR;
1251 	}
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;
1259 	}
1260 	return 0;
1261 }
1262 
1263 
1264 #define MAXMARK 26			/* max number of marks */
1265 
1266 line_t	*mark[MAXMARK];			/* line markers */
1267 int markno;				/* line marker count */
1268 
1269 /* mark_line_node: set a line node mark */
1270 int
1271 mark_line_node(lp, n)
1272 	line_t *lp;
1273 	int n;
1274 {
1275 	if (!islower(n)) {
1276 		sprintf(errmsg, "invalid mark character");
1277 		return ERR;
1278 	} else if (mark[n - 'a'] == NULL)
1279 		markno++;
1280 	mark[n - 'a'] = lp;
1281 	return 0;
1282 }
1283 
1284 
1285 /* get_marked_node_addr: return address of a marked line */
1286 long
1287 get_marked_node_addr(n)
1288 	int n;
1289 {
1290 	if (!islower(n)) {
1291 		sprintf(errmsg, "invalid mark character");
1292 		return ERR;
1293 	}
1294 	return get_line_node_addr(mark[n - 'a']);
1295 }
1296 
1297 
1298 /* unmark_line_node: clear line node mark */
1299 void
1300 unmark_line_node(lp)
1301 	line_t *lp;
1302 {
1303 	int i;
1304 
1305 	for (i = 0; markno && i < MAXMARK; i++)
1306 		if (mark[i] == lp) {
1307 			mark[i] = NULL;
1308 			markno--;
1309 		}
1310 }
1311 
1312 
1313 /* dup_line_node: return a pointer to a copy of a line node */
1314 line_t *
1315 dup_line_node(lp)
1316 	line_t *lp;
1317 {
1318 	line_t *np;
1319 
1320 	if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) {
1321 		fprintf(stderr, "%s\n", strerror(errno));
1322 		sprintf(errmsg, "out of memory");
1323 		return NULL;
1324 	}
1325 	np->seek = lp->seek;
1326 	np->len = lp->len;
1327 	return np;
1328 }
1329 
1330 
1331 /* has_trailing_escape:  return the parity of escapes preceding a character
1332    in a string */
1333 int
1334 has_trailing_escape(s, t)
1335 	char *s;
1336 	char *t;
1337 {
1338     return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1339 }
1340 
1341 
1342 /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1343 char *
1344 strip_escapes(s)
1345 	char *s;
1346 {
1347 	static char *file = NULL;
1348 	static int filesz = 0;
1349 
1350 	int i = 0;
1351 
1352 	REALLOC(file, filesz, MAXPATHLEN + 1, NULL);
1353 	/* assert: no trailing escape */
1354 	while ((file[i++] = (*s == '\\') != '\0' ? *++s : *s))
1355 		s++;
1356 	return file;
1357 }
1358 
1359 
1360 void
1361 signal_hup(signo)
1362 	int signo;
1363 {
1364 	if (mutex)
1365 		sigflags |= (1 << (signo - 1));
1366 	else	handle_hup(signo);
1367 }
1368 
1369 
1370 void
1371 signal_int(signo)
1372 	int signo;
1373 {
1374 	if (mutex)
1375 		sigflags |= (1 << (signo - 1));
1376 	else	handle_int(signo);
1377 }
1378 
1379 
1380 void
1381 handle_hup(signo)
1382 	int signo;
1383 {
1384 	char *hup = NULL;		/* hup filename */
1385 	char *s;
1386 	int n;
1387 
1388 	if (!sigactive)
1389 		quit(1);
1390 	sigflags &= ~(1 << (signo - 1));
1391 	if (addr_last && write_file("ed.hup", "w", 1, addr_last) < 0 &&
1392 	    (s = getenv("HOME")) != NULL &&
1393 	    (n = strlen(s)) + 8 <= MAXPATHLEN &&	/* "ed.hup" + '/' */
1394 	    (hup = (char *) malloc(n + 10)) != NULL) {
1395 		strcpy(hup, s);
1396 		if (hup[n - 1] != '/')
1397 			hup[n] = '/', hup[n+1] = '\0';
1398 		strcat(hup, "ed.hup");
1399 		write_file(hup, "w", 1, addr_last);
1400 	}
1401 	quit(2);
1402 }
1403 
1404 
1405 void
1406 handle_int(signo)
1407 	int signo;
1408 {
1409 	if (!sigactive)
1410 		quit(1);
1411 	sigflags &= ~(1 << (signo - 1));
1412 #ifdef _POSIX_SOURCE
1413 	siglongjmp(env, -1);
1414 #else
1415 	longjmp(env, -1);
1416 #endif
1417 }
1418 
1419 
1420 int cols = 72;				/* wrap column */
1421 
1422 void
1423 handle_winch(signo)
1424 	int signo;
1425 {
1426 	struct winsize ws;		/* window size structure */
1427 
1428 	sigflags &= ~(1 << (signo - 1));
1429 	if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) {
1430 		if (ws.ws_row > 2) rows = ws.ws_row - 2;
1431 		if (ws.ws_col > 8) cols = ws.ws_col - 8;
1432 	}
1433 }
1434 
1435 
1436 /* is_legal_filename: return a legal filename */
1437 int
1438 is_legal_filename(s)
1439 	char *s;
1440 {
1441 	if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1442 		sprintf(errmsg, "shell access restricted");
1443 		return 0;
1444 	}
1445 	return 1;
1446 }
1447