source: trunk/athena/etc/synctree/vtable.c @ 6952

Revision 6952, 2.2 KB checked in by probe, 31 years ago (diff)
Initial revision
  • Property svn:executable set to *
Line 
1/* Copyright (C) 1988  Tim Shepard   All rights reserved. */
2
3#include "synctree.h"
4#include <strings.h>
5#define NBUCKETSL2 8
6#define NBUCKETS (1 << (NBUCKETSL2 -1))
7#define HASHMASK (NBUCKETS - 1)
8
9static unsigned int hash(s)
10     char *s;
11{ unsigned int r = 0;
12  while (*s != 0) r ^= (((*(s++)) * 91) & HASHMASK);
13  return r;
14}   
15   
16struct var {
17  char            *name;
18  unsigned int    hash;    /* what name hashes to */
19  int             level;   /* the level at the time this entry was created */
20  struct var      *next;   /* next of same hash */
21  struct var      *chain;  /* previous variable added to table */
22  bool            val;
23};
24
25static struct var  *vars[NBUCKETS];
26static struct var  *chain; /* the variable most recently added to table */
27static int level;
28
29static struct var *find_var(name)
30     char *name;
31{ struct var *v;
32  unsigned int h;
33  h = hash(name);
34  v = vars[h];
35  while (v != 0) {
36    if (strcmp(name,v->name) == 0) break;
37    v = v->next;
38  }
39  return v;
40}
41
42static struct var *new_var(name)
43     char *name;
44{ struct var *v;
45  char *s;
46  unsigned int h;
47
48  h = hash(name);
49  s = (char *) malloc(strlen(name) + 1);
50  strcpy(s,name);
51  v = (struct var *) malloc(sizeof(struct var));
52  v->name = s;
53  v->hash = h;
54  v->level = level;
55  v->next = vars[h];
56  v->chain = chain;
57  v->val = FALSE;
58  chain = v;
59  vars[h] = v;
60  return v;
61}
62
63void setvar(name)
64     char *name;
65{ struct var *v;
66  if (((v = find_var(name)) == 0) || (v->level < level))
67    v = new_var(name);
68  v->val = TRUE;
69}
70
71void unsetvar(name)
72     char *name;
73{ struct var *v;
74  if (((v = find_var(name)) == 0) || (v->level < level))
75    v = new_var(name);
76  v->val = FALSE;
77}
78
79bool getvar(name)
80     char *name;
81{ struct var *v;
82  unsigned int invert = 0;
83  bool var_val;
84  if (name[0] == '!') {
85    name++;
86    invert = 1;
87  }
88  /* C doesn't have a logical XOR ! */
89  var_val = (bool) (((v = find_var(name)) != (struct var *) 0) &&
90                    (v->val != FALSE));
91  return invert? (bool) !var_val : var_val;
92}
93
94void vtable_push()
95{ level++;
96}
97
98void vtable_pop(){
99  if (level > 0) level--;
100  while ((chain != 0) && (chain->level > level)) {
101    struct var *old;
102    vars[chain->hash] = chain->next;
103    old = chain;
104    chain = chain->chain;
105    free(old->name);
106    free(old);
107  }
108}
Note: See TracBrowser for help on using the repository browser.