/*
 *	ulib.c
 *
 *	Amiga UUPC library
 *
 *	$Id: ulib.c,v 1.2 90/01/16 10:27:53 crash Exp Locker: crash $
 */

#ifndef lint
static char RCSid[] = "$Id: ulib.c,v 1.2 90/01/16 10:27:53 crash Exp Locker: crash $";
#endif /* lint */

#include <stdio.h>
#include "host.h"

#if 0
#  if MCH_AMIGA			/* If Manx C */
#   include <sgtty.h>
#  endif
#endif

/*
 *	login (for slave in PC mode)
 *
 *	Real dumb login handshake
 */

login()
{
	char	logmsg[132];
#ifdef PC
lretry:
	msgtime = 9999;
	rmsg(logmsg, 0); /* wait for a <CR> or <NL> */
	msgtime = 2 * MSGTIME;
	wmsg("Login: ", 0);
	rmsg(logmsg, 0);
	printmsg( 0, "login = %s", logmsg );
	wmsg("Password:", 0);
	rmsg(logmsg, 0);
	printmsg( 14, "Password = %s", logmsg );
	if (strcmp(logmsg, "uucp") != 0)
		goto lretry;
#endif
	return('I');
}

char inbuf[BUFSIZ];
char outbuf[BUFSIZ];

swrite(data, num)
int	num;
char	*data;
{
	int test;
	unsigned char * cp;

	test = SIOWrite( data, num );
	return( test );
}


/*
 *	non-blocking read essential to "g" protocol
 *
 *	see "dcpgpkt.c" for description
 *
 *	This all changes in multitask systems.  Requests for
 *	I/O should get queued and an event flag given.  Then the
 *	requesting process (e.g. gmachine()) waits for the event
 *	flag to fire, processing either a read or a write.
 *
 *	Could be implemented on VAX/VMS or DG but not MS-DOS.
 */

sread(buf, num, timeout)
char	*buf;
int	num, timeout;
{
#if 0
	return( SIORead( buf, num, num, timeout*10 ) );
#endif
	int count;
	int test;
	unsigned char * cp;

	if (debuglevel > 13)
		fputc( '[', stderr );
	printmsg( 15, "sread: num: %d  timeout: %d", num, timeout );

	count = SIORead( buf, num, num, timeout*10 );
	printmsg( 15, "sread: read: %d ", count );

	if (debuglevel > 13 && count > 0) {
		test = count;
		cp = (unsigned char *) buf;
		while (test--)
			fprintf( stderr, isprint(*cp)? "[%c]":"[%02x]", *cp++ );
	}
	if (debuglevel > 13)
		fputc( ']', stderr );
	return( count );
}

openline(name, baud)
char	*name, *baud;
{
	printmsg( 3, "openline: name: \"%s\"  baud: \"%s\"", name, baud );
	if ( SIOInit( name, baud ) )
		return -1;
	SIOInBuffer( inbuf, BUFSIZ );
	SIOOutBuffer( outbuf, BUFSIZ );
	return( 0 );
}

closeline()
{
	SIOClose( 1 );
}

void nodot(string)
{
	return;
}

notimp( argc, argv )
char *argv[];
{
	fprintf( stderr, "shell: %s not implemented\n", *argv );
}

/*
 *	RNews:	my private rnews!
 *
 *		C-News would call a shell script which gives its' stdin to a
 *		program called "newsspool".  The newsspool program scans the input
 *		and peels off the "#! c[7]unbatch" header and then puts the file
 *		into $NEWSART/in.coming/nspool.XXXXXX
 *
 *		So what we do here is call that "newsspool" program as a function.
 *		See the file "rnews.c"
 */
