#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)vocab.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: vocab.c,v 1.16 2021/05/02 12:50:43 rillig Exp $");
#endif
#endif
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include "hdr.h"
#include "extern.h"
void
destroy(int object)
{
move(object, 0);
}
void
juggle(int object)
{
int i, j;
i = place[object];
j = fixed[object];
move(object, i);
move(object + 100, j);
}
void
move(int object, int where)
{
int from;
if (object <= 100)
from = place[object];
else
from = fixed[object - 100];
if (from > 0 && from <= 300)
carry(object, from);
drop(object, where);
}
int
put(int object, int where, int pval)
{
move(object, where);
return (-1 - pval);
}
void
carry(int object, int where)
{
int temp;
if (object <= 100) {
if (place[object] == -1)
return;
place[object] = -1;
holding++;
}
if (atloc[where] == object) {
atloc[where] = links[object];
return;
}
for (temp = atloc[where]; links[temp] != object; temp = links[temp]);
links[temp] = links[object];
}
void
drop(int object, int where)
{
if (object > 100)
fixed[object - 100] = where;
else {
if (place[object] == -1)
holding--;
place[object] = where;
}
if (where <= 0)
return;
links[object] = atloc[where];
atloc[where] = object;
}
int
vocab(const char *word, int type, int value)
{
int adr;
const char *s;
char *t;
int hash, i;
struct hashtab *h;
for (hash = 0, s = word, i = 0; i < 5 && *s; i++)
hash += *s++;
hash = (hash * 3719) & 077777;
hash %= HTSIZE;
for (adr = hash;; adr++) {
if (adr == HTSIZE)
adr = 0;
h = &voc[adr];
switch (type) {
case -2:
if (h->val)
goto exitloop2;
h->val = value;
h->atab = malloc(length(word));
if (h->atab == NULL)
err(1, NULL);
for (s = word, t = h->atab; *s;)
*t++ = *s++ ^ '=';
*t = 0 ^ '=';
return (0);
case -1:
if (h->val == 0)
return (-1);
for (s = word, t = h->atab; *t ^ '=';)
if ((*s++ ^ '=') != *t++)
goto exitloop2;
if ((*s ^ '=') != *t && s - word < 5)
goto exitloop2;
return (h->val);
default:
if (h->val == 0)
errx(1,"Unable to find %s in vocab", word);
for (s = word, t = h->atab; *t ^ '=';)
if ((*s++ ^ '=') != *t++)
goto exitloop2;
if (h->val / 1000 != type)
continue;
return (h->val % 1000);
}
exitloop2:
if (adr + 1 == hash || hash == 0)
errx(1,"Hash table overflow");
}
}
static __unused void
prht(void)
{
int i, j, l;
char *c;
struct hashtab *h;
for (i = 0; i < HTSIZE / 10 + 1; i++) {
printf("%4d", i * 10);
for (j = 0; j < 10; j++) {
if (i * 10 + j >= HTSIZE)
break;
h = &voc[i * 10 + j];
putchar(' ');
if (h->val == 0) {
printf("-----");
continue;
}
for (l = 0, c = h->atab; l < 5; l++)
if ((*c ^ '='))
putchar(*c++ ^ '=');
else
putchar(' ');
}
putchar('\n');
}
}