Annotation of 43BSDReno/contrib/isode-beta/others/idist/lookup.c, revision 1.1.1.1

1.1       root        1: /* lookup.c -- maintains the symbol table */
                      2: 
                      3: /*
                      4:  * $Header: /f/osi/others/idist/RCS/lookup.c,v 7.0 89/11/23 21:58:36 mrose Rel $
                      5:  *
                      6:  * This file largely untouched from the original UCB sources.
                      7:  *
                      8:  * Julian Onions <[email protected]>
                      9:  * Nottingham University Computer Science.
                     10:  *
                     11:  *
                     12:  * $Log:       lookup.c,v $
                     13:  * Revision 7.0  89/11/23  21:58:36  mrose
                     14:  * Release 6.0
                     15:  * 
                     16:  */
                     17: 
                     18: /*
                     19:  * Copyright (c) 1983 Regents of the University of California.
                     20:  * All rights reserved.  The Berkeley software License Agreement
                     21:  * specifies the terms and conditions for redistribution.
                     22:  */
                     23: 
                     24: #ifndef lint
                     25: static char sccsid[] = "@(#)lookup.c   5.1 (Berkeley) 6/6/85";
                     26: static char *rcsid = "$Header: /f/osi/others/idist/RCS/lookup.c,v 7.0 89/11/23 21:58:36 mrose Rel $";
                     27: #endif
                     28: 
                     29: #include "defs.h"
                     30: 
                     31:        /* symbol types */
                     32: #define VAR    1
                     33: #define CONST  2
                     34: 
                     35: struct syment {
                     36:        int     s_type;
                     37:        char    *s_name;
                     38:        struct  namelist *s_value;
                     39:        struct  syment *s_next;
                     40: };
                     41: 
                     42: static struct syment *hashtab[HASHSIZE];
                     43: 
                     44: /*
                     45:  * Define a variable from a command line argument.
                     46:  */
                     47: define(name)
                     48:        char *name;
                     49: {
                     50:        register char *cp, *s;
                     51:        register struct namelist *nl;
                     52:        struct namelist *value;
                     53: 
                     54:        if (debug)
                     55:                printf("define(%s)\n", name);
                     56: 
                     57:        cp = index(name, '=');
                     58:        if (cp == NULL)
                     59:                value = NULL;
                     60:        else if (cp[1] == '\0') {
                     61:                *cp = '\0';
                     62:                value = NULL;
                     63:        } else if (cp[1] != '(') {
                     64:                *cp++ = '\0';
                     65:                value = makenl(cp);
                     66:        } else {
                     67:                nl = NULL;
                     68:                *cp++ = '\0';
                     69:                do
                     70:                        cp++;
                     71:                while (*cp == ' ' || *cp == '\t');
                     72:                for (s = cp; ; s++) {
                     73:                        switch (*s) {
                     74:                        case ')':
                     75:                                *s = '\0';
                     76:                        case '\0':
                     77:                                break;
                     78:                        case ' ':
                     79:                        case '\t':
                     80:                                *s++ = '\0';
                     81:                                while (*s == ' ' || *s == '\t')
                     82:                                        s++;
                     83:                                if (*s == ')')
                     84:                                        *s = '\0';
                     85:                                break;
                     86:                        default:
                     87:                                continue;
                     88:                        }
                     89:                        if (nl == NULL)
                     90:                                value = nl = makenl(cp);
                     91:                        else {
                     92:                                nl->n_next = makenl(cp);
                     93:                                nl = nl->n_next;
                     94:                        }
                     95:                        if (*s == '\0')
                     96:                                break;
                     97:                        cp = s;
                     98:                }
                     99:        }
                    100:        (void) lookup(name, REPLACE, value);
                    101: }
                    102: 
                    103: /*
                    104:  * Lookup name in the table and return a pointer to it.
                    105:  * LOOKUP - just do lookup, return NULL if not found.
                    106:  * INSERT - insert name with value, error if already defined.
                    107:  * REPLACE - insert or replace name with value.
                    108:  */
                    109: 
                    110: struct namelist *
                    111: lookup(name, action, value)
                    112:        char *name;
                    113:        int action;
                    114:        struct namelist *value;
                    115: {
                    116:        register unsigned n;
                    117:        register char *cp;
                    118:        register struct syment *s;
                    119:        char buf[256];
                    120: 
                    121:        if (debug)
                    122:                printf("lookup(%s, %d, %x)\n", name, action, value);
                    123: 
                    124:        n = 0;
                    125:        for (cp = name; *cp; )
                    126:                n += *cp++;
                    127:        n %= HASHSIZE;
                    128: 
                    129:        for (s = hashtab[n]; s != NULL; s = s->s_next) {
                    130:                if (strcmp(name, s->s_name))
                    131:                        continue;
                    132:                if (action != LOOKUP) {
                    133:                        if (action != INSERT || s->s_type != CONST) {
                    134:                                (void) sprintf(buf, "%s redefined", name);
                    135:                                yyerror(buf);
                    136:                        }
                    137:                }
                    138:                return(s->s_value);
                    139:        }
                    140: 
                    141:        if (action == LOOKUP) {
                    142:                (void) sprintf(buf, "%s undefined", name);
                    143:                yyerror(buf);
                    144:                return(NULL);
                    145:        }
                    146: 
                    147:        s = ALLOC(syment);
                    148:        if (s == NULL)
                    149:                adios (NULLCP, "ran out of memory\n");
                    150:        s->s_next = hashtab[n];
                    151:        hashtab[n] = s;
                    152:        s->s_type = action == INSERT ? VAR : CONST;
                    153:        s->s_name = name;
                    154:        s->s_value = value;
                    155:        return(value);
                    156: }

unix.superglobalmegacorp.com

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