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.
|
---|
7 | *
|
---|
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.
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifdef COMMENT
|
---|
14 |
|
---|
15 | The initials "SB" stand for "String Block" or "String Buffer".
|
---|
16 |
|
---|
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.
|
---|
34 |
|
---|
35 | ************ MACHINE DEPENDENT DEFINITIONS **********
|
---|
36 |
|
---|
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).
|
---|
42 |
|
---|
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.
|
---|
47 |
|
---|
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.
|
---|
54 |
|
---|
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.
|
---|
60 |
|
---|
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
|
---|
65 |
|
---|
66 | #endif /* COMMENT */
|
---|
67 | |
---|
68 |
|
---|
69 | /* First try to define a few things in a semi-portable way
|
---|
70 | */
|
---|
71 | #include "eesite.h"
|
---|
72 | #ifdef __STDC__ /* Implementation supports ANSI stuff? */
|
---|
73 | #include <limits.h> /* Get sizes for char stuff */
|
---|
74 | #define _SBMUCHAR 1 /* Can use "unsigned char" */
|
---|
75 | #define _SBMCHARSIGN (CHAR_MIN < 0) /* True if "char" is sign-extended */
|
---|
76 | #define CHAR_MASK (UCHAR_MAX)
|
---|
77 |
|
---|
78 | #else /* not ANSI */
|
---|
79 | #ifndef _SBMUCHAR /* Default assumes no "unsigned char" */
|
---|
80 | #define _SBMUCHAR 0
|
---|
81 | #endif
|
---|
82 | #ifndef _SBMCHARSIGN /* Default assumes "char" is sign-extended */
|
---|
83 | #define _SBMCHARSIGN 1
|
---|
84 | #endif
|
---|
85 | #ifndef CHAR_MASK /* Default assumes "char" is 8 bits */
|
---|
86 | #define CHAR_MASK 0377
|
---|
87 | #endif
|
---|
88 | #endif /* not ANSI */
|
---|
89 |
|
---|
90 | /* Define "sb_uchartoint" as a macro which ensures that an unsigned
|
---|
91 | ** character value is converted properly to an int value.
|
---|
92 | */
|
---|
93 | #if (_SBMUCHAR || (_SBMCHARSIGN==0))
|
---|
94 | #define sb_uchartoint(a) (a) /* No fear of sign extension */
|
---|
95 | #else
|
---|
96 | #define sb_uchartoint(a) ((a)&CHAR_MASK) /* Bah, sign extension */
|
---|
97 | #endif
|
---|
98 |
|
---|
99 |
|
---|
100 | /* Defs for machines with a base-2 WDSIZE. Yes, the (int) is indeed necessary
|
---|
101 | * (to allow implicit conversion to long where needed - the PDP11 compiler
|
---|
102 | * is known to lose without it, because sizeof is cast as "unsigned int"
|
---|
103 | * which loses big in long masks!)
|
---|
104 | */
|
---|
105 | #define WORD int
|
---|
106 | #define WDSIZE ((int)(sizeof(WORD)))
|
---|
107 | #define WDMASK (WDSIZE-1)
|
---|
108 | #define WDBITS ((WDSIZE>>2)+(1&WDMASK))
|
---|
109 |
|
---|
110 | #define rnddiv(a) ((a)>>WDBITS) /* # words, rounded down */
|
---|
111 | #define rndrem(a) ((a)&WDMASK) /* # bytes remaining past wd bndary */
|
---|
112 | #define rnddwn(a) ((a)&~WDMASK) /* Round down to word boundary */
|
---|
113 | #define rndup(a) rnddwn((a)+WDSIZE-1) /* Round up to word boundary */
|
---|
114 |
|
---|
115 | #ifdef COMMENT /* The following are for machines without a base-2 WDSIZE */
|
---|
116 | #define rnddiv(a) ((a)/WDSIZE)
|
---|
117 | #define rndrem(a) ((a)%WDSIZE)
|
---|
118 | #define rnddwn(a) ((a)-rndrem(a))
|
---|
119 | #define rndup(a) rnddwn((a)+WDSIZE-1)
|
---|
120 | #undef WDMASK /* These become meaningless and anything */
|
---|
121 | #undef WDBITS /* which uses them should be changed! */
|
---|
122 | #endif /* COMMENT */
|
---|
123 |
|
---|
124 | /* The following 3 definitions are somewhat machine-dependent,
|
---|
125 | * but are specifically intended for general use and work for all
|
---|
126 | * currently known C implementations.
|
---|
127 | * SBMO must be an integer-type object large enough to hold
|
---|
128 | * the largest difference in SBMA pointers, and must not be
|
---|
129 | * used in signed comparisons.
|
---|
130 | */
|
---|
131 |
|
---|
132 | typedef long chroff; /* CHROFF - Char offset in disk/sbstr */
|
---|
133 | typedef unsigned int SBMO; /* SBMO - Char offset in memory */
|
---|
134 | typedef
|
---|
135 | #if _SBMUCHAR
|
---|
136 | unsigned
|
---|
137 | #endif
|
---|
138 | char *SBMA; /* SBMA - Pointer to char loc in memory */
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | /* The following definitions tend to be system-dependent. Only the
|
---|
143 | * SBM and SBSTR routines use them.
|
---|
144 | */
|
---|
145 | #define SB_NFILES 32 /* # of open files we can hack. Actually
|
---|
146 | * this is max FD value plus 1. */
|
---|
147 | #define SB_BUFSIZ 512 /* Optimal buffer size (system block size) */
|
---|
148 | #define SB_SLOP (16*WDSIZE) /* # slop chars to tolerate for allocations */
|
---|
149 |
|
---|
150 | #define SMNODES (20) /* # SM or SD nodes to create when needed */
|
---|
151 | #define SMCHUNKSIZ (16*512) /* # bytes of mem to create (via sbrk) " " */
|
---|
152 | #define MAXSBMO ((SBMO)-1) /* Used in SBM only */
|
---|
153 | /* MAXSBMO should be the largest possible SBMO value. */
|
---|
154 | |
---|
155 |
|
---|
156 | #define EOF (-1)
|
---|
157 | #define SBFILE struct sbfile
|
---|
158 | #define SBBUF struct sbbuffer
|
---|
159 | #define SBSTR struct sdblk /* Start of a sbstring */
|
---|
160 |
|
---|
161 | struct sbfile {
|
---|
162 | int sfflags; /* Various flags */
|
---|
163 | int sffd; /* FD for file (-1 if none) */
|
---|
164 | struct sdblk *sfptr1; /* Ptr to 1st node in phys list */
|
---|
165 | chroff sflen; /* Original length of file FD is for */
|
---|
166 | };
|
---|
167 |
|
---|
168 | /* Definition of SBBUF string/buffer */
|
---|
169 | struct sbbuffer {
|
---|
170 | SBMA sbiop; /* I/O pointer into in-core text */
|
---|
171 | int sbrleft; /* # chars left for reading */
|
---|
172 | int sbwleft; /* # chars left for writing */
|
---|
173 | int sbflags; /* Various flags */
|
---|
174 | chroff sbdot; /* Logical pos for start of current sdblk */
|
---|
175 | chroff sboff; /* Offset into current sdblk (if no smblk)*/
|
---|
176 | struct sdblk *sbcur; /* Pointer to current SD block of string */
|
---|
177 | };
|
---|
178 | /* Flags for "sbflags" */
|
---|
179 | #define SB_OVW 01 /* Over-write mode */
|
---|
180 | #define SB_WRIT 02 /* Written; smuse needs to be updated from sbiop */
|
---|
181 |
|
---|
182 | /* NOTE: An unused sbbuf structure should be completely zeroed.
|
---|
183 | * This will cause routines to handle it properly
|
---|
184 | * if they are accidentally pointed at it.
|
---|
185 | */
|
---|
186 |
|
---|
187 | /* Definition of SDBLK */
|
---|
188 | struct sdblk {
|
---|
189 | struct sdblk *slforw; /* Logical sequence forward link */
|
---|
190 | struct sdblk *slback; /* Logical sequence backward link */
|
---|
191 | int sdflags;
|
---|
192 | struct sdblk *sdforw; /* Physical sequence (disk) */
|
---|
193 | struct sdblk *sdback; /* ditto - backptr for easy flushing */
|
---|
194 | struct smblk *sdmem; /* Mem pointer, 0 if no in-core version */
|
---|
195 | SBFILE *sdfile; /* File pointer, 0 if no disk version */
|
---|
196 | chroff sdlen; /* # chars in disk text */
|
---|
197 | chroff sdaddr; /* Disk address of text */
|
---|
198 | };
|
---|
199 | /* Flags for "sdflags" */
|
---|
200 | #define SD_LOCK 0100000 /* Locked because opened by a SBBUF */
|
---|
201 | #define SD_LCK2 0040000 /* Locked for other reasons */
|
---|
202 | #define SD_MOD 0020000 /* Modified, mem blk is real stuff */
|
---|
203 | #define SD_NID 0323 /* Node ID marks active (not on freelist) */
|
---|
204 | #define SD_LOCKS (SD_LOCK|SD_LCK2)
|
---|
205 |
|
---|
206 | /* Note sdback is ONLY needed for fixing up phys list when a sdblk is
|
---|
207 | * deleted (so as to find previous blk in phys list). Perhaps it shd
|
---|
208 | * be flushed (ie only use SDFORW)? How to do deletions - use circular
|
---|
209 | * list? Sigh.
|
---|
210 | */
|
---|
211 |
|
---|
212 | /* Definition of SMBLK (used by SBM routines) */
|
---|
213 | struct smblk {
|
---|
214 | struct smblk *smforw; /* Links to other mem blks, in phys order */
|
---|
215 | struct smblk *smback;
|
---|
216 | int smflags; /* Type, in-use flags */
|
---|
217 | SBMA smaddr; /* Mem address of text */
|
---|
218 | SBMO smlen; /* # bytes in mem block */
|
---|
219 | SBMO smuse; /* # bytes "used" in block */
|
---|
220 | };
|
---|
221 | /* Flags for "smflags" */
|
---|
222 | #define SM_USE 0100000 /* Block is in use (mem free if off) */
|
---|
223 | #define SM_NXM 040000 /* Block mem is non-existent */
|
---|
224 | #define SM_EXT 020000 /* Block mem owned by external (non-SBM) rtn*/
|
---|
225 | #define SM_MNODS 010000 /* Block holds SMBLK nodes */
|
---|
226 | #define SM_DNODS 04000 /* Block holds SDBLK nodes */
|
---|
227 | #define SM_NID 0315 /* Node in-use identifier (low byte) */
|
---|
228 |
|
---|
229 | /* Error handler type values */
|
---|
230 | #define SBMERR 0 /* Error in SBM package */
|
---|
231 | #define SBXERR 1 /* Error in SBSTR package */
|
---|
232 | #define SBFERR 2 /* "Error" - SBSTR package found a file overwritten.
|
---|
233 | * Non-zero return will continue normally. */
|
---|
234 |
|
---|
235 |
|
---|
236 | /* Redefine certain external symbols to be unique in the first 6 chars
|
---|
237 | ** to conform with ANSI requirements.
|
---|
238 | */
|
---|
239 | #define sbm_nfre sbmnfre /* SBM stuff */
|
---|
240 | #define sbm_nfor sbmnfor
|
---|
241 | #define sbm_nmov sbmnmov
|
---|
242 | #define sbm_ngc sbmngc
|
---|
243 | #define sbx_ndget sbxndg /* SBSTR stuff */
|
---|
244 | #define sbx_ndel sbxnde
|
---|
245 | #define sbx_ndfre sbxndf
|
---|
246 | #define sbx_sdcpy sbxsdc
|
---|
247 | #define sbx_sdgc sbxsdg
|
---|
248 | #define sbe_sdlist sbesls /* SBERR stuff */
|
---|
249 | #define sbe_sdtab sbestb
|
---|
250 | #define sbe_sds sbesds
|
---|
251 | #define sbe_sbvfy sbesbv
|
---|
252 | #define sbe_sbs sbesbs
|
---|
253 |
|
---|
254 | /* Forward declarations */
|
---|
255 | extern SBMA sbm_lowaddr; /* For roundoff purposes */
|
---|
256 |
|
---|
257 | extern SBFILE sbv_tf; /* SBFILE for temp swapout file */
|
---|
258 | extern int (*sbv_debug)(); /* Error handler address */
|
---|
259 | extern off_t lseek(); /* For sbstr code mostly */
|
---|
260 | extern char *mktemp();
|
---|
261 | extern char *malloc();
|
---|
262 | extern char *calloc();
|
---|
263 | extern SBBUF *sb_open();
|
---|
264 | extern SBSTR *sb_close(), *sb_fduse(), *sbs_cpy(), *sbs_app(), *sb_cpyn(),
|
---|
265 | *sb_killn();
|
---|
266 | extern struct sdblk *sbx_ready();
|
---|
267 | extern chroff sb_tell(), sb_ztell(), sbs_len();
|
---|
268 | |
---|
269 |
|
---|
270 | /* Definition of SB_GETC, SB_PUTC, SB_BACKC macros */
|
---|
271 |
|
---|
272 | #define sb_putc(s,c) (--((s)->sbwleft) >= 0 ? \
|
---|
273 | (*(s)->sbiop++ = c) : sb_sputc(s,c))
|
---|
274 | #define sb_getc(s) (--((s)->sbrleft) >= 0 ? \
|
---|
275 | sb_uchartoint(*(s)->sbiop++) : sb_sgetc(s))
|
---|
276 | #define sb_peekc(s) ((s)->sbrleft > 0 ? \
|
---|
277 | sb_uchartoint(*(s)->sbiop) : sb_speekc(s))
|
---|
278 |
|
---|
279 | /* WARNING - sb_backc must ONLY be used if last operation was a
|
---|
280 | * successful sb_getc!! For slow but sure invocation use sb_rgetc.
|
---|
281 | */
|
---|
282 | #define sb_backc(s) (++(s->sbrleft), --(s->sbiop))
|
---|
283 |
|
---|
284 | #include "sbproto.h" /* function prototypes */
|
---|