*** empty log message ***
This commit is contained in:
142
tecs/coc.c
142
tecs/coc.c
@@ -1,4 +1,12 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/socket.h>
|
||||
#include <strings.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "errhdl.h"
|
||||
#include "util.h"
|
||||
#include "coc.h"
|
||||
/*-------------------------------------------------------------------------*/
|
||||
/* CreateSocketAddress stolen from Tcl. Thanks to John Ousterhout */
|
||||
@@ -44,7 +52,7 @@ int CocCreateSockAdr(
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
int CocRecv(int fd, buf_type *buf)
|
||||
int CocRecv(int fd, Str_Buf *buf)
|
||||
{
|
||||
struct timeval tmo={0,1};
|
||||
fd_set mask;
|
||||
@@ -57,11 +65,9 @@ int CocRecv(int fd, buf_type *buf)
|
||||
ERR_SI(i=select(fd+1,&mask,NULL,NULL,&tmo));
|
||||
if (i==0) ERR_MSG("time out");
|
||||
|
||||
buf_put_start(buf);
|
||||
ERR_SI(ret=recv(fd, buf->buf, buf->isize, 0));
|
||||
if (ret==0) { ERR_COD(ECONNRESET); }
|
||||
buf->usize=ret;
|
||||
buf_reset(buf);
|
||||
ERR_SI(buf->wrpos=recv(fd, buf->buf, buf->dsize, 0));
|
||||
if (buf->wrpos==0) { ERR_COD(ECONNRESET); }
|
||||
str_get_start(buf);
|
||||
return(ret);
|
||||
OnError: return(-1);
|
||||
}
|
||||
@@ -71,7 +77,17 @@ void CocVarList(CocVar **varList) {
|
||||
varListHdl=varList;
|
||||
}
|
||||
|
||||
CocVar *CocFindVar(CocVar *varList, const char *name) {
|
||||
void CocList() {
|
||||
CocVar *p;
|
||||
|
||||
p=*varListHdl;
|
||||
while (p!=NULL) {
|
||||
printf("%s %d\n", p->name, p->type);
|
||||
p=p->next;
|
||||
}
|
||||
}
|
||||
|
||||
CocVar *CocFindVar1(CocVar *varList, const char *name) {
|
||||
CocVar *p;
|
||||
|
||||
p=varList;
|
||||
@@ -79,67 +95,129 @@ CocVar *CocFindVar(CocVar *varList, const char *name) {
|
||||
return(p);
|
||||
}
|
||||
|
||||
void CocDefVar(const char *name, void *var, int type, int *flag) {
|
||||
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=CocFindVar(*varListHdl, name);
|
||||
p=CocFindVar1(*varListHdl, name);
|
||||
if (p==NULL) {
|
||||
p=malloc(sizeof(*p));
|
||||
p->next=*varListHdl;
|
||||
*varListHdl=p;
|
||||
str_copy(p->name, name);
|
||||
}
|
||||
str_copy(p->name, name);
|
||||
p->var=var;
|
||||
p->type=type;
|
||||
p->flag=flag;
|
||||
return(p);
|
||||
}
|
||||
|
||||
int CocGetVar(CocVar *varList, buf_type *buf, const char *name) {
|
||||
CocVar *var;
|
||||
void CocDefVarS(const char *name, const char *tname, void *var, int type) {
|
||||
CocVar *p, *t;
|
||||
|
||||
var=CocFindVar(varList, name);
|
||||
assert(type==COC_PTR || type==COC_STRUCT);
|
||||
p=CocDefVar(name, var, type, &CocRD);
|
||||
p->strucType=CocDefVar(tname, NULL, COC_TYPE, &CocRD);
|
||||
}
|
||||
|
||||
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 (varList==serverVarList) { /* we are the server */
|
||||
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");
|
||||
}
|
||||
if (var->type==-1) {
|
||||
*(int *)var->var=buf_get_int(buf);
|
||||
ERR_I(str_get_int(buf, (int *)adr));
|
||||
} else if (var->type==-2) {
|
||||
*(float *)var->var=buf_get_float(buf);
|
||||
ERR_I(str_get_float(buf, (float *)adr));
|
||||
} else if (var->type>1) {
|
||||
str_ncpy((char *)var->var, buf_get_str(buf), var->type);
|
||||
ERR_P(str_nget_str(buf, (char *)adr, var->type));
|
||||
} else {
|
||||
ERR_MSG("unknown type");
|
||||
}
|
||||
if (varList==serverVarList) { /* we are the server */
|
||||
if (secure) { /* we are the server */
|
||||
(*var->flag)++;
|
||||
}
|
||||
return(0);
|
||||
OnError: return(-1);
|
||||
}
|
||||
|
||||
int CocPutVar(CocVar *varList, buf_type *buf, const char *name) {
|
||||
int CocPutVar(CocVar *varList, Str_Buf *buf, const char *name, int secure) {
|
||||
CocVar *var;
|
||||
void *adr;
|
||||
char *c;
|
||||
|
||||
var=CocFindVar(varList, name);
|
||||
|
||||
var=CocFindVar(varList, name, &adr);
|
||||
if (var==NULL) ERR_MSG("undefined variable");
|
||||
if (varList!=serverVarList) { /* we are the client */
|
||||
if (adr==NULL) {
|
||||
adr=var->var;
|
||||
} else {
|
||||
adr=(char *)adr + (int)var->var;
|
||||
}
|
||||
if (secure) { /* we are the client */
|
||||
if (var->flag==&CocRD) ERR_MSG("variable is read only");
|
||||
}
|
||||
if (var->type==-1) {
|
||||
buf_put_int(buf, *(int *)var->var);
|
||||
ERR_I(str_put_int(buf, *(int *)adr));
|
||||
} else if (var->type==-2) {
|
||||
buf_put_float(buf, *(float *)var->var);
|
||||
ERR_I(str_put_float(buf, *(float *)adr));
|
||||
} else if (var->type>1) {
|
||||
c=var->var;
|
||||
if (c[0]<=2 && c[0]!=0) { ERR_MSG("illegal string"); }
|
||||
buf_put_str(buf, c);
|
||||
ERR_I(str_put_str(buf, adr));
|
||||
} else {
|
||||
ERR_MSG("unknown type");
|
||||
}
|
||||
if (varList!=serverVarList) { /* we are a client */
|
||||
if (secure) { /* we are a client */
|
||||
if (var->flag!=NULL) (*var->flag)++;
|
||||
}
|
||||
return(0);
|
||||
@@ -159,3 +237,11 @@ void CocFreeVarList(CocVar **varList) {
|
||||
}
|
||||
*varList=NULL;
|
||||
}
|
||||
|
||||
void CocDelay(int msec) {
|
||||
struct timeval tmo;
|
||||
|
||||
tmo.tv_sec=msec / 1000;
|
||||
tmo.tv_usec=(msec % 1000)*1000+1;
|
||||
select(1,NULL,NULL,NULL,&tmo);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user