/* SDB - definition file */

/*	set to alloc,free/hfree (normal) or malloc,mfree (debug)	*/
#define CALLOC alloc
#define CFREE hfree

/* #define DBPI */ /* Database procedural interface support (not full) */
/* #define DEBUG */ /* Trace prints etc */

/*	Character defs		*/

#define BS	0x7F
#define CR	0xD
#define FF	0xC
#define ESC	0x1B

#define STDIN	0
#define STDOUT	1
#define STDLST	2
#define STDPUN	3
#define STDERR	4

/*	Program limits		*/

#define LINEMAX		132	/* Max input line length */
#define	TABLEMAX	132	/*	table input length */
#define KEYWORDMAX	10	/*	keyword length */
#define NUMBERMAX	20	/*	number length */
#define MATHMAX		99	/*	Maths number max */
#define STRINGMAX	132	/*	string length */
#define CODEMAX		100	/*	length of code array */
#define STACKMAX	20	/*	interpreter stack size */

/*	Token defs	*/

#define	EOS		0
#define LSS		-1
#define LEQ		-2
#define EQL		-3
#define CONT		-4
#define NEQ		-5
#define GEQ		-6
#define GTR		-7
#define SELECT		-8
#define FROM		-9
#define WHERE		-10
#define CREATE		-11
#define DELETE		-12
#define INSERT		-13
#define EXXIT		-14
#define CHARR		-15
#define NUM		-16
#define ID		-17
#define STRING		-18
#define NUMBER		-19
#define UPDATE		-20
#define PRINT		-21
#define IMPORT		-22
#define EXPORT		-23
#define INTO		-24
#define PROMPT		-25
#define COMPRESS	-26
#define EXTRACT		-27
#define DEFYNE		-28
#define SHOW		-29
#define USING		-30
#define SORT		-31
#define BY		-32
#define ASCENDING	-33
#define DESCENDING	-34
#define SET		-35
#define ERASE		-36
#define RENAME		-37

/*	character tokens		*/

#define PLUS	'+'
#define CHAND	'&'
#define CHOR	'|'
#define CHNOT	'~'
#define COMMENT	'#'
#define CHALL	'*'
#define HELP	'?'


/*	Other tokens used in interpreter	*/

#define XSTOP	0
#define XPUSH	1
#define XNOT	2
#define TKXOR	3
#define TKXAND	4

/*	Operand types	*/

#define LITERAL	1
#define ATTR	2
#define TEMP	3

/*	Attribute data types	*/

#define TCHAR	1
#define TNUM	2

/*	Tuple status codes	*/

#define UNUSED	0
#define ACTIVE	1
#define DELETED	2

/*	Relation header page format defs	*/

#define RNSIZE	10
#define HSIZE	16
#define ASIZE	16
#define	ANSIZE	10
#define NATTRS	31

/*	Sizeof various structs	- NGT	*/

#define RELSIZE	672
#define SCNSIZE 10
#define SELSIZE 8
#define BINDSIZE 8
#define SATSIZE 16
#define SRELSIZE 10
#define OPSIZE 8	/*  I think - check up later */
#define CELLSIZE 2
#define MACSIZE 6
#define MTSIZE 4
#define IFSIZE 12
#define KEYSIZE 8
#define IOBSIZE 136	/* I/O buffer - iobuf */
#define TOTSIZE 10	/* Total field size */

/*	Error codes	*/

#define ENDSET	0
#define INSMEM	1
#define RELFNF	2
#define BADHDR	3
#define TUPINP	4
#define TUPOUT	5
#define RELFUL	6
#define RELCRE	7
#define DUPATT	8
#define MAXATT	9
#define INSBLK	10
#define SYNTAX	11
#define ATUNDF	12
#define ATAMBG	13
#define RLUNDF	14
#define CDSIZE	15
#define INPFNF	16
#define OUTCRE	17
#define INDFNF	18
#define BADSET	19

struct iobuf {
	int fd;
	int isect;
	int nextc;
	int lastop;
	char buff [128];
};

struct attribute {
	char at_name[ANSIZE];
	char at_type;
	char at_size;
	char at_scale;
	char at_width;
	char at_unused[ASIZE-ANSIZE-4];
};

struct header {		/* Must be 512 bytes */
	char hd_tcnt[2];
	char hd_tmax[2];
	char hd_data[2];
	char hd_size[2];
	char hd_unused[HSIZE-8];
	struct attribute hd_attrs[NATTRS];
};

struct relation {
	char rl_name[RNSIZE];
	unsigned rl_tcnt;
	unsigned rl_tmax;
	int rl_data;
	int rl_size;
	int rl_store;
	struct iobuf rl_fd;
	int rl_scnref;
	struct header rl_header;
	struct relation *rl_next;
};

struct scan {
	struct relation *sc_relation;
	unsigned sc_dtnum;
	unsigned sc_atnum;
	int sc_store;
	char *sc_tuple;
};

struct srel {
	char *sr_name;
	struct scan *sr_scan;
	int sr_ctuple;
	int sr_update;
	struct srel *sr_next;
};

struct sattr {
	char *sa_rname;
	char *sa_aname;
	char *sa_name;
	char *sa_aptr;
	char *sa_total;		/* total field */
	struct srel *sa_srel;
	struct attribute *sa_attr;
	struct sattr *sa_next;
};

struct operand {
	int o_type;
	union {
		struct {
			int ovc_type;
			char *ovc_string;
			int ovc_length;
		} ov_char;
		int ov_boolean;
	} o_value;
};

union codecell {
	int c_operator;
	struct operand *c_operand;
};

struct binding {
	struct attribute *bd_attr;
	char *bd_vtuple;
	char *bd_vuser;
	struct binding *bd_next;
};

struct sel {
	struct srel *sl_rels;
	struct sattr *sl_attrs;
	union codecell *sl_where;
	struct binding *sl_bindings;
};

struct mtext {
	char *mt_text;
	struct mtext *mt_next;
};

struct macro {
	char *mc_name;
	struct mtext *mc_mtext;
	struct macro *mc_next;
};

struct ifile {
	char *if_fp;
	struct mtext *if_mtext;
	char *if_cmdline;
	int if_savech;
	char *if_lptr;
	struct ifile *if_next;
};

struct skey {
	int sk_type;
	struct attribute *sk_aptr;
	int sk_start;
	struct skey *sk_next;
};

/* Global variables */

char dbv_tstring[STRINGMAX+1];
char *iprompt,*cprompt;
char cmdline[LINEMAX+2],*lptr;
char gbuf[LINEMAX];
char *pbuf;
char buffer[TABLEMAX+1];
char obuf[BUFSIZ];
int bndx;
int cndx;
int atbol;
int savech;
int savetkn;

int dbv_fold;
int dbv_token;
int dbv_tvalue;
int dbv_errcode;
int dbv_verify;
int dbv_total;
int dbv_auto;
int dbv_logging;
int dbv_lnct;
int dbv_pgln;
int dbv_page;

FILE *dbv_lfp;		/* log file */
struct ifile *dbv_ifp;
struct macro *dbv_macros;
struct operand *stack[STACKMAX],**sptr;
union codecell *cptr;
union codecell code[CODEMAX+1];

struct relation *reltns;

struct sel *selptr;

#define RETERR(erra) dbv_errcode = erra; return (NULL);

/* sdbio end */
struct relation *reltns;

struct sel *selptr;

#define RETERR(erra) dbv_errcode = erra; r