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