/* (c) Copyright 1987 Michael Goetz								*/
/*																			*/
/*				File:		  	define									*/
/*				Returns:	  	void										*/
/*				Parameters:	char * ztext - key					*/
/*								int val - value						*/
/*																			*/

#include <stdio.h>
#include <string.h>

#include "advm.h"
#include "string.h"
#include "units.h"
#include "values.h"
#include "xrflag.h"
#include "mash.h"
#include "snapit.h"


void define (ztext, val)
char *ztext;
int val;

{
	char ztexan [13];
	int found,imodx,rehash,j;

	strncpy(ztexan, (ztext[0] == '@')?ztext+1:ztext, 12);
	ztexan[12] = 0;
	found = mash(ztexan);
	imodx = found % rehshn;
	rehash = rehshv[imodx];
	found = found % 2000;
	if (xref)
		{
		fprintf(xreffi, "%-12.12s %2d %-12.12s %5d*\n",
					ztexan,fnum,zfid,nrec);
		xrflag = TRUE;
		}

	for (j=0; j<2000 && zname[found][0] && strcmp(zname[found], ztexan); j++)
		found = (found+rehash) % 2000;

		/* figure out why loop stopped */
	if (j >= 2000)
		{
		printf("SYMBOL TABLE OVERFLOW - RECOMPILE\n");
		exit(1);
		}
	if (!zname[found][0])
		 {
		 strncpy(zname[found],ztexan,12);
		 zname[found][12] = 0;
		 keys[found] = val;
		 auxval[found] = 0;
		 refit[found] = refdef[val/1000];
		 zunref[found] = '*';
		 }
	 else
		 {
		 snapit();
		 printf("--Duplicate symbol\n");
		 troubl = TRUE;
		 }
	}
