1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23/*	  All Rights Reserved  	*/
24
25
26/* Copyright (c) 1981 Regents of the University of California */
27#ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.7	*/
28/*
29 * The editor uses a temporary file for files being edited, in a structure
30 * similar to that of ed.  The first block of the file is used for a header
31 * block which guides recovery after editor/system crashes.
32 * Lines are represented in core by a pointer into the temporary file which
33 * is packed into 16 bits (32 on VMUNIX).  All but the low bit index the temp
34 * file; the last is used by global commands.  The parameters below control
35 * how much the other bits are shifted left before they index the temp file.
36 * Larger shifts give more slop in the temp file but allow larger files
37 * to be edited.
38 *
39 * The editor does not garbage collect the temporary file.  When a new
40 * file is edited, the temporary file is rather discarded and a new one
41 * created for the new file.  Garbage collection would be rather complicated
42 * in ex because of the general undo, and in any case would require more
43 * work when throwing lines away because marks would have be carefully
44 * checked before reallocating temporary file space.  Said another way,
45 * each time you create a new line in the temporary file you get a unique
46 * number back, and this is a property used by marks.
47 *
48 * The following temp file parameters allow 256k bytes in the temporary
49 * file.  By changing to the numbers in comments you can get 512k.
50 * For VMUNIX you get more than you could ever want.
51 * VMUNIX uses long (32 bit) integers giving much more
52 * space in the temp file and no waste.  This doubles core
53 * requirements but allows files of essentially unlimited size to be edited.
54 */
55#ifndef VMUNIX
56#define	BLKMSK	0777		/* 01777 */
57#define	BNDRY	8		/* 16 */
58#define	INCRMT	0200		/* 0100 */
59#define	LBTMSK	0770		/* 0760 */
60#define	NMBLKS	506		/* 1018 */
61#define	OFFBTS	7		/* 6 */
62#define	OFFMSK	0177		/* 077 */
63#define	SHFT	2		/* 3 */
64#else
65#define	BLKMSK	077777
66#define	BNDRY	2
67#define	INCRMT	04000		/* 02000 */
68#define	LBTMSK	03776		/* 01776 */
69#define	NMBLKS	077770
70#define	OFFBTS	11		/* 10 */
71#define	OFFMSK	03777		/* 01777 */
72#define	SHFT	0
73#endif
74
75/*
76 * The editor uses three buffers into the temporary file (ed uses two
77 * and is very similar).  These are two read buffers and one write buffer.
78 * Basically, the editor deals with the file as a sequence of BUFSIZE character
79 * blocks.  Each block contains some number of lines (and lines
80 * can run across block boundaries.
81 *
82 * New lines are written into the last block in the temporary file
83 * which is in core as obuf.  When a line is needed which isn't in obuf,
84 * then it is brought into an input buffer.  As there are two, the choice
85 * is to take the buffer into which the last read (of the two) didn't go.
86 * Thus this is a 2 buffer LRU replacement strategy.  Measurement
87 * shows that this saves roughly 25% of the buffer reads over a one
88 * input buffer strategy.  Since the editor (on our VAX over 1 week)
89 * spends (spent) roughly 30% of its time in the system read routine,
90 * this can be a big help.
91 */
92var bool	hitin2;		/* Last read hit was ibuff2 not ibuff */
93var bool	ichang2;	/* Have actually changed ibuff2 */
94var bool	ichanged;	/* Have actually changed ibuff */
95var short	iblock;		/* Temp file block number of ibuff (or -1) */
96var short	iblock2;	/* Temp file block number of ibuff2 (or -1) */
97var short	ninbuf;		/* Number useful chars left in input buffer */
98var short	nleft;		/* Number usable chars left in output buffer */
99var short	oblock;		/* Temp file block number of obuff (or -1) */
100#ifndef VMUNIX
101var short	tline;		/* Current temp file ptr */
102#else
103var int	tline;
104#endif
105
106var unsigned char	ibuff[BUFSIZE];
107var unsigned char	ibuff2[BUFSIZE];
108var unsigned char	obuff[BUFSIZE];
109
110/*
111 * Structure of the descriptor block which resides
112 * in the first block of the temporary file and is
113 * the guiding light for crash recovery.
114 *
115 * As the Blocks field below implies, there are temporary file blocks
116 * devoted to (some) image of the incore array of pointers into the temp
117 * file.  Thus, to recover from a crash we use these indices to get the
118 * line pointers back, and then use the line pointers to get the text back.
119 * Except for possible lost lines due to sandbagged I/O, the entire
120 * file (at the time of the last editor "sync") can be recovered from
121 * the temp file.
122 */
123
124/* This definition also appears in expreserve.c... beware */
125struct 	header {
126	time_t	Time;			/* Time temp file last updated */
127	int	Uid;
128#ifndef VMUNIX
129	short	Flines;			/* Number of lines in file */
130#else
131	int	Flines;
132#endif
133	unsigned char	Savedfile[FNSIZE];	/* The current file name */
134	short	Blocks[LBLKS];		/* Blocks where line pointers stashed */
135	short 	encrypted;		/* Encrypted temp file flag */
136};
137var struct 	header H;
138
139#define	uid		H.Uid
140#define	flines		H.Flines
141#define	savedfile	H.Savedfile
142#define	blocks		H.Blocks
143