/*
 *	Copyright (C) 1986  SRI International
 *	Copyright (C) 1988  TGV, Incorporated
 *
 *	Routines to deal with "TAKE" files
 *
 */
#include "gtjfn.h"
#include <stat.h>
#include <stdio.h>
#include "vax-mm.h"

static FILE *Take_File;		/* File we are getting commands from */

/*
 *	Process the "TAKE" command
 */
CMD_Take()
{
	static struct comnd_function file = {COMND_INPUT_FILE};
	static struct gtjfn_block gtjfn;
	int i;
	char Filename[128];

	/*
	 *	Noise
	 */
	Noise("commands from file");
	/*
	 *	Parse the filename
	 */
	gtjfn.default_extension = "CMD";
	Command_State.gtjfn = &gtjfn;
	if (COMND(&Command_State,&file,&i,0) < 0) return;
	if (local_jfns_jsys(Filename,i) < 0) return;
	tops_free_jfn(i);	/* TEMP */
	/*
	 *	Confirm
	 */
	Confirm();
	/*
	 *	If there is already a TAKE file, close it
	 */
	if (Take_File) {
		fclose(Take_File);
		Take_File = NULL;
		Doing_Take_File = 0;
	}
	/*
	 *	Open the NEW take file
	 */
	Take_File = fopen(Filename,"r");
	if (Take_File == NULL) {
		printf("?Can't open TAKE file\n");
	} else {
		Doing_Take_File = 1;
	}
	/*
	 *	Definitely not doing RSCAN anymore
	 */
	Doing_Rscan = 0;
	/*
	 *	Done
	 */
	return(0);
}


/*
 *	Process the next line of the TAKE file
 */
Take_Comnd_Init(State)
register struct comnd_state *State;
{
	int i;

	/*
	 *	Reparse address is NOT set
	 */
	State->flags &= ~COMND_REPARSE_ADDR_SET;
	/*
	 *	Read the next line from the batch file
	 */
	if (fgets(State->buffer,State->buffer_size,Take_File) == NULL) {
		/*
		 *	End of batch file
		 */
		fclose(Take_File);
		Take_File = NULL;
		Doing_Take_File = 0;
		return;
	}
	/*
	 *	Setup the parse
	 */
	i = strlen(State->buffer)-1;
	State->next_field = State->buffer;
	State->unparsed_characters = i;
	State->space_left = State->buffer_size - State->unparsed_characters;
	State->flags &= ~COMND_STATUS_FLAGS;
	State->flags |= COMND_CR_TYPED;
	/*
	 *	Done
	 */
	return;
}
