added depend label
[unix-history] / usr / src / usr.bin / rdist / lookup.c
index 6a5836c..908cdaf 100644 (file)
@@ -1,9 +1,28 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)lookup.c    4.2 (Berkeley) 83/09/27";
-#endif
+static char sccsid[] = "@(#)lookup.c   5.1 (Berkeley) %G%";
+#endif not lint
 
 #include "defs.h"
 
 
 #include "defs.h"
 
+       /* symbol types */
+#define VAR    1
+#define CONST  2
+
+struct syment {
+       int     s_type;
+       char    *s_name;
+       struct  namelist *s_value;
+       struct  syment *s_next;
+};
+
+static struct syment *hashtab[HASHSIZE];
+
 /*
  * Define a variable from a command line argument.
  */
 /*
  * Define a variable from a command line argument.
  */
@@ -11,19 +30,23 @@ define(name)
        char *name;
 {
        register char *cp, *s;
        char *name;
 {
        register char *cp, *s;
-       register struct block *bp, *value;
+       register struct namelist *nl;
+       struct namelist *value;
 
        if (debug)
                printf("define(%s)\n", name);
 
        cp = index(name, '=');
 
        if (debug)
                printf("define(%s)\n", name);
 
        cp = index(name, '=');
-       if (cp == NULL || cp[1] == '\0')
+       if (cp == NULL)
+               value = NULL;
+       else if (cp[1] == '\0') {
+               *cp = '\0';
                value = NULL;
                value = NULL;
-       else if (cp[1] != '(') {
+       else if (cp[1] != '(') {
                *cp++ = '\0';
                *cp++ = '\0';
-               value = makeblock(NAME, cp);
+               value = makenl(cp);
        } else {
        } else {
-               bp = NULL;
+               nl = NULL;
                *cp++ = '\0';
                do
                        cp++;
                *cp++ = '\0';
                do
                        cp++;
@@ -45,102 +68,70 @@ define(name)
                        default:
                                continue;
                        }
                        default:
                                continue;
                        }
-                       if (bp == NULL)
-                               value = bp = makeblock(NAME, cp);
+                       if (nl == NULL)
+                               value = nl = makenl(cp);
                        else {
                        else {
-                               bp->b_next = makeblock(NAME, cp);
-                               bp = bp->b_next;
+                               nl->n_next = makenl(cp);
+                               nl = nl->n_next;
                        }
                        if (*s == '\0')
                                break;
                        cp = s;
                }
        }
                        }
                        if (*s == '\0')
                                break;
                        cp = s;
                }
        }
-       bp = makeblock(VAR, name);
-       bp->b_args = value;
-       (void) lookup(bp->b_name, bp, 1);
+       (void) lookup(name, REPLACE, value);
 }
 
 }
 
-static struct block *hashtab[HASHSIZE];
-
 /*
  * Lookup name in the table and return a pointer to it.
 /*
  * Lookup name in the table and return a pointer to it.
- * Insert == 0 - just do lookup, return NULL if not found.
- * insert == 1 - insert name with value, error if already defined.
- * insert == 2 - replace name with value if not entered with insert == 1.
+ * LOOKUP - just do lookup, return NULL if not found.
+ * INSERT - insert name with value, error if already defined.
+ * REPLACE - insert or replace name with value.
  */
 
  */
 
-struct block *
-lookup(name, value, insert)
+struct namelist *
+lookup(name, action, value)
        char *name;
        char *name;
-       struct block *value;
-       int insert;
+       int action;
+       struct namelist *value;
 {
        register unsigned n;
        register char *cp;
 {
        register unsigned n;
        register char *cp;
-       register struct block *b, *f;
+       register struct syment *s;
+       char buf[256];
 
        if (debug)
 
        if (debug)
-               printf("lookup(%s, %x, %d)\n", name, value, insert);
+               printf("lookup(%s, %d, %x)\n", name, action, value);
 
        n = 0;
        for (cp = name; *cp; )
                n += *cp++;
        n %= HASHSIZE;
 
 
        n = 0;
        for (cp = name; *cp; )
                n += *cp++;
        n %= HASHSIZE;
 
-       for (b = hashtab[n]; b != NULL; b = b->b_next) {
-               if (strcmp(name, b->b_name))
+       for (s = hashtab[n]; s != NULL; s = s->s_next) {
+               if (strcmp(name, s->s_name))
                        continue;
                        continue;
-               if (insert) {
-                       if (b->b_type == NAME) {
-                               warn("%s redefined\n", name);
-                               f = b->b_args;
-                               b->b_args = value->b_args;
-                               value->b_args = f;
-                       } else if (value->b_type == VAR)
-                               fatal("%s redefined\n", name);
-                       while (f = value->b_next) {
-                               value->b_next = f->b_next;
-                               free(f->b_name);
-                               free(f);
+               if (action != LOOKUP) {
+                       if (action != INSERT || s->s_type != CONST) {
+                               sprintf(buf, "%s redefined", name);
+                               yyerror(buf);
                        }
                        }
-                       free(value->b_name);
-                       free(value);
                }
                }
-               return(b);
+               return(s->s_value);
        }
 
        }
 
-       if (!insert)
-               fatal("%s not defined", name);
-
-       value->b_next = hashtab[n];
-       hashtab[n] = value;
-       return(value);
-}
-
-/*
- * Make a block for lists of variables, commands, etc.
- */
-struct block *
-makeblock(type, name)
-       int type;
-       register char *name;
-{
-       register char *cp;
-       register struct block *bp;
+       if (action == LOOKUP) {
+               yyerror(sprintf(buf, "%s undefined", name));
+               return(NULL);
+       }
 
 
-       bp = ALLOC(block);
-       if (bp == NULL)
+       s = ALLOC(syment);
+       if (s == NULL)
                fatal("ran out of memory\n");
                fatal("ran out of memory\n");
-       bp->b_type = type;
-       bp->b_next = bp->b_args = NULL;
-       if (type == NAME || type == VAR) {
-               bp->b_name = cp = (char *) malloc(strlen(name) + 1);
-               if (cp == NULL)
-                       fatal("ran out of memory\n");
-               while (*cp++ = *name++)
-                       ;
-       } else
-               bp->b_name = NULL;
-       return(bp);
+       s->s_next = hashtab[n];
+       hashtab[n] = s;
+       s->s_type = action == INSERT ? VAR : CONST;
+       s->s_name = name;
+       s->s_value = value;
+       return(value);
 }
 }