const char *_get_construction_func(char *input,size_t size, ndxml *dataNode) { char *org = input; *input = 0; int subNodes = ndxml_getsub_num(dataNode); for (int n = 0; n < subNodes; ++n) { int len = 0; ndxml *node1 = ndxml_refsubi(dataNode, n); int bIsString = false; const char *pType = ndxml_getattr_val(node1, "type"); if (0 == ndstricmp((char*)pType, (char*)"string") || 0 == ndstricmp((char*)pType, (char*)"char")) { bIsString = true; } const char *pArray = ndxml_getattr_val(node1, "isArray"); const char *pValName = ndxml_getattr_val(node1, "name"); const char *pDftVal = ndxml_getattr_val(node1, "default"); if (pArray && pArray[0] && pArray[0] == '1') { if (!bIsString) { len = snprintf(input, size, "\t\t%sCount = 0 ;\n", pValName); } else { if (pDftVal) { len = snprintf(input, size, "\t\tstrncpy(%s,\"%s\",sizeof(%s));\n", pValName,pDftVal,pValName); } else { len = snprintf(input, size, "\t\t%s[0] = 0 ;\n", pValName); } } } else { if (is_base_type(pType)) { len = snprintf(input, size, "\t\t%s = %s ;\n", pValName, pDftVal ? pDftVal : "0"); } } input += len; size -= len; } return org; }
int add_basetype_from_die(Dwarf_Debug dbg, Dwarf_Die parent_die, Dwarf_Die die) { int ret = DW_DLV_ERROR; Dwarf_Error err = 0; Dwarf_Off offset = 0; Dwarf_Unsigned bsz = 0; basetype_t *t; if (is_base_type(die) != 1) return -1; ret = dwarf_die_CU_offset(die, &offset, &err); if (ret != DW_DLV_OK) { derror("error in dwarf_die_CU_offset()"); goto error; } ret = dwarf_bytesize(die, &bsz, &err); if (ret != DW_DLV_OK) { derror("error in dwarf_bytesize()"); goto error; } /* We construct a table of base types here so that we can iœndex it * later to find the types of some of the probes on the stack. */ t = get_or_add_type(offset); get_child_name(dbg, die, t->name, 128); t->ohm_type = get_type_ohmtype(t); t->size = bsz; t->nelem = 1; t->elems = NULL; return 1; error: derror("error in add_basetype_from_die()"); return -1; }
/* start -> statement_expr | decl_variable */ void finsh_parser_run(struct finsh_parser* self, const u_char* string) { enum finsh_token_type token; struct finsh_node *node; node = NULL; /* init parser */ self->parser_string = (u_char*)string; /* init token */ finsh_token_init(&(self->token), self->parser_string); /* get next token */ next_token(token, &(self->token)); while (token != finsh_token_type_eof && token != finsh_token_type_bad) { switch (token) { case finsh_token_type_identifier: /* process expr_statement */ finsh_token_replay(&(self->token)); if (self->root != NULL) { finsh_node_sibling(node) = proc_expr_statement(self); if (finsh_node_sibling(node) != NULL) node = finsh_node_sibling(node); } else { node = proc_expr_statement(self); self->root = node; } break; default: if (is_base_type(token) || token == finsh_token_type_unsigned) { /* variable decl */ finsh_token_replay(&(self->token)); if (self->root != NULL) { finsh_node_sibling(node) = proc_variable_decl(self); if (finsh_node_sibling(node) != NULL) node = finsh_node_sibling(node); } else { node = proc_variable_decl(self); self->root = node; } } else { /* process expr_statement */ finsh_token_replay(&(self->token)); if (self->root != NULL) { finsh_node_sibling(node) = proc_expr_statement(self); if (finsh_node_sibling(node) != NULL) node = finsh_node_sibling(node); else next_token(token, &(self->token)); } else { node = proc_expr_statement(self); self->root = node; } } break; } /* get next token */ next_token(token, &(self->token)); } }
/* type -> type_prefix type_basic | type_basic type_prefix -> UNSIGNED type_basic -> VOID | CHAR | SHORT | INT | STRING */ static enum finsh_type proc_type(struct finsh_parser* self) { enum finsh_type type; enum finsh_token_type token; /* set init type */ type = finsh_type_unknown; next_token(token, &(self->token)); if ( is_base_type(token) ) /* base_type */ { switch (token) { case finsh_token_type_void: type = finsh_type_void; break; case finsh_token_type_char: type = finsh_type_char; break; case finsh_token_type_short: type = finsh_type_short; break; case finsh_token_type_int: type = finsh_type_int; break; case finsh_token_type_long: type = finsh_type_long; break; default: goto __return; } } else if ( token == finsh_token_type_unsigned ) /* unsigned base_type */ { next_token(token, &(self->token)); if ( is_base_type(token) ) { switch (token) { case finsh_token_type_char: type = finsh_type_uchar; break; case finsh_token_type_short: type = finsh_type_ushort; break; case finsh_token_type_int: type = finsh_type_uint; break; case finsh_token_type_long: type = finsh_type_ulong; break; default: goto __return; } } else { finsh_token_replay(&(self->token)); finsh_error_set(FINSH_ERROR_EXPECT_TYPE); } } else { goto __return; } /* parse for pointer */ next_token(token, &(self->token)); if (token == finsh_token_type_mul) { switch (type) { case finsh_type_void: type = finsh_type_voidp; break; case finsh_type_char: case finsh_type_uchar: type = finsh_type_charp; break; case finsh_type_short: case finsh_type_ushort: type = finsh_type_shortp; break; case finsh_type_int: case finsh_type_uint: type = finsh_type_intp; break; case finsh_type_long: case finsh_type_ulong: type = finsh_type_longp; break; default: type = finsh_type_voidp; break; } } else finsh_token_replay(&(self->token)); return type; __return: finsh_token_replay(&(self->token)); finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); return type; }
int _save_dataTypeNode(ndxml *sub, FILE *pf,FILE *pfCpp, const char *version, bool SaveDB) { const char *pName = ndxml_getname(sub) ; if (!pName) { return-1 ; } char name_buf[256] ; char construct_func[1024*16]; const char *pComment = ndxml_getattr_val(sub, "comment"); if (pComment) { fprintf(pf, "// %s\n", pComment) ; } if (SaveDB) { snprintf(name_buf, sizeof(name_buf), "%s_v%s",pName,version) ; pName = name_buf ; //fprintf(pf, "struct %s \n{\n ", pName) ; //fprintf(pf, "\t%s() \n\t{\n\t\tmemset(this, 0, sizeof(*this) ); \n\t}\n\n",pName, ) ; } else { //fprintf(pf, "struct %s \n{\n ", pName) ; //fprintf(pf, "\t%s() \n\t{\n\t\tmemset(this, 0, sizeof(*this) ); \n\t}\n\n",pName) ; } fprintf(pf, "struct %s \n{\n ", pName); fprintf(pf, "\t%s() \n\t{\n%s\t}\n\n", pName, _get_construction_func(construct_func, sizeof(construct_func), sub)); fprintf(pf, "\t%s& operator=(const %s &r)\n\t{\n%s\t\treturn *this;\n\t}\n\n", pName, pName, _get_copy_function_func(construct_func, sizeof(construct_func), sub)); char buf_read_func[4096] ; char buf_write_func[4096]; char buf_toUser_func[4096]; char buf_fromUser_func[4096]; char buf_toJson_func[4096]; char *pReadStream = buf_read_func; char *pWriteStream = buf_write_func ; char *pTouserStream = buf_toUser_func; char *pFromuserStream = buf_fromUser_func; char *pToJsonStream = buf_toJson_func; size_t read_size = sizeof(buf_read_func); size_t write_size = sizeof(buf_write_func); size_t toUser_size = sizeof(buf_toUser_func); size_t fromUser_size = sizeof(buf_fromUser_func); size_t toJson_size = sizeof(buf_toJson_func); int subNodes = ndxml_getsub_num(sub) ; for (int n=0; n<subNodes; ++n) { ndxml *node1 = ndxml_refsubi(sub,n) ; int bIsString =false ; const char *pType = ndxml_getattr_val(node1, "type"); if (0==ndstricmp((char*)pType, (char*)"string")||0==ndstricmp((char*)pType, (char*)"char") ) { pType = "char" ; bIsString = true ; } const char *pArray = ndxml_getattr_val(node1,"isArray") ; const char *pValName = ndxml_getattr_val(node1, "name") ; // if (is_base_type(pType)) { // int size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_NODE_NEW_LINE(p, outSize);\n"); // pToJsonStream += size; // toJson_size -= size; // } if (pArray && pArray[0] && pArray[0]=='1') { if (bIsString) { fprintf(pf,"\t%s %s[%s]; \t//%s \n",getType(pType,version), pValName, ndxml_getattr_val(node1, "arrayMarco"),ndxml_getattr_val(node1, "comment") ) ; //read-string function int size = snprintf(pReadStream, read_size, "\t\tREAD_STREAM_STRING(inmsg, data.%s, %s);\n",pValName , ndxml_getattr_val(node1, "arrayMarco")) ; pReadStream += size ; read_size -= size; //write string function size = snprintf(pWriteStream, write_size, "\t\tWRITE_STREAM(omsg,data.%s);\n",pValName ) ; pWriteStream += size ; write_size -= size; //write to userdefine function size = snprintf(pTouserStream, toUser_size, "\t\tTO_USER_DEF_TEXT(paramUserData, %s, paramInputData);\n", pValName); pTouserStream += size; toUser_size -= size; size = snprintf(pFromuserStream, fromUser_size, "\t\tFROM_USER_DEF_TEXT(paramUserData, %s, paramInputData, %s);\n", pValName, ndxml_getattr_val(node1, "arrayMarco")); pFromuserStream += size; fromUser_size -= size; //to json char *pToJsonStream = buf_toJson_func; size_t PrintJson(char *buf, size_t bufsize, const char *name, NDUINT8 &data); size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_BASE_PRINT_TOBUF(p, outSize, \"%s\", (const char*)data.%s);\n", pValName, pValName); pToJsonStream += size; toJson_size -= size; } else { fprintf(pf,"\tNDUINT16 %sCount; \t \n", pValName ) ; fprintf(pf,"\t%s %s[%s]; \t//%s \n",getType(pType,version), pValName, ndxml_getattr_val(node1, "arrayMarco"), ndxml_getattr_val(node1, "comment") ) ; //write push function fprintf(pf, "\tbool push_%s(const %s &val)\n\t{\n", pValName, getType(pType, version)); fprintf(pf, "\t\tif (%sCount < %s) {\n", pValName, ndxml_getattr_val(node1, "arrayMarco")); fprintf(pf, "\t\t\t%s[%sCount++] = val;\n", pValName, pValName); fprintf(pf, "\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n"); //read array string fucntion int size = snprintf(pReadStream, read_size, "\n\t\tREAD_STREAM(inmsg,data.%sCount);\n" "\t\tif( data.%sCount > %s ) {\n\t\t\treturn -1;\n\t\t}\n ", pValName,pValName, ndxml_getattr_val(node1, "arrayMarco") ) ; pReadStream += size ; read_size -= size; size = snprintf(pReadStream, read_size, "\t\tfor(int i =0; i<data.%sCount;++i )" "{\n\t\t\tREAD_STREAM(inmsg,data.%s[i]);\n\t\t}\n ", pValName, pValName ) ; pReadStream += size ; read_size -= size; //write array string function size = snprintf(pWriteStream, write_size, "\n\t\tif(data.%sCount > %s) {\n\t\t\treturn -1; \n\t\t} \n ", pValName, ndxml_getattr_val(node1, "arrayMarco") ) ; pWriteStream += size ; write_size -= size; size = snprintf(pWriteStream, write_size, "\t\tWRITE_STREAM(omsg,data.%sCount);\n" "\t\tfor(int i =0 ; i < data.%sCount;++i){\n" "\t\t\tWRITE_STREAM(omsg,data.%s[i]); \n\t\t} \n", pValName,pValName,pValName ) ; pWriteStream += size ; write_size -= size; //usef define function if (is_base_type(pType)) { size = snprintf(pTouserStream, toUser_size, "\t\tTO_USER_DEF_BASE_ARR(paramUserData, %s, paramInputData);\n", pValName); pTouserStream += size; toUser_size -= size; size = snprintf(pFromuserStream, fromUser_size, "\t\tFROM_USER_DEF_BASE_ARR(paramUserData, %s, paramInputData, %s);\n", pValName, ndxml_getattr_val(node1, "arrayMarco")); pFromuserStream += size; fromUser_size -= size; size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_BASEARRAY_PRINT_TOBUF(p, outSize, \"%s\", data.%s,data.%sCount);\n", pValName, pValName, pValName); pToJsonStream += size; toJson_size -= size; } else { size = snprintf(pTouserStream, toUser_size, "\t\tTO_USER_DEF_NODE_ARR(paramUserData, %s, paramInputData);\n", pValName); pTouserStream += size; toUser_size -= size; size = snprintf(pFromuserStream, fromUser_size, "\t\tFROM_USER_DEF_NODE_ARR(paramUserData, %s, paramInputData, %s);\n", pValName, ndxml_getattr_val(node1, "arrayMarco")); pFromuserStream += size; fromUser_size -= size; size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_ARRAY_PRINT_TOBUF(p, outSize, \"%s\", data.%s,data.%sCount);\n", pValName, pValName, pValName); pToJsonStream += size; toJson_size -= size; } } } else { fprintf(pf,"\t%s %s; \t//%s \n",getType(pType,version), pValName, ndxml_getattr_val(node1, "comment") ) ; //readstream int size = snprintf(pReadStream, read_size, "\t\tREAD_STREAM(inmsg, data.%s);\n", pValName); pReadStream += size ; read_size -= size; size = snprintf(pWriteStream, write_size, "\t\tWRITE_STREAM(omsg,data.%s);\n", pValName); pWriteStream += size ; write_size -= size; if (is_base_type(pType) || ndstricmp("binaryData",pType)==0) { size = snprintf(pTouserStream, toUser_size, "\t\tTO_USER_DEF_ELE(paramUserData, %s, paramInputData);\n", pValName); pTouserStream += size; toUser_size -= size; size = snprintf(pFromuserStream, fromUser_size, "\t\tFROM_USER_DEF_ELE(paramUserData, %s, paramInputData);\n", pValName); pFromuserStream += size; fromUser_size -= size; size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_BASE_PRINT_TOBUF(p, outSize, \"%s\", data.%s);\n", pValName, pValName); pToJsonStream += size; toJson_size -= size; } else { size = snprintf(pTouserStream, toUser_size, "\t\tTO_USER_DEF_NODE(paramUserData, %s, paramInputData);\n", pValName); pTouserStream += size; toUser_size -= size; size = snprintf(pFromuserStream, fromUser_size, "\t\tFROM_USER_DEF_NODE(paramUserData, %s, paramInputData);\n", pValName); pFromuserStream += size; fromUser_size -= size; size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_NODE_PRINT_TOBUF(p, outSize, \"%s\", data.%s);\n", pValName, pValName); pToJsonStream += size; toJson_size -= size; } } if (n==subNodes-1) { /*int size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_NODE_NEW_LINE(p, outSize);\n"); pToJsonStream += size; toJson_size -= size;*/ } else { int size = snprintf(pToJsonStream, toJson_size, "\t\tJSON_NODE_INTERVAL(p, outSize);\n"); pToJsonStream += size; toJson_size -= size; } } fprintf(pf, "};\n") ; fprintf(pfCpp, "\n\tint ReadStream(NDIStreamMsg &inmsg,NetMessage::%s &data)\n\t{\n", pName) ; fprintf(pfCpp, "%s\n\t\tTRYTO_MOVE_STRUCT_END(inmsg);\n\t\treturn 0; \n\t}\n", buf_read_func) ; fprintf(pfCpp, "\tint WriteStream(NDOStreamMsg &omsg,const NetMessage::%s &data)\n\t{\n", pName) ; fprintf(pfCpp, "\t\t\n"); fprintf(pfCpp, "%s\n\t\tEND_STRUCT_STREAM(omsg); \n\t\treturn 0; \n\t}\n", buf_write_func) ; fprintf(pf, "int ReadStream(NDIStreamMsg &inmsg,NetMessage::%s &data);\n", pName) ; fprintf(pf, "int WriteStream(NDOStreamMsg &omsg,const NetMessage::%s &data);\n", pName) ; //write json size_t PrintJson(char *buf, size_t bufsize, const char *name, const char*data); fprintf(pf, "size_t PrintJson(char *buf, size_t bufsize, const char *, %s &data);\n", pName); fprintf(pfCpp, "\n\tsize_t PrintJson(char *outBuf, size_t outSize, const char *name, %s &data)\n ", pName); fprintf(pfCpp, "\t{\n\t\tchar *p = outBuf;\n\t\tJSON_NODE_OUT_BEGIN(p,outSize);\n"); fprintf(pfCpp, "%s\n\t\tJSON_NODE_OUT_END(p,outSize);\n\t\treturn (p-outBuf); \n\t}\n", buf_toJson_func); //write to user-define data paramUserData, %s, paramInputData fprintf(pfCpp, "#ifndef WITHOUT_LOGIC_PARSER\n"); fprintf(pfCpp, "\n\tint ToUserDef(LogicUserDefStruct ¶mUserData,const NetMessage::%s ¶mInputData)\n\t{\n", pName); fprintf(pfCpp, "%s\n\t\treturn 0; \n\t}\n", buf_toUser_func); fprintf(pfCpp, "\tint FromUserDef(const LogicUserDefStruct ¶mUserData, NetMessage::%s ¶mInputData)\n\t{\n", pName); fprintf(pfCpp, "%s\n\t\treturn 0; \n\t}\n", buf_fromUser_func); fprintf(pfCpp, "#endif\n\n"); fprintf(pf, "#ifndef WITHOUT_LOGIC_PARSER\n"); fprintf(pf, "int ToUserDef(LogicUserDefStruct &userData,const NetMessage::%s &data);\n", pName); fprintf(pf, "int FromUserDef(const LogicUserDefStruct &userData, NetMessage::%s &data);\n", pName); fprintf(pf, "#endif\n\n\n"); return 0; }