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 #pragma ident "%Z%%M% %I% %E% SMI" 187c478bd9Sstevel@tonic-gate 197c478bd9Sstevel@tonic-gate #include "defs.h" 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate /* symbol types */ 227c478bd9Sstevel@tonic-gate #define VAR 1 237c478bd9Sstevel@tonic-gate #define CONST 2 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate struct syment { 267c478bd9Sstevel@tonic-gate int s_type; 277c478bd9Sstevel@tonic-gate char *s_name; 287c478bd9Sstevel@tonic-gate struct namelist *s_value; 297c478bd9Sstevel@tonic-gate struct syment *s_next; 307c478bd9Sstevel@tonic-gate }; 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate static struct syment *hashtab[HASHSIZE]; 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * Define a variable from a command line argument. 367c478bd9Sstevel@tonic-gate */ 37*740638c8Sbw void 387c478bd9Sstevel@tonic-gate define(name) 397c478bd9Sstevel@tonic-gate char *name; 407c478bd9Sstevel@tonic-gate { 417c478bd9Sstevel@tonic-gate register char *cp, *s; 427c478bd9Sstevel@tonic-gate register struct namelist *nl; 437c478bd9Sstevel@tonic-gate struct namelist *value; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate if (debug) 467c478bd9Sstevel@tonic-gate printf("define(%s)\n", name); 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate cp = index(name, '='); 497c478bd9Sstevel@tonic-gate if (cp == NULL) 507c478bd9Sstevel@tonic-gate value = NULL; 517c478bd9Sstevel@tonic-gate else if (cp[1] == '\0') { 527c478bd9Sstevel@tonic-gate *cp = '\0'; 537c478bd9Sstevel@tonic-gate value = NULL; 547c478bd9Sstevel@tonic-gate } else if (cp[1] != '(') { 557c478bd9Sstevel@tonic-gate *cp++ = '\0'; 567c478bd9Sstevel@tonic-gate value = makenl(cp); 577c478bd9Sstevel@tonic-gate } else { 587c478bd9Sstevel@tonic-gate nl = NULL; 597c478bd9Sstevel@tonic-gate *cp++ = '\0'; 607c478bd9Sstevel@tonic-gate do 617c478bd9Sstevel@tonic-gate cp++; 627c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t'); 637c478bd9Sstevel@tonic-gate for (s = cp; ; s++) { 647c478bd9Sstevel@tonic-gate switch (*s) { 657c478bd9Sstevel@tonic-gate case ')': 667c478bd9Sstevel@tonic-gate *s = '\0'; 677c478bd9Sstevel@tonic-gate case '\0': 687c478bd9Sstevel@tonic-gate break; 697c478bd9Sstevel@tonic-gate case ' ': 707c478bd9Sstevel@tonic-gate case '\t': 717c478bd9Sstevel@tonic-gate *s++ = '\0'; 727c478bd9Sstevel@tonic-gate while (*s == ' ' || *s == '\t') 737c478bd9Sstevel@tonic-gate s++; 747c478bd9Sstevel@tonic-gate if (*s == ')') 757c478bd9Sstevel@tonic-gate *s = '\0'; 767c478bd9Sstevel@tonic-gate break; 777c478bd9Sstevel@tonic-gate default: 787c478bd9Sstevel@tonic-gate continue; 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate if (nl == NULL) 817c478bd9Sstevel@tonic-gate value = nl = makenl(cp); 827c478bd9Sstevel@tonic-gate else { 837c478bd9Sstevel@tonic-gate nl->n_next = makenl(cp); 847c478bd9Sstevel@tonic-gate nl = nl->n_next; 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate if (*s == '\0') 877c478bd9Sstevel@tonic-gate break; 887c478bd9Sstevel@tonic-gate cp = s; 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate (void) lookup(name, REPLACE, value); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * Lookup name in the table and return a pointer to it. 967c478bd9Sstevel@tonic-gate * LOOKUP - just do lookup, return NULL if not found. 977c478bd9Sstevel@tonic-gate * INSERT - insert name with value, error if already defined. 987c478bd9Sstevel@tonic-gate * REPLACE - insert or replace name with value. 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate struct namelist * 1027c478bd9Sstevel@tonic-gate lookup(name, action, value) 1037c478bd9Sstevel@tonic-gate char *name; 1047c478bd9Sstevel@tonic-gate int action; 1057c478bd9Sstevel@tonic-gate struct namelist *value; 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate register unsigned n; 1087c478bd9Sstevel@tonic-gate register char *cp; 1097c478bd9Sstevel@tonic-gate register struct syment *s; 1107c478bd9Sstevel@tonic-gate char buf[256]; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (debug) 1137c478bd9Sstevel@tonic-gate printf("lookup(%s, %d, %x)\n", name, action, value); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate n = 0; 1167c478bd9Sstevel@tonic-gate for (cp = name; *cp; ) 1177c478bd9Sstevel@tonic-gate n += *cp++; 1187c478bd9Sstevel@tonic-gate n %= HASHSIZE; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate for (s = hashtab[n]; s != NULL; s = s->s_next) { 1217c478bd9Sstevel@tonic-gate if (strcmp(name, s->s_name)) 1227c478bd9Sstevel@tonic-gate continue; 1237c478bd9Sstevel@tonic-gate if (action != LOOKUP) { 1247c478bd9Sstevel@tonic-gate if (action != INSERT || s->s_type != CONST) { 1257c478bd9Sstevel@tonic-gate (void)sprintf(buf, "%.*s redefined", 1267c478bd9Sstevel@tonic-gate sizeof(buf) - sizeof(" redefined"), name); 1277c478bd9Sstevel@tonic-gate yyerror(buf); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate return(s->s_value); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (action == LOOKUP) { 1347c478bd9Sstevel@tonic-gate (void)sprintf(buf, "%.*s undefined", 1357c478bd9Sstevel@tonic-gate sizeof(buf) - sizeof(" undefined"), name); 1367c478bd9Sstevel@tonic-gate yyerror(buf); 1377c478bd9Sstevel@tonic-gate return(NULL); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate s = ALLOC(syment); 1417c478bd9Sstevel@tonic-gate if (s == NULL) 1427c478bd9Sstevel@tonic-gate fatal("ran out of memory\n"); 1437c478bd9Sstevel@tonic-gate s->s_next = hashtab[n]; 1447c478bd9Sstevel@tonic-gate hashtab[n] = s; 1457c478bd9Sstevel@tonic-gate s->s_type = action == INSERT ? VAR : CONST; 1467c478bd9Sstevel@tonic-gate s->s_name = name; 1477c478bd9Sstevel@tonic-gate s->s_value = value; 1487c478bd9Sstevel@tonic-gate return(value); 1497c478bd9Sstevel@tonic-gate } 150