Simplified ttyio driver
This commit is contained in:
parent
fbe265e949
commit
19f28d4c83
@ -133,7 +133,7 @@ userlist.o: ../config.h ../lib/mbselib.h ../lib/mbse.h ../lib/users.h userlist.h
|
||||
timestats.o: ../config.h ../lib/mbselib.h ../lib/mbse.h ../lib/users.h timestats.h funcs.h language.h input.h exitinfo.h term.h
|
||||
logentry.o: ../config.h ../lib/mbselib.h ../lib/mbse.h ../lib/users.h logentry.h
|
||||
ymsend.o: ../config.h ../lib/mbselib.h ../lib/mbse.h ttyio.h zmmisc.h transfer.h openport.h timeout.h term.h ymsend.h
|
||||
ymrecv.o: ../config.h ../lib/mbselib.h ../lib/mbse.h ttyio.h zmmisc.h ymrecv.h
|
||||
ymrecv.o: ../config.h ../lib/mbselib.h ../lib/mbse.h ttyio.h timeout.h zmmisc.h zmrecv.h ymrecv.h
|
||||
zmmisc.o: ../config.h ../lib/mbselib.h ttyio.h input.h zmmisc.h
|
||||
zmsend.o: ../config.h ../lib/mbselib.h ttyio.h zmmisc.h transfer.h openport.h timeout.h
|
||||
zmrecv.o: ../config.h ../lib/mbselib.h ../lib/users.h ttyio.h transfer.h zmmisc.h zmrecv.h ymrecv.h openport.h timeout.h input.h
|
||||
|
370
mbsebbs/ttyio.c
370
mbsebbs/ttyio.c
@ -34,20 +34,16 @@
|
||||
|
||||
|
||||
extern int hanged_up;
|
||||
extern char *inetaddr;
|
||||
|
||||
#define TT_BUFSIZ 1024
|
||||
#define NUMTIMERS 3
|
||||
|
||||
|
||||
int tty_status = 0;
|
||||
int f_flags;
|
||||
static char buffer[TT_BUFSIZ];
|
||||
static char *next;
|
||||
static int left = 0;
|
||||
|
||||
|
||||
static time_t timer[NUMTIMERS];
|
||||
|
||||
char *ttystat[]= {(char *)"Ok",
|
||||
(char *)"Error",
|
||||
@ -57,100 +53,6 @@ char *ttystat[]= {(char *)"Ok",
|
||||
(char *)"Empty",
|
||||
(char *)"UnCompress"};
|
||||
|
||||
int tty_resettimer(int tno);
|
||||
void tty_resettimers(void);
|
||||
int tty_settimer(int,int);
|
||||
int tty_expired(int);
|
||||
int tty_running(int);
|
||||
|
||||
#define RESETTIMER(x) tty_resettimer(x)
|
||||
#define RESETTIMERS() tty_resettimers()
|
||||
#define SETTIMER(x,y) tty_settimer(x,y)
|
||||
#define EXPIRED(x) tty_expired(x)
|
||||
#define RUNNING(x) tty_running(x)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* timer functions
|
||||
*/
|
||||
int tty_resettimer(int tno)
|
||||
{
|
||||
if (tno >= NUMTIMERS) {
|
||||
errno = EINVAL;
|
||||
WriteError("ttyio: invalid timer No for resettimer(%d)", tno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
timer[tno] = (time_t) 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void tty_resettimers(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUMTIMERS; i++)
|
||||
timer[i] = (time_t)0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_settimer(int tno, int interval)
|
||||
{
|
||||
if (tno >= NUMTIMERS) {
|
||||
errno = EINVAL;
|
||||
WriteError("ttyio: invalid timer No for settimer(%d)", tno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
timer[tno]=time((time_t*)NULL)+interval;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_expired(int tno)
|
||||
{
|
||||
time_t now;
|
||||
|
||||
if (tno >= NUMTIMERS) {
|
||||
errno = EINVAL;
|
||||
WriteError("ttyio: invalid timer No for expired(%d)", tno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if timer is running
|
||||
*/
|
||||
if (timer[tno] == (time_t) 0)
|
||||
return 0;
|
||||
|
||||
now = time(NULL);
|
||||
return (now >= timer[tno]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_running(int tno)
|
||||
{
|
||||
if (tno > NUMTIMERS) {
|
||||
errno = EINVAL;
|
||||
WriteError("ttyio: invalid timer for tty_running(%d)", tno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* check if timer is running
|
||||
*/
|
||||
if (timer[tno] == (time_t) 0)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@ -158,8 +60,8 @@ int tty_running(int tno)
|
||||
*/
|
||||
static int tty_read(char *buf, int size, int tot)
|
||||
{
|
||||
time_t timeout, now;
|
||||
int i, rc;
|
||||
time_t now;
|
||||
int rc;
|
||||
fd_set readfds, writefds, exceptfds;
|
||||
struct timeval seltimer;
|
||||
|
||||
@ -168,29 +70,13 @@ static int tty_read(char *buf, int size, int tot)
|
||||
tty_status = 0;
|
||||
|
||||
now = time(NULL);
|
||||
timeout = (time_t)300; /* maximum of 5 minutes */
|
||||
|
||||
for (i = 0; i < NUMTIMERS; i++) {
|
||||
if (timer[i]) {
|
||||
if (now >= timer[i]) {
|
||||
tty_status=STAT_TIMEOUT;
|
||||
Syslog('!', "tty_read: timer %d already expired, return", i);
|
||||
return -tty_status;
|
||||
} else {
|
||||
if (timeout > (timer[i]-now))
|
||||
timeout=timer[i]-now;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((tot != -1) && (timeout > tot))
|
||||
timeout=tot;
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&writefds);
|
||||
FD_ZERO(&exceptfds);
|
||||
FD_SET(0,&readfds);
|
||||
FD_SET(0,&exceptfds);
|
||||
seltimer.tv_sec=timeout;
|
||||
seltimer.tv_sec=tot;
|
||||
seltimer.tv_usec=0;
|
||||
|
||||
rc = select(1,&readfds,&writefds,&exceptfds,&seltimer);
|
||||
@ -289,117 +175,6 @@ int tty_check(void)
|
||||
|
||||
|
||||
|
||||
int tty_putcheck(int size)
|
||||
{
|
||||
fd_set set;
|
||||
struct timeval timeout;
|
||||
|
||||
/*
|
||||
* Initialize the file descriptor set.
|
||||
*/
|
||||
FD_ZERO(&set);
|
||||
FD_SET(1, &set);
|
||||
|
||||
/*
|
||||
* Initialize the timeout data structure.
|
||||
*/
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
/*
|
||||
* `select' returns 0 if timeout, 1 if input available, -1 if error.
|
||||
*/
|
||||
return select(FD_SETSIZE, NULL, &set, NULL, &timeout);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_waitputget(int tot)
|
||||
{
|
||||
int i, rc;
|
||||
time_t timeout, now;
|
||||
fd_set readfds, writefds, exceptfds;
|
||||
struct timeval seltimer;
|
||||
|
||||
tty_status=0;
|
||||
now = time(NULL);
|
||||
timeout=(time_t)300; /* maximum of 5 minutes */
|
||||
|
||||
for (i = 0; i < NUMTIMERS; i++) {
|
||||
if (timer[i]) {
|
||||
if (now >= timer[i]) {
|
||||
tty_status = STAT_TIMEOUT;
|
||||
WriteError("tty_waitputget: timer %d already expired, return",i);
|
||||
return -tty_status;
|
||||
} else {
|
||||
if (timeout > (timer[i]-now))
|
||||
timeout = timer[i]-now;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((tot != -1) && (timeout > tot))
|
||||
timeout=tot;
|
||||
Syslog('t', "tty_waitputget: timeout=%d",timeout);
|
||||
|
||||
/*
|
||||
* Initialize the file descriptor set.
|
||||
*/
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&writefds);
|
||||
FD_ZERO(&exceptfds);
|
||||
|
||||
FD_SET(0, &readfds);
|
||||
FD_SET(1, &writefds);
|
||||
FD_SET(0, &exceptfds);
|
||||
FD_SET(1, &exceptfds);
|
||||
|
||||
/*
|
||||
* Initialize the timeout data structure.
|
||||
*/
|
||||
seltimer.tv_sec = timeout;
|
||||
seltimer.tv_usec = 0;
|
||||
|
||||
/*
|
||||
* `select' returns 0 if timeout, 1 if input available, -1 if error.
|
||||
*/
|
||||
rc = select(FD_SETSIZE, &readfds, &writefds, &exceptfds, &seltimer);
|
||||
|
||||
if (rc < 0) {
|
||||
if (hanged_up) {
|
||||
tty_status=STAT_HANGUP;
|
||||
WriteError("tty_waitputget: hanged_up flag");
|
||||
} else {
|
||||
WriteError("$tty_waitputget: select failed");
|
||||
tty_status=STAT_ERROR;
|
||||
}
|
||||
} else if (rc == 0) {
|
||||
tty_status=STAT_TIMEOUT;
|
||||
} else {
|
||||
/* rc > 0 */
|
||||
if ((FD_ISSET(0,&exceptfds)) || (FD_ISSET(1,&exceptfds))) {
|
||||
WriteError("$tty_waitputget: exeption error");
|
||||
tty_status=STAT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (tty_status) {
|
||||
Syslog('t', "tty_waitputget: return after select status %s",ttystat[tty_status]);
|
||||
return -tty_status;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
|
||||
if (FD_ISSET(0,&readfds))
|
||||
rc |= 1;
|
||||
|
||||
if (FD_ISSET(1,&writefds))
|
||||
rc |= 2;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Discard all available input characters
|
||||
*/
|
||||
@ -420,26 +195,6 @@ void tty_flushout(void)
|
||||
|
||||
|
||||
|
||||
int tty_ungetc(int c)
|
||||
{
|
||||
if (next == buffer) {
|
||||
if (left >= TT_BUFSIZ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
next = buffer + TT_BUFSIZ - left;
|
||||
memcpy(next, buffer, left);
|
||||
}
|
||||
|
||||
next--;
|
||||
*next = c;
|
||||
left++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_getc(int tot)
|
||||
{
|
||||
if (!left) {
|
||||
@ -458,35 +213,6 @@ int tty_getc(int tot)
|
||||
|
||||
|
||||
|
||||
int tty_get(char *buf, int size, int tot)
|
||||
{
|
||||
int result=0;
|
||||
|
||||
if (left >= size) {
|
||||
memcpy(buf,next,size);
|
||||
next += size;
|
||||
left -= size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (left > 0) {
|
||||
memcpy(buf,next,left);
|
||||
buf += left;
|
||||
next += left;
|
||||
size -= left;
|
||||
left=0;
|
||||
}
|
||||
|
||||
while ((result=tty_read(buf,size,tot)) > 0) {
|
||||
buf += result;
|
||||
size -= result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_putc(int c)
|
||||
{
|
||||
char buf = c;
|
||||
@ -502,93 +228,3 @@ int tty_put(char *buf, int size)
|
||||
}
|
||||
|
||||
|
||||
|
||||
int tty_putget(char **obuf, int *osize, char **ibuf, int *isize)
|
||||
{
|
||||
time_t timeout, now;
|
||||
int i, rc;
|
||||
fd_set readfds, writefds, exceptfds;
|
||||
struct timeval seltimer;
|
||||
|
||||
tty_status = 0;
|
||||
now = time(NULL);
|
||||
timeout = (time_t)300; /* maximum of 5 minutes */
|
||||
|
||||
for (i = 0; i < NUMTIMERS; i++) {
|
||||
if (timer[i]) {
|
||||
if (now >= timer[i]) {
|
||||
tty_status = STAT_TIMEOUT;
|
||||
WriteError("tty_putget: timer %d already expired, return",i);
|
||||
return -tty_status;
|
||||
} else {
|
||||
if (timeout > (timer[i]-now))
|
||||
timeout=timer[i]-now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Syslog('t', "tty_putget: timeout=%d",timeout);
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&writefds);
|
||||
FD_ZERO(&exceptfds);
|
||||
FD_SET(0,&readfds);
|
||||
FD_SET(1,&writefds);
|
||||
FD_SET(0,&exceptfds);
|
||||
FD_SET(1,&exceptfds);
|
||||
seltimer.tv_sec=timeout;
|
||||
seltimer.tv_usec=0;
|
||||
|
||||
rc=select(2,&readfds,&writefds,&exceptfds,&seltimer);
|
||||
if (rc < 0) {
|
||||
if (hanged_up) {
|
||||
tty_status=STAT_HANGUP;
|
||||
WriteError("tty_putget: hanged_up flag");
|
||||
} else {
|
||||
WriteError("$tty_putget: select failed");
|
||||
tty_status=STAT_ERROR;
|
||||
}
|
||||
} else if (rc == 0) {
|
||||
tty_status=STAT_TIMEOUT;
|
||||
} else {
|
||||
/* rc > 0 */
|
||||
if ((FD_ISSET(0,&exceptfds)) || (FD_ISSET(1,&exceptfds))) {
|
||||
WriteError("$tty_putget: exeption error");
|
||||
tty_status=STAT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (tty_status) {
|
||||
Syslog('t', "tty_putget: return after select status %s",ttystat[tty_status]);
|
||||
return -tty_status;
|
||||
}
|
||||
|
||||
if (FD_ISSET(0,&readfds) && *isize) {
|
||||
rc = read(0, *ibuf, *isize);
|
||||
if (rc < 0) {
|
||||
WriteError("$tty_putget: read failed");
|
||||
tty_status=STAT_ERROR;
|
||||
} else {
|
||||
(*ibuf)+=rc;
|
||||
(*isize)-=rc;
|
||||
}
|
||||
}
|
||||
|
||||
if (FD_ISSET(1,&writefds) && *osize) {
|
||||
rc=write(1, *obuf, *osize);
|
||||
if (rc < 0) {
|
||||
WriteError("$tty_putget: write failed");
|
||||
tty_status=STAT_ERROR;
|
||||
} else {
|
||||
(*obuf)+=rc;
|
||||
(*osize)-=rc;
|
||||
}
|
||||
}
|
||||
|
||||
if (tty_status)
|
||||
return -tty_status;
|
||||
else
|
||||
return ((*isize == 0) | ((*osize == 0) << 1));
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,28 +3,13 @@
|
||||
#ifndef TTYIO_H
|
||||
#define TTYIO_H
|
||||
|
||||
#define TIMERNO_BRAIN 0 /* BRAIN timerno */
|
||||
#define TIMERNO_RX 1 /* Receiver timerno */
|
||||
#define TIMERNO_TX 2 /* Transmitter timerno */
|
||||
|
||||
#define RESETTIMER(x) tty_resettimer(x)
|
||||
#define RESETTIMERS() tty_resettimers()
|
||||
#define SETTIMER(x,y) tty_settimer(x,y)
|
||||
#define EXPIRED(x) tty_expired(x)
|
||||
#define RUNNING(x) tty_running(x)
|
||||
|
||||
#define TCHECK() tty_check()
|
||||
#define PUTCHECK(x) tty_putcheck(x)
|
||||
#define WAITPUTGET(x) tty_waitputget(x)
|
||||
#define FLUSHOUT() tty_flushout()
|
||||
#define FLUSHIN() tty_flushin()
|
||||
#define PUTCHAR(x) tty_putc(x)
|
||||
#define PUT(x,y) tty_put(x,y)
|
||||
#define PUTSTR(x) tty_put(x,strlen(x))
|
||||
#define GETCHAR(x) tty_getc(x)
|
||||
#define UNGETCHAR(x) tty_ungetc(x)
|
||||
#define GET(x,y,z) tty_get(x,y,z)
|
||||
#define PUTGET(a,b,x,y) tty_putget(a,b,x,y)
|
||||
#define STATUS tty_status
|
||||
|
||||
#define STAT_SUCCESS 0
|
||||
@ -94,22 +79,11 @@
|
||||
|
||||
extern int tty_status;
|
||||
|
||||
extern int tty_resettimer(int tno);
|
||||
extern void tty_resettimers(void);
|
||||
extern int tty_settimer(int,int);
|
||||
extern int tty_expired(int);
|
||||
extern int tty_running(int);
|
||||
extern int tty_check(void);
|
||||
extern int tty_waitputget(int);
|
||||
extern int tty_ungetc(int);
|
||||
extern int tty_getc(int);
|
||||
extern int tty_get(char*,int,int);
|
||||
extern int tty_putcheck(int);
|
||||
extern int tty_putc(int);
|
||||
extern int tty_put(char*,int);
|
||||
extern int tty_putget(char**,int*,char**,int*);
|
||||
extern void tty_flushout(void);
|
||||
extern void tty_flushin(void);
|
||||
extern void sendbrk(void);
|
||||
|
||||
#endif
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "../lib/mbselib.h"
|
||||
#include "../lib/mbse.h"
|
||||
#include "ttyio.h"
|
||||
#include "timeout.h"
|
||||
#include "zmmisc.h"
|
||||
#include "zmrecv.h"
|
||||
#include "ymrecv.h"
|
||||
@ -64,7 +65,7 @@ int wcrxpn(char *rpn, int want1k)
|
||||
register int c;
|
||||
size_t Blklen = 0; /* record length of received packets */
|
||||
|
||||
Crcflg = want1k
|
||||
Crcflg = want1k;
|
||||
purgeline(0);
|
||||
Syslog('x', "%s: wcrxpn() crc=%s", protname(), Crcflg ? "true":"false");
|
||||
|
||||
|
@ -213,7 +213,6 @@ static int wctxpn(char *fname)
|
||||
if ((input_f != stdin) && *fname)
|
||||
sprintf(p, "%lu %lo %o 0 %d %ld", (long) f.st_size, f.st_mtime,
|
||||
(unsigned int)((no_unixmode) ? 0 : f.st_mode), Filesleft, Totalleft);
|
||||
Syslog('x', "Sending: %s", txbuf);
|
||||
|
||||
Totalleft -= f.st_size;
|
||||
if (--Filesleft <= 0)
|
||||
@ -221,8 +220,6 @@ static int wctxpn(char *fname)
|
||||
if (Totalleft < 0)
|
||||
Totalleft = 0;
|
||||
|
||||
Syslog('x', "Totalleft = %d", Totalleft);
|
||||
|
||||
/* force 1k blocks if name won't fit in 128 byte block */
|
||||
if (txbuf[125])
|
||||
blklen=1024;
|
||||
@ -307,10 +304,11 @@ static int wctx(long bytes_total)
|
||||
register size_t thisblklen;
|
||||
register int sectnum, attempts, firstch;
|
||||
|
||||
firstsec=TRUE; thisblklen = blklen;
|
||||
firstsec=TRUE;
|
||||
thisblklen = blklen;
|
||||
Syslog('x', "wctx: file length=%ld, blklen=%d", bytes_total, blklen);
|
||||
|
||||
while ((firstch = GETCHAR(Rxtimeout))!=NAK && firstch != WANTCRC
|
||||
while ((firstch = GETCHAR(Rxtimeout)) != NAK && firstch != WANTCRC
|
||||
&& firstch != WANTG && firstch != TIMEOUT && firstch != CAN);
|
||||
if (firstch == CAN) {
|
||||
Syslog('x', "Receiver Cancelled");
|
||||
|
Reference in New Issue
Block a user