?? chat.c
字號:
/*****************************************************************************
* CHAT.C - Communications Dialog (Chat) Code File.
*
* Copyright (c) 1996, 1998 by Global Election Systems Inc.
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice and the following disclaimer are included verbatim in any
* distributions. No written agreement, license, or royalty fee is required
* for any of the authorized uses.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
* REVISION HISTORY
*
* 98-06-16 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
* Adapted from modem.c in release 1.94p.
*****************************************************************************/
#include "typedefs.h"
#include "avconfig.h"
#include <string.h>
#include "avos.h"
#include "v25.h" // Required by butctrl.h.
#include "butctrl.h"
#include "poll.h"
#include "netbuf.h" // Required by devio.h.
#include "devio.h"
#include "stdio.h"
#include "chat.h"
#include "debug.h"
/*************************/
/*** LOCAL DEFINITIONS ***/
/*************************/
#define MAXFLUSH 1000 /* Max characters to flush before sendRecv(). */
#define RECVBUFSZ 100 /* Size of the receive buffer. */
#define MAXRESPONSE 10 /* Max response strings that sendRecv() can match. */
// Pattern matching states.
#define PMSKIPSOURCE 5
#define PMSKIPPATTERN 4
#define PMMULTIPLYING 3
#define PMTRYNEXT 2
#define PMMATCHING 1
#define PMINITIALIZE 0
#define PMSUCCESS -1
#define PMFAIL -2
/************************/
/*** LOCAL DATA TYPES ***/
/************************/
typedef struct patternContext_s {
int st; /* The pattern matching status. */
char *patStr; /* The pattern string to match. */
UINT patNdx; /* Index to the pattern string. */
UINT sourceNdx; /* Index to the source string. */
UINT matchNdx; /* Index to the source string. */
} PatternContext;
/***********************************/
/*** LOCAL FUNCTION DECLARATIONS ***/
/***********************************/
static PatternContext *copyPattern(PatternContext *srcPat, PatternContext *destPat,
int srcOffset, int matchOffset, int patOffset);
static int tryNextSource(PatternContext *respPat);
static int matchEOL(char *sourceStr, PatternContext *respPat);
static int matchCurrent(char *sourceStr, PatternContext *respPat);
static int patternMatch(char *sourceStr, PatternContext *respPat) ;
/***********************************/
/*** PUBLIC FUNCTION DEFINITIONS ***/
/***********************************/
/* Send a string to the modem and wait a limited time for one of a list of
* up to MAXRESPONSE possible responses.
* Returns: >= 0 if successful as the index of the matching response string,
* -1 if timed out, or -2 if aborted by user pressing the NO button.
*/
int sendRecv(int fd, const char *sendStr, UINT timeLimit, UINT respStrQty, ...)
{
int i, st, curChar;
int finished = FALSE;
char recvBuf[RECVBUFSZ]; /* The receive buffer and index. */
UINT recvNdx;
PatternContext respPat[MAXRESPONSE]; /* Response pattern structures. */
PatternContext *curRespPat;
void *arg;
LONG timeOut = timeLimit * 100; /* Time limit converted to milliseconds. */
CHATTRACE((LOG_INFO, TL_CHAT, "sendRecv: s=[%Z] t=%d q=%d",
sendStr, timeLimit, respStrQty));
/* Flush the input buffer up to MAXFLUSH characters. */
st = 0;
while ((i = read(fd, recvBuf, RECVBUFSZ - 1)) > 0) {
st += i;
if (st >= MAXFLUSH) {
CHATTRACE((LOG_ERR, TL_CHAT, "sendRecv: Too much garbage from device %d", fd));
st = -1;
finished = TRUE;
} else {
recvBuf[min(i, RECVBUFSZ - 1)] = '\0';
CHATTRACE((LOG_DEBUG, TL_CHAT,"sendRecv: flushed[%.30Z]", recvBuf));
}
}
/* Send string to device if not null. */
if (!finished && sendStr != NULL && sendStr[0] != '\0') {
CHATTRACE((LOG_INFO, TL_CHAT, "sendRecv: sending [%Z]", sendStr));
i = strlen(sendStr);
if (write(fd, sendStr, i) != i ) {
CHATTRACE((LOG_ERR, TL_CHAT, "sendRecv: Error sending [%Z] to %d",
sendStr, fd));
st = -1;
finished = TRUE;
}
}
/* Set up for matching the response strings. */
arg = &respStrQty;
((int *)arg)++; /* Assume that UINT pushed as an int on target machine. */
if (respStrQty >= MAXRESPONSE) {
CHATTRACE((LOG_WARNING, TL_CHAT, "sendRecv: WARNING! Ignoring extra response strings"));
respStrQty = MAXRESPONSE;
}
#if TRACECHAT > 0
recvNdx = 0;
#endif
for (i = 0; i < respStrQty; i++) {
curRespPat = &respPat[i];
curRespPat->st = 0; /* Initialize */
curRespPat->patStr = *((char **)arg)++;
curRespPat->patNdx = 0;
curRespPat->sourceNdx = 0;
curRespPat->matchNdx = 0;
#if TRACECHAT > 0
sprintf(&recvBuf[recvNdx], " p%d=[%Z]", i, respPat[i].patStr);
recvNdx = strlen(recvBuf);
if (recvNdx >= RECVBUFSZ - 32 || recvNdx >= 50) {
CHATTRACE((LOG_INFO, TL_CHAT, "sendRecv: %s", recvBuf));
recvNdx = 0;
}
#endif
}
#if TRACECHAT > 0
if (recvNdx > 0) {
CHATTRACE((LOG_INFO, TL_CHAT, "sendRecv: %s", recvBuf));
}
#endif
/* Wait limited time for response. */
recvNdx = 0;
recvBuf[0] = '\0';
while (!finished) {
/* Abort if user presses the NO button. */
if (buttonNoStatus() == NOBUTTON) {
CHATTRACE((LOG_ERR, TL_CHAT, "sendRecv: User abort!"));
st = -2;
finished = TRUE;
/* Read next character. */
} else if ((i = read(fd, &curChar, 1)) == 1) {
/* Trap ^C as abort character. */
if (curChar == '\003') {
st = -2;
finished = TRUE;
} else if (curChar > 0) {
recvBuf[recvNdx++] = curChar;
recvBuf[recvNdx] = '\0';
timeOut--; /* Assume a character takes at least a millisecond. */
for (i = 0; i < respStrQty && !finished; i++) {
if (patternMatch(recvBuf, &respPat[i]) == 0) {
st = i;
finished = TRUE;
}
}
}
/* Abort if read failed. */
} else if (i < -1) {
CHATTRACE((LOG_ERR, TL_CHAT, "sendRecv: Error reading from %d", fd));
st = -2; /* Assume user pressed NO button to generate error. */
finished = TRUE;
/* Abort if timed out. */
} else if (timeOut <= 0) {
CHATTRACE((LOG_DETAIL, TL_CHAT, "sendRecv:abort due to timeout"));
st = -1; /* Time out */
finished = TRUE;
/* Wait for 100ms but abort if NO button pressed. */
} else if (prompt(NOBUTTON, 100, NULL) == NOBUTTON) {
CHATTRACE((LOG_ERR, TL_CHAT, "sendRecv: User aborted reading from %d", fd);)
st = -2;
finished = TRUE;
/* Decrement the timer. */
} else
timeOut -= 100;
}
// Trace shows the last 20 bytes received if it'll fit.
#if TRACECHAT > 0
i = MIN(recvNdx, 20);
if (i == 0) i = 1;
#endif
CHATTRACE((LOG_INFO, TL_CHAT, "sendRecv: [%*.*Z] => %d",
i, LOGMSGLEN - 25, &recvBuf[recvNdx - i], st));
return st;
}
/**********************************/
/*** LOCAL FUNCTION DEFINITIONS ***/
/**********************************/
/*
* Copy the source pattern into the destination pattern and add the source, match and
* pattern offsets to the corresponding indexes.
* Return the destination pattern.
*/
static PatternContext *copyPattern(PatternContext *srcPat, PatternContext *destPat,
int srcOffset, int matchOffset, int patOffset)
{
destPat->st = srcPat->st;
destPat->patStr = srcPat->patStr;
destPat->patNdx = srcPat->patNdx + patOffset;
destPat->sourceNdx = srcPat->sourceNdx + srcOffset;
destPat->matchNdx = srcPat->matchNdx + matchOffset;
return destPat;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -