#include <osbind.h>

#define RS232		1
#define CONSOLE		2

static void send_ax( str )
register char	*str;
{
    while ( *str ) {
	Bconout( RS232, *str++ );
	Bconin( RS232 );
    }
}

static void conws( str )
register char	*str;
{
    while ( *str )
	Bconout( CONSOLE, *str++ );
}

static void send_aux( str )
register char	*str;
{
	send_ax( "ATDT " );
	send_ax( str );
	send_ax( "\r" );
}

static void dial_usage()
{
	conws( "usage: dial [-sh] number [number ...]\r\n\n" );
	conws( "       s = silent mode, no confirmation when connected\r\n" );
	conws( "       h = hold/wait for keypress on connection & keypress\r\n" );
	conws( "           before connection cancels program\r\n" );
	exit( -1 );
}

main( argc, argv )
int	argc;
char	*argv[];
{
    int     argptr, silent = 0, hold = 0, argbeg = 1;

    if ( argc == 1 )
	dial_usage();
    if ( argv[1][0] == '-' )
    {
	argbeg++;
	while ( *argv[1] != '\0' )
	    switch ( *argv[1]++ )
	    {
		case '-':
			break;
		case 's':
		case 'S':
			silent++;
			break;
		case 'h':
		case 'H':
			hold++;
			break;
		default:
			dial_usage();
	    }
    }
    if ( argc == argbeg )
	dial_usage();
    for (;;)
	for ( argptr = argbeg; argptr < argc; ++argptr ) {
	    register char   ch;

	    while ( Bconstat( RS232 ) )
		Bconin( RS232 );
	    send_aux( argv[argptr] );
	loop:
	    if ( hold && Bconstat( CONSOLE ) )
	    {
		Bconin( CONSOLE );
		Bconout( RS232, '\r' );
		exit( 0 );
	    }
	    ch = Bconin( RS232 );
	    if ( ch == 'T' ) {
		if ( !silent )
		{
		    conws( "\r\n\dial: connection to: " );
		    conws( argv[argptr] );
		    conws( "\r\n\007" );
		}
		if ( hold )
		    Bconin( CONSOLE );
		exit( 0 );
	    }
	    if ( ch == '>' )
		continue;
	    goto loop;
	}
}