/*		ulib.c


		Amiga library
		

	Things to do in uu host

		serial I/O
		
		directory stuff
			opendir, readdir, closedir

		prolog and epilog

		system call

*/

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

/**/
/*
 *
 *      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("Username:", 0);
	rmsg(logmsg, 0);
	printmsg( 0, "Username = %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;

	if (debuglevel > 14)
		fputc( '{', stderr );
	if (debuglevel > 14) {
		test = num;
		cp = data;
		while (test--)
			fprintf( stderr, isprint(*cp)? "{%c}":"{%02x}", *cp++ );
	}
	test = SIOWrite( data, num );
	if (debuglevel > 14)
		fputc( '}', stderr );
	return( test );

}

/* non-blocking read essential to "g" protocol */
/* see "dcpgpkt.c" for description */
/* This all changes in a mtask systems. Requests for */
/* I/O should get qued 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;
{
/*
	return( SIORead( buf, num, num, timeout*10 ) );
*/
	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 = 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 );
}


nodot(string)
{
}



notimp( argc, argv )
char *argv[];
{
	/*debuglevelMsg("\Pcheck argc (08) and argv (0a) ");*/
	fprintf( stderr, "shell: %s not implemented\n", *argv );
}

/*------------------------------------------------------------------*/
/*	RNews:	my private rnews!				    */
/*------------------------------------------------------------------*/
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", 't' )) != 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;
	   }
	
	if ( (f = FOPEN( filename, "w", 't' )) == (FILE *)NULL ) {
		fprintf( stderr, "rnews: can't open %s\n", filename );
		return;
	}

	if ( (fin = FOPEN( inname, "r", 't' )) == NULL )
	  {
		fprintf( stderr, "rnews: Couldn't open %s\n", inname );
		fclose( f );
		return;
	  }
	
	while ( fgets( buf, BUFSIZ, fin ) != (char *)NULL ) 
		fputs( buf, f );

	fclose( f );
	fclose( fin );
}



/* 		shell


*/

char * getcwd();

shell( command, inname, outname, errname )
char * command;
char * inname;
char * outname;
{

	char	* argvec[50];

	int rmail();
	int rnews();
	
	int 	argcp;

	char **argvp;
	char args;
	

	int	(*proto)();
	
	argcp = 0;

	argcp = getargs( command, argvec );

	argvp = argvec;
	args = argcp;

	if ( debuglevel > 5 ) {
		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; 
	}
		
	if ( *inname != '\0' ) {
		fprintf( stderr, "reopening stdin as %s\n", inname );
		fprintf( stderr, "curdir: %s\n", getcwd(NULL, 0)); /* */
		if ( freopen( inname, "r", stdin ) == NULL ) 
			fprintf( stderr, "Couldn't open %s, %d\n", inname, errno );
	}
	(proto)( argcp, argvp );

	if ( *inname != '\0' )
	   freopen( "*", "r", stdin );

}





