/*====================================================================*
 *
 *   ttycat.c - serial port test program;
 *
 *   write one or more files from stdin and write to one tty port;
 *   read serial stream from second tty port and write to stdout; 
 *
 *   this programs is designed for Linux operation only; apologies
 *   to the Microsoft-ies in userland;
 *
 *.  Released 2005 by Charles Maier Associates Ltd for private use;
 *:  Licensed under GNU General Public License version 2 and later;
 *;  Designed for CMLinux using LFS and Debian GNU/Linux software;
 *
 *--------------------------------------------------------------------*/

#define _GETOPT_H

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <memory.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <termios.h>

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include "../tools/getoptv.h"
#include "../tools/putoptv.h"
#include "../tools/error.h"
#include "../tools/files.h"

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include "../tools/getoptv.c"
#include "../tools/putoptv.c"
#include "../tools/version.c"
#include "../tools/efreopen.c"
#include "../tools/error.c"
#endif

/*====================================================================*
 *   program constants;
 *--------------------------------------------------------------------*/

#define TTYS0 "/dev/ttyS0"
#define TTYS1 "/dev/ttyS1"

/*====================================================================*
 *   
 *   void interrupt (signo_t signal);
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

static void interrupt (signo_t signal) 

{
	fprintf (stderr, "Process %d terminated by user\n", getpid ());
	return;
}

/*====================================================================*
 *   
 *   void interrupts ();
 *   
 *.  Atheros Powerline Toolkit for HomePlug AV;
 *:  Published 2006-2009 by Intellon Corp. ALL RIGHTS RESERVED;
 *;  For demonstration; Not for production use;
 *
 *--------------------------------------------------------------------*/

static void interrupts ()

{
	struct sigaction action;
	memset (&action, 0, sizeof (action));
	action.sa_handler = interrupt;
	sigaction (SIGTERM, &action, (struct sigaction *)(0));
	sigaction (SIGQUIT, &action, (struct sigaction *)(0));
	sigaction (SIGTSTP, &action, (struct sigaction *)(0));
	sigaction (SIGINT, &action, (struct sigaction *)(0));
	sigaction (SIGHUP, &action, (struct sigaction *)(0));
	return;
}

/*====================================================================*
 *
 *   signed copy (signed ifd, signed ofd, void * memory, size_t extent);
 *
 *.  Released 2005 by Charles Maier Associates Ltd for private use;
 *:  Licensed under GNU General Public License version 2 and later;
 *;  Designed for CMLinux using LFS and Debian GNU/Linux software;
 *
 *--------------------------------------------------------------------*/

static signed copy (signed ifd, signed ofd, void * memory, signed extent) 

{
	while ((extent = read (ifd, memory, extent)) > 0) 
	{
		if (write (ofd, memory, extent) < extent) 
		{
			return (-1);
		}
	}
	return (0);
}

/*====================================================================*
 *
 *   signed function (signed ttys0, signed ttys1, void * memory, size_t extent);
 *
 *.  Released 2005 by Charles Maier Associates Ltd for private use;
 *:  Licensed under GNU General Public License version 2 and later;
 *;  Designed for CMLinux using LFS and Debian GNU/Linux software;
 *
 *--------------------------------------------------------------------*/

static signed function (signed fd0, signed fd1, void * memory, signed extent) 

{
	pid_t pid = fork ();
	if (pid < 0) 
	{
		error (1, errno, "Can't fork process");
	}
	if (pid > 0) 
	{
		copy (fd1, STDOUT_FILENO, memory, extent);
	}
	else
	{
		copy (STDIN_FILENO, fd0, memory, extent);
	}
	return (0);
}

/*====================================================================*
 *
 *   int main (int argc, const char * argv []);
 *
 *
 *.  Motley Tools by Charles Maier; cmaier@cmassoc.net;
 *:  Published 2005 by Charles Maier Associates for internal use;
 *;  Released under GNU General Public License v2 and later;
 *
 *--------------------------------------------------------------------*/

int main (int argc, const char * argv []) 

{
	static const char *optv [] = 
	{
		"r:s:",
		PUTOPTV_S_FUNNEL,
		"copy one or more files to stdout",
		"r s\tread tty (s) and write stdout [" TTYS1 "]",
		"s s\tread stdin and write tty (s) [" TTYS0 "]",
		(const char *)(0) 
	};
	struct termios initial;
	struct termios current;
	byte buffer [100];
	char const * ttys0 = TTYS0;
	char const * ttys1 = TTYS1;
	signed fd0 = -1;
	signed fd1 = -1;
	signed c;
	optmin = 0;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch (c) 
		{
		case 's':
			ttys0 = optarg;
			break;
		case 'r':
			ttys1 = optarg;
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if ((fd0 = open (ttys0, O_BINARY | O_WRONLY | O_NOCTTY)) == -1) 
	{
		error (1, errno, "Can't open %s for write", ttys0);
	}
	if ((fd1 = open (ttys1, O_BINARY | O_RDONLY | O_NOCTTY)) == -1) 
	{
		error (1, errno, "Can't open %s for read", ttys1);
	}
	tcgetattr (fd0, &initial);
	memset (&current, 0, sizeof (current));
	current.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
	current.c_iflag = IGNPAR;
	current.c_oflag = 0;
	current.c_lflag = 0;
	current.c_cc [VTIME] = 0;
	current.c_cc [VMIN] = 5;
	tcflush (fd0, TCIFLUSH);
	tcflush (fd1, TCIFLUSH);
	tcsetattr (fd0, TCSANOW, &current);
	tcsetattr (fd1, TCSANOW, &current);
	interrupts ();
	if (!argc) 
	{
		function (fd0, fd1, buffer, sizeof (buffer));
	}
	while ((argc) && (*argv)) 
	{
		if (efreopen (*argv, "rb", stdin)) 
		{
			function (fd0, fd1, buffer, sizeof (buffer));
		}
		argc--;
		argv++;
	}
	tcsetattr (fd0, TCSANOW, &initial);
	tcsetattr (fd1, TCSANOW, &initial);
	close (fd0);
	close (fd1);
	exit (0);
}