xref: /csrg-svn/usr.bin/rdist/lookup.c (revision 17917)
115110Sralph #ifndef lint
2*17917Sralph static	char *sccsid = "@(#)lookup.c	4.6 (Berkeley) 85/02/04";
315110Sralph #endif
415110Sralph 
515110Sralph #include "defs.h"
615110Sralph 
716028Sralph 	/* symbol types */
816028Sralph #define VAR	1
916028Sralph #define CONST	2
1016028Sralph 
1116028Sralph struct syment {
1216028Sralph 	int	s_type;
1316028Sralph 	char	*s_name;
1416028Sralph 	struct	namelist *s_value;
1516028Sralph 	struct	syment *s_next;
1616028Sralph };
1716028Sralph 
1816028Sralph static struct syment *hashtab[HASHSIZE];
1916028Sralph 
2015110Sralph /*
2115110Sralph  * Define a variable from a command line argument.
2215110Sralph  */
2315110Sralph define(name)
2415110Sralph 	char *name;
2515110Sralph {
2615110Sralph 	register char *cp, *s;
2716028Sralph 	register struct namelist *nl;
2816028Sralph 	struct namelist *value;
2915110Sralph 
3015110Sralph 	if (debug)
3115110Sralph 		printf("define(%s)\n", name);
3215110Sralph 
3315110Sralph 	cp = index(name, '=');
3415200Sralph 	if (cp == NULL)
3515110Sralph 		value = NULL;
3615200Sralph 	else if (cp[1] == '\0') {
3716028Sralph 		*cp = '\0';
3815200Sralph 		value = NULL;
3915200Sralph 	} else if (cp[1] != '(') {
4015200Sralph 		*cp++ = '\0';
4116059Sralph 		value = makenl(cp);
4215110Sralph 	} else {
4316028Sralph 		nl = NULL;
4415110Sralph 		*cp++ = '\0';
4515110Sralph 		do
4615110Sralph 			cp++;
4715110Sralph 		while (*cp == ' ' || *cp == '\t');
4815110Sralph 		for (s = cp; ; s++) {
4915110Sralph 			switch (*s) {
5015110Sralph 			case ')':
5115110Sralph 				*s = '\0';
5215110Sralph 			case '\0':
5315110Sralph 				break;
5415110Sralph 			case ' ':
5515110Sralph 			case '\t':
5615110Sralph 				*s++ = '\0';
5715110Sralph 				while (*s == ' ' || *s == '\t')
5815110Sralph 					s++;
5915110Sralph 				if (*s == ')')
6015110Sralph 					*s = '\0';
6115110Sralph 				break;
6215110Sralph 			default:
6315110Sralph 				continue;
6415110Sralph 			}
6516028Sralph 			if (nl == NULL)
6616028Sralph 				value = nl = makenl(cp);
6715110Sralph 			else {
6816028Sralph 				nl->n_next = makenl(cp);
6916028Sralph 				nl = nl->n_next;
7015110Sralph 			}
7115110Sralph 			if (*s == '\0')
7215110Sralph 				break;
7315110Sralph 			cp = s;
7415110Sralph 		}
7515110Sralph 	}
7616028Sralph 	(void) lookup(name, REPLACE, value);
7715110Sralph }
7815110Sralph 
7915110Sralph /*
8015110Sralph  * Lookup name in the table and return a pointer to it.
8116028Sralph  * LOOKUP - just do lookup, return NULL if not found.
8216028Sralph  * INSERT - insert name with value, error if already defined.
8316028Sralph  * REPLACE - insert or replace name with value.
8415110Sralph  */
8515110Sralph 
8616028Sralph struct namelist *
8716028Sralph lookup(name, action, value)
8815110Sralph 	char *name;
8916028Sralph 	int action;
9016028Sralph 	struct namelist *value;
9115110Sralph {
9215110Sralph 	register unsigned n;
9315110Sralph 	register char *cp;
9416028Sralph 	register struct syment *s;
95*17917Sralph 	char buf[256];
9615110Sralph 
9715110Sralph 	if (debug)
9816028Sralph 		printf("lookup(%s, %d, %x)\n", name, action, value);
9915110Sralph 
10015110Sralph 	n = 0;
10115110Sralph 	for (cp = name; *cp; )
10215110Sralph 		n += *cp++;
10315110Sralph 	n %= HASHSIZE;
10415110Sralph 
10516028Sralph 	for (s = hashtab[n]; s != NULL; s = s->s_next) {
10616028Sralph 		if (strcmp(name, s->s_name))
10715110Sralph 			continue;
10816028Sralph 		if (action != LOOKUP) {
109*17917Sralph 			if (action != INSERT || s->s_type != CONST) {
110*17917Sralph 				sprintf(buf, "%s redefined", name);
111*17917Sralph 				yyerror(buf);
112*17917Sralph 			}
11315110Sralph 		}
11416028Sralph 		return(s->s_value);
11515110Sralph 	}
11615110Sralph 
117*17917Sralph 	if (action == LOOKUP) {
118*17917Sralph 		yyerror(sprintf(buf, "%s undefined", name));
119*17917Sralph 		return(NULL);
120*17917Sralph 	}
12115110Sralph 
12216028Sralph 	s = ALLOC(syment);
12316028Sralph 	if (s == NULL)
12416028Sralph 		fatal("ran out of memory\n");
12516028Sralph 	s->s_next = hashtab[n];
12616028Sralph 	hashtab[n] = s;
12716028Sralph 	s->s_type = action == INSERT ? VAR : CONST;
12816028Sralph 	s->s_name = name;
12916028Sralph 	s->s_value = value;
13015110Sralph 	return(value);
13115110Sralph }
132