xref: /csrg-svn/usr.bin/rdist/lookup.c (revision 32503)
122373Sdist /*
222373Sdist  * Copyright (c) 1983 Regents of the University of California.
322373Sdist  * All rights reserved.  The Berkeley software License Agreement
422373Sdist  * specifies the terms and conditions for redistribution.
522373Sdist  */
622373Sdist 
715110Sralph #ifndef lint
8*32503Sbostic static char sccsid[] = "@(#)lookup.c	5.2 (Berkeley) 10/22/87";
922373Sdist #endif not lint
1015110Sralph 
1115110Sralph #include "defs.h"
1215110Sralph 
1316028Sralph 	/* symbol types */
1416028Sralph #define VAR	1
1516028Sralph #define CONST	2
1616028Sralph 
1716028Sralph struct syment {
1816028Sralph 	int	s_type;
1916028Sralph 	char	*s_name;
2016028Sralph 	struct	namelist *s_value;
2116028Sralph 	struct	syment *s_next;
2216028Sralph };
2316028Sralph 
2416028Sralph static struct syment *hashtab[HASHSIZE];
2516028Sralph 
2615110Sralph /*
2715110Sralph  * Define a variable from a command line argument.
2815110Sralph  */
2915110Sralph define(name)
3015110Sralph 	char *name;
3115110Sralph {
3215110Sralph 	register char *cp, *s;
3316028Sralph 	register struct namelist *nl;
3416028Sralph 	struct namelist *value;
3515110Sralph 
3615110Sralph 	if (debug)
3715110Sralph 		printf("define(%s)\n", name);
3815110Sralph 
3915110Sralph 	cp = index(name, '=');
4015200Sralph 	if (cp == NULL)
4115110Sralph 		value = NULL;
4215200Sralph 	else if (cp[1] == '\0') {
4316028Sralph 		*cp = '\0';
4415200Sralph 		value = NULL;
4515200Sralph 	} else if (cp[1] != '(') {
4615200Sralph 		*cp++ = '\0';
4716059Sralph 		value = makenl(cp);
4815110Sralph 	} else {
4916028Sralph 		nl = NULL;
5015110Sralph 		*cp++ = '\0';
5115110Sralph 		do
5215110Sralph 			cp++;
5315110Sralph 		while (*cp == ' ' || *cp == '\t');
5415110Sralph 		for (s = cp; ; s++) {
5515110Sralph 			switch (*s) {
5615110Sralph 			case ')':
5715110Sralph 				*s = '\0';
5815110Sralph 			case '\0':
5915110Sralph 				break;
6015110Sralph 			case ' ':
6115110Sralph 			case '\t':
6215110Sralph 				*s++ = '\0';
6315110Sralph 				while (*s == ' ' || *s == '\t')
6415110Sralph 					s++;
6515110Sralph 				if (*s == ')')
6615110Sralph 					*s = '\0';
6715110Sralph 				break;
6815110Sralph 			default:
6915110Sralph 				continue;
7015110Sralph 			}
7116028Sralph 			if (nl == NULL)
7216028Sralph 				value = nl = makenl(cp);
7315110Sralph 			else {
7416028Sralph 				nl->n_next = makenl(cp);
7516028Sralph 				nl = nl->n_next;
7615110Sralph 			}
7715110Sralph 			if (*s == '\0')
7815110Sralph 				break;
7915110Sralph 			cp = s;
8015110Sralph 		}
8115110Sralph 	}
8216028Sralph 	(void) lookup(name, REPLACE, value);
8315110Sralph }
8415110Sralph 
8515110Sralph /*
8615110Sralph  * Lookup name in the table and return a pointer to it.
8716028Sralph  * LOOKUP - just do lookup, return NULL if not found.
8816028Sralph  * INSERT - insert name with value, error if already defined.
8916028Sralph  * REPLACE - insert or replace name with value.
9015110Sralph  */
9115110Sralph 
9216028Sralph struct namelist *
9316028Sralph lookup(name, action, value)
9415110Sralph 	char *name;
9516028Sralph 	int action;
9616028Sralph 	struct namelist *value;
9715110Sralph {
9815110Sralph 	register unsigned n;
9915110Sralph 	register char *cp;
10016028Sralph 	register struct syment *s;
10117917Sralph 	char buf[256];
10215110Sralph 
10315110Sralph 	if (debug)
10416028Sralph 		printf("lookup(%s, %d, %x)\n", name, action, value);
10515110Sralph 
10615110Sralph 	n = 0;
10715110Sralph 	for (cp = name; *cp; )
10815110Sralph 		n += *cp++;
10915110Sralph 	n %= HASHSIZE;
11015110Sralph 
11116028Sralph 	for (s = hashtab[n]; s != NULL; s = s->s_next) {
11216028Sralph 		if (strcmp(name, s->s_name))
11315110Sralph 			continue;
11416028Sralph 		if (action != LOOKUP) {
11517917Sralph 			if (action != INSERT || s->s_type != CONST) {
116*32503Sbostic 				(void)sprintf(buf, "%s redefined", name);
11717917Sralph 				yyerror(buf);
11817917Sralph 			}
11915110Sralph 		}
12016028Sralph 		return(s->s_value);
12115110Sralph 	}
12215110Sralph 
12317917Sralph 	if (action == LOOKUP) {
124*32503Sbostic 		(void)sprintf(buf, "%s undefined", name);
125*32503Sbostic 		yyerror(buf);
12617917Sralph 		return(NULL);
12717917Sralph 	}
12815110Sralph 
12916028Sralph 	s = ALLOC(syment);
13016028Sralph 	if (s == NULL)
13116028Sralph 		fatal("ran out of memory\n");
13216028Sralph 	s->s_next = hashtab[n];
13316028Sralph 	hashtab[n] = s;
13416028Sralph 	s->s_type = action == INSERT ? VAR : CONST;
13516028Sralph 	s->s_name = name;
13616028Sralph 	s->s_value = value;
13715110Sralph 	return(value);
13815110Sralph }
139