xref: /minix/bin/ed/main.c (revision 4db99f40)
1 /*	$NetBSD: main.c,v 1.28.8.2 2018/06/22 10:08:22 martin 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.\
35  All rights reserved.");
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.28.8.2 2018/06/22 10:08:22 martin 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 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 */
100 
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 */
107 
108 
109 static const char usage[] = "Usage: %s [-] [-ESsx] [-p string] [name]\n";
110 
111 /* ed: line editor */
112 int
main(int ac,char * av[])113 main(int ac, char *av[])
114 {
115 	int c, n;
116 	long status = 0;
117 	volatile int argc = ac;
118 	char ** volatile argv = av;
119 
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;
137 
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 */
148 		}
149 	argv += optind;
150 	argc -= optind;
151 	if (argc && **argv == '-') {
152 		scripted = 1;
153 		if (argc > 1) {
154 			optind = 1;
155 			goto top;
156 		}
157 		argv++;
158 		argc--;
159 	}
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
173 	{
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);
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 				seterrmsg("warning: file modified");
207 				if (!isatty(0)) {
208 					if (garrulous) {
209 						fprintf(stderr,
210 						    "script, line %d: %s\n",
211 						    lineno, errmsg);
212 					}
213 					quit(2);
214 				}
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;
227 		}
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;
237 		}
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);
250 				}
251 				quit(2);
252 			}
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);
262 				}
263 			}
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);
271 				}
272 				quit(2);
273 			}
274 			break;
275 		}
276 	}
277 	/* NOTREACHED */
278 }
279 
280 long first_addr, second_addr, addr_cnt;
281 
282 /* extract_addr_range: get line addresses from the command buffer until an
283    illegal address is seen; return status */
284 int
extract_addr_range(void)285 extract_addr_range(void)
286 {
287 	long addr;
288 
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;
299 	}
300 	if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr)
301 		first_addr = second_addr;
302 	return (addr == ERR) ? ERR : 0;
303 }
304 
305 
306 #define	SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') \
307 	ibufp++
308 
309 #define MUST_BE_FIRST() \
310 	if (!first) { seterrmsg("invalid address"); return ERR; }
311 
312 /*  next_addr: return the next line address in the command buffer */
313 long
next_addr(void)314 next_addr(void)
315 {
316 	char *hd;
317 	long addr = current_addr;
318 	long n;
319 	int first = 1;
320 	int c;
321 
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;
374 			}
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;
384 		}
385 	/* NOTREACHED */
386 }
387 
388 
389 #ifdef BACKWARDS
390 /* GET_THIRD_ADDR: get a legal address from the command buffer */
391 #define GET_THIRD_ADDR(addr) \
392 { \
393 	long ol1, ol2; \
394 \
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; \
404 	} \
405 	addr = second_addr; \
406 	first_addr = ol1, second_addr = ol2; \
407 }
408 #else	/* BACKWARDS */
409 /* GET_THIRD_ADDR: get a legal address from the command buffer */
410 #define GET_THIRD_ADDR(addr) \
411 { \
412 	long ol1, ol2; \
413 \
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; \
420 	} \
421 	addr = second_addr; \
422 	first_addr = ol1, second_addr = ol2; \
423 }
424 #endif
425 
426 
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++; \
443 		} \
444 	} while (!done); \
445 	if (*ibufp++ != '\n') { \
446 		seterrmsg("invalid command suffix"); \
447 		return ERR; \
448 	} \
449 }
450 
451 
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 */
457 
458 int patlock = 0;	/* if set, pattern not freed by get_compiled_pattern() */
459 
460 long rows = 22;		/* scroll length: ws_row - 2 */
461 
462 /* exec_command: execute the next command in command buffer; return print
463    request, if any */
464 int
exec_command(void)465 exec_command(void)
466 {
467 	static pattern_t *pat = NULL;
468 	static int sgflag = 0;
469 	static long sgnum = 0;
470 
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;
478 
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;
533 		}
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;
553 		}
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;
579 		}
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;
587 		}
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;
596 		}
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;
616 		}
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;
636 		}
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;
662 		}
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;
671 		}
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;
691 		}
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;
726 				}
727 			}
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;
737 		}
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);
748 			}
749 			pat = tpat;
750 			patlock = 1;		/* reserve pattern */
751 		}
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++;
776 			}
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;
798 		}
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++;
808 		}
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;
825 		}
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;
839 		}
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;
871 		}
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;
891 	}
892 	return gflag;
893 }
894 
895 
896 /* check_addr_range: return status of address range check */
897 int
check_addr_range(long n,long m)898 check_addr_range(long n, long m)
899 {
900 	if (addr_cnt == 0) {
901 		first_addr = n;
902 		second_addr = m;
903 	}
904 	if (first_addr > second_addr || 1 > first_addr ||
905 	    second_addr > addr_last) {
906 		seterrmsg("invalid address");
907 		return ERR;
908 	}
909 	return 0;
910 }
911 
912 
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
get_matching_node_addr(pattern_t * pat,int dir)917 get_matching_node_addr(pattern_t *pat, int dir)
918 {
919 	char *s;
920 	long n = current_addr;
921 	line_t *lp;
922 
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;
934 		}
935 	} while (n != current_addr);
936 	seterrmsg("no match");
937 	return  ERR;
938 }
939 
940 
941 /* get_filename: return pointer to copy of filename in the command buffer */
942 char *
get_filename(void)943 get_filename(void)
944 {
945 	static char *file = NULL;
946 	static int filesz = 0;
947 
948 	int n;
949 
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;
966 		}
967 	}
968 #ifndef BACKWARDS
969 	else if (*old_filename == '\0') {
970 		seterrmsg("no current filename");
971 		return  NULL;
972 	}
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;
979 }
980 
981 
982 /* get_shell_command: read a shell command from stdin; return substitution
983    status */
984 int
get_shell_command(void)985 get_shell_command(void)
986 {
987 	static char *buf = NULL;
988 	static int n = 0;
989 
990 	char *s;			/* substitution char pointer */
991 	int i = 0;
992 	int j = 0;
993 
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++;
1013 			}
1014 #ifdef BACKWARDS
1015 			else if (shcmd == NULL || *(shcmd + 1) == '\0')
1016 #else
1017 			else if (shcmd == NULL)
1018 #endif
1019 			{
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++;
1027 			}
1028 			break;
1029 		case '%':
1030 			if (*old_filename  == '\0') {
1031 				seterrmsg("no current filename");
1032 				return ERR;
1033 			}
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;
1040 		}
1041 	REALLOC(shcmd, shcmdsz, i + 1, ERR);
1042 	memcpy(shcmd, buf, i);
1043 	shcmd[shcmdi = i] = '\0';
1044 	return *s == '!' || *s == '%';
1045 }
1046 
1047 
1048 /* append_lines: insert text from stdin to after line n; stop when either a
1049    single period is read or EOF; return status */
1050 int
append_lines(long n)1051 append_lines(long n)
1052 {
1053 	int l;
1054 	char *lp = ibuf;
1055 	char *eot;
1056 	undo_t *up = NULL;
1057 
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;
1065 			}
1066 			lp = ibuf;
1067 		} else if (*(lp = ibufp) == '\0')
1068 			return 0;
1069 		else {
1070 			while (*ibufp++ != '\n')
1071 				;
1072 			l = ibufp - lp;
1073 		}
1074 		if (l == 2 && lp[0] == '.' && lp[1] == '\n') {
1075 			return 0;
1076 		}
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;
1089 			}
1090 		} while (lp != eot);
1091 		modified = 1;
1092 		SPL0();
1093 	}
1094 	/* NOTREACHED */
1095 }
1096 
1097 
1098 /* join_lines: replace a range of lines with the joined text of those lines */
1099 int
join_lines(long from,long to)1100 join_lines(long from, long to)
1101 {
1102 	static char *buf = NULL;
1103 	static int n;
1104 
1105 	char *s;
1106 	int size = 0;
1107 	line_t *bp, *ep;
1108 
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;
1117 	}
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;
1128 	}
1129 	modified = 1;
1130 	SPL0();
1131 	return 0;
1132 }
1133 
1134 
1135 /* move_lines: move a range of lines */
1136 int
move_lines(long addr)1137 move_lines(long addr)
1138 {
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);
1143 
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! */
1163 		}
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);
1170 	}
1171 	if (isglobal)
1172 		unset_active_nodes(b2->q_forw, a2);
1173 	modified = 1;
1174 	SPL0();
1175 	return 0;
1176 }
1177 
1178 
1179 /* copy_lines: copy a range of lines; return status */
1180 int
copy_lines(long addr)1181 copy_lines(long addr)
1182 {
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;
1187 
1188 	current_addr = addr;
1189 	if (first_addr <= addr && addr < second_addr) {
1190 		n =  addr - first_addr + 1;
1191 		m = second_addr - addr;
1192 	}
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;
1199 			}
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;
1207 			}
1208 			modified = 1;
1209 			SPL0();
1210 		}
1211 	return 0;
1212 }
1213 
1214 
1215 /* delete_lines: delete a range of lines */
1216 int
delete_lines(long from,long to)1217 delete_lines(long from, long to)
1218 {
1219 	line_t *n, *p;
1220 
1221 	SPL1();
1222 	if (push_undo_stack(UDEL, from, to) == NULL) {
1223 		SPL0();
1224 		return ERR;
1225 	}
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;
1237 }
1238 
1239 
1240 /* display_lines: print a range of lines to stdout */
1241 int
display_lines(long from,long to,int gflag)1242 display_lines(long from, long to, int gflag)
1243 {
1244 	line_t *bp;
1245 	line_t *ep;
1246 	char *s;
1247 
1248 	if (!from) {
1249 		seterrmsg("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
mark_line_node(line_t * lp,int n)1271 mark_line_node(line_t *lp, int n)
1272 {
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;
1280 }
1281 
1282 
1283 /* get_marked_node_addr: return address of a marked line */
1284 long
get_marked_node_addr(int n)1285 get_marked_node_addr(int n)
1286 {
1287 	if (!islower(n)) {
1288 		seterrmsg("invalid mark character");
1289 		return ERR;
1290 	}
1291 	return get_line_node_addr(mark[n - 'a']);
1292 }
1293 
1294 
1295 /* unmark_line_node: clear line node mark */
1296 void
unmark_line_node(line_t * lp)1297 unmark_line_node(line_t *lp)
1298 {
1299 	int i;
1300 
1301 	for (i = 0; markno && i < MAXMARK; i++)
1302 		if (mark[i] == lp) {
1303 			mark[i] = NULL;
1304 			markno--;
1305 		}
1306 }
1307 
1308 
1309 /* dup_line_node: return a pointer to a copy of a line node */
1310 line_t *
dup_line_node(line_t * lp)1311 dup_line_node(line_t *lp)
1312 {
1313 	line_t *np;
1314 
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;
1319 	}
1320 	np->seek = lp->seek;
1321 	np->len = lp->len;
1322 	return np;
1323 }
1324 
1325 
1326 /* has_trailing_escape:  return the parity of escapes preceding a character
1327    in a string */
1328 int
has_trailing_escape(char * s,char * t)1329 has_trailing_escape(char *s, char *t)
1330 {
1331     return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1);
1332 }
1333 
1334 
1335 /* strip_escapes: return copy of escaped string of at most length MAXPATHLEN */
1336 char *
strip_escapes(const char * s)1337 strip_escapes(const char *s)
1338 {
1339 	static char *file = NULL;
1340 	static int filesz = 0;
1341 
1342 	int i = 0;
1343 
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;
1350 }
1351 
1352 
1353 void
signal_hup(int signo)1354 signal_hup(int signo)
1355 {
1356 	if (mutex)
1357 		sigflags |= (1 << (signo - 1));
1358 	else	handle_hup(signo);
1359 }
1360 
1361 
1362 void
signal_int(int signo)1363 signal_int(int signo)
1364 {
1365 	if (mutex)
1366 		sigflags |= (1 << (signo - 1));
1367 	else	handle_int(signo);
1368 }
1369 
1370 
1371 void
handle_hup(int signo)1372 handle_hup(int signo)
1373 {
1374 	char *hup = NULL;		/* hup filename */
1375 	char *s;
1376 	int n;
1377 
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);
1390 	}
1391 	quit(2);
1392 }
1393 
1394 
1395 void
handle_int(int signo)1396 handle_int(int signo)
1397 {
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
1406 }
1407 
1408 
1409 int cols = 72;				/* wrap column */
1410 
1411 void
handle_winch(int signo)1412 handle_winch(int signo)
1413 {
1414 	struct winsize ws;		/* window size structure */
1415 
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;
1420 	}
1421 }
1422 
1423 
1424 /* is_legal_filename: return a legal filename */
1425 int
is_legal_filename(char * s)1426 is_legal_filename(char *s)
1427 {
1428 	if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) {
1429 		seterrmsg("shell access restricted");
1430 		return 0;
1431 	}
1432 	return 1;
1433 }
1434