xref: /original-bsd/usr.bin/ex/ex_temp.h (revision e049d966)
1cd8169f0Sbostic /*-
2*e049d966Sbostic  * Copyright (c) 1980, 1993
3*e049d966Sbostic  *	The Regents of the University of California.  All rights reserved.
46f1350a3Sdist  *
5cd8169f0Sbostic  * %sccs.include.proprietary.c%
6cd8169f0Sbostic  *
7*e049d966Sbostic  *	@(#)ex_temp.h	8.1 (Berkeley) 06/09/93
86f1350a3Sdist  */
96f1350a3Sdist 
10a87d28c0Smark /*
11a87d28c0Smark  * The editor uses a temporary file for files being edited, in a structure
12a87d28c0Smark  * similar to that of ed.  The first block of the file is used for a header
13a87d28c0Smark  * block which guides recovery after editor/system crashes.
14a87d28c0Smark  * Lines are represented in core by a pointer into the temporary file which
15cde8cdf9Smark  * is packed into 16 bits (32 on VMUNIX).  All but the low bit index the temp
16cde8cdf9Smark  * file; the last is used by global commands.  The parameters below control
17cde8cdf9Smark  * how much the other bits are shifted left before they index the temp file.
18a87d28c0Smark  * Larger shifts give more slop in the temp file but allow larger files
19a87d28c0Smark  * to be edited.
20a87d28c0Smark  *
21a87d28c0Smark  * The editor does not garbage collect the temporary file.  When a new
22a87d28c0Smark  * file is edited, the temporary file is rather discarded and a new one
23a87d28c0Smark  * created for the new file.  Garbage collection would be rather complicated
24a87d28c0Smark  * in ex because of the general undo, and in any case would require more
25a87d28c0Smark  * work when throwing lines away because marks would have be carefully
26a87d28c0Smark  * checked before reallocating temporary file space.  Said another way,
27a87d28c0Smark  * each time you create a new line in the temporary file you get a unique
28a87d28c0Smark  * number back, and this is a property used by marks.
29a87d28c0Smark  *
30a87d28c0Smark  * The following temp file parameters allow 256k bytes in the temporary
31a87d28c0Smark  * file.  By changing to the numbers in comments you can get 512k.
32cde8cdf9Smark  * For VMUNIX you get more than you could ever want.
33cde8cdf9Smark  * VMUNIX uses long (32 bit) integers giving much more
34cde8cdf9Smark  * space in the temp file and no waste.  This doubles core
35cde8cdf9Smark  * requirements but allows files of essentially unlimited size to be edited.
36a87d28c0Smark  */
37cde8cdf9Smark #ifndef VMUNIX
38a87d28c0Smark #define	BLKMSK	0777		/* 01777 */
39a87d28c0Smark #define	BNDRY	8		/* 16 */
40a87d28c0Smark #define	INCRMT	0200		/* 0100 */
41a87d28c0Smark #define	LBTMSK	0770		/* 0760 */
42a87d28c0Smark #define	NMBLKS	506		/* 1018 */
43a87d28c0Smark #define	OFFBTS	7		/* 6 */
44a87d28c0Smark #define	OFFMSK	0177		/* 077 */
45a87d28c0Smark #define	SHFT	2		/* 3 */
46cde8cdf9Smark #else
47cde8cdf9Smark #define	BLKMSK	077777
48cde8cdf9Smark #define	BNDRY	2
49cde8cdf9Smark #define	INCRMT	02000
50cde8cdf9Smark #define	LBTMSK	01776
51cde8cdf9Smark #define	NMBLKS	077770
52cde8cdf9Smark #define	OFFBTS	10
53cde8cdf9Smark #define	OFFMSK	01777
54cde8cdf9Smark #define	SHFT	0
55cde8cdf9Smark #endif
56a87d28c0Smark 
57a87d28c0Smark /*
58a87d28c0Smark  * The editor uses three buffers into the temporary file (ed uses two
59a87d28c0Smark  * and is very similar).  These are two read buffers and one write buffer.
60cde8cdf9Smark  * Basically, the editor deals with the file as a sequence of BUFSIZ character
61cde8cdf9Smark  * blocks.  Each block contains some number of lines (and lines
62a87d28c0Smark  * can run across block boundaries.
63a87d28c0Smark  *
64a87d28c0Smark  * New lines are written into the last block in the temporary file
65a87d28c0Smark  * which is in core as obuf.  When a line is needed which isn't in obuf,
66a87d28c0Smark  * then it is brought into an input buffer.  As there are two, the choice
67a87d28c0Smark  * is to take the buffer into which the last read (of the two) didn't go.
68a87d28c0Smark  * Thus this is a 2 buffer LRU replacement strategy.  Measurement
69a87d28c0Smark  * shows that this saves roughly 25% of the buffer reads over a one
70a87d28c0Smark  * input buffer strategy.  Since the editor (on our VAX over 1 week)
71a87d28c0Smark  * spends (spent) roughly 30% of its time in the system read routine,
72a87d28c0Smark  * this can be a big help.
73a87d28c0Smark  */
740be8406aSdist var bool	hitin2;		/* Last read hit was ibuff2 not ibuff */
750be8406aSdist var bool	ichang2;	/* Have actually changed ibuff2 */
760be8406aSdist var bool	ichanged;	/* Have actually changed ibuff */
770be8406aSdist var short	iblock;		/* Temp file block number of ibuff (or -1) */
780be8406aSdist var short	iblock2;	/* Temp file block number of ibuff2 (or -1) */
790be8406aSdist var short	ninbuf;		/* Number useful chars left in input buffer */
800be8406aSdist var short	nleft;		/* Number usable chars left in output buffer */
810be8406aSdist var short	oblock;		/* Temp file block number of obuff (or -1) */
82cde8cdf9Smark #ifndef VMUNIX
830be8406aSdist var short	tline;		/* Current temp file ptr */
84cde8cdf9Smark #else
850be8406aSdist var int	tline;
86cde8cdf9Smark #endif
87a87d28c0Smark 
880be8406aSdist var char	ibuff[BUFSIZ];
890be8406aSdist var char	ibuff2[BUFSIZ];
900be8406aSdist var char	obuff[BUFSIZ];
91a87d28c0Smark 
92a87d28c0Smark /*
93a87d28c0Smark  * Structure of the descriptor block which resides
94a87d28c0Smark  * in the first block of the temporary file and is
95a87d28c0Smark  * the guiding light for crash recovery.
96a87d28c0Smark  *
97a87d28c0Smark  * As the Blocks field below implies, there are temporary file blocks
98a87d28c0Smark  * devoted to (some) image of the incore array of pointers into the temp
99a87d28c0Smark  * file.  Thus, to recover from a crash we use these indices to get the
100a87d28c0Smark  * line pointers back, and then use the line pointers to get the text back.
101a87d28c0Smark  * Except for possible lost lines due to sandbagged I/O, the entire
102a87d28c0Smark  * file (at the time of the last editor "sync") can be recovered from
103a87d28c0Smark  * the temp file.
104a87d28c0Smark  */
105a87d28c0Smark 
106a87d28c0Smark /* This definition also appears in expreserve.c... beware */
107a87d28c0Smark struct 	header {
108a87d28c0Smark 	time_t	Time;			/* Time temp file last updated */
1090be8406aSdist 	int	Uid;
110cde8cdf9Smark #ifndef VMUNIX
111a87d28c0Smark 	short	Flines;			/* Number of lines in file */
112cde8cdf9Smark #else
113cde8cdf9Smark 	int	Flines;
114cde8cdf9Smark #endif
115a87d28c0Smark 	char	Savedfile[FNSIZE];	/* The current file name */
116a87d28c0Smark 	short	Blocks[LBLKS];		/* Blocks where line pointers stashed */
1170be8406aSdist };
1180be8406aSdist var struct 	header H;
119a87d28c0Smark 
120a87d28c0Smark #define	uid		H.Uid
121a87d28c0Smark #define	flines		H.Flines
122a87d28c0Smark #define	savedfile	H.Savedfile
123a87d28c0Smark #define	blocks		H.Blocks
124