- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
443
statusfile.c
443
statusfile.c
@@ -53,244 +53,247 @@
|
||||
#include "exebuf.h"
|
||||
|
||||
static int parameterChange = 0;
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int StatusFileTask(void *data) {
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int StatusFileTask(void *data)
|
||||
{
|
||||
char *pFile = NULL;
|
||||
|
||||
if (parameterChange) {
|
||||
parameterChange = 0;
|
||||
|
||||
assert(pServ->pSics);
|
||||
pFile = IFindOption(pSICSOptions,"statusfile");
|
||||
pFile = IFindOption(pSICSOptions, "statusfile");
|
||||
if (pFile) {
|
||||
WriteSicsStatus(pServ->pSics,pFile,0);
|
||||
WriteSicsStatus(pServ->pSics, pFile, 0);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
|
||||
/*---------------------------------------------------------------------*/
|
||||
static int motorSave = 0;
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int BackupStatus(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int iRet;
|
||||
char pBueffel[512];
|
||||
char *pFile = NULL;
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int BackupStatus(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int iRet;
|
||||
char pBueffel[512];
|
||||
char *pFile = NULL;
|
||||
|
||||
assert(pSics);
|
||||
assert(pCon);
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
pFile = IFindOption(pSICSOptions,"statusfile");
|
||||
if(pFile)
|
||||
{
|
||||
iRet = WriteSicsStatus(pSics,pFile,motorSave);
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: No filename given for backup, Aborted.",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
assert(pSics);
|
||||
assert(pCon);
|
||||
|
||||
if (argc < 2) {
|
||||
pFile = IFindOption(pSICSOptions, "statusfile");
|
||||
if (pFile) {
|
||||
iRet = WriteSicsStatus(pSics, pFile, motorSave);
|
||||
} else {
|
||||
SCWrite(pCon, "ERROR: No filename given for backup, Aborted.",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (strcmp(argv[1], "motorSave") == 0) {
|
||||
if (motorSave == 1)
|
||||
motorSave = 0;
|
||||
else
|
||||
{
|
||||
if(strcmp(argv[1],"motorSave") == 0)
|
||||
{
|
||||
if(motorSave== 1)
|
||||
motorSave= 0;
|
||||
else
|
||||
motorSave= 1;
|
||||
sprintf(pBueffel,"New Value of motorSave= %d\n",motorSave);
|
||||
SCWrite(pCon,pBueffel,eValue);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
iRet = WriteSicsStatus(pSics,argv[1],motorSave);
|
||||
}
|
||||
}
|
||||
|
||||
if(!iRet)
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: could not open file %s\n", argv[1]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
motorSave = 1;
|
||||
sprintf(pBueffel, "New Value of motorSave= %d\n", motorSave);
|
||||
SCWrite(pCon, pBueffel, eValue);
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static int restoreOccurred = 0;
|
||||
int hasRestored(){
|
||||
return restoreOccurred;
|
||||
} else {
|
||||
iRet = WriteSicsStatus(pSics, argv[1], motorSave);
|
||||
}
|
||||
}
|
||||
|
||||
if (!iRet) {
|
||||
sprintf(pBueffel, "ERROR: could not open file %s\n", argv[1]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------*/
|
||||
static int restoreOccurred = 0;
|
||||
int hasRestored()
|
||||
{
|
||||
return restoreOccurred;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
pObjectDescriptor pDes;
|
||||
int errList;
|
||||
}RestoreObj, *pRestoreObj;
|
||||
pObjectDescriptor pDes;
|
||||
int errList;
|
||||
} RestoreObj, *pRestoreObj;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void killRestore(void *data){
|
||||
pRestoreObj self = (pRestoreObj)data;
|
||||
if(self == NULL){
|
||||
return;
|
||||
}
|
||||
if(self->errList >= 0){
|
||||
LLDdeleteBlob(self->errList);
|
||||
}
|
||||
if(self->pDes != NULL){
|
||||
DeleteDescriptor(self->pDes);
|
||||
}
|
||||
free(self);
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SaveRestore(void *obj, char *name, FILE *fd){
|
||||
int status;
|
||||
char buffer[1024];
|
||||
|
||||
pRestoreObj self = (pRestoreObj)obj;
|
||||
if(self == NULL){
|
||||
return 0;
|
||||
}
|
||||
fprintf(fd,"\n#--- BEGIN (commands producing errors on last restore)\n");
|
||||
status = LLDnodePtr2First(self->errList);
|
||||
while(status == 1){
|
||||
LLDstringData(self->errList,buffer);
|
||||
fprintf(fd,"%s", buffer);
|
||||
status = LLDnodePtr2Next(self->errList);
|
||||
}
|
||||
fprintf(fd,"#--- END (commands producing errors on last restore)\n\n");
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int InstallBckRestore(SConnection *pCon, SicsInterp *pSics){
|
||||
pRestoreObj pNew = NULL;
|
||||
|
||||
pNew = malloc(sizeof(RestoreObj));
|
||||
if(pNew == NULL){
|
||||
SCWrite(pCon,"ERROR: no memory to create restore object! This is SERIOUS!!!",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNew->pDes = CreateDescriptor("BckRestore");
|
||||
pNew->errList = LLDstringCreate();
|
||||
if(pNew->pDes == NULL || pNew->errList < 0){
|
||||
SCWrite(pCon,"ERROR: no memory to create restore object! This is SERIOUS!!!",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNew->pDes->SaveStatus = SaveRestore;
|
||||
AddCommand(pSics,"Backup",BackupStatus,NULL,NULL);
|
||||
AddCommand(pSics,"Restore",RestoreStatus,killRestore,pNew);
|
||||
|
||||
return 1;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static int listRestoreErr(pRestoreObj self, SConnection *pCon){
|
||||
char buffer[1024];
|
||||
int status;
|
||||
pDynString data = NULL;
|
||||
|
||||
SCStartBuffering(pCon);
|
||||
status = LLDnodePtr2First(self->errList);
|
||||
while(status == 1){
|
||||
LLDstringData(self->errList,buffer);
|
||||
SCWrite(pCon,buffer,eValue);
|
||||
status = LLDnodePtr2Next(self->errList);
|
||||
}
|
||||
data = SCEndBuffering(pCon);
|
||||
if(data != NULL){
|
||||
SCWrite(pCon,GetCharArray(data),eValue);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int RestoreStatus(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
char pBueffel[512];
|
||||
int iRights;
|
||||
int iRet;
|
||||
char *pFile = NULL;
|
||||
writeFunc oldWrite;
|
||||
pExeBuf buffi = NULL;
|
||||
pRestoreObj self = (pRestoreObj)pData;
|
||||
|
||||
assert(pSics);
|
||||
assert(pCon);
|
||||
assert(self != NULL);
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
pFile = IFindOption(pSICSOptions,"statusfile");
|
||||
if(pFile)
|
||||
{
|
||||
sprintf(pBueffel,"%s",pFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: No filename given for backup, Aborted.",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strcasecmp(argv[1],"listerr") == 0){
|
||||
return listRestoreErr(self,pCon);
|
||||
} else if(strcasecmp(argv[1],"killerr") == 0){
|
||||
if(self->errList >= 0){
|
||||
LLDdeleteBlob(self->errList);
|
||||
}
|
||||
self->errList = LLDstringCreate();
|
||||
StatusFileDirty();
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
} else {
|
||||
sprintf(pBueffel,"%s",argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
buffi = exeBufCreate("restore");
|
||||
if(buffi == NULL){
|
||||
SCWrite(pCon,"ERROR: failed to allocate buffer for restore",eError);
|
||||
return 0;
|
||||
}
|
||||
iRet = exeBufLoad(buffi,pBueffel);
|
||||
if(iRet != 1){
|
||||
exeBufDelete(buffi);
|
||||
SCWrite(pCon,"ERROR: failed open status file",eError);
|
||||
return 0;
|
||||
}
|
||||
LLDdeleteBlob(self->errList);
|
||||
self->errList = LLDstringCreate();
|
||||
iRights = SCGetRights(pCon);
|
||||
pCon->iUserRights = usInternal;
|
||||
oldWrite = SCGetWriteFunc(pCon);
|
||||
SCSetWriteFunc(pCon,SCNotWrite);
|
||||
iRet = exeBufProcessErrList(buffi,pSics,pCon,self->errList);
|
||||
restoreOccurred = 1;
|
||||
SCSetWriteFunc(pCon,oldWrite);
|
||||
pCon->iUserRights = iRights;
|
||||
exeBufDelete(buffi);
|
||||
/*
|
||||
if we do not override parameterChange here, the backup file
|
||||
would be overwritten after each restore... Not the right thing
|
||||
to do!
|
||||
*/
|
||||
parameterChange = 0;
|
||||
SCSendOK(pCon);
|
||||
return iRet;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatusFileDirty(void) {
|
||||
parameterChange = 1;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatusFileInit(void) {
|
||||
TaskRegister(pServ->pTasker, StatusFileTask, NULL, NULL, NULL, 0);
|
||||
static void killRestore(void *data)
|
||||
{
|
||||
pRestoreObj self = (pRestoreObj) data;
|
||||
if (self == NULL) {
|
||||
return;
|
||||
}
|
||||
if (self->errList >= 0) {
|
||||
LLDdeleteBlob(self->errList);
|
||||
}
|
||||
if (self->pDes != NULL) {
|
||||
DeleteDescriptor(self->pDes);
|
||||
}
|
||||
free(self);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int SaveRestore(void *obj, char *name, FILE * fd)
|
||||
{
|
||||
int status;
|
||||
char buffer[1024];
|
||||
|
||||
pRestoreObj self = (pRestoreObj) obj;
|
||||
if (self == NULL) {
|
||||
return 0;
|
||||
}
|
||||
fprintf(fd,
|
||||
"\n#--- BEGIN (commands producing errors on last restore)\n");
|
||||
status = LLDnodePtr2First(self->errList);
|
||||
while (status == 1) {
|
||||
LLDstringData(self->errList, buffer);
|
||||
fprintf(fd, "%s", buffer);
|
||||
status = LLDnodePtr2Next(self->errList);
|
||||
}
|
||||
fprintf(fd, "#--- END (commands producing errors on last restore)\n\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int InstallBckRestore(SConnection * pCon, SicsInterp * pSics)
|
||||
{
|
||||
pRestoreObj pNew = NULL;
|
||||
|
||||
pNew = malloc(sizeof(RestoreObj));
|
||||
if (pNew == NULL) {
|
||||
SCWrite(pCon,
|
||||
"ERROR: no memory to create restore object! This is SERIOUS!!!",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNew->pDes = CreateDescriptor("BckRestore");
|
||||
pNew->errList = LLDstringCreate();
|
||||
if (pNew->pDes == NULL || pNew->errList < 0) {
|
||||
SCWrite(pCon,
|
||||
"ERROR: no memory to create restore object! This is SERIOUS!!!",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNew->pDes->SaveStatus = SaveRestore;
|
||||
AddCommand(pSics, "Backup", BackupStatus, NULL, NULL);
|
||||
AddCommand(pSics, "Restore", RestoreStatus, killRestore, pNew);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static int listRestoreErr(pRestoreObj self, SConnection * pCon)
|
||||
{
|
||||
char buffer[1024];
|
||||
int status;
|
||||
pDynString data = NULL;
|
||||
|
||||
SCStartBuffering(pCon);
|
||||
status = LLDnodePtr2First(self->errList);
|
||||
while (status == 1) {
|
||||
LLDstringData(self->errList, buffer);
|
||||
SCWrite(pCon, buffer, eValue);
|
||||
status = LLDnodePtr2Next(self->errList);
|
||||
}
|
||||
data = SCEndBuffering(pCon);
|
||||
if (data != NULL) {
|
||||
SCWrite(pCon, GetCharArray(data), eValue);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int RestoreStatus(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
char pBueffel[512];
|
||||
int iRights;
|
||||
int iRet;
|
||||
char *pFile = NULL;
|
||||
writeFunc oldWrite;
|
||||
pExeBuf buffi = NULL;
|
||||
pRestoreObj self = (pRestoreObj) pData;
|
||||
|
||||
assert(pSics);
|
||||
assert(pCon);
|
||||
assert(self != NULL);
|
||||
|
||||
if (argc < 2) {
|
||||
pFile = IFindOption(pSICSOptions, "statusfile");
|
||||
if (pFile) {
|
||||
sprintf(pBueffel, "%s", pFile);
|
||||
} else {
|
||||
SCWrite(pCon, "ERROR: No filename given for backup, Aborted.",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (strcasecmp(argv[1], "listerr") == 0) {
|
||||
return listRestoreErr(self, pCon);
|
||||
} else if (strcasecmp(argv[1], "killerr") == 0) {
|
||||
if (self->errList >= 0) {
|
||||
LLDdeleteBlob(self->errList);
|
||||
}
|
||||
self->errList = LLDstringCreate();
|
||||
StatusFileDirty();
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
} else {
|
||||
sprintf(pBueffel, "%s", argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
buffi = exeBufCreate("restore");
|
||||
if (buffi == NULL) {
|
||||
SCWrite(pCon, "ERROR: failed to allocate buffer for restore", eError);
|
||||
return 0;
|
||||
}
|
||||
iRet = exeBufLoad(buffi, pBueffel);
|
||||
if (iRet != 1) {
|
||||
exeBufDelete(buffi);
|
||||
SCWrite(pCon, "ERROR: failed open status file", eError);
|
||||
return 0;
|
||||
}
|
||||
LLDdeleteBlob(self->errList);
|
||||
self->errList = LLDstringCreate();
|
||||
iRights = SCGetRights(pCon);
|
||||
pCon->iUserRights = usInternal;
|
||||
oldWrite = SCGetWriteFunc(pCon);
|
||||
SCSetWriteFunc(pCon, SCNotWrite);
|
||||
iRet = exeBufProcessErrList(buffi, pSics, pCon, self->errList);
|
||||
restoreOccurred = 1;
|
||||
SCSetWriteFunc(pCon, oldWrite);
|
||||
pCon->iUserRights = iRights;
|
||||
exeBufDelete(buffi);
|
||||
/*
|
||||
if we do not override parameterChange here, the backup file
|
||||
would be overwritten after each restore... Not the right thing
|
||||
to do!
|
||||
*/
|
||||
parameterChange = 0;
|
||||
SCSendOK(pCon);
|
||||
return iRet;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatusFileDirty(void)
|
||||
{
|
||||
parameterChange = 1;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatusFileInit(void)
|
||||
{
|
||||
TaskRegister(pServ->pTasker, StatusFileTask, NULL, NULL, NULL, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user