re-enable munmap().
[minix.git] / commands / elle / sb.h
bloba4808f7d56f3a39fa02d83d8fca975df127ea6bf
1 /* SB - Copyright 1982 by Ken Harrenstien, SRI International
2 * This software is quasi-public; it may be used freely with
3 * like software, but may NOT be sold or made part of licensed
4 * products without permission of the author. In all cases
5 * the source code and any modifications thereto must remain
6 * available to any user.
8 * This is part of the SB library package.
9 * Any software using the SB library must likewise be made
10 * quasi-public, with freely available sources.
13 #ifdef COMMENT
15 The initials "SB" stand for "String Block" or "String Buffer".
17 SBBUFFER - A SB buffer containing a sbstring opened for editing.
18 SBFILE - A structure holding file-specific information for all
19 SDBLKs pointing to that file.
20 SBSTRING - A SB string; conceptually a single string, but actually
21 a linked list of SDBLKs. Unless opened by a SBBUFFER,
22 only a few operations are allowed on SBSTRINGs (creating,
23 copying, deleting).
24 SDBLK - One of the linked nodes constituting a sbstring. Each SDBLK
25 node points to a continuous string either in memory or
26 on disk, or both.
27 SBLK - Another name for SDBLK.
28 SMBLK - An allocated chunk of memory. Also refers to the node structure
29 maintained by the SBM memory management routines, which
30 points to the actual chunk of memory.
31 SBM - Name of the memory management package. SBM routines are used
32 to allocate memory in general, and are not just for
33 use by SB routines.
35 ************ MACHINE DEPENDENT DEFINITIONS **********
37 The following compile time definitions represent machine
38 dependent parameters which are intended mainly for use only by SBM and
39 SBSTR routines. Other programs should use them with caution. Note
40 that a great deal of code assumes that type "int" corresponds to a basic
41 machine word (as per C Reference Manual).
43 The current definitions will only work for machines which have
44 1, 2, 4, or 8 "char" bytes in a machine word. Any other size will
45 require some changes to the definitions and possibly to some places
46 using them.
48 WORD - integer-type definition corresponding to machine word.
49 WDSIZE - # addressable char bytes in a machine word. (1, 2, 4, 8)
50 WDBITS - # low order bits in an address, ie log2(WDSIZE). (0, 1, 2, 3)
51 WDMASK - Mask for low order bits of address (0, 1, 3, 7)
52 CHAR_MASK - If defined, machine does sign-extension on chars, and
53 they must be masked with this value.
55 Note that the macro for WDBITS has no mathematical significance
56 other than being an expression which happens to evaluate into the right
57 constant for the 4 allowed values of WDSIZE, and in fact it is this
58 crock which restricts WDSIZE! If C had a base 2 logarithm expression
59 then any power of 2 could be used.
61 Values for machines
62 WORD WDSIZE WDBITS WDMASK
63 PDP11, Z8000, I8086 int 2 1 01
64 VAX11, M68000, PDP10 int 4 2 03
66 #endif /* COMMENT */
68 /* First try to define a few things in a semi-portable way
70 #include "eesite.h"
71 #ifdef __STDC__ /* Implementation supports ANSI stuff? */
72 #include <limits.h> /* Get sizes for char stuff */
73 #define _SBMUCHAR 1 /* Can use "unsigned char" */
74 #define _SBMCHARSIGN (CHAR_MIN < 0) /* True if "char" is sign-extended */
75 #define CHAR_MASK (UCHAR_MAX)
77 #else /* not ANSI */
78 #ifndef _SBMUCHAR /* Default assumes no "unsigned char" */
79 #define _SBMUCHAR 0
80 #endif
81 #ifndef _SBMCHARSIGN /* Default assumes "char" is sign-extended */
82 #define _SBMCHARSIGN 1
83 #endif
84 #ifndef CHAR_MASK /* Default assumes "char" is 8 bits */
85 #define CHAR_MASK 0377
86 #endif
87 #endif /* not ANSI */
89 /* Define "sb_uchartoint" as a macro which ensures that an unsigned
90 ** character value is converted properly to an int value.
92 #if (_SBMUCHAR || (_SBMCHARSIGN==0))
93 #define sb_uchartoint(a) (a) /* No fear of sign extension */
94 #else
95 #define sb_uchartoint(a) ((a)&CHAR_MASK) /* Bah, sign extension */
96 #endif
99 /* Defs for machines with a base-2 WDSIZE. Yes, the (int) is indeed necessary
100 * (to allow implicit conversion to long where needed - the PDP11 compiler
101 * is known to lose without it, because sizeof is cast as "unsigned int"
102 * which loses big in long masks!)
104 #define WORD int
105 #define WDSIZE ((int)(sizeof(WORD)))
106 #define WDMASK (WDSIZE-1)
107 #define WDBITS ((WDSIZE>>2)+(1&WDMASK))
109 #define rnddiv(a) ((a)>>WDBITS) /* # words, rounded down */
110 #define rndrem(a) ((a)&WDMASK) /* # bytes remaining past wd bndary */
111 #define rnddwn(a) ((a)&~WDMASK) /* Round down to word boundary */
112 #define rndup(a) rnddwn((a)+WDSIZE-1) /* Round up to word boundary */
114 #ifdef COMMENT /* The following are for machines without a base-2 WDSIZE */
115 #define rnddiv(a) ((a)/WDSIZE)
116 #define rndrem(a) ((a)%WDSIZE)
117 #define rnddwn(a) ((a)-rndrem(a))
118 #define rndup(a) rnddwn((a)+WDSIZE-1)
119 #undef WDMASK /* These become meaningless and anything */
120 #undef WDBITS /* which uses them should be changed! */
121 #endif /* COMMENT */
123 /* The following 3 definitions are somewhat machine-dependent,
124 * but are specifically intended for general use and work for all
125 * currently known C implementations.
126 * SBMO must be an integer-type object large enough to hold
127 * the largest difference in SBMA pointers, and must not be
128 * used in signed comparisons.
131 typedef long chroff; /* CHROFF - Char offset in disk/sbstr */
132 typedef unsigned int SBMO; /* SBMO - Char offset in memory */
133 typedef
134 #if _SBMUCHAR
135 unsigned
136 #endif
137 char *SBMA; /* SBMA - Pointer to char loc in memory */
141 /* The following definitions tend to be system-dependent. Only the
142 * SBM and SBSTR routines use them.
144 #define SB_NFILES 32 /* # of open files we can hack. Actually
145 * this is max FD value plus 1. */
146 #define SB_BUFSIZ 512 /* Optimal buffer size (system block size) */
147 #define SB_SLOP (16*WDSIZE) /* # slop chars to tolerate for allocations */
149 #define SMNODES (20) /* # SM or SD nodes to create when needed */
150 #define SMCHUNKSIZ (16*512) /* # bytes of mem to create (via sbrk) " " */
151 #define MAXSBMO ((SBMO)-1) /* Used in SBM only */
152 /* MAXSBMO should be the largest possible SBMO value. */
154 #define EOF (-1)
155 #define SBFILE struct sbfile
156 #define SBBUF struct sbbuffer
157 #define SBSTR struct sdblk /* Start of a sbstring */
159 struct sbfile {
160 int sfflags; /* Various flags */
161 int sffd; /* FD for file (-1 if none) */
162 struct sdblk *sfptr1; /* Ptr to 1st node in phys list */
163 chroff sflen; /* Original length of file FD is for */
166 /* Definition of SBBUF string/buffer */
167 struct sbbuffer {
168 SBMA sbiop; /* I/O pointer into in-core text */
169 int sbrleft; /* # chars left for reading */
170 int sbwleft; /* # chars left for writing */
171 int sbflags; /* Various flags */
172 chroff sbdot; /* Logical pos for start of current sdblk */
173 chroff sboff; /* Offset into current sdblk (if no smblk)*/
174 struct sdblk *sbcur; /* Pointer to current SD block of string */
176 /* Flags for "sbflags" */
177 #define SB_OVW 01 /* Over-write mode */
178 #define SB_WRIT 02 /* Written; smuse needs to be updated from sbiop */
180 /* NOTE: An unused sbbuf structure should be completely zeroed.
181 * This will cause routines to handle it properly
182 * if they are accidentally pointed at it.
185 /* Definition of SDBLK */
186 struct sdblk {
187 struct sdblk *slforw; /* Logical sequence forward link */
188 struct sdblk *slback; /* Logical sequence backward link */
189 int sdflags;
190 struct sdblk *sdforw; /* Physical sequence (disk) */
191 struct sdblk *sdback; /* ditto - backptr for easy flushing */
192 struct smblk *sdmem; /* Mem pointer, 0 if no in-core version */
193 SBFILE *sdfile; /* File pointer, 0 if no disk version */
194 chroff sdlen; /* # chars in disk text */
195 chroff sdaddr; /* Disk address of text */
197 /* Flags for "sdflags" */
198 #define SD_LOCK 0100000 /* Locked because opened by a SBBUF */
199 #define SD_LCK2 0040000 /* Locked for other reasons */
200 #define SD_MOD 0020000 /* Modified, mem blk is real stuff */
201 #define SD_NID 0323 /* Node ID marks active (not on freelist) */
202 #define SD_LOCKS (SD_LOCK|SD_LCK2)
204 /* Note sdback is ONLY needed for fixing up phys list when a sdblk is
205 * deleted (so as to find previous blk in phys list). Perhaps it shd
206 * be flushed (ie only use SDFORW)? How to do deletions - use circular
207 * list? Sigh.
210 /* Definition of SMBLK (used by SBM routines) */
211 struct smblk {
212 struct smblk *smforw; /* Links to other mem blks, in phys order */
213 struct smblk *smback;
214 int smflags; /* Type, in-use flags */
215 SBMA smaddr; /* Mem address of text */
216 SBMO smlen; /* # bytes in mem block */
217 SBMO smuse; /* # bytes "used" in block */
219 /* Flags for "smflags" */
220 #define SM_USE 0100000 /* Block is in use (mem free if off) */
221 #define SM_NXM 040000 /* Block mem is non-existent */
222 #define SM_EXT 020000 /* Block mem owned by external (non-SBM) rtn*/
223 #define SM_MNODS 010000 /* Block holds SMBLK nodes */
224 #define SM_DNODS 04000 /* Block holds SDBLK nodes */
225 #define SM_NID 0315 /* Node in-use identifier (low byte) */
227 /* Error handler type values */
228 #define SBMERR 0 /* Error in SBM package */
229 #define SBXERR 1 /* Error in SBSTR package */
230 #define SBFERR 2 /* "Error" - SBSTR package found a file overwritten.
231 * Non-zero return will continue normally. */
234 /* Redefine certain external symbols to be unique in the first 6 chars
235 ** to conform with ANSI requirements.
237 #define sbm_nfre sbmnfre /* SBM stuff */
238 #define sbm_nfor sbmnfor
239 #define sbm_nmov sbmnmov
240 #define sbm_ngc sbmngc
241 #define sbx_ndget sbxndg /* SBSTR stuff */
242 #define sbx_ndel sbxnde
243 #define sbx_ndfre sbxndf
244 #define sbx_sdcpy sbxsdc
245 #define sbx_sdgc sbxsdg
246 #define sbe_sdlist sbesls /* SBERR stuff */
247 #define sbe_sdtab sbestb
248 #define sbe_sds sbesds
249 #define sbe_sbvfy sbesbv
250 #define sbe_sbs sbesbs
252 /* Forward declarations */
253 extern SBMA sbm_lowaddr; /* For roundoff purposes */
255 extern SBFILE sbv_tf; /* SBFILE for temp swapout file */
256 extern int (*sbv_debug)(); /* Error handler address */
257 extern off_t lseek(); /* For sbstr code mostly */
258 extern char *mktemp();
259 extern char *malloc();
260 extern char *calloc();
261 extern SBBUF *sb_open();
262 extern SBSTR *sb_close(), *sb_fduse(), *sbs_cpy(), *sbs_app(), *sb_cpyn(),
263 *sb_killn();
264 extern struct sdblk *sbx_ready();
265 extern chroff sb_tell(), sb_ztell(), sbs_len();
267 /* Definition of SB_GETC, SB_PUTC, SB_BACKC macros */
269 #define sb_putc(s,c) (--((s)->sbwleft) >= 0 ? \
270 (*(s)->sbiop++ = c) : sb_sputc(s,c))
271 #define sb_getc(s) (--((s)->sbrleft) >= 0 ? \
272 sb_uchartoint(*(s)->sbiop++) : sb_sgetc(s))
273 #define sb_peekc(s) ((s)->sbrleft > 0 ? \
274 sb_uchartoint(*(s)->sbiop) : sb_speekc(s))
276 /* WARNING - sb_backc must ONLY be used if last operation was a
277 * successful sb_getc!! For slow but sure invocation use sb_rgetc.
279 #define sb_backc(s) (++(s->sbrleft), --(s->sbiop))
281 #include "sbproto.h" /* function prototypes */