

/* This program bridges two (fossil) comm ports together. */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <dos.h>

#include "modem.h"     /* Routines for accessing the modem(s). */

#define ERRORLEVEL_SUCCESS       0
#define ERRORLEVEL_BAD_PARAMETER 1
#define ERRORLEVEL_PROBLEM       2

int incoming_modem_port,outgoing_modem_port,  /* For holding command lines parameters. */
    temp_int;

char lf=10,cr=13,print_to_screen_flag=0,temp_char;
char CRLF_string[3]={13,10,0},CR_string[2]={13,0};


/*------------------------------ The Program! --------------------------------*/
int main(int argc,char *argv[])
{
if (argc != 3)
  {
  printf("?Illegal number of arguments; command line syntax: \n");
  printf("  com-link p1 p2                                   \n");
  printf(" where p1 is the incoming (bbs) comm port number   \n");
  printf("   and p2 is the outgoing (second) comm port.      \n");
  return(ERRORLEVEL_BAD_PARAMETER);
  }
incoming_modem_port = atoi(argv[1]);  /* Get program arguments. */
outgoing_modem_port = atoi(argv[2]);

printf("Incoming comm. port is %d; outgoing is %d.\n",incoming_modem_port,outgoing_modem_port);
printf("Ctrl-A to end; Ctrl-B to toggle screen logging....\n");
printf("Screen logging is now OFF.\n");

/* Open both ports. */
if (fossil_init(incoming_modem_port))
  {
  printf("?Couldn't initialize first comm port!\n");
  exit(ERRORLEVEL_BAD_PARAMETER);
  }
if (fossil_init(outgoing_modem_port))
  {
  printf("?Couldn't initialize second comm port!\n");
  fossil_close(incoming_modem_port,0);
  exit(ERRORLEVEL_BAD_PARAMETER);
  }

send_to_modem(incoming_modem_port,CRLF_string);
send_to_modem(incoming_modem_port,"Wait a second while I initialize the modem.");
send_to_modem(incoming_modem_port,CRLF_string);
send_to_modem(outgoing_modem_port,CR_string);
wait(1);
send_to_modem(outgoing_modem_port,CR_string);
wait(1);
send_to_modem(outgoing_modem_port,"ATZ");
send_to_modem(outgoing_modem_port,CR_string);
wait(1);
send_to_modem(outgoing_modem_port,"ATTE1");
send_to_modem(outgoing_modem_port,CR_string);
send_to_modem(incoming_modem_port,"You are now connected to the modem.  Hang up to quit.");
send_to_modem(incoming_modem_port,CRLF_string);
send_to_modem(incoming_modem_port,CRLF_string);

the_loop:
if (kbhit())
  {
  temp_char = getch();
  if (temp_char == 1)  /* Ctrl-A (quit command) received? */
    goto time_to_exit;
  if (temp_char == 2)  /* Ctrl-B (toggle screen logging) received? */
    {
    if (print_to_screen_flag)
      {
      print_to_screen_flag = 0;
      printf("\nScreen logging is now OFF.\n");
      }
    else
      {
      print_to_screen_flag = 1;
      printf("\nScreen logging is now ON.\n");
      }
    }
  else
    fossil_write(outgoing_modem_port,temp_char);
  }

if (fossil_available(incoming_modem_port))
  fossil_write(outgoing_modem_port,(char)fossil_receive(incoming_modem_port));

if (fossil_available(outgoing_modem_port))
  {
  temp_int = fossil_receive(outgoing_modem_port);
  fossil_write(incoming_modem_port,(char)temp_int);
  if (print_to_screen_flag)
    printf("%c",(char)temp_int);
  }

/* Check to see if the person hung up. */

/*
if (!fossil_cd(incoming_modem_port))
  {
  printf("Lost CD!\n");
  goto problem_occurred;
  }
*/

goto the_loop;

time_to_exit:
fossil_close(incoming_modem_port,0); /* Don't drop the DTR on this modem. */
fossil_close(outgoing_modem_port,1);   /* The 1 drops the DTR too. */
return(ERRORLEVEL_SUCCESS);

problem_occurred:
fossil_close(incoming_modem_port,0);
fossil_close(outgoing_modem_port,1);
return(ERRORLEVEL_PROBLEM);


}



