- added AsconHostport function

- added composite terminator in standard protocol
This commit is contained in:
zolliker
2012-06-19 06:31:06 +00:00
parent 3141f63f20
commit 36d1204da0
3 changed files with 70 additions and 11 deletions

56
ascon.c
View File

@ -71,7 +71,8 @@ void AsconError(Ascon *a, char *msg, int errorno)
"timeout"
};
char *state;
char num[8];
if (a->state < 0 || a->state >= AsconMaxState) {
state = "bad state";
} else {
@ -128,8 +129,10 @@ static void AsconConnect(Ascon * a)
return;
}
colon = strchr(a->hostport, ':');
if (colon == NULL)
if (colon == NULL) {
AsconError(a, "expected 'host:port' or 'unconnected'", 0);
return;
}
port = atoi(colon + 1);
if (port <= 0) {
AsconError(a, "bad port number", 0);
@ -321,6 +324,7 @@ int AsconBaseHandler(Ascon * a)
if (ret == 0) {
/* in progress */
} else if (ret > 0) {
DynStringClear(a->errmsg);
a->state = AsconConnectDone; /* success */
} else if (ret < 0) {
AsconError(a, "ASC3", errno);
@ -419,7 +423,7 @@ int AsconStdHandler(Ascon * a)
int result;
char chr;
int ret, l;
char *cmd, *opt;
char *cmd, *opt, *buf;
switch (a->state) {
case AsconWriteStart:
@ -464,9 +468,18 @@ int AsconStdHandler(Ascon * a)
}
if (a->replyTerminator != NULL) {
if (strchr(a->replyTerminator, chr) != NULL) {
a->state = AsconReadDone;
if (chr == '\n' || chr == '\r') {
DynStringBackspace(a->rdBuffer); /* remove LF or CR */
if (a->compositeTerminator) {
/* one character was o.k., but all have to match */
l = strlen(a->replyTerminator);
buf = GetCharArray(a->rdBuffer) + GetDynStringLength(a->rdBuffer) - l;
if (strncmp(buf, a->replyTerminator, l) == 0) {
a->state = AsconReadDone;
}
} else {
a->state = AsconReadDone;
if (chr == '\n' || chr == '\r') {
DynStringBackspace(a->rdBuffer); /* remove LF or CR */
}
}
}
} else {
@ -502,10 +515,13 @@ int AsconStdHandler(Ascon * a)
* Treat hex strings as terminators right. Note that this
* is limited to single character terminators.
* M.Z. changed strstr to strncmp (more precise)
*
* M.Z. add new option (composite terminator):
* convert 'term' to term
*/
static void AsconCheckTerminators(Ascon *a)
void AsconCheckTerminators(Ascon *a)
{
int c;
int c, i, l;
if (a->sendTerminator != NULL && strncmp(a->sendTerminator,"0x",2) == 0) {
sscanf(a->sendTerminator,"%x",&c);
@ -517,6 +533,17 @@ static void AsconCheckTerminators(Ascon *a)
a->replyTerminator[0] = (char)c;
a->replyTerminator[1] = '\0';
}
a->compositeTerminator = 0;
if (a->replyTerminator != NULL && a->replyTerminator[0] == '\'') {
l = strlen(a->replyTerminator);
if (l > 2 && a->replyTerminator[l-1] == '\'') {
for (i = 0; i < l - 2; i++) {
a->replyTerminator[i] = a->replyTerminator[i+1];
}
a->replyTerminator[l-2] = '\0';
a->compositeTerminator = 1;
}
}
}
int AsconInterpreteArgs(int argc, char *argv[],
@ -715,6 +742,7 @@ AsconStatus AsconTask(Ascon * a)
return AsconUnconnected;
case AsconConnectDone:
a->state = AsconIdle;
DynStringClear(a->errmsg); /* connection o.k. */
return AsconReady;
case AsconWriteDone:
if (a->noResponse) {
@ -731,6 +759,7 @@ AsconStatus AsconTask(Ascon * a)
case AsconReadDone:
a->state = AsconIdle;
a->responseValid = 1;
DynStringClear(a->errmsg);
return AsconReady;
case AsconIdle:
return AsconReady;
@ -794,5 +823,14 @@ char *AsconGetError(Ascon *a)
int AsconLastState(Ascon *a)
{
return (int)a->state;
return (int)a->state;
}
char *AsconHostport(Ascon *a)
{
if (a==NULL) {
return NULL;
}
return a->hostport;
}