Annotation of researchv8dc/cmd/cyntax/cem/string.c, revision 1.1.1.1

1.1       root        1: #include       "cem.h"
                      2: 
                      3: /*
                      4:  *     String table management routines.
                      5:  *
                      6:  *     str_pages is a vector of pointers to pages which hold the
                      7:  *     the string table.  str_ptr points into the current page
                      8:  *     and str_end is the end of the current page.  str_limit
                      9:  *     is how many pages str_pages can hold.  str_count is the
                     10:  *     count of pages.  str_index is the index into the resultant
                     11:  *     string table (offset by 1 so that 0 means "no string").
                     12:  */
                     13: 
                     14: static char    *str_ptr;
                     15: static char    *str_end;
                     16: static char    **str_pages;
                     17: static int     str_count;
                     18: static int     str_limit;
                     19: static long    str_index       = 1;
                     20: 
                     21: /*
                     22:  *     Dump the string table to the output file and return the size.
                     23:  */
                     24: long
                     25: dump_strings()
                     26: {
                     27:        register int    i;
                     28:        register int    j;
                     29: 
                     30:        for (i = 0, j = str_count - 1; i < j; i++)
                     31:        {
                     32:                if (write(out_fid, str_pages[i], OUTZ) == SYSERROR)
                     33:                {
                     34:                        fprintf(stderr, "%s: ", my_name);
                     35:                        perror("could not write output");
                     36:                        exit(1);
                     37:                }
                     38:        }
                     39: 
                     40:        if (str_ptr != str_end && write(out_fid, str_pages[i], str_ptr - str_pages[i]) == SYSERROR)
                     41:        {
                     42:                fprintf(stderr, "%s: ", my_name);
                     43:                perror("could not write output");
                     44:                exit(1);
                     45:        }
                     46: 
                     47:        return str_index - 1;
                     48: }
                     49: 
                     50: /*
                     51:  *     Add a string to the string table.  Return its index (via p)
                     52:  *     and a pointer to a printable version of it.  Normally this
                     53:  *     pointer points into the string table but if the string
                     54:  *     straddles pages we allocate and copy.  The length is passed
                     55:  *     as a courtesy.
                     56:  */
                     57: char   *
                     58: str_alloc(s, len, p)
                     59: register char  *s;
                     60: register int   len;
                     61: long           *p;
                     62: {
                     63:        register char   *q;
                     64:        register char   *r;
                     65: 
                     66:        *p = str_index;
                     67:        str_index += len;
                     68:        q = str_ptr;
                     69: 
                     70:        if (q + len > str_end)
                     71:        {
                     72:                register char   *e;
                     73:                register char   *t;
                     74: 
                     75:                /*
                     76:                 *      String straddles page boundary.
                     77:                 */
                     78:                r = alloc(len);
                     79:                t = r;
                     80:                e = str_end;
                     81: 
                     82:                while (--len >= 0)
                     83:                {
                     84:                        if (q == e)
                     85:                        {
                     86:                                /*
                     87:                                 *      Allocate a new page.
                     88:                                 */
                     89:                                q = salloc((long)OUTZ);
                     90: 
                     91:                                if (str_count == str_limit)
                     92:                                {
                     93:                                        /*
                     94:                                         *      Extend page pointer vector.
                     95:                                         */
                     96:                                        str_limit += STR_INC;
                     97:                                        str_pages = vector(str_pages, str_limit, char *);
                     98:                                }
                     99: 
                    100:                                e = &q[OUTZ];
                    101:                                str_pages[str_count++] = q;
                    102:                        }
                    103: 
                    104:                        *q++ = *s;
                    105:                        *t++ = *s++;
                    106:                }
                    107: 
                    108:                str_end = e;
                    109:                str_ptr = q;
                    110:        }
                    111:        else
                    112:        {
                    113:                r = q;
                    114: 
                    115:                while (--len >= 0)
                    116:                        *q++ = *s++;
                    117: 
                    118:                str_ptr = q;
                    119:        }
                    120: 
                    121:        return r;
                    122: }
                    123: 
                    124: /*
                    125:  *     Install an input string table into the symbol table and
                    126:  *     output string table.  Make a table which translates old
                    127:  *     string table indexes into symbol table pointers.
                    128:  */
                    129: void
                    130: install_strings(p, n)
                    131: register char  *p;
                    132: register long  n;
                    133: {
                    134:        register symbol **v;
                    135:        extern symbol   *find_symbol();
                    136: 
                    137:        /*
                    138:         *      n is the size of the string table.  allocate
                    139:         *      a slot for 0 ("no string") and put a NULL in it.
                    140:         */
                    141:        v = (symbol **)salloc((n + 1) * sizeof (symbol *));
                    142:        str_trans = v;
                    143:        *v++ = NULL;
                    144: 
                    145:        while (n > 0)
                    146:        {
                    147:                *v++ = find_symbol(p);
                    148:                n--;
                    149: 
                    150:                while (*p++ != '\0')
                    151:                {
                    152:                        *v++ = NULL;
                    153:                        n--;
                    154:                }
                    155:        }
                    156: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.