122373Sdist /*
2*63070Sbostic * Copyright (c) 1983, 1993
3*63070Sbostic * The Regents of the University of California. All rights reserved.
433415Sbostic *
542760Sbostic * %sccs.include.redist.c%
622373Sdist */
722373Sdist
815110Sralph #ifndef lint
9*63070Sbostic static char sccsid[] = "@(#)lookup.c 8.1 (Berkeley) 06/09/93";
1033415Sbostic #endif /* not lint */
1115110Sralph
1215110Sralph #include "defs.h"
1315110Sralph
1416028Sralph /* symbol types */
1516028Sralph #define VAR 1
1616028Sralph #define CONST 2
1716028Sralph
1816028Sralph struct syment {
1916028Sralph int s_type;
2016028Sralph char *s_name;
2116028Sralph struct namelist *s_value;
2216028Sralph struct syment *s_next;
2316028Sralph };
2416028Sralph
2516028Sralph static struct syment *hashtab[HASHSIZE];
2616028Sralph
2715110Sralph /*
2815110Sralph * Define a variable from a command line argument.
2915110Sralph */
3054705Sbostic void
define(name)3115110Sralph define(name)
3215110Sralph char *name;
3315110Sralph {
3415110Sralph register char *cp, *s;
3516028Sralph register struct namelist *nl;
3616028Sralph struct namelist *value;
3715110Sralph
3815110Sralph if (debug)
3915110Sralph printf("define(%s)\n", name);
4015110Sralph
4115110Sralph cp = index(name, '=');
4215200Sralph if (cp == NULL)
4315110Sralph value = NULL;
4415200Sralph else if (cp[1] == '\0') {
4516028Sralph *cp = '\0';
4615200Sralph value = NULL;
4715200Sralph } else if (cp[1] != '(') {
4815200Sralph *cp++ = '\0';
4916059Sralph value = makenl(cp);
5015110Sralph } else {
5116028Sralph nl = NULL;
5215110Sralph *cp++ = '\0';
5315110Sralph do
5415110Sralph cp++;
5515110Sralph while (*cp == ' ' || *cp == '\t');
5615110Sralph for (s = cp; ; s++) {
5715110Sralph switch (*s) {
5815110Sralph case ')':
5915110Sralph *s = '\0';
6015110Sralph case '\0':
6115110Sralph break;
6215110Sralph case ' ':
6315110Sralph case '\t':
6415110Sralph *s++ = '\0';
6515110Sralph while (*s == ' ' || *s == '\t')
6615110Sralph s++;
6715110Sralph if (*s == ')')
6815110Sralph *s = '\0';
6915110Sralph break;
7015110Sralph default:
7115110Sralph continue;
7215110Sralph }
7316028Sralph if (nl == NULL)
7416028Sralph value = nl = makenl(cp);
7515110Sralph else {
7616028Sralph nl->n_next = makenl(cp);
7716028Sralph nl = nl->n_next;
7815110Sralph }
7915110Sralph if (*s == '\0')
8015110Sralph break;
8115110Sralph cp = s;
8215110Sralph }
8315110Sralph }
8416028Sralph (void) lookup(name, REPLACE, value);
8515110Sralph }
8615110Sralph
8715110Sralph /*
8815110Sralph * Lookup name in the table and return a pointer to it.
8916028Sralph * LOOKUP - just do lookup, return NULL if not found.
9016028Sralph * INSERT - insert name with value, error if already defined.
9116028Sralph * REPLACE - insert or replace name with value.
9215110Sralph */
9315110Sralph
9416028Sralph struct namelist *
lookup(name,action,value)9516028Sralph lookup(name, action, value)
9615110Sralph char *name;
9716028Sralph int action;
9816028Sralph struct namelist *value;
9915110Sralph {
10015110Sralph register unsigned n;
10115110Sralph register char *cp;
10216028Sralph register struct syment *s;
10317917Sralph char buf[256];
10415110Sralph
10515110Sralph if (debug)
10616028Sralph printf("lookup(%s, %d, %x)\n", name, action, value);
10715110Sralph
10815110Sralph n = 0;
10915110Sralph for (cp = name; *cp; )
11015110Sralph n += *cp++;
11115110Sralph n %= HASHSIZE;
11215110Sralph
11316028Sralph for (s = hashtab[n]; s != NULL; s = s->s_next) {
11416028Sralph if (strcmp(name, s->s_name))
11515110Sralph continue;
11616028Sralph if (action != LOOKUP) {
11717917Sralph if (action != INSERT || s->s_type != CONST) {
11832503Sbostic (void)sprintf(buf, "%s redefined", name);
11917917Sralph yyerror(buf);
12017917Sralph }
12115110Sralph }
12216028Sralph return(s->s_value);
12315110Sralph }
12415110Sralph
12517917Sralph if (action == LOOKUP) {
12632503Sbostic (void)sprintf(buf, "%s undefined", name);
12732503Sbostic yyerror(buf);
12817917Sralph return(NULL);
12917917Sralph }
13015110Sralph
13116028Sralph s = ALLOC(syment);
13216028Sralph if (s == NULL)
13316028Sralph fatal("ran out of memory\n");
13416028Sralph s->s_next = hashtab[n];
13516028Sralph hashtab[n] = s;
13616028Sralph s->s_type = action == INSERT ? VAR : CONST;
13716028Sralph s->s_name = name;
13816028Sralph s->s_value = value;
13915110Sralph return(value);
14015110Sralph }
141