/** Whhtable.c
  *
  *   DESCRIPTION:
  *   ===========
  *
  * This function finds the index associated with the function
  * name given in the argument string.
  *
  * It uses a hashing function that was found using the program
  * 'hash' by W.G.J. Langeveld.
  *
  *   SYNOPSIS:
  *   =========
  *
  * index = whhtable_index(name)
  *
  * int index the index of the function, -1 if not found.
  * char *name name of function.
  *
  *	Implement the Amiga environment access functions for rexxarplib.library.
  *
  *   AUTHOR/DATE:  W.G.J. Langeveld, November 1987.
  *   ============
  *
  *	CURRENT VERSION:
  *
  *	This version has been converted to SAS C 6.5 format. It has been modified
  *	for modern definition sequences for ANSI compilation. This no longer works
  *	with OS versions prior to 2.04.
  *
  * Latest change is to fix so it compiles with 3.9 NDK and make the RALP_
  * command name prefix work in all cases.
  *
  *   AUTHOR/DATE:  Joanne Dow, jdow@bix.com, June 1998.
  *   AUTHOR/DATE:  Joanne Dow, jdow@bix.com, 23 Apr 2002.
  *   ============
  *
  **/

#include <exec/types.h>
//#include "ralprotos.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static int hash_table[] = 
{
	14,   2,   0,  -1,  37,  -1,  -1,  17,  32,   3,
	11,  24,  -1,  21,  18,  29,  -1,  39,  -1,  -1,
	-1,  -1,   5,   4,  -1,  -1,  15,  -1,  27,  36,
	-1,  -1,  -1,  28,  42,  -1,  -1,  -1,  -1,  16,
	-1,  12,   8,  45,  -1,  -1,  26,  23,  -1,  -1,
	-1,  38,  -1,  -1,  40,  -1,  44,  -1,   1,  -1,
	-1,   6,  -1,  43,  -1,  -1,  34,  31,  -1,  -1,
	-1,  33,  41,  -1,  -1,  -1,  22,  20,  -1,  -1,
	-1,  -1,  -1,  -1,   7,  -1,  -1,  -1,  -1,  30,
	-1,  -1,  10,  -1,  -1,  -1,  -1,  13,  25,   9,
	-1,  -1,  19,  -1,  -1,  -1,  -1,  -1,  -1,  35,
	-1,  -1,  -1,  
};


int whhtable_index( char *s );
static int hash(unsigned char *);

int whhtable_index( char *s )
{
	return(hash_table[hash(s)]);
}

static int hash( unsigned char *s )
{
	register int res;
	register unsigned char *sp;
	register unsigned int c;
	
	res = strlen(s);
	
	for (sp = s; *sp; sp++ ) 
	{
		c = toupper(*sp);
		res = ((res * 110 + c ) & 0x7ff);
	}
#ifdef DEBUG_HASH
	{
		FILE *foobar = fopen( "ram:foobar", "a" );
		if ( foobar )
		{
			fprintf( foobar, "whh Cmd: \"%s\" hash %d\n", s, res % 37 );
			fclose( foobar );
		}
	}
#endif
	return(res % 113);
}
