xref: /plan9/sys/src/cmd/ed.c (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
1 /*
2  * Editor
3  */
4 #include <u.h>
5 #include <libc.h>
6 #include <bio.h>
7 #include <regexp.h>
8 
9 enum
10 {
11 	FNSIZE	= 128,		/* file name */
12 	LBSIZE	= 4096,		/* max line size */
13 	BLKSIZE	= 4096,		/* block size in temp file */
14 	NBLK	= 8191,		/* max size of temp file */
15 	ESIZE	= 256,		/* max size of reg exp */
16 	GBSIZE	= 256,		/* max size of global command */
17 	MAXSUB	= 9,		/* max number of sub reg exp */
18 	ESCFLG	= 0xFFFF,	/* escape Rune - user defined code */
19 	EOF	= -1,
20 };
21 
22 void	(*oldhup)(int);
23 void	(*oldquit)(int);
24 int*	addr1;
25 int*	addr2;
26 int	anymarks;
27 Biobuf	bcons;
28 int	col;
29 long	count;
30 int*	dol;
31 int*	dot;
32 int	fchange;
33 char	file[FNSIZE];
34 Rune	genbuf[LBSIZE];
35 int	given;
36 Rune*	globp;
37 int	iblock;
38 int	ichanged;
39 int	io;
40 Biobuf	iobuf;
41 int	lastc;
42 char	line[70];
43 Rune*	linebp;
44 Rune	linebuf[LBSIZE];
45 int	listf;
46 int	listn;
47 Rune*	loc1;
48 Rune*	loc2;
49 int	names[26];
50 int	nleft;
51 int	oblock;
52 int	oflag;
53 Reprog	*pattern;
54 int	peekc;
55 int	pflag;
56 int	rescuing;
57 Rune	rhsbuf[LBSIZE/2];
58 char	savedfile[FNSIZE];
59 jmp_buf	savej;
60 int	subnewa;
61 int	subolda;
62 Resub	subexp[MAXSUB];
63 char*	tfname;
64 int	tline;
65 int	waiting;
66 int	wrapp;
67 int*	zero;
68 
69 char	Q[]	= "";
70 char	T[]	= "TMP";
71 char	WRERR[]	= "WRITE ERROR";
72 int	bpagesize = 20;
73 char	hex[]	= "0123456789abcdef";
74 char*	linp	= line;
75 ulong	nlall = 128;
76 int	tfile	= -1;
77 int	vflag	= 1;
78 
79 void	add(int);
80 int*	address(void);
81 int	append(int(*)(void), int*);
82 void	browse(void);
83 void	callunix(void);
84 void	commands(void);
85 void	compile(int);
86 int	compsub(void);
87 void	dosub(void);
88 void	error(char*);
89 int	match(int*);
90 void	exfile(int);
91 void	filename(int);
92 Rune*	getblock(int, int);
93 int	getchr(void);
94 int	getcopy(void);
95 int	getfile(void);
96 Rune*	getline(int);
97 int	getnum(void);
98 int	getsub(void);
99 int	gettty(void);
100 void	global(int);
101 void	init(void);
102 void	join(void);
103 void	move(int);
104 void	newline(void);
105 void	nonzero(void);
106 void	notifyf(void*, char*);
107 Rune*	place(Rune*, Rune*, Rune*);
108 void	printcom(void);
109 void	putchr(int);
110 void	putd(void);
111 void	putfile(void);
112 int	putline(void);
113 void	putshst(Rune*);
114 void	putst(char*);
115 void	quit(void);
116 void	rdelete(int*, int*);
117 void	regerror(char *);
118 void	reverse(int*, int*);
119 void	setnoaddr(void);
120 void	setwide(void);
121 void	squeeze(int);
122 void	substitute(int);
123 
124 void
125 main(int argc, char *argv[])
126 {
127 	char *p1, *p2;
128 
129 	Binit(&bcons, 0, OREAD);
130 	notify(notifyf);
131 	ARGBEGIN {
132 	case 'o':
133 		oflag = 1;
134 		vflag = 0;
135 		break;
136 	} ARGEND
137 
138 	USED(argc);
139 	if(*argv && (strcmp(*argv, "-") == 0)) {
140 		argv++;
141 		vflag = 0;
142 	}
143 	if(oflag) {
144 		p1 = "/fd/1";
145 		p2 = savedfile;
146 		while(*p2++ = *p1++)
147 			;
148 		globp = L"a";
149 	} else
150 	if(*argv) {
151 		p1 = *argv;
152 		p2 = savedfile;
153 		while(*p2++ = *p1++)
154 			if(p2 >= &savedfile[sizeof(savedfile)])
155 				p2--;
156 		globp = L"r";
157 	}
158 	zero = malloc((nlall+5)*sizeof(int*));
159 	tfname = mktemp("/tmp/eXXXXX");
160 	init();
161 	setjmp(savej);
162 	commands();
163 	quit();
164 }
165 
166 void
167 commands(void)
168 {
169 	int *a1, c, temp;
170 	char lastsep;
171 	Dir *d;
172 
173 	for(;;) {
174 		if(pflag) {
175 			pflag = 0;
176 			addr1 = addr2 = dot;
177 			printcom();
178 		}
179 		c = '\n';
180 		for(addr1 = 0;;) {
181 			lastsep = c;
182 			a1 = address();
183 			c = getchr();
184 			if(c != ',' && c != ';')
185 				break;
186 			if(lastsep == ',')
187 				error(Q);
188 			if(a1 == 0) {
189 				a1 = zero+1;
190 				if(a1 > dol)
191 					a1--;
192 			}
193 			addr1 = a1;
194 			if(c == ';')
195 				dot = a1;
196 		}
197 		if(lastsep != '\n' && a1 == 0)
198 			a1 = dol;
199 		if((addr2=a1) == 0) {
200 			given = 0;
201 			addr2 = dot;
202 		} else
203 			given = 1;
204 		if(addr1 == 0)
205 			addr1 = addr2;
206 		switch(c) {
207 
208 		case 'a':
209 			add(0);
210 			continue;
211 
212 		case 'b':
213 			nonzero();
214 			browse();
215 			continue;
216 
217 		case 'c':
218 			nonzero();
219 			newline();
220 			rdelete(addr1, addr2);
221 			append(gettty, addr1-1);
222 			continue;
223 
224 		case 'd':
225 			nonzero();
226 			newline();
227 			rdelete(addr1, addr2);
228 			continue;
229 
230 		case 'E':
231 			fchange = 0;
232 			c = 'e';
233 		case 'e':
234 			setnoaddr();
235 			if(vflag && fchange) {
236 				fchange = 0;
237 				error(Q);
238 			}
239 			filename(c);
240 			init();
241 			addr2 = zero;
242 			goto caseread;
243 
244 		case 'f':
245 			setnoaddr();
246 			filename(c);
247 			putst(savedfile);
248 			continue;
249 
250 		case 'g':
251 			global(1);
252 			continue;
253 
254 		case 'i':
255 			add(-1);
256 			continue;
257 
258 
259 		case 'j':
260 			if(!given)
261 				addr2++;
262 			newline();
263 			join();
264 			continue;
265 
266 		case 'k':
267 			nonzero();
268 			c = getchr();
269 			if(c < 'a' || c > 'z')
270 				error(Q);
271 			newline();
272 			names[c-'a'] = *addr2 & ~01;
273 			anymarks |= 01;
274 			continue;
275 
276 		case 'm':
277 			move(0);
278 			continue;
279 
280 		case 'n':
281 			listn++;
282 			newline();
283 			printcom();
284 			continue;
285 
286 		case '\n':
287 			if(a1==0) {
288 				a1 = dot+1;
289 				addr2 = a1;
290 				addr1 = a1;
291 			}
292 			if(lastsep==';')
293 				addr1 = a1;
294 			printcom();
295 			continue;
296 
297 		case 'l':
298 			listf++;
299 		case 'p':
300 		case 'P':
301 			newline();
302 			printcom();
303 			continue;
304 
305 		case 'Q':
306 			fchange = 0;
307 		case 'q':
308 			setnoaddr();
309 			newline();
310 			quit();
311 
312 		case 'r':
313 			filename(c);
314 		caseread:
315 			if((io=open(file, OREAD)) < 0) {
316 				lastc = '\n';
317 				error(file);
318 			}
319 			if((d = dirfstat(io)) != nil){
320 				if(d->mode & DMAPPEND)
321 					print("warning: %s is append only\n", file);
322 				free(d);
323 			}
324 			Binit(&iobuf, io, OREAD);
325 			setwide();
326 			squeeze(0);
327 			c = zero != dol;
328 			append(getfile, addr2);
329 			exfile(OREAD);
330 
331 			fchange = c;
332 			continue;
333 
334 		case 's':
335 			nonzero();
336 			substitute(globp != 0);
337 			continue;
338 
339 		case 't':
340 			move(1);
341 			continue;
342 
343 		case 'u':
344 			nonzero();
345 			newline();
346 			if((*addr2&~01) != subnewa)
347 				error(Q);
348 			*addr2 = subolda;
349 			dot = addr2;
350 			continue;
351 
352 		case 'v':
353 			global(0);
354 			continue;
355 
356 		case 'W':
357 			wrapp++;
358 		case 'w':
359 			setwide();
360 			squeeze(dol>zero);
361 			temp = getchr();
362 			if(temp != 'q' && temp != 'Q') {
363 				peekc = temp;
364 				temp = 0;
365 			}
366 			filename(c);
367 			if(!wrapp ||
368 			  ((io = open(file, OWRITE)) == -1) ||
369 			  ((seek(io, 0L, 2)) == -1))
370 				if((io = create(file, OWRITE, 0666)) < 0)
371 					error(file);
372 			Binit(&iobuf, io, OWRITE);
373 			wrapp = 0;
374 			if(dol > zero)
375 				putfile();
376 			exfile(OWRITE);
377 			if(addr1<=zero+1 && addr2==dol)
378 				fchange = 0;
379 			if(temp == 'Q')
380 				fchange = 0;
381 			if(temp)
382 				quit();
383 			continue;
384 
385 		case '=':
386 			setwide();
387 			squeeze(0);
388 			newline();
389 			count = addr2 - zero;
390 			putd();
391 			putchr(L'\n');
392 			continue;
393 
394 		case '!':
395 			callunix();
396 			continue;
397 
398 		case EOF:
399 			return;
400 
401 		}
402 		error(Q);
403 	}
404 }
405 
406 void
407 printcom(void)
408 {
409 	int *a1;
410 
411 	nonzero();
412 	a1 = addr1;
413 	do {
414 		if(listn) {
415 			count = a1-zero;
416 			putd();
417 			putchr(L'\t');
418 		}
419 		putshst(getline(*a1++));
420 	} while(a1 <= addr2);
421 	dot = addr2;
422 	listf = 0;
423 	listn = 0;
424 	pflag = 0;
425 }
426 
427 int*
428 address(void)
429 {
430 	int sign, *a, opcnt, nextopand, *b, c;
431 
432 	nextopand = -1;
433 	sign = 1;
434 	opcnt = 0;
435 	a = dot;
436 	do {
437 		do {
438 			c = getchr();
439 		} while(c == ' ' || c == '\t');
440 		if(c >= '0' && c <= '9') {
441 			peekc = c;
442 			if(!opcnt)
443 				a = zero;
444 			a += sign*getnum();
445 		} else
446 		switch(c) {
447 		case '$':
448 			a = dol;
449 		case '.':
450 			if(opcnt)
451 				error(Q);
452 			break;
453 		case '\'':
454 			c = getchr();
455 			if(opcnt || c < 'a' || c > 'z')
456 				error(Q);
457 			a = zero;
458 			do {
459 				a++;
460 			} while(a <= dol && names[c-'a'] != (*a & ~01));
461 			break;
462 		case '?':
463 			sign = -sign;
464 		case '/':
465 			compile(c);
466 			b = a;
467 			for(;;) {
468 				a += sign;
469 				if(a <= zero)
470 					a = dol;
471 				if(a > dol)
472 					a = zero;
473 				if(match(a))
474 					break;
475 				if(a == b)
476 					error(Q);
477 			}
478 			break;
479 		default:
480 			if(nextopand == opcnt) {
481 				a += sign;
482 				if(a < zero || dol < a)
483 					continue;       /* error(Q); */
484 			}
485 			if(c != '+' && c != '-' && c != '^') {
486 				peekc = c;
487 				if(opcnt == 0)
488 					a = 0;
489 				return a;
490 			}
491 			sign = 1;
492 			if(c != '+')
493 				sign = -sign;
494 			nextopand = ++opcnt;
495 			continue;
496 		}
497 		sign = 1;
498 		opcnt++;
499 	} while(zero <= a && a <= dol);
500 	error(Q);
501 	return 0;
502 }
503 
504 int
505 getnum(void)
506 {
507 	int r, c;
508 
509 	r = 0;
510 	for(;;) {
511 		c = getchr();
512 		if(c < '0' || c > '9')
513 			break;
514 		r = r*10 + (c-'0');
515 	}
516 	peekc = c;
517 	return r;
518 }
519 
520 void
521 setwide(void)
522 {
523 	if(!given) {
524 		addr1 = zero + (dol>zero);
525 		addr2 = dol;
526 	}
527 }
528 
529 void
530 setnoaddr(void)
531 {
532 	if(given)
533 		error(Q);
534 }
535 
536 void
537 nonzero(void)
538 {
539 	squeeze(1);
540 }
541 
542 void
543 squeeze(int i)
544 {
545 	if(addr1 < zero+i || addr2 > dol || addr1 > addr2)
546 		error(Q);
547 }
548 
549 void
550 newline(void)
551 {
552 	int c;
553 
554 	c = getchr();
555 	if(c == '\n' || c == EOF)
556 		return;
557 	if(c == 'p' || c == 'l' || c == 'n') {
558 		pflag++;
559 		if(c == 'l')
560 			listf++;
561 		else
562 		if(c == 'n')
563 			listn++;
564 		c = getchr();
565 		if(c == '\n')
566 			return;
567 	}
568 	error(Q);
569 }
570 
571 void
572 filename(int comm)
573 {
574 	char *p1, *p2;
575 	Rune rune;
576 	int c;
577 
578 	count = 0;
579 	c = getchr();
580 	if(c == '\n' || c == EOF) {
581 		p1 = savedfile;
582 		if(*p1 == 0 && comm != 'f')
583 			error(Q);
584 		p2 = file;
585 		while(*p2++ = *p1++)
586 			;
587 		return;
588 	}
589 	if(c != ' ')
590 		error(Q);
591 	while((c=getchr()) == ' ')
592 		;
593 	if(c == '\n')
594 		error(Q);
595 	p1 = file;
596 	do {
597 		if(p1 >= &file[sizeof(file)-6] || c == ' ' || c == EOF)
598 			error(Q);
599 		rune = c;
600 		p1 += runetochar(p1, &rune);
601 	} while((c=getchr()) != '\n');
602 	*p1 = 0;
603 	if(savedfile[0] == 0 || comm == 'e' || comm == 'f') {
604 		p1 = savedfile;
605 		p2 = file;
606 		while(*p1++ = *p2++)
607 			;
608 	}
609 }
610 
611 void
612 exfile(int om)
613 {
614 
615 	if(om == OWRITE)
616 		if(Bflush(&iobuf) < 0)
617 			error(Q);
618 	close(io);
619 	io = -1;
620 	if(vflag) {
621 		putd();
622 		putchr(L'\n');
623 	}
624 }
625 
626 void
627 error1(char *s)
628 {
629 	int c;
630 
631 	wrapp = 0;
632 	listf = 0;
633 	listn = 0;
634 	count = 0;
635 	seek(0, 0, 2);
636 	pflag = 0;
637 	if(globp)
638 		lastc = '\n';
639 	globp = 0;
640 	peekc = lastc;
641 	if(lastc)
642 		for(;;) {
643 			c = getchr();
644 			if(c == '\n' || c == EOF)
645 				break;
646 		}
647 	if(io > 0) {
648 		close(io);
649 		io = -1;
650 	}
651 	putchr(L'?');
652 	putst(s);
653 }
654 
655 void
656 error(char *s)
657 {
658 	error1(s);
659 	longjmp(savej, 1);
660 }
661 
662 void
663 rescue(void)
664 {
665 	rescuing = 1;
666 	if(dol > zero) {
667 		addr1 = zero+1;
668 		addr2 = dol;
669 		io = create("ed.hup", OWRITE, 0666);
670 		if(io > 0){
671 			Binit(&iobuf, io, OWRITE);
672 			putfile();
673 		}
674 	}
675 	fchange = 0;
676 	quit();
677 }
678 
679 void
680 notifyf(void *a, char *s)
681 {
682 	if(strcmp(s, "interrupt") == 0){
683 		if(rescuing || waiting)
684 			noted(NCONT);
685 		putchr(L'\n');
686 		lastc = '\n';
687 		error1(Q);
688 		notejmp(a, savej, 0);
689 	}
690 	if(strcmp(s, "hangup") == 0){
691 		if(rescuing)
692 			noted(NDFLT);
693 		rescue();
694 	}
695 	fprint(2, "ed: note: %s\n", s);
696 	abort();
697 }
698 
699 int
700 getchr(void)
701 {
702 	if(lastc = peekc) {
703 		peekc = 0;
704 		return lastc;
705 	}
706 	if(globp) {
707 		if((lastc=*globp++) != 0)
708 			return lastc;
709 		globp = 0;
710 		return EOF;
711 	}
712 	lastc = Bgetrune(&bcons);
713 	return lastc;
714 }
715 
716 int
717 gety(void)
718 {
719 	int c;
720 	Rune *gf, *p;
721 
722 	p = linebuf;
723 	gf = globp;
724 	for(;;) {
725 		c = getchr();
726 		if(c == '\n') {
727 			*p = 0;
728 			return 0;
729 		}
730 		if(c == EOF) {
731 			if(gf)
732 				peekc = c;
733 			return c;
734 		}
735 		if(c == 0)
736 			continue;
737 		*p++ = c;
738 		if(p >= &linebuf[LBSIZE-2])
739 			error(Q);
740 	}
741 	return 0;
742 }
743 
744 int
745 gettty(void)
746 {
747 	int rc;
748 
749 	rc = gety();
750 	if(rc)
751 		return rc;
752 	if(linebuf[0] == '.' && linebuf[1] == 0)
753 		return EOF;
754 	return 0;
755 }
756 
757 int
758 getfile(void)
759 {
760 	int c;
761 	Rune *lp;
762 
763 	lp = linebuf;
764 	do {
765 		c = Bgetrune(&iobuf);
766 		if(c < 0) {
767 			if(lp > linebuf) {
768 				putst("'\\n' appended");
769 				c = '\n';
770 			} else
771 				return EOF;
772 		}
773 		if(lp >= &linebuf[LBSIZE]) {
774 			lastc = '\n';
775 			error(Q);
776 		}
777 		*lp++ = c;
778 		count++;
779 	} while(c != '\n');
780 	lp[-1] = 0;
781 	return 0;
782 }
783 
784 void
785 putfile(void)
786 {
787 	int *a1;
788 	Rune *lp;
789 	long c;
790 
791 	a1 = addr1;
792 	do {
793 		lp = getline(*a1++);
794 		for(;;) {
795 			count++;
796 			c = *lp++;
797 			if(c == 0) {
798 				if(Bputrune(&iobuf, '\n') < 0)
799 					error(Q);
800 				break;
801 			}
802 			if(Bputrune(&iobuf, c) < 0)
803 				error(Q);
804 		}
805 	} while(a1 <= addr2);
806 	if(Bflush(&iobuf) < 0)
807 		error(Q);
808 }
809 
810 int
811 append(int (*f)(void), int *a)
812 {
813 	int *a1, *a2, *rdot, nline, tl;
814 
815 	nline = 0;
816 	dot = a;
817 	while((*f)() == 0) {
818 		if((dol-zero) >= nlall) {
819 			nlall += 512;
820 			a1 = realloc(zero, (nlall+5)*sizeof(int*));
821 			if(a1 == 0) {
822 				error("MEM?");
823 				rescue();
824 			}
825 			tl = a1 - zero;	/* relocate pointers */
826 			zero += tl;
827 			addr1 += tl;
828 			addr2 += tl;
829 			dol += tl;
830 			dot += tl;
831 		}
832 		tl = putline();
833 		nline++;
834 		a1 = ++dol;
835 		a2 = a1+1;
836 		rdot = ++dot;
837 		while(a1 > rdot)
838 			*--a2 = *--a1;
839 		*rdot = tl;
840 	}
841 	return nline;
842 }
843 
844 void
845 add(int i)
846 {
847 	if(i && (given || dol > zero)) {
848 		addr1--;
849 		addr2--;
850 	}
851 	squeeze(0);
852 	newline();
853 	append(gettty, addr2);
854 }
855 
856 void
857 browse(void)
858 {
859 	int forward, n;
860 	static bformat, bnum; /* 0 */
861 
862 	forward = 1;
863 	peekc = getchr();
864 	if(peekc != '\n'){
865 		if(peekc == '-' || peekc == '+') {
866 			if(peekc == '-')
867 				forward = 0;
868 			getchr();
869 		}
870 		n = getnum();
871 		if(n > 0)
872 			bpagesize = n;
873 	}
874 	newline();
875 	if(pflag) {
876 		bformat = listf;
877 		bnum = listn;
878 	} else {
879 		listf = bformat;
880 		listn = bnum;
881 	}
882 	if(forward) {
883 		addr1 = addr2;
884 		addr2 += bpagesize;
885 		if(addr2 > dol)
886 			addr2 = dol;
887 	} else {
888 		addr1 = addr2-bpagesize;
889 		if(addr1 <= zero)
890 			addr1 = zero+1;
891 	}
892 	printcom();
893 }
894 
895 void
896 callunix(void)
897 {
898 	int c, pid;
899 	Rune rune;
900 	char buf[512];
901 	char *p;
902 
903 	setnoaddr();
904 	p = buf;
905 	while((c=getchr()) != EOF && c != '\n')
906 		if(p < &buf[sizeof(buf) - 6]) {
907 			rune = c;
908 			p += runetochar(p, &rune);
909 		}
910 	*p = 0;
911 	pid = fork();
912 	if(pid == 0) {
913 		execl("/bin/rc", "rc", "-c", buf, 0);
914 		exits("execl failed");
915 	}
916 	waiting = 1;
917 	while(waitpid() != pid)
918 		;
919 	waiting = 0;
920 	if(vflag)
921 		putst("!");
922 }
923 
924 void
925 quit(void)
926 {
927 	if(vflag && fchange && dol!=zero) {
928 		fchange = 0;
929 		error(Q);
930 	}
931 	remove(tfname);
932 	exits(0);
933 }
934 
935 void
936 onquit(int sig)
937 {
938 	USED(sig);
939 	quit();
940 }
941 
942 void
943 rdelete(int *ad1, int *ad2)
944 {
945 	int *a1, *a2, *a3;
946 
947 	a1 = ad1;
948 	a2 = ad2+1;
949 	a3 = dol;
950 	dol -= a2 - a1;
951 	do {
952 		*a1++ = *a2++;
953 	} while(a2 <= a3);
954 	a1 = ad1;
955 	if(a1 > dol)
956 		a1 = dol;
957 	dot = a1;
958 	fchange = 1;
959 }
960 
961 void
962 gdelete(void)
963 {
964 	int *a1, *a2, *a3;
965 
966 	a3 = dol;
967 	for(a1=zero; (*a1&01)==0; a1++)
968 		if(a1>=a3)
969 			return;
970 	for(a2=a1+1; a2<=a3;) {
971 		if(*a2 & 01) {
972 			a2++;
973 			dot = a1;
974 		} else
975 			*a1++ = *a2++;
976 	}
977 	dol = a1-1;
978 	if(dot > dol)
979 		dot = dol;
980 	fchange = 1;
981 }
982 
983 Rune*
984 getline(int tl)
985 {
986 	Rune *lp, *bp;
987 	int nl;
988 
989 	lp = linebuf;
990 	bp = getblock(tl, OREAD);
991 	nl = nleft;
992 	tl &= ~((BLKSIZE/2) - 1);
993 	while(*lp++ = *bp++) {
994 		nl -= sizeof(Rune);
995 		if(nl == 0) {
996 			bp = getblock(tl += BLKSIZE/2, OREAD);
997 			nl = nleft;
998 		}
999 	}
1000 	return linebuf;
1001 }
1002 
1003 int
1004 putline(void)
1005 {
1006 	Rune *lp, *bp;
1007 	int nl, tl;
1008 
1009 	fchange = 1;
1010 	lp = linebuf;
1011 	tl = tline;
1012 	bp = getblock(tl, OWRITE);
1013 	nl = nleft;
1014 	tl &= ~((BLKSIZE/2)-1);
1015 	while(*bp = *lp++) {
1016 		if(*bp++ == '\n') {
1017 			bp[-1] = 0;
1018 			linebp = lp;
1019 			break;
1020 		}
1021 		nl -= sizeof(Rune);
1022 		if(nl == 0) {
1023 			tl += BLKSIZE/2;
1024 			bp = getblock(tl, OWRITE);
1025 			nl = nleft;
1026 		}
1027 	}
1028 	nl = tline;
1029 	tline += ((lp-linebuf) + 03) & 077776;
1030 	return nl;
1031 }
1032 
1033 void
1034 blkio(int b, uchar *buf, long (*iofcn)(int, void *, long))
1035 {
1036 	seek(tfile, b*BLKSIZE, 0);
1037 	if((*iofcn)(tfile, buf, BLKSIZE) != BLKSIZE) {
1038 		error(T);
1039 	}
1040 }
1041 
1042 Rune*
1043 getblock(int atl, int iof)
1044 {
1045 	int bno, off;
1046 
1047 	static uchar ibuff[BLKSIZE];
1048 	static uchar obuff[BLKSIZE];
1049 
1050 	bno = atl / (BLKSIZE/2);
1051 	off = (atl<<1) & (BLKSIZE-1) & ~03;
1052 	if(bno >= NBLK) {
1053 		lastc = '\n';
1054 		error(T);
1055 	}
1056 	nleft = BLKSIZE - off;
1057 	if(bno == iblock) {
1058 		ichanged |= iof;
1059 		return (Rune*)(ibuff+off);
1060 	}
1061 	if(bno == oblock)
1062 		return (Rune*)(obuff+off);
1063 	if(iof == OREAD) {
1064 		if(ichanged)
1065 			blkio(iblock, ibuff, write);
1066 		ichanged = 0;
1067 		iblock = bno;
1068 		blkio(bno, ibuff, read);
1069 		return (Rune*)(ibuff+off);
1070 	}
1071 	if(oblock >= 0)
1072 		blkio(oblock, obuff, write);
1073 	oblock = bno;
1074 	return (Rune*)(obuff+off);
1075 }
1076 
1077 void
1078 init(void)
1079 {
1080 	int *markp;
1081 
1082 	close(tfile);
1083 	tline = 2;
1084 	for(markp = names; markp < &names[26]; )
1085 		*markp++ = 0;
1086 	subnewa = 0;
1087 	anymarks = 0;
1088 	iblock = -1;
1089 	oblock = -1;
1090 	ichanged = 0;
1091 	if((tfile = create(tfname, ORDWR, 0600)) < 0){
1092 		error1(T);
1093 		exits(0);
1094 	}
1095 	dot = dol = zero;
1096 }
1097 
1098 void
1099 global(int k)
1100 {
1101 	Rune *gp, globuf[GBSIZE];
1102 	int c, *a1;
1103 
1104 	if(globp)
1105 		error(Q);
1106 	setwide();
1107 	squeeze(dol > zero);
1108 	c = getchr();
1109 	if(c == '\n')
1110 		error(Q);
1111 	compile(c);
1112 	gp = globuf;
1113 	while((c=getchr()) != '\n') {
1114 		if(c == EOF)
1115 			error(Q);
1116 		if(c == '\\') {
1117 			c = getchr();
1118 			if(c != '\n')
1119 				*gp++ = '\\';
1120 		}
1121 		*gp++ = c;
1122 		if(gp >= &globuf[GBSIZE-2])
1123 			error(Q);
1124 	}
1125 	if(gp == globuf)
1126 		*gp++ = 'p';
1127 	*gp++ = '\n';
1128 	*gp = 0;
1129 	for(a1=zero; a1<=dol; a1++) {
1130 		*a1 &= ~01;
1131 		if(a1 >= addr1 && a1 <= addr2 && match(a1) == k)
1132 			*a1 |= 01;
1133 	}
1134 
1135 	/*
1136 	 * Special case: g/.../d (avoid n^2 algorithm)
1137 	 */
1138 	if(globuf[0] == 'd' && globuf[1] == '\n' && globuf[2] == 0) {
1139 		gdelete();
1140 		return;
1141 	}
1142 	for(a1=zero; a1<=dol; a1++) {
1143 		if(*a1 & 01) {
1144 			*a1 &= ~01;
1145 			dot = a1;
1146 			globp = globuf;
1147 			commands();
1148 			a1 = zero;
1149 		}
1150 	}
1151 }
1152 
1153 void
1154 join(void)
1155 {
1156 	Rune *gp, *lp;
1157 	int *a1;
1158 
1159 	nonzero();
1160 	gp = genbuf;
1161 	for(a1=addr1; a1<=addr2; a1++) {
1162 		lp = getline(*a1);
1163 		while(*gp = *lp++)
1164 			if(gp++ >= &genbuf[LBSIZE-2])
1165 				error(Q);
1166 	}
1167 	lp = linebuf;
1168 	gp = genbuf;
1169 	while(*lp++ = *gp++)
1170 		;
1171 	*addr1 = putline();
1172 	if(addr1 < addr2)
1173 		rdelete(addr1+1, addr2);
1174 	dot = addr1;
1175 }
1176 
1177 void
1178 substitute(int inglob)
1179 {
1180 	int *mp, *a1, nl, gsubf, n;
1181 
1182 	n = getnum();	/* OK even if n==0 */
1183 	gsubf = compsub();
1184 	for(a1 = addr1; a1 <= addr2; a1++) {
1185 		if(match(a1)){
1186 			int *ozero;
1187 			int m = n;
1188 
1189 			do {
1190 				int span = loc2-loc1;
1191 
1192 				if(--m <= 0) {
1193 					dosub();
1194 					if(!gsubf)
1195 						break;
1196 					if(span == 0) {	/* null RE match */
1197 						if(*loc2 == 0)
1198 							break;
1199 						loc2++;
1200 					}
1201 				}
1202 			} while(match(0));
1203 			if(m <= 0) {
1204 				inglob |= 01;
1205 				subnewa = putline();
1206 				*a1 &= ~01;
1207 				if(anymarks) {
1208 					for(mp=names; mp<&names[26]; mp++)
1209 						if(*mp == *a1)
1210 							*mp = subnewa;
1211 				}
1212 				subolda = *a1;
1213 				*a1 = subnewa;
1214 				ozero = zero;
1215 				nl = append(getsub, a1);
1216 				addr2 += nl;
1217 				nl += zero-ozero;
1218 				a1 += nl;
1219 			}
1220 		}
1221 	}
1222 	if(inglob == 0)
1223 		error(Q);
1224 }
1225 
1226 int
1227 compsub(void)
1228 {
1229 	int seof, c;
1230 	Rune *p;
1231 
1232 	seof = getchr();
1233 	if(seof == '\n' || seof == ' ')
1234 		error(Q);
1235 	compile(seof);
1236 	p = rhsbuf;
1237 	for(;;) {
1238 		c = getchr();
1239 		if(c == '\\') {
1240 			c = getchr();
1241 			*p++ = ESCFLG;
1242 			if(p >= &rhsbuf[LBSIZE/2])
1243 				error(Q);
1244 		} else
1245 		if(c == '\n' && (!globp || !globp[0])) {
1246 			peekc = c;
1247 			pflag++;
1248 			break;
1249 		} else
1250 		if(c == seof)
1251 			break;
1252 		*p++ = c;
1253 		if(p >= &rhsbuf[LBSIZE/2])
1254 			error(Q);
1255 	}
1256 	*p = 0;
1257 	peekc = getchr();
1258 	if(peekc == 'g') {
1259 		peekc = 0;
1260 		newline();
1261 		return 1;
1262 	}
1263 	newline();
1264 	return 0;
1265 }
1266 
1267 int
1268 getsub(void)
1269 {
1270 	Rune *p1, *p2;
1271 
1272 	p1 = linebuf;
1273 	if((p2 = linebp) == 0)
1274 		return EOF;
1275 	while(*p1++ = *p2++)
1276 		;
1277 	linebp = 0;
1278 	return 0;
1279 }
1280 
1281 void
1282 dosub(void)
1283 {
1284 	Rune *lp, *sp, *rp;
1285 	int c, n;
1286 
1287 	lp = linebuf;
1288 	sp = genbuf;
1289 	rp = rhsbuf;
1290 	while(lp < loc1)
1291 		*sp++ = *lp++;
1292 	while(c = *rp++) {
1293 		if(c == '&'){
1294 			sp = place(sp, loc1, loc2);
1295 			continue;
1296 		}
1297 		if(c == ESCFLG && (c = *rp++) >= '1' && c < MAXSUB+'0') {
1298 			n = c-'0';
1299 			if(subexp[n].rsp && subexp[n].rep) {
1300 				sp = place(sp, subexp[n].rsp, subexp[n].rep);
1301 				continue;
1302 			}
1303 			error(Q);
1304 		}
1305 		*sp++ = c;
1306 		if(sp >= &genbuf[LBSIZE])
1307 			error(Q);
1308 	}
1309 	lp = loc2;
1310 	loc2 = sp - genbuf + linebuf;
1311 	while(*sp++ = *lp++)
1312 		if(sp >= &genbuf[LBSIZE])
1313 			error(Q);
1314 	lp = linebuf;
1315 	sp = genbuf;
1316 	while(*lp++ = *sp++)
1317 		;
1318 }
1319 
1320 Rune*
1321 place(Rune *sp, Rune *l1, Rune *l2)
1322 {
1323 
1324 	while(l1 < l2) {
1325 		*sp++ = *l1++;
1326 		if(sp >= &genbuf[LBSIZE])
1327 			error(Q);
1328 	}
1329 	return sp;
1330 }
1331 
1332 void
1333 move(int cflag)
1334 {
1335 	int *adt, *ad1, *ad2;
1336 
1337 	nonzero();
1338 	if((adt = address())==0)	/* address() guarantees addr is in range */
1339 		error(Q);
1340 	newline();
1341 	if(cflag) {
1342 		int *ozero, delta;
1343 		ad1 = dol;
1344 		ozero = zero;
1345 		append(getcopy, ad1++);
1346 		ad2 = dol;
1347 		delta = zero - ozero;
1348 		ad1 += delta;
1349 		adt += delta;
1350 	} else {
1351 		ad2 = addr2;
1352 		for(ad1 = addr1; ad1 <= ad2;)
1353 			*ad1++ &= ~01;
1354 		ad1 = addr1;
1355 	}
1356 	ad2++;
1357 	if(adt<ad1) {
1358 		dot = adt + (ad2-ad1);
1359 		if((++adt)==ad1)
1360 			return;
1361 		reverse(adt, ad1);
1362 		reverse(ad1, ad2);
1363 		reverse(adt, ad2);
1364 	} else
1365 	if(adt >= ad2) {
1366 		dot = adt++;
1367 		reverse(ad1, ad2);
1368 		reverse(ad2, adt);
1369 		reverse(ad1, adt);
1370 	} else
1371 		error(Q);
1372 	fchange = 1;
1373 }
1374 
1375 void
1376 reverse(int *a1, int *a2)
1377 {
1378 	int t;
1379 
1380 	for(;;) {
1381 		t = *--a2;
1382 		if(a2 <= a1)
1383 			return;
1384 		*a2 = *a1;
1385 		*a1++ = t;
1386 	}
1387 }
1388 
1389 int
1390 getcopy(void)
1391 {
1392 	if(addr1 > addr2)
1393 		return EOF;
1394 	getline(*addr1++);
1395 	return 0;
1396 }
1397 
1398 void
1399 compile(int eof)
1400 {
1401 	Rune c;
1402 	char *ep;
1403 	char expbuf[ESIZE];
1404 
1405 	if((c = getchr()) == '\n') {
1406 		peekc = c;
1407 		c = eof;
1408 	}
1409 	if(c == eof) {
1410 		if(!pattern)
1411 			error(Q);
1412 		return;
1413 	}
1414 	if(pattern) {
1415 		free(pattern);
1416 		pattern = 0;
1417 	}
1418 	ep = expbuf;
1419 	do {
1420 		if(c == '\\') {
1421 			if(ep >= expbuf+sizeof(expbuf)) {
1422 				error(Q);
1423 				return;
1424 			}
1425 			ep += runetochar(ep, &c);
1426 			if((c = getchr()) == '\n') {
1427 				error(Q);
1428 				return;
1429 			}
1430 		}
1431 		if(ep >= expbuf+sizeof(expbuf)) {
1432 			error(Q);
1433 			return;
1434 		}
1435 		ep += runetochar(ep, &c);
1436 	} while((c = getchr()) != eof && c != '\n');
1437 	if(c == '\n')
1438 		peekc = c;
1439 	*ep = 0;
1440 	pattern = regcomp(expbuf);
1441 }
1442 
1443 int
1444 match(int *addr)
1445 {
1446 	if(!pattern)
1447 		return 0;
1448 	if(addr){
1449 		if(addr == zero)
1450 			return 0;
1451 		subexp[0].rsp = getline(*addr);
1452 	} else
1453 		subexp[0].rsp = loc2;
1454 	subexp[0].rep = 0;
1455 	if(rregexec(pattern, linebuf, subexp, MAXSUB)) {
1456 		loc1 = subexp[0].rsp;
1457 		loc2 = subexp[0].rep;
1458 		return 1;
1459 	}
1460 	loc1 = loc2 = 0;
1461 	return 0;
1462 
1463 }
1464 
1465 void
1466 putd(void)
1467 {
1468 	int r;
1469 
1470 	r = count%10;
1471 	count /= 10;
1472 	if(count)
1473 		putd();
1474 	putchr(r + L'0');
1475 }
1476 
1477 void
1478 putst(char *sp)
1479 {
1480 	Rune r;
1481 
1482 	col = 0;
1483 	for(;;) {
1484 		sp += chartorune(&r, sp);
1485 		if(r == 0)
1486 			break;
1487 		putchr(r);
1488 	}
1489 	putchr(L'\n');
1490 }
1491 
1492 void
1493 putshst(Rune *sp)
1494 {
1495 	col = 0;
1496 	while(*sp)
1497 		putchr(*sp++);
1498 	putchr(L'\n');
1499 }
1500 
1501 void
1502 putchr(int ac)
1503 {
1504 	char *lp;
1505 	int c;
1506 	Rune rune;
1507 
1508 	lp = linp;
1509 	c = ac;
1510 	if(listf) {
1511 		if(c == '\n') {
1512 			if(linp != line && linp[-1] == ' ') {
1513 				*lp++ = '\\';
1514 				*lp++ = 'n';
1515 			}
1516 		} else {
1517 			if(col > (72-6-2)) {
1518 				col = 8;
1519 				*lp++ = '\\';
1520 				*lp++ = '\n';
1521 				*lp++ = '\t';
1522 			}
1523 			col++;
1524 			if(c=='\b' || c=='\t' || c=='\\') {
1525 				*lp++ = '\\';
1526 				if(c == '\b')
1527 					c = 'b';
1528 				else
1529 				if(c == '\t')
1530 					c = 't';
1531 				col++;
1532 			} else
1533 			if(c<' ' || c>='\177') {
1534 				*lp++ = '\\';
1535 				*lp++ = 'x';
1536 				*lp++ =  hex[c>>12];
1537 				*lp++ =  hex[c>>8&0xF];
1538 				*lp++ =  hex[c>>4&0xF];
1539 				c     =  hex[c&0xF];
1540 				col += 5;
1541 			}
1542 		}
1543 	}
1544 
1545 	rune = c;
1546 	lp += runetochar(lp, &rune);
1547 
1548 	if(c == '\n' || lp >= &line[sizeof(line)-5]) {
1549 		linp = line;
1550 		write(oflag? 2: 1, line, lp-line);
1551 		return;
1552 	}
1553 	linp = lp;
1554 }
1555 
1556 char*
1557 mktemp(char *as)
1558 {
1559 	char *s;
1560 	unsigned pid;
1561 	int i;
1562 
1563 	pid = getpid();
1564 	s = as;
1565 	while(*s++)
1566 		;
1567 	s--;
1568 	while(*--s == 'X') {
1569 		*s = pid % 10 + '0';
1570 		pid /= 10;
1571 	}
1572 	s++;
1573 	i = 'a';
1574 	while(access(as, 0) != -1) {
1575 		if(i == 'z')
1576 			return "/";
1577 		*s = i++;
1578 	}
1579 	return as;
1580 }
1581 
1582 void
1583 regerror(char *s)
1584 {
1585 	USED(s);
1586 	error(Q);
1587 }
1588