[BACK]Return to lookup.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / oldrdist

Annotation of src/usr.bin/oldrdist/lookup.c, Revision 1.10

1.10    ! deraadt     1: /*     $OpenBSD: lookup.c,v 1.9 2004/01/15 19:12:31 otto Exp $ */
1.2       deraadt     2:
1.1       dm          3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.8       millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       dm         16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #include "defs.h"
                     33:
                     34:        /* symbol types */
                     35: #define VAR    1
                     36: #define CONST  2
                     37:
                     38: struct syment {
                     39:        int     s_type;
                     40:        char    *s_name;
                     41:        struct  namelist *s_value;
                     42:        struct  syment *s_next;
                     43: };
                     44:
                     45: static struct syment *hashtab[HASHSIZE];
                     46:
                     47: /*
                     48:  * Define a variable from a command line argument.
                     49:  */
                     50: void
                     51: define(name)
                     52:        char *name;
                     53: {
1.6       mpech      54:        char *cp, *s;
                     55:        struct namelist *nl;
1.1       dm         56:        struct namelist *value;
                     57:
                     58:        if (debug)
                     59:                printf("define(%s)\n", name);
                     60:
1.4       millert    61:        cp = strchr(name, '=');
1.1       dm         62:        if (cp == NULL)
                     63:                value = NULL;
                     64:        else if (cp[1] == '\0') {
                     65:                *cp = '\0';
                     66:                value = NULL;
                     67:        } else if (cp[1] != '(') {
                     68:                *cp++ = '\0';
                     69:                value = makenl(cp);
                     70:        } else {
                     71:                nl = NULL;
                     72:                *cp++ = '\0';
                     73:                do
                     74:                        cp++;
                     75:                while (*cp == ' ' || *cp == '\t');
                     76:                for (s = cp; ; s++) {
                     77:                        switch (*s) {
                     78:                        case ')':
                     79:                                *s = '\0';
                     80:                        case '\0':
                     81:                                break;
                     82:                        case ' ':
                     83:                        case '\t':
                     84:                                *s++ = '\0';
                     85:                                while (*s == ' ' || *s == '\t')
                     86:                                        s++;
                     87:                                if (*s == ')')
                     88:                                        *s = '\0';
                     89:                                break;
                     90:                        default:
                     91:                                continue;
                     92:                        }
                     93:                        if (nl == NULL)
                     94:                                value = nl = makenl(cp);
                     95:                        else {
                     96:                                nl->n_next = makenl(cp);
                     97:                                nl = nl->n_next;
                     98:                        }
                     99:                        if (*s == '\0')
                    100:                                break;
                    101:                        cp = s;
                    102:                }
                    103:        }
                    104:        (void) lookup(name, REPLACE, value);
                    105: }
                    106:
                    107: /*
                    108:  * Lookup name in the table and return a pointer to it.
                    109:  * LOOKUP - just do lookup, return NULL if not found.
                    110:  * INSERT - insert name with value, error if already defined.
                    111:  * REPLACE - insert or replace name with value.
                    112:  */
                    113:
                    114: struct namelist *
                    115: lookup(name, action, value)
                    116:        char *name;
                    117:        int action;
                    118:        struct namelist *value;
                    119: {
1.7       deraadt   120:        unsigned int n;
1.6       mpech     121:        char *cp;
                    122:        struct syment *s;
1.5       millert   123:        char buf[BUFSIZ];
1.1       dm        124:
                    125:        if (debug)
1.9       otto      126:                printf("lookup(%s, %d, %p)\n", name, action, value);
1.1       dm        127:
                    128:        n = 0;
                    129:        for (cp = name; *cp; )
                    130:                n += *cp++;
                    131:        n %= HASHSIZE;
                    132:
                    133:        for (s = hashtab[n]; s != NULL; s = s->s_next) {
                    134:                if (strcmp(name, s->s_name))
                    135:                        continue;
                    136:                if (action != LOOKUP) {
                    137:                        if (action != INSERT || s->s_type != CONST) {
1.5       millert   138:                                (void)snprintf(buf, sizeof(buf),
                    139:                                        "%s redefined", name);
1.1       dm        140:                                yyerror(buf);
                    141:                        }
                    142:                }
                    143:                return(s->s_value);
                    144:        }
                    145:
                    146:        if (action == LOOKUP) {
1.5       millert   147:                (void)snprintf(buf, sizeof(buf), "%s undefined", name);
1.1       dm        148:                yyerror(buf);
                    149:                return(NULL);
                    150:        }
                    151:
                    152:        s = ALLOC(syment);
                    153:        if (s == NULL)
                    154:                fatal("ran out of memory\n");
                    155:        s->s_next = hashtab[n];
                    156:        hashtab[n] = s;
                    157:        s->s_type = action == INSERT ? VAR : CONST;
                    158:        s->s_name = name;
                    159:        s->s_value = value;
                    160:        return(value);
                    161: }