10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*13093SRoger.Faulkner@Oracle.COM
22597Sceastha /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24597Sceastha */
25597Sceastha
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate
300Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "ex.h"
330Sstevel@tonic-gate #include "ex_temp.h"
340Sstevel@tonic-gate #include "ex_vis.h"
350Sstevel@tonic-gate #include "ex_tty.h"
36802Scf46844 #include <unistd.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * Editor temporary file routines.
400Sstevel@tonic-gate * Very similar to those of ed, except uses 2 input buffers.
410Sstevel@tonic-gate */
420Sstevel@tonic-gate #define READ 0
430Sstevel@tonic-gate #define WRITE 1
440Sstevel@tonic-gate
450Sstevel@tonic-gate unsigned char tfname[PATH_MAX+1];
460Sstevel@tonic-gate static unsigned char rfname[PATH_MAX+1];
470Sstevel@tonic-gate static unsigned char tempname[PATH_MAX+1];
480Sstevel@tonic-gate int havetmp;
490Sstevel@tonic-gate short tfile = -1;
500Sstevel@tonic-gate static short rfile = -1;
510Sstevel@tonic-gate
52597Sceastha extern int junk();
53597Sceastha
54802Scf46844 void
fileinit(void)55802Scf46844 fileinit(void)
560Sstevel@tonic-gate {
57802Scf46844 unsigned char *p;
58802Scf46844 pid_t j;
59802Scf46844 int i;
600Sstevel@tonic-gate struct stat64 stbuf;
610Sstevel@tonic-gate
620Sstevel@tonic-gate if (tline == INCRMT * (HBLKS+2))
630Sstevel@tonic-gate return;
640Sstevel@tonic-gate cleanup(0);
650Sstevel@tonic-gate if (tfile != -1)
660Sstevel@tonic-gate close(tfile);
670Sstevel@tonic-gate tline = INCRMT * (HBLKS+2);
680Sstevel@tonic-gate blocks[0] = HBLKS;
690Sstevel@tonic-gate blocks[1] = HBLKS+1;
700Sstevel@tonic-gate blocks[2] = -1;
710Sstevel@tonic-gate dirtcnt = 0;
720Sstevel@tonic-gate iblock = -1;
730Sstevel@tonic-gate iblock2 = -1;
740Sstevel@tonic-gate oblock = -1;
750Sstevel@tonic-gate if (strlen(svalue(vi_DIRECTORY)) > (PATH_MAX -13))
760Sstevel@tonic-gate error(gettext("User set directory too long"));
770Sstevel@tonic-gate CP(tfname, svalue(vi_DIRECTORY));
780Sstevel@tonic-gate if (stat64((char *)tfname, &stbuf)) {
790Sstevel@tonic-gate dumbness:
800Sstevel@tonic-gate if (setexit() == 0)
810Sstevel@tonic-gate filioerr(tfname);
820Sstevel@tonic-gate else
830Sstevel@tonic-gate putNFL();
840Sstevel@tonic-gate cleanup(1);
850Sstevel@tonic-gate exit(++errcnt);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate if (!ISDIR(stbuf)) {
880Sstevel@tonic-gate errno = ENOTDIR;
890Sstevel@tonic-gate goto dumbness;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate CP(tempname, tfname);
920Sstevel@tonic-gate ichanged = 0;
930Sstevel@tonic-gate ichang2 = 0;
940Sstevel@tonic-gate (void) strcat(tfname, "/ExXXXXXX");
950Sstevel@tonic-gate if ((tfile = mkstemp((char *)tfname)) < 0)
960Sstevel@tonic-gate goto dumbness;
970Sstevel@tonic-gate #ifdef VMUNIX
980Sstevel@tonic-gate {
99802Scf46844 extern int stilinc; /* see below */
1000Sstevel@tonic-gate stilinc = 0;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate #endif
1030Sstevel@tonic-gate havetmp = 1;
1040Sstevel@tonic-gate /* brk((unsigned char *)fendcore); */
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
107802Scf46844 void
cleanup(bool all)108802Scf46844 cleanup(bool all)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate pid_t pgrp;
1110Sstevel@tonic-gate if (all) {
1120Sstevel@tonic-gate if (kflag)
1130Sstevel@tonic-gate crypt_close(perm);
1140Sstevel@tonic-gate if (xtflag)
1150Sstevel@tonic-gate crypt_close(tperm);
116802Scf46844 putpad((unsigned char *)exit_ca_mode);
1170Sstevel@tonic-gate flush();
1180Sstevel@tonic-gate if (ioctl(2, TIOCGPGRP, &pgrp) == 0) {
1190Sstevel@tonic-gate if (pgrp == getpgid(0)) {
1200Sstevel@tonic-gate #ifdef XPG4
1210Sstevel@tonic-gate if (envlines != -1 || envcolumns != -1) {
1220Sstevel@tonic-gate struct winsize jwin;
1230Sstevel@tonic-gate jwin.ws_row = oldlines;
1240Sstevel@tonic-gate jwin.ws_col = oldcolumns;
1250Sstevel@tonic-gate ioctl(0, TIOCSWINSZ, &jwin);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate #endif /* XPG4 */
1280Sstevel@tonic-gate resetterm();
1290Sstevel@tonic-gate normtty--;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate } else {
1320Sstevel@tonic-gate #ifdef XPG4
1330Sstevel@tonic-gate if (envlines != -1 || envcolumns != -1) {
1340Sstevel@tonic-gate struct winsize jwin;
1350Sstevel@tonic-gate jwin.ws_row = oldlines;
1360Sstevel@tonic-gate jwin.ws_col = oldcolumns;
1370Sstevel@tonic-gate ioctl(0, TIOCSWINSZ, &jwin);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate #endif /* XPG4 */
1400Sstevel@tonic-gate resetterm();
1410Sstevel@tonic-gate normtty--;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate if (havetmp)
145802Scf46844 unlink((char *)tfname);
1460Sstevel@tonic-gate havetmp = 0;
1470Sstevel@tonic-gate if (all && rfile >= 0) {
148802Scf46844 unlink((char *)rfname);
1490Sstevel@tonic-gate close(rfile);
1500Sstevel@tonic-gate rfile = -1;
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate if (all == 1)
1530Sstevel@tonic-gate exit(errcnt);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
156802Scf46844 void
getaline(line tl)157*13093SRoger.Faulkner@Oracle.COM getaline(line tl)
1580Sstevel@tonic-gate {
159802Scf46844 unsigned char *bp, *lp;
160802Scf46844 int nl;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate lp = linebuf;
1630Sstevel@tonic-gate bp = getblock(tl, READ);
1640Sstevel@tonic-gate nl = nleft;
1650Sstevel@tonic-gate tl &= ~OFFMSK;
1660Sstevel@tonic-gate while (*lp++ = *bp++)
1670Sstevel@tonic-gate if (--nl == 0) {
1680Sstevel@tonic-gate bp = getblock(tl += INCRMT, READ);
1690Sstevel@tonic-gate nl = nleft;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
173597Sceastha int
putline(void)174597Sceastha putline(void)
1750Sstevel@tonic-gate {
176597Sceastha unsigned char *bp, *lp;
177597Sceastha unsigned char tmpbp;
178597Sceastha int nl;
1790Sstevel@tonic-gate line tl;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate dirtcnt++;
1820Sstevel@tonic-gate lp = linebuf;
1830Sstevel@tonic-gate change();
1840Sstevel@tonic-gate tl = tline;
1850Sstevel@tonic-gate bp = getblock(tl, WRITE);
1860Sstevel@tonic-gate nl = nleft;
1870Sstevel@tonic-gate tl &= ~OFFMSK;
1880Sstevel@tonic-gate while (*bp = *lp++) {
189597Sceastha tmpbp = *bp;
190597Sceastha if (tmpbp == '\n') {
191597Sceastha *bp = 0;
1920Sstevel@tonic-gate linebp = lp;
1930Sstevel@tonic-gate break;
194597Sceastha } else if (junk(*bp++)) {
195802Scf46844 checkjunk(tmpbp);
196597Sceastha *--bp;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate if (--nl == 0) {
1990Sstevel@tonic-gate bp = getblock(tl += INCRMT, WRITE);
2000Sstevel@tonic-gate nl = nleft;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate tl = tline;
2040Sstevel@tonic-gate tline += (((lp - linebuf) + BNDRY - 1) >> SHFT) & 077776;
2050Sstevel@tonic-gate return (tl);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate int read();
2090Sstevel@tonic-gate int write();
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate unsigned char *
getblock(atl,iof)2120Sstevel@tonic-gate getblock(atl, iof)
2130Sstevel@tonic-gate line atl;
2140Sstevel@tonic-gate int iof;
2150Sstevel@tonic-gate {
216802Scf46844 int bno, off;
217802Scf46844 unsigned char *p1, *p2;
218802Scf46844 int n;
2190Sstevel@tonic-gate line *tmpptr;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate bno = (atl >> OFFBTS) & BLKMSK;
2220Sstevel@tonic-gate off = (atl << SHFT) & LBTMSK;
2230Sstevel@tonic-gate if (bno >= NMBLKS) {
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * When we overflow tmpfile buffers,
2260Sstevel@tonic-gate * throw away line which could not be
2270Sstevel@tonic-gate * put into buffer.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate for (tmpptr = dot; tmpptr < unddol; tmpptr++)
2300Sstevel@tonic-gate *tmpptr = *(tmpptr+1);
2310Sstevel@tonic-gate if (dot == dol)
2320Sstevel@tonic-gate dot--;
2330Sstevel@tonic-gate dol--;
2340Sstevel@tonic-gate unddol--;
2350Sstevel@tonic-gate error(gettext(" Tmp file too large"));
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate nleft = BUFSIZE - off;
2380Sstevel@tonic-gate if (bno == iblock) {
2390Sstevel@tonic-gate ichanged |= iof;
2400Sstevel@tonic-gate hitin2 = 0;
2410Sstevel@tonic-gate return (ibuff + off);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate if (bno == iblock2) {
2440Sstevel@tonic-gate ichang2 |= iof;
2450Sstevel@tonic-gate hitin2 = 1;
2460Sstevel@tonic-gate return (ibuff2 + off);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate if (bno == oblock)
2490Sstevel@tonic-gate return (obuff + off);
2500Sstevel@tonic-gate if (iof == READ) {
2510Sstevel@tonic-gate if (hitin2 == 0) {
2520Sstevel@tonic-gate if (ichang2) {
2530Sstevel@tonic-gate if (xtflag)
2540Sstevel@tonic-gate if (run_crypt(0L, ibuff2,
2550Sstevel@tonic-gate CRSIZE, tperm) == -1)
2560Sstevel@tonic-gate filioerr(tfname);
2570Sstevel@tonic-gate blkio(iblock2, ibuff2, write);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate ichang2 = 0;
2600Sstevel@tonic-gate iblock2 = bno;
2610Sstevel@tonic-gate blkio(bno, ibuff2, read);
2620Sstevel@tonic-gate if (xtflag)
2630Sstevel@tonic-gate if (run_crypt(0L, ibuff2, CRSIZE, tperm) == -1)
2640Sstevel@tonic-gate filioerr(tfname);
2650Sstevel@tonic-gate hitin2 = 1;
2660Sstevel@tonic-gate return (ibuff2 + off);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate hitin2 = 0;
2690Sstevel@tonic-gate if (ichanged) {
2700Sstevel@tonic-gate if (xtflag)
2710Sstevel@tonic-gate if (run_crypt(0L, ibuff, CRSIZE, tperm) == -1)
2720Sstevel@tonic-gate filioerr(tfname);
2730Sstevel@tonic-gate blkio(iblock, ibuff, write);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate ichanged = 0;
2760Sstevel@tonic-gate iblock = bno;
2770Sstevel@tonic-gate blkio(bno, ibuff, read);
2780Sstevel@tonic-gate if (xtflag)
2790Sstevel@tonic-gate if (run_crypt(0L, ibuff, CRSIZE, tperm) == -1)
2800Sstevel@tonic-gate filioerr(tfname);
2810Sstevel@tonic-gate return (ibuff + off);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate if (oblock >= 0) {
2840Sstevel@tonic-gate if (xtflag) {
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate * Encrypt block before writing, so some devious
2870Sstevel@tonic-gate * person can't look at temp file while editing.
2880Sstevel@tonic-gate */
2890Sstevel@tonic-gate p1 = obuff;
2900Sstevel@tonic-gate p2 = crbuf;
2910Sstevel@tonic-gate n = CRSIZE;
2920Sstevel@tonic-gate while (n--)
2930Sstevel@tonic-gate *p2++ = *p1++;
2940Sstevel@tonic-gate if (run_crypt(0L, crbuf, CRSIZE, tperm) == -1)
2950Sstevel@tonic-gate filioerr(tfname);
2960Sstevel@tonic-gate blkio(oblock, crbuf, write);
2970Sstevel@tonic-gate } else
2980Sstevel@tonic-gate blkio(oblock, obuff, write);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate oblock = bno;
3010Sstevel@tonic-gate return (obuff + off);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate #ifdef VMUNIX
3050Sstevel@tonic-gate #define INCORB 64
3060Sstevel@tonic-gate unsigned char incorb[INCORB+1][BUFSIZE];
3070Sstevel@tonic-gate #define pagrnd(a) ((unsigned char *)(((int)a)&~(BUFSIZE-1)))
3080Sstevel@tonic-gate int stilinc; /* up to here not written yet */
3090Sstevel@tonic-gate #endif
3100Sstevel@tonic-gate
311802Scf46844 void
blkio(short b,unsigned char * buf,int (* iofcn)())312802Scf46844 blkio(short b, unsigned char *buf, int (*iofcn)())
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate #ifdef VMUNIX
3160Sstevel@tonic-gate if (b < INCORB) {
3170Sstevel@tonic-gate if (iofcn == read) {
3180Sstevel@tonic-gate bcopy(pagrnd(incorb[b+1]), buf, BUFSIZE);
3190Sstevel@tonic-gate return;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate bcopy(buf, pagrnd(incorb[b+1]), BUFSIZE);
3220Sstevel@tonic-gate if (laste) {
3230Sstevel@tonic-gate if (b >= stilinc)
3240Sstevel@tonic-gate stilinc = b + 1;
3250Sstevel@tonic-gate return;
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate } else if (stilinc)
3280Sstevel@tonic-gate tflush();
3290Sstevel@tonic-gate #endif
3300Sstevel@tonic-gate lseek(tfile, (long)(unsigned)b * BUFSIZE, 0);
3310Sstevel@tonic-gate if ((*iofcn)(tfile, buf, BUFSIZE) != BUFSIZE)
3320Sstevel@tonic-gate filioerr(tfname);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate #ifdef VMUNIX
336802Scf46844 void
tlaste(void)337802Scf46844 tlaste(void)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate if (stilinc)
3410Sstevel@tonic-gate dirtcnt = 0;
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
344802Scf46844 void
tflush(void)345802Scf46844 tflush(void)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate int i = stilinc;
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate stilinc = 0;
3500Sstevel@tonic-gate lseek(tfile, (long)0, 0);
3510Sstevel@tonic-gate if (write(tfile, pagrnd(incorb[1]), i * BUFSIZE) != (i * BUFSIZE))
3520Sstevel@tonic-gate filioerr(tfname);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate #endif
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * Synchronize the state of the temporary file in case
3580Sstevel@tonic-gate * a crash occurs.
3590Sstevel@tonic-gate */
360802Scf46844 void
synctmp(void)361802Scf46844 synctmp(void)
3620Sstevel@tonic-gate {
363802Scf46844 int cnt;
364802Scf46844 line *a;
365802Scf46844 short *bp;
366802Scf46844 unsigned char *p1, *p2;
367802Scf46844 int n;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate #ifdef VMUNIX
3700Sstevel@tonic-gate if (stilinc)
3710Sstevel@tonic-gate return;
3720Sstevel@tonic-gate #endif
3730Sstevel@tonic-gate if (dol == zero)
3740Sstevel@tonic-gate return;
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * In theory, we need to encrypt iblock and iblock2 before writing
3770Sstevel@tonic-gate * them out, as well as oblock, but in practice ichanged and ichang2
3780Sstevel@tonic-gate * can never be set, so this isn't really needed. Likewise, the
3790Sstevel@tonic-gate * code in getblock above for iblock+iblock2 isn't needed.
3800Sstevel@tonic-gate */
3810Sstevel@tonic-gate if (ichanged)
3820Sstevel@tonic-gate blkio(iblock, ibuff, write);
3830Sstevel@tonic-gate ichanged = 0;
3840Sstevel@tonic-gate if (ichang2)
3850Sstevel@tonic-gate blkio(iblock2, ibuff2, write);
3860Sstevel@tonic-gate ichang2 = 0;
3870Sstevel@tonic-gate if (oblock != -1)
3880Sstevel@tonic-gate if (xtflag) {
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * Encrypt block before writing, so some devious
3910Sstevel@tonic-gate * person can't look at temp file while editing.
3920Sstevel@tonic-gate */
3930Sstevel@tonic-gate p1 = obuff;
3940Sstevel@tonic-gate p2 = crbuf;
3950Sstevel@tonic-gate n = CRSIZE;
3960Sstevel@tonic-gate while (n--)
3970Sstevel@tonic-gate *p2++ = *p1++;
3980Sstevel@tonic-gate if (run_crypt(0L, crbuf, CRSIZE, tperm) == -1)
3990Sstevel@tonic-gate filioerr(tfname);
4000Sstevel@tonic-gate blkio(oblock, crbuf, write);
4010Sstevel@tonic-gate } else
4020Sstevel@tonic-gate blkio(oblock, obuff, write);
4030Sstevel@tonic-gate time(&H.Time);
4040Sstevel@tonic-gate uid = getuid();
4050Sstevel@tonic-gate if (xtflag)
4060Sstevel@tonic-gate H.encrypted = 1;
4070Sstevel@tonic-gate else
4080Sstevel@tonic-gate H.encrypted = 0;
4090Sstevel@tonic-gate *zero = (line) H.Time;
4100Sstevel@tonic-gate for (a = zero, bp = blocks; a <= dol;
411597Sceastha a += BUFSIZE / sizeof (*a), bp++) {
4120Sstevel@tonic-gate if (bp >= &H.Blocks[LBLKS-1])
4130Sstevel@tonic-gate error(gettext(
414597Sceastha "file too large to recover with -r option"));
4150Sstevel@tonic-gate if (*bp < 0) {
4160Sstevel@tonic-gate tline = (tline + OFFMSK) &~ OFFMSK;
4170Sstevel@tonic-gate *bp = ((tline >> OFFBTS) & BLKMSK);
4180Sstevel@tonic-gate if (*bp > NMBLKS)
4190Sstevel@tonic-gate error(gettext(" Tmp file too large"));
4200Sstevel@tonic-gate tline += INCRMT;
4210Sstevel@tonic-gate oblock = *bp + 1;
4220Sstevel@tonic-gate bp[1] = -1;
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate lseek(tfile, (long)(unsigned)*bp * BUFSIZE, 0);
4250Sstevel@tonic-gate cnt = ((dol - a) + 2) * sizeof (line);
4260Sstevel@tonic-gate if (cnt > BUFSIZE)
4270Sstevel@tonic-gate cnt = BUFSIZE;
4280Sstevel@tonic-gate if (write(tfile, (char *)a, cnt) != cnt) {
4290Sstevel@tonic-gate oops:
4300Sstevel@tonic-gate *zero = 0;
4310Sstevel@tonic-gate filioerr(tfname);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate *zero = 0;
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate flines = lineDOL();
4360Sstevel@tonic-gate lseek(tfile, 0l, 0);
4370Sstevel@tonic-gate if (write(tfile, (char *)&H, sizeof (H)) != sizeof (H))
4380Sstevel@tonic-gate goto oops;
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
441802Scf46844 void
TSYNC(void)442802Scf46844 TSYNC(void)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate if (dirtcnt > MAXDIRT) {
4460Sstevel@tonic-gate #ifdef VMUNIX
4470Sstevel@tonic-gate if (stilinc)
4480Sstevel@tonic-gate tflush();
4490Sstevel@tonic-gate #endif
4500Sstevel@tonic-gate dirtcnt = 0;
4510Sstevel@tonic-gate synctmp();
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * Named buffer routines.
4570Sstevel@tonic-gate * These are implemented differently than the main buffer.
4580Sstevel@tonic-gate * Each named buffer has a chain of blocks in the register file.
4590Sstevel@tonic-gate * Each block contains roughly 508 chars of text,
4600Sstevel@tonic-gate * and a previous and next block number. We also have information
4610Sstevel@tonic-gate * about which blocks came from deletes of multiple partial lines,
4620Sstevel@tonic-gate * e.g. deleting a sentence or a LISP object.
4630Sstevel@tonic-gate *
4640Sstevel@tonic-gate * We maintain a free map for the temp file. To free the blocks
4650Sstevel@tonic-gate * in a register we must read the blocks to find how they are chained
4660Sstevel@tonic-gate * together.
4670Sstevel@tonic-gate *
4680Sstevel@tonic-gate * BUG: The default savind of deleted lines in numbered
4690Sstevel@tonic-gate * buffers may be rather inefficient; it hasn't been profiled.
4700Sstevel@tonic-gate */
4710Sstevel@tonic-gate struct strreg {
4720Sstevel@tonic-gate short rg_flags;
4730Sstevel@tonic-gate short rg_nleft;
4740Sstevel@tonic-gate short rg_first;
4750Sstevel@tonic-gate short rg_last;
4760Sstevel@tonic-gate } strregs[('z'-'a'+1) + ('9'-'0'+1)], *strp;
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate struct rbuf {
4790Sstevel@tonic-gate short rb_prev;
4800Sstevel@tonic-gate short rb_next;
4810Sstevel@tonic-gate unsigned char rb_text[BUFSIZE - 2 * sizeof (short)];
4820Sstevel@tonic-gate } *rbuf, KILLrbuf, putrbuf, YANKrbuf, regrbuf;
4830Sstevel@tonic-gate #ifdef VMUNIX
4840Sstevel@tonic-gate short rused[256];
4850Sstevel@tonic-gate #else
4860Sstevel@tonic-gate short rused[32];
4870Sstevel@tonic-gate #endif
4880Sstevel@tonic-gate short rnleft;
4890Sstevel@tonic-gate short rblock;
4900Sstevel@tonic-gate short rnext;
4910Sstevel@tonic-gate unsigned char *rbufcp;
4920Sstevel@tonic-gate
493802Scf46844 void
regio(short b,int (* iofcn)())494802Scf46844 regio(short b, int (*iofcn)())
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate if (rfile == -1) {
4980Sstevel@tonic-gate CP(rfname, tempname);
4990Sstevel@tonic-gate (void) strcat(rfname, "/RxXXXXXX");
5000Sstevel@tonic-gate if ((rfile = mkstemp((char *)rfname)) < 0)
5010Sstevel@tonic-gate filioerr(rfname);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate lseek(rfile, (long)b * BUFSIZE, 0);
5040Sstevel@tonic-gate if ((*iofcn)(rfile, rbuf, BUFSIZE) != BUFSIZE)
5050Sstevel@tonic-gate filioerr(rfname);
5060Sstevel@tonic-gate rblock = b;
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate
509802Scf46844 int
REGblk(void)510802Scf46844 REGblk(void)
5110Sstevel@tonic-gate {
512802Scf46844 int i, j, m;
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate for (i = 0; i < sizeof (rused) / sizeof (rused[0]); i++) {
5150Sstevel@tonic-gate m = (rused[i] ^ 0177777) & 0177777;
5160Sstevel@tonic-gate if (i == 0)
5170Sstevel@tonic-gate m &= ~1;
5180Sstevel@tonic-gate if (m != 0) {
5190Sstevel@tonic-gate j = 0;
5200Sstevel@tonic-gate while ((m & 1) == 0)
5210Sstevel@tonic-gate j++, m >>= 1;
5220Sstevel@tonic-gate rused[i] |= (1 << j);
5230Sstevel@tonic-gate #ifdef RDEBUG
524802Scf46844 viprintf("allocating block %d\n", i * 16 + j);
5250Sstevel@tonic-gate #endif
5260Sstevel@tonic-gate return (i * 16 + j);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate error(gettext("Out of register space (ugh)"));
5300Sstevel@tonic-gate /*NOTREACHED*/
531802Scf46844 return (0);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate struct strreg *
mapreg(c)5350Sstevel@tonic-gate mapreg(c)
536802Scf46844 int c;
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate if (isupper(c))
5400Sstevel@tonic-gate c = tolower(c);
5410Sstevel@tonic-gate return (isdigit(c) ? &strregs[('z'-'a'+1)+(c-'0')] : &strregs[c-'a']);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate int shread();
5450Sstevel@tonic-gate
546802Scf46844 void
KILLreg(int c)547802Scf46844 KILLreg(int c)
5480Sstevel@tonic-gate {
549802Scf46844 struct strreg *sp;
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate rbuf = &KILLrbuf;
5520Sstevel@tonic-gate sp = mapreg(c);
5530Sstevel@tonic-gate rblock = sp->rg_first;
5540Sstevel@tonic-gate sp->rg_first = sp->rg_last = 0;
5550Sstevel@tonic-gate sp->rg_flags = sp->rg_nleft = 0;
5560Sstevel@tonic-gate while (rblock != 0) {
5570Sstevel@tonic-gate #ifdef RDEBUG
558802Scf46844 viprintf("freeing block %d\n", rblock);
5590Sstevel@tonic-gate #endif
5600Sstevel@tonic-gate rused[rblock / 16] &= ~(1 << (rblock % 16));
5610Sstevel@tonic-gate regio(rblock, shread);
5620Sstevel@tonic-gate rblock = rbuf->rb_next;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate /*VARARGS*/
567802Scf46844 int
shread(void)568802Scf46844 shread(void)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate struct front { short a; short b; };
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate if (read(rfile, (char *)rbuf, sizeof (struct front)) ==
573597Sceastha sizeof (struct front))
5740Sstevel@tonic-gate return (sizeof (struct rbuf));
5750Sstevel@tonic-gate return (0);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate int getREG();
5790Sstevel@tonic-gate
580802Scf46844 int
putreg(unsigned char c)581802Scf46844 putreg(unsigned char c)
5820Sstevel@tonic-gate {
583802Scf46844 line *odot = dot;
584802Scf46844 line *odol = dol;
585802Scf46844 int cnt;
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate deletenone();
5880Sstevel@tonic-gate appendnone();
5890Sstevel@tonic-gate rbuf = &putrbuf;
5900Sstevel@tonic-gate rnleft = 0;
5910Sstevel@tonic-gate rblock = 0;
5920Sstevel@tonic-gate rnext = mapreg(c)->rg_first;
5930Sstevel@tonic-gate if (rnext == 0) {
5940Sstevel@tonic-gate if (inopen) {
5950Sstevel@tonic-gate splitw++;
5960Sstevel@tonic-gate vclean();
5970Sstevel@tonic-gate vgoto(WECHO, 0);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate vreg = -1;
6000Sstevel@tonic-gate error(gettext("Nothing in register %c"), c);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate if (inopen && partreg(c)) {
6030Sstevel@tonic-gate if (!FIXUNDO) {
6040Sstevel@tonic-gate splitw++; vclean(); vgoto(WECHO, 0); vreg = -1;
6050Sstevel@tonic-gate error(gettext("Can't put partial line inside macro"));
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate squish();
6080Sstevel@tonic-gate addr1 = addr2 = dol;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate cnt = append(getREG, addr2);
6110Sstevel@tonic-gate if (inopen && partreg(c)) {
6120Sstevel@tonic-gate unddol = dol;
6130Sstevel@tonic-gate dol = odol;
6140Sstevel@tonic-gate dot = odot;
6150Sstevel@tonic-gate pragged(0);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate killcnt(cnt);
6180Sstevel@tonic-gate notecnt = cnt;
619802Scf46844 return (0);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate
622802Scf46844 short
partreg(unsigned char c)623802Scf46844 partreg(unsigned char c)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate return (mapreg(c)->rg_flags);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate
629802Scf46844 void
notpart(int c)630802Scf46844 notpart(int c)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate if (c)
6340Sstevel@tonic-gate mapreg(c)->rg_flags = 0;
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate
637802Scf46844 int
getREG(void)638802Scf46844 getREG(void)
6390Sstevel@tonic-gate {
640802Scf46844 unsigned char *lp = linebuf;
641802Scf46844 int c;
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate for (;;) {
6440Sstevel@tonic-gate if (rnleft == 0) {
6450Sstevel@tonic-gate if (rnext == 0)
6460Sstevel@tonic-gate return (EOF);
6470Sstevel@tonic-gate regio(rnext, read);
6480Sstevel@tonic-gate rnext = rbuf->rb_next;
6490Sstevel@tonic-gate rbufcp = rbuf->rb_text;
6500Sstevel@tonic-gate rnleft = sizeof (rbuf->rb_text);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate c = *rbufcp;
6530Sstevel@tonic-gate if (c == 0)
6540Sstevel@tonic-gate return (EOF);
6550Sstevel@tonic-gate rbufcp++, --rnleft;
6560Sstevel@tonic-gate if (c == '\n') {
6570Sstevel@tonic-gate *lp++ = 0;
6580Sstevel@tonic-gate return (0);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate *lp++ = c;
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
664802Scf46844 int
YANKreg(int c)665802Scf46844 YANKreg(int c)
6660Sstevel@tonic-gate {
667802Scf46844 line *addr;
668802Scf46844 struct strreg *sp;
6690Sstevel@tonic-gate unsigned char savelb[LBSIZE];
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate if (isdigit(c))
6720Sstevel@tonic-gate kshift();
6730Sstevel@tonic-gate if (islower(c))
6740Sstevel@tonic-gate KILLreg(c);
6750Sstevel@tonic-gate strp = sp = mapreg(c);
6760Sstevel@tonic-gate sp->rg_flags = inopen && cursor && wcursor;
6770Sstevel@tonic-gate rbuf = &YANKrbuf;
6780Sstevel@tonic-gate if (sp->rg_last) {
6790Sstevel@tonic-gate regio(sp->rg_last, read);
6800Sstevel@tonic-gate rnleft = sp->rg_nleft;
6810Sstevel@tonic-gate rbufcp = &rbuf->rb_text[sizeof (rbuf->rb_text) - rnleft];
6820Sstevel@tonic-gate } else {
6830Sstevel@tonic-gate rblock = 0;
6840Sstevel@tonic-gate rnleft = 0;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate CP(savelb, linebuf);
6870Sstevel@tonic-gate for (addr = addr1; addr <= addr2; addr++) {
688*13093SRoger.Faulkner@Oracle.COM getaline(*addr);
6890Sstevel@tonic-gate if (sp->rg_flags) {
6900Sstevel@tonic-gate if (addr == addr2)
6910Sstevel@tonic-gate *wcursor = 0;
6920Sstevel@tonic-gate if (addr == addr1)
6930Sstevel@tonic-gate strcpy(linebuf, cursor);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate YANKline();
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate rbflush();
6980Sstevel@tonic-gate killed();
6990Sstevel@tonic-gate CP(linebuf, savelb);
700802Scf46844 return (0);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
703802Scf46844 void
kshift(void)704802Scf46844 kshift(void)
7050Sstevel@tonic-gate {
706802Scf46844 int i;
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate KILLreg('9');
7090Sstevel@tonic-gate for (i = '8'; i >= '0'; i--)
7100Sstevel@tonic-gate copy(mapreg(i+1), mapreg(i), sizeof (struct strreg));
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate
713802Scf46844 void
YANKline(void)714802Scf46844 YANKline(void)
7150Sstevel@tonic-gate {
716802Scf46844 unsigned char *lp = linebuf;
717802Scf46844 struct rbuf *rp = rbuf;
718802Scf46844 int c;
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate do {
7210Sstevel@tonic-gate c = *lp++;
7220Sstevel@tonic-gate if (c == 0)
7230Sstevel@tonic-gate c = '\n';
7240Sstevel@tonic-gate if (rnleft == 0) {
7250Sstevel@tonic-gate rp->rb_next = REGblk();
7260Sstevel@tonic-gate rbflush();
7270Sstevel@tonic-gate rblock = rp->rb_next;
7280Sstevel@tonic-gate rp->rb_next = 0;
7290Sstevel@tonic-gate rp->rb_prev = rblock;
7300Sstevel@tonic-gate rnleft = sizeof (rp->rb_text);
7310Sstevel@tonic-gate rbufcp = rp->rb_text;
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate *rbufcp++ = c;
7340Sstevel@tonic-gate --rnleft;
7350Sstevel@tonic-gate } while (c != '\n');
7360Sstevel@tonic-gate if (rnleft)
7370Sstevel@tonic-gate *rbufcp = 0;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate
740802Scf46844 void
rbflush(void)741802Scf46844 rbflush(void)
7420Sstevel@tonic-gate {
743802Scf46844 struct strreg *sp = strp;
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate if (rblock == 0)
7460Sstevel@tonic-gate return;
7470Sstevel@tonic-gate regio(rblock, write);
7480Sstevel@tonic-gate if (sp->rg_first == 0)
7490Sstevel@tonic-gate sp->rg_first = rblock;
7500Sstevel@tonic-gate sp->rg_last = rblock;
7510Sstevel@tonic-gate sp->rg_nleft = rnleft;
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate /* Register c to char buffer buf of size buflen */
755802Scf46844 void
regbuf(c,buf,buflen)7560Sstevel@tonic-gate regbuf(c, buf, buflen)
7570Sstevel@tonic-gate unsigned char c;
7580Sstevel@tonic-gate unsigned char *buf;
7590Sstevel@tonic-gate int buflen;
7600Sstevel@tonic-gate {
761802Scf46844 unsigned char *p, *lp;
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate rbuf = ®rbuf;
7640Sstevel@tonic-gate rnleft = 0;
7650Sstevel@tonic-gate rblock = 0;
7660Sstevel@tonic-gate rnext = mapreg(c)->rg_first;
7670Sstevel@tonic-gate if (rnext == 0) {
7680Sstevel@tonic-gate *buf = 0;
7690Sstevel@tonic-gate error(gettext("Nothing in register %c"), c);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate p = buf;
7720Sstevel@tonic-gate while (getREG() == 0) {
7730Sstevel@tonic-gate lp = linebuf;
7740Sstevel@tonic-gate while (*lp) {
7750Sstevel@tonic-gate if (p >= &buf[buflen])
7760Sstevel@tonic-gate error(value(vi_TERSE) ?
7770Sstevel@tonic-gate gettext("Register too long") : gettext("Register too long to fit in memory"));
7780Sstevel@tonic-gate *p++ = *lp++;
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate *p++ = '\n';
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate if (partreg(c)) p--;
7830Sstevel@tonic-gate *p = '\0';
7840Sstevel@tonic-gate getDOT();
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate #ifdef TRACE
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate * Test code for displaying named registers.
7910Sstevel@tonic-gate */
7920Sstevel@tonic-gate
shownam()7930Sstevel@tonic-gate shownam()
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate int k;
7960Sstevel@tonic-gate
797802Scf46844 viprintf("\nRegister Contents\n");
798802Scf46844 viprintf("======== ========\n");
7990Sstevel@tonic-gate for (k = 'a'; k <= 'z'; k++) {
8000Sstevel@tonic-gate rbuf = &putrbuf;
8010Sstevel@tonic-gate rnleft = 0;
8020Sstevel@tonic-gate rblock = 0;
8030Sstevel@tonic-gate rnext = mapreg(k)->rg_first;
804802Scf46844 viprintf(" %c:", k);
8050Sstevel@tonic-gate if (rnext == 0)
806802Scf46844 viprintf("\t\tNothing in register.\n");
8070Sstevel@tonic-gate while (getREG() == 0) {
808802Scf46844 viprintf("\t\t%s\n", linebuf);
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate return (0);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate * Test code for displaying numbered registers.
8160Sstevel@tonic-gate */
8170Sstevel@tonic-gate
shownbr()8180Sstevel@tonic-gate shownbr()
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate int k;
8210Sstevel@tonic-gate
822802Scf46844 viprintf("\nRegister Contents\n");
823802Scf46844 viprintf("======== ========\n");
8240Sstevel@tonic-gate for (k = '1'; k <= '9'; k++) {
8250Sstevel@tonic-gate rbuf = &putrbuf;
8260Sstevel@tonic-gate rnleft = 0;
8270Sstevel@tonic-gate rblock = 0;
8280Sstevel@tonic-gate rnext = mapreg(k)->rg_first;
829802Scf46844 viprintf(" %c:", k);
8300Sstevel@tonic-gate if (rnext == 0)
831802Scf46844 viprintf("\t\tNothing in register.\n");
8320Sstevel@tonic-gate while (getREG() == 0) {
833802Scf46844 viprintf("\t\t%s\n", linebuf);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate return (0);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate #endif
839