#include "tsl.h"

/* This file is part of TSL.
 *
 * TSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * TSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TSL; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

static void process_file( FILE *tslfile, struct list_struct *section,
		struct list_struct *line, char *name );

/* This function processes the main TSL instruction file.  */
struct list_struct *process_tsl( char *filename )
{
	FILE *tslfile;
	struct list_struct *section, *line;

	/* Open the TSL file for reading */
	tslfile = fopen( filename, "r" );
	if( tslfile == NULL )
	{
		printf( "Unable to open TSL file (%s)\n", filename );
		exit(1);
	}

	/* Create the lists for holding lines and sections */
	line = list_create();
	section = list_create();
	list_add( section, "_init", line );				/* Tie lines to the section */

	/* Process the passed file, using the specified section and appending to
		 its "lines" */
	process_file( tslfile, section, line, filename );

	fclose( tslfile );

	return section;
}

/* This function processed the specified file, used for the main TSL file
	 and for any include files */
static void process_file( FILE *tslfile, struct list_struct *section,
		struct list_struct *line, char *name )
{
	char *buffer;
	struct list_struct *fields;
	int line_no;
	FILE *include_file;

	/* Initialize variables and buffer */
	buffer=calloc( 1, 1024 );
	assert( buffer != NULL );
	line_no = 0;

	/* Loop through the file */
	while( ! feof( tslfile ) )
	{
		if ( fgets( buffer, 1024, tslfile ) == NULL )		/* Read a line from the file */
			break;
		if( buffer[strlen(buffer)-1] == '\n' )	/* Delete the newline */
			buffer[strlen(buffer)-1] = 0;
		fields = list_create_from_string( buffer );		/* Parse into words */
		++line_no;

		/* If this is a blank line, don't add it */
		if ( list_count(fields) == 0 )
		{
			free( fields );												/* Free the memory for the structure */
			continue;															/* Process the next line */
		}

		/* If this is a comment line, don't add it */
		if ( ((char *)list_get1(fields))[0] == '#' )
		{
			list_free( fields );									/* Free the fields read */
			free( fields );												/* Free the structure */
			continue;															/* Process the next line */
		}

		/* If this is a "label", then it is the beginning of a new section */
		if ( strcmp(list_get1(fields),"label") == 0 )
		{
			line = list_create();									/* New lines for a new section */
			list_next( fields );									/* Point to the name of the label */
			list_add( section, list_get1(fields), line );   /* Bind the new line to the
																												 new section */
			continue;
		}

		/* If this is an "include", then read in the next file now.  Whether the file
			 can be included or not, the resulting include command is not kept */
		if ( strcmp(list_get1(fields),"include") == 0 )
		{
			/* The filename is the second argument, open it for read */
			list_next(fields);
			include_file = fopen( list_get1(fields), "r" );
			/* If we can't open it, print a message and ignore the line */
			if ( include_file == NULL )
			{
				printf( "Unable to open include file: %s\n", (char *)list_get1(fields) );
				list_free(fields);
				free(fields);
				continue;
			}
			/* If it is open, process the file, and return back to this one when the
				 first is done */
			process_file( include_file, section, line, (char *)list_get1(fields) );
			list_free(fields);
			free(fields);
			fclose( include_file );
			continue;
		}

		/* No special handling is required for this line, just add the fields to the
			 line */
		list_add( line, (void *)line_no, fields );
	}
}	

