heirloom-ed

view ed.c @ 3:ac52712b2b5e

Remove the fspec(5) tabulator support We don't want such bloat in ed(1)!
author markus schnalke <meillo@marmaro.de>
date Sun, 12 Apr 2015 21:45:34 +0200
parents a09d0630f05b
children 4165f1b57d18
line source
1 /*
2 * Editor
3 */
5 /*
6 * Changes by Gunnar Ritter, Freiburg i. Br., Germany, July 2003.
7 */
8 /* from Unix 32V /usr/src/cmd/ed.c */
9 /*
10 * Copyright(C) Caldera International Inc. 2001-2002. 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 * Redistributions of source code and documentation must retain the
16 * above copyright notice, this list of conditions and the following
17 * disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed or owned by Caldera
24 * International, Inc.
25 * Neither the name of Caldera International, Inc. nor the names of
26 * other contributors may be used to endorse or promote products
27 * derived from this software without specific prior written permission.
28 *
29 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
30 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
34 * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
39 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
43 #if __GNUC__ >= 3 && __GNUC_MINOR__ >= 4 || __GNUC__ >= 4
44 #define USED __attribute__ ((used))
45 #elif defined __GNUC__
46 #define USED __attribute__ ((unused))
47 #else
48 #define USED
49 #endif
50 #if defined (SU3)
51 static const char sccsid[] USED = "@(#)ed_su3.sl 1.99 (gritter) 7/27/06";
52 #elif defined (SUS)
53 static const char sccsid[] USED = "@(#)ed_sus.sl 1.99 (gritter) 7/27/06";
54 #elif defined (S42)
55 static const char sccsid[] USED = "@(#)ed_s42.sl 1.99 (gritter) 7/27/06";
56 #else /* !SU3, !SUS, !S42 */
57 static const char sccsid[] USED = "@(#)ed.sl 1.99 (gritter) 7/27/06";
58 #endif /* !SU3, !SUS, !S42 */
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <sys/wait.h>
63 #include <sys/stat.h>
64 #include <fcntl.h>
65 #include <unistd.h>
66 #include <time.h>
67 #include <string.h>
68 #include <stdlib.h>
69 #include <signal.h>
70 #include "sigset.h"
71 #include <setjmp.h>
72 #include <libgen.h>
73 #include <inttypes.h>
74 #include <locale.h>
75 #include <wchar.h>
76 #include <ctype.h>
77 #include <wctype.h>
78 #include <limits.h>
79 static int FNSIZE;
80 static int LBSIZE;
81 static int RHSIZE;
82 #define ESIZE 2048
83 static int GBSIZE;
84 #undef EOF
85 #define EOF -1
86 #define puts(s) xxputs(s)
87 #define getline(t, n) xxgetline(t, n)
89 #if (LONG_MAX > 017777777777L)
90 #define MAXCNT 0777777777777777777777L /* 2^63-1 */
91 #else
92 #define MAXCNT 017777777777L /* 2^31-1 */
93 #endif
94 #define BLKMSK (MAXCNT>>8) /* was 0377 */
96 #define READ 0
97 #define WRITE 1
98 #define EXIST 2
100 static int peekc;
101 static int lastc;
102 static char *savedfile;
103 static char *file;
104 static struct stat fstbuf;
105 static char *linebuf;
106 static char *rhsbuf;
107 static char expbuf[ESIZE + 4];
108 static long *zero;
109 static long *undzero;
110 static long *dot;
111 static long *unddot;
112 static long *dol;
113 static long *unddol;
114 static long *addr1;
115 static long *addr2;
116 static char *genbuf;
117 static long count;
118 static char *linebp;
119 static int ninbuf;
120 static int io;
121 static int ioeof;
122 static int pflag;
123 static char *wrtemp;
124 static uid_t myuid;
125 static void (*oldhup)(int);
126 static void (*oldquit)(int);
127 static void (*oldpipe)(int);
128 static int vflag = 1;
129 static int listf;
130 static int numbf;
131 static char *globp;
132 static int tfile = -1;
133 static long tline;
134 static char tfname[64];
135 static char ibuff[512];
136 static int iblock = -1;
137 static char obuff[512];
138 static int oblock = -1;
139 static int ichanged;
140 static int nleft;
141 static long *names;
142 static long *undnames;
143 static int anymarks;
144 static int subnewa;
145 static int fchange;
146 static int wrapp;
147 static unsigned nlall = 128;
148 static const char *progname;
149 static const char *prompt = "*";
150 static int Pflag;
151 static int prhelp;
152 static const char *prvmsg;
153 static int lastsig;
154 static int pipid = -1;
155 static int readop;
156 static int status;
157 static int mb_cur_max;
158 static int needsub;
159 static int insub;
160 static int maxlength;
161 static int rspec;
162 static int Nflag;
163 static int bcount = 22;
164 static int ocount = 11;
166 static jmp_buf savej;
168 static void usage(char);
169 static void commands(void);
170 static long *address(void);
171 static void setdot(void);
172 static void setall(void);
173 static void setnoaddr(void);
174 static void nonzero(void);
175 static void newline(void);
176 static void filename(int);
177 static void exfile(void);
178 static void onintr(int);
179 static void onhup(int);
180 static void onpipe(int);
181 static void error(const char *);
182 static void error2(const char *, const char *);
183 static void errput(const char *, const char *);
184 static int getchr(void);
185 static int gettty(void);
186 static long getnum(void);
187 static int getfile(void);
188 static void putfile(void);
189 static int append(int (*)(void), long *);
190 static void callunix(void);
191 static char *readcmd(void);
192 static void quit(int);
193 static void delete(void);
194 static void rdelete(long *, long *);
195 static void gdelete(void);
196 static char *getline(long, int);
197 static int putline(void);
198 static char *getblock(long, long);
199 static void blkio(long, char *, int);
200 static void init(void);
201 static void global(int, int);
202 static void globrd(char **, int);
203 static void join(void);
204 static void substitute(int);
205 static int compsub(void);
206 static int getsub(void);
207 static int dosub(int);
208 static int place(int, const char *, const char *);
209 static void move(int);
210 static void reverse(long *, long *);
211 static int getcopy(void);
212 static int execute(int, long *, int);
213 static void cmplerr(int);
214 static void doprnt(long *, long *);
215 static void putd(long);
216 static void puts(const char *);
217 static void nlputs(const char *);
218 static void list(const char *);
219 static int lstchr(int);
220 static void putstr(const char *);
221 static void putchr(int);
222 static void checkpoint(void);
223 static void undo(void);
224 static int maketf(int);
225 static int creatf(const char *);
226 static int sopen(const char *, int);
227 static void sclose(int);
228 static void growlb(const char *);
229 static void growrhs(const char *);
230 static void growfn(const char *);
231 static void help(void);
233 #define INIT
234 #define GETC() getchr()
235 #define UNGETC(c) (peekc = c)
236 #define PEEKC() (peekc = getchr())
237 #define RETURN(c) return c
238 #define ERROR(c) cmplerr(c)
239 static wint_t GETWC(char *);
241 #if defined (SUS) || defined (S42) || defined (SU3)
243 #include <regex.h>
245 #define NBRA 9
247 static char *braslist[NBRA];
248 static char *braelist[NBRA];
249 static char *loc1, *loc2, *locs;
250 static int nbra;
251 static int circf;
252 static int nodelim;
254 static char *compile(char *, char *, const char *, int);
255 static int step(const char *, const char *);
257 #else /* !SUS, !S42, !SU3 */
259 #include <regexp.h>
261 #endif /* !SUS, !S42, !SU3 */
263 int
264 main(int argc, char **argv)
265 {
266 register int i;
267 void (*oldintr)(int);
269 progname = basename(argv[0]);
270 #if defined (SUS) || defined (S42) || defined (SU3)
271 setlocale(LC_COLLATE, "");
272 #endif
273 setlocale(LC_CTYPE, "");
274 mb_cur_max = MB_CUR_MAX;
275 myuid = getuid();
276 oldquit = sigset(SIGQUIT, SIG_IGN);
277 oldhup = sigset(SIGHUP, SIG_IGN);
278 oldintr = sigset(SIGINT, SIG_IGN);
279 if (sigset(SIGTERM, SIG_IGN) != SIG_IGN)
280 sigset(SIGTERM, quit);
281 oldpipe = sigset(SIGPIPE, onpipe);
282 argv++;
283 if (argc > 1 && **argv=='-') {
284 if ((*argv)[1]=='\0') {
285 vflag = 0;
286 } else {
287 usage((*argv)[1]);
288 }
289 argv++;
290 argc--;
291 }
293 growfn("no space");
294 if (argc>1) {
295 i = -1;
296 do
297 if (++i >= FNSIZE)
298 growfn("maximum of characters in "
299 "file names reached");
300 while (savedfile[i] = (*argv)[i]);
301 globp = "e";
302 }
303 names = malloc(26*sizeof *names);
304 undnames = malloc(26*sizeof *undnames);
305 zero = malloc(nlall*sizeof *zero);
306 if ((undzero = malloc(nlall*sizeof *undzero)) == NULL)
307 puts("no memory for undo");
308 growlb("no space");
309 growrhs("no space");
310 init();
311 if (oldintr != SIG_IGN)
312 sigset(SIGINT, onintr);
313 if (oldhup != SIG_IGN)
314 sigset(SIGHUP, onhup);
315 setjmp(savej);
316 if (lastsig) {
317 sigrelse(lastsig);
318 lastsig = 0;
319 }
320 commands();
321 quit(0);
322 /*NOTREACHED*/
323 return 0;
324 }
326 static void
327 usage(char c)
328 {
329 if (c) {
330 write(2, progname, strlen(progname));
331 write(2, ": illegal option -- ", 20);
332 write(2, &c, 1);
333 write(2, "\n", 1);
334 }
335 write(2, "usage: ", 7);
336 write(2, progname, strlen(progname));
337 write(2, " [-] [file]\n", 12);
338 exit(2);
339 }
341 static void
342 commands(void)
343 {
344 register long *a1;
345 register int c;
346 int n;
348 for (;;) {
349 if (pflag) {
350 pflag = 0;
351 addr1 = addr2 = dot;
352 goto print;
353 }
354 if (Pflag && globp == NULL)
355 write(1, prompt, strlen(prompt));
356 addr1 = 0;
357 addr2 = 0;
358 switch (c = getchr()) {
359 case ',':
360 case ';':
361 addr2 = c == ',' ? zero+1 : dot;
362 if (((peekc = getchr()) < '0' || peekc > '9') &&
363 peekc != ' ' && peekc != '\t' &&
364 peekc != '+' && peekc != '-' &&
365 peekc != '^' && peekc != '?' &&
366 peekc != '/' && peekc != '$' &&
367 peekc != '.' && peekc != '\'') {
368 addr1 = addr2;
369 a1 = dol;
370 goto loop;
371 }
372 break;
373 default:
374 peekc = c;
375 }
376 do {
377 addr1 = addr2;
378 if ((a1 = address())==0) {
379 c = getchr();
380 break;
381 }
382 loop: addr2 = a1;
383 if ((c=getchr()) == ';') {
384 c = ',';
385 dot = a1;
386 }
387 } while (c==',');
388 if (addr1==0)
389 addr1 = addr2;
390 switch(c) {
392 case 'a':
393 setdot();
394 newline();
395 checkpoint();
396 append(gettty, addr2);
397 continue;
399 case 'c':
400 #if defined (SU3)
401 if (addr1 == zero && addr1+1 <= dol) {
402 if (addr1 == addr2)
403 addr2++;
404 addr1++;
405 }
406 #endif /* SU3 */
407 delete();
408 append(gettty, addr1-1);
409 #if defined (SUS) || defined (SU3)
410 if (dot == addr1-1 && addr1 <= dol)
411 dot = addr1;
412 #endif /* SUS || SU3 */
413 continue;
415 case 'd':
416 delete();
417 continue;
419 case 'E':
420 fchange = 0;
421 c = 'e';
422 case 'e':
423 setnoaddr();
424 if (vflag && fchange) {
425 fchange = 0;
426 error("warning: expecting `w'");
427 }
428 filename(c);
429 init();
430 addr2 = zero;
431 goto caseread;
433 case 'f':
434 setnoaddr();
435 filename(c);
436 puts(savedfile);
437 continue;
439 case 'g':
440 global(1, 0);
441 continue;
443 case 'G':
444 global(1, 1);
445 continue;
447 case 'H':
448 prhelp = !prhelp;
449 /*FALLTHRU*/
451 case 'h':
452 if ((peekc = getchr()) == 'e') {
453 peekc = 0;
454 if (getchr() != 'l' || getchr() != 'p' ||
455 getchr() != '\n')
456 error("illegal suffix");
457 setnoaddr();
458 help();
459 continue;
460 }
461 newline();
462 setnoaddr();
463 if (prvmsg)
464 puts(prvmsg);
465 continue;
467 case 'i':
468 setdot();
469 #if defined (SU3)
470 if (addr1 == zero) {
471 if (addr1 == addr2)
472 addr2++;
473 addr1++;
474 if (dol != zero)
475 nonzero();
476 } else
477 #endif /* SU3 */
478 nonzero();
479 newline();
480 checkpoint();
481 append(gettty, addr2-1);
482 if (dot == addr2-1)
483 dot++;
484 continue;
487 case 'j':
488 if (addr2==0) {
489 addr1 = dot;
490 addr2 = dot+1;
491 }
492 setdot();
493 newline();
494 nonzero();
495 checkpoint();
496 if (addr1 != addr2)
497 join();
498 continue;
500 case 'k':
501 if ((c = getchr()) < 'a' || c > 'z')
502 error("mark not lower case");
503 newline();
504 setdot();
505 nonzero();
506 names[c-'a'] = *addr2 & ~01;
507 anymarks |= 01;
508 continue;
510 case 'm':
511 move(0);
512 continue;
514 case '\n':
515 if (addr2==0)
516 addr2 = dot+1;
517 addr1 = addr2;
518 goto print;
520 case 'n':
521 numbf = 1;
522 newline();
523 goto print;
525 case 'N':
526 newline();
527 setnoaddr();
528 Nflag = !Nflag;
529 continue;
531 case 'b':
532 case 'o':
533 n = getnum();
534 newline();
535 setdot();
536 nonzero();
537 if (n >= 0) {
538 if (c == 'b')
539 bcount = n;
540 else
541 ocount = n;
542 }
543 if (c == 'b') {
544 a1 = addr2+bcount > dol ? dol : addr2 + bcount;
545 doprnt(addr1, a1);
546 dot = a1;
547 } else {
548 a1 = addr2+ocount > dol ? dol : addr2 + ocount;
549 doprnt(addr2-ocount<zero+1?zero+1:addr2-ocount, a1);
550 dot = addr2;
551 }
552 continue;
554 case 'l':
555 listf++;
556 case 'p':
557 newline();
558 print:
559 setdot();
560 nonzero();
561 doprnt(addr1, addr2);
562 dot = addr2;
563 continue;
565 case 'P':
566 setnoaddr();
567 newline();
568 Pflag = !Pflag;
569 continue;
571 case 'Q':
572 fchange = 0;
573 case 'q':
574 setnoaddr();
575 newline();
576 quit(0);
578 case 'r':
579 filename(c);
580 caseread:
581 if ((io = sopen(file, READ)) < 0) {
582 lastc = '\n';
583 error2("cannot open input file", file);
584 }
585 ioeof = 0;
586 setall();
587 ninbuf = 0;
588 if (c == 'r')
589 checkpoint();
590 n = zero != dol;
591 rspec = (c == 'e' || !n) && file[0] != '!';
592 append(getfile, addr2);
593 rspec = 0;
594 exfile();
595 fchange = n;
596 continue;
598 case 's':
599 setdot();
600 nonzero();
601 substitute(globp!=0);
602 continue;
604 case 't':
605 move(1);
606 continue;
608 case 'u':
609 setdot();
610 newline();
611 if (unddot == NULL)
612 error("nothing to undo");
613 undo();
614 continue;
616 case 'v':
617 global(0, 0);
618 continue;
620 case 'V':
621 global(0, 1);
622 continue;
624 case 'W':
625 wrapp++;
626 case 'w':
627 write:
628 setall();
629 if (zero != dol)
630 nonzero();
631 filename(c);
632 if(!wrapp ||
633 ((io = open(file,O_WRONLY|O_APPEND)) == -1) ||
634 ((lseek(io, 0, SEEK_END)) == -1)) {
635 struct stat st;
636 if (lstat(file, &st) == 0 &&
637 (st.st_mode&S_IFMT) == S_IFREG &&
638 st.st_nlink == 1 &&
639 (myuid==0 || myuid==st.st_uid)) {
640 char *cp, *tp;
641 int nio;
642 if ((io = sopen(file, EXIST)) < 0)
643 error("cannot create output file");
644 if ((wrtemp = malloc(strlen(file)+8)) == NULL)
645 error("out of memory");
646 for (cp = file, tp = wrtemp; *cp; cp++)
647 *tp++ = *cp;
648 while (tp > wrtemp && tp[-1] != '/')
649 tp--;
650 for (cp = "\7XXXXXX"; *cp; cp++)
651 *tp++ = *cp;
652 *tp = '\0';
653 if ((nio = mkstemp(wrtemp)) < 0) {
654 free(wrtemp);
655 wrtemp = NULL;
656 ftruncate(io, 0);
657 } else {
658 close(io);
659 io = nio;
660 }
661 } else {
662 if ((io = sopen(file, WRITE)) < 0)
663 error("cannot create output file");
664 }
665 }
666 if (zero != dol) {
667 ioeof = 0;
668 wrapp = 0;
669 putfile();
670 }
671 exfile();
672 if (addr1==zero+1 && addr2==dol || addr1==addr2 && dol==zero)
673 fchange = 0;
674 if (c == 'z')
675 quit(0);
676 continue;
678 case 'z':
679 if ((peekc=getchr()) != '\n')
680 error("illegal suffix");
681 setnoaddr();
682 goto write;
684 case '=':
685 setall();
686 newline();
687 putd((addr2-zero)&MAXCNT);
688 putchr('\n');
689 continue;
691 case '!':
692 callunix();
693 continue;
695 case EOF:
696 return;
698 }
699 error("unknown command");
700 }
701 }
703 static long *
704 address(void)
705 {
706 register long *a1;
707 register int minus, c;
708 int n, relerr;
710 minus = 0;
711 a1 = 0;
712 for (;;) {
713 c = getchr();
714 if ('0'<=c && c<='9') {
715 n = 0;
716 do {
717 n *= 10;
718 n += c - '0';
719 } while ((c = getchr())>='0' && c<='9');
720 peekc = c;
721 if (a1==0)
722 a1 = zero;
723 if (minus<0)
724 n = -n;
725 a1 += n;
726 minus = 0;
727 continue;
728 }
729 relerr = 0;
730 if (a1 || minus)
731 relerr++;
732 switch(c) {
733 case ' ':
734 case '\t':
735 continue;
737 case '+':
738 minus++;
739 if (a1==0)
740 a1 = dot;
741 continue;
743 case '-':
744 case '^':
745 minus--;
746 if (a1==0)
747 a1 = dot;
748 continue;
750 case '?':
751 case '/':
752 compile(NULL, expbuf, &expbuf[ESIZE], c);
753 a1 = dot;
754 for (;;) {
755 if (c=='/') {
756 a1++;
757 if (a1 > dol)
758 a1 = zero;
759 } else {
760 a1--;
761 if (a1 < zero)
762 a1 = dol;
763 }
764 if (execute(0, a1, 0))
765 break;
766 if (a1==dot)
767 error("search string not found");
768 }
769 break;
771 case '$':
772 a1 = dol;
773 break;
775 case '.':
776 a1 = dot;
777 break;
779 case '\'':
780 if ((c = getchr()) < 'a' || c > 'z')
781 error("mark not lower case");
782 for (a1=zero; a1<=dol; a1++)
783 if (names[c-'a'] == (*a1 & ~01))
784 break;
785 break;
787 default:
788 peekc = c;
789 if (a1==0)
790 return(0);
791 a1 += minus;
792 if (a1<zero || a1>dol)
793 error("line out of range");
794 return(a1);
795 }
796 if (relerr)
797 error("bad number");
798 }
799 }
801 static void
802 setdot(void)
803 {
804 if (addr2 == 0)
805 addr1 = addr2 = dot;
806 if (addr1 > addr2)
807 error("bad range");
808 }
810 static void
811 setall(void)
812 {
813 if (addr2==0) {
814 addr1 = zero+1;
815 addr2 = dol;
816 if (dol==zero)
817 addr1 = zero;
818 }
819 setdot();
820 }
822 static void
823 setnoaddr(void)
824 {
825 if (addr2)
826 error("Illegal address count");
827 }
829 static void
830 nonzero(void)
831 {
832 if (addr1<=zero || addr2>dol)
833 error("line out of range");
834 }
836 static void
837 newline(void)
838 {
839 register int c;
841 if ((c = getchr()) == '\n')
842 return;
843 if (c=='p' || c=='l' || c=='n') {
844 pflag++;
845 if (c=='l')
846 listf++;
847 else if (c=='n')
848 numbf = 1;
849 if (getchr() == '\n')
850 return;
851 }
852 error("illegal suffix");
853 }
855 static void
856 filename(int comm)
857 {
858 register char *p1, *p2;
859 register int c, i;
861 count = 0;
862 c = getchr();
863 if (c=='\n' || c==EOF) {
864 p1 = savedfile;
865 if (*p1==0 && comm!='f')
866 error("illegal or missing filename");
867 p2 = file;
868 while (*p2++ = *p1++)
869 ;
870 return;
871 }
872 if (c!=' ')
873 error("no space after command");
874 while ((c = getchr()) == ' ')
875 ;
876 if (c=='\n')
877 error("illegal or missing filename");
878 i = 0;
879 do {
880 if (i >= FNSIZE)
881 growfn("maximum of characters in file names reached");
882 file[i++] = c;
883 if (c==' ' && file[0] != '!' || c==EOF)
884 error("illegal or missing filename");
885 } while ((c = getchr()) != '\n');
886 file[i++] = 0;
887 if ((savedfile[0]==0 || comm=='e' || comm=='f') && file[0] != '!') {
888 p1 = savedfile;
889 p2 = file;
890 while (*p1++ = *p2++)
891 ;
892 }
893 }
895 static void
896 exfile(void)
897 {
898 sclose(io);
899 io = -1;
900 if (wrtemp) {
901 extern int rename(const char *, const char *);
902 if (rename(wrtemp, file) < 0)
903 error("cannot create output file");
904 if (myuid == 0)
905 chown(file, fstbuf.st_uid, fstbuf.st_gid);
906 chmod(file, fstbuf.st_mode & 07777);
907 free(wrtemp);
908 wrtemp = NULL;
909 }
910 if (vflag) {
911 putd(count);
912 putchr('\n');
913 }
914 }
916 static void
917 onintr(int signo)
918 {
919 lastsig = signo;
920 putchr('\n');
921 lastc = '\n';
922 if (readop) {
923 puts("\007read may be incomplete - beware!\007");
924 fchange = 0;
925 }
926 error("interrupt");
927 }
929 static void
930 onhup(int signo)
931 {
932 if (dol > zero && fchange) {
933 addr1 = zero+1;
934 addr2 = dol;
935 io = creat("ed.hup", 0666);
936 if (io < 0) {
937 char *home = getenv("HOME");
938 if (home) {
939 char *fn = malloc(strlen(home) + 10);
940 if (fn) {
941 strcpy(fn, home);
942 strcat(fn, "/ed.hup");
943 io = creat(fn, 0666);
944 }
945 }
946 }
947 if (io >= 0)
948 putfile();
949 }
950 fchange = 0;
951 status = 0200 | signo;
952 quit(0);
953 }
955 static void
956 onpipe(int signo)
957 {
958 lastsig = signo;
959 error("write or open on pipe failed");
960 }
962 static void
963 error(const char *s)
964 {
965 error2(s, NULL);
966 }
968 static void
969 error2(const char *s, const char *fn)
970 {
971 register int c;
973 wrapp = 0;
974 listf = 0;
975 numbf = 0;
976 errput(s, fn);
977 count = 0;
978 if (lseek(0, 0, SEEK_END) > 0)
979 status = 2;
980 pflag = 0;
981 if (globp)
982 lastc = '\n';
983 globp = 0;
984 peekc = lastc;
985 if(lastc)
986 while ((c = getchr()) != '\n' && c != EOF)
987 ;
988 if (io > 0) {
989 sclose(io);
990 io = -1;
991 }
992 if (wrtemp) {
993 unlink(wrtemp);
994 free(wrtemp);
995 wrtemp = NULL;
996 }
997 longjmp(savej, 1);
998 }
1000 static void
1001 errput(const char *s, const char *fn)
1003 prvmsg = s;
1004 if (fn) {
1005 putchr('?');
1006 puts(fn);
1007 } else
1008 puts("?");
1009 if (prhelp)
1010 puts(s);
1013 static int
1014 getchr(void)
1016 char c;
1017 if (lastc=peekc) {
1018 peekc = 0;
1019 return(lastc);
1021 if (globp) {
1022 if ((lastc = *globp++) != 0)
1023 return(lastc);
1024 globp = 0;
1025 return(EOF);
1027 if (read(0, &c, 1) <= 0)
1028 return(lastc = EOF);
1029 lastc = c;
1030 return(lastc);
1033 static int
1034 gettty(void)
1036 register int c, i;
1037 register char *gf;
1039 i = 0;
1040 gf = globp;
1041 while ((c = getchr()) != '\n') {
1042 if (c==EOF) {
1043 if (gf)
1044 peekc = c;
1045 return(c);
1047 if (c == 0)
1048 continue;
1049 if (i >= LBSIZE)
1050 growlb("line too long");
1051 linebuf[i++] = c;
1053 if (i >= LBSIZE-2)
1054 growlb("line too long");
1055 linebuf[i++] = 0;
1056 if (linebuf[0]=='.' && linebuf[1]==0)
1057 return(EOF);
1058 #if !defined (SUS) && !defined (SU3)
1059 if (linebuf[0]=='\\' && linebuf[1]=='.' && linebuf[2]==0)
1060 linebuf[0]='.', linebuf[1]=0;
1061 #endif
1062 return(0);
1065 static long
1066 getnum(void)
1068 char scount[20];
1069 int i;
1071 i = 0;
1072 while ((peekc=getchr()) >= '0' && peekc <= '9' && i < sizeof scount) {
1073 scount[i++] = peekc;
1074 peekc = 0;
1076 scount[i] = '\0';
1077 return i ? atol(scount) : -1;
1080 static int
1081 getfile(void)
1083 register int c, i, j;
1084 static int nextj;
1086 i = 0;
1087 j = nextj;
1088 do {
1089 if (--ninbuf < 0) {
1090 if (ioeof || (ninbuf=read(io, genbuf, LBSIZE)-1) < 0) {
1091 if (ioeof == 0 && ninbuf < -1) {
1092 puts("input error");
1093 status = 1;
1095 if (i > 0) {
1096 puts("'\\n' appended");
1097 c = '\n';
1098 ioeof = 1;
1099 goto wrc;
1101 return(EOF);
1103 j = 0;
1105 c = genbuf[j++]&0377;
1106 wrc: if (i >= LBSIZE) {
1107 lastc = '\n';
1108 growlb("line too long");
1110 linebuf[i++] = c ? c : '\n';
1111 count++;
1112 } while (c != '\n');
1113 linebuf[--i] = 0;
1114 nextj = j;
1115 if (maxlength && i > maxlength) {
1116 putstr("line too long: lno = ");
1117 putd((dot - zero+1)&MAXCNT);
1118 putchr('\n');
1120 return(0);
1123 static void
1124 putfile(void)
1126 long *a1;
1127 int n;
1128 register char *fp, *lp;
1129 register int nib;
1131 nib = 512;
1132 fp = genbuf;
1133 a1 = addr1;
1134 do {
1135 lp = getline(*a1++, 0);
1136 if (maxlength) {
1137 for (n = 0; lp[n]; n++);
1138 if (n > maxlength) {
1139 putstr("line too long: lno = ");
1140 putd((a1-1 - zero)&MAXCNT);
1141 putchr('\n');
1144 for (;;) {
1145 if (--nib < 0) {
1146 n = fp-genbuf;
1147 if(write(io, genbuf, n) != n)
1148 error("write error");
1149 nib = 511;
1150 fp = genbuf;
1152 count++;
1153 if ((*fp++ = *lp++) == 0) {
1154 fp[-1] = '\n';
1155 break;
1156 } else if (fp[-1] == '\n')
1157 fp[-1] = '\0';
1159 } while (a1 <= addr2);
1160 n = fp-genbuf;
1161 if(write(io, genbuf, n) != n)
1162 error("write error");
1165 static int
1166 append(int (*f)(void), long *a)
1168 register long *a1, *a2, *rdot;
1169 int nline, tl;
1171 nline = 0;
1172 dot = a;
1173 while ((*f)() == 0) {
1174 if ((dol-zero)+1 >= nlall) {
1175 long *ozero = zero;
1176 nlall += 512;
1177 if ((zero = realloc(zero, nlall*sizeof *zero))==NULL) {
1178 lastc = '\n';
1179 zero = ozero;
1180 error("out of memory for append");
1182 dot += zero - ozero;
1183 dol += zero - ozero;
1184 addr1 += zero - ozero;
1185 addr2 += zero - ozero;
1186 if (unddot) {
1187 unddot += zero - ozero;
1188 unddol += zero - ozero;
1190 if (undzero) {
1191 ozero = undzero;
1192 if ((undzero = realloc(undzero,
1193 nlall*sizeof *undzero)) == 0) {
1194 puts("no memory for undo");
1195 free(ozero);
1199 tl = putline();
1200 nline++;
1201 a1 = ++dol;
1202 a2 = a1+1;
1203 rdot = ++dot;
1204 while (a1 > rdot)
1205 *--a2 = *--a1;
1206 *rdot = tl;
1208 return(nline);
1211 static void
1212 callunix(void)
1214 char *line;
1215 void (*savint)(int);
1216 pid_t pid, rpid;
1217 int retcode;
1219 setnoaddr();
1220 line = readcmd();
1221 if ((pid = fork()) == 0) {
1222 sigset(SIGHUP, oldhup);
1223 sigset(SIGQUIT, oldquit);
1224 sigset(SIGPIPE, oldpipe);
1225 execl(SHELL, "sh", "-c", line, NULL);
1226 _exit(0100);
1227 } else if (pid < 0)
1228 error("fork failed - try again");
1229 savint = sigset(SIGINT, SIG_IGN);
1230 while ((rpid = wait(&retcode)) != pid && rpid != -1)
1232 sigset(SIGINT, savint);
1233 if (vflag)
1234 puts("!");
1237 #define cmadd(c) ((i>=cmsize ? \
1238 ((line=realloc(line,cmsize+=128)) == 0 ? \
1239 (error("line too long"),0) : 0, 0) \
1240 : 0), line[i++]=(c))
1242 static char *
1243 readcmd(void)
1245 static char *line, *prev;
1246 static int cmsize, pvsize;
1247 char *pp;
1248 int c, mod = 0, i;
1250 i = 0;
1251 if ((c = getchr()) == '!') {
1252 for (pp = prev; *pp; pp++)
1253 line[i++] = *pp;
1254 mod = 1;
1255 c = getchr();
1257 while (c != '\n' && c != EOF) {
1258 if (c == '\\') {
1259 c = getchr();
1260 if (c != '%')
1261 cmadd('\\');
1262 cmadd(c);
1263 } else if (c == '%') {
1264 for (pp = savedfile; *pp; pp++)
1265 cmadd(*pp);
1266 mod = 1;
1267 } else
1268 cmadd(c);
1269 c = getchr();
1271 cmadd('\0');
1272 if (pvsize < cmsize && (prev = realloc(prev, pvsize=cmsize)) == 0)
1273 error("line too long");
1274 strcpy(prev, line);
1275 if (mod)
1276 nlputs(line);
1277 return line;
1280 static void
1281 quit(int signo)
1283 lastsig = signo;
1284 if (vflag && fchange) {
1285 fchange = 0;
1286 error("warning: expecting `w'");
1288 if (wrtemp)
1289 unlink(wrtemp);
1290 unlink(tfname);
1291 exit(status);
1294 static void
1295 delete(void)
1297 setdot();
1298 newline();
1299 nonzero();
1300 checkpoint();
1301 rdelete(addr1, addr2);
1304 static void
1305 rdelete(long *ad1, long *ad2)
1307 register long *a1, *a2, *a3;
1309 a1 = ad1;
1310 a2 = ad2+1;
1311 a3 = dol;
1312 dol -= a2 - a1;
1313 do {
1314 *a1++ = *a2++;
1315 } while (a2 <= a3);
1316 a1 = ad1;
1317 if (a1 > dol)
1318 a1 = dol;
1319 dot = a1;
1320 fchange = 1;
1323 static void
1324 gdelete(void)
1326 register long *a1, *a2, *a3;
1328 a3 = dol;
1329 for (a1=zero+1; (*a1&01)==0; a1++)
1330 if (a1>=a3)
1331 return;
1332 for (a2=a1+1; a2<=a3;) {
1333 if (*a2&01) {
1334 a2++;
1335 dot = a1;
1336 } else
1337 *a1++ = *a2++;
1339 dol = a1-1;
1340 if (dot>dol)
1341 dot = dol;
1342 fchange = 1;
1345 static char *
1346 getline(long tl, int nulterm)
1348 register char *bp, *lp;
1349 register long nl;
1351 lp = linebuf;
1352 bp = getblock(tl, READ);
1353 nl = nleft;
1354 tl &= ~0377;
1355 while (*lp++ = *bp++) {
1356 if (lp[-1] == '\n' && nulterm) {
1357 lp[-1] = '\0';
1358 break;
1360 if (--nl == 0) {
1361 bp = getblock(tl+=0400, READ);
1362 nl = nleft;
1365 return(linebuf);
1368 static int
1369 putline(void)
1371 register char *bp, *lp;
1372 register long nl;
1373 long tl;
1375 fchange = 1;
1376 lp = linebuf;
1377 tl = tline;
1378 bp = getblock(tl, WRITE);
1379 nl = nleft;
1380 tl &= ~0377;
1381 while (*bp = *lp++) {
1382 if (*bp++ == '\n' && insub) {
1383 *--bp = 0;
1384 linebp = lp;
1385 break;
1387 if (--nl == 0) {
1388 bp = getblock(tl+=0400, WRITE);
1389 nl = nleft;
1392 nl = tline;
1393 tline += (((lp-linebuf)+03)>>1)&(MAXCNT-1);
1394 return(nl);
1397 static char *
1398 getblock(long atl, long iof)
1400 register long bno, off;
1402 bno = (atl>>8)&BLKMSK;
1403 off = (atl<<1)&0774;
1404 if (bno >= BLKMSK) {
1405 lastc = '\n';
1406 error("temp file too big");
1408 nleft = 512 - off;
1409 if (bno==iblock) {
1410 ichanged |= iof;
1411 return(ibuff+off);
1413 if (bno==oblock)
1414 return(obuff+off);
1415 if (iof==READ) {
1416 if (ichanged)
1417 blkio(iblock, ibuff, 1);
1418 ichanged = 0;
1419 iblock = bno;
1420 blkio(bno, ibuff, 0);
1421 return(ibuff+off);
1423 if (oblock>=0)
1424 blkio(oblock, obuff, 1);
1425 oblock = bno;
1426 return(obuff+off);
1429 static void
1430 blkio(long b, char *buf, int wr)
1432 lseek(tfile, b<<9, SEEK_SET);
1433 if ((wr ? write(tfile, buf, 512) : read (tfile, buf, 512)) != 512) {
1434 status = 1;
1435 error("I/O error on temp file");
1439 static void
1440 init(void)
1442 register long *markp;
1444 tline = 2;
1445 for (markp = names; markp < &names[26]; markp++)
1446 *markp = 0;
1447 for (markp = undnames; markp < &undnames[26]; markp++)
1448 *markp = 0;
1449 subnewa = 0;
1450 anymarks = 0;
1451 iblock = -1;
1452 oblock = -1;
1453 ichanged = 0;
1454 tfile = maketf(tfile);
1455 dot = dol = zero;
1456 unddot = NULL;
1459 static void
1460 global(int k, int ia)
1462 register int c;
1463 register long *a1;
1464 static char *globuf;
1465 char mb[MB_LEN_MAX+1];
1466 int spflag = 0;
1468 if (globp)
1469 error("multiple globals not allowed");
1470 setall();
1471 nonzero();
1472 if ((c=GETWC(mb))=='\n')
1473 error("incomplete global expression");
1474 compile(NULL, expbuf, &expbuf[ESIZE], c);
1475 if (!ia) {
1476 globrd(&globuf, EOF);
1477 if (globuf[0] == '\n')
1478 globuf[0] = 'p', globuf[1] = '\n', globuf[2] = '\0';
1479 } else {
1480 newline();
1481 spflag = pflag;
1482 pflag = 0;
1484 checkpoint();
1485 for (a1=zero; a1<=dol; a1++) {
1486 *a1 &= ~01;
1487 if (a1>=addr1 && a1<=addr2 && execute(0, a1, 0)==k)
1488 *a1 |= 01;
1490 /*
1491 * Special case: g/.../d (avoid n^2 algorithm)
1492 */
1493 if (!ia && globuf[0]=='d' && globuf[1]=='\n' && globuf[2]=='\0') {
1494 gdelete();
1495 return;
1497 for (a1=zero; a1<=dol; a1++) {
1498 if (*a1 & 01) {
1499 *a1 &= ~01;
1500 dot = a1;
1501 if (ia) {
1502 puts(getline(*a1, 0));
1503 if ((c = getchr()) == EOF)
1504 error("command expected");
1505 if (c == 'a' || c == 'c' || c == 'i')
1506 error("a, i, or c not allowed in G");
1507 else if (c == '&') {
1508 if ((c = getchr()) != '\n')
1509 error("end of line expected");
1510 if (globuf == 0 || *globuf == 0)
1511 error("no remembered command");
1512 } else if (c == '\n') {
1513 a1 = zero;
1514 continue;
1515 } else
1516 globrd(&globuf, c);
1518 globp = globuf;
1519 commands();
1520 globp = NULL;
1521 a1 = zero;
1524 if (ia)
1525 pflag = spflag;
1528 static void
1529 globrd(char **globuf, register int c)
1531 register int i;
1533 if (*globuf == 0 && (*globuf = malloc(GBSIZE=256)) == 0)
1534 error("global too long");
1535 i = 0;
1536 if (c != EOF)
1537 (*globuf)[i++] = c;
1538 while ((c = getchr()) != '\n') {
1539 if (c==EOF)
1540 error("incomplete global expression");
1541 if (c=='\\') {
1542 c = getchr();
1543 if (c!='\n')
1544 (*globuf)[i++] = '\\';
1546 (*globuf)[i++] = c;
1547 if (i>=GBSIZE-4 && (*globuf=realloc(*globuf,GBSIZE+=256)) == 0)
1548 error("global too long");
1550 (*globuf)[i++] = '\n';
1551 (*globuf)[i++] = 0;
1554 static void
1555 join(void)
1557 register int i, j;
1558 register long *a1;
1560 j = 0;
1561 for (a1=addr1; a1<=addr2; a1++) {
1562 i = getline(*a1, 0) - linebuf;
1563 while (genbuf[j] = linebuf[i++])
1564 if (j++ >= LBSIZE-2)
1565 growlb("line too long");
1567 i = 0;
1568 j = 0;
1569 while (linebuf[i++] = genbuf[j++])
1571 *addr1 = putline();
1572 if (addr1<addr2)
1573 rdelete(addr1+1, addr2);
1574 dot = addr1;
1577 static void
1578 substitute(int inglob)
1580 register long *markp;
1581 register long *a1;
1582 intptr_t nl;
1583 int gsubf;
1585 checkpoint();
1586 gsubf = compsub();
1587 insub = 1;
1588 for (a1 = addr1; a1 <= addr2; a1++) {
1589 long *ozero;
1590 if (execute(0, a1, 1)==0)
1591 continue;
1592 inglob |= dosub(gsubf < 2);
1593 if (gsubf) {
1594 int i = 1;
1596 while (*loc2) {
1597 if (execute(1, NULL, 1)==0)
1598 break;
1599 inglob |= dosub(gsubf == -1 || ++i == gsubf);
1602 subnewa = putline();
1603 *a1 &= ~01;
1604 if (anymarks) {
1605 for (markp = names; markp < &names[26]; markp++)
1606 if (*markp == *a1)
1607 *markp = subnewa;
1609 *a1 = subnewa;
1610 ozero = zero;
1611 nl = append(getsub, a1);
1612 nl += zero-ozero;
1613 a1 += nl;
1614 addr2 += nl;
1616 insub = 0;
1617 if (inglob==0)
1618 error("no match");
1621 static int
1622 compsub(void)
1624 register int seof, c, i;
1625 static char *oldrhs;
1626 static int orhssz;
1627 char mb[MB_LEN_MAX+1];
1629 if ((seof = GETWC(mb)) == '\n' || seof == ' ')
1630 error("illegal or missing delimiter");
1631 nodelim = 0;
1632 compile(NULL, expbuf, &expbuf[ESIZE], seof);
1633 i = 0;
1634 for (;;) {
1635 c = GETWC(mb);
1636 if (c=='\\') {
1637 if (i >= RHSIZE-2)
1638 growrhs("replacement string too long");
1639 rhsbuf[i++] = c;
1640 c = GETWC(mb);
1641 } else if (c=='\n') {
1642 if (globp && *globp) {
1643 if (i >= RHSIZE-2)
1644 growrhs("replacement string too long");
1645 rhsbuf[i++] = '\\';
1647 else if (nodelim)
1648 error("illegal or missing delimiter");
1649 else {
1650 peekc = c;
1651 pflag++;
1652 break;
1654 } else if (c==seof)
1655 break;
1656 for (c = 0; c==0 || mb[c]; c++) {
1657 if (i >= RHSIZE-2)
1658 growrhs("replacement string too long");
1659 rhsbuf[i++] = mb[c];
1662 rhsbuf[i++] = 0;
1663 if (rhsbuf[0] == '%' && rhsbuf[1] == 0) {
1664 if (orhssz == 0)
1665 error("no remembered replacement string");
1666 strcpy(rhsbuf, oldrhs);
1667 } else {
1668 if (orhssz < RHSIZE &&
1669 (oldrhs = realloc(oldrhs, orhssz=RHSIZE)) == 0)
1670 error("replacement string too long");
1671 strcpy(oldrhs, rhsbuf);
1673 if ((peekc = getchr()) == 'g') {
1674 peekc = 0;
1675 newline();
1676 return(-1);
1677 } else if (peekc >= '0' && peekc <= '9') {
1678 c = getnum();
1679 if (c < 1 || c > LBSIZE)
1680 error("invalid count");
1681 newline();
1682 return c;
1684 newline();
1685 return(0);
1688 static int
1689 getsub(void)
1691 register char *p1, *p2;
1693 p1 = linebuf;
1694 if ((p2 = linebp) == 0)
1695 return(EOF);
1696 while (*p1++ = *p2++)
1698 linebp = 0;
1699 return(0);
1702 static int
1703 dosub(int really)
1705 register char *lp, *sp;
1706 register int i, j, k;
1707 int c;
1709 if (!really)
1710 goto copy;
1711 i = 0;
1712 j = 0;
1713 k = 0;
1714 while (&linebuf[i] < loc1)
1715 genbuf[j++] = linebuf[i++];
1716 while (c = rhsbuf[k++]&0377) {
1717 if (c=='&') {
1718 j = place(j, loc1, loc2);
1719 continue;
1720 } else if (c == '\\') {
1721 c = rhsbuf[k++]&0377;
1722 if (c >='1' && c < nbra+'1') {
1723 j = place(j, braslist[c-'1'], braelist[c-'1']);
1724 continue;
1727 if (j >= LBSIZE)
1728 growlb("line too long");
1729 genbuf[j++] = c;
1731 i = loc2 - linebuf;
1732 loc2 = j + linebuf;
1733 #if defined (SUS) || defined (SU3) || defined (S42)
1734 if (loc1 == &linebuf[i]) {
1735 int n;
1736 wchar_t wc;
1737 if (mb_cur_max > 1 && (n = mbtowc(&wc, loc2, mb_cur_max)) > 0)
1738 loc2 += n;
1739 else
1740 loc2++;
1742 #endif /* SUS || SU3 || S42 */
1743 while (genbuf[j++] = linebuf[i++])
1744 if (j >= LBSIZE)
1745 growlb("line too long");
1746 if (really) {
1747 lp = linebuf;
1748 sp = genbuf;
1749 } else {
1750 copy: sp = linebuf;
1751 lp = genbuf;
1753 while (*lp++ = *sp++)
1755 return really;
1758 static int
1759 place(register int j, register const char *l1, register const char *l2)
1762 while (l1 < l2) {
1763 genbuf[j++] = *l1++;
1764 if (j >= LBSIZE)
1765 growlb("line too long");
1767 return(j);
1770 static void
1771 move(int cflag)
1773 register long *adt, *ad1, *ad2;
1775 setdot();
1776 nonzero();
1777 if ((adt = address())==0)
1778 error("illegal move destination");
1779 newline();
1780 checkpoint();
1781 if (cflag) {
1782 long *ozero;
1783 intptr_t delta;
1784 ad1 = dol;
1785 ozero = zero;
1786 append(getcopy, ad1++);
1787 ad2 = dol;
1788 delta = zero - ozero;
1789 ad1 += delta;
1790 adt += delta;
1791 } else {
1792 ad2 = addr2;
1793 for (ad1 = addr1; ad1 <= ad2;)
1794 *ad1++ &= ~01;
1795 ad1 = addr1;
1797 ad2++;
1798 if (adt<ad1) {
1799 dot = adt + (ad2-ad1);
1800 if ((++adt)==ad1)
1801 return;
1802 reverse(adt, ad1);
1803 reverse(ad1, ad2);
1804 reverse(adt, ad2);
1805 } else if (adt >= ad2) {
1806 dot = adt++;
1807 reverse(ad1, ad2);
1808 reverse(ad2, adt);
1809 reverse(ad1, adt);
1810 } else
1811 error("illegal move destination");
1812 fchange = 1;
1815 static void
1816 reverse(register long *a1, register long *a2)
1818 register int t;
1820 for (;;) {
1821 t = *--a2;
1822 if (a2 <= a1)
1823 return;
1824 *a2 = *a1;
1825 *a1++ = t;
1829 static int
1830 getcopy(void)
1832 if (addr1 > addr2)
1833 return(EOF);
1834 getline(*addr1++, 0);
1835 return(0);
1838 static int
1839 execute(int gf, long *addr, int subst)
1841 register char *p1, *p2, c;
1843 for (c=0; c<NBRA; c++) {
1844 braslist[c&0377] = 0;
1845 braelist[c&0377] = 0;
1847 if (gf) {
1848 if (circf)
1849 return(0);
1850 p1 = linebuf;
1851 p2 = genbuf;
1852 while (*p1++ = *p2++)
1854 locs = p1 = loc2;
1855 } else {
1856 if (addr==zero)
1857 return(0);
1858 p1 = getline(*addr, 1);
1859 locs = 0;
1861 needsub = subst;
1862 return step(p1, expbuf);
1865 static void
1866 cmplerr(int c)
1868 const char *msg;
1870 #if !defined (SUS) && !defined (S42) && !defined (SU3)
1871 expbuf[0] = 0;
1872 #endif
1873 switch (c) {
1874 case 11:
1875 msg = "Range endpoint too large";
1876 break;
1877 case 16:
1878 msg = "bad number";
1879 break;
1880 case 25:
1881 msg = "`\\digit' out of range";
1882 break;
1883 case 36:
1884 msg = "illegal or missing delimiter";
1885 break;
1886 case 41:
1887 msg = "no remembered search string";
1888 break;
1889 case 42:
1890 msg = "'\\( \\)' imbalance";
1891 break;
1892 case 43:
1893 msg = "Too many `\\(' s";
1894 break;
1895 case 44:
1896 msg = "more than 2 numbers given";
1897 break;
1898 case 45:
1899 msg = "'\\}' expected";
1900 break;
1901 case 46:
1902 msg = "first number exceeds second";
1903 break;
1904 case 49:
1905 msg = "'[ ]' imbalance";
1906 break;
1907 case 50:
1908 msg = "regular expression overflow";
1909 break;
1910 case 67:
1911 msg = "illegal byte sequence";
1912 break;
1913 default:
1914 msg = "regular expression error";
1915 break;
1917 error(msg);
1920 static void
1921 doprnt(long *bot, long *top)
1923 long *a1;
1925 a1 = bot;
1926 do {
1927 if (numbf ^ Nflag) {
1928 putd(a1-zero);
1929 putchr('\t');
1931 nlputs(getline(*a1++, 0));
1932 } while (a1 <= top);
1933 pflag = 0;
1934 listf = 0;
1935 numbf = 0;
1938 static void
1939 putd(long c)
1941 register int r;
1943 r = c%10;
1944 c /= 10;
1945 if (c)
1946 putd(c);
1947 putchr(r + '0');
1950 static void
1951 nlputs(register const char *sp)
1953 if (listf)
1954 list(sp);
1955 else
1956 puts(sp);
1959 static void
1960 puts(register const char *sp)
1962 while (*sp) {
1963 if (*sp != '\n')
1964 putchr(*sp++ & 0377);
1965 else
1966 sp++, putchr('\0');
1968 putchr('\n');
1971 static void
1972 list(const char *lp)
1974 int col, n;
1975 wchar_t c;
1977 col = numbf ^ Nflag ? 8 : 0;
1978 while (*lp) {
1979 if (mb_cur_max > 1 && *lp&0200)
1980 n = mbtowc(&c, lp, mb_cur_max);
1981 else {
1982 n = 1;
1983 c = *lp&0377;
1985 if (col+1 >= 72) {
1986 col = 0;
1987 putchr('\\');
1988 putchr('\n');
1990 if (n<0 ||
1991 #if defined (SUS) || defined (S42) || defined (SU3)
1992 c == '\\' ||
1993 #endif /* SUS || S42 || SU3 */
1994 !(mb_cur_max>1 ? iswprint(c) : isprint(c))) {
1995 if (n<0)
1996 n = 1;
1997 while (n--)
1998 col += lstchr(*lp++&0377);
1999 } else if (mb_cur_max>1) {
2000 col += wcwidth(c);
2001 while (n--)
2002 putchr(*lp++&0377);
2003 } else {
2004 putchr(*lp++&0377);
2005 col++;
2008 #if defined (SUS) || defined (S42) || defined (SU3)
2009 putchr('$');
2010 #endif
2011 putchr('\n');
2014 static int
2015 lstchr(int c)
2017 int cad = 1, d;
2019 #if !defined (SUS) && !defined (S42) && !defined (SU3)
2020 if (c=='\t') {
2021 c = '>';
2022 goto esc;
2024 if (c=='\b') {
2025 c = '<';
2026 esc:
2027 putchr('-');
2028 putchr('\b');
2029 putchr(c);
2030 } else if (c == '\n') {
2031 putchr('\\');
2032 putchr('0');
2033 putchr('0');
2034 putchr('0');
2035 cad = 4;
2036 #else /* !SUS, !S42, !SU3 */
2037 if (c == '\n')
2038 c = '\0';
2039 if (c == '\\') {
2040 putchr('\\');
2041 putchr('\\');
2042 cad = 2;
2043 } else if (c == '\a') {
2044 putchr('\\');
2045 putchr('a');
2046 cad = 2;
2047 } else if (c == '\b') {
2048 putchr('\\');
2049 putchr('b');
2050 cad = 2;
2051 } else if (c == '\f') {
2052 putchr('\\');
2053 putchr('f');
2054 cad = 2;
2055 } else if (c == '\r') {
2056 putchr('\\');
2057 putchr('r');
2058 cad = 2;
2059 } else if (c == '\t') {
2060 putchr('\\');
2061 putchr('t');
2062 cad = 2;
2063 } else if (c == '\v') {
2064 putchr('\\');
2065 putchr('v');
2066 cad = 2;
2067 #endif /* !SUS, !S42, !SU3 */
2068 } else {
2069 putchr('\\');
2070 putchr(((c&~077)>>6)+'0');
2071 c &= 077;
2072 d = c & 07;
2073 putchr(c > d ? ((c-d)>>3)+'0' : '0');
2074 putchr(d+'0');
2075 cad = 4;
2077 return cad;
2080 static void
2081 putstr(const char *s)
2083 while (*s)
2084 putchr(*s++);
2087 static char line[70];
2088 static char *linp = line;
2090 static void
2091 putchr(int ac)
2093 register char *lp;
2094 register int c;
2096 lp = linp;
2097 c = ac;
2098 *lp++ = c;
2099 if(c == '\n' || lp >= &line[64]) {
2100 linp = line;
2101 write(1, line, lp-line);
2102 return;
2104 linp = lp;
2107 static void
2108 checkpoint(void)
2110 long *a1, *a2;
2112 if (undzero && globp == NULL) {
2113 for (a1 = zero+1, a2 = undzero+1; a1 <= dol; a1++, a2++)
2114 *a2 = *a1;
2115 unddot = &undzero[dot-zero];
2116 unddol = &undzero[dol-zero];
2117 for (a1 = names, a2 = undnames; a1 < &names[26]; a1++, a2++)
2118 *a2 = *a1;
2122 #define swap(a, b) (t = a, a = b, b = t)
2124 static void
2125 undo(void)
2127 long *t;
2129 if (undzero == NULL)
2130 error("no undo information saved");
2131 swap(zero, undzero);
2132 swap(dot, unddot);
2133 swap(dol, unddol);
2134 swap(names, undnames);
2137 static int
2138 maketf(int fd)
2140 char *tmpdir;
2142 if (fd == -1) {
2143 if ((tmpdir = getenv("TMPDIR")) == NULL ||
2144 (fd = creatf(tmpdir)) < 0)
2145 if ((fd = creatf("/var/tmp")) < 0 &&
2146 (fd = creatf("/tmp")) < 0)
2147 error("cannot create temporary file");
2148 } else
2149 ftruncate(fd, 0); /* blkio() will seek to 0 anyway */
2150 return fd;
2153 static int
2154 creatf(const char *tmpdir)
2156 if (strlen(tmpdir) >= sizeof tfname - 9)
2157 return -1;
2158 strcpy(tfname, tmpdir);
2159 strcat(tfname, "/eXXXXXX");
2160 return mkstemp(tfname);
2163 static int
2164 sopen(const char *fn, int rdwr)
2166 int pf[2], fd = -1;
2168 if (fn[0] == '!') {
2169 fn++;
2170 if (pipe(pf) < 0)
2171 error("write or open on pipe failed");
2172 switch (pipid = fork()) {
2173 case 0:
2174 if (rdwr == READ)
2175 dup2(pf[1], 1);
2176 else
2177 dup2(pf[0], 0);
2178 close(pf[0]);
2179 close(pf[1]);
2180 sigset(SIGHUP, oldhup);
2181 sigset(SIGQUIT, oldquit);
2182 sigset(SIGPIPE, oldpipe);
2183 execl(SHELL, "sh", "-c", fn, NULL);
2184 _exit(0100);
2185 default:
2186 close(pf[rdwr == READ ? 1 : 0]);
2187 fd = pf[rdwr == READ ? 0 : 1];
2188 break;
2189 case -1:
2190 error("fork failed - try again");
2192 } else if (rdwr == READ)
2193 fd = open(fn, O_RDONLY);
2194 else if (rdwr == EXIST)
2195 fd = open(fn, O_WRONLY);
2196 else /*if (rdwr == WRITE)*/
2197 fd = creat(fn, 0666);
2198 if (fd >= 0 && rdwr == READ)
2199 readop = 1;
2200 if (fd >= 0)
2201 fstat(fd, &fstbuf);
2202 return fd;
2205 static void
2206 sclose(int fd)
2208 int status;
2210 close(fd);
2211 if (pipid >= 0) {
2212 while (wait(&status) != pipid);
2213 pipid = -1;
2215 readop = 0;
2218 static wint_t
2219 GETWC(char *mb)
2221 int c, n;
2223 n = 1;
2224 mb[0] = c = GETC();
2225 mb[1] = '\0';
2226 if (mb_cur_max > 1 && c&0200 && c != EOF) {
2227 int m;
2228 wchar_t wc;
2230 while ((m = mbtowc(&wc, mb, mb_cur_max)) < 0 && n<mb_cur_max) {
2231 mb[n++] = c = GETC();
2232 mb[n] = '\0';
2233 if (c == '\n' || c == EOF)
2234 break;
2236 if (m != n)
2237 ERROR(67);
2238 return wc;
2239 } else
2240 return c;
2243 static void
2244 growlb(const char *msg)
2246 char *olb = linebuf;
2247 int i;
2249 LBSIZE += 512;
2250 if ((linebuf = realloc(linebuf, LBSIZE)) == NULL ||
2251 (genbuf = realloc(genbuf, LBSIZE)) == NULL)
2252 error(msg);
2253 if (linebuf != olb) {
2254 loc1 += linebuf - olb;
2255 loc2 += linebuf - olb;
2256 for (i = 0; i < NBRA; i++) {
2257 if (braslist[i])
2258 braslist[i] += linebuf - olb;
2259 if (braelist[i])
2260 braelist[i] += linebuf - olb;
2265 static void
2266 growrhs(const char *msg)
2268 RHSIZE += 256;
2269 if ((rhsbuf = realloc(rhsbuf, RHSIZE)) == NULL)
2270 error(msg);
2273 static void
2274 growfn(const char *msg)
2276 FNSIZE += 64;
2277 if ((savedfile = realloc(savedfile, FNSIZE)) == NULL ||
2278 (file = realloc(file, FNSIZE)) == NULL)
2279 error(msg);
2280 if (FNSIZE == 64)
2281 file[0] = savedfile[0] = 0;
2284 #if defined (SUS) || defined (S42) || defined (SU3)
2285 union ptrstore {
2286 void *vp;
2287 char bp[sizeof (void *)];
2288 };
2290 static void *
2291 fetchptr(const char *bp)
2293 union ptrstore u;
2294 int i;
2296 for (i = 0; i < sizeof (void *); i++)
2297 u.bp[i] = bp[i];
2298 return u.vp;
2301 static void
2302 storeptr(void *vp, char *bp)
2304 union ptrstore u;
2305 int i;
2307 u.vp = vp;
2308 for (i = 0; i < sizeof (void *); i++)
2309 bp[i] = u.bp[i];
2312 #define add(c) ((i>=LBSIZE ? (growlb("regular expression overflow"),0) : 0), \
2313 genbuf[i++] = (c))
2315 #define copy(s) { \
2316 int m; \
2317 for (m = 0; m==0 || s[m]; m++) \
2318 add(s[m]); \
2321 static char *
2322 compile(char *unused, char *ep, const char *endbuf, int seof)
2324 INIT
2325 int c, d, i;
2326 regex_t *rp;
2327 char *op;
2328 char mb[MB_LEN_MAX+1];
2330 op = ep;
2331 ep += 2;
2332 if ((rp = fetchptr(ep)) == NULL) {
2333 if ((rp = calloc(1, sizeof *rp)) == NULL)
2334 ERROR(50);
2335 storeptr(rp, ep);
2337 ep += sizeof (void *);
2338 i = 0;
2339 nbra = 0;
2340 do {
2341 if ((c = GETWC(mb)) == seof)
2342 add('\0');
2343 else if (c == '\\') {
2344 copy(mb);
2345 c = GETWC(mb);
2346 if (c == '(')
2347 nbra++;
2348 goto normchar;
2349 } else if (c == '[') {
2350 add(c);
2351 d = EOF;
2352 do {
2353 c = GETWC(mb);
2354 if (c == EOF || c == '\n')
2355 ERROR(49);
2356 copy(mb);
2357 if (d=='[' && (c==':' || c=='.' || c=='=')) {
2358 d = c;
2359 do {
2360 c = GETWC(mb);
2361 if (c == EOF || c == '\n')
2362 ERROR(49);
2363 copy(mb);
2364 } while (c != d || PEEKC() != ']');
2365 c = GETWC(mb);
2366 copy(mb);
2367 c = EOF;
2369 d = c;
2370 } while (c != ']');
2371 } else {
2372 if (c == EOF || c == '\n') {
2373 if (c == '\n')
2374 UNGETC(c);
2375 mb[0] = c = '\0';
2377 if (c == '\0')
2378 nodelim = 1;
2379 normchar: copy(mb);
2381 } while (genbuf[i-1] != '\0');
2382 if (genbuf[0]) {
2383 int reflags = 0;
2385 #ifdef REG_ANGLES
2386 reflags |= REG_ANGLES;
2387 #endif
2388 #if defined (SU3) && defined (REG_AVOIDNULL)
2389 reflags |= REG_AVOIDNULL;
2390 #endif
2391 if (op[0])
2392 regfree(rp);
2393 op[0] = 0;
2394 switch (regcomp(rp, genbuf, reflags)) {
2395 case 0:
2396 break;
2397 case REG_ESUBREG:
2398 ERROR(25);
2399 /*NOTREACHED*/
2400 case REG_EBRACK:
2401 ERROR(49);
2402 /*NOTREACHED*/
2403 case REG_EPAREN:
2404 ERROR(42);
2405 /*NOTREACHED*/
2406 case REG_BADBR:
2407 case REG_EBRACE:
2408 ERROR(45);
2409 /*NOTREACHED*/
2410 case REG_ERANGE:
2411 ERROR(11);
2412 /*NOTREACHED*/
2413 case REG_ESPACE:
2414 ERROR(50);
2415 /*NOTREACHED*/
2416 default:
2417 ERROR(-1);
2419 op[0] = 1;
2420 circf = op[1] = genbuf[0] == '^';
2421 } else if (op[0]) {
2422 circf = op[1];
2423 } else
2424 ERROR(41);
2425 return ep + sizeof (void *);
2428 static int
2429 step(const char *lp, const char *ep)
2431 regex_t *rp;
2432 regmatch_t bralist[NBRA+1];
2433 int eflag = 0;
2434 int res;
2435 int i;
2437 rp = fetchptr(&ep[2]);
2438 if (ep[0] == 0)
2439 return 0;
2440 if (locs)
2441 eflag |= REG_NOTBOL;
2442 if ((res = regexec(rp, lp, needsub? NBRA+1 : 0, bralist, eflag)) == 0 &&
2443 needsub) {
2444 loc1 = (char *)lp + bralist[0].rm_so;
2445 loc2 = (char *)lp + bralist[0].rm_eo;
2446 for (i = 1; i <= NBRA; i++) {
2447 if (bralist[i].rm_so != -1) {
2448 braslist[i-1] = (char *)lp + bralist[i].rm_so;
2449 braelist[i-1] = (char *)lp + bralist[i].rm_eo;
2450 } else
2451 braslist[i-1] = braelist[i-1] = NULL;
2454 return res == 0;
2456 #endif /* SUS || S42 || SU3 */
2458 static void
2459 help(void)
2461 const char *desc[] = {
2462 "(.)a append up to .",
2463 "(.)b[n] browse n lines",
2464 "(.,.)c change up to .",
2465 "(.,.)d delete lines",
2466 "e [file] edit file",
2467 "E [file] force edit",
2468 "f [file] print or set file",
2469 "(1,$)g/RE/cmd global cmd",
2470 "(1,$)G/RE/ interactive global",
2471 "h print last error",
2472 "H toggle error messages",
2473 "help print this screen",
2474 "(.)i insert up to .",
2475 "(.,.+1)j join lines",
2476 "(.)kx mark line with x",
2477 "(.,.)l list lines",
2478 "(.,.)ma move lines to a",
2479 "(.,.)n number lines",
2480 "N revert n and p",
2481 "(.)o[n] show n lines of context",
2482 "(.,.)p print lines",
2483 "P toggle prompt",
2484 "q quit",
2485 "Q force quit",
2486 "($)r read file",
2487 "(.,.)s/RE/repl/ search and replace",
2488 "(.,.)s/RE/rp/g replace all occurrences",
2489 "(.,.)s/RE/rp/n replace n-th occurrence",
2490 "(.,.)ta transfer lines to a",
2491 "u undo last change",
2492 "(1,$)v/RE/cmd reverse global",
2493 "(1,$)V/RE/ reverse i/a global",
2494 "(1,$)w [file] write file",
2495 "(1,$)W [file] append to file",
2496 "z write buffer and quit",
2497 "($)= print line number",
2498 "!command execute shell command",
2499 "(.+1)<newline> print one line",
2500 "/RE find RE forwards",
2501 "?RE find RE backwards",
2502 "1 first line",
2503 ". current line",
2504 "$ last line",
2505 ", 1,$",
2506 "; .,$",
2507 NULL
2508 };
2509 char line[100];
2510 int c, half, i, k;
2512 half = (sizeof desc / sizeof *desc) / 2;
2513 for (i = 0; i < half && desc[i]; i++) {
2514 c = 0;
2515 for (k = 0; desc[i][k]; k++)
2516 line[c++] = desc[i][k];
2517 if (desc[i+half]) {
2518 while (c < 40)
2519 line[c++] = ' ';
2520 for (k = 0; desc[i+half][k]; k++)
2521 line[c++] = desc[i+half][k];
2523 line[c] = 0;
2524 puts(line);