*** empty log message ***

This commit is contained in:
cvs
2000-03-15 10:10:22 +00:00
parent 749772c6ae
commit 49669ee311
13 changed files with 2601 additions and 90 deletions

1569
tecs/asynsrv_utility.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -77,7 +77,7 @@ void buf_put_int(buf_type *buf, int val)
return;
}
void buf_put_str(buf_type *buf, char *str)
void buf_put_str(buf_type *buf, const char *str)
{ char *b;
int l;

View File

@ -4,61 +4,63 @@
/*-------------------------------------------------------------------------*/
int CocConnect(CocConn *conn, int allowConnRefused) {
int i;
struct sockaddr_in sadr;
ERR_I(CocCreateSockAdr(&sadr, conn->host, conn->port));
ERR_SI(conn->fd=socket(AF_INET, SOCK_STREAM, 0));
i = 1;
ERR_SI(setsockopt(conn->fd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(int))); /* allow quick port reuse */
i=connect(conn->fd, (struct sockaddr *)&sadr, sizeof(sadr));
if (i<0) {
if (allowConnRefused && errno==ECONNREFUSED) return(1);
ERR_COD(errno);
}
return(0);
OnError: return(-1);
}
int CocOpen(CocConn *conn)
{
int i, cnt, try, port, tryConn, tmo;
struct sockaddr_in sadr;
char *p, *q;
int i, try, tmo;
try=2;
tryConn=1;
tmo=0;
ERR_I(i=CocConnect(conn, conn->startcmd[0]!='\0'));
if (i==0) return(0);
printf("%s\n", conn->startcmd);
ERR_I(system(conn->startcmd));
try=15;
tmo=100; /* wait total ca. 10 sec. for 15 tries */
while (try>0) {
try--;
while (tryConn>0) {
port=CocPORT;
cnt=CocPORTS;
tryConn--;
while (cnt>0) {
ERR_SI(conn->fd=socket(AF_INET, SOCK_STREAM, 0));
ERR_I(CocCreateSockAdr(&sadr, conn->host, port));
i=connect(conn->fd, (struct sockaddr *)&sadr, sizeof(sadr));
if (i>=0) return(0);
if (errno!=ECONNREFUSED) { ERR_COD(errno); }
port++; cnt--;
util_delay(tmo); tmo=tmo*5/4;
ERR_I(i=CocConnect(conn, try>0));
if (i==0) return(0);
}
util_delay(tmo); tmo+=100;
}
if (conn->startcmd[0]=='\0' || try<=0) { ERR_COD(ECONNREFUSED); }
p=conn->startcmd;
q=strchr(p,' ');
while (q!=NULL) {
p=q+1;
q=strchr(p,' ');
};
printf("start server %s\n", p);
ERR_I(system(conn->startcmd));
util_delay(100); tmo=200;
tryConn=5;
}
ERR_COD(ECONNREFUSED);
ERR_MSG("error in CocConnect");
OnError: return(-1);
}
/*-------------------------------------------------------------------------*/
void CocInitClient(CocConn *conn, char *host, char *magic, int bufsize, char *startcmd)
{ void *e;
int CocInitClient(CocConn *conn, char *host, int port, char *magic, int bufsize, char *startcmd) {
assert(conn!=NULL);
if (bufsize==0) bufsize=1024;
conn->cmdbuf=buf_create(bufsize);
conn->resbuf=buf_create(bufsize);
conn->port=port;
str_copy(conn->host, host);
str_copy(conn->magic, magic);
str_copy(conn->startcmd, startcmd);
conn->fd=-1;
conn->varList=NULL;
CocVarList(&conn->varList);
ERR_I(CocOpen(conn));
ERR_I(CocSendMagic(conn, conn->magic));
return(0);
OnError: return(-1);
}
/*-------------------------------------------------------------------------*/
@ -102,6 +104,7 @@ int CocCmd(CocConn *conn, const char *rwList)
char nam[32];
CocVar *var;
assert(conn!=NULL);
buf_put_start(conn->cmdbuf);
s=rwList;
setmode=0;
@ -117,7 +120,7 @@ int CocCmd(CocConn *conn, const char *rwList)
assert(i<32);
str_ncpy(nam, s, i+1);
if (setmode) {
if (nam[i-1]==']') { nam[i-1]='\0'; setmode=0; }
if (nam[i-1]==']') { i--; nam[i]='\0'; setmode=0; }
buf_put_str(conn->cmdbuf, nam);
ERR_I(CocPutVar(conn->varList, conn->cmdbuf, nam));
if (!setmode) buf_put_str(conn->cmdbuf, "]");
@ -159,9 +162,10 @@ int CocCmd(CocConn *conn, const char *rwList)
setmode=1;
}
i=t-s;
if (!setmode) {
if (setmode) {
if (*(t-1)==']') setmode=0;
} else {
str_ncpy(nam, s, i+1);
if (nam[i-1]==']') { nam[i-1]='\0'; setmode=0; }
ERR_I(CocGetVar(conn->varList, conn->resbuf, nam));
}
s=t+1;
@ -173,6 +177,7 @@ int CocCmd(CocConn *conn, const char *rwList)
}
void CocCloseClient(CocConn *conn) {
assert(conn!=NULL);
close(conn->fd);
buf_free(conn->cmdbuf);
buf_free(conn->resbuf);

View File

@ -19,7 +19,7 @@ int CocCreateSockAdr(
(void) memset((char *) sockaddrPtr, '\0', sizeof(struct sockaddr_in));
sockaddrPtr->sin_family = AF_INET;
sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF));
if (host == NULL) {
if (host == NULL || host[0]=='\0') {
addr.s_addr = INADDR_ANY;
} else {
hostent = gethostbyname(host);
@ -113,7 +113,7 @@ int CocGetVar(CocVar *varList, buf_type *buf, const char *name) {
ERR_MSG("unknown type");
}
if (varList==serverVarList) { /* we are the server */
if (var->flag!=NULL) (*var->flag)++;
(*var->flag)++;
}
return(0);
OnError: return(-1);
@ -125,7 +125,7 @@ int CocPutVar(CocVar *varList, buf_type *buf, const char *name) {
var=CocFindVar(varList, name);
if (var==NULL) ERR_MSG("undefined variable");
if (varList!=serverVarList) { /* we are a client */
if (varList!=serverVarList) { /* we are the client */
if (var->flag==&CocRD) ERR_MSG("variable is read only");
}
if (var->type==-1) {

242
tecs/dlog.c Normal file
View File

@ -0,0 +1,242 @@
/* switch off ANSI_C_SOURCE (allows compiling all sources with cc/stand=ansi) */
#ifdef __VMS
#ifdef __HIDE_FORBIDDEN_NAMES
#undef __HIDE_FORBIDDEN_NAMES
#endif
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include "errhdl.h"
#include "util.h"
#include "dlog.h"
#define VERSION 1.1
#define DEFINED(F) (abs((F)-dset->undef)>1.0e-5*(F))
int DlogWrite(DlogSet *dset, int idx, void *data) {
int p;
p = dset->headsize + (idx % dset->nlen) * dset->fsize;
if (p!=dset->pos) {
ERR_SI(lseek(dset->fd, p, SEEK_SET));
}
ERR_SI(write(dset->fd, data, dset->fsize));
dset->pos = p + dset->fsize;
return(0);
OnError:
return(-1);
}
int DlogRead(DlogSet *dset, int idx, void *data) {
int p;
p = dset->headsize + (idx % dset->nlen) * dset->fsize;
if (p!=dset->pos) {
ERR_SI(lseek(dset->fd, p, SEEK_SET));
}
ERR_SI(read(dset->fd, data, dset->fsize));
dset->pos = p + dset->fsize;
return(0);
OnError:
return(-1);
}
int DlogOpen(DlogSet *dset, char *name, int write) {
int i, p, np, fd;
fd=0;
str_copy(dset->name, name);
if (write) {
#ifdef __VMS
ERR_SI(fd=open(name, O_RDWR | O_SYNC, 0, "SHR=UPD"));
#else
ERR_SI(fd=open(name, O_RDWR | O_SYNC, 0));
#endif
} else {
#ifdef __VMS
ERR_SI(fd=open(name, O_RDONLY | O_SYNC, 0, "SHR=UPD"));
#else
ERR_SI(fd=open(name, O_RDONLY | O_SYNC, 0));
#endif
}
dset->fd=fd;
p=(char *)&dset->fd - (char *)dset;
ERR_SI(read(dset->fd, dset, p));
dset->pos=p;
if ((int)(dset->version*1000+0.5) != (int)(VERSION*1000+0.5)) ERR_MSG("version mismatch");
if (dset->headsize != p) ERR_MSG("illegal dlog file");
return(0);
OnError:
if (fd!=0) close(fd);
return(-1);
}
int DlogCreate(DlogSet *dset, char *name, time_t start, int nset, int nlen, int period, float undef) {
int fd, i, j, p;
va_list ap;
float f[DLOG_MAX_SET];
fd=0;
if (nset>DLOG_MAX_SET) ERR_MSG("nset too large");
dset->nset=nset;
dset->nlen=nlen;
dset->start=start;
dset->last=start-period;
dset->period=period;
dset->undef=undef;
dset->version=VERSION;
str_copy(dset->name, name);
for (i=0; i<nset; i++) {
f[i]=dset->undef;
}
ERR_SI(fd=open(name, O_RDWR | O_CREAT | O_SYNC | O_TRUNC, 0666));
dset->fd=fd;
p=(char *)&dset->fd - (char *)dset;
dset->headsize = p;
dset->fsize = nset * sizeof(float);
ERR_SI(write(dset->fd, dset, p));
dset->pos=p;
return(0);
OnError:
if (fd!=0) close(fd);
return(-1);
}
int DlogPut(DlogSet *dset, time_t time, int nset, float val[]) {
int i, i0, j;
float f[DLOG_MAX_SET], f0[DLOG_MAX_SET];
if (nset > dset->nset) nset = dset->nset;
for (j=0; j < nset; j++) { f[j]=val[j]; }
for (j=nset; j < dset->nset; j++) { f[j]=dset->undef; }
i = ( time - dset->start ) / dset->period;
i0 = ( dset->last - dset->start ) / dset->period;
if (i0==i) {
ERR_I(DlogRead(dset, i, f0));
for (j=0; j<nset; j++) {
if (DEFINED(f0[j]) && /* when last point defined */
(i%2 && f0[j]>f[j] || /* take maximum (for odd i) */
!i%2 && f0[j]<f[j] ) /* or minimum (for even i) */
) f[j]=f0[j]; /* of f0 and f0 */
}
} else if (i0<i-1) {
for (j=0; j < dset->nset; j++) { f0[j]=dset->undef; }
i0++;
while (i0<i) {
ERR_I(DlogWrite(dset, i0, f0));
i0++;
}
}
ERR_I(DlogWrite(dset, i, f));
dset->last=time;
return(0);
OnError:
return(-1);
}
int DlogGet(DlogSet *dset, int iset, int nmax, double *starttime, float x[], float y[]) {
int i0, i, n, undef;
float f[DLOG_MAX_SET];
if (iset<0) ERR_MSG("illegal iset");
if (iset>=dset->nset) return(0);
i = ( dset->last - dset->start ) / dset->period;
i0=0;
if (i0 <= i - dset->nlen) {
i0 = i - dset->nlen + 1;
}
*starttime = dset->start + i0 * dset->period;
n=0;
undef=2;
while (i0<=i) {
if (n>=nmax) return(n);
ERR_I(DlogRead(dset, i0, f));
i0++;
if (DEFINED(f[iset])) {
x[n]=(float)i0*dset->period;
y[n]=f[iset];
n++;
undef=0;
} else if (undef==0) {
undef=1;
} else if (undef==1) {
undef=2;
x[n]=0;
y[n]=0;
n++;
}
}
return(n);
OnError:
return(-1);
}
int DlogGetMany(DlogSet *dset, int nset, int nmax, double *starttime, float x[], float y[], int index[]) {
int n, k, nmx, ntot;
ntot=0;
for (k=0; k<nset; k++) {
index[k]=ntot;
nmx=(nmax-ntot)/(nset-k);
ERR_I(n=DlogGet(dset, k, nmx, starttime, &x[ntot], &y[ntot]));
ntot+=n;
}
return(ntot);
OnError:
return(-1);
}
int Dlog_Get_(char *name, int *nset, int *nmax, double *starttime, float x[], float y[], int index[], int namlen) {
DlogSet dset;
char buf[64];
int ntot;
/* for compaq unix */
str_ntrim(buf, sizeof(buf), name, namlen);
ERR_I(DlogOpen(&dset, buf, 0));
ERR_I(ntot=DlogGetMany(&dset, *nset, *nmax, starttime, x, y, index));
DlogClose(&dset);
return(ntot);
OnError:
ErrShow("error in Dlog_Get");
return(-1);
}
int Dlog_Get(char **name, int *nset, int *nmax, double *starttime, float x[], float y[], int index[]) {
/* for VMS */
return(Dlog_Get_(name[1], nset, nmax, starttime, x, y, index, *(short *)name));
}
int DlogUpd(DlogSet *dset) {
ERR_SI(lseek(dset->fd, 0, SEEK_SET));
ERR_SI(write(dset->fd, &dset->last, sizeof(int)));
dset->pos=sizeof(int);
#ifdef __VMS
close(dset->fd);
DlogOpen(dset, dset->name, 1);
#endif
return(0);
OnError:
return(-1);
}
int DlogClose(DlogSet *dset) {
ERR_I(DlogUpd(dset));
close(dset->fd);
return(0);
OnError:
return(-1);
}

97
tecs/geterrno.c Normal file
View File

@ -0,0 +1,97 @@
#define ident "1B01"
#ifdef VAXC
#module GetErrno ident
#endif
#ifdef __DECC
#pragma module GetErrno ident
#pragma nostandard
#endif
/*
** +--------------------------------------------------------------+
** | Paul Scherrer Institute |
** | Computing Section |
** | |
** | This software may be used freely by non-profit organizations.|
** | It may be copied provided that the name of P.S.I. and of the |
** | author is included. Neither P.S.I. nor the author assume any |
** | responsibility for the use of this software outside of P.S.I.|
** +--------------------------------------------------------------+
**
** Module Name . . . . . . . . : [...LIB.SINQ]GETERRNO.C
**
** Author . . . . . . . . . . : D. Maden
** Date of creation . . . . . . : Nov 1995
**
** To compile this module, use:
$ import tasmad
$ define/group sinq_c_tlb mad_lib:sinq_c.tlb
$ cc /debug /noopt /obj=[]GetErrno -
tasmad_disk:[mad.lib.sinq]GetErrno +
sinq_c_tlb/lib
** To include this module in SINQ.OLB, use:
$ import tasmad
$ define/group sinq_c_tlb mad_lib:sinq_c.tlb
$
$ define/group sinq_olb mad_lib:sinq_dbg.olb
$ @tasmad_disk:[mad.lib.sinq]sinq_olb GetErrno debug
$
$ define/group sinq_olb mad_lib:sinq.olb
$ @tasmad_disk:[mad.lib.sinq]sinq_olb GetErrno
**
** Updates:
** 1A01 2-Nov-1995 DM. Initial version.
** 1B01 21-Mar-1996 DM. Move from DELTAT.OLB to SINQ.OLB.
**============================================================================
** The following entry points are included:
**
#include <sinq_prototypes.h>
void GetErrno (int *his_errno, int *his_vaxc_errno)
** --------
** Input Args:
** none
** Output Args:
** his_errno - value of "errno".
** his_vaxc_errno - on VMS systems only, value of "vaxc$errno". Otherwise
** set to 1.
** Modified Args:
** none
** Return status:
** none
** Global variables modified:
** none
** Description:
** GetErrno returns a copy of the universal error variable "errno" (and,
** on VMS systems, vaxc$errno) to a local variable supplied by the user.
** This can occasionally be useful when debugging since the debugger on
** VMS can't easily examine them.
**============================================================================
** Global Definitions
*/
#ifdef VAXC
#include errno
#else
#include <errno.h>
#endif
/*--------------------------------------------------------------------------
** Global Variables
*/
/*--------------------------------------------------------------------------
** GetErrno: Make copies of errno and vaxc$errno for debug.
*/
void GetErrno (int *his_errno, int *his_vaxc_errno) {
/* ========
*/
*his_errno = errno; /* Make copy of errno */
#ifdef __VMS
*his_vaxc_errno = vaxc$errno; /* Make copy of vaxc$errno */
#else
*his_vaxc_errno = 1;
#endif
return;
}
/*------------------------------------------------- End of GETERRNO.C =======*/

191
tecs/logfile.c Normal file
View File

@ -0,0 +1,191 @@
/* switch off ANSI_C_SOURCE (allows compiling all sources with cc/stand=ansi) */
#ifdef __VMS
#ifdef __HIDE_FORBIDDEN_NAMES
#undef __HIDE_FORBIDDEN_NAMES
#endif
#endif
#include <sys/time.h>
#include <sys/timeb.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "logfile.h"
#include "errhdl.h"
#include "util.h"
static FILE *fil=NULL;
static char lnam[256]="", filnam[256]="";
static char ebuf[20000]="";
static char *eptr=&ebuf[0];
static int lastStamp=0;
static int notDated=0;
int logfileStd=0;
int dirty=0;
void logfileOpen(int first) {
struct tm *tim;
struct timeb btim;
dirty=1;
if (logfileStd) {
fil=stdout;
return;
}
assert(fil==NULL);
if (first) {
ftime(&btim);
tim=localtime(&btim.time);
if (notDated) {
if (lnam[0]=='\0') {
str_copy(filnam, "test.log");
} else {
str_copy(filnam, lnam);
str_append(filnam, ".log");
}
} else {
sprintf(filnam, "%s%04d-%02d-%02d.log", lnam, tim->tm_year+1900, tim->tm_mon+1, tim->tm_mday);
}
}
#ifdef __VMS
if (first && notDated) {
fil=fopen(filnam, "w", "SHR=UPD"); /* new version at restart */
} else {
fil=fopen(filnam, "a", "SHR=UPD");
}
#else
if (first && notDated) {
fil=fopen(filnam, "w"); /* overwrite at restart */
} else {
fil=fopen(filnam, "a");
}
#endif
}
void logfileInit(char *path, int nodate) {
str_copy(lnam, path);
lastStamp=-1;
notDated=nodate;
logfileOpen(1);
}
void logfileStamp(char *text)
{ struct tm *tim;
struct timeb btim;
int stamp;
ftime(&btim);
tim=localtime(&btim.time);
stamp=tim->tm_hour*60+tim->tm_min;
if (stamp!=lastStamp) {
if (stamp<lastStamp) { /* time smaller than last time -> new day -> new logfile */
if (fil!=NULL) { fclose(fil); fil=NULL; }
logfileOpen(0);
} else {
if (!dirty) return;
#ifdef __VMS
if (fil==NULL) logfileOpen(0);
#endif
}
lastStamp=stamp;
fprintf(fil, "%02d:%02d %s\n",tim->tm_hour,tim->tm_min,text);
dirty=0;
}
#ifdef __VMS
if (!logfileStd) { fclose(fil); fil=NULL; }
#endif
}
void logfileUpd()
{
#ifdef __VMS
if (!logfileStd) { fclose(fil); fil=NULL; }
#else
fflush(fil);
#endif
}
void logfileOutTmp(char *fmt, ...)
{ va_list ap;
va_start(ap, fmt);
if (eptr!=NULL) {
if (eptr-ebuf > sizeof(ebuf)-512) {
sprintf(eptr, "... buffer full ... \n");
eptr=NULL;
} else {
vsprintf(eptr, fmt, ap);
eptr+=strlen(eptr);
}
}
va_end(ap);
}
void logfileOut(char *fmt, ...)
{ va_list ap;
if (!dirty) { dirty=1; logfileStamp(""); }
dirty=1;
#ifdef __VMS
if (fil==NULL) logfileOpen(0);
#endif
va_start(ap, fmt);
vfprintf(fil, fmt, ap);
va_end(ap);
}
void logfilePurge() {
ebuf[0]='\0';
eptr=&ebuf[0];
}
void logfileWriteTmp() {
if (!dirty) { dirty=1; logfileStamp(""); }
dirty=1;
#ifdef __VMS
if (fil==NULL) logfileOpen(0);
#endif
if (ebuf[0]!='\0') fprintf(fil, "%s", ebuf);
ebuf[0]='\0';
eptr=&ebuf[0];
}
void logfileShowErr(char *text)
{
logfileWriteTmp();
ErrWrite(fil, text);
}
void logfileClose()
{ if (fil!=NULL) { fclose(fil); fil=NULL; }
}
void logfileOutBuf(buf_type *buf)
{ char *obuf, *str;
int osiz;
obuf=buf->buf;
osiz=buf->size;
while (buf->size>0) {
if (buf->buf==NULL) {
logfileOutTmp(" ?"); break;
} else if (buf->buf[0]==1) {
logfileOutTmp(" %d", buf_get_int(buf));
} else if (buf->buf[0]==2) {
logfileOutTmp(" %f", buf_get_float(buf));
} else {
str=buf_get_str(buf);
if (*str=='\0') {
logfileOutTmp(" .");
} else {
logfileOutTmp(" %s", str);
}
}
}
buf->size=osiz;
buf->buf=obuf;
}

103
tecs/lsc.c Normal file
View File

@ -0,0 +1,103 @@
#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);
}

99
tecs/serutil.c Normal file
View File

@ -0,0 +1,99 @@
#include <stdlib.h>
#include <string.h>
#include "rs232c_def.h"
#include "asynsrv_def.h"
#include "sinq_prototypes.h"
#include "errhdl.h"
#include "serutil.h"
#include "logfile.h"
#include "util.h"
#define A_CHK(R) if (1!=(R)) { SerA_error(); ErrTxt(#R,0); goto OnError; }
void (*idleHandler)(int,int);
struct SerChan {
struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */
struct RS__MsgStruct to_host;
struct RS__RespStruct from_host;
int logIt;
char cmd[SER_BUF_LEN];
};
void SerA_error() {
char *a_txt;
int a_cod, a_my, a_vms;
AsynSrv_ErrInfo(&a_txt, &a_cod, &a_my, &a_vms);
ErrMsg("asynsrv error"); ErrTxt(a_txt,0);
}
SerChannel *SerOpen(const char *host, int logIt, int msecTmo, void (*idleHdl)(int,int)) {
struct SerChan *ser;
char hbuf[64];
char *p, *c;
int port, chan;
idleHandler=idleHdl;
ser=calloc(1, sizeof(*ser));
str_copy(hbuf, host);
p=str_split1(hbuf, ':');
str_copy(ser->asyn_info.host, hbuf);
port=4000;
chan=0;
if (p!=NULL) {
c=str_split1(p, '/');
if (c!=NULL) chan=atoi(c);
port=atoi(p);
}
ser->asyn_info.port=port;
ser->asyn_info.chan=chan;
ser->logIt=logIt;
A_CHK(AsynSrv_Open(&ser->asyn_info));
if (msecTmo==0) msecTmo=5000;
A_CHK(AsynSrv_Config(&ser->asyn_info, "msecTmo", msecTmo, "idleHdl", idleHdl, NULL));
return((SerChannel *)ser);
OnError: return(NULL);
}
int SerClose(SerChannel *serch) {
struct SerChan *ser;
ser=(struct SerChan *)serch;
A_CHK(AsynSrv_Close(&ser->asyn_info, 0));
return(0);
OnError: return(-1);
}
char *SerCmdC(SerChannel *serch, const char *cmnd) {
char cmd[SER_BUF_LEN];
int l;
l=strlen(cmnd);
if (l>=SER_BUF_LEN-1) ERR_COD(ENOBUFS);
strcpy(cmd, cmnd);
return(SerCmd(serch, cmd));
OnError: return(NULL);
}
char *SerCmd(SerChannel *serch, char *cmnd) {
int l;
struct SerChan *ser;
char *result;
l=strlen(cmnd);
if (l>=SER_BUF_LEN-1) ERR_COD(ENOBUFS);
ser=(struct SerChan *)serch;
if (ser->logIt) logfileOutTmp(">%s\n", cmnd);
cmnd[l]=ser->asyn_info.eot[1];
cmnd[l+1]='\0';
A_CHK(AsynSrv_SendCmnds(&ser->asyn_info, &ser->to_host, &ser->from_host, cmnd, NULL));
result=AsynSrv_GetReply(&ser->asyn_info, &ser->from_host, NULL);
if (result==NULL) ERR_MSG("empty result");
/* if (idleHandler!=NULL) idleHandler(50,0); */
if (ser->logIt) logfileOutTmp("<%s\n", result);
return(result);
OnError: return(NULL);
}

142
tecs/strjoin.c Normal file
View File

@ -0,0 +1,142 @@
#define ident "1B03"
#ifdef VAXC
#module StrJoin ident
#endif
#ifdef __DECC
#pragma module StrJoin ident
#endif
/*
** +--------------------------------------------------------------+
** | Paul Scherrer Institute |
** | Department ASQ |
** | |
** | This software may be used freely by non-profit organizations.|
** | It may be copied provided that the name of P.S.I. and of the |
** | author is included. Neither P.S.I. nor the author assume any |
** | responsibility for the use of this software outside of P.S.I.|
** +--------------------------------------------------------------+
**
** Module Name . . . . . . . . : [...LIB.SINQ]STRJOIN.C
**
** Author . . . . . . . . . . : D. Maden
** Date of creation . . . . . . : Nov 1995
**
** To compile this module, use:
$ import tasmad
$ define/group sinq_c_tlb mad_lib:sinq_c.tlb
$ cc /debug /noopt /obj=[]StrEdit -
tasmad_disk:[mad.lib.sinq]StrEdit +
sinq_c_tlb/lib
** To include this module in SINQ.OLB, use:
$ import tasmad
$ define/group sinq_c_tlb mad_lib:sinq_c.tlb
$
$ define/group sinq_olb mad_lib:sinq_dbg.olb
$ @tasmad_disk:[mad.lib.sinq]sinq_olb StrEdit debug
$
$ define/group sinq_olb mad_lib:sinq.olb
$ @tasmad_disk:[mad.lib.sinq]sinq_olb StrEdit
**
** Updates:
** 1A01 2-Nov-1995 DM. Initial version.
** 1B01 21-Mar-1996 DM. Move from DELTAT.OLB to SINQ.OLB.
** 1B03 28-May-1997 DM. Allow result string to be either of source
** strings.
**============================================================================
** The following entry points are included in this module:
**
**-------------------------------------------------------------------------
** #include <sinq_prototypes.h>
**
** char *StrJoin (&result, result_size, &str_a, &str_b)
** -------
** Input Args:
** int result_size - max size of "result". The resultant string will
** have a max length of (result_size - 1) to allow
** for the zero terminator
** char *str_a - Pointer to first string to be joined.
** char *str_b - Pointer to second string to be joined.
** Output Args:
** char *result - Pointer to resulting string.
** Modified Args:
** none
** Return value:
** Pointer to resulting string.
** Global variables modified:
** none
** Routines called:
** None
** Description:
** The routine joins 2 strings, checking for total string length and
** ensuring the result will be zero terminated. The "result" arg may be
** the same as "str_a" or "str_b".
**-------------------------------------------------------------------------
** Global Definitions
*/
#include <string.h>
#define NIL '\0'
/*
**====================================================================
*/
/*
**====================================================================
** StrJoin - join 2 strings.
** Note: strncat is used exclusively rather than
** strncpy to be sure result is always
** null terminated.
*/
char *StrJoin (
/* =======
*/ char *result,
int result_size,
char *str_a,
char *str_b) {
int i, size, size_a, size_b;
size = result_size - 1;
if (size < 0) return result;
if (result == str_a) { /* Are the result and str_a the same? */
size_a = strlen (str_a); /* Yes */
if (size_a > size) { /* Check sizes anyway. */
result[size] = NIL; /* Truncate str_a. No room for str_b! */
}else {
size = size - strlen (result); /* And append str_b */
if (size > 0) {
strncat (result, str_b, size);
}
}
}else if (result == str_b) { /* Are the result and str_b the same? */
size_a = strlen (str_a); /* Yes, this is a bit complicated! */
size_b = strlen (str_b);
if (size_a >= size) { /* If str_a completely fills result, .. */
result[0] = NIL; /* .. then just copy in str_a */
strncat (result, str_a, size);
}else {
/*
** Otherwise, str_b must first be moved to
** make room for str_a and then str_a must
** be put at the front of the result.
*/
if ((size_a + size_b) > size) size_b = size - size_a;
result[size_a+size_b] = NIL;
for (i = (size_b-1); i >= 0; i--) {
result[size_a+i] = str_b[i];
}
memcpy (result, str_a, size_a);
}
}else { /* Result is neither str_a nor str_b so .. */
result[0] = NIL; /* .. str_a needs to be copied */
strncat (result, str_a, size);
size = size - strlen (result); /* And str_a appended */
if (size > 0) strncat (result, str_b, size);
}
return result;
}
/*-------------------------------------------------- End of STRJOIN.C =======*/

View File

@ -1,27 +1,22 @@
#include "errhdl.h"
#include "client.h"
#include "tecc.h"
char command[80], response[80], device[80];
float tempX, tempP, tempC;
static char device[80], command[80];
static int quit;
static float tempX, tempP, tempC;
pTecsClient TeccInit(char *server) {
pTecsClient TeccInit(char *startcmd, int port) {
CocConn *conn;
char buf[80];
ERR_SP(conn=(CocConn *)malloc(sizeof(*conn)));
#ifdef __VMS
sprintf(buf, "@start_tecs %s", server);
#else
sprintf(buf, "start_tecs %s", server);
#endif
CocInitClient(conn, "", "#rwacs", 0, buf);
ERR_I(CocInitClient(conn, "", port, "#rwacs", 0, startcmd));
CocDefFlt(tempX, CocRD);
CocDefFlt(tempP, CocRD);
CocDefFlt(tempC, CocWR);
CocDefStr(device, CocWR);
CocDefStr(command, CocWR);
CocDefStr(response, CocWR);
CocDefInt(quit, CocWR);
CocDefCmd(command);
return((pTecsClient)conn);
OnError: return(NULL);
}
@ -39,13 +34,6 @@ char *TeccGetDev(pTecsClient conn) {
OnError: return(NULL);
}
int TeccGet(pTecsClient conn, float *temp) {
ERR_I(CocCmd((CocConn *)conn, "tempP"));
*temp=tempP;
return(0);
OnError: return(-1);
}
int TeccGet3(pTecsClient conn, float temp[3]) {
ERR_I(CocCmd((CocConn *)conn, "tempC,tempX,tempP"));
temp[0]=tempC;
@ -55,6 +43,14 @@ int TeccGet3(pTecsClient conn, float temp[3]) {
OnError: return(-1);
}
int TeccGet(pTecsClient conn, float *temp) {
float t[3];
ERR_I(TeccGet3(conn, t));
*temp=t[2];
return(0);
OnError: return(-1);
}
int TeccSet(pTecsClient conn, float temp) {
tempC=temp;
ERR_I(CocCmd((CocConn *)conn, "[tempC]"));
@ -63,17 +59,30 @@ int TeccSet(pTecsClient conn, float temp) {
}
int TeccSend(pTecsClient conn, char *cmd, char *reply, int replyLen) {
command[0]='\0';
while (0!=strcmp(command, cmd)) {
char *res;
int cnt;
str_copy(command, cmd);
strcpy(response,"<none>");
ERR_I(CocCmd((CocConn *)conn, "[command,response]"));
while (0==strcmp(response,"<none>") && 0==strcmp(command, cmd)) {
ERR_I(CocCmd((CocConn *)conn, "[$]"));
cnt=40;
util_delay(100);
while (cnt>0) {
ERR_I(CocCmd((CocConn *)conn, "$"));
if (command[0]!='\0') {
str_ncpy(reply, command, replyLen);
return(0);
}
util_delay(250);
ERR_I(CocCmd((CocConn *)conn, "command,response"));
cnt--;
}
}
str_ncpy(reply, response, replyLen);
str_ncpy(reply, "<no response>", replyLen);
return(0);
OnError: return(-1);
}
int TeccQuitServer(pTecsClient conn) {
quit=1;
ERR_I(CocCmd((CocConn *)conn, "quit"));
return(0);
OnError: return(-1);
}
@ -119,7 +128,7 @@ int TeccSendVms(pTecsClient conn, char **cmd, char **reply) {
strncpy(cbuf, cmd[1], l);
while (l>0 && cbuf[l-1]==' ') l--; /* trim */
cbuf[l]='\0';
TeccSend(conn, cbuf, rbuf, sizeof(rbuf));
ERR_I(TeccSend(conn, cbuf, rbuf, sizeof(rbuf)));
lr=strlen(rbuf);
l=*(short *)reply;
if (lr>=l) lr=l;

View File

@ -163,10 +163,20 @@ int str_cmp(const char *str1, const char *str2) {
return(0);
}
int str_ncpy(char *dst, const char *src, int n) {
strncpy(dst, src, n);
if (dst[n-1]!='\0') {
dst[n-1]='\0';
int str_ncpy(char *dst, const char *src, int maxdest) {
strncpy(dst, src, maxdest);
if (dst[maxdest-1]!='\0') {
dst[maxdest-1]='\0';
ERR_MSG("destination string too short");
}
return(0);
OnError: return(-1);
}
int str_ncat(char *dst, const char *src, int maxdest) {
strncat(dst, src, maxdest-strlen(dst)-1);
if (dst[maxdest-1]!='\0') {
dst[maxdest-1]='\0';
ERR_MSG("destination string too short");
}
return(0);

View File

@ -40,6 +40,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
#include <tcl.h>
#include "fortify.h"
#include "conman.h"
@ -57,11 +58,10 @@
#include "servlog.h"
#include "sicsvar.h"
#include "tecs/tecc.h"
#include "tecs/util.h"
#include "tecs/errhdl.h"
extern pServer pServ;
typedef struct __EVDriver *pEVDriver;
#include "evdriver.i"
/*------------------------- The Driver ------------------------------------*/
@ -72,11 +72,14 @@
/*-----------------------------------------------------------------------*/
typedef struct {
void *pData;
char server[64];
int iLastError;
char *lastError;
int iLastError, port;
char server[256];
} TecsDriv, *pTecsDriv;
static time_t lastGet=0;
/*-------------------------------------------------------------------------*/
int TecsWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[])
@ -151,11 +154,19 @@
{
pTecsDriv pMe = NULL;
int iRet;
time_t now;
assert(self);
pMe = (pTecsDriv)self->pPrivate;
assert(pMe);
time(&now);
if (now>lastGet) {
lastGet=now;
} else {
SicsWait(1); /* avoid extensive network traffic */
}
/* get temperature */
iRet = TeccGet(pMe->pData, fPos);
if(iRet < 0 )
@ -178,8 +189,7 @@
/* set temperature */
iRet = TeccSet(pMe->pData, fVal);
if(iRet != 1)
{
if(iRet < 0) {
pMe->iLastError=1; /* severe */
pMe->lastError = ErrMessage;
return 0;
@ -196,7 +206,7 @@
assert(pMe);
*iCode = pMe->iLastError;
error=pMe->lastError;
str_ncpy(error, pMe->lastError, iErrLen);
return 1;
}
/*---------------------------------------------------------------------------*/
@ -243,7 +253,7 @@
pMe = (pTecsDriv )self->pPrivate;
assert(pMe);
pMe->pData = TeccInit(pMe->server);
pMe->pData = TeccInit(pMe->server, pMe->port);
if(pMe->pData==NULL)
{
pMe->iLastError = 1; /* severe */
@ -262,7 +272,7 @@
pMe = (pTecsDriv )self->pPrivate;
assert(pMe);
TeccClose(&pMe->pData);
TeccClose(pMe->pData);
pMe->pData=NULL;
return 1;
}
@ -288,6 +298,7 @@
pEVDriver pNew = NULL;
pTecsDriv pMe = NULL;
pSicsVariable pInst = NULL;
char *pStart = NULL, *pBin=NULL, *pLog=NULL, *pPort=NULL;
pNew = CreateEVDriver(argc,argv);
pMe = (pTecsDriv)malloc(sizeof(TecsDriv));
@ -302,12 +313,45 @@
/* initalise pTecsDriver */
pMe->lastError = NULL;
/* get the start server option */
pStart = IFindOption(pSICSOptions, "TecsStartCmd");
if (pStart==NULL) return(NULL);
str_copy(pMe->server, pStart);
/* get the port number for tecs */
pMe->port=0;
pPort = IFindOption(pSICSOptions, "TecsPort");
if (pPort!=NULL) {
pMe->port=atoi(pPort);
}
if (pMe->port==0) {
pPort="9750";
pMe->port=atoi(pPort);
}
str_append(pMe->server, " -p ");
str_append(pMe->server, pPort);
/* get the instrument name */
pInst = FindVariable(pServ->pSics,"instrument");
if (pInst==NULL ||
pInst->text==NULL ||
strlen(pInst->text)>sizeof(pMe->server)-6) return NULL;
sprintf(pMe->server,"tecs_%s", pInst->text);
str_append(pMe->server, " -n tecs_");
str_append(pMe->server, pInst->text);
/* add binDir option if present */
pBin = IFindOption(pSICSOptions, "TecsBinDir");
if (pBin!=NULL) {
str_append(pMe->server, " -b ");
str_append(pMe->server, pBin);
}
/* add logDir option if present */
pLog = IFindOption(pSICSOptions, "TecsLogDir");
if (pLog!=NULL) {
str_append(pMe->server, " -d ");
str_append(pMe->server, pLog);
}
str_append(pMe->server, " &");
/* initialise function pointers */
pNew->SetValue = TecsRun;