#ifdef CNEWS
static void RNews( inname )
char *inname;
{
	int		dummy;
	char	*argv[3];

	argv[0] = "built_in_rnews";
	argv[1] = (char *) NULL;
	argv[2] = (char *) NULL;

	freopen( inname, "r", stdin );
	dummy = debuglevel;				/* Turn off debugging... */
	debuglevel = 0;
	rnews( 1, argv );
	debuglevel = dummy;				/* Turn it back on */
	freopen( "*", "r", stdin );
}
#else /* !CNEWS */
static void RNews( inname )
char *inname;
{
	extern	char *newsdir;
	register struct tm	*thetm;
	long	tloc;
	char	filename[132];
	char	format[128];
	FILE 	*f, *fin;
	FILE	*FOPEN();
	char	buf[BUFSIZ];

	static int count = 0;
	int	len;

	/* inname is of form "D.jlamiBCnnnn".  Pick off the nnnn. */
	len = strlen( inname ) - 1;
	while ( len >= 0 ) {
		if ( '0' <= inname[len] && inname[len] <= '9' )
			--len;
		else
			break;
	}
	sprintf( filename, "%s/%s", newsdir, &inname[len+1] );
	if ( (f = FOPEN( filename, "r" )) != NULL ) {
		/* Already exists, so make a timestamped one. */
		fclose( f );
		tloc = time( (long *)NULL );
		thetm = localtime( &tloc );

		sprintf( filename, "%s/%02d%02d%02d%02d%02d%02d.%03d",
		    newsdir,
		    thetm->tm_year % 100, thetm->tm_mon,
		    thetm->tm_mday, thetm->tm_hour,
		    thetm->tm_min,  thetm->tm_sec,  count);
		++count;
	}
	fprintf( stderr, "rnews: %s\n", filename );

	/*
	 *	The file-types are binary 'cuz the news comes in compressed.
	 */

	if ( (f = FOPEN( filename, "w", 'b' )) == (FILE *)NULL ) {
		fprintf( stderr, "rnews: can't open %s\n", filename );
		return;
	}
	if ( (fin = FOPEN( inname, "r", 'b' )) == NULL ) {
		fprintf( stderr, "rnews: Couldn't open %s\n", inname );
		fclose( f );
		return;
	}
	while ( tloc = fread(buf, 1, BUFSIZ, fin) )
		fwrite(buf, 1, tloc, f);

	fclose( f );
	fclose( fin );
}
#endif /* !CNEWS */

/*
 *	shell
 */

char *getcwd();

shell( command, inname, outname, errname )
char *command, *inname, *outname, *errname;
{
#ifdef AMIGA
# ifdef FJE
	FILE	tmp_stdout, tmp_stderr;
	extern	FILE *logfile;
# endif
#endif
	char	*argvec[50];
	int		rmail();
	int		rnews();
	int 	argcp, args;
	char	**argvp;
	int		(*proto)();

	argcp = getargs( command, argvec );
	argvp = argvec;

	if ( debuglevel > 5 ) {
		args = argcp;
		while ( args )
			fprintf( stderr, "arg: %d  %s\n", args--, *argvp++ );
		argvp = argvec;
		args = argcp;
	}
	proto = notimp;
	if ( strcmp( *argvp, "rmail" ) == SAME )
		proto = rmail;
	else if ( strcmp( *argvp, "rnews" ) == SAME ) {
		/* proto = rnews; */
		RNews( inname );
		return;
	}
#ifdef AMIGA
# ifdef FJE
	if (!outname || !*outname) {
		fprintf( stderr, "assigning stdout to LOGFILE\n" );
		tmp_stdout = *stdout;
		*stdout = *logfile;
	}
	if (!errname || !*errname) {
		fprintf( stderr, "assigning stderr to LOGFILE\n" );
		tmp_stderr = *stderr;
		*stderr = *logfile;
	}
# endif /* FJE */
#endif /* AMIGA */

	if ( *inname != '\0' ) {
		printmsg( 2, "reopening stdin as %s\n", inname );
		printmsg( 2, "curdir: %s\n", getcwd(NULL, 0));
		if ( freopen( inname, "r", stdin ) == NULL )
			printmsg( 0, "Couldn't open %s: %d\n", inname, errno );
	}
	(*proto)( argcp, argvp );

#ifdef AMIGA
	freopen( "*", "r", stdin );
# ifdef FJE
	*stdout = tmp_stdout;
	*stderr = tmp_stderr;
# endif /* FJE */
#else /* !AMIGA */
	freopen( device, "r", stdin );
#endif /* !AMIGA */
}
