# include "def.h"
int hash(char *as)
{
/* hashpjw from Aho+Sethi+Ullman */
unsigned char *s = (unsigned char *)as;
unsigned h, g;
for (h = 0; *s != '\0'; s++) {
h = (h<<4) + *s;
if ((g = h&0xf0000000) != 0) {
h ^= (g>>24);
h ^= g;
}
}
return (h^(h>>16))&0x7fff;
}
void err(char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, progname? progname: "refer");
fprintf(stderr, ": ");
vfprintf(stderr, s, ap);
va_end(ap);
putc('\n', stderr);
exit(1);
}
void errline(char *s, int l, ...)
{
va_list ap;
va_start(ap, l);
fprintf(stderr, progname? progname: "refer");
fprintf(stderr, ": ");
fprintf(stderr, "on line %d: ", l);
vfprintf(stderr, s, ap);
va_end(ap);
putc('\n', stderr);
exit(1);
}
void warn(int lineno, char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, progname? progname: "refer");
fprintf(stderr, ": warning: ");
if (lineno)
fprintf(stderr, "line %d: ", lineno);
vfprintf(stderr, s, ap);
va_end(ap);
putc('\n', stderr);
}
void *zalloc(unsigned m, unsigned n)
{
char *t;
# if D1
fprintf(stderr, "calling calloc for %d*%d bytes\n", m, n);
# endif
t = calloc(m, n);
if (t == 0)
err("ran out of memory");
# if D1
fprintf(stderr, "calloc returned 0x%x\n", t);
# endif
return t;
}
|