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