/* main.c */

/* 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
 */

#include "tsl.h"

void syntax();												/* Show program syntax */

int timeout_flag = 0;

int main( int argc, char *argv[] )
{
	int port;					/* TCP port to connect on */
	char *filename;		/* The filename to read scripts from */
	int syntaxonly=0; /* Show syntax only */
	struct list_struct *pseudocode;		/* Will contain parsed TSL instructions */
	int sockfd;				/* Communications conduit to remote host */
	struct sigaction sigh;

	/* Initialize defaults */
	port = 23;												/* Use TCP port 23 by default */
 	filename = strdup("telnet.tsl");	/* Use this filename by default */
	assert( filename != NULL );

	/* Process any command-line switches */
	while ( 1 )
	{
		int startup_switch;
		startup_switch = getopt( argc, argv, "p:f:h");
		if ( startup_switch == -1 )
			break;

		switch( startup_switch)
		{
			case 'p':
				port = atoi( optarg ); 
				break;
			case 'f':
				filename = strdup( optarg );
				break;
			default:          /* Includes -h */
				syntaxonly=1;
		}
	}

	/* If the remote host wasn't specified, if -h was specified, or if
		 an unknown option was specified, show the program syntax and exit */
	if ( syntaxonly || argv[optind] == NULL)
	{
		syntax();
		return 1;
	}

	/* Parse the TSL file, placing the resulting pseudo-code into the
		 pseudocode structure */
	pseudocode = process_tsl( filename );

	/* Connect to the remote host */
	sockfd = tsl_connect( argv[optind], port ); 

	/* Set up the signal handler */
	sigemptyset( &sigh.sa_mask );
	sigaddset( &sigh.sa_mask, SIGALRM );
	sigh.sa_flags = 0;
	sigh.sa_handler = catch_sig;
	sigaction( SIGALRM, &sigh, 0 );

	/* Run the pseudocode */
	runtime( sockfd, pseudocode );

	return 0;
}

void syntax()
{
	printf( "TSL %s - A network protocol scripting tool\n", VERSION );
	printf( "Copyright (C) 2007 Tom Martin\n\n" );

	printf( "TSL is free software; you can redistribute it and/or modify\n" );
	printf( "it under the terms of the GNU General Public License as published by\n" );
	printf( "the Free Software Foundation; either version 2 of the License, or\n" );
	printf( "(at your option) any later version.\n\n" );
	printf( "TSL is distributed in the hope that it will be useful,\n" );
	printf( "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" );
	printf( "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" );
	printf( "GNU General Public License for more details.\n\n" );
	printf( "You should have received a copy of the GNU General Public License\n" );
	printf( "along with TSL; if not, write to the Free Software\n" );
	printf( "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n\n" );
	printf( "Correct syntax is:\n" );
	printf( "  tsl <host> [-p port] [-f telnet.tsl]\n\n" );
	printf( "Where:\n" );
	printf( "  <host> is the IP address to connect to\n" );
	printf( "  <port> is the TCP port to use, 23 by default\n" );
	printf( "  <telnet.tsl> is the name of the script, telnet.tsl by default\n\n" );
}

