- fixed bug and refactored ClientPut

This commit is contained in:
2017-06-14 15:40:43 +02:00
parent 881fa2d79e
commit f92c308c01

88
macro.c
View File

@@ -613,9 +613,9 @@ int InternalFileEval(SConnection * pCon, SicsInterp * pInter, void *pData,
int ClientPut(SConnection * pCon, SicsInterp * pInter, void *pData, int ClientPut(SConnection * pCon, SicsInterp * pInter, void *pData,
int argc, char *argv[]) int argc, char *argv[])
{ {
OutCode eOut = eLog; int eOut;
int i = 0, iCode, iLen; int i = 0, iCode;
char *ppCode; size_t iLen;
char *pMessage = NULL; char *pMessage = NULL;
assert(pCon); assert(pCon);
@@ -630,91 +630,41 @@ int ClientPut(SConnection * pCon, SicsInterp * pInter, void *pData,
if (argc > 2) { if (argc > 2) {
/* the last one must be the code */ /* the last one must be the code */
iCode = argc - 1; iCode = argc - 1;
ppCode = strdup(argv[iCode]); eOut = OutCodeFromText(argv[iCode], NULL);
strtolower(ppCode); if (eOut < 0) { /* invalid code: assume it is content */
while (pCode[i] != NULL) { iCode = argc;
if (strcmp(pCode[i], ppCode) == 0) {
break;
}
i++;
}
if (ppCode) {
free(ppCode);
} }
} else { } else {
i = eLog;
iCode = argc;
}
switch (i) {
case 0:
eOut = eInternal;
break;
case 1:
eOut = eCommand;
break;
case 2:
eOut = eHWError;
break;
case 3:
eOut = eInError;
break;
case 4:
eOut = eStatus;
break;
case 5:
eOut = eValue;
break;
case 6:
eOut = eWarning;
break;
case 7:
eOut = eFinish;
break;
case 8:
eOut = eEvent;
break;
case 9:
eOut = eWarning;
break;
case 10:
eOut = eError;
break;
case 11:
eOut = eLog;
break;
case 12:
eOut = eLogError;
break;
default:
eOut = eLog; eOut = eLog;
iCode = argc; iCode = argc;
break;
} }
/* recombine the message */ /* recombine the message */
/* find length */ /* find length */
iLen = 0; iLen = 1;
for (i = 1; i < iCode; i++) { for (i = 1; i < iCode; i++) {
iLen += strlen(argv[i]); iLen += strlen(argv[i]) + 1;
} }
pMessage = (char *) malloc((iLen + 100) * sizeof(char)); pMessage = (char *) malloc(iLen);
if (!pMessage) { if (!pMessage) {
SCWrite(pCon, "ERROR: out of memory in clientput", eLogError); SCWrite(pCon, "ERROR: out of memory in clientput", eLogError);
return 0; return 0;
} }
memset(pMessage, 0, (iLen + 100) * sizeof(char)); memset(pMessage, 0, iLen);
Arg2Text(iCode - 1, &argv[1], pMessage, (iLen + 100) * sizeof(char)); Arg2Text(iCode - 1, &argv[1], pMessage, iLen);
/* /*
as the outcode is optional we have to test the message in order to get as the outcode is optional we have to test the message in order to get
proper outcodes for the log proper outcodes for the log
*/ */
if(strstr(pMessage,"ERROR") != NULL){ if (eOut < 0) { /* code not given */
eOut = eLogError; eOut = eLog;
} if(strstr(pMessage,"ERROR") != NULL){
if(strstr(pMessage,"WARNING") != NULL){ eOut = eLogError;
eOut = eWarning; }
if(strstr(pMessage,"WARNING") != NULL){
eOut = eWarning;
}
} }
SCWrite(pCon, pMessage, eOut); SCWrite(pCon, pMessage, eOut);