From c9408a9b45a4a91e8ebac6a5bac20e0f9234e605 Mon Sep 17 00:00:00 2001 From: Ferdi Franceschini Date: Thu, 26 Oct 2006 10:59:03 +1000 Subject: [PATCH] Added json support. r1192 | ffr | 2006-10-26 10:59:03 +1000 (Thu, 26 Oct 2006) | 2 lines --- protocol.c | 8 ++--- sicshipadaba.c | 98 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/protocol.c b/protocol.c index ab9ee6ec..a7ef6f2d 100644 --- a/protocol.c +++ b/protocol.c @@ -632,7 +632,10 @@ struct json_object *mkJSON_Object(SConnection *pCon, char *pBuffer, int iOut) json_object_object_add(msg_json, "flag", json_object_new_string(pCode[iOut])); break; } - if (iOut == eStatus || iOut == eError || iOut == eWarning) { + if (iOut == eValue || iOut == eEvent) { + tmp_json = json_tokener_parse(pBuffer); + if (is_error(msg_json)) { linenum = __LINE__; goto reporterr; } + } else { /* Strip \r and \n */ for (pBufferFrom=pBufferTo=pBuffer; ; pBufferFrom++) { if (*pBufferFrom == '\r' || *pBufferFrom == '\n') @@ -644,9 +647,6 @@ struct json_object *mkJSON_Object(SConnection *pCon, char *pBuffer, int iOut) } tmp_json = json_object_new_string(pBuffer); if (is_error(msg_json)) { linenum = __LINE__; goto reporterr; } - } else { - tmp_json = json_tokener_parse(pBuffer); - if (is_error(msg_json)) { linenum = __LINE__; goto reporterr; } } json_object_object_add(msg_json, "data", tmp_json); return msg_json; diff --git a/sicshipadaba.c b/sicshipadaba.c index bb6d7254..e958c4b9 100644 --- a/sicshipadaba.c +++ b/sicshipadaba.c @@ -21,7 +21,7 @@ static int scriptUpdate = -1; static hdbUpdateTask taskData; enum formatStyle {plain, cli, json}; char *formatName[] = {"plain", "cli", "json"}; -static enum formatStyle currFmtStyle=plain; +static enum formatStyle currFmtStyle=json; pDynString formatClientValue(char *name, hdbValue hVal, int children); @@ -1409,14 +1409,33 @@ static int GetHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData, pDynString parData = NULL; char error[512], oriPath[512];; int i, status; + int pathArg = 1, fmtArg=1; + char *usage ="ERROR:valid arguments are [-json] nodepath"; - if(argc < 2) { - SCWrite(pCon,"ERROR: need path to node to print",eError); +/*XXX + switch (argc) { + case 2: + pathArg = 1; + currFmtStyle = plain; + break; + case 3: + pathArg = 2; + if(strcmp(argv[1],"-json") == 0){ + currFmtStyle = json; + } else { + SCWrite(pCon,usage,eError); return 0; + } + break; + default: + SCWrite(pCon,usage,eError); + return 0; + break; } +*/ - strncpy(oriPath,argv[1], 511); - targetNode = locateSICSNode(pSics,pCon,argv[1]); + strncpy(oriPath,argv[pathArg], 511); + targetNode = locateSICSNode(pSics,pCon,argv[pathArg]); if(targetNode == NULL){ return 0; } @@ -1616,14 +1635,32 @@ static pDynString formatJSONList(pHdb node){ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]){ pHdb node = NULL; - int pathArg = 1; pDynString listData = NULL; - - if(argc < 2) { - SCWrite(pCon,"ERROR: need path to node to print",eError); + int pathArg = 1, fmtArg=1; + char *usage ="ERROR:valid arguments are [-json] nodepath"; + +/*XXX + switch (argc) { + case 2: + pathArg = 1; + currFmtStyle = plain; + break; + case 3: + pathArg = 2; + if(strcmp(argv[1],"-json") == 0){ + currFmtStyle = json; + } else { + SCWrite(pCon,usage,eError); + return 0; + } + break; + default: + SCWrite(pCon,usage,eError); return 0; + break; } - +*/ +/*XXX if(strchr(argv[1],'-') != NULL){ pathArg = 2; if(argc < 3){ @@ -1631,20 +1668,20 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData, return 0; } } - +*/ node = locateSICSNode(pSics,pCon,argv[pathArg]); if(node == NULL){ return 0; } - strtolower(argv[1]); - if (argc == 3) { + strtolower(argv[pathArg]); +/*XXX if (argc == 3) { if(strcmp(argv[1],"-val") == 0){ listData = formatListWithVal(node); } else if(strcmp(argv[1],"-cli") == 0 || currFmtStyle == cli){ listData = formatClientList(node); } - } else { + } else { */ switch (currFmtStyle) { case cli: listData = formatClientList(node); @@ -1656,7 +1693,7 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData, default: listData = formatPlainList(node); } - } + //} if(listData == NULL){ SCWrite(pCon,"ERROR: failed to format list", eError); @@ -1682,19 +1719,44 @@ static int AutoNotifyHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]){ pHdb node = NULL; int id, status; + int pathArg = 1, idArg=2; + char *usage ="ERROR:valid arguments are [-json] nodepath id"; + + switch (argc) { + case 3: + pathArg = 1; + idArg = 2; + currFmtStyle = json; + break; + case 4: + /* pathArg = 2; + idArg = 3; + if(strcmp(argv[1],"-json") == 0){ + currFmtStyle = json; + } else { + SCWrite(pCon,usage,eError); + return 0; + }*/ + break; + default: + SCWrite(pCon,usage,eError); + return 0; + break; + } + /*XXX if(argc < 3) { SCWrite(pCon,"ERROR: need path and id in order to add notify", eError); return 0; } - - node = locateSICSNode(pSics,pCon,argv[1]); + */ + node = locateSICSNode(pSics,pCon,argv[pathArg]); if(node == NULL){ return 0; } - id = atoi(argv[2]); + id = atoi(argv[idArg]); status = InstallSICSNotify(node, pCon, id, 1); if(status == 1){