/*
Copyright (C) Magna Carta Software, Inc. 1990. All Rights Reserved
C COMMUNICATIONS TOOLKIT
CWAITS.C -- Basic serial I/O functions (no data translation)
*/
#define CCT_DEVELOPMENT
#if (defined(CCTW) || defined(_WINDOWS))
#include <windows.h>
#endif
#include <dos.h>
#include <string.h>
#include <comm.h>
#define MAX_STRING_LEN 80 /* max. length of search strings */
/*
C_WAITS -- Wait for the specified time to receive the specified string
from the specified port. Incorrect characters received during the wait time
are ignored.
Arguments:
COMM_PORT *p -- a pointer to the port;
char *p_str -- a pointer to the desired string;
WORD duration -- the maximum time to wait for the whole string;
Return value:
0 -- success -- correct string returned;
EOF -- whole string not returned in time;
USER_CANCELLED -- abort key entered;
*/
short c_waits(COMM_PORT *p, char *p_str, WORD duration)
{
short ch, ret = 0;
WORD now;
unsigned long t=0;
char buf[MAX_STRING_LEN], *ps, *pb, *savepb;
pb = buf;
ps = p_str;
memset(buf, 0X0, MAX_STRING_LEN);
now = peek(0, LOW_TIME);
while (*ps != '\0' && !ret) {
if (now != (WORD) peek(0, LOW_TIME)) {
t += 55;
now = peek(0, LOW_TIME);
if (t >= (unsigned long) duration) ret = EOF;
}
if (!ret) {
if (pb > &buf[MAX_STRING_LEN-1]) pb = buf; /* return to start of buffer */
if (is_key(p->abort_key)) return(USER_CANCELLED);
ch = c_inchar(p);
if (ch == EOF) continue;
if (isrxecholocal(p) && ch >= 0) (*p->con_out)(p, ch);
if ((char) ch == *ps) {
ps++; /* success -- advance string ptr. */
*pb++ = (char) ch;
}
else { /* failure -- return to start */
savepb = pb;
pb = buf+1;
ps = p_str;
/*
This loop is necessary to catch the start of a correct string
that occurs after the first character in "buf".
E.g. waiting for "abracadabra" we may receive "abrabracadabra".
*/
while (pb < savepb) {
if (*pb++ != *ps) ps = p_str;
else ps++;
}
if ((char) ch == *ps) {
ps++; /* success -- advance string ptr. */
*pb++ = (char) ch;
}
}
}
}
if (p->rx_tbdelay) mspause(p->rx_tbdelay);
return (ret);
}