104 lines
2.4 KiB
C
104 lines
2.4 KiB
C
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include "errhdl.h"
|
|
#include "logfile.h"
|
|
#include "lsc.h"
|
|
#include "util.h"
|
|
|
|
#define MC LSC_MAX_CMDS
|
|
#define MAX_PAR 16
|
|
#define MAX_ARG 9
|
|
/* when changing FBUF_LEN, change also the constant in the fscanf call in subroutine LscExeCmd */
|
|
#define FBUF_LEN 132
|
|
|
|
int LscEqPar(char *par, char *res) {
|
|
char pbuf[SER_BUF_LEN], rbuf[SER_BUF_LEN];
|
|
char *plist[MAX_PAR], *rlist[MAX_PAR];
|
|
int i,n,i1,i2;
|
|
float f1, f2;
|
|
char ch1, ch2;
|
|
|
|
strcpy(pbuf, par);
|
|
strcpy(rbuf, res);
|
|
n=MAX_PAR;
|
|
str_split(pbuf, ',', plist, &n);
|
|
str_split(rbuf, ',', rlist, &n);
|
|
for (i=0; i<n; i++) {
|
|
if (*plist[i]!='\0' && 0!=str_cmp(plist[i], rlist[i])) {
|
|
i1=sscanf(plist[i], "%f%1s", &f1, &ch1);
|
|
i2=sscanf(rlist[i], "%f%1s", &f2, &ch2);
|
|
if (i1!=1 || i2!=1 || abs(f1-f2)>1e-4+abs(f1)*5e-6) {
|
|
logfileOut("%s#%s\n", plist[i], rlist[i]);
|
|
return(0);
|
|
}
|
|
}
|
|
}
|
|
return(1);
|
|
}
|
|
|
|
int LscCmdChkC(SerChannel *ser, const char *cmds) {
|
|
char buf[128];
|
|
assert(strlen(cmds)<128);
|
|
strcpy(buf, cmds);
|
|
return(LscCmdChk(ser, buf));
|
|
}
|
|
|
|
int LscCmdChk(SerChannel *ser, char *cmds) {
|
|
char *b, *o, *cn, *c, *d, *r, *res;
|
|
char *clist[MC], *plist[MC], *rlist[MC], *olist[MC];
|
|
char obuf[SER_BUF_LEN], r1[SER_BUF_LEN];
|
|
int i,n,m,j,cnt,l;
|
|
|
|
if (NULL!=strchr(cmds, '?')) ERR_COD(EINVAL);
|
|
cn=cmds;
|
|
o=&obuf[0];
|
|
m=MC;
|
|
do {
|
|
n=m;
|
|
cn=str_split(cn, ';', clist, &n);
|
|
for (i=0; i<n; i++) {
|
|
c=clist[i];
|
|
b=strchr(c, ' ');
|
|
d=strchr(c, ':');
|
|
if (d==NULL) ERR_COD(EINVAL);
|
|
if (b==NULL || b>d) {
|
|
*d=' '; b=d;
|
|
} else {
|
|
*d=',';
|
|
}
|
|
plist[i]=d+1;
|
|
l=strlen(c);
|
|
if ((o-&obuf[0])+l+(d-c)+2>SER_BUF_LEN) ERR_COD(ENOBUFS);
|
|
olist[i]=o;
|
|
strcpy(o, c); o+=l;
|
|
*o=';'; o++;
|
|
strncpy(o, c, d-c);
|
|
o[b-c]='?'; o+=d-c;
|
|
if (b==d) o++;
|
|
*o=';'; o++;
|
|
};
|
|
o--; *o='\0';
|
|
cnt=0;
|
|
do {
|
|
cnt++;
|
|
if (cnt>3) ERR_MSG("can not set parameter");
|
|
ERR_P(res=SerCmd(ser, obuf));
|
|
if (cnt>0) {
|
|
if (0==strcmp(r1, res)) break; /* got two times the same result */
|
|
}
|
|
strcpy(r1, res);
|
|
j=n;
|
|
r=str_split(res, ';', rlist, &n);
|
|
if (r==NULL || n!=j) continue;
|
|
for (i=0; i<n; i++) { /* compare parameters */
|
|
if (! LscEqPar(plist[i], rlist[i])) { j=0; break; }
|
|
}
|
|
} while (j==0);
|
|
} while (cn!=NULL);
|
|
return(0);
|
|
OnError: return(-1);
|
|
}
|