Merge branch 'develop' into moreremo
This commit is contained in:
280
conman.c
280
conman.c
@ -3,7 +3,7 @@
|
||||
Connection management for SICS. This is one the core files for
|
||||
SICS. Does a lot. See the descriptions with individual functions
|
||||
below.
|
||||
|
||||
|
||||
|
||||
Mark Koennecke, October 1996
|
||||
|
||||
@ -73,6 +73,7 @@
|
||||
#include "statusfile.h"
|
||||
#include "sicshipadaba.h"
|
||||
#include "protocol.h"
|
||||
#include "sicsvar.h"
|
||||
/*
|
||||
#define UUDEB 1
|
||||
define UUDEB , for buffer writing for checking encoding */
|
||||
@ -81,6 +82,13 @@ extern pServer pServ;
|
||||
|
||||
#include "outcode.c" /* text for OutCode */
|
||||
|
||||
int KillCapture(SConnection * pCon);
|
||||
|
||||
int LogCapture(SConnection * pCon, SicsInterp * pInter, void *pData,
|
||||
int argc, char *argv[]);
|
||||
|
||||
int LogOutput(SConnection * pCon, SicsInterp * pInter, void *pData,
|
||||
int argc, char *argv[]);
|
||||
|
||||
/*------ Max Size of Command Stack */
|
||||
#define MAXSTACK 1024
|
||||
@ -108,6 +116,24 @@ struct SCStore {
|
||||
long macroStack;
|
||||
commandContext cc;
|
||||
};
|
||||
|
||||
/*
|
||||
* This will return the value of a SICS int variable called "sicsdebug"
|
||||
* If sicsdebug is not defined it will return 1
|
||||
* TODO Maybe define debugging levels.
|
||||
*
|
||||
* return 0=debug off, 1=debug on or sicsdebug not defined.
|
||||
*/
|
||||
int sicsdebug() {
|
||||
pSicsVariable debug;
|
||||
|
||||
debug = FindVariable(pServ->pSics, "sicsdebug");
|
||||
if (debug) {
|
||||
return debug->iVal;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/*===========================================================================*/
|
||||
static char *ConName(long ident)
|
||||
{
|
||||
@ -652,7 +678,7 @@ int SCPrintf(SConnection * self, int iOut, char *fmt, ...)
|
||||
va_list ap;
|
||||
char buf[256];
|
||||
char *dyn;
|
||||
int l;
|
||||
unsigned int l;
|
||||
int res;
|
||||
|
||||
va_start(ap, fmt);
|
||||
@ -781,6 +807,64 @@ static int testAndWriteSocket(SConnection * pCon, char *buffer, int iOut)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int isOK(const char *buffer)
|
||||
{
|
||||
if ((buffer[0] == 'O' && buffer[1] == 'K')
|
||||
&& (buffer[2] == '\0' || buffer[2] == '\r' || buffer[2] == '\n'))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static void testAndWriteSICSLog(SConnection * self, char *buffer, int iOut)
|
||||
{
|
||||
char tp;
|
||||
/* We don't want to log the "OK" messages */
|
||||
if (isOK(buffer))
|
||||
return;
|
||||
|
||||
if (SCinMacro(self)) {
|
||||
tp = 'M';
|
||||
if (sicsdebug())
|
||||
tp = 'D';
|
||||
} else
|
||||
tp = 'N';
|
||||
switch (iOut) {
|
||||
case eStatus:
|
||||
case eStart:
|
||||
case eFinish:
|
||||
case eEvent:
|
||||
case eHdbValue:
|
||||
case eHdbEvent:
|
||||
case eLog:
|
||||
case eLogError:
|
||||
case eError:
|
||||
case eWarning:
|
||||
/* Log it */
|
||||
SICSLogWriteCode(buffer, iOut, tp);
|
||||
return;
|
||||
case eValue:
|
||||
if (sicsdebug() || !SCinMacro(self)) {
|
||||
/* Log it */
|
||||
SICSLogWriteCode(buffer, iOut, tp);
|
||||
return;
|
||||
} else {
|
||||
/* Suppressed */
|
||||
#if 0
|
||||
tp = tolower(tp);
|
||||
SICSLogWriteCode(buffer, iOut, tp);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
printf("Unrecognized ouput code %d in testAndWriteSICSLog: FIXME!!!\n", iOut);
|
||||
SICSLogWriteCode(buffer, iOut, tp);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
int SCNormalWrite(SConnection * self, char *buffer, int iOut)
|
||||
{
|
||||
@ -795,7 +879,11 @@ int SCNormalWrite(SConnection * self, char *buffer, int iOut)
|
||||
}
|
||||
|
||||
/* log it for any case */
|
||||
SICSLogWrite(buffer, iOut);
|
||||
#if 0
|
||||
SICSLogWrite(buffer, iOut);
|
||||
#else
|
||||
testAndWriteSICSLog(self, buffer, iOut);
|
||||
#endif
|
||||
|
||||
testAndWriteCommandLog(self, buffer, iOut);
|
||||
|
||||
@ -2144,6 +2232,190 @@ int ConSicsAction(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void hookFunc(const char *pText, OutCode eOut, void*pData)
|
||||
{
|
||||
SConnection *pCon = (SConnection *) pData;
|
||||
int text_len = strlen(pText);
|
||||
char txt[5];
|
||||
|
||||
if (!VerifyConnection(pCon)) {
|
||||
return;
|
||||
}
|
||||
if (!ANETvalidHandle(pCon->sockHandle)) {
|
||||
return;
|
||||
}
|
||||
snprintf(txt, 5, "%3s:", OutCodeToTxt(eOut));
|
||||
ANETwrite(pCon->sockHandle, txt, 4);
|
||||
ANETwrite(pCon->sockHandle, (char *)pText, text_len);
|
||||
if (pText[text_len - 1] != '\n')
|
||||
ANETwrite(pCon->sockHandle, "\n", 1);
|
||||
}
|
||||
|
||||
int KillCapture(SConnection * pCon)
|
||||
{
|
||||
RemSICSLogHook(pCon);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------
|
||||
the command function:
|
||||
Syntax:
|
||||
Kill kills all logging
|
||||
Log OutCode starts loggin OutCode events
|
||||
All starts logging all events
|
||||
-------------------------------------------------------------------------- */
|
||||
|
||||
int LogCapture(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
SConnection * pConMaster;
|
||||
char pBueffel[512];
|
||||
int i;
|
||||
|
||||
pConMaster = SCfindMaster(pCon);
|
||||
if (pConMaster == NULL)
|
||||
return 0;
|
||||
/* check no af args */
|
||||
if (argc < 2) {
|
||||
snprintf(pBueffel,sizeof(pBueffel)-1, "Insufficient number of arguments to %s", argv[0]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
argtolower(argc, argv);
|
||||
|
||||
/* Branch according to argv[1] */
|
||||
if (strcmp(argv[1], "kill") == 0 || strcmp(argv[1], "none") == 0) {
|
||||
SCPrintf(pCon, eLog, "getlog %s", argv[1]);
|
||||
KillCapture(pConMaster);
|
||||
return 1;
|
||||
} else if (strcmp(argv[1], "what") == 0) {
|
||||
unsigned int code_bits = 0;
|
||||
pDynString buffer;
|
||||
code_bits = (1 << iNoCodes) - 1;
|
||||
buffer = CreateDynString(100, 100);
|
||||
for (i = 0; i < iNoCodes; ++i) {
|
||||
if (code_bits & (1 << i)) {
|
||||
if (GetDynStringLength(buffer) > 0)
|
||||
DynStringConcatChar(buffer, ',');
|
||||
DynStringConcat(buffer, (char *)OutCodeToTxt(i));
|
||||
}
|
||||
}
|
||||
SCPrintf(pCon, eLog, "getlog %s", GetCharArray(buffer));
|
||||
DeleteDynString(buffer);
|
||||
return 1;
|
||||
} else if (strcmp(argv[1], "list") == 0) {
|
||||
unsigned int code_bits = 0;
|
||||
pDynString buffer;
|
||||
code_bits = GetSICSLogHook(pConMaster);
|
||||
if (code_bits == 0) {
|
||||
SCPrintf(pCon, eLog, "getlog none");
|
||||
return 1;
|
||||
}
|
||||
buffer = CreateDynString(100, 100);
|
||||
for (i = 0; i < iNoCodes; ++i) {
|
||||
if (code_bits & (1 << i)) {
|
||||
if (GetDynStringLength(buffer) > 0)
|
||||
DynStringConcatChar(buffer, ',');
|
||||
DynStringConcat(buffer, (char *)OutCodeToTxt(i));
|
||||
}
|
||||
}
|
||||
SCPrintf(pCon, eLog, "getlog %s", GetCharArray(buffer));
|
||||
DeleteDynString(buffer);
|
||||
return 1;
|
||||
} else if (strcmp(argv[1], "all") == 0) {
|
||||
SCPrintf(pCon, eLog, "getlog all");
|
||||
AddSICSLogHook(hookFunc, "all", pConMaster);
|
||||
return 1;
|
||||
} else if (argc == 2) {
|
||||
/* must be outcode, try find it */
|
||||
SCPrintf(pCon, eLog, "getlog %s", argv[1]);
|
||||
AddSICSLogHook(hookFunc, argv[1], pConMaster);
|
||||
return 1;
|
||||
} else {
|
||||
/* make it a list */
|
||||
int i;
|
||||
size_t len;
|
||||
char *pBuff;
|
||||
for (i = 1, len = 0; i < argc; ++i)
|
||||
len += strlen(argv[i]) + 1;
|
||||
if (len > sizeof(pBueffel))
|
||||
pBuff = malloc(len);
|
||||
else
|
||||
pBuff = pBueffel;
|
||||
if (pBuff == NULL) {
|
||||
SCWrite(pCon, "Out of memory in LogCapture\n", eError);
|
||||
return 1;
|
||||
}
|
||||
for (i = 1, len = 0; i < argc; ++i) {
|
||||
if (i > 1)
|
||||
pBuff[len++] = ',';
|
||||
strcpy(&pBuff[len], argv[i]);
|
||||
len += strlen(argv[i]);
|
||||
}
|
||||
SCPrintf(pCon, eLog, "getlog %s", pBuff);
|
||||
AddSICSLogHook(hookFunc, pBuff, pConMaster);
|
||||
if (pBuff != pBueffel)
|
||||
free(pBuff);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* ------------------------------------------------------------------------
|
||||
the command function:
|
||||
Syntax:
|
||||
LogOutput [OutCode] <text>
|
||||
Logs <text> with outcode OutCode default eLog
|
||||
-------------------------------------------------------------------------- */
|
||||
|
||||
int LogOutput(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
char pBueffel[512];
|
||||
char *pBuff;
|
||||
int i, result, start;
|
||||
size_t len;
|
||||
OutCode outcode;
|
||||
|
||||
/* check no af args */
|
||||
if (argc < 2) {
|
||||
snprintf(pBueffel,sizeof(pBueffel)-1, "Insufficient number of arguments to %s", argv[0]);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* assume default eLog unless told otherwise */
|
||||
start = 1;
|
||||
outcode = eLog;
|
||||
if (argv[1][0] == '@') {
|
||||
result = OutCodeFromText(&argv[1][1], &outcode);
|
||||
if (result >= 0) {
|
||||
start = 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* make it a string */
|
||||
for (i = start, len = 0; i < argc; ++i)
|
||||
len += strlen(argv[i]) + 1;
|
||||
if (len > sizeof(pBueffel))
|
||||
pBuff = malloc(len+10);
|
||||
else
|
||||
pBuff = pBueffel;
|
||||
if (pBuff == NULL) {
|
||||
SCWrite(pCon, "Out of memory in LogOutput\n", eError);
|
||||
return 1;
|
||||
}
|
||||
for (i = start, len = 0; i < argc; ++i) {
|
||||
if (i > start)
|
||||
pBuff[len++] = ' ';
|
||||
strcpy(&pBuff[len], argv[i]);
|
||||
len += strlen(argv[i]);
|
||||
}
|
||||
SICSLogWrite(pBuff, outcode);
|
||||
if (pBuff != pBueffel)
|
||||
free(pBuff);
|
||||
return 1;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
int SCTaskFunction(void *pData)
|
||||
{
|
||||
@ -2214,7 +2486,7 @@ int SCTaskFunction(void *pData)
|
||||
pPassword = strtok(NULL, " \t\r\n");
|
||||
iRet = IsValidUser(pUser, pPassword);
|
||||
if (iRet >= 0) {
|
||||
SCWrite(self, "Login OK", eError);
|
||||
SCWrite(self, "Login OK", eLog);
|
||||
self->iLogin = 1;
|
||||
SCSetRights(self, iRet);
|
||||
pHost[0] = '\0';
|
||||
|
Reference in New Issue
Block a user