sb.h

Go to the documentation of this file.
00001 /* SB - Copyright 1982 by Ken Harrenstien, SRI International
00002  *      This software is quasi-public; it may be used freely with
00003  *      like software, but may NOT be sold or made part of licensed
00004  *      products without permission of the author.  In all cases
00005  *      the source code and any modifications thereto must remain
00006  *      available to any user.
00007  *
00008  *      This is part of the SB library package.
00009  *      Any software using the SB library must likewise be made
00010  *      quasi-public, with freely available sources.
00011  */
00012 
00013 #ifdef COMMENT
00014 
00015 The initials "SB" stand for "String Block" or "String Buffer".
00016 
00017 SBBUFFER - A SB buffer containing a sbstring opened for editing.
00018 SBFILE   - A structure holding file-specific information for all
00019                 SDBLKs pointing to that file.
00020 SBSTRING - A SB string; conceptually a single string, but actually
00021                 a linked list of SDBLKs.  Unless opened by a SBBUFFER,
00022                 only a few operations are allowed on SBSTRINGs (creating,
00023                 copying, deleting).
00024 SDBLK    - One of the linked nodes constituting a sbstring.  Each SDBLK
00025                 node points to a continuous string either in memory or
00026                 on disk, or both.
00027 SBLK     - Another name for SDBLK.
00028 SMBLK    - An allocated chunk of memory.  Also refers to the node structure
00029                 maintained by the SBM memory management routines, which
00030                 points to the actual chunk of memory.
00031 SBM      - Name of the memory management package.  SBM routines are used
00032                 to allocate memory in general, and are not just for
00033                 use by SB routines.
00034 
00035 ************ MACHINE DEPENDENT DEFINITIONS **********
00036 
00037         The following compile time definitions represent machine
00038 dependent parameters which are intended mainly for use only by SBM and
00039 SBSTR routines.  Other programs should use them with caution.  Note
00040 that a great deal of code assumes that type "int" corresponds to a basic
00041 machine word (as per C Reference Manual).
00042 
00043         The current definitions will only work for machines which have
00044 1, 2, 4, or 8 "char" bytes in a machine word.  Any other size will
00045 require some changes to the definitions and possibly to some places
00046 using them.
00047 
00048 WORD   - integer-type definition corresponding to machine word.
00049 WDSIZE - # addressable char bytes in a machine word.            (1, 2, 4, 8)
00050 WDBITS - # low order bits in an address, ie log2(WDSIZE).       (0, 1, 2, 3)
00051 WDMASK - Mask for low order bits of address                     (0, 1, 3, 7)
00052 CHAR_MASK - If defined, machine does sign-extension on chars, and
00053         they must be masked with this value.
00054 
00055         Note that the macro for WDBITS has no mathematical significance
00056 other than being an expression which happens to evaluate into the right
00057 constant for the 4 allowed values of WDSIZE, and in fact it is this
00058 crock which restricts WDSIZE!  If C had a base 2 logarithm expression
00059 then any power of 2 could be used.
00060 
00061 Values for machines
00062                                 WORD    WDSIZE  WDBITS  WDMASK
00063         PDP11, Z8000, I8086     int     2       1       01
00064         VAX11, M68000, PDP10    int     4       2       03
00065 
00066 #endif /* COMMENT */
00067 
00068 /* First try to define a few things in a semi-portable way
00069 */
00070 #include "eesite.h"
00071 #ifdef __STDC__         /* Implementation supports ANSI stuff? */
00072 #include <limits.h>             /* Get sizes for char stuff */
00073 #define _SBMUCHAR 1             /* Can use "unsigned char" */
00074 #define _SBMCHARSIGN (CHAR_MIN < 0)     /* True if "char" is sign-extended */
00075 #define CHAR_MASK (UCHAR_MAX)
00076 
00077 #else   /* not ANSI */
00078 #ifndef _SBMUCHAR               /* Default assumes no "unsigned char" */
00079 #define _SBMUCHAR 0
00080 #endif
00081 #ifndef _SBMCHARSIGN            /* Default assumes "char" is sign-extended */
00082 #define _SBMCHARSIGN 1
00083 #endif
00084 #ifndef CHAR_MASK               /* Default assumes "char" is 8 bits */
00085 #define CHAR_MASK 0377
00086 #endif
00087 #endif  /* not ANSI */
00088 
00089 /* Define "sb_uchartoint" as a macro which ensures that an unsigned
00090 ** character value is converted properly to an int value.
00091 */
00092 #if (_SBMUCHAR || (_SBMCHARSIGN==0))
00093 #define sb_uchartoint(a) (a)            /* No fear of sign extension */
00094 #else
00095 #define sb_uchartoint(a) ((a)&CHAR_MASK)        /* Bah, sign extension */
00096 #endif
00097 
00098 
00099 /* Defs for machines with a base-2 WDSIZE.  Yes, the (int) is indeed necessary
00100  * (to allow implicit conversion to long where needed - the PDP11 compiler
00101  * is known to lose without it, because sizeof is cast as "unsigned int"
00102  * which loses big in long masks!)
00103  */
00104 #define WORD int
00105 #define WDSIZE ((int)(sizeof(WORD)))
00106 #define WDMASK (WDSIZE-1)
00107 #define WDBITS ((WDSIZE>>2)+(1&WDMASK))
00108 
00109 #define rnddiv(a) ((a)>>WDBITS)         /* # words, rounded down */
00110 #define rndrem(a) ((a)&WDMASK)          /* # bytes remaining past wd bndary */
00111 #define rnddwn(a) ((a)&~WDMASK)         /* Round down to word boundary */
00112 #define rndup(a)  rnddwn((a)+WDSIZE-1)  /* Round up to word boundary */
00113 
00114 #ifdef COMMENT  /* The following are for machines without a base-2 WDSIZE */
00115 #define rnddiv(a) ((a)/WDSIZE)
00116 #define rndrem(a) ((a)%WDSIZE)
00117 #define rnddwn(a) ((a)-rndrem(a))
00118 #define rndup(a)  rnddwn((a)+WDSIZE-1)
00119 #undef WDMASK                   /* These become meaningless and anything */
00120 #undef WDBITS                   /* which uses them should be changed! */
00121 #endif /* COMMENT */
00122 
00123 /* The following 3 definitions are somewhat machine-dependent,
00124  * but are specifically intended for general use and work for all
00125  * currently known C implementations.
00126  *      SBMO must be an integer-type object large enough to hold
00127  *      the largest difference in SBMA pointers, and must not be
00128  *      used in signed comparisons.
00129  */
00130 
00131 typedef long chroff;            /* CHROFF - Char offset in disk/sbstr */
00132 typedef unsigned int SBMO;      /* SBMO - Char offset in memory */
00133 typedef
00134 #if _SBMUCHAR
00135         unsigned
00136 #endif
00137                 char *SBMA;     /* SBMA - Pointer to char loc in memory */
00138 
00139 
00140 
00141 /* The following definitions tend to be system-dependent.  Only the
00142  * SBM and SBSTR routines use them.
00143  */
00144 #define SB_NFILES 32            /* # of open files we can hack.  Actually
00145                                  * this is max FD value plus 1. */
00146 #define SB_BUFSIZ 512           /* Optimal buffer size (system block size) */
00147 #define SB_SLOP (16*WDSIZE)     /* # slop chars to tolerate for allocations */
00148 
00149 #define SMNODES (20)            /* # SM or SD nodes to create when needed */
00150 #define SMCHUNKSIZ (16*512)     /* # bytes of mem to create (via sbrk) " " */
00151 #define MAXSBMO ((SBMO)-1)      /* Used in SBM only */
00152                 /* MAXSBMO should be the largest possible SBMO value. */
00153 
00154 #define EOF (-1)
00155 #define SBFILE struct sbfile
00156 #define SBBUF struct sbbuffer
00157 #define SBSTR struct sdblk      /* Start of a sbstring */
00158 
00159 struct sbfile {
00160         int sfflags;            /* Various flags */
00161         int sffd;               /* FD for file (-1 if none) */
00162         struct sdblk *sfptr1;   /* Ptr to 1st node in phys list */
00163         chroff sflen;           /* Original length of file FD is for */
00164 };
00165 
00166         /* Definition of SBBUF string/buffer */
00167 struct sbbuffer {
00168         SBMA sbiop;             /* I/O pointer into in-core text */
00169         int sbrleft;            /* # chars left for reading */
00170         int sbwleft;            /* # chars left for writing */
00171         int sbflags;            /* Various flags */
00172         chroff sbdot;           /* Logical pos for start of current sdblk */
00173         chroff sboff;           /* Offset into current sdblk (if no smblk)*/
00174         struct sdblk *sbcur;    /* Pointer to current SD block of string */
00175 };
00176         /* Flags for "sbflags" */
00177 #define SB_OVW  01      /* Over-write mode */
00178 #define SB_WRIT 02      /* Written; smuse needs to be updated from sbiop */
00179 
00180         /* NOTE: An unused sbbuf structure should be completely zeroed.
00181          *      This will cause routines to handle it properly
00182          *      if they are accidentally pointed at it.
00183          */
00184 
00185         /* Definition of SDBLK */
00186 struct sdblk {
00187         struct sdblk *slforw;   /* Logical sequence forward link */
00188         struct sdblk *slback;   /* Logical sequence backward link */
00189         int sdflags;
00190         struct sdblk *sdforw;   /* Physical sequence (disk) */
00191         struct sdblk *sdback;   /* ditto - backptr for easy flushing */
00192         struct smblk *sdmem;    /* Mem pointer, 0 if no in-core version */
00193         SBFILE *sdfile;         /* File pointer, 0 if no disk version */
00194         chroff sdlen;           /* # chars in disk text */
00195         chroff sdaddr;          /* Disk address of text */
00196 };
00197         /* Flags for "sdflags" */
00198 #define SD_LOCK 0100000         /* Locked because opened by a SBBUF */
00199 #define SD_LCK2 0040000         /* Locked for other reasons */
00200 #define SD_MOD  0020000         /* Modified, mem blk is real stuff */
00201 #define SD_NID     0323         /* Node ID marks active (not on freelist) */
00202 #define SD_LOCKS (SD_LOCK|SD_LCK2)
00203 
00204 /* Note sdback is ONLY needed for fixing up phys list when a sdblk is
00205  * deleted (so as to find previous blk in phys list).  Perhaps it shd
00206  * be flushed (ie only use SDFORW)?  How to do deletions - use circular
00207  * list?  Sigh.
00208  */
00209 
00210         /* Definition of SMBLK (used by SBM routines) */
00211 struct smblk {
00212         struct smblk *smforw;   /* Links to other mem blks, in phys order */
00213         struct smblk *smback;
00214         int smflags;            /* Type, in-use flags */
00215         SBMA smaddr;            /* Mem address of text */
00216         SBMO smlen;             /* # bytes in mem block */
00217         SBMO smuse;             /* # bytes "used" in block */
00218 };
00219         /* Flags for "smflags" */
00220 #define SM_USE  0100000         /* Block is in use (mem free if off) */
00221 #define SM_NXM   040000         /* Block mem is non-existent */
00222 #define SM_EXT   020000         /* Block mem owned by external (non-SBM) rtn*/
00223 #define SM_MNODS 010000         /* Block holds SMBLK nodes */
00224 #define SM_DNODS  04000         /* Block holds SDBLK nodes */
00225 #define SM_NID     0315         /* Node in-use identifier (low byte) */
00226 
00227 /* Error handler type values */
00228 #define SBMERR 0        /* Error in SBM package */
00229 #define SBXERR 1        /* Error in SBSTR package */
00230 #define SBFERR 2        /* "Error" - SBSTR package found a file overwritten.
00231                          *      Non-zero return will continue normally. */
00232 
00233 
00234 /* Redefine certain external symbols to be unique in the first 6 chars
00235 ** to conform with ANSI requirements.
00236 */
00237 #define sbm_nfre sbmnfre        /* SBM stuff */
00238 #define sbm_nfor sbmnfor
00239 #define sbm_nmov sbmnmov
00240 #define sbm_ngc  sbmngc
00241 #define sbx_ndget sbxndg        /* SBSTR stuff */
00242 #define sbx_ndel  sbxnde
00243 #define sbx_ndfre sbxndf
00244 #define sbx_sdcpy sbxsdc
00245 #define sbx_sdgc  sbxsdg
00246 #define sbe_sdlist sbesls       /* SBERR stuff */
00247 #define sbe_sdtab  sbestb
00248 #define sbe_sds    sbesds
00249 #define sbe_sbvfy  sbesbv
00250 #define sbe_sbs    sbesbs
00251 
00252 /* Forward declarations */
00253 extern SBMA sbm_lowaddr;        /* For roundoff purposes */
00254 
00255 extern SBFILE sbv_tf;           /* SBFILE for temp swapout file */
00256 extern int (*sbv_debug)();      /* Error handler address */
00257 extern off_t lseek();           /* For sbstr code mostly */
00258 extern char *mktemp();
00259 extern char *malloc();
00260 extern char *calloc();
00261 extern SBBUF *sb_open();
00262 extern SBSTR *sb_close(), *sb_fduse(), *sbs_cpy(), *sbs_app(), *sb_cpyn(),
00263         *sb_killn();
00264 extern struct sdblk *sbx_ready();
00265 extern chroff sb_tell(), sb_ztell(), sbs_len();
00266 
00267 /* Definition of SB_GETC, SB_PUTC, SB_BACKC macros */
00268 
00269 #define sb_putc(s,c) (--((s)->sbwleft) >= 0 ? \
00270                                 (*(s)->sbiop++ = c) : sb_sputc(s,c))
00271 #define sb_getc(s)   (--((s)->sbrleft) >= 0 ? \
00272                                 sb_uchartoint(*(s)->sbiop++) : sb_sgetc(s))
00273 #define sb_peekc(s)  ((s)->sbrleft > 0 ? \
00274                                 sb_uchartoint(*(s)->sbiop)   : sb_speekc(s))
00275 
00276 /* WARNING - sb_backc must ONLY be used if last operation was a
00277  * successful sb_getc!!  For slow but sure invocation use sb_rgetc.
00278  */
00279 #define sb_backc(s) (++(s->sbrleft), --(s->sbiop))
00280 
00281 #include "sbproto.h"    /* function prototypes */

Generated on Fri Apr 14 22:56:51 2006 for minix by  doxygen 1.4.6