home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware 1 2 the Maxx
/
sw_1.zip
/
sw_1
/
PROGRAM
/
PCL4C30.ZIP
/
BBS.C
< prev
next >
Wrap
Text File
|
1992-01-20
|
7KB
|
260 lines
/*
** --- bbs.c ---
**
** EXAMPLE CODE: This code demonstrates the use of the Personal
** Communications Library in the design of a single user bulletin
** board system.
**
** BBS.C can transfer files using Ascii, XMODEM, and YMODEM (BATCH)
** protocols.
**
** NOTE: must link with BBS_IO, MODEM_IO, BBSFILE, XYMODEM, XYPACKET,
** CRC, DOS, DIR, and library PCL4C.LIB. See make files.
**
** This example program (not the PCL4C library) is donated to
** the Public Domain by MarshallSoft Computing, Inc. It is
** provided as an example of the use of the PCL4C.
**
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include "pcl4c.h"
#include "ascii.h"
#include "bbs.h"
/* Set HAYES to 1 on the following line if you are using **
** a Hayes compatible modem */
#define HAYES 1
int EchoFlag = TRUE;
int TransferFlag = FALSE;
int Port; /* current COM port [0..3] */
char Filename[15]; /* file name buffer */
char Buffer[1024]; /* block buffer */
char RxBuf[1024]; /* PCL4C receive buffer */
char *BaudRate[10] = {"300","600","1200","2400","4800","9600",
"19200","38400","57600","115200"};
char Directory[65]; /* current directory */
char UserName[40]; /* caller's user name */
char PassName[20]; /* caller's password */
int AsciiFlag = TRUE;
int CRCflag = TRUE;
int OneKflag = FALSE;
int BatchFlag = FALSE;
main(argc,argv)
int argc;
char *argv[];
{
char c;
int i, k, n;
int Code; /* PCL4C return code */
int Delta; /* delta port status */
int Status; /* port status */
int BaudCode; /* baud rate code ( index into BaudRate[] )
/* get port & baud rate from command line */
if(argc!=3)
{printf("Usage: 'BBS port baud' -- example 'BBS 1 9600'\n");
exit(1);
}
Port = atoi(argv[1]) - 1;
if((Port<0) || (Port>3))
{printf("Port must be 1 to 4\n");
exit(1);
}
BaudCode = BaudMatch(argv[2]);
if(BaudCode<0)
{printf("Cannot recognize baud rate = %s\n",argv[2]);
exit(1);
}
/* setup receive buffer */
Code = SioRxBuf(Port,RxBuf,Size1024);
if(Code<0)
{SioError(Code);
exit(1);
}
/* set parms & reset (initialize) COM port */
SioParms(Port,NoParity,OneStopBit,WordLength8);
MyStart(Port,BaudCode);
/* init CRC table */
InitCRC();
/* set DTR and RTS */
SioDTR(Port,'S');
SioRTS(Port,'S');
/* initialize screen */
Scroll(0,0,23,79,0,NORMAL);
Position(0,0);
printf("BBS 1.1: COM%d %s baud. Type Ctrl-BREAK to exit.\n",
1+Port,BaudRate[BaudCode]);
Directory[0] = '\\';
CurrentDir(&Directory[1]);
printf("Current directory = '%s'\n", Directory);
#if HAYES
/* set DTR and RTS */
SioDTR(Port,'S');
SioRTS(Port,'S');
/* wait for Modem to say its ready */
printf("Waiting for Modem DSR.");
while( !SioDSR(Port) )
{
if(SioKeyPress()||SioBrkKey()) MyExit(0,"Aborted by user");
putchar('.');
SioDelay(18);
}
putchar('\n');
/* initialize (Hayes compatible) modem */
SendTo(Port,"!AT E1 S7=60 S11=60 V1 X1 Q0 S0=1!");
if(!WaitFor(Port,"OK")) MyExit(0,"Expecting OK");
#endif
printf("\nMODEM READY\n");
/* wait for next caller */
SioRxFlush(Port);
while(1)
{TestBreak();
#if HAYES
/* wait for RING */
while(1)
{TestBreak();
if(SioRI(Port))
{SioDelay(SECONDS/2);
if (WaitFor(Port,"RING")) break;
else MyExit(0,"RING not received");
}
} /* end while */
/* wait for CONNECT */
while(1)
{TestBreak();
if(SioDCD(Port))
{SioDelay(SECONDS/2);
if (WaitFor(Port,"CONNECT")) break;
else MyExit(0,"CONNECT nor received ");
}
} /* end while */
/* flush past any remaining chars */
SioDelay(SECONDS/4);
SioRxFlush(Port);
#endif
printf("Waiting for incoming character\n");
/* wait for incoming character */
while(1)
{/* Control-BREAK ? */
TestBreak();
/* any incoming over serial port ? */
i = SioGetc(Port,0);
if(i>-1)
{/* disregard 0 */
if((char)i=='\0') continue;
/* start session */
Session(Port);
SioRxFlush(Port);
} /* end if */
/* any change in DCD or DSR ? */
Delta = SioModem(Port,DeltaDCD|DeltaDSR);
if(Delta)
{/* display new status */
Status = SioModem(Port,DCD|DSR);
if(!Delta&DeltaDSR) MyExit(0,"Dropped DSR");
if(!Delta&DeltaDCD)
{printf("\nDropped Data Carrier\n");
break;
}
} /* end if */
} /* end while */
} /* end while */
}
/*** start up processing ***/
int MyStart(Port,BaudCode)
int Port;
int BaudCode;
{int i, Code;
/* try up to 3 times to reset COM port */
for(i=0;i<3;i++)
{printf("Resetting COM%d at %s baud\n",Port+1,BaudRate[BaudCode]);
if( (Code = SioReset(Port,BaudCode))==0) return;
if(Code<0) MyExit(Code,"Error reseting port");
SioDone(Port);
/* display errors */
if(Code&OverrunError) puts("Overrun Error");
if(Code&ParityError) puts("Parity Error");
if(Code&FramingError) puts("Framing Error");
if(Code&BreakDetect) puts("Break Detect");
}
exit(1);
}
/*** find baud rate match ***/
int BaudMatch(ptr)
char *ptr;
{int i;
/* find baud rate in table */
for(i=0;i<10;i++) if(strcmp(BaudRate[i],ptr)==0) return(i);
/* baud rate string not in table ! */
return(-1);
}
/*** exit program ***/
int MyExit(code,msg)
int code;
char *msg;
{int Code;
if(code<0) SioError(code);
/* Assert UART break & sign off */
SioBrkSig(Port,'A');
printf("TERMINATING: %s\n",msg);
SioDelay(8);
SioDone(Port);
exit(0);
}
/*** process user's session from logon to logoff ***/
int Session(Port)
int Port;
{int i;
int Code;
int ErrorCount = 0;
SioRxFlush(Port);
EchoFlag = TRUE;
PutCRLF(Port);
PutString(Port,"BBS program");
/* get username */
PutString(Port,"Logon: ");
GetString(Port,UserName,40);
/* get password */
EchoFlag = FALSE;
PutString(Port,"Password: ");
GetString(Port,PassName,20);
EchoFlag = TRUE;
puts("logon OK");
/* OK */
while(1)
{i = TopMenu(Port);
switch(i)
{case 0: /* EXIT */
PutString(Port,"Logging off");
PutCRLF(Port);
return;
case 1: /* Files */
while(1)
{i = FileMenu(Port);
if(i==0) break;
FileSystem(Port,i);
}
break;
} /* end switch */
} /* end while */
} /* end Session */