/***  init.c  ***/

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#ifndef __TURBOC__
/** standard prototypes **/
	void *calloc(unsigned,unsigned);
	int fprintf(const FILE*,const char*,...);
	int _filbuf(FILE*);
	void exit(int);
	int fclose(const FILE*);
	int fread(void*,int,int,const FILE*);
#endif
#include "Algol.h"
#include "standard.h"

/*----------------------------------------------------------------------*/
/* input files */
char *prld_file, *prnt_file, *prog_file, *dump_file;  FILE *infile;

#if DEBUG
	FILE *O; /*trace-file*/
#endif

/*----------------------------------------------------------------------*/
static read_int(void);

static int ch;

static int read_int()
{int i;
	i=getc(infile)<<8;
	return (i|getc(infile));
	/* return((i|=(ch=getc(infile))) & 0X8000?i-65536:i); */
}

/*----------------------------------------------------------------------*/

void create_IA()
{int i;
	LIND = (short int *)calloc(max_IND+1,sizeof(short int));
	VIND = (short int *)calloc(max_IND+1,sizeof(short int));
	A    = (short int *)calloc(max_A+1,sizeof(short int));
	B    = (int *)calloc(max_A+1,sizeof(int));
	C    = (int *)calloc(max_A+1,sizeof(int));
	D    = (short int *)calloc(max_A+1,sizeof(short int));
	for(i=1;i<max_A;i++)D[i]=i+1; D[max_A]=0;
	infile=fopen(prld_file,"rb");
	if(infile==NULL){
	    fprintf(stderr,"cannot find file %s, aborted\n",prld_file);
	    exit(1);
	} /* load standard prelude */
	J=read_int(); init_A=FS=read_int(); i=0;
	while(ch !=EOF && ++i<=J){LIND[i]=read_int(); VIND[i]=read_int();}
	i=0; while(ch != EOF && ++i<FS){
            A[i]=read_int(); B[i]=read_int(); C[i]=read_int(); D[i]=1000;
	}
	if(ch==EOF){
	    fprintf(stderr,"file %s is ruined\n",prld_file); exit(1);
	}
	fclose(infile);
	initialize_pi_and_random();
}

/*----------------------------------------------------------------------*/
/*  program to X, denotations to DN */

void create_X()
{	max_X = max_PX+2;
	X = (short int *)calloc(max_X, sizeof(short int));
	DN= (int *)calloc(max_DN+1,sizeof(int));
}

/* program-listing, gets the values max_X and max_DN */

int program_printing(list) bool list;
{	infile=fopen(prnt_file,"rb");
	if(infile==NULL){
	    fprintf(stderr,"cannot find file %s, aborted\n",prnt_file);
	    exit(1);
	} /* everything is passed to stdout till 0 is found */
	while((ch=getc(infile))>0) if(list) fprintf(stdout,"%c",ch);
	max_PX=read_int(); max_DN=read_int(); ch=getc(infile);
	if(getc(infile)){
	    fprintf(stderr,"file %s is ruined\n",prnt_file);
	    exit(1);
	} /* looks whether the program is syntactically correct */
	fclose(infile); return(ch);
}

void read_program()
{int i;
	infile=fopen(prog_file,"rb");
	if(infile==NULL){
	    fprintf(stderr,"cannot find file %s, aborted\n",prog_file);
	    exit(1);
	}  /* now reads X[1:max_PX] and DN[1:max_DN] */
	i=ch=0; while(ch != EOF && ++i < max_PX)X[i]=read_int();
	if(fread(&DN[1],sizeof(int),max_DN,infile) != max_DN || getc(infile)){
	    fprintf(stderr,"file %s is ruined\n",prog_file);
	    exit(1);
	}
	fclose(infile);
}

bool deal_arguments(argc,argv) int argc; char *argv[];
{
#if DEBUG
	if(argc<6){
	    fprintf(stderr,"%s\n",
			"args: %prelude %work1 %work2 %dump %trace [-]");
	    exit(1);}
	prld_file=argv[1]; prnt_file=argv[2]; prog_file=argv[3];
	dump_file=argv[4];
	return(argc==6);
#else
	if(argc<5){
	    fprintf(stderr,"%s\n","args: %prelude %work1 %work2 %dump [-]");
	    exit(1);}
	prld_file=argv[1]; prnt_file=argv[2]; prog_file=argv[3];
	dump_file=argv[4];
	return(argc==5);
#endif
}
	
