1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/* hash.c: hash table support for functions and variables. */
/*
Functions and variables are cached in both internal and external
form for performance. Thus a variable which is never "dereferenced"
with a $ is passed on to rc's children untouched. This is not so
important for variables, but is a big win for functions, where a call
to yyparse() is involved.
*/
#include "rc.h"
#include "sigmsgs.h"
static bool var_exportable(char *);
static bool fn_exportable(char *);
static int hash(char *, int);
static int find(char *, Htab *, int);
static void free_fn(rc_Function *);
Htab *fp;
Htab *vp;
static int fused, fsize, vused, vsize;
static char **env;
static int bozosize;
static int envsize;
static bool env_dirty = TRUE;
static char *dead = "";
#define HASHSIZE 64 /* rc was debugged with HASHSIZE == 2; 64 is about right for normal use */
extern void inithash() {
Htab *fpp, *vpp;
int i;
fp = ealloc(sizeof(Htab) * HASHSIZE);
vp = ealloc(sizeof(Htab) * HASHSIZE);
fused = vused = 0;
fsize = vsize = HASHSIZE;
for (vpp = vp, fpp = fp, i = 0; i < HASHSIZE; i++, vpp++, fpp++)
vpp->name = fpp->name = NULL;
}
#define ADV() {if ((c = *s++) == '\0') break;}
/* hash function courtesy of paul haahr */
static int hash(char *s, int size) {
int c, n = 0;
while (1) {
ADV();
n += (c << 17) ^ (c << 11) ^ (c << 5) ^ (c >> 1);
ADV();
n ^= (c << 14) + (c << 7) + (c << 4) + c;
ADV();
n ^= (~c << 11) | ((c << 3) ^ (c >> 1));
ADV();
n -= (c << 16) | (c << 9) | (c << 2) | (c & 3);
}
if (n < 0)
n = ~n;
return n & (size - 1); /* need power of 2 size */
}
static bool rehash(Htab *ht) {
int i, j, size;
int newsize, newused;
Htab *newhtab;
if (ht == fp) {
if (fsize > 2 * fused)
return FALSE;
size = fsize;
} else {
if (vsize > 2 * vused)
return FALSE;
size = vsize;
}
newsize = 2 * size;
newhtab = ealloc(newsize * sizeof(Htab));
for (i = 0; i < newsize; i++)
newhtab[i].name = NULL;
for (i = newused = 0; i < size; i++)
if (ht[i].name != NULL && ht[i].name != dead) {
newused++;
j = hash(ht[i].name, newsize);
while (newhtab[j].name != NULL) {
j++;
j &= (newsize - 1);
}
newhtab[j].name = ht[i].name;
newhtab[j].p = ht[i].p;
}
if (ht == fp) {
fused = newused;
fp = newhtab;
fsize = newsize;
} else {
vused = newused;
vp = newhtab;
vsize = newsize;
}
efree(ht);
return TRUE;
}
#define varfind(s) find(s, vp, vsize)
#define fnfind(s) find(s, fp, fsize)
static int find(char *s, Htab *ht, int size) {
int h = hash(s, size);
while (ht[h].name != NULL && !streq(ht[h].name, s)) {
h++;
h &= size - 1;
}
return h;
}
extern void *lookup(char *s, Htab *ht) {
int h = find(s, ht, ht == fp ? fsize : vsize);
return (ht[h].name == NULL) ? NULL : ht[h].p;
}
extern rc_Function *get_fn_place(char *s) {
int h = fnfind(s);
env_dirty = TRUE;
if (fp[h].name == NULL) {
if (rehash(fp))
h = fnfind(s);
fused++;
fp[h].name = ecpy(s);
fp[h].p = enew(rc_Function);
} else
free_fn(fp[h].p);
return fp[h].p;
}
extern Variable *get_var_place(char *s, bool stack) {
Variable *new;
int h = varfind(s);
env_dirty = TRUE;
if (vp[h].name == NULL) {
if (rehash(vp))
h = varfind(s);
vused++;
vp[h].name = ecpy(s);
vp[h].p = enew(Variable);
((Variable *)vp[h].p)->n = NULL;
return vp[h].p;
} else {
if (stack) { /* increase the stack by 1 */
new = enew(Variable);
new->n = vp[h].p;
return vp[h].p = new;
} else { /* trample the top of the stack */
new = vp[h].p;
efree(new->extdef);
listfree(new->def);
return new;
}
}
}
extern void delete_fn(char *s) {
int h = fnfind(s);
if (fp[h].name == NULL)
return; /* not found */
env_dirty = TRUE;
free_fn(fp[h].p);
efree(fp[h].p);
efree(fp[h].name);
if (fp[(h+1)&(fsize-1)].name == NULL) {
--fused;
fp[h].name = NULL;
} else {
fp[h].name = dead;
}
}
extern void delete_var(char *s, bool stack) {
int h = varfind(s);
Variable *v;
if (vp[h].name == NULL)
return; /* not found */
env_dirty = TRUE;
v = vp[h].p;
efree(v->extdef);
listfree(v->def);
if (v->n != NULL) { /* This is the top of a stack */
if (stack) { /* pop */
vp[h].p = v->n;
efree(v);
} else { /* else just empty */
v->extdef = NULL;
v->def = NULL;
}
} else { /* needs to be removed from the hash table */
efree(v);
efree(vp[h].name);
if (vp[(h+1)&(vsize-1)].name == NULL) {
--vused;
vp[h].name = NULL;
} else {
vp[h].name = dead;
}
}
}
static void free_fn(rc_Function *f) {
treefree(f->def);
efree(f->extdef);
}
extern void initenv(char **envp) {
int n;
for (n = 0; envp[n] != NULL; n++)
;
n++; /* one for the null terminator */
if (n < HASHSIZE)
n = HASHSIZE;
env = ealloc((envsize = 2 * n) * sizeof (char *));
for (; *envp != NULL; envp++)
if (strncmp(*envp, "fn_", conststrlen("fn_")) == 0) {
if (!dashpee)
fnassign_string(*envp);
} else {
if (!varassign_string(*envp)) /* add to bozo env */
env[bozosize++] = *envp;
}
}
static char *neverexport[] = {
"apid", "apids", "bqstatus", "cdpath", "home",
"ifs", "path", "pid", "status", "*"
};
/* for a few variables that have default values, we export them only
if they've been explicitly set; maybeexport[n].flag is TRUE if this
has occurred. */
struct nameflag {
char *name;
bool flag;
};
static struct nameflag maybeexport[] = {
{ "prompt", FALSE },
{ "version", FALSE }
};
void set_exportable(char *s, bool b) {
int i;
for (i = 0; i < arraysize(maybeexport); ++i)
if (maybeexport[i].flag != b && streq(s, maybeexport[i].name))
maybeexport[i].flag = b;
}
static bool var_exportable(char *s) {
int i;
for (i = 0; i < arraysize(neverexport); i++)
if (streq(s, neverexport[i]))
return FALSE;
for (i = 0; i < arraysize(maybeexport); i++)
if (maybeexport[i].flag == FALSE && streq(s, maybeexport[i].name))
return FALSE;
return TRUE;
}
static bool fn_exportable(char *s) {
int i;
if (strncmp(s, "sig", conststrlen("sig")) == 0) { /* small speed hack */
for (i = 0; i < NUMOFSIGNALS; i++)
if (streq(s, signals[i].name))
return FALSE;
if (streq(s, "sigexit"))
return FALSE;
}
return TRUE;
}
extern char **makeenv() {
int ep, i;
char *v;
if (!env_dirty)
return env;
env_dirty = FALSE;
ep = bozosize;
if (vsize + fsize + 1 + bozosize > envsize) {
envsize = 2 * (bozosize + vsize + fsize + 1);
env = erealloc(env, envsize * sizeof(char *));
}
for (i = 0; i < vsize; i++) {
if (vp[i].name == NULL || vp[i].name == dead || !var_exportable(vp[i].name))
continue;
v = varlookup_string(vp[i].name);
if (v != NULL)
env[ep++] = v;
}
for (i = 0; i < fsize; i++) {
if (fp[i].name == NULL || fp[i].name == dead || !fn_exportable(fp[i].name))
continue;
env[ep++] = fnlookup_string(fp[i].name);
}
env[ep] = NULL;
qsort(env, (size_t) ep, sizeof(char *), starstrcmp);
return env;
}
extern void whatare_all_vars(bool showfn, bool showvar) {
int i;
List *s;
if (showvar)
for (i = 0; i < vsize; i++)
if (vp[i].name != NULL && (s = varlookup(vp[i].name)) != NULL)
prettyprint_var(1, vp[i].name, s);
if (showfn)
for (i = 0; i < fsize; i++)
if (fp[i].name != NULL && fp[i].name != dead)
prettyprint_fn(1, fp[i].name, fnlookup(fp[i].name));
}
/* fake getenv() for readline() follows: */
#if EDITLINE || READLINE
extern char *getenv(const char *name) {
List *s;
if (name == NULL || vp == NULL || (s = varlookup((char *) name)) == NULL)
return NULL;
return s->w;
}
#endif
|