new tecs version M.Z.08.2001
This commit is contained in:
267
tecs/coc_util.c
267
tecs/coc_util.c
@@ -1,24 +1,15 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/socket.h>
|
||||
#include <strings.h>
|
||||
#include <sys/time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "sys_util.h"
|
||||
#include "err_handling.h"
|
||||
#include "str_util.h"
|
||||
#include "myc_err.h"
|
||||
#include "myc_str.h"
|
||||
#include "coc_util.h"
|
||||
/*-------------------------------------------------------------------------*/
|
||||
/* CreateSocketAddress stolen from Tcl. Thanks to John Ousterhout */
|
||||
|
||||
CocVar *serverVarList=NULL;
|
||||
static CocVar **varListHdl=&serverVarList;
|
||||
int CocRD=0;
|
||||
int CocWR=0;
|
||||
|
||||
int CocCreateSockAdr(
|
||||
struct sockaddr_in *sockaddrPtr, /* Socket address */
|
||||
const char *host, /* Host. NULL implies INADDR_ANY */
|
||||
@@ -55,202 +46,6 @@ int CocCreateSockAdr(
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
int CocRecv(int fd, Str_Buf *buf)
|
||||
{
|
||||
struct timeval tmo={0,1};
|
||||
fd_set mask;
|
||||
int i;
|
||||
|
||||
tmo.tv_sec=5; /* timeout 5 sec. */
|
||||
|
||||
FD_ZERO(&mask);
|
||||
FD_SET(fd, &mask);
|
||||
ERR_SI(i=select(fd+1,&mask,NULL,NULL,&tmo));
|
||||
if (i==0) ERR_MSG("time out");
|
||||
|
||||
ERR_SI(buf->wrpos=recv(fd, buf->buf, buf->dsize, 0));
|
||||
if (buf->wrpos==0) { ERR_COD(ECONNRESET); }
|
||||
str_get_start(buf);
|
||||
return(buf->wrpos);
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
void CocVarList(CocVar **varList) {
|
||||
assert(varList!=NULL);
|
||||
varListHdl=varList;
|
||||
}
|
||||
|
||||
void CocList() {
|
||||
CocVar *p;
|
||||
|
||||
p=*varListHdl;
|
||||
while (p!=NULL) {
|
||||
printf("%s %d ", p->name, p->type);
|
||||
p=p->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
CocVar *CocFindVar1(CocVar *varList, const char *name) {
|
||||
CocVar *p;
|
||||
|
||||
p=varList;
|
||||
while (p!=NULL && 0!=strcasecmp(p->name,name)) p=p->next;
|
||||
return(p);
|
||||
}
|
||||
|
||||
CocVar *CocFindVar(CocVar *varList, const char *name, void **adr) {
|
||||
CocVar *p, *t;
|
||||
const char *f;
|
||||
void *base;
|
||||
char nam[32];
|
||||
|
||||
f=str_split(nam, name, '.');
|
||||
if (f==NULL) {
|
||||
f=str_split(nam, name, '-');
|
||||
if (f!=NULL) {
|
||||
if (f[0]!='>') {
|
||||
f=NULL;
|
||||
} else {
|
||||
f++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (f!=NULL) {
|
||||
if (adr!=NULL) *adr=NULL;
|
||||
p=CocFindVar1(varList, nam);
|
||||
if (p==NULL) { return(NULL); }
|
||||
t=p->strucType;
|
||||
if (t==NULL) { return(NULL); }
|
||||
str_copy(nam, t->name);
|
||||
str_append(nam, ":");
|
||||
str_append(nam, f);
|
||||
if (adr!=NULL) {
|
||||
base=p->var;
|
||||
if (p->type==COC_PTR) base=*(void **)base;
|
||||
*adr=base;
|
||||
}
|
||||
} else if (adr!=NULL) {
|
||||
*adr=NULL;
|
||||
}
|
||||
p=CocFindVar1(varList, nam);
|
||||
if (p!=NULL && p->type==COC_ALIAS) { /* recursive call for alias */
|
||||
p=CocFindVar(varList, p->var, adr);
|
||||
}
|
||||
return(p);
|
||||
}
|
||||
|
||||
CocVar *CocDefVar(const char *name, void *var, int type, int *flag) {
|
||||
CocVar *p;
|
||||
const char *f;
|
||||
void *adr;
|
||||
|
||||
assert(varListHdl!=NULL);
|
||||
p=CocFindVar1(*varListHdl, name);
|
||||
if (p==NULL) {
|
||||
NEW(p);
|
||||
p->next=*varListHdl;
|
||||
*varListHdl=p;
|
||||
str_copy(p->name, name);
|
||||
p->type=type;
|
||||
} else {
|
||||
assert(p->type==type);
|
||||
}
|
||||
p->var=var;
|
||||
p->flag=flag;
|
||||
/* printf("define %s %d\n", name, (int)var); */
|
||||
return(p);
|
||||
OnError:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
void CocDefVarS(const char *name, const char *tname, void *var, int type) {
|
||||
CocVar *p, *t;
|
||||
|
||||
assert(type==COC_PTR || type==COC_STRUCT);
|
||||
p=CocDefVar(name, var, type, &CocRD);
|
||||
p->strucType=CocDefVar(tname, NULL, COC_TYPE, &CocRD);
|
||||
}
|
||||
|
||||
char err_name[64];
|
||||
|
||||
int CocGetVar(CocVar *varList, Str_Buf *buf, const char *name, int secure) {
|
||||
CocVar *var;
|
||||
void *adr;
|
||||
|
||||
var=CocFindVar(varList, name, &adr);
|
||||
if (var==NULL) ERR_MSG("undefined variable");
|
||||
if (adr==NULL) {
|
||||
adr=var->var;
|
||||
} else {
|
||||
adr=(char *)adr + (int)var->var;
|
||||
}
|
||||
if (secure) { /* we are the server */
|
||||
if (var->flag==&CocRD) ERR_MSG("variable is read only");
|
||||
}
|
||||
/* printf("get %s %d\n", name, (int)adr); */
|
||||
if (var->type==-1) {
|
||||
ERR_I(str_get_int(buf, (int *)adr));
|
||||
} else if (var->type==-2) {
|
||||
ERR_I(str_get_float(buf, (float *)adr));
|
||||
} else if (var->type>1) {
|
||||
ERR_P(str_nget_str(buf, (char *)adr, var->type));
|
||||
} else {
|
||||
ERR_MSG("unknown type");
|
||||
}
|
||||
if (secure) { /* we are the server */
|
||||
(*var->flag)++;
|
||||
}
|
||||
return(0);
|
||||
OnError: str_copy(err_name, name); ErrTxt(err_name,0); return(-1);
|
||||
}
|
||||
|
||||
int CocPutVar(CocVar *varList, Str_Buf *buf, const char *name, int secure) {
|
||||
CocVar *var;
|
||||
void *adr;
|
||||
char *c;
|
||||
|
||||
var=CocFindVar(varList, name, &adr);
|
||||
if (var==NULL) ERR_MSG("undefined variable");
|
||||
if (adr==NULL) {
|
||||
adr=var->var;
|
||||
} else {
|
||||
adr=(char *)adr + (int)var->var;
|
||||
}
|
||||
if (secure) { /* check access */
|
||||
if (var->flag==&CocRD) ERR_MSG("variable is read only");
|
||||
}
|
||||
/* printf("put %s %d\n", name, (int)adr); */
|
||||
if (var->type==-1) {
|
||||
ERR_I(str_put_int(buf, *(int *)adr));
|
||||
} else if (var->type==-2) {
|
||||
ERR_I(str_put_float(buf, *(float *)adr));
|
||||
} else if (var->type>1) {
|
||||
ERR_I(str_put_str(buf, adr));
|
||||
} else {
|
||||
ERR_MSG("unknown type");
|
||||
}
|
||||
if (secure) { /* we are a client */
|
||||
if (var->flag!=NULL) (*var->flag)++;
|
||||
}
|
||||
return(0);
|
||||
OnError: str_copy(err_name, name); ErrTxt(err_name,0); return(-1);
|
||||
}
|
||||
|
||||
void CocFreeVarList(CocVar **varList) {
|
||||
CocVar *p, *v;
|
||||
|
||||
if (varList==NULL) varList=&serverVarList;
|
||||
v=*varList;
|
||||
while (v!=NULL) {
|
||||
p=v;
|
||||
v=p->next;
|
||||
p->next=NULL;
|
||||
FREE(p);
|
||||
}
|
||||
*varList=NULL;
|
||||
}
|
||||
|
||||
void CocDelay(int msec) {
|
||||
struct timeval tmo;
|
||||
|
||||
@@ -258,3 +53,59 @@ void CocDelay(int msec) {
|
||||
tmo.tv_usec=(msec % 1000)*1000+1;
|
||||
select(1,NULL,NULL,NULL,&tmo);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
int CocSend(int fd, char *str, int size) {
|
||||
int siz;
|
||||
siz=htonl(size);
|
||||
ERR_SI(send(fd, &siz, 4, 0));
|
||||
ERR_SI(send(fd, str, size, 0));
|
||||
return(0);
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
int CocRecv(int fd, StrBuf *buf, int timeout, int *flag) {
|
||||
struct timeval tmo={0,1};
|
||||
fd_set mask;
|
||||
int i, l;
|
||||
int siz, n;
|
||||
|
||||
if (timeout>=0) {
|
||||
tmo.tv_sec=timeout;
|
||||
|
||||
FD_ZERO(&mask);
|
||||
FD_SET(fd, &mask);
|
||||
ERR_SI(i=select(fd+1,&mask,NULL,NULL,&tmo));
|
||||
if (flag!=NULL) {
|
||||
*flag=0;
|
||||
if (i==0) return(0);
|
||||
} else {
|
||||
if (i==0) { ERR_MSG("time out"); }
|
||||
}
|
||||
}
|
||||
|
||||
n=0;
|
||||
ERR_SI(i=recv(fd, &n, 4, 0));
|
||||
if (i!=4) {
|
||||
ERR_COD(ECONNRESET);
|
||||
}
|
||||
siz=ntohl(n);
|
||||
if (siz > buf->dsize)
|
||||
ERR_MSG("buffer too small");
|
||||
ERR_SI(l=recv(fd, buf->buf, siz, 0));
|
||||
buf->wrpos=l;
|
||||
while (buf->wrpos<siz) {
|
||||
if (l==0) {
|
||||
ERR_COD(ECONNRESET);
|
||||
}
|
||||
ERR_SI(l=recv(fd, buf->buf+buf->wrpos, siz, 0));
|
||||
buf->wrpos+=l;
|
||||
}
|
||||
StrReset(buf);
|
||||
return(buf->wrpos);
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user