home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1342 / kef.h.SH < prev    next >
Encoding:
Text File  |  1990-12-28  |  6.8 KB  |  243 lines

  1. case $CONFIG in
  2. '')
  3.     if test ! -f config.sh; then
  4.     ln ../config.sh . || \
  5.     ln ../../config.sh . || \
  6.     ln ../../../config.sh . || \
  7.     (echo "Can't find config.sh."; exit 1)
  8.     echo "Using config.sh from above..."
  9.     fi
  10.     . ./config.sh
  11.     ;;
  12. esac
  13. : This forces SH files to create target in same directory as SH file.
  14. : This is so that make depend always knows where to find SH derivatives.
  15. case "$0" in
  16. */*) cd `expr X$0 : 'X\(.*\)/'` ;;
  17. esac
  18. echo "Extracting kef.h (with variable substitutions)"
  19. cat >kef.h <<!GROK!THIS!
  20. #ifndef _KEF_H_
  21. #define _KEF_H_
  22. /*
  23.  * Headerfile for KEF.
  24.  *
  25.  * sccsid = @(#)  kef.h  (v2.12 4/17/90)
  26.  */
  27.  
  28. /*
  29.  * On most machines the following definitions are ok:
  30.  *        char * malloc(), * realloc();
  31.  *        void free ();
  32.  * Only on SUN OS 3.5 (so far) there is a difference :
  33.  *        int free ();
  34.  * This  will result in some warnings, so we set here FREE_TYPE to
  35.  * the appropriate type.
  36.  */
  37. #define FREE_TYPE    $d_intfree        /* */
  38.  
  39. typedef unsigned char byte;
  40.  
  41. /*
  42.  * "Pointers" are indices into arrays. Their type is "kef_index",
  43.  * two bytes unsigned.
  44.  */
  45. typedef unsigned short kef_index;
  46.  
  47. /*
  48.  * Values are stored as 2 byte unsigned, called "kef_value".
  49.  */
  50. typedef unsigned short kef_value;
  51.  
  52. /*
  53.  * KEF_NIL specifies the "no pointer" (NULL) value for type kef_index.
  54.  * (Let's play it save and use 2^16-2)
  55.  */
  56.  
  57. # define KEF_NIL    ((kef_value)65534)
  58.  
  59. /*
  60.  * Free nodes are fitted with a high tag value prior to "cleaning up".
  61.  * This limits the number of nodes to 65532. Oh well.
  62.  */
  63. # define HIGH_TAG    ((kef_value)65533)
  64.  
  65. /*
  66.  * kef_node: holds information for one character.
  67.  *        next    : sequential link to next node
  68.  *         sub        : points at possible successor characters
  69.  *        what    : character associated with the node.
  70.  *        value    : value associated with this node; use macro HAS_VALUE to
  71.  *                    test whether there is a value in this node.
  72.  *        flags   : takes several flags:
  73.  *                    value : 0/1 is value present at node
  74.  *                    clean : 0/1 are the subsequent nodes connected,i.e.
  75.  *                            are they stored in adjacent array elements
  76.  *        num        : how many successors do we have?
  77.  *        tag        : used to store temporary values before "cleaning up"
  78.  *                and to link nodes when traversing the tree
  79.  */
  80. struct kef_node {
  81.     kef_index    sub;
  82.     kef_index    next;
  83.     short        value;
  84.     short        num;
  85.     kef_index    tag;
  86.     char        what;
  87.     byte        flags;
  88. };
  89.  
  90. # define VALUE_FLAG        0x01
  91. # define CLEAN_FLAG        0x02
  92. # define FREE_FLAG        0x04
  93.  
  94. # define HAS_VALUE(n)    ((n).flags & VALUE_FLAG)
  95. # define SET_VALUE(n)    ((n).flags |= VALUE_FLAG)
  96. # define CLEAR_VALUE(n)    ((n).flags &= ~VALUE_FLAG)
  97.  
  98. # define IS_CLEAN(n)    ((n).flags & CLEAN_FLAG)
  99. # define SET_CLEAN(n)    ((n).flags |= CLEAN_FLAG)
  100. # define CLEAR_CLEAN(n)    ((n).flags &= ~CLEAN_FLAG)
  101.  
  102. # define IS_FREE(n)        ((n).flags & FREE_FLAG)
  103. # define SET_FREE(n)    ((n).flags |= FREE_FLAG)
  104. # define CLEAR_FREE(n)    ((n).flags &= ~FREE_FLAG)
  105.  
  106.  
  107. struct kef_db {
  108.     short num_nodes;            /* number of nodes in array node */
  109.     short used_nodes;            /* number of used nodes */
  110.     short high_nodes;            /* number of used+free nodes */
  111.  
  112.     kef_index    root;            /* first node */
  113.     kef_index    free_list;        /* index into node : start of free list */
  114.  
  115.     short longest;                /* length of longest string */
  116.  
  117.     byte must_clean;            /* should clean flag be recomputed ? */
  118.     byte was_allocated;            /* was array node allocated ? */
  119.  
  120.     struct kef_node * node;
  121.  
  122.     char * (* malloc)();        /* internally used malloc */
  123.     char * (* realloc)();        /* internally used realloc */
  124.     FREE_TYPE (* free)();        /* internally used free */
  125. };
  126.  
  127. /*
  128.  * This is the main kef structure.
  129.  * The buffer looks like this:
  130.  * Guaranteed:
  131.  *
  132.  * <---------- size_buf -------------> 
  133.  * |---------------------------------|
  134.  * ^      ^             ^            ^
  135.  * buf    from          to           one character at end is reserved
  136.  *
  137.  * After call to fillbuf, from routine get:
  138.  *
  139.  * <---------- size_buf -------------> 
  140.  * |---------------------------------|
  141.  * ^      ^       ^     ^            ^
  142.  * buf    from    cur   to           one character at end is reserved
  143.  * Newly read characters are from "cur" to "to" exclusive. Old character,
  144.  * aka. lookahead are from "from" to "cur" exclusive.
  145.  */
  146.  
  147. struct kef {
  148.     int    fd;                        /* filedescriptor for this struct */
  149.     short size_buf;                /* size of buffer which can be used by read */
  150.     
  151.     char is_delay;
  152.     char is_timeout;
  153.     int timeout;
  154.     int delay;
  155.  
  156.     byte * buf;
  157.     byte * buf_end;                /* one after last possible character in buf */
  158.  
  159.     byte * from;                /* start of not discarded characters */
  160.     byte * to;                    /* one after end of available characters */
  161.     byte * cur;                    /* start of new characters after fillbuf */
  162.  
  163.     struct kef_db * db;            /* pointer to kdb structure */
  164.  
  165.     char * (* malloc)();        /* internally used malloc */
  166.     char * (* realloc)();        /* internally used realloc */
  167.     FREE_TYPE (* free)();        /* internally used free */
  168.  
  169.     char use_db;                /* should we use database ? */
  170.     char filler;                /* just to pad it on 2 bytes boundaries */
  171. };
  172.  
  173. /*
  174.  * Routine kef_get returns either a character or a value from a string
  175.  * or one of the following constants except KEF_CHAR. KEF_CHAR is
  176.  * returned by functions called by kef_get, like kef_see or kef_wait.
  177.  */
  178. # define KEF_CHAR            0
  179. # define KEF_NO_CHAR        -1
  180. # define KEF_INTERRUPTED    -2
  181. # define KEF_EOF            -3
  182. # define KEF_SOME_ERROR        -4
  183.  
  184. /*
  185.  * A kef_db structure can be written to file, even as only a part of a file.
  186.  * Structure of file:
  187.  * - 4 byte magic number
  188.  * - 2 byte swap info for 2 byte quantities
  189.  * - 2 byte number of nodes
  190.  * - 2 byte number of bytes in first part of kdb structure.
  191.  *            This is equal to "offsetof(kdb->node)" and should be the same
  192.  *            on all machines that have sizeof(short)=2
  193.  * - 2 byte sizeof(struct kef_node); just to be sure.
  194.  * - kef_db structure part
  195.  * - if number of bytes in kef_db part was not a multiple of 4, there are
  196.  *   several padding characters
  197.  * - nodes
  198.  * The first part is gathered into a header, to minimize read/writes
  199.  */
  200. # define KEF_MAGIC0    0x81
  201. # define KEF_MAGIC1    ('K' | 0x80)
  202. # define KEF_MAGIC2    ('E' | 0x80)
  203. # define KEF_MAGIC3    'F'
  204.  
  205. struct kef_fheader {
  206.     byte    magic[4];
  207.     short    swap;
  208.     short    num_nodes;
  209.     short    size_kdb;
  210.     short    size_node;
  211. };
  212.  
  213. /*
  214.  * Error messages for KEF and KEF_DB.
  215.  * Codes:
  216.  * 0 : ok
  217.  * -1 thru -9 : return codes from routines, but no error
  218.  * less or equal -10 : error code
  219.  */
  220. # define KEF_E_OK                0
  221.  
  222. # define KEF_E_SOME_ERROR        -10
  223. # define KEF_E_NODES_USEDUP        -11
  224. # define KEF_E_STRING_TWICE        -12
  225. # define KEF_E_WRITE_ERROR        -13
  226. # define KEF_E_READ_ERROR        -14
  227. # define KEF_E_NOT_FOUND        -15
  228. # define KEF_E_EMPTY_STRING        -16
  229. # define KEF_E_WRONG_MAGIC        -17
  230. # define KEF_E_TOO_MANY_NODES    -18
  231. # define KEF_E_SIZE_KDB            -19
  232. # define KEF_E_SIZE_NODE        -20
  233. # define KEF_E_NO_MALLOC        -21
  234. # define KEF_E_NO_REALLOC        -22
  235. # define KEF_E_NO_FREE            -23
  236. # define KEF_E_ALLOC_FAILED        -24
  237. # define KEF_E_CHECK            -25
  238. # define KEF_E_INV_PAR            -26
  239. # define KEF_E_INV_INX            -27
  240. # define KEF_E_TAG_LOOP            -28
  241. #endif     /* _KEF_H_ */
  242. !GROK!THIS!
  243.