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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22597Sceastha /* 23597Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24597Sceastha * Use is subject to license terms. 25597Sceastha */ 26597Sceastha 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */ 320Sstevel@tonic-gate 33597Sceastha #pragma ident "%Z%%M% %I% %E% SMI" 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include "ex.h" 360Sstevel@tonic-gate #include "ex_temp.h" 370Sstevel@tonic-gate #include "ex_vis.h" 380Sstevel@tonic-gate #include "ex_tty.h" 39*802Scf46844 #include <unistd.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * Editor temporary file routines. 430Sstevel@tonic-gate * Very similar to those of ed, except uses 2 input buffers. 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate #define READ 0 460Sstevel@tonic-gate #define WRITE 1 470Sstevel@tonic-gate 480Sstevel@tonic-gate unsigned char tfname[PATH_MAX+1]; 490Sstevel@tonic-gate static unsigned char rfname[PATH_MAX+1]; 500Sstevel@tonic-gate static unsigned char tempname[PATH_MAX+1]; 510Sstevel@tonic-gate int havetmp; 520Sstevel@tonic-gate short tfile = -1; 530Sstevel@tonic-gate static short rfile = -1; 540Sstevel@tonic-gate 55597Sceastha extern int junk(); 56597Sceastha 57*802Scf46844 void 58*802Scf46844 fileinit(void) 590Sstevel@tonic-gate { 60*802Scf46844 unsigned char *p; 61*802Scf46844 pid_t j; 62*802Scf46844 int i; 630Sstevel@tonic-gate struct stat64 stbuf; 640Sstevel@tonic-gate 650Sstevel@tonic-gate if (tline == INCRMT * (HBLKS+2)) 660Sstevel@tonic-gate return; 670Sstevel@tonic-gate cleanup(0); 680Sstevel@tonic-gate if (tfile != -1) 690Sstevel@tonic-gate close(tfile); 700Sstevel@tonic-gate tline = INCRMT * (HBLKS+2); 710Sstevel@tonic-gate blocks[0] = HBLKS; 720Sstevel@tonic-gate blocks[1] = HBLKS+1; 730Sstevel@tonic-gate blocks[2] = -1; 740Sstevel@tonic-gate dirtcnt = 0; 750Sstevel@tonic-gate iblock = -1; 760Sstevel@tonic-gate iblock2 = -1; 770Sstevel@tonic-gate oblock = -1; 780Sstevel@tonic-gate if (strlen(svalue(vi_DIRECTORY)) > (PATH_MAX -13)) 790Sstevel@tonic-gate error(gettext("User set directory too long")); 800Sstevel@tonic-gate CP(tfname, svalue(vi_DIRECTORY)); 810Sstevel@tonic-gate if (stat64((char *)tfname, &stbuf)) { 820Sstevel@tonic-gate dumbness: 830Sstevel@tonic-gate if (setexit() == 0) 840Sstevel@tonic-gate filioerr(tfname); 850Sstevel@tonic-gate else 860Sstevel@tonic-gate putNFL(); 870Sstevel@tonic-gate cleanup(1); 880Sstevel@tonic-gate exit(++errcnt); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate if (!ISDIR(stbuf)) { 910Sstevel@tonic-gate errno = ENOTDIR; 920Sstevel@tonic-gate goto dumbness; 930Sstevel@tonic-gate } 940Sstevel@tonic-gate CP(tempname, tfname); 950Sstevel@tonic-gate ichanged = 0; 960Sstevel@tonic-gate ichang2 = 0; 970Sstevel@tonic-gate (void) strcat(tfname, "/ExXXXXXX"); 980Sstevel@tonic-gate if ((tfile = mkstemp((char *)tfname)) < 0) 990Sstevel@tonic-gate goto dumbness; 1000Sstevel@tonic-gate #ifdef VMUNIX 1010Sstevel@tonic-gate { 102*802Scf46844 extern int stilinc; /* see below */ 1030Sstevel@tonic-gate stilinc = 0; 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate #endif 1060Sstevel@tonic-gate havetmp = 1; 1070Sstevel@tonic-gate /* brk((unsigned char *)fendcore); */ 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 110*802Scf46844 void 111*802Scf46844 cleanup(bool all) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate pid_t pgrp; 1140Sstevel@tonic-gate if (all) { 1150Sstevel@tonic-gate if (kflag) 1160Sstevel@tonic-gate crypt_close(perm); 1170Sstevel@tonic-gate if (xtflag) 1180Sstevel@tonic-gate crypt_close(tperm); 119*802Scf46844 putpad((unsigned char *)exit_ca_mode); 1200Sstevel@tonic-gate flush(); 1210Sstevel@tonic-gate if (ioctl(2, TIOCGPGRP, &pgrp) == 0) { 1220Sstevel@tonic-gate if (pgrp == getpgid(0)) { 1230Sstevel@tonic-gate #ifdef XPG4 1240Sstevel@tonic-gate if (envlines != -1 || envcolumns != -1) { 1250Sstevel@tonic-gate struct winsize jwin; 1260Sstevel@tonic-gate jwin.ws_row = oldlines; 1270Sstevel@tonic-gate jwin.ws_col = oldcolumns; 1280Sstevel@tonic-gate ioctl(0, TIOCSWINSZ, &jwin); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate #endif /* XPG4 */ 1310Sstevel@tonic-gate resetterm(); 1320Sstevel@tonic-gate normtty--; 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate } else { 1350Sstevel@tonic-gate #ifdef XPG4 1360Sstevel@tonic-gate if (envlines != -1 || envcolumns != -1) { 1370Sstevel@tonic-gate struct winsize jwin; 1380Sstevel@tonic-gate jwin.ws_row = oldlines; 1390Sstevel@tonic-gate jwin.ws_col = oldcolumns; 1400Sstevel@tonic-gate ioctl(0, TIOCSWINSZ, &jwin); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate #endif /* XPG4 */ 1430Sstevel@tonic-gate resetterm(); 1440Sstevel@tonic-gate normtty--; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate if (havetmp) 148*802Scf46844 unlink((char *)tfname); 1490Sstevel@tonic-gate havetmp = 0; 1500Sstevel@tonic-gate if (all && rfile >= 0) { 151*802Scf46844 unlink((char *)rfname); 1520Sstevel@tonic-gate close(rfile); 1530Sstevel@tonic-gate rfile = -1; 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate if (all == 1) 1560Sstevel@tonic-gate exit(errcnt); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 159*802Scf46844 void 160*802Scf46844 getline(line tl) 1610Sstevel@tonic-gate { 162*802Scf46844 unsigned char *bp, *lp; 163*802Scf46844 int nl; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate lp = linebuf; 1660Sstevel@tonic-gate bp = getblock(tl, READ); 1670Sstevel@tonic-gate nl = nleft; 1680Sstevel@tonic-gate tl &= ~OFFMSK; 1690Sstevel@tonic-gate while (*lp++ = *bp++) 1700Sstevel@tonic-gate if (--nl == 0) { 1710Sstevel@tonic-gate bp = getblock(tl += INCRMT, READ); 1720Sstevel@tonic-gate nl = nleft; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 176597Sceastha int 177597Sceastha putline(void) 1780Sstevel@tonic-gate { 179597Sceastha unsigned char *bp, *lp; 180597Sceastha unsigned char tmpbp; 181597Sceastha int nl; 1820Sstevel@tonic-gate line tl; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate dirtcnt++; 1850Sstevel@tonic-gate lp = linebuf; 1860Sstevel@tonic-gate change(); 1870Sstevel@tonic-gate tl = tline; 1880Sstevel@tonic-gate bp = getblock(tl, WRITE); 1890Sstevel@tonic-gate nl = nleft; 1900Sstevel@tonic-gate tl &= ~OFFMSK; 1910Sstevel@tonic-gate while (*bp = *lp++) { 192597Sceastha tmpbp = *bp; 193597Sceastha if (tmpbp == '\n') { 194597Sceastha *bp = 0; 1950Sstevel@tonic-gate linebp = lp; 1960Sstevel@tonic-gate break; 197597Sceastha } else if (junk(*bp++)) { 198*802Scf46844 checkjunk(tmpbp); 199597Sceastha *--bp; 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate if (--nl == 0) { 2020Sstevel@tonic-gate bp = getblock(tl += INCRMT, WRITE); 2030Sstevel@tonic-gate nl = nleft; 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate tl = tline; 2070Sstevel@tonic-gate tline += (((lp - linebuf) + BNDRY - 1) >> SHFT) & 077776; 2080Sstevel@tonic-gate return (tl); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate int read(); 2120Sstevel@tonic-gate int write(); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate unsigned char * 2150Sstevel@tonic-gate getblock(atl, iof) 2160Sstevel@tonic-gate line atl; 2170Sstevel@tonic-gate int iof; 2180Sstevel@tonic-gate { 219*802Scf46844 int bno, off; 220*802Scf46844 unsigned char *p1, *p2; 221*802Scf46844 int n; 2220Sstevel@tonic-gate line *tmpptr; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate bno = (atl >> OFFBTS) & BLKMSK; 2250Sstevel@tonic-gate off = (atl << SHFT) & LBTMSK; 2260Sstevel@tonic-gate if (bno >= NMBLKS) { 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * When we overflow tmpfile buffers, 2290Sstevel@tonic-gate * throw away line which could not be 2300Sstevel@tonic-gate * put into buffer. 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate for (tmpptr = dot; tmpptr < unddol; tmpptr++) 2330Sstevel@tonic-gate *tmpptr = *(tmpptr+1); 2340Sstevel@tonic-gate if (dot == dol) 2350Sstevel@tonic-gate dot--; 2360Sstevel@tonic-gate dol--; 2370Sstevel@tonic-gate unddol--; 2380Sstevel@tonic-gate error(gettext(" Tmp file too large")); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate nleft = BUFSIZE - off; 2410Sstevel@tonic-gate if (bno == iblock) { 2420Sstevel@tonic-gate ichanged |= iof; 2430Sstevel@tonic-gate hitin2 = 0; 2440Sstevel@tonic-gate return (ibuff + off); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate if (bno == iblock2) { 2470Sstevel@tonic-gate ichang2 |= iof; 2480Sstevel@tonic-gate hitin2 = 1; 2490Sstevel@tonic-gate return (ibuff2 + off); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate if (bno == oblock) 2520Sstevel@tonic-gate return (obuff + off); 2530Sstevel@tonic-gate if (iof == READ) { 2540Sstevel@tonic-gate if (hitin2 == 0) { 2550Sstevel@tonic-gate if (ichang2) { 2560Sstevel@tonic-gate if (xtflag) 2570Sstevel@tonic-gate if (run_crypt(0L, ibuff2, 2580Sstevel@tonic-gate CRSIZE, tperm) == -1) 2590Sstevel@tonic-gate filioerr(tfname); 2600Sstevel@tonic-gate blkio(iblock2, ibuff2, write); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate ichang2 = 0; 2630Sstevel@tonic-gate iblock2 = bno; 2640Sstevel@tonic-gate blkio(bno, ibuff2, read); 2650Sstevel@tonic-gate if (xtflag) 2660Sstevel@tonic-gate if (run_crypt(0L, ibuff2, CRSIZE, tperm) == -1) 2670Sstevel@tonic-gate filioerr(tfname); 2680Sstevel@tonic-gate hitin2 = 1; 2690Sstevel@tonic-gate return (ibuff2 + off); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate hitin2 = 0; 2720Sstevel@tonic-gate if (ichanged) { 2730Sstevel@tonic-gate if (xtflag) 2740Sstevel@tonic-gate if (run_crypt(0L, ibuff, CRSIZE, tperm) == -1) 2750Sstevel@tonic-gate filioerr(tfname); 2760Sstevel@tonic-gate blkio(iblock, ibuff, write); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate ichanged = 0; 2790Sstevel@tonic-gate iblock = bno; 2800Sstevel@tonic-gate blkio(bno, ibuff, read); 2810Sstevel@tonic-gate if (xtflag) 2820Sstevel@tonic-gate if (run_crypt(0L, ibuff, CRSIZE, tperm) == -1) 2830Sstevel@tonic-gate filioerr(tfname); 2840Sstevel@tonic-gate return (ibuff + off); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate if (oblock >= 0) { 2870Sstevel@tonic-gate if (xtflag) { 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Encrypt block before writing, so some devious 2900Sstevel@tonic-gate * person can't look at temp file while editing. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate p1 = obuff; 2930Sstevel@tonic-gate p2 = crbuf; 2940Sstevel@tonic-gate n = CRSIZE; 2950Sstevel@tonic-gate while (n--) 2960Sstevel@tonic-gate *p2++ = *p1++; 2970Sstevel@tonic-gate if (run_crypt(0L, crbuf, CRSIZE, tperm) == -1) 2980Sstevel@tonic-gate filioerr(tfname); 2990Sstevel@tonic-gate blkio(oblock, crbuf, write); 3000Sstevel@tonic-gate } else 3010Sstevel@tonic-gate blkio(oblock, obuff, write); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate oblock = bno; 3040Sstevel@tonic-gate return (obuff + off); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate #ifdef VMUNIX 3080Sstevel@tonic-gate #define INCORB 64 3090Sstevel@tonic-gate unsigned char incorb[INCORB+1][BUFSIZE]; 3100Sstevel@tonic-gate #define pagrnd(a) ((unsigned char *)(((int)a)&~(BUFSIZE-1))) 3110Sstevel@tonic-gate int stilinc; /* up to here not written yet */ 3120Sstevel@tonic-gate #endif 3130Sstevel@tonic-gate 314*802Scf46844 void 315*802Scf46844 blkio(short b, unsigned char *buf, int (*iofcn)()) 3160Sstevel@tonic-gate { 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate #ifdef VMUNIX 3190Sstevel@tonic-gate if (b < INCORB) { 3200Sstevel@tonic-gate if (iofcn == read) { 3210Sstevel@tonic-gate bcopy(pagrnd(incorb[b+1]), buf, BUFSIZE); 3220Sstevel@tonic-gate return; 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate bcopy(buf, pagrnd(incorb[b+1]), BUFSIZE); 3250Sstevel@tonic-gate if (laste) { 3260Sstevel@tonic-gate if (b >= stilinc) 3270Sstevel@tonic-gate stilinc = b + 1; 3280Sstevel@tonic-gate return; 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate } else if (stilinc) 3310Sstevel@tonic-gate tflush(); 3320Sstevel@tonic-gate #endif 3330Sstevel@tonic-gate lseek(tfile, (long)(unsigned)b * BUFSIZE, 0); 3340Sstevel@tonic-gate if ((*iofcn)(tfile, buf, BUFSIZE) != BUFSIZE) 3350Sstevel@tonic-gate filioerr(tfname); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate #ifdef VMUNIX 339*802Scf46844 void 340*802Scf46844 tlaste(void) 3410Sstevel@tonic-gate { 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate if (stilinc) 3440Sstevel@tonic-gate dirtcnt = 0; 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 347*802Scf46844 void 348*802Scf46844 tflush(void) 3490Sstevel@tonic-gate { 3500Sstevel@tonic-gate int i = stilinc; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate stilinc = 0; 3530Sstevel@tonic-gate lseek(tfile, (long)0, 0); 3540Sstevel@tonic-gate if (write(tfile, pagrnd(incorb[1]), i * BUFSIZE) != (i * BUFSIZE)) 3550Sstevel@tonic-gate filioerr(tfname); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate #endif 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * Synchronize the state of the temporary file in case 3610Sstevel@tonic-gate * a crash occurs. 3620Sstevel@tonic-gate */ 363*802Scf46844 void 364*802Scf46844 synctmp(void) 3650Sstevel@tonic-gate { 366*802Scf46844 int cnt; 367*802Scf46844 line *a; 368*802Scf46844 short *bp; 369*802Scf46844 unsigned char *p1, *p2; 370*802Scf46844 int n; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate #ifdef VMUNIX 3730Sstevel@tonic-gate if (stilinc) 3740Sstevel@tonic-gate return; 3750Sstevel@tonic-gate #endif 3760Sstevel@tonic-gate if (dol == zero) 3770Sstevel@tonic-gate return; 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * In theory, we need to encrypt iblock and iblock2 before writing 3800Sstevel@tonic-gate * them out, as well as oblock, but in practice ichanged and ichang2 3810Sstevel@tonic-gate * can never be set, so this isn't really needed. Likewise, the 3820Sstevel@tonic-gate * code in getblock above for iblock+iblock2 isn't needed. 3830Sstevel@tonic-gate */ 3840Sstevel@tonic-gate if (ichanged) 3850Sstevel@tonic-gate blkio(iblock, ibuff, write); 3860Sstevel@tonic-gate ichanged = 0; 3870Sstevel@tonic-gate if (ichang2) 3880Sstevel@tonic-gate blkio(iblock2, ibuff2, write); 3890Sstevel@tonic-gate ichang2 = 0; 3900Sstevel@tonic-gate if (oblock != -1) 3910Sstevel@tonic-gate if (xtflag) { 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Encrypt block before writing, so some devious 3940Sstevel@tonic-gate * person can't look at temp file while editing. 3950Sstevel@tonic-gate */ 3960Sstevel@tonic-gate p1 = obuff; 3970Sstevel@tonic-gate p2 = crbuf; 3980Sstevel@tonic-gate n = CRSIZE; 3990Sstevel@tonic-gate while (n--) 4000Sstevel@tonic-gate *p2++ = *p1++; 4010Sstevel@tonic-gate if (run_crypt(0L, crbuf, CRSIZE, tperm) == -1) 4020Sstevel@tonic-gate filioerr(tfname); 4030Sstevel@tonic-gate blkio(oblock, crbuf, write); 4040Sstevel@tonic-gate } else 4050Sstevel@tonic-gate blkio(oblock, obuff, write); 4060Sstevel@tonic-gate time(&H.Time); 4070Sstevel@tonic-gate uid = getuid(); 4080Sstevel@tonic-gate if (xtflag) 4090Sstevel@tonic-gate H.encrypted = 1; 4100Sstevel@tonic-gate else 4110Sstevel@tonic-gate H.encrypted = 0; 4120Sstevel@tonic-gate *zero = (line) H.Time; 4130Sstevel@tonic-gate for (a = zero, bp = blocks; a <= dol; 414597Sceastha a += BUFSIZE / sizeof (*a), bp++) { 4150Sstevel@tonic-gate if (bp >= &H.Blocks[LBLKS-1]) 4160Sstevel@tonic-gate error(gettext( 417597Sceastha "file too large to recover with -r option")); 4180Sstevel@tonic-gate if (*bp < 0) { 4190Sstevel@tonic-gate tline = (tline + OFFMSK) &~ OFFMSK; 4200Sstevel@tonic-gate *bp = ((tline >> OFFBTS) & BLKMSK); 4210Sstevel@tonic-gate if (*bp > NMBLKS) 4220Sstevel@tonic-gate error(gettext(" Tmp file too large")); 4230Sstevel@tonic-gate tline += INCRMT; 4240Sstevel@tonic-gate oblock = *bp + 1; 4250Sstevel@tonic-gate bp[1] = -1; 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate lseek(tfile, (long)(unsigned)*bp * BUFSIZE, 0); 4280Sstevel@tonic-gate cnt = ((dol - a) + 2) * sizeof (line); 4290Sstevel@tonic-gate if (cnt > BUFSIZE) 4300Sstevel@tonic-gate cnt = BUFSIZE; 4310Sstevel@tonic-gate if (write(tfile, (char *)a, cnt) != cnt) { 4320Sstevel@tonic-gate oops: 4330Sstevel@tonic-gate *zero = 0; 4340Sstevel@tonic-gate filioerr(tfname); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate *zero = 0; 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate flines = lineDOL(); 4390Sstevel@tonic-gate lseek(tfile, 0l, 0); 4400Sstevel@tonic-gate if (write(tfile, (char *)&H, sizeof (H)) != sizeof (H)) 4410Sstevel@tonic-gate goto oops; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 444*802Scf46844 void 445*802Scf46844 TSYNC(void) 4460Sstevel@tonic-gate { 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate if (dirtcnt > MAXDIRT) { 4490Sstevel@tonic-gate #ifdef VMUNIX 4500Sstevel@tonic-gate if (stilinc) 4510Sstevel@tonic-gate tflush(); 4520Sstevel@tonic-gate #endif 4530Sstevel@tonic-gate dirtcnt = 0; 4540Sstevel@tonic-gate synctmp(); 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate /* 4590Sstevel@tonic-gate * Named buffer routines. 4600Sstevel@tonic-gate * These are implemented differently than the main buffer. 4610Sstevel@tonic-gate * Each named buffer has a chain of blocks in the register file. 4620Sstevel@tonic-gate * Each block contains roughly 508 chars of text, 4630Sstevel@tonic-gate * and a previous and next block number. We also have information 4640Sstevel@tonic-gate * about which blocks came from deletes of multiple partial lines, 4650Sstevel@tonic-gate * e.g. deleting a sentence or a LISP object. 4660Sstevel@tonic-gate * 4670Sstevel@tonic-gate * We maintain a free map for the temp file. To free the blocks 4680Sstevel@tonic-gate * in a register we must read the blocks to find how they are chained 4690Sstevel@tonic-gate * together. 4700Sstevel@tonic-gate * 4710Sstevel@tonic-gate * BUG: The default savind of deleted lines in numbered 4720Sstevel@tonic-gate * buffers may be rather inefficient; it hasn't been profiled. 4730Sstevel@tonic-gate */ 4740Sstevel@tonic-gate struct strreg { 4750Sstevel@tonic-gate short rg_flags; 4760Sstevel@tonic-gate short rg_nleft; 4770Sstevel@tonic-gate short rg_first; 4780Sstevel@tonic-gate short rg_last; 4790Sstevel@tonic-gate } strregs[('z'-'a'+1) + ('9'-'0'+1)], *strp; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate struct rbuf { 4820Sstevel@tonic-gate short rb_prev; 4830Sstevel@tonic-gate short rb_next; 4840Sstevel@tonic-gate unsigned char rb_text[BUFSIZE - 2 * sizeof (short)]; 4850Sstevel@tonic-gate } *rbuf, KILLrbuf, putrbuf, YANKrbuf, regrbuf; 4860Sstevel@tonic-gate #ifdef VMUNIX 4870Sstevel@tonic-gate short rused[256]; 4880Sstevel@tonic-gate #else 4890Sstevel@tonic-gate short rused[32]; 4900Sstevel@tonic-gate #endif 4910Sstevel@tonic-gate short rnleft; 4920Sstevel@tonic-gate short rblock; 4930Sstevel@tonic-gate short rnext; 4940Sstevel@tonic-gate unsigned char *rbufcp; 4950Sstevel@tonic-gate 496*802Scf46844 void 497*802Scf46844 regio(short b, int (*iofcn)()) 4980Sstevel@tonic-gate { 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate if (rfile == -1) { 5010Sstevel@tonic-gate CP(rfname, tempname); 5020Sstevel@tonic-gate (void) strcat(rfname, "/RxXXXXXX"); 5030Sstevel@tonic-gate if ((rfile = mkstemp((char *)rfname)) < 0) 5040Sstevel@tonic-gate filioerr(rfname); 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate lseek(rfile, (long)b * BUFSIZE, 0); 5070Sstevel@tonic-gate if ((*iofcn)(rfile, rbuf, BUFSIZE) != BUFSIZE) 5080Sstevel@tonic-gate filioerr(rfname); 5090Sstevel@tonic-gate rblock = b; 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 512*802Scf46844 int 513*802Scf46844 REGblk(void) 5140Sstevel@tonic-gate { 515*802Scf46844 int i, j, m; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate for (i = 0; i < sizeof (rused) / sizeof (rused[0]); i++) { 5180Sstevel@tonic-gate m = (rused[i] ^ 0177777) & 0177777; 5190Sstevel@tonic-gate if (i == 0) 5200Sstevel@tonic-gate m &= ~1; 5210Sstevel@tonic-gate if (m != 0) { 5220Sstevel@tonic-gate j = 0; 5230Sstevel@tonic-gate while ((m & 1) == 0) 5240Sstevel@tonic-gate j++, m >>= 1; 5250Sstevel@tonic-gate rused[i] |= (1 << j); 5260Sstevel@tonic-gate #ifdef RDEBUG 527*802Scf46844 viprintf("allocating block %d\n", i * 16 + j); 5280Sstevel@tonic-gate #endif 5290Sstevel@tonic-gate return (i * 16 + j); 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate error(gettext("Out of register space (ugh)")); 5330Sstevel@tonic-gate /*NOTREACHED*/ 534*802Scf46844 return (0); 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate struct strreg * 5380Sstevel@tonic-gate mapreg(c) 539*802Scf46844 int c; 5400Sstevel@tonic-gate { 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate if (isupper(c)) 5430Sstevel@tonic-gate c = tolower(c); 5440Sstevel@tonic-gate return (isdigit(c) ? &strregs[('z'-'a'+1)+(c-'0')] : &strregs[c-'a']); 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate int shread(); 5480Sstevel@tonic-gate 549*802Scf46844 void 550*802Scf46844 KILLreg(int c) 5510Sstevel@tonic-gate { 552*802Scf46844 struct strreg *sp; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate rbuf = &KILLrbuf; 5550Sstevel@tonic-gate sp = mapreg(c); 5560Sstevel@tonic-gate rblock = sp->rg_first; 5570Sstevel@tonic-gate sp->rg_first = sp->rg_last = 0; 5580Sstevel@tonic-gate sp->rg_flags = sp->rg_nleft = 0; 5590Sstevel@tonic-gate while (rblock != 0) { 5600Sstevel@tonic-gate #ifdef RDEBUG 561*802Scf46844 viprintf("freeing block %d\n", rblock); 5620Sstevel@tonic-gate #endif 5630Sstevel@tonic-gate rused[rblock / 16] &= ~(1 << (rblock % 16)); 5640Sstevel@tonic-gate regio(rblock, shread); 5650Sstevel@tonic-gate rblock = rbuf->rb_next; 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate /*VARARGS*/ 570*802Scf46844 int 571*802Scf46844 shread(void) 5720Sstevel@tonic-gate { 5730Sstevel@tonic-gate struct front { short a; short b; }; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate if (read(rfile, (char *)rbuf, sizeof (struct front)) == 576597Sceastha sizeof (struct front)) 5770Sstevel@tonic-gate return (sizeof (struct rbuf)); 5780Sstevel@tonic-gate return (0); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate int getREG(); 5820Sstevel@tonic-gate 583*802Scf46844 int 584*802Scf46844 putreg(unsigned char c) 5850Sstevel@tonic-gate { 586*802Scf46844 line *odot = dot; 587*802Scf46844 line *odol = dol; 588*802Scf46844 int cnt; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate deletenone(); 5910Sstevel@tonic-gate appendnone(); 5920Sstevel@tonic-gate rbuf = &putrbuf; 5930Sstevel@tonic-gate rnleft = 0; 5940Sstevel@tonic-gate rblock = 0; 5950Sstevel@tonic-gate rnext = mapreg(c)->rg_first; 5960Sstevel@tonic-gate if (rnext == 0) { 5970Sstevel@tonic-gate if (inopen) { 5980Sstevel@tonic-gate splitw++; 5990Sstevel@tonic-gate vclean(); 6000Sstevel@tonic-gate vgoto(WECHO, 0); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate vreg = -1; 6030Sstevel@tonic-gate error(gettext("Nothing in register %c"), c); 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate if (inopen && partreg(c)) { 6060Sstevel@tonic-gate if (!FIXUNDO) { 6070Sstevel@tonic-gate splitw++; vclean(); vgoto(WECHO, 0); vreg = -1; 6080Sstevel@tonic-gate error(gettext("Can't put partial line inside macro")); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate squish(); 6110Sstevel@tonic-gate addr1 = addr2 = dol; 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate cnt = append(getREG, addr2); 6140Sstevel@tonic-gate if (inopen && partreg(c)) { 6150Sstevel@tonic-gate unddol = dol; 6160Sstevel@tonic-gate dol = odol; 6170Sstevel@tonic-gate dot = odot; 6180Sstevel@tonic-gate pragged(0); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate killcnt(cnt); 6210Sstevel@tonic-gate notecnt = cnt; 622*802Scf46844 return (0); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 625*802Scf46844 short 626*802Scf46844 partreg(unsigned char c) 6270Sstevel@tonic-gate { 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate return (mapreg(c)->rg_flags); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 632*802Scf46844 void 633*802Scf46844 notpart(int c) 6340Sstevel@tonic-gate { 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (c) 6370Sstevel@tonic-gate mapreg(c)->rg_flags = 0; 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 640*802Scf46844 int 641*802Scf46844 getREG(void) 6420Sstevel@tonic-gate { 643*802Scf46844 unsigned char *lp = linebuf; 644*802Scf46844 int c; 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate for (;;) { 6470Sstevel@tonic-gate if (rnleft == 0) { 6480Sstevel@tonic-gate if (rnext == 0) 6490Sstevel@tonic-gate return (EOF); 6500Sstevel@tonic-gate regio(rnext, read); 6510Sstevel@tonic-gate rnext = rbuf->rb_next; 6520Sstevel@tonic-gate rbufcp = rbuf->rb_text; 6530Sstevel@tonic-gate rnleft = sizeof (rbuf->rb_text); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate c = *rbufcp; 6560Sstevel@tonic-gate if (c == 0) 6570Sstevel@tonic-gate return (EOF); 6580Sstevel@tonic-gate rbufcp++, --rnleft; 6590Sstevel@tonic-gate if (c == '\n') { 6600Sstevel@tonic-gate *lp++ = 0; 6610Sstevel@tonic-gate return (0); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate *lp++ = c; 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate 667*802Scf46844 int 668*802Scf46844 YANKreg(int c) 6690Sstevel@tonic-gate { 670*802Scf46844 line *addr; 671*802Scf46844 struct strreg *sp; 6720Sstevel@tonic-gate unsigned char savelb[LBSIZE]; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (isdigit(c)) 6750Sstevel@tonic-gate kshift(); 6760Sstevel@tonic-gate if (islower(c)) 6770Sstevel@tonic-gate KILLreg(c); 6780Sstevel@tonic-gate strp = sp = mapreg(c); 6790Sstevel@tonic-gate sp->rg_flags = inopen && cursor && wcursor; 6800Sstevel@tonic-gate rbuf = &YANKrbuf; 6810Sstevel@tonic-gate if (sp->rg_last) { 6820Sstevel@tonic-gate regio(sp->rg_last, read); 6830Sstevel@tonic-gate rnleft = sp->rg_nleft; 6840Sstevel@tonic-gate rbufcp = &rbuf->rb_text[sizeof (rbuf->rb_text) - rnleft]; 6850Sstevel@tonic-gate } else { 6860Sstevel@tonic-gate rblock = 0; 6870Sstevel@tonic-gate rnleft = 0; 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate CP(savelb, linebuf); 6900Sstevel@tonic-gate for (addr = addr1; addr <= addr2; addr++) { 6910Sstevel@tonic-gate getline(*addr); 6920Sstevel@tonic-gate if (sp->rg_flags) { 6930Sstevel@tonic-gate if (addr == addr2) 6940Sstevel@tonic-gate *wcursor = 0; 6950Sstevel@tonic-gate if (addr == addr1) 6960Sstevel@tonic-gate strcpy(linebuf, cursor); 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate YANKline(); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate rbflush(); 7010Sstevel@tonic-gate killed(); 7020Sstevel@tonic-gate CP(linebuf, savelb); 703*802Scf46844 return (0); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 706*802Scf46844 void 707*802Scf46844 kshift(void) 7080Sstevel@tonic-gate { 709*802Scf46844 int i; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate KILLreg('9'); 7120Sstevel@tonic-gate for (i = '8'; i >= '0'; i--) 7130Sstevel@tonic-gate copy(mapreg(i+1), mapreg(i), sizeof (struct strreg)); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 716*802Scf46844 void 717*802Scf46844 YANKline(void) 7180Sstevel@tonic-gate { 719*802Scf46844 unsigned char *lp = linebuf; 720*802Scf46844 struct rbuf *rp = rbuf; 721*802Scf46844 int c; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate do { 7240Sstevel@tonic-gate c = *lp++; 7250Sstevel@tonic-gate if (c == 0) 7260Sstevel@tonic-gate c = '\n'; 7270Sstevel@tonic-gate if (rnleft == 0) { 7280Sstevel@tonic-gate rp->rb_next = REGblk(); 7290Sstevel@tonic-gate rbflush(); 7300Sstevel@tonic-gate rblock = rp->rb_next; 7310Sstevel@tonic-gate rp->rb_next = 0; 7320Sstevel@tonic-gate rp->rb_prev = rblock; 7330Sstevel@tonic-gate rnleft = sizeof (rp->rb_text); 7340Sstevel@tonic-gate rbufcp = rp->rb_text; 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate *rbufcp++ = c; 7370Sstevel@tonic-gate --rnleft; 7380Sstevel@tonic-gate } while (c != '\n'); 7390Sstevel@tonic-gate if (rnleft) 7400Sstevel@tonic-gate *rbufcp = 0; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 743*802Scf46844 void 744*802Scf46844 rbflush(void) 7450Sstevel@tonic-gate { 746*802Scf46844 struct strreg *sp = strp; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate if (rblock == 0) 7490Sstevel@tonic-gate return; 7500Sstevel@tonic-gate regio(rblock, write); 7510Sstevel@tonic-gate if (sp->rg_first == 0) 7520Sstevel@tonic-gate sp->rg_first = rblock; 7530Sstevel@tonic-gate sp->rg_last = rblock; 7540Sstevel@tonic-gate sp->rg_nleft = rnleft; 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate /* Register c to char buffer buf of size buflen */ 758*802Scf46844 void 7590Sstevel@tonic-gate regbuf(c, buf, buflen) 7600Sstevel@tonic-gate unsigned char c; 7610Sstevel@tonic-gate unsigned char *buf; 7620Sstevel@tonic-gate int buflen; 7630Sstevel@tonic-gate { 764*802Scf46844 unsigned char *p, *lp; 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate rbuf = ®rbuf; 7670Sstevel@tonic-gate rnleft = 0; 7680Sstevel@tonic-gate rblock = 0; 7690Sstevel@tonic-gate rnext = mapreg(c)->rg_first; 7700Sstevel@tonic-gate if (rnext == 0) { 7710Sstevel@tonic-gate *buf = 0; 7720Sstevel@tonic-gate error(gettext("Nothing in register %c"), c); 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate p = buf; 7750Sstevel@tonic-gate while (getREG() == 0) { 7760Sstevel@tonic-gate lp = linebuf; 7770Sstevel@tonic-gate while (*lp) { 7780Sstevel@tonic-gate if (p >= &buf[buflen]) 7790Sstevel@tonic-gate error(value(vi_TERSE) ? 7800Sstevel@tonic-gate gettext("Register too long") : gettext("Register too long to fit in memory")); 7810Sstevel@tonic-gate *p++ = *lp++; 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate *p++ = '\n'; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate if (partreg(c)) p--; 7860Sstevel@tonic-gate *p = '\0'; 7870Sstevel@tonic-gate getDOT(); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate #ifdef TRACE 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* 7930Sstevel@tonic-gate * Test code for displaying named registers. 7940Sstevel@tonic-gate */ 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate shownam() 7970Sstevel@tonic-gate { 7980Sstevel@tonic-gate int k; 7990Sstevel@tonic-gate 800*802Scf46844 viprintf("\nRegister Contents\n"); 801*802Scf46844 viprintf("======== ========\n"); 8020Sstevel@tonic-gate for (k = 'a'; k <= 'z'; k++) { 8030Sstevel@tonic-gate rbuf = &putrbuf; 8040Sstevel@tonic-gate rnleft = 0; 8050Sstevel@tonic-gate rblock = 0; 8060Sstevel@tonic-gate rnext = mapreg(k)->rg_first; 807*802Scf46844 viprintf(" %c:", k); 8080Sstevel@tonic-gate if (rnext == 0) 809*802Scf46844 viprintf("\t\tNothing in register.\n"); 8100Sstevel@tonic-gate while (getREG() == 0) { 811*802Scf46844 viprintf("\t\t%s\n", linebuf); 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate return (0); 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * Test code for displaying numbered registers. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate shownbr() 8220Sstevel@tonic-gate { 8230Sstevel@tonic-gate int k; 8240Sstevel@tonic-gate 825*802Scf46844 viprintf("\nRegister Contents\n"); 826*802Scf46844 viprintf("======== ========\n"); 8270Sstevel@tonic-gate for (k = '1'; k <= '9'; k++) { 8280Sstevel@tonic-gate rbuf = &putrbuf; 8290Sstevel@tonic-gate rnleft = 0; 8300Sstevel@tonic-gate rblock = 0; 8310Sstevel@tonic-gate rnext = mapreg(k)->rg_first; 832*802Scf46844 viprintf(" %c:", k); 8330Sstevel@tonic-gate if (rnext == 0) 834*802Scf46844 viprintf("\t\tNothing in register.\n"); 8350Sstevel@tonic-gate while (getREG() == 0) { 836*802Scf46844 viprintf("\t\t%s\n", linebuf); 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate return (0); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate #endif 842