17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
37c478bd9Sstevel@tonic-gate  * All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
67c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
77c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
87c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
97c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
107c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
117c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
127c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
137c478bd9Sstevel@tonic-gate  *
14*740638c8Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
15*740638c8Sbw  * Use is subject to license terms.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "defs.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate 	/* symbol types */
217c478bd9Sstevel@tonic-gate #define VAR	1
227c478bd9Sstevel@tonic-gate #define CONST	2
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate struct syment {
257c478bd9Sstevel@tonic-gate 	int	s_type;
267c478bd9Sstevel@tonic-gate 	char	*s_name;
277c478bd9Sstevel@tonic-gate 	struct	namelist *s_value;
287c478bd9Sstevel@tonic-gate 	struct	syment *s_next;
297c478bd9Sstevel@tonic-gate };
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate static struct syment *hashtab[HASHSIZE];
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Define a variable from a command line argument.
357c478bd9Sstevel@tonic-gate  */
36*740638c8Sbw void
define(name)377c478bd9Sstevel@tonic-gate define(name)
387c478bd9Sstevel@tonic-gate 	char *name;
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	register char *cp, *s;
417c478bd9Sstevel@tonic-gate 	register struct namelist *nl;
427c478bd9Sstevel@tonic-gate 	struct namelist *value;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	if (debug)
457c478bd9Sstevel@tonic-gate 		printf("define(%s)\n", name);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	cp = index(name, '=');
487c478bd9Sstevel@tonic-gate 	if (cp == NULL)
497c478bd9Sstevel@tonic-gate 		value = NULL;
507c478bd9Sstevel@tonic-gate 	else if (cp[1] == '\0') {
517c478bd9Sstevel@tonic-gate 		*cp = '\0';
527c478bd9Sstevel@tonic-gate 		value = NULL;
537c478bd9Sstevel@tonic-gate 	} else if (cp[1] != '(') {
547c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
557c478bd9Sstevel@tonic-gate 		value = makenl(cp);
567c478bd9Sstevel@tonic-gate 	} else {
577c478bd9Sstevel@tonic-gate 		nl = NULL;
587c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
597c478bd9Sstevel@tonic-gate 		do
607c478bd9Sstevel@tonic-gate 			cp++;
617c478bd9Sstevel@tonic-gate 		while (*cp == ' ' || *cp == '\t');
627c478bd9Sstevel@tonic-gate 		for (s = cp; ; s++) {
637c478bd9Sstevel@tonic-gate 			switch (*s) {
647c478bd9Sstevel@tonic-gate 			case ')':
657c478bd9Sstevel@tonic-gate 				*s = '\0';
667c478bd9Sstevel@tonic-gate 			case '\0':
677c478bd9Sstevel@tonic-gate 				break;
687c478bd9Sstevel@tonic-gate 			case ' ':
697c478bd9Sstevel@tonic-gate 			case '\t':
707c478bd9Sstevel@tonic-gate 				*s++ = '\0';
717c478bd9Sstevel@tonic-gate 				while (*s == ' ' || *s == '\t')
727c478bd9Sstevel@tonic-gate 					s++;
737c478bd9Sstevel@tonic-gate 				if (*s == ')')
747c478bd9Sstevel@tonic-gate 					*s = '\0';
757c478bd9Sstevel@tonic-gate 				break;
767c478bd9Sstevel@tonic-gate 			default:
777c478bd9Sstevel@tonic-gate 				continue;
787c478bd9Sstevel@tonic-gate 			}
797c478bd9Sstevel@tonic-gate 			if (nl == NULL)
807c478bd9Sstevel@tonic-gate 				value = nl = makenl(cp);
817c478bd9Sstevel@tonic-gate 			else {
827c478bd9Sstevel@tonic-gate 				nl->n_next = makenl(cp);
837c478bd9Sstevel@tonic-gate 				nl = nl->n_next;
847c478bd9Sstevel@tonic-gate 			}
857c478bd9Sstevel@tonic-gate 			if (*s == '\0')
867c478bd9Sstevel@tonic-gate 				break;
877c478bd9Sstevel@tonic-gate 			cp = s;
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	(void) lookup(name, REPLACE, value);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Lookup name in the table and return a pointer to it.
957c478bd9Sstevel@tonic-gate  * LOOKUP - just do lookup, return NULL if not found.
967c478bd9Sstevel@tonic-gate  * INSERT - insert name with value, error if already defined.
977c478bd9Sstevel@tonic-gate  * REPLACE - insert or replace name with value.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate struct namelist *
lookup(name,action,value)1017c478bd9Sstevel@tonic-gate lookup(name, action, value)
1027c478bd9Sstevel@tonic-gate 	char *name;
1037c478bd9Sstevel@tonic-gate 	int action;
1047c478bd9Sstevel@tonic-gate 	struct namelist *value;
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	register unsigned n;
1077c478bd9Sstevel@tonic-gate 	register char *cp;
1087c478bd9Sstevel@tonic-gate 	register struct syment *s;
1097c478bd9Sstevel@tonic-gate 	char buf[256];
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (debug)
1127c478bd9Sstevel@tonic-gate 		printf("lookup(%s, %d, %x)\n", name, action, value);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	n = 0;
1157c478bd9Sstevel@tonic-gate 	for (cp = name; *cp; )
1167c478bd9Sstevel@tonic-gate 		n += *cp++;
1177c478bd9Sstevel@tonic-gate 	n %= HASHSIZE;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	for (s = hashtab[n]; s != NULL; s = s->s_next) {
1207c478bd9Sstevel@tonic-gate 		if (strcmp(name, s->s_name))
1217c478bd9Sstevel@tonic-gate 			continue;
1227c478bd9Sstevel@tonic-gate 		if (action != LOOKUP) {
1237c478bd9Sstevel@tonic-gate 			if (action != INSERT || s->s_type != CONST) {
1247c478bd9Sstevel@tonic-gate 				(void)sprintf(buf, "%.*s redefined",
1257c478bd9Sstevel@tonic-gate 				      sizeof(buf) - sizeof(" redefined"), name);
1267c478bd9Sstevel@tonic-gate 				yyerror(buf);
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		return(s->s_value);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (action == LOOKUP) {
1337c478bd9Sstevel@tonic-gate 		(void)sprintf(buf, "%.*s undefined",
1347c478bd9Sstevel@tonic-gate 			sizeof(buf) - sizeof(" undefined"), name);
1357c478bd9Sstevel@tonic-gate 		yyerror(buf);
1367c478bd9Sstevel@tonic-gate 		return(NULL);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	s = ALLOC(syment);
1407c478bd9Sstevel@tonic-gate 	if (s == NULL)
1417c478bd9Sstevel@tonic-gate 		fatal("ran out of memory\n");
1427c478bd9Sstevel@tonic-gate 	s->s_next = hashtab[n];
1437c478bd9Sstevel@tonic-gate 	hashtab[n] = s;
1447c478bd9Sstevel@tonic-gate 	s->s_type = action == INSERT ? VAR : CONST;
1457c478bd9Sstevel@tonic-gate 	s->s_name = name;
1467c478bd9Sstevel@tonic-gate 	s->s_value = value;
1477c478bd9Sstevel@tonic-gate 	return(value);
1487c478bd9Sstevel@tonic-gate }
149