home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / PCL4C30.ZIP / BBS.C < prev    next >
Text File  |  1992-01-20  |  7KB  |  260 lines

  1. /*
  2. **                    ---  bbs.c ---
  3. **
  4. **   EXAMPLE CODE: This code demonstrates the use of the Personal
  5. **   Communications Library in the design of a single user bulletin
  6. **   board system.
  7. **
  8. **   BBS.C can transfer files using Ascii, XMODEM, and YMODEM (BATCH)
  9. **   protocols.
  10. **
  11. **   NOTE: must link with BBS_IO, MODEM_IO, BBSFILE, XYMODEM, XYPACKET,
  12. **   CRC, DOS, DIR, and library PCL4C.LIB. See make files.
  13. **
  14. **   This example program (not the PCL4C library) is donated to
  15. **   the Public Domain by MarshallSoft Computing, Inc. It is
  16. **   provided as an example of the use of the PCL4C.
  17. **
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <sys\types.h>
  23. #include <sys\stat.h>
  24.  
  25. #include "pcl4c.h"
  26. #include "ascii.h"
  27. #include "bbs.h"
  28.  
  29. /*  Set HAYES to 1 on the following line if you are using  **
  30. **  a Hayes compatible modem                               */
  31.  
  32. #define HAYES 1
  33.  
  34. int EchoFlag = TRUE;
  35. int TransferFlag = FALSE;
  36.  
  37. int Port;               /* current COM port [0..3] */
  38. char Filename[15];      /* file name buffer */
  39. char Buffer[1024];      /* block buffer */
  40. char RxBuf[1024];       /* PCL4C receive buffer */
  41. char *BaudRate[10] = {"300","600","1200","2400","4800","9600",
  42.                       "19200","38400","57600","115200"};
  43. char Directory[65];     /* current directory */
  44. char UserName[40];      /* caller's user name */
  45. char PassName[20];      /* caller's password */
  46. int AsciiFlag = TRUE;
  47. int CRCflag   = TRUE;
  48. int OneKflag  = FALSE;
  49. int BatchFlag = FALSE;
  50.  
  51. main(argc,argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.  char c;
  56.  int i, k, n;
  57.  int Code;          /* PCL4C return code */
  58.  int Delta;         /* delta port status */
  59.  int Status;        /* port status */
  60.  int BaudCode;      /* baud rate code ( index into BaudRate[] )
  61.  /* get port & baud rate from command line */
  62.  if(argc!=3)
  63.      {printf("Usage: 'BBS port baud' -- example 'BBS 1 9600'\n");
  64.       exit(1);
  65.      }
  66.  Port = atoi(argv[1]) - 1;
  67.  if((Port<0) || (Port>3))
  68.      {printf("Port must be 1 to 4\n");
  69.       exit(1);
  70.      }
  71.  BaudCode = BaudMatch(argv[2]);
  72.  if(BaudCode<0)
  73.      {printf("Cannot recognize baud rate = %s\n",argv[2]);
  74.       exit(1);
  75.      }
  76.  /* setup receive buffer */
  77.  Code = SioRxBuf(Port,RxBuf,Size1024);
  78.  if(Code<0)
  79.      {SioError(Code);
  80.       exit(1);
  81.      }
  82.  /* set parms & reset (initialize) COM port */
  83.  SioParms(Port,NoParity,OneStopBit,WordLength8);
  84.  MyStart(Port,BaudCode);
  85.  /* init CRC table */
  86.  InitCRC();
  87.  /* set DTR and RTS */
  88.  SioDTR(Port,'S');
  89.  SioRTS(Port,'S');
  90.  /* initialize screen */
  91.  Scroll(0,0,23,79,0,NORMAL);
  92.  Position(0,0);
  93.  printf("BBS 1.1: COM%d %s baud. Type Ctrl-BREAK to exit.\n",
  94.        1+Port,BaudRate[BaudCode]);
  95.  Directory[0] = '\\';
  96.  CurrentDir(&Directory[1]);
  97.  printf("Current directory = '%s'\n", Directory);
  98.  
  99. #if HAYES
  100.  /* set DTR and RTS */
  101.  SioDTR(Port,'S');
  102.  SioRTS(Port,'S');
  103.  /* wait for Modem to say its ready */
  104.  printf("Waiting for Modem DSR.");
  105.  while( !SioDSR(Port) )
  106.      {
  107.       if(SioKeyPress()||SioBrkKey()) MyExit(0,"Aborted by user");
  108.       putchar('.');
  109.       SioDelay(18);
  110.      }
  111.  putchar('\n');
  112.  /* initialize (Hayes compatible) modem */
  113.  SendTo(Port,"!AT E1 S7=60 S11=60 V1 X1 Q0 S0=1!");
  114.  if(!WaitFor(Port,"OK")) MyExit(0,"Expecting OK");
  115. #endif
  116.  
  117.  printf("\nMODEM READY\n");
  118.  /* wait for next caller */
  119.  SioRxFlush(Port);
  120.  while(1)
  121.      {TestBreak();
  122.  
  123. #if HAYES
  124.       /* wait for RING */
  125.       while(1)
  126.         {TestBreak();
  127.          if(SioRI(Port))
  128.            {SioDelay(SECONDS/2);
  129.             if (WaitFor(Port,"RING")) break;
  130.             else MyExit(0,"RING not received");
  131.            }
  132.         } /* end while */
  133.       /* wait for CONNECT */
  134.       while(1)
  135.         {TestBreak();
  136.          if(SioDCD(Port))
  137.            {SioDelay(SECONDS/2);
  138.             if (WaitFor(Port,"CONNECT")) break;
  139.             else MyExit(0,"CONNECT nor received ");
  140.            }
  141.         } /* end while */
  142.       /* flush past any remaining chars */
  143.       SioDelay(SECONDS/4);
  144.       SioRxFlush(Port);
  145. #endif
  146.  
  147.       printf("Waiting for incoming character\n");
  148.       /* wait for incoming character */
  149.       while(1)
  150.          {/* Control-BREAK ? */
  151.           TestBreak();
  152.           /* any incoming over serial port ? */
  153.           i = SioGetc(Port,0);
  154.           if(i>-1)
  155.              {/* disregard 0 */
  156.               if((char)i=='\0') continue;
  157.               /* start session */
  158.               Session(Port);
  159.               SioRxFlush(Port);
  160.              } /* end if */
  161.          /* any change in DCD or DSR ? */
  162.          Delta = SioModem(Port,DeltaDCD|DeltaDSR);
  163.          if(Delta)
  164.              {/* display new status */
  165.               Status = SioModem(Port,DCD|DSR);
  166.               if(!Delta&DeltaDSR) MyExit(0,"Dropped DSR");
  167.               if(!Delta&DeltaDCD)
  168.                  {printf("\nDropped Data Carrier\n");
  169.                   break;
  170.                  }
  171.              } /* end if */
  172.         } /* end while */
  173.    } /* end while */
  174. }
  175.  
  176. /*** start up processing ***/
  177.  
  178. int MyStart(Port,BaudCode)
  179. int Port;
  180. int BaudCode;
  181. {int i, Code;
  182.  /* try up to 3 times to reset COM port */
  183.  for(i=0;i<3;i++)
  184.      {printf("Resetting COM%d at %s baud\n",Port+1,BaudRate[BaudCode]);
  185.       if( (Code = SioReset(Port,BaudCode))==0) return;
  186.       if(Code<0) MyExit(Code,"Error reseting port");
  187.       SioDone(Port);
  188.       /* display errors */
  189.       if(Code&OverrunError) puts("Overrun Error");
  190.       if(Code&ParityError)  puts("Parity Error");
  191.       if(Code&FramingError) puts("Framing Error");
  192.       if(Code&BreakDetect)  puts("Break Detect");
  193.      }
  194.  exit(1);
  195. }
  196.  
  197. /*** find baud rate match ***/
  198.  
  199. int BaudMatch(ptr)
  200. char *ptr;
  201. {int i;
  202.  /* find baud rate in table */
  203.  for(i=0;i<10;i++) if(strcmp(BaudRate[i],ptr)==0) return(i);
  204.  /* baud rate string not in table ! */
  205.  return(-1);
  206. }
  207.  
  208. /*** exit program ***/
  209.  
  210. int MyExit(code,msg)
  211. int code;
  212. char *msg;
  213. {int Code;
  214.  if(code<0) SioError(code);
  215.  /* Assert UART break & sign off */
  216.  SioBrkSig(Port,'A');
  217.  printf("TERMINATING: %s\n",msg);
  218.  SioDelay(8);
  219.  SioDone(Port);
  220.  exit(0);
  221. }
  222.  
  223. /*** process user's session from logon to logoff ***/
  224.  
  225. int Session(Port)
  226. int Port;
  227. {int i;
  228.  int Code;
  229.  int ErrorCount = 0;
  230.  SioRxFlush(Port);
  231.  EchoFlag = TRUE;
  232.  PutCRLF(Port);
  233.  PutString(Port,"BBS program");
  234.  /* get username */
  235.  PutString(Port,"Logon: ");
  236.  GetString(Port,UserName,40);
  237.  /* get password */
  238.  EchoFlag = FALSE;
  239.  PutString(Port,"Password: ");
  240.  GetString(Port,PassName,20);
  241.  EchoFlag = TRUE;
  242.  puts("logon OK");
  243.  /* OK */
  244.  while(1)
  245.    {i = TopMenu(Port);
  246.     switch(i)
  247.        {case 0: /* EXIT */
  248.                 PutString(Port,"Logging off");
  249.                 PutCRLF(Port);
  250.                 return;
  251.         case 1: /* Files */
  252.                 while(1)
  253.                    {i = FileMenu(Port);
  254.                     if(i==0) break;
  255.                     FileSystem(Port,i);
  256.                    }
  257.                 break;
  258.        } /* end switch */
  259.    } /* end while */
  260. } /* end Session */