heirloom-ed

view ed.c @ 2:a09d0630f05b

removed unnecessary command line options
author markus schnalke <meillo@marmaro.de>
date Tue, 12 Aug 2014 18:08:24 +0200
parents 1493bea5ac22
children ac52712b2b5e
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 <termios.h>
72 #include <setjmp.h>
73 #include <libgen.h>
74 #include <inttypes.h>
75 #include <locale.h>
76 #include <wchar.h>
77 #include <ctype.h>
78 #include <wctype.h>
79 #include <limits.h>
80 #include <termios.h>
81 static int FNSIZE;
82 static int LBSIZE;
83 static int RHSIZE;
84 #define ESIZE 2048
85 static int GBSIZE;
86 #undef EOF
87 #define EOF -1
88 #define puts(s) xxputs(s)
89 #define getline(t, n) xxgetline(t, n)
91 #if (LONG_MAX > 017777777777L)
92 #define MAXCNT 0777777777777777777777L /* 2^63-1 */
93 #else
94 #define MAXCNT 017777777777L /* 2^31-1 */
95 #endif
96 #define BLKMSK (MAXCNT>>8) /* was 0377 */
98 #define READ 0
99 #define WRITE 1
100 #define EXIST 2
102 struct tabulator {
103 struct tabulator *t_nxt; /* next list element */
104 const char *t_str; /* tabulator string */
105 int t_tab; /* tab stop position */
106 int t_rep; /* repetitive tab count */
107 };
109 static int peekc;
110 static int lastc;
111 static char *savedfile;
112 static char *file;
113 static struct stat fstbuf;
114 static char *linebuf;
115 static char *rhsbuf;
116 static char expbuf[ESIZE + 4];
117 static long *zero;
118 static long *undzero;
119 static long *dot;
120 static long *unddot;
121 static long *dol;
122 static long *unddol;
123 static long *addr1;
124 static long *addr2;
125 static char *genbuf;
126 static long count;
127 static char *linebp;
128 static int ninbuf;
129 static int io;
130 static int ioeof;
131 static int pflag;
132 static char *wrtemp;
133 static uid_t myuid;
134 static void (*oldhup)(int);
135 static void (*oldquit)(int);
136 static void (*oldpipe)(int);
137 static int vflag = 1;
138 static int listf;
139 static int numbf;
140 static char *globp;
141 static int tfile = -1;
142 static long tline;
143 static char tfname[64];
144 static char ibuff[512];
145 static int iblock = -1;
146 static char obuff[512];
147 static int oblock = -1;
148 static int ichanged;
149 static int nleft;
150 static long *names;
151 static long *undnames;
152 static int anymarks;
153 static int subnewa;
154 static int fchange;
155 static int wrapp;
156 static unsigned nlall = 128;
157 static const char *progname;
158 static const char *prompt = "*";
159 static int Pflag;
160 static int prhelp;
161 static const char *prvmsg;
162 static int lastsig;
163 static int pipid = -1;
164 static int readop;
165 static int status;
166 static int mb_cur_max;
167 static int needsub;
168 static int insub;
169 static struct tabulator *tabstops;
170 static int maxlength;
171 static int rspec;
172 static int Nflag;
173 static int bcount = 22;
174 static int ocount = 11;
176 static jmp_buf savej;
178 static void usage(char);
179 static void commands(void);
180 static long *address(void);
181 static void setdot(void);
182 static void setall(void);
183 static void setnoaddr(void);
184 static void nonzero(void);
185 static void newline(void);
186 static void filename(int);
187 static void exfile(void);
188 static void onintr(int);
189 static void onhup(int);
190 static void onpipe(int);
191 static void error(const char *);
192 static void error2(const char *, const char *);
193 static void errput(const char *, const char *);
194 static int getchr(void);
195 static int gettty(void);
196 static long getnum(void);
197 static int getfile(void);
198 static void putfile(void);
199 static int append(int (*)(void), long *);
200 static void callunix(void);
201 static char *readcmd(void);
202 static void quit(int);
203 static void delete(void);
204 static void rdelete(long *, long *);
205 static void gdelete(void);
206 static char *getline(long, int);
207 static int putline(void);
208 static char *getblock(long, long);
209 static void blkio(long, char *, int);
210 static void init(void);
211 static void global(int, int);
212 static void globrd(char **, int);
213 static void join(void);
214 static void substitute(int);
215 static int compsub(void);
216 static int getsub(void);
217 static int dosub(int);
218 static int place(int, const char *, const char *);
219 static void move(int);
220 static void reverse(long *, long *);
221 static int getcopy(void);
222 static int execute(int, long *, int);
223 static void cmplerr(int);
224 static void doprnt(long *, long *);
225 static void putd(long);
226 static void puts(const char *);
227 static void nlputs(const char *);
228 static void list(const char *);
229 static int lstchr(int);
230 static void putstr(const char *);
231 static void putchr(int);
232 static void checkpoint(void);
233 static void undo(void);
234 static int maketf(int);
235 static int creatf(const char *);
236 static int sopen(const char *, int);
237 static void sclose(int);
238 static void fspec(const char *);
239 static const char *ftok(const char **);
240 static struct tabulator *tabstring(const char *);
241 static void freetabs(void);
242 static void expand(const char *);
243 static void growlb(const char *);
244 static void growrhs(const char *);
245 static void growfn(const char *);
246 static void help(void);
248 #define INIT
249 #define GETC() getchr()
250 #define UNGETC(c) (peekc = c)
251 #define PEEKC() (peekc = getchr())
252 #define RETURN(c) return c
253 #define ERROR(c) cmplerr(c)
254 static wint_t GETWC(char *);
256 #if defined (SUS) || defined (S42) || defined (SU3)
258 #include <regex.h>
260 #define NBRA 9
262 static char *braslist[NBRA];
263 static char *braelist[NBRA];
264 static char *loc1, *loc2, *locs;
265 static int nbra;
266 static int circf;
267 static int nodelim;
269 static char *compile(char *, char *, const char *, int);
270 static int step(const char *, const char *);
272 #else /* !SUS, !S42, !SU3 */
274 #include <regexp.h>
276 #endif /* !SUS, !S42, !SU3 */
278 int
279 main(int argc, char **argv)
280 {
281 register int i;
282 void (*oldintr)(int);
284 progname = basename(argv[0]);
285 #if defined (SUS) || defined (S42) || defined (SU3)
286 setlocale(LC_COLLATE, "");
287 #endif
288 setlocale(LC_CTYPE, "");
289 mb_cur_max = MB_CUR_MAX;
290 myuid = getuid();
291 oldquit = sigset(SIGQUIT, SIG_IGN);
292 oldhup = sigset(SIGHUP, SIG_IGN);
293 oldintr = sigset(SIGINT, SIG_IGN);
294 if (sigset(SIGTERM, SIG_IGN) != SIG_IGN)
295 sigset(SIGTERM, quit);
296 oldpipe = sigset(SIGPIPE, onpipe);
297 argv++;
298 if (argc > 1 && **argv=='-') {
299 if ((*argv)[1]=='\0') {
300 vflag = 0;
301 } else {
302 usage((*argv)[1]);
303 }
304 argv++;
305 argc--;
306 }
308 growfn("no space");
309 if (argc>1) {
310 i = -1;
311 do
312 if (++i >= FNSIZE)
313 growfn("maximum of characters in "
314 "file names reached");
315 while (savedfile[i] = (*argv)[i]);
316 globp = "e";
317 }
318 names = malloc(26*sizeof *names);
319 undnames = malloc(26*sizeof *undnames);
320 zero = malloc(nlall*sizeof *zero);
321 if ((undzero = malloc(nlall*sizeof *undzero)) == NULL)
322 puts("no memory for undo");
323 growlb("no space");
324 growrhs("no space");
325 init();
326 if (oldintr != SIG_IGN)
327 sigset(SIGINT, onintr);
328 if (oldhup != SIG_IGN)
329 sigset(SIGHUP, onhup);
330 setjmp(savej);
331 if (lastsig) {
332 sigrelse(lastsig);
333 lastsig = 0;
334 }
335 commands();
336 quit(0);
337 /*NOTREACHED*/
338 return 0;
339 }
341 static void
342 usage(char c)
343 {
344 if (c) {
345 write(2, progname, strlen(progname));
346 write(2, ": illegal option -- ", 20);
347 write(2, &c, 1);
348 write(2, "\n", 1);
349 }
350 write(2, "usage: ", 7);
351 write(2, progname, strlen(progname));
352 write(2, " [-] [file]\n", 12);
353 exit(2);
354 }
356 static void
357 commands(void)
358 {
359 register long *a1;
360 register int c;
361 int n;
363 for (;;) {
364 if (pflag) {
365 pflag = 0;
366 addr1 = addr2 = dot;
367 goto print;
368 }
369 if (Pflag && globp == NULL)
370 write(1, prompt, strlen(prompt));
371 addr1 = 0;
372 addr2 = 0;
373 switch (c = getchr()) {
374 case ',':
375 case ';':
376 addr2 = c == ',' ? zero+1 : dot;
377 if (((peekc = getchr()) < '0' || peekc > '9') &&
378 peekc != ' ' && peekc != '\t' &&
379 peekc != '+' && peekc != '-' &&
380 peekc != '^' && peekc != '?' &&
381 peekc != '/' && peekc != '$' &&
382 peekc != '.' && peekc != '\'') {
383 addr1 = addr2;
384 a1 = dol;
385 goto loop;
386 }
387 break;
388 default:
389 peekc = c;
390 }
391 do {
392 addr1 = addr2;
393 if ((a1 = address())==0) {
394 c = getchr();
395 break;
396 }
397 loop: addr2 = a1;
398 if ((c=getchr()) == ';') {
399 c = ',';
400 dot = a1;
401 }
402 } while (c==',');
403 if (addr1==0)
404 addr1 = addr2;
405 switch(c) {
407 case 'a':
408 setdot();
409 newline();
410 checkpoint();
411 append(gettty, addr2);
412 continue;
414 case 'c':
415 #if defined (SU3)
416 if (addr1 == zero && addr1+1 <= dol) {
417 if (addr1 == addr2)
418 addr2++;
419 addr1++;
420 }
421 #endif /* SU3 */
422 delete();
423 append(gettty, addr1-1);
424 #if defined (SUS) || defined (SU3)
425 if (dot == addr1-1 && addr1 <= dol)
426 dot = addr1;
427 #endif /* SUS || SU3 */
428 continue;
430 case 'd':
431 delete();
432 continue;
434 case 'E':
435 fchange = 0;
436 c = 'e';
437 case 'e':
438 setnoaddr();
439 if (vflag && fchange) {
440 fchange = 0;
441 error("warning: expecting `w'");
442 }
443 filename(c);
444 init();
445 addr2 = zero;
446 goto caseread;
448 case 'f':
449 setnoaddr();
450 filename(c);
451 puts(savedfile);
452 continue;
454 case 'g':
455 global(1, 0);
456 continue;
458 case 'G':
459 global(1, 1);
460 continue;
462 case 'H':
463 prhelp = !prhelp;
464 /*FALLTHRU*/
466 case 'h':
467 if ((peekc = getchr()) == 'e') {
468 peekc = 0;
469 if (getchr() != 'l' || getchr() != 'p' ||
470 getchr() != '\n')
471 error("illegal suffix");
472 setnoaddr();
473 help();
474 continue;
475 }
476 newline();
477 setnoaddr();
478 if (prvmsg)
479 puts(prvmsg);
480 continue;
482 case 'i':
483 setdot();
484 #if defined (SU3)
485 if (addr1 == zero) {
486 if (addr1 == addr2)
487 addr2++;
488 addr1++;
489 if (dol != zero)
490 nonzero();
491 } else
492 #endif /* SU3 */
493 nonzero();
494 newline();
495 checkpoint();
496 append(gettty, addr2-1);
497 if (dot == addr2-1)
498 dot++;
499 continue;
502 case 'j':
503 if (addr2==0) {
504 addr1 = dot;
505 addr2 = dot+1;
506 }
507 setdot();
508 newline();
509 nonzero();
510 checkpoint();
511 if (addr1 != addr2)
512 join();
513 continue;
515 case 'k':
516 if ((c = getchr()) < 'a' || c > 'z')
517 error("mark not lower case");
518 newline();
519 setdot();
520 nonzero();
521 names[c-'a'] = *addr2 & ~01;
522 anymarks |= 01;
523 continue;
525 case 'm':
526 move(0);
527 continue;
529 case '\n':
530 if (addr2==0)
531 addr2 = dot+1;
532 addr1 = addr2;
533 goto print;
535 case 'n':
536 numbf = 1;
537 newline();
538 goto print;
540 case 'N':
541 newline();
542 setnoaddr();
543 Nflag = !Nflag;
544 continue;
546 case 'b':
547 case 'o':
548 n = getnum();
549 newline();
550 setdot();
551 nonzero();
552 if (n >= 0) {
553 if (c == 'b')
554 bcount = n;
555 else
556 ocount = n;
557 }
558 if (c == 'b') {
559 a1 = addr2+bcount > dol ? dol : addr2 + bcount;
560 doprnt(addr1, a1);
561 dot = a1;
562 } else {
563 a1 = addr2+ocount > dol ? dol : addr2 + ocount;
564 doprnt(addr2-ocount<zero+1?zero+1:addr2-ocount, a1);
565 dot = addr2;
566 }
567 continue;
569 case 'l':
570 listf++;
571 case 'p':
572 newline();
573 print:
574 setdot();
575 nonzero();
576 doprnt(addr1, addr2);
577 dot = addr2;
578 continue;
580 case 'P':
581 setnoaddr();
582 newline();
583 Pflag = !Pflag;
584 continue;
586 case 'Q':
587 fchange = 0;
588 case 'q':
589 setnoaddr();
590 newline();
591 quit(0);
593 case 'r':
594 filename(c);
595 caseread:
596 if ((io = sopen(file, READ)) < 0) {
597 lastc = '\n';
598 error2("cannot open input file", file);
599 }
600 ioeof = 0;
601 setall();
602 ninbuf = 0;
603 if (c == 'r')
604 checkpoint();
605 n = zero != dol;
606 rspec = (c == 'e' || !n) && file[0] != '!';
607 append(getfile, addr2);
608 rspec = 0;
609 exfile();
610 fchange = n;
611 continue;
613 case 's':
614 setdot();
615 nonzero();
616 substitute(globp!=0);
617 continue;
619 case 't':
620 move(1);
621 continue;
623 case 'u':
624 setdot();
625 newline();
626 if (unddot == NULL)
627 error("nothing to undo");
628 undo();
629 continue;
631 case 'v':
632 global(0, 0);
633 continue;
635 case 'V':
636 global(0, 1);
637 continue;
639 case 'W':
640 wrapp++;
641 case 'w':
642 write:
643 setall();
644 if (zero != dol)
645 nonzero();
646 filename(c);
647 if(!wrapp ||
648 ((io = open(file,O_WRONLY|O_APPEND)) == -1) ||
649 ((lseek(io, 0, SEEK_END)) == -1)) {
650 struct stat st;
651 if (lstat(file, &st) == 0 &&
652 (st.st_mode&S_IFMT) == S_IFREG &&
653 st.st_nlink == 1 &&
654 (myuid==0 || myuid==st.st_uid)) {
655 char *cp, *tp;
656 int nio;
657 if ((io = sopen(file, EXIST)) < 0)
658 error("cannot create output file");
659 if ((wrtemp = malloc(strlen(file)+8)) == NULL)
660 error("out of memory");
661 for (cp = file, tp = wrtemp; *cp; cp++)
662 *tp++ = *cp;
663 while (tp > wrtemp && tp[-1] != '/')
664 tp--;
665 for (cp = "\7XXXXXX"; *cp; cp++)
666 *tp++ = *cp;
667 *tp = '\0';
668 if ((nio = mkstemp(wrtemp)) < 0) {
669 free(wrtemp);
670 wrtemp = NULL;
671 ftruncate(io, 0);
672 } else {
673 close(io);
674 io = nio;
675 }
676 } else {
677 if ((io = sopen(file, WRITE)) < 0)
678 error("cannot create output file");
679 }
680 }
681 if (zero != dol) {
682 ioeof = 0;
683 wrapp = 0;
684 putfile();
685 }
686 exfile();
687 if (addr1==zero+1 && addr2==dol || addr1==addr2 && dol==zero)
688 fchange = 0;
689 if (c == 'z')
690 quit(0);
691 continue;
693 case 'z':
694 if ((peekc=getchr()) != '\n')
695 error("illegal suffix");
696 setnoaddr();
697 goto write;
699 case '=':
700 setall();
701 newline();
702 putd((addr2-zero)&MAXCNT);
703 putchr('\n');
704 continue;
706 case '!':
707 callunix();
708 continue;
710 case EOF:
711 return;
713 }
714 error("unknown command");
715 }
716 }
718 static long *
719 address(void)
720 {
721 register long *a1;
722 register int minus, c;
723 int n, relerr;
725 minus = 0;
726 a1 = 0;
727 for (;;) {
728 c = getchr();
729 if ('0'<=c && c<='9') {
730 n = 0;
731 do {
732 n *= 10;
733 n += c - '0';
734 } while ((c = getchr())>='0' && c<='9');
735 peekc = c;
736 if (a1==0)
737 a1 = zero;
738 if (minus<0)
739 n = -n;
740 a1 += n;
741 minus = 0;
742 continue;
743 }
744 relerr = 0;
745 if (a1 || minus)
746 relerr++;
747 switch(c) {
748 case ' ':
749 case '\t':
750 continue;
752 case '+':
753 minus++;
754 if (a1==0)
755 a1 = dot;
756 continue;
758 case '-':
759 case '^':
760 minus--;
761 if (a1==0)
762 a1 = dot;
763 continue;
765 case '?':
766 case '/':
767 compile(NULL, expbuf, &expbuf[ESIZE], c);
768 a1 = dot;
769 for (;;) {
770 if (c=='/') {
771 a1++;
772 if (a1 > dol)
773 a1 = zero;
774 } else {
775 a1--;
776 if (a1 < zero)
777 a1 = dol;
778 }
779 if (execute(0, a1, 0))
780 break;
781 if (a1==dot)
782 error("search string not found");
783 }
784 break;
786 case '$':
787 a1 = dol;
788 break;
790 case '.':
791 a1 = dot;
792 break;
794 case '\'':
795 if ((c = getchr()) < 'a' || c > 'z')
796 error("mark not lower case");
797 for (a1=zero; a1<=dol; a1++)
798 if (names[c-'a'] == (*a1 & ~01))
799 break;
800 break;
802 default:
803 peekc = c;
804 if (a1==0)
805 return(0);
806 a1 += minus;
807 if (a1<zero || a1>dol)
808 error("line out of range");
809 return(a1);
810 }
811 if (relerr)
812 error("bad number");
813 }
814 }
816 static void
817 setdot(void)
818 {
819 if (addr2 == 0)
820 addr1 = addr2 = dot;
821 if (addr1 > addr2)
822 error("bad range");
823 }
825 static void
826 setall(void)
827 {
828 if (addr2==0) {
829 addr1 = zero+1;
830 addr2 = dol;
831 if (dol==zero)
832 addr1 = zero;
833 }
834 setdot();
835 }
837 static void
838 setnoaddr(void)
839 {
840 if (addr2)
841 error("Illegal address count");
842 }
844 static void
845 nonzero(void)
846 {
847 if (addr1<=zero || addr2>dol)
848 error("line out of range");
849 }
851 static void
852 newline(void)
853 {
854 register int c;
856 if ((c = getchr()) == '\n')
857 return;
858 if (c=='p' || c=='l' || c=='n') {
859 pflag++;
860 if (c=='l')
861 listf++;
862 else if (c=='n')
863 numbf = 1;
864 if (getchr() == '\n')
865 return;
866 }
867 error("illegal suffix");
868 }
870 static void
871 filename(int comm)
872 {
873 register char *p1, *p2;
874 register int c, i;
876 count = 0;
877 c = getchr();
878 if (c=='\n' || c==EOF) {
879 p1 = savedfile;
880 if (*p1==0 && comm!='f')
881 error("illegal or missing filename");
882 p2 = file;
883 while (*p2++ = *p1++)
884 ;
885 return;
886 }
887 if (c!=' ')
888 error("no space after command");
889 while ((c = getchr()) == ' ')
890 ;
891 if (c=='\n')
892 error("illegal or missing filename");
893 i = 0;
894 do {
895 if (i >= FNSIZE)
896 growfn("maximum of characters in file names reached");
897 file[i++] = c;
898 if (c==' ' && file[0] != '!' || c==EOF)
899 error("illegal or missing filename");
900 } while ((c = getchr()) != '\n');
901 file[i++] = 0;
902 if ((savedfile[0]==0 || comm=='e' || comm=='f') && file[0] != '!') {
903 p1 = savedfile;
904 p2 = file;
905 while (*p1++ = *p2++)
906 ;
907 }
908 }
910 static void
911 exfile(void)
912 {
913 sclose(io);
914 io = -1;
915 if (wrtemp) {
916 extern int rename(const char *, const char *);
917 if (rename(wrtemp, file) < 0)
918 error("cannot create output file");
919 if (myuid == 0)
920 chown(file, fstbuf.st_uid, fstbuf.st_gid);
921 chmod(file, fstbuf.st_mode & 07777);
922 free(wrtemp);
923 wrtemp = NULL;
924 }
925 if (vflag) {
926 putd(count);
927 putchr('\n');
928 }
929 }
931 static void
932 onintr(int signo)
933 {
934 lastsig = signo;
935 putchr('\n');
936 lastc = '\n';
937 if (readop) {
938 puts("\007read may be incomplete - beware!\007");
939 fchange = 0;
940 }
941 error("interrupt");
942 }
944 static void
945 onhup(int signo)
946 {
947 if (dol > zero && fchange) {
948 addr1 = zero+1;
949 addr2 = dol;
950 io = creat("ed.hup", 0666);
951 if (io < 0) {
952 char *home = getenv("HOME");
953 if (home) {
954 char *fn = malloc(strlen(home) + 10);
955 if (fn) {
956 strcpy(fn, home);
957 strcat(fn, "/ed.hup");
958 io = creat(fn, 0666);
959 }
960 }
961 }
962 if (io >= 0)
963 putfile();
964 }
965 fchange = 0;
966 status = 0200 | signo;
967 quit(0);
968 }
970 static void
971 onpipe(int signo)
972 {
973 lastsig = signo;
974 error("write or open on pipe failed");
975 }
977 static void
978 error(const char *s)
979 {
980 error2(s, NULL);
981 }
983 static void
984 error2(const char *s, const char *fn)
985 {
986 register int c;
988 wrapp = 0;
989 listf = 0;
990 numbf = 0;
991 errput(s, fn);
992 count = 0;
993 if (lseek(0, 0, SEEK_END) > 0)
994 status = 2;
995 pflag = 0;
996 if (globp)
997 lastc = '\n';
998 globp = 0;
999 peekc = lastc;
1000 if(lastc)
1001 while ((c = getchr()) != '\n' && c != EOF)
1003 if (io > 0) {
1004 sclose(io);
1005 io = -1;
1007 if (wrtemp) {
1008 unlink(wrtemp);
1009 free(wrtemp);
1010 wrtemp = NULL;
1012 longjmp(savej, 1);
1015 static void
1016 errput(const char *s, const char *fn)
1018 prvmsg = s;
1019 if (fn) {
1020 putchr('?');
1021 puts(fn);
1022 } else
1023 puts("?");
1024 if (prhelp)
1025 puts(s);
1028 static int
1029 getchr(void)
1031 char c;
1032 if (lastc=peekc) {
1033 peekc = 0;
1034 return(lastc);
1036 if (globp) {
1037 if ((lastc = *globp++) != 0)
1038 return(lastc);
1039 globp = 0;
1040 return(EOF);
1042 if (read(0, &c, 1) <= 0)
1043 return(lastc = EOF);
1044 lastc = c;
1045 return(lastc);
1048 static int
1049 gettty(void)
1051 register int c, i;
1052 register char *gf;
1054 i = 0;
1055 gf = globp;
1056 while ((c = getchr()) != '\n') {
1057 if (c==EOF) {
1058 if (gf)
1059 peekc = c;
1060 return(c);
1062 if (c == 0)
1063 continue;
1064 if (i >= LBSIZE)
1065 growlb("line too long");
1066 linebuf[i++] = c;
1068 if (i >= LBSIZE-2)
1069 growlb("line too long");
1070 linebuf[i++] = 0;
1071 if (linebuf[0]=='.' && linebuf[1]==0)
1072 return(EOF);
1073 #if !defined (SUS) && !defined (SU3)
1074 if (linebuf[0]=='\\' && linebuf[1]=='.' && linebuf[2]==0)
1075 linebuf[0]='.', linebuf[1]=0;
1076 #endif
1077 return(0);
1080 static long
1081 getnum(void)
1083 char scount[20];
1084 int i;
1086 i = 0;
1087 while ((peekc=getchr()) >= '0' && peekc <= '9' && i < sizeof scount) {
1088 scount[i++] = peekc;
1089 peekc = 0;
1091 scount[i] = '\0';
1092 return i ? atol(scount) : -1;
1095 static int
1096 getfile(void)
1098 register int c, i, j;
1099 static int nextj;
1101 i = 0;
1102 j = nextj;
1103 do {
1104 if (--ninbuf < 0) {
1105 if (ioeof || (ninbuf=read(io, genbuf, LBSIZE)-1) < 0) {
1106 if (ioeof == 0 && ninbuf < -1) {
1107 puts("input error");
1108 status = 1;
1110 if (i > 0) {
1111 puts("'\\n' appended");
1112 c = '\n';
1113 ioeof = 1;
1114 goto wrc;
1116 return(EOF);
1118 j = 0;
1120 c = genbuf[j++]&0377;
1121 wrc: if (i >= LBSIZE) {
1122 lastc = '\n';
1123 growlb("line too long");
1125 linebuf[i++] = c ? c : '\n';
1126 count++;
1127 } while (c != '\n');
1128 linebuf[--i] = 0;
1129 nextj = j;
1130 if (rspec && dot == zero)
1131 fspec(linebuf);
1132 if (maxlength && i > maxlength) {
1133 putstr("line too long: lno = ");
1134 putd((dot - zero+1)&MAXCNT);
1135 putchr('\n');
1137 return(0);
1140 static void
1141 putfile(void)
1143 long *a1;
1144 int n;
1145 register char *fp, *lp;
1146 register int nib;
1148 nib = 512;
1149 fp = genbuf;
1150 a1 = addr1;
1151 do {
1152 lp = getline(*a1++, 0);
1153 if (maxlength) {
1154 for (n = 0; lp[n]; n++);
1155 if (n > maxlength) {
1156 putstr("line too long: lno = ");
1157 putd((a1-1 - zero)&MAXCNT);
1158 putchr('\n');
1161 for (;;) {
1162 if (--nib < 0) {
1163 n = fp-genbuf;
1164 if(write(io, genbuf, n) != n)
1165 error("write error");
1166 nib = 511;
1167 fp = genbuf;
1169 count++;
1170 if ((*fp++ = *lp++) == 0) {
1171 fp[-1] = '\n';
1172 break;
1173 } else if (fp[-1] == '\n')
1174 fp[-1] = '\0';
1176 } while (a1 <= addr2);
1177 n = fp-genbuf;
1178 if(write(io, genbuf, n) != n)
1179 error("write error");
1182 static int
1183 append(int (*f)(void), long *a)
1185 register long *a1, *a2, *rdot;
1186 int nline, tl;
1188 nline = 0;
1189 dot = a;
1190 while ((*f)() == 0) {
1191 if ((dol-zero)+1 >= nlall) {
1192 long *ozero = zero;
1193 nlall += 512;
1194 if ((zero = realloc(zero, nlall*sizeof *zero))==NULL) {
1195 lastc = '\n';
1196 zero = ozero;
1197 error("out of memory for append");
1199 dot += zero - ozero;
1200 dol += zero - ozero;
1201 addr1 += zero - ozero;
1202 addr2 += zero - ozero;
1203 if (unddot) {
1204 unddot += zero - ozero;
1205 unddol += zero - ozero;
1207 if (undzero) {
1208 ozero = undzero;
1209 if ((undzero = realloc(undzero,
1210 nlall*sizeof *undzero)) == 0) {
1211 puts("no memory for undo");
1212 free(ozero);
1216 tl = putline();
1217 nline++;
1218 a1 = ++dol;
1219 a2 = a1+1;
1220 rdot = ++dot;
1221 while (a1 > rdot)
1222 *--a2 = *--a1;
1223 *rdot = tl;
1225 return(nline);
1228 static void
1229 callunix(void)
1231 char *line;
1232 void (*savint)(int);
1233 pid_t pid, rpid;
1234 int retcode;
1236 setnoaddr();
1237 line = readcmd();
1238 if ((pid = fork()) == 0) {
1239 sigset(SIGHUP, oldhup);
1240 sigset(SIGQUIT, oldquit);
1241 sigset(SIGPIPE, oldpipe);
1242 execl(SHELL, "sh", "-c", line, NULL);
1243 _exit(0100);
1244 } else if (pid < 0)
1245 error("fork failed - try again");
1246 savint = sigset(SIGINT, SIG_IGN);
1247 while ((rpid = wait(&retcode)) != pid && rpid != -1)
1249 sigset(SIGINT, savint);
1250 if (vflag)
1251 puts("!");
1254 #define cmadd(c) ((i>=cmsize ? \
1255 ((line=realloc(line,cmsize+=128)) == 0 ? \
1256 (error("line too long"),0) : 0, 0) \
1257 : 0), line[i++]=(c))
1259 static char *
1260 readcmd(void)
1262 static char *line, *prev;
1263 static int cmsize, pvsize;
1264 char *pp;
1265 int c, mod = 0, i;
1267 i = 0;
1268 if ((c = getchr()) == '!') {
1269 for (pp = prev; *pp; pp++)
1270 line[i++] = *pp;
1271 mod = 1;
1272 c = getchr();
1274 while (c != '\n' && c != EOF) {
1275 if (c == '\\') {
1276 c = getchr();
1277 if (c != '%')
1278 cmadd('\\');
1279 cmadd(c);
1280 } else if (c == '%') {
1281 for (pp = savedfile; *pp; pp++)
1282 cmadd(*pp);
1283 mod = 1;
1284 } else
1285 cmadd(c);
1286 c = getchr();
1288 cmadd('\0');
1289 if (pvsize < cmsize && (prev = realloc(prev, pvsize=cmsize)) == 0)
1290 error("line too long");
1291 strcpy(prev, line);
1292 if (mod)
1293 nlputs(line);
1294 return line;
1297 static void
1298 quit(int signo)
1300 lastsig = signo;
1301 if (vflag && fchange) {
1302 fchange = 0;
1303 error("warning: expecting `w'");
1305 if (wrtemp)
1306 unlink(wrtemp);
1307 unlink(tfname);
1308 exit(status);
1311 static void
1312 delete(void)
1314 setdot();
1315 newline();
1316 nonzero();
1317 checkpoint();
1318 rdelete(addr1, addr2);
1321 static void
1322 rdelete(long *ad1, long *ad2)
1324 register long *a1, *a2, *a3;
1326 a1 = ad1;
1327 a2 = ad2+1;
1328 a3 = dol;
1329 dol -= a2 - a1;
1330 do {
1331 *a1++ = *a2++;
1332 } while (a2 <= a3);
1333 a1 = ad1;
1334 if (a1 > dol)
1335 a1 = dol;
1336 dot = a1;
1337 fchange = 1;
1340 static void
1341 gdelete(void)
1343 register long *a1, *a2, *a3;
1345 a3 = dol;
1346 for (a1=zero+1; (*a1&01)==0; a1++)
1347 if (a1>=a3)
1348 return;
1349 for (a2=a1+1; a2<=a3;) {
1350 if (*a2&01) {
1351 a2++;
1352 dot = a1;
1353 } else
1354 *a1++ = *a2++;
1356 dol = a1-1;
1357 if (dot>dol)
1358 dot = dol;
1359 fchange = 1;
1362 static char *
1363 getline(long tl, int nulterm)
1365 register char *bp, *lp;
1366 register long nl;
1368 lp = linebuf;
1369 bp = getblock(tl, READ);
1370 nl = nleft;
1371 tl &= ~0377;
1372 while (*lp++ = *bp++) {
1373 if (lp[-1] == '\n' && nulterm) {
1374 lp[-1] = '\0';
1375 break;
1377 if (--nl == 0) {
1378 bp = getblock(tl+=0400, READ);
1379 nl = nleft;
1382 return(linebuf);
1385 static int
1386 putline(void)
1388 register char *bp, *lp;
1389 register long nl;
1390 long tl;
1392 fchange = 1;
1393 lp = linebuf;
1394 tl = tline;
1395 bp = getblock(tl, WRITE);
1396 nl = nleft;
1397 tl &= ~0377;
1398 while (*bp = *lp++) {
1399 if (*bp++ == '\n' && insub) {
1400 *--bp = 0;
1401 linebp = lp;
1402 break;
1404 if (--nl == 0) {
1405 bp = getblock(tl+=0400, WRITE);
1406 nl = nleft;
1409 nl = tline;
1410 tline += (((lp-linebuf)+03)>>1)&(MAXCNT-1);
1411 return(nl);
1414 static char *
1415 getblock(long atl, long iof)
1417 register long bno, off;
1419 bno = (atl>>8)&BLKMSK;
1420 off = (atl<<1)&0774;
1421 if (bno >= BLKMSK) {
1422 lastc = '\n';
1423 error("temp file too big");
1425 nleft = 512 - off;
1426 if (bno==iblock) {
1427 ichanged |= iof;
1428 return(ibuff+off);
1430 if (bno==oblock)
1431 return(obuff+off);
1432 if (iof==READ) {
1433 if (ichanged)
1434 blkio(iblock, ibuff, 1);
1435 ichanged = 0;
1436 iblock = bno;
1437 blkio(bno, ibuff, 0);
1438 return(ibuff+off);
1440 if (oblock>=0)
1441 blkio(oblock, obuff, 1);
1442 oblock = bno;
1443 return(obuff+off);
1446 static void
1447 blkio(long b, char *buf, int wr)
1449 lseek(tfile, b<<9, SEEK_SET);
1450 if ((wr ? write(tfile, buf, 512) : read (tfile, buf, 512)) != 512) {
1451 status = 1;
1452 error("I/O error on temp file");
1456 static void
1457 init(void)
1459 register long *markp;
1461 tline = 2;
1462 for (markp = names; markp < &names[26]; markp++)
1463 *markp = 0;
1464 for (markp = undnames; markp < &undnames[26]; markp++)
1465 *markp = 0;
1466 subnewa = 0;
1467 anymarks = 0;
1468 iblock = -1;
1469 oblock = -1;
1470 ichanged = 0;
1471 tfile = maketf(tfile);
1472 dot = dol = zero;
1473 unddot = NULL;
1476 static void
1477 global(int k, int ia)
1479 register int c;
1480 register long *a1;
1481 static char *globuf;
1482 char mb[MB_LEN_MAX+1];
1483 int spflag = 0;
1485 if (globp)
1486 error("multiple globals not allowed");
1487 setall();
1488 nonzero();
1489 if ((c=GETWC(mb))=='\n')
1490 error("incomplete global expression");
1491 compile(NULL, expbuf, &expbuf[ESIZE], c);
1492 if (!ia) {
1493 globrd(&globuf, EOF);
1494 if (globuf[0] == '\n')
1495 globuf[0] = 'p', globuf[1] = '\n', globuf[2] = '\0';
1496 } else {
1497 newline();
1498 spflag = pflag;
1499 pflag = 0;
1501 checkpoint();
1502 for (a1=zero; a1<=dol; a1++) {
1503 *a1 &= ~01;
1504 if (a1>=addr1 && a1<=addr2 && execute(0, a1, 0)==k)
1505 *a1 |= 01;
1507 /*
1508 * Special case: g/.../d (avoid n^2 algorithm)
1509 */
1510 if (!ia && globuf[0]=='d' && globuf[1]=='\n' && globuf[2]=='\0') {
1511 gdelete();
1512 return;
1514 for (a1=zero; a1<=dol; a1++) {
1515 if (*a1 & 01) {
1516 *a1 &= ~01;
1517 dot = a1;
1518 if (ia) {
1519 puts(getline(*a1, 0));
1520 if ((c = getchr()) == EOF)
1521 error("command expected");
1522 if (c == 'a' || c == 'c' || c == 'i')
1523 error("a, i, or c not allowed in G");
1524 else if (c == '&') {
1525 if ((c = getchr()) != '\n')
1526 error("end of line expected");
1527 if (globuf == 0 || *globuf == 0)
1528 error("no remembered command");
1529 } else if (c == '\n') {
1530 a1 = zero;
1531 continue;
1532 } else
1533 globrd(&globuf, c);
1535 globp = globuf;
1536 commands();
1537 globp = NULL;
1538 a1 = zero;
1541 if (ia)
1542 pflag = spflag;
1545 static void
1546 globrd(char **globuf, register int c)
1548 register int i;
1550 if (*globuf == 0 && (*globuf = malloc(GBSIZE=256)) == 0)
1551 error("global too long");
1552 i = 0;
1553 if (c != EOF)
1554 (*globuf)[i++] = c;
1555 while ((c = getchr()) != '\n') {
1556 if (c==EOF)
1557 error("incomplete global expression");
1558 if (c=='\\') {
1559 c = getchr();
1560 if (c!='\n')
1561 (*globuf)[i++] = '\\';
1563 (*globuf)[i++] = c;
1564 if (i>=GBSIZE-4 && (*globuf=realloc(*globuf,GBSIZE+=256)) == 0)
1565 error("global too long");
1567 (*globuf)[i++] = '\n';
1568 (*globuf)[i++] = 0;
1571 static void
1572 join(void)
1574 register int i, j;
1575 register long *a1;
1577 j = 0;
1578 for (a1=addr1; a1<=addr2; a1++) {
1579 i = getline(*a1, 0) - linebuf;
1580 while (genbuf[j] = linebuf[i++])
1581 if (j++ >= LBSIZE-2)
1582 growlb("line too long");
1584 i = 0;
1585 j = 0;
1586 while (linebuf[i++] = genbuf[j++])
1588 *addr1 = putline();
1589 if (addr1<addr2)
1590 rdelete(addr1+1, addr2);
1591 dot = addr1;
1594 static void
1595 substitute(int inglob)
1597 register long *markp;
1598 register long *a1;
1599 intptr_t nl;
1600 int gsubf;
1602 checkpoint();
1603 gsubf = compsub();
1604 insub = 1;
1605 for (a1 = addr1; a1 <= addr2; a1++) {
1606 long *ozero;
1607 if (execute(0, a1, 1)==0)
1608 continue;
1609 inglob |= dosub(gsubf < 2);
1610 if (gsubf) {
1611 int i = 1;
1613 while (*loc2) {
1614 if (execute(1, NULL, 1)==0)
1615 break;
1616 inglob |= dosub(gsubf == -1 || ++i == gsubf);
1619 subnewa = putline();
1620 *a1 &= ~01;
1621 if (anymarks) {
1622 for (markp = names; markp < &names[26]; markp++)
1623 if (*markp == *a1)
1624 *markp = subnewa;
1626 *a1 = subnewa;
1627 ozero = zero;
1628 nl = append(getsub, a1);
1629 nl += zero-ozero;
1630 a1 += nl;
1631 addr2 += nl;
1633 insub = 0;
1634 if (inglob==0)
1635 error("no match");
1638 static int
1639 compsub(void)
1641 register int seof, c, i;
1642 static char *oldrhs;
1643 static int orhssz;
1644 char mb[MB_LEN_MAX+1];
1646 if ((seof = GETWC(mb)) == '\n' || seof == ' ')
1647 error("illegal or missing delimiter");
1648 nodelim = 0;
1649 compile(NULL, expbuf, &expbuf[ESIZE], seof);
1650 i = 0;
1651 for (;;) {
1652 c = GETWC(mb);
1653 if (c=='\\') {
1654 if (i >= RHSIZE-2)
1655 growrhs("replacement string too long");
1656 rhsbuf[i++] = c;
1657 c = GETWC(mb);
1658 } else if (c=='\n') {
1659 if (globp && *globp) {
1660 if (i >= RHSIZE-2)
1661 growrhs("replacement string too long");
1662 rhsbuf[i++] = '\\';
1664 else if (nodelim)
1665 error("illegal or missing delimiter");
1666 else {
1667 peekc = c;
1668 pflag++;
1669 break;
1671 } else if (c==seof)
1672 break;
1673 for (c = 0; c==0 || mb[c]; c++) {
1674 if (i >= RHSIZE-2)
1675 growrhs("replacement string too long");
1676 rhsbuf[i++] = mb[c];
1679 rhsbuf[i++] = 0;
1680 if (rhsbuf[0] == '%' && rhsbuf[1] == 0) {
1681 if (orhssz == 0)
1682 error("no remembered replacement string");
1683 strcpy(rhsbuf, oldrhs);
1684 } else {
1685 if (orhssz < RHSIZE &&
1686 (oldrhs = realloc(oldrhs, orhssz=RHSIZE)) == 0)
1687 error("replacement string too long");
1688 strcpy(oldrhs, rhsbuf);
1690 if ((peekc = getchr()) == 'g') {
1691 peekc = 0;
1692 newline();
1693 return(-1);
1694 } else if (peekc >= '0' && peekc <= '9') {
1695 c = getnum();
1696 if (c < 1 || c > LBSIZE)
1697 error("invalid count");
1698 newline();
1699 return c;
1701 newline();
1702 return(0);
1705 static int
1706 getsub(void)
1708 register char *p1, *p2;
1710 p1 = linebuf;
1711 if ((p2 = linebp) == 0)
1712 return(EOF);
1713 while (*p1++ = *p2++)
1715 linebp = 0;
1716 return(0);
1719 static int
1720 dosub(int really)
1722 register char *lp, *sp;
1723 register int i, j, k;
1724 int c;
1726 if (!really)
1727 goto copy;
1728 i = 0;
1729 j = 0;
1730 k = 0;
1731 while (&linebuf[i] < loc1)
1732 genbuf[j++] = linebuf[i++];
1733 while (c = rhsbuf[k++]&0377) {
1734 if (c=='&') {
1735 j = place(j, loc1, loc2);
1736 continue;
1737 } else if (c == '\\') {
1738 c = rhsbuf[k++]&0377;
1739 if (c >='1' && c < nbra+'1') {
1740 j = place(j, braslist[c-'1'], braelist[c-'1']);
1741 continue;
1744 if (j >= LBSIZE)
1745 growlb("line too long");
1746 genbuf[j++] = c;
1748 i = loc2 - linebuf;
1749 loc2 = j + linebuf;
1750 #if defined (SUS) || defined (SU3) || defined (S42)
1751 if (loc1 == &linebuf[i]) {
1752 int n;
1753 wchar_t wc;
1754 if (mb_cur_max > 1 && (n = mbtowc(&wc, loc2, mb_cur_max)) > 0)
1755 loc2 += n;
1756 else
1757 loc2++;
1759 #endif /* SUS || SU3 || S42 */
1760 while (genbuf[j++] = linebuf[i++])
1761 if (j >= LBSIZE)
1762 growlb("line too long");
1763 if (really) {
1764 lp = linebuf;
1765 sp = genbuf;
1766 } else {
1767 copy: sp = linebuf;
1768 lp = genbuf;
1770 while (*lp++ = *sp++)
1772 return really;
1775 static int
1776 place(register int j, register const char *l1, register const char *l2)
1779 while (l1 < l2) {
1780 genbuf[j++] = *l1++;
1781 if (j >= LBSIZE)
1782 growlb("line too long");
1784 return(j);
1787 static void
1788 move(int cflag)
1790 register long *adt, *ad1, *ad2;
1792 setdot();
1793 nonzero();
1794 if ((adt = address())==0)
1795 error("illegal move destination");
1796 newline();
1797 checkpoint();
1798 if (cflag) {
1799 long *ozero;
1800 intptr_t delta;
1801 ad1 = dol;
1802 ozero = zero;
1803 append(getcopy, ad1++);
1804 ad2 = dol;
1805 delta = zero - ozero;
1806 ad1 += delta;
1807 adt += delta;
1808 } else {
1809 ad2 = addr2;
1810 for (ad1 = addr1; ad1 <= ad2;)
1811 *ad1++ &= ~01;
1812 ad1 = addr1;
1814 ad2++;
1815 if (adt<ad1) {
1816 dot = adt + (ad2-ad1);
1817 if ((++adt)==ad1)
1818 return;
1819 reverse(adt, ad1);
1820 reverse(ad1, ad2);
1821 reverse(adt, ad2);
1822 } else if (adt >= ad2) {
1823 dot = adt++;
1824 reverse(ad1, ad2);
1825 reverse(ad2, adt);
1826 reverse(ad1, adt);
1827 } else
1828 error("illegal move destination");
1829 fchange = 1;
1832 static void
1833 reverse(register long *a1, register long *a2)
1835 register int t;
1837 for (;;) {
1838 t = *--a2;
1839 if (a2 <= a1)
1840 return;
1841 *a2 = *a1;
1842 *a1++ = t;
1846 static int
1847 getcopy(void)
1849 if (addr1 > addr2)
1850 return(EOF);
1851 getline(*addr1++, 0);
1852 return(0);
1855 static int
1856 execute(int gf, long *addr, int subst)
1858 register char *p1, *p2, c;
1860 for (c=0; c<NBRA; c++) {
1861 braslist[c&0377] = 0;
1862 braelist[c&0377] = 0;
1864 if (gf) {
1865 if (circf)
1866 return(0);
1867 p1 = linebuf;
1868 p2 = genbuf;
1869 while (*p1++ = *p2++)
1871 locs = p1 = loc2;
1872 } else {
1873 if (addr==zero)
1874 return(0);
1875 p1 = getline(*addr, 1);
1876 locs = 0;
1878 needsub = subst;
1879 return step(p1, expbuf);
1882 static void
1883 cmplerr(int c)
1885 const char *msg;
1887 #if !defined (SUS) && !defined (S42) && !defined (SU3)
1888 expbuf[0] = 0;
1889 #endif
1890 switch (c) {
1891 case 11:
1892 msg = "Range endpoint too large";
1893 break;
1894 case 16:
1895 msg = "bad number";
1896 break;
1897 case 25:
1898 msg = "`\\digit' out of range";
1899 break;
1900 case 36:
1901 msg = "illegal or missing delimiter";
1902 break;
1903 case 41:
1904 msg = "no remembered search string";
1905 break;
1906 case 42:
1907 msg = "'\\( \\)' imbalance";
1908 break;
1909 case 43:
1910 msg = "Too many `\\(' s";
1911 break;
1912 case 44:
1913 msg = "more than 2 numbers given";
1914 break;
1915 case 45:
1916 msg = "'\\}' expected";
1917 break;
1918 case 46:
1919 msg = "first number exceeds second";
1920 break;
1921 case 49:
1922 msg = "'[ ]' imbalance";
1923 break;
1924 case 50:
1925 msg = "regular expression overflow";
1926 break;
1927 case 67:
1928 msg = "illegal byte sequence";
1929 break;
1930 default:
1931 msg = "regular expression error";
1932 break;
1934 error(msg);
1937 static void
1938 doprnt(long *bot, long *top)
1940 long *a1;
1942 a1 = bot;
1943 do {
1944 if (numbf ^ Nflag) {
1945 putd(a1-zero);
1946 putchr('\t');
1948 nlputs(getline(*a1++, 0));
1949 } while (a1 <= top);
1950 pflag = 0;
1951 listf = 0;
1952 numbf = 0;
1955 static void
1956 putd(long c)
1958 register int r;
1960 r = c%10;
1961 c /= 10;
1962 if (c)
1963 putd(c);
1964 putchr(r + '0');
1967 static void
1968 nlputs(register const char *sp)
1970 if (listf)
1971 list(sp);
1972 else if (tabstops)
1973 expand(sp);
1974 else
1975 puts(sp);
1978 static void
1979 puts(register const char *sp)
1981 while (*sp) {
1982 if (*sp != '\n')
1983 putchr(*sp++ & 0377);
1984 else
1985 sp++, putchr('\0');
1987 putchr('\n');
1990 static void
1991 list(const char *lp)
1993 int col, n;
1994 wchar_t c;
1996 col = numbf ^ Nflag ? 8 : 0;
1997 while (*lp) {
1998 if (mb_cur_max > 1 && *lp&0200)
1999 n = mbtowc(&c, lp, mb_cur_max);
2000 else {
2001 n = 1;
2002 c = *lp&0377;
2004 if (col+1 >= 72) {
2005 col = 0;
2006 putchr('\\');
2007 putchr('\n');
2009 if (n<0 ||
2010 #if defined (SUS) || defined (S42) || defined (SU3)
2011 c == '\\' ||
2012 #endif /* SUS || S42 || SU3 */
2013 !(mb_cur_max>1 ? iswprint(c) : isprint(c))) {
2014 if (n<0)
2015 n = 1;
2016 while (n--)
2017 col += lstchr(*lp++&0377);
2018 } else if (mb_cur_max>1) {
2019 col += wcwidth(c);
2020 while (n--)
2021 putchr(*lp++&0377);
2022 } else {
2023 putchr(*lp++&0377);
2024 col++;
2027 #if defined (SUS) || defined (S42) || defined (SU3)
2028 putchr('$');
2029 #endif
2030 putchr('\n');
2033 static int
2034 lstchr(int c)
2036 int cad = 1, d;
2038 #if !defined (SUS) && !defined (S42) && !defined (SU3)
2039 if (c=='\t') {
2040 c = '>';
2041 goto esc;
2043 if (c=='\b') {
2044 c = '<';
2045 esc:
2046 putchr('-');
2047 putchr('\b');
2048 putchr(c);
2049 } else if (c == '\n') {
2050 putchr('\\');
2051 putchr('0');
2052 putchr('0');
2053 putchr('0');
2054 cad = 4;
2055 #else /* !SUS, !S42, !SU3 */
2056 if (c == '\n')
2057 c = '\0';
2058 if (c == '\\') {
2059 putchr('\\');
2060 putchr('\\');
2061 cad = 2;
2062 } else if (c == '\a') {
2063 putchr('\\');
2064 putchr('a');
2065 cad = 2;
2066 } else if (c == '\b') {
2067 putchr('\\');
2068 putchr('b');
2069 cad = 2;
2070 } else if (c == '\f') {
2071 putchr('\\');
2072 putchr('f');
2073 cad = 2;
2074 } else if (c == '\r') {
2075 putchr('\\');
2076 putchr('r');
2077 cad = 2;
2078 } else if (c == '\t') {
2079 putchr('\\');
2080 putchr('t');
2081 cad = 2;
2082 } else if (c == '\v') {
2083 putchr('\\');
2084 putchr('v');
2085 cad = 2;
2086 #endif /* !SUS, !S42, !SU3 */
2087 } else {
2088 putchr('\\');
2089 putchr(((c&~077)>>6)+'0');
2090 c &= 077;
2091 d = c & 07;
2092 putchr(c > d ? ((c-d)>>3)+'0' : '0');
2093 putchr(d+'0');
2094 cad = 4;
2096 return cad;
2099 static void
2100 putstr(const char *s)
2102 while (*s)
2103 putchr(*s++);
2106 static char line[70];
2107 static char *linp = line;
2109 static void
2110 putchr(int ac)
2112 register char *lp;
2113 register int c;
2115 lp = linp;
2116 c = ac;
2117 *lp++ = c;
2118 if(c == '\n' || lp >= &line[64]) {
2119 linp = line;
2120 write(1, line, lp-line);
2121 return;
2123 linp = lp;
2126 static void
2127 checkpoint(void)
2129 long *a1, *a2;
2131 if (undzero && globp == NULL) {
2132 for (a1 = zero+1, a2 = undzero+1; a1 <= dol; a1++, a2++)
2133 *a2 = *a1;
2134 unddot = &undzero[dot-zero];
2135 unddol = &undzero[dol-zero];
2136 for (a1 = names, a2 = undnames; a1 < &names[26]; a1++, a2++)
2137 *a2 = *a1;
2141 #define swap(a, b) (t = a, a = b, b = t)
2143 static void
2144 undo(void)
2146 long *t;
2148 if (undzero == NULL)
2149 error("no undo information saved");
2150 swap(zero, undzero);
2151 swap(dot, unddot);
2152 swap(dol, unddol);
2153 swap(names, undnames);
2156 static int
2157 maketf(int fd)
2159 char *tmpdir;
2161 if (fd == -1) {
2162 if ((tmpdir = getenv("TMPDIR")) == NULL ||
2163 (fd = creatf(tmpdir)) < 0)
2164 if ((fd = creatf("/var/tmp")) < 0 &&
2165 (fd = creatf("/tmp")) < 0)
2166 error("cannot create temporary file");
2167 } else
2168 ftruncate(fd, 0); /* blkio() will seek to 0 anyway */
2169 return fd;
2172 static int
2173 creatf(const char *tmpdir)
2175 if (strlen(tmpdir) >= sizeof tfname - 9)
2176 return -1;
2177 strcpy(tfname, tmpdir);
2178 strcat(tfname, "/eXXXXXX");
2179 return mkstemp(tfname);
2182 static int
2183 sopen(const char *fn, int rdwr)
2185 int pf[2], fd = -1;
2187 if (fn[0] == '!') {
2188 fn++;
2189 if (pipe(pf) < 0)
2190 error("write or open on pipe failed");
2191 switch (pipid = fork()) {
2192 case 0:
2193 if (rdwr == READ)
2194 dup2(pf[1], 1);
2195 else
2196 dup2(pf[0], 0);
2197 close(pf[0]);
2198 close(pf[1]);
2199 sigset(SIGHUP, oldhup);
2200 sigset(SIGQUIT, oldquit);
2201 sigset(SIGPIPE, oldpipe);
2202 execl(SHELL, "sh", "-c", fn, NULL);
2203 _exit(0100);
2204 default:
2205 close(pf[rdwr == READ ? 1 : 0]);
2206 fd = pf[rdwr == READ ? 0 : 1];
2207 break;
2208 case -1:
2209 error("fork failed - try again");
2211 } else if (rdwr == READ)
2212 fd = open(fn, O_RDONLY);
2213 else if (rdwr == EXIST)
2214 fd = open(fn, O_WRONLY);
2215 else /*if (rdwr == WRITE)*/
2216 fd = creat(fn, 0666);
2217 if (fd >= 0 && rdwr == READ)
2218 readop = 1;
2219 if (fd >= 0)
2220 fstat(fd, &fstbuf);
2221 return fd;
2224 static void
2225 sclose(int fd)
2227 int status;
2229 close(fd);
2230 if (pipid >= 0) {
2231 while (wait(&status) != pipid);
2232 pipid = -1;
2234 readop = 0;
2237 static void
2238 fspec(const char *lp)
2240 struct termios ts;
2241 const char *cp;
2243 freetabs();
2244 maxlength = 0;
2245 if (tcgetattr(1, &ts) < 0
2246 #ifdef TAB3
2247 || (ts.c_oflag&TAB3) == 0
2248 #endif
2250 return;
2251 while (lp[0]) {
2252 if (lp[0] == '<' && lp[1] == ':')
2253 break;
2254 lp++;
2256 if (lp[0]) {
2257 lp += 2;
2258 while ((cp = ftok(&lp)) != NULL) {
2259 switch (*cp) {
2260 case 't':
2261 freetabs();
2262 if ((tabstops = tabstring(&cp[1])) == NULL)
2263 goto err;
2264 break;
2265 case 's':
2266 maxlength = atoi(&cp[1]);
2267 break;
2268 case 'm':
2269 case 'd':
2270 case 'e':
2271 break;
2272 case ':':
2273 if (cp[1] == '>') {
2274 if (tabstops == NULL)
2275 if ((tabstops = tabstring("0"))
2276 == NULL)
2277 goto err;
2278 return;
2280 /*FALLTHRU*/
2281 default:
2282 err: freetabs();
2283 maxlength = 0;
2284 errput("PWB spec problem", NULL);
2285 return;
2291 static const char *
2292 ftok(const char **lp)
2294 const char *cp;
2296 while (**lp && **lp != ':' && (**lp == ' ' || **lp == '\t'))
2297 (*lp)++;
2298 cp = *lp;
2299 while (**lp && **lp != ':' && **lp != ' ' && **lp != '\t')
2300 (*lp)++;
2301 return cp;
2304 static struct tabulator *
2305 repetitive(int repetition)
2307 struct tabulator *tp, *tabspec;
2308 int col, i;
2310 if ((tp = tabspec = calloc(1, sizeof *tp)) == NULL)
2311 return NULL;
2312 tp->t_rep = repetition;
2313 if (repetition > 0) {
2314 for (col = 1+repetition, i = 0; i < 22; col += repetition) {
2315 if ((tp->t_nxt = calloc(1, sizeof *tp)) == NULL)
2316 return NULL;
2317 tp = tp->t_nxt;
2318 tp->t_tab = col;
2321 return tabspec;
2324 #define blank(c) ((c) == ' ' || (c) == '\t')
2326 static struct tabulator *
2327 tablist(const char *s)
2329 struct tabulator *tp, *tabspec;
2330 char *x;
2331 int prev = 0, val;
2333 if ((tp = tabspec = calloc(1, sizeof *tp)) == NULL)
2334 return NULL;
2335 for (;;) {
2336 while (*s == ',')
2337 s++;
2338 if (*s == '\0' || blank(*s) || *s == ':')
2339 break;
2340 val = strtol(s, &x, 10);
2341 if (*s == '+')
2342 val += prev;
2343 prev = val;
2344 if (*s == '-' || (*x != ',' && !blank(*x) && *x != ':' &&
2345 *x != '\0'))
2346 return NULL;
2347 s = x;
2348 if ((tp->t_nxt = calloc(1, sizeof *tp)) == NULL)
2349 return NULL;
2350 tp = tp->t_nxt;
2351 tp->t_tab = val;
2353 return tabspec;
2356 static struct tabulator *
2357 tabstring(const char *s)
2359 const struct {
2360 const char *c_nam;
2361 const char *c_str;
2362 } canned[] = {
2363 { "a", "1,10,16,36,72" },
2364 { "a2", "1,10,16,40,72" },
2365 { "c", "1,8,12,16,20,55" },
2366 { "c2", "1,6,10,14,49" },
2367 { "c3", "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67" },
2368 { "f", "1,7,11,15,19,23" },
2369 { "p", "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61" },
2370 { "s", "1,10,55" },
2371 { "u", "1,12,20,44" },
2372 { 0, 0 }
2373 };
2375 int i, j;
2377 if (s[0] == '-') {
2378 if (s[1] >= '0' && s[1] <= '9' && ((i = atoi(&s[1])) != 0))
2379 return repetitive(i);
2380 for (i = 0; canned[i].c_nam; i++) {
2381 for (j = 0; canned[i].c_nam[j]; j++)
2382 if (s[j+1] != canned[i].c_nam[j])
2383 break;
2384 if ((s[j+1]=='\0' || s[j+1]==':' || blank(s[j+1])) &&
2385 canned[i].c_nam[j] == '\0')
2386 return tablist(canned[i].c_str);
2388 return NULL;
2389 } else
2390 return tablist(s);
2393 static void
2394 freetabs(void)
2396 struct tabulator *tp;
2398 tp = tabstops;
2399 while (tp) {
2400 tabstops = tp->t_nxt;
2401 free(tp);
2402 tp = tabstops;
2406 static void
2407 expand(const char *s)
2409 struct tabulator *tp = tabstops;
2410 int col = 0, n = 1, m, tabcnt = 0, nspc;
2411 wchar_t wc;
2413 while (*s) {
2414 nspc = 0;
2415 switch (*s) {
2416 case '\n':
2417 putchr('\0');
2418 s++;
2419 continue;
2420 case '\t':
2421 if (tp) {
2422 if (tp->t_rep) {
2423 if (col % tp->t_rep == 0) {
2424 nspc++;
2425 col++;
2427 while (col % tp->t_rep) {
2428 nspc++;
2429 col++;
2431 break;
2433 while (tp && (col>tp->t_tab || tp->t_tab == 0))
2434 tp = tp->t_nxt;
2435 if (tp && col == tp->t_tab) {
2436 nspc++;
2437 col++;
2438 tp = tp->t_nxt;
2440 if (tp) {
2441 while (col < tp->t_tab) {
2442 nspc++;
2443 col++;
2445 tp = tp->t_nxt;
2446 break;
2449 tabcnt = 1;
2450 nspc++;
2451 break;
2452 default:
2453 if (mb_cur_max>1 && (n=mbtowc(&wc, s, mb_cur_max))>0) {
2454 if ((m = wcwidth(wc)) > 0)
2455 col += m;
2456 } else {
2457 col++;
2458 n = 1;
2461 if (maxlength && col > maxlength) {
2462 putstr("\ntoo long");
2463 break;
2465 if (nspc) {
2466 while (nspc--)
2467 putchr(' ');
2468 s++;
2469 } else
2470 while (n--)
2471 putchr(*s++);
2473 if (tabcnt)
2474 putstr("\ntab count");
2475 putchr('\n');
2478 static wint_t
2479 GETWC(char *mb)
2481 int c, n;
2483 n = 1;
2484 mb[0] = c = GETC();
2485 mb[1] = '\0';
2486 if (mb_cur_max > 1 && c&0200 && c != EOF) {
2487 int m;
2488 wchar_t wc;
2490 while ((m = mbtowc(&wc, mb, mb_cur_max)) < 0 && n<mb_cur_max) {
2491 mb[n++] = c = GETC();
2492 mb[n] = '\0';
2493 if (c == '\n' || c == EOF)
2494 break;
2496 if (m != n)
2497 ERROR(67);
2498 return wc;
2499 } else
2500 return c;
2503 static void
2504 growlb(const char *msg)
2506 char *olb = linebuf;
2507 int i;
2509 LBSIZE += 512;
2510 if ((linebuf = realloc(linebuf, LBSIZE)) == NULL ||
2511 (genbuf = realloc(genbuf, LBSIZE)) == NULL)
2512 error(msg);
2513 if (linebuf != olb) {
2514 loc1 += linebuf - olb;
2515 loc2 += linebuf - olb;
2516 for (i = 0; i < NBRA; i++) {
2517 if (braslist[i])
2518 braslist[i] += linebuf - olb;
2519 if (braelist[i])
2520 braelist[i] += linebuf - olb;
2525 static void
2526 growrhs(const char *msg)
2528 RHSIZE += 256;
2529 if ((rhsbuf = realloc(rhsbuf, RHSIZE)) == NULL)
2530 error(msg);
2533 static void
2534 growfn(const char *msg)
2536 FNSIZE += 64;
2537 if ((savedfile = realloc(savedfile, FNSIZE)) == NULL ||
2538 (file = realloc(file, FNSIZE)) == NULL)
2539 error(msg);
2540 if (FNSIZE == 64)
2541 file[0] = savedfile[0] = 0;
2544 #if defined (SUS) || defined (S42) || defined (SU3)
2545 union ptrstore {
2546 void *vp;
2547 char bp[sizeof (void *)];
2548 };
2550 static void *
2551 fetchptr(const char *bp)
2553 union ptrstore u;
2554 int i;
2556 for (i = 0; i < sizeof (void *); i++)
2557 u.bp[i] = bp[i];
2558 return u.vp;
2561 static void
2562 storeptr(void *vp, char *bp)
2564 union ptrstore u;
2565 int i;
2567 u.vp = vp;
2568 for (i = 0; i < sizeof (void *); i++)
2569 bp[i] = u.bp[i];
2572 #define add(c) ((i>=LBSIZE ? (growlb("regular expression overflow"),0) : 0), \
2573 genbuf[i++] = (c))
2575 #define copy(s) { \
2576 int m; \
2577 for (m = 0; m==0 || s[m]; m++) \
2578 add(s[m]); \
2581 static char *
2582 compile(char *unused, char *ep, const char *endbuf, int seof)
2584 INIT
2585 int c, d, i;
2586 regex_t *rp;
2587 char *op;
2588 char mb[MB_LEN_MAX+1];
2590 op = ep;
2591 ep += 2;
2592 if ((rp = fetchptr(ep)) == NULL) {
2593 if ((rp = calloc(1, sizeof *rp)) == NULL)
2594 ERROR(50);
2595 storeptr(rp, ep);
2597 ep += sizeof (void *);
2598 i = 0;
2599 nbra = 0;
2600 do {
2601 if ((c = GETWC(mb)) == seof)
2602 add('\0');
2603 else if (c == '\\') {
2604 copy(mb);
2605 c = GETWC(mb);
2606 if (c == '(')
2607 nbra++;
2608 goto normchar;
2609 } else if (c == '[') {
2610 add(c);
2611 d = EOF;
2612 do {
2613 c = GETWC(mb);
2614 if (c == EOF || c == '\n')
2615 ERROR(49);
2616 copy(mb);
2617 if (d=='[' && (c==':' || c=='.' || c=='=')) {
2618 d = c;
2619 do {
2620 c = GETWC(mb);
2621 if (c == EOF || c == '\n')
2622 ERROR(49);
2623 copy(mb);
2624 } while (c != d || PEEKC() != ']');
2625 c = GETWC(mb);
2626 copy(mb);
2627 c = EOF;
2629 d = c;
2630 } while (c != ']');
2631 } else {
2632 if (c == EOF || c == '\n') {
2633 if (c == '\n')
2634 UNGETC(c);
2635 mb[0] = c = '\0';
2637 if (c == '\0')
2638 nodelim = 1;
2639 normchar: copy(mb);
2641 } while (genbuf[i-1] != '\0');
2642 if (genbuf[0]) {
2643 int reflags = 0;
2645 #ifdef REG_ANGLES
2646 reflags |= REG_ANGLES;
2647 #endif
2648 #if defined (SU3) && defined (REG_AVOIDNULL)
2649 reflags |= REG_AVOIDNULL;
2650 #endif
2651 if (op[0])
2652 regfree(rp);
2653 op[0] = 0;
2654 switch (regcomp(rp, genbuf, reflags)) {
2655 case 0:
2656 break;
2657 case REG_ESUBREG:
2658 ERROR(25);
2659 /*NOTREACHED*/
2660 case REG_EBRACK:
2661 ERROR(49);
2662 /*NOTREACHED*/
2663 case REG_EPAREN:
2664 ERROR(42);
2665 /*NOTREACHED*/
2666 case REG_BADBR:
2667 case REG_EBRACE:
2668 ERROR(45);
2669 /*NOTREACHED*/
2670 case REG_ERANGE:
2671 ERROR(11);
2672 /*NOTREACHED*/
2673 case REG_ESPACE:
2674 ERROR(50);
2675 /*NOTREACHED*/
2676 default:
2677 ERROR(-1);
2679 op[0] = 1;
2680 circf = op[1] = genbuf[0] == '^';
2681 } else if (op[0]) {
2682 circf = op[1];
2683 } else
2684 ERROR(41);
2685 return ep + sizeof (void *);
2688 static int
2689 step(const char *lp, const char *ep)
2691 regex_t *rp;
2692 regmatch_t bralist[NBRA+1];
2693 int eflag = 0;
2694 int res;
2695 int i;
2697 rp = fetchptr(&ep[2]);
2698 if (ep[0] == 0)
2699 return 0;
2700 if (locs)
2701 eflag |= REG_NOTBOL;
2702 if ((res = regexec(rp, lp, needsub? NBRA+1 : 0, bralist, eflag)) == 0 &&
2703 needsub) {
2704 loc1 = (char *)lp + bralist[0].rm_so;
2705 loc2 = (char *)lp + bralist[0].rm_eo;
2706 for (i = 1; i <= NBRA; i++) {
2707 if (bralist[i].rm_so != -1) {
2708 braslist[i-1] = (char *)lp + bralist[i].rm_so;
2709 braelist[i-1] = (char *)lp + bralist[i].rm_eo;
2710 } else
2711 braslist[i-1] = braelist[i-1] = NULL;
2714 return res == 0;
2716 #endif /* SUS || S42 || SU3 */
2718 static void
2719 help(void)
2721 const char *desc[] = {
2722 "(.)a append up to .",
2723 "(.)b[n] browse n lines",
2724 "(.,.)c change up to .",
2725 "(.,.)d delete lines",
2726 "e [file] edit file",
2727 "E [file] force edit",
2728 "f [file] print or set file",
2729 "(1,$)g/RE/cmd global cmd",
2730 "(1,$)G/RE/ interactive global",
2731 "h print last error",
2732 "H toggle error messages",
2733 "help print this screen",
2734 "(.)i insert up to .",
2735 "(.,.+1)j join lines",
2736 "(.)kx mark line with x",
2737 "(.,.)l list lines",
2738 "(.,.)ma move lines to a",
2739 "(.,.)n number lines",
2740 "N revert n and p",
2741 "(.)o[n] show n lines of context",
2742 "(.,.)p print lines",
2743 "P toggle prompt",
2744 "q quit",
2745 "Q force quit",
2746 "($)r read file",
2747 "(.,.)s/RE/repl/ search and replace",
2748 "(.,.)s/RE/rp/g replace all occurrences",
2749 "(.,.)s/RE/rp/n replace n-th occurrence",
2750 "(.,.)ta transfer lines to a",
2751 "u undo last change",
2752 "(1,$)v/RE/cmd reverse global",
2753 "(1,$)V/RE/ reverse i/a global",
2754 "(1,$)w [file] write file",
2755 "(1,$)W [file] append to file",
2756 "z write buffer and quit",
2757 "($)= print line number",
2758 "!command execute shell command",
2759 "(.+1)<newline> print one line",
2760 "/RE find RE forwards",
2761 "?RE find RE backwards",
2762 "1 first line",
2763 ". current line",
2764 "$ last line",
2765 ", 1,$",
2766 "; .,$",
2767 NULL
2768 };
2769 char line[100];
2770 int c, half, i, k;
2772 half = (sizeof desc / sizeof *desc) / 2;
2773 for (i = 0; i < half && desc[i]; i++) {
2774 c = 0;
2775 for (k = 0; desc[i][k]; k++)
2776 line[c++] = desc[i][k];
2777 if (desc[i+half]) {
2778 while (c < 40)
2779 line[c++] = ' ';
2780 for (k = 0; desc[i+half][k]; k++)
2781 line[c++] = desc[i+half][k];
2783 line[c] = 0;
2784 puts(line);