提交 d6f527d8 编写于 作者: S Shengliang Guan

[TD-1952]<fix>: tsim crash while run background scripts

上级 855ecf59
...@@ -100,7 +100,7 @@ typedef struct _cmd_t { ...@@ -100,7 +100,7 @@ typedef struct _cmd_t {
int16_t cmdno; int16_t cmdno;
int16_t nlen; int16_t nlen;
char name[MAX_SIM_CMD_NAME_LEN]; char name[MAX_SIM_CMD_NAME_LEN];
bool (*parseCmd)(char *, struct _cmd_t *, int); bool (*parseCmd)(char *, struct _cmd_t *, int32_t);
bool (*executeCmd)(struct _script_t *script, char *option); bool (*executeCmd)(struct _script_t *script, char *option);
struct _cmd_t *next; struct _cmd_t *next;
} SCommand; } SCommand;
...@@ -111,7 +111,7 @@ typedef struct { ...@@ -111,7 +111,7 @@ typedef struct {
int16_t errorJump; // sql jump flag, while '-x' exist in sql cmd, this flag int16_t errorJump; // sql jump flag, while '-x' exist in sql cmd, this flag
// will be SQL_JUMP_TRUE, otherwise is SQL_JUMP_FALSE */ // will be SQL_JUMP_TRUE, otherwise is SQL_JUMP_FALSE */
int16_t lineNum; // correspodning line number in original file int16_t lineNum; // correspodning line number in original file
int optionOffset; // relative option offset int32_t optionOffset;// relative option offset
} SCmdLine; } SCmdLine;
typedef struct _var_t { typedef struct _var_t {
...@@ -121,59 +121,56 @@ typedef struct _var_t { ...@@ -121,59 +121,56 @@ typedef struct _var_t {
} SVariable; } SVariable;
typedef struct _script_t { typedef struct _script_t {
int type; int32_t type;
bool killed; bool killed;
void * taos;
void *taos; char rows[12]; // number of rows data retrieved
char rows[12]; // number of rows data retrieved char data[MAX_QUERY_ROW_NUM][MAX_QUERY_COL_NUM][MAX_QUERY_VALUE_LEN]; // query results
char data[MAX_QUERY_ROW_NUM][MAX_QUERY_COL_NUM] char system_exit_code[12];
[MAX_QUERY_VALUE_LEN]; // query results char system_ret_content[MAX_SYSTEM_RESULT_LEN];
char system_exit_code[12]; int32_t varLen;
char system_ret_content[MAX_SYSTEM_RESULT_LEN]; int32_t linePos; // current cmd position
int32_t numOfLines; // number of lines in the script
int varLen; int32_t bgScriptLen;
int linePos; // current cmd position char fileName[MAX_FILE_NAME_LEN]; // script file name
int numOfLines; // number of lines in the script char error[MAX_ERROR_LEN];
int bgScriptLen; char * optionBuffer;
char fileName[MAX_FILE_NAME_LEN]; // script file name
char error[MAX_ERROR_LEN];
char *optionBuffer;
SCmdLine *lines; // command list SCmdLine *lines; // command list
SVariable variables[MAX_VAR_LEN]; SVariable variables[MAX_VAR_LEN];
pthread_t bgPid;
char auth[128];
struct _script_t *bgScripts[MAX_BACKGROUND_SCRIPT_NUM]; struct _script_t *bgScripts[MAX_BACKGROUND_SCRIPT_NUM];
char auth[128];
} SScript; } SScript;
extern SScript *simScriptList[MAX_MAIN_SCRIPT_NUM]; extern SScript *simScriptList[MAX_MAIN_SCRIPT_NUM];
extern SCommand simCmdList[]; extern SCommand simCmdList[];
extern int simScriptPos; extern int32_t simScriptPos;
extern int simScriptSucced; extern int32_t simScriptSucced;
extern int simDebugFlag; extern int32_t simDebugFlag;
extern char tsScriptDir[]; extern char tsScriptDir[];
extern bool simAsyncQuery; extern bool simAsyncQuery;
SScript *simParseScript(char *fileName); SScript *simParseScript(char *fileName);
SScript *simProcessCallOver(SScript *script); SScript *simProcessCallOver(SScript *script);
void *simExecuteScript(void *script); void * simExecuteScript(void *script);
void simInitsimCmdList(); void simInitsimCmdList();
bool simSystemInit(); bool simSystemInit();
void simSystemCleanUp(); void simSystemCleanUp();
char *simGetVariable(SScript *script, char *varName, int varLen); char * simGetVariable(SScript *script, char *varName, int32_t varLen);
bool simExecuteExpCmd(SScript *script, char *option); bool simExecuteExpCmd(SScript *script, char *option);
bool simExecuteTestCmd(SScript *script, char *option); bool simExecuteTestCmd(SScript *script, char *option);
bool simExecuteGotoCmd(SScript *script, char *option); bool simExecuteGotoCmd(SScript *script, char *option);
bool simExecuteRunCmd(SScript *script, char *option); bool simExecuteRunCmd(SScript *script, char *option);
bool simExecuteRunBackCmd(SScript *script, char *option); bool simExecuteRunBackCmd(SScript *script, char *option);
bool simExecuteSystemCmd(SScript *script, char *option); bool simExecuteSystemCmd(SScript *script, char *option);
bool simExecuteSystemContentCmd(SScript *script, char *option); bool simExecuteSystemContentCmd(SScript *script, char *option);
bool simExecutePrintCmd(SScript *script, char *option); bool simExecutePrintCmd(SScript *script, char *option);
bool simExecuteSleepCmd(SScript *script, char *option); bool simExecuteSleepCmd(SScript *script, char *option);
bool simExecuteReturnCmd(SScript *script, char *option); bool simExecuteReturnCmd(SScript *script, char *option);
bool simExecuteSqlCmd(SScript *script, char *option); bool simExecuteSqlCmd(SScript *script, char *option);
bool simExecuteSqlErrorCmd(SScript *script, char *rest); bool simExecuteSqlErrorCmd(SScript *script, char *rest);
bool simExecuteSqlSlowCmd(SScript *script, char *option); bool simExecuteSqlSlowCmd(SScript *script, char *option);
bool simExecuteRestfulCmd(SScript *script, char *rest); bool simExecuteRestfulCmd(SScript *script, char *rest);
void simVisuallizeOption(SScript *script, char *src, char *dst); void simVisuallizeOption(SScript *script, char *src, char *dst);
#endif #endif
\ No newline at end of file
...@@ -50,6 +50,6 @@ typedef struct { ...@@ -50,6 +50,6 @@ typedef struct {
char sexpLen[MAX_NUM_BLOCK]; /*switch expression length */ char sexpLen[MAX_NUM_BLOCK]; /*switch expression length */
} SBlock; } SBlock;
bool simParseExpression(char *token, int lineNum); bool simParseExpression(char *token, int32_t lineNum);
#endif #endif
\ No newline at end of file
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _DEFAULT_SOURCE
#include "os.h" #include "os.h"
#include "sim.h" #include "sim.h"
#include "taos.h" #include "taos.h"
...@@ -38,30 +39,28 @@ void simLogSql(char *sql, bool useSharp) { ...@@ -38,30 +39,28 @@ void simLogSql(char *sql, bool useSharp) {
} else { } else {
fprintf(fp, "%s;\n", sql); fprintf(fp, "%s;\n", sql);
} }
fflush(fp); fflush(fp);
} }
char *simParseArbitratorName(char *varName); char *simParseArbitratorName(char *varName);
char *simParseHostName(char *varName); char *simParseHostName(char *varName);
char *simGetVariable(SScript *script, char *varName, int varLen) { char *simGetVariable(SScript *script, char *varName, int32_t varLen) {
if (strncmp(varName, "hostname", 8) == 0) { if (strncmp(varName, "hostname", 8) == 0) {
return simParseHostName(varName); return simParseHostName(varName);
} }
if (strncmp(varName, "arbitrator", 10) == 0) { if (strncmp(varName, "arbitrator", 10) == 0) {
return simParseArbitratorName(varName); return simParseArbitratorName(varName);
} }
if (strncmp(varName, "error", varLen) == 0) return script->error; if (strncmp(varName, "error", varLen) == 0) return script->error;
if (strncmp(varName, "rows", varLen) == 0) return script->rows; if (strncmp(varName, "rows", varLen) == 0) return script->rows;
if (strncmp(varName, "system_exit", varLen) == 0) if (strncmp(varName, "system_exit", varLen) == 0) return script->system_exit_code;
return script->system_exit_code;
if (strncmp(varName, "system_content", varLen) == 0) if (strncmp(varName, "system_content", varLen) == 0) return script->system_ret_content;
return script->system_ret_content;
// variable like data2_192.168.0.1 // variable like data2_192.168.0.1
if (strncmp(varName, "data", 4) == 0) { if (strncmp(varName, "data", 4) == 0) {
...@@ -70,16 +69,16 @@ char *simGetVariable(SScript *script, char *varName, int varLen) { ...@@ -70,16 +69,16 @@ char *simGetVariable(SScript *script, char *varName, int varLen) {
} }
if (varName[5] == '_') { if (varName[5] == '_') {
int col = varName[4] - '0'; int32_t col = varName[4] - '0';
if (col < 0 || col >= MAX_QUERY_COL_NUM) { if (col < 0 || col >= MAX_QUERY_COL_NUM) {
return "null"; return "null";
} }
char *keyName; char * keyName;
int keyLen; int32_t keyLen;
paGetToken(varName + 6, &keyName, &keyLen); paGetToken(varName + 6, &keyName, &keyLen);
for (int i = 0; i < MAX_QUERY_ROW_NUM; ++i) { for (int32_t i = 0; i < MAX_QUERY_ROW_NUM; ++i) {
if (strncmp(keyName, script->data[i][0], keyLen) == 0) { if (strncmp(keyName, script->data[i][0], keyLen) == 0) {
simDebug("script:%s, keyName:%s, keyValue:%s", script->fileName, script->data[i][0], script->data[i][col]); simDebug("script:%s, keyName:%s, keyValue:%s", script->fileName, script->data[i][0], script->data[i][col]);
return script->data[i][col]; return script->data[i][col];
...@@ -87,16 +86,16 @@ char *simGetVariable(SScript *script, char *varName, int varLen) { ...@@ -87,16 +86,16 @@ char *simGetVariable(SScript *script, char *varName, int varLen) {
} }
return "null"; return "null";
} else if (varName[6] == '_') { } else if (varName[6] == '_') {
int col = (varName[4] - '0') * 10 + (varName[5] - '0'); int32_t col = (varName[4] - '0') * 10 + (varName[5] - '0');
if (col < 0 || col >= MAX_QUERY_COL_NUM) { if (col < 0 || col >= MAX_QUERY_COL_NUM) {
return "null"; return "null";
} }
char *keyName; char * keyName;
int keyLen; int32_t keyLen;
paGetToken(varName + 7, &keyName, &keyLen); paGetToken(varName + 7, &keyName, &keyLen);
for (int i = 0; i < MAX_QUERY_ROW_NUM; ++i) { for (int32_t i = 0; i < MAX_QUERY_ROW_NUM; ++i) {
if (strncmp(keyName, script->data[i][0], keyLen) == 0) { if (strncmp(keyName, script->data[i][0], keyLen) == 0) {
simTrace("script:%s, keyName:%s, keyValue:%s", script->fileName, script->data[i][0], script->data[i][col]); simTrace("script:%s, keyName:%s, keyValue:%s", script->fileName, script->data[i][0], script->data[i][col]);
return script->data[i][col]; return script->data[i][col];
...@@ -104,8 +103,8 @@ char *simGetVariable(SScript *script, char *varName, int varLen) { ...@@ -104,8 +103,8 @@ char *simGetVariable(SScript *script, char *varName, int varLen) {
} }
return "null"; return "null";
} else { } else {
int row = varName[4] - '0'; int32_t row = varName[4] - '0';
int col = varName[5] - '0'; int32_t col = varName[5] - '0';
if (row < 0 || row >= MAX_QUERY_ROW_NUM) { if (row < 0 || row >= MAX_QUERY_ROW_NUM) {
return "null"; return "null";
} }
...@@ -118,7 +117,7 @@ char *simGetVariable(SScript *script, char *varName, int varLen) { ...@@ -118,7 +117,7 @@ char *simGetVariable(SScript *script, char *varName, int varLen) {
} }
} }
for (int i = 0; i < script->varLen; ++i) { for (int32_t i = 0; i < script->varLen; ++i) {
SVariable *var = &script->variables[i]; SVariable *var = &script->variables[i];
if (var->varNameLen != varLen) { if (var->varNameLen != varLen) {
continue; continue;
...@@ -144,11 +143,11 @@ char *simGetVariable(SScript *script, char *varName, int varLen) { ...@@ -144,11 +143,11 @@ char *simGetVariable(SScript *script, char *varName, int varLen) {
return var->varValue; return var->varValue;
} }
int simExecuteExpression(SScript *script, char *exp) { int32_t simExecuteExpression(SScript *script, char *exp) {
char *op1, *op2, *var1, *var2, *var3, *rest; char * op1, *op2, *var1, *var2, *var3, *rest;
int op1Len, op2Len, var1Len, var2Len, var3Len, val0, val1; int32_t op1Len, op2Len, var1Len, var2Len, var3Len, val0, val1;
char t0[512], t1[512], t2[512], t3[1024]; char t0[512], t1[512], t2[512], t3[1024];
int result; int32_t result;
rest = paGetToken(exp, &var1, &var1Len); rest = paGetToken(exp, &var1, &var1Len);
rest = paGetToken(rest, &op1, &op1Len); rest = paGetToken(rest, &op1, &op1Len);
...@@ -234,7 +233,7 @@ bool simExecuteExpCmd(SScript *script, char *option) { ...@@ -234,7 +233,7 @@ bool simExecuteExpCmd(SScript *script, char *option) {
} }
bool simExecuteTestCmd(SScript *script, char *option) { bool simExecuteTestCmd(SScript *script, char *option) {
int result; int32_t result;
result = simExecuteExpression(script, option); result = simExecuteExpression(script, option);
if (result >= 0) if (result >= 0)
...@@ -285,13 +284,12 @@ bool simExecuteRunBackCmd(SScript *script, char *option) { ...@@ -285,13 +284,12 @@ bool simExecuteRunBackCmd(SScript *script, char *option) {
sprintf(script->error, "lineNum:%d. parse file:%s error", script->lines[script->linePos].lineNum, fileName); sprintf(script->error, "lineNum:%d. parse file:%s error", script->lines[script->linePos].lineNum, fileName);
return false; return false;
} }
simInfo("script:%s, start to execute in background", newScript->fileName);
newScript->type = SIM_SCRIPT_TYPE_BACKGROUND; newScript->type = SIM_SCRIPT_TYPE_BACKGROUND;
script->bgScripts[script->bgScriptLen++] = newScript; script->bgScripts[script->bgScriptLen++] = newScript;
simInfo("script:%s, start to execute in background,", newScript->fileName);
pthread_t pid; if (pthread_create(&newScript->bgPid, NULL, simExecuteScript, (void *)newScript) != 0) {
if (pthread_create(&pid, NULL, simExecuteScript, (void *)newScript) != 0) {
sprintf(script->error, "lineNum:%d. create background thread failed", script->lines[script->linePos].lineNum); sprintf(script->error, "lineNum:%d. create background thread failed", script->lines[script->linePos].lineNum);
return false; return false;
} }
...@@ -307,13 +305,13 @@ bool simExecuteSystemCmd(SScript *script, char *option) { ...@@ -307,13 +305,13 @@ bool simExecuteSystemCmd(SScript *script, char *option) {
simVisuallizeOption(script, option, buf + strlen(buf)); simVisuallizeOption(script, option, buf + strlen(buf));
simLogSql(buf, true); simLogSql(buf, true);
int code = system(buf); int32_t code = system(buf);
int repeatTimes = 0; int32_t repeatTimes = 0;
while (code < 0) { while (code < 0) {
simError("script:%s, failed to execute %s , code %d, errno:%d %s, repeatTimes:%d", simError("script:%s, failed to execute %s , code %d, errno:%d %s, repeatTimes:%d", script->fileName, buf, code,
script->fileName, buf, code, errno, strerror(errno), repeatTimes); errno, strerror(errno), repeatTimes);
taosMsleep(1000); taosMsleep(1000);
#ifdef LINUX #ifdef LINUX
signal(SIGCHLD, SIG_DFL); signal(SIGCHLD, SIG_DFL);
#endif #endif
if (repeatTimes++ >= 10) { if (repeatTimes++ >= 10) {
...@@ -368,8 +366,8 @@ bool simExecutePrintCmd(SScript *script, char *rest) { ...@@ -368,8 +366,8 @@ bool simExecutePrintCmd(SScript *script, char *rest) {
} }
bool simExecuteSleepCmd(SScript *script, char *option) { bool simExecuteSleepCmd(SScript *script, char *option) {
int delta; int32_t delta;
char buf[1024]; char buf[1024];
simVisuallizeOption(script, option, buf); simVisuallizeOption(script, option, buf);
option = buf; option = buf;
...@@ -395,7 +393,7 @@ bool simExecuteReturnCmd(SScript *script, char *option) { ...@@ -395,7 +393,7 @@ bool simExecuteReturnCmd(SScript *script, char *option) {
simVisuallizeOption(script, option, buf); simVisuallizeOption(script, option, buf);
option = buf; option = buf;
int ret = 1; int32_t ret = 1;
if (option && option[0] != 0) ret = atoi(option); if (option && option[0] != 0) ret = atoi(option);
if (ret < 0) { if (ret < 0) {
...@@ -411,8 +409,8 @@ bool simExecuteReturnCmd(SScript *script, char *option) { ...@@ -411,8 +409,8 @@ bool simExecuteReturnCmd(SScript *script, char *option) {
} }
void simVisuallizeOption(SScript *script, char *src, char *dst) { void simVisuallizeOption(SScript *script, char *src, char *dst) {
char *var, *token, *value; char * var, *token, *value;
int dstLen, srcLen, tokenLen; int32_t dstLen, srcLen, tokenLen;
dst[0] = 0, dstLen = 0; dst[0] = 0, dstLen = 0;
...@@ -420,14 +418,14 @@ void simVisuallizeOption(SScript *script, char *src, char *dst) { ...@@ -420,14 +418,14 @@ void simVisuallizeOption(SScript *script, char *src, char *dst) {
var = strchr(src, '$'); var = strchr(src, '$');
if (var == NULL) break; if (var == NULL) break;
if (var && ((var - src - 1) > 0) && *(var - 1) == '\\') { if (var && ((var - src - 1) > 0) && *(var - 1) == '\\') {
srcLen = (int)(var - src - 1); srcLen = (int32_t)(var - src - 1);
memcpy(dst + dstLen, src, srcLen); memcpy(dst + dstLen, src, srcLen);
dstLen += srcLen; dstLen += srcLen;
src = var; src = var;
break; break;
} }
srcLen = (int)(var - src); srcLen = (int32_t)(var - src);
memcpy(dst + dstLen, src, srcLen); memcpy(dst + dstLen, src, srcLen);
dstLen += srcLen; dstLen += srcLen;
...@@ -435,13 +433,13 @@ void simVisuallizeOption(SScript *script, char *src, char *dst) { ...@@ -435,13 +433,13 @@ void simVisuallizeOption(SScript *script, char *src, char *dst) {
value = simGetVariable(script, token, tokenLen); value = simGetVariable(script, token, tokenLen);
strcpy(dst + dstLen, value); strcpy(dst + dstLen, value);
dstLen += (int)strlen(value); dstLen += (int32_t)strlen(value);
} }
strcpy(dst + dstLen, src); strcpy(dst + dstLen, src);
} }
void simCloseRestFulConnect(SScript *script) { void simCloseRestFulConnect(SScript *script) {
memset(script->auth, 0, sizeof(script->auth)); memset(script->auth, 0, sizeof(script->auth));
} }
...@@ -465,7 +463,7 @@ void simCloseTaosdConnect(SScript *script) { ...@@ -465,7 +463,7 @@ void simCloseTaosdConnect(SScript *script) {
// {"status":"succ","code":0,"desc":"/KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04"} // {"status":"succ","code":0,"desc":"/KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04"}
// {"status":"succ","head":["affected_rows"],"data":[[1]],"rows":1} // {"status":"succ","head":["affected_rows"],"data":[[1]],"rows":1}
// {"status":"succ","head":["ts","i"],"data":[["2017-12-25 21:28:41.022",1],["2017-12-25 21:28:42.022",2],["2017-12-25 21:28:43.022",3],["2017-12-25 21:28:44.022",4],["2017-12-25 21:28:45.022",5],["2017-12-25 21:28:46.022",6],["2017-12-25 21:28:47.022",7],["2017-12-25 21:28:48.022",8],["2017-12-25 21:28:49.022",9],["2017-12-25 21:28:50.022",10]],"rows":10} // {"status":"succ","head":["ts","i"],"data":[["2017-12-25 21:28:41.022",1],["2017-12-25 21:28:42.022",2],["2017-12-25 21:28:43.022",3],["2017-12-25 21:28:44.022",4],["2017-12-25 21:28:45.022",5],["2017-12-25 21:28:46.022",6],["2017-12-25 21:28:47.022",7],["2017-12-25 21:28:48.022",8],["2017-12-25 21:28:49.022",9],["2017-12-25 21:28:50.022",10]],"rows":10}
int simParseHttpCommandResult(SScript *script, char *command) { int32_t simParseHttpCommandResult(SScript *script, char *command) {
cJSON* root = cJSON_Parse(command); cJSON* root = cJSON_Parse(command);
if (root == NULL) { if (root == NULL) {
simError("script:%s, failed to parse json, response:%s", script->fileName, command); simError("script:%s, failed to parse json, response:%s", script->fileName, command);
...@@ -492,14 +490,15 @@ int simParseHttpCommandResult(SScript *script, char *command) { ...@@ -492,14 +490,15 @@ int simParseHttpCommandResult(SScript *script, char *command) {
cJSON_Delete(root); cJSON_Delete(root);
return -1; return -1;
} }
int retcode = (int)code->valueint; int32_t retcode = (int32_t)code->valueint;
if (retcode != 1017) { if (retcode != 1017) {
simError("script:%s, json:status:%s not equal to succ, response:%s", script->fileName, status->valuestring, command); simError("script:%s, json:status:%s not equal to succ, response:%s", script->fileName, status->valuestring,
command);
cJSON_Delete(root); cJSON_Delete(root);
return retcode; return retcode;
} else { } else {
simDebug("script:%s, json:status:%s not equal to succ, but code is %d, response:%s", script->fileName, simDebug("script:%s, json:status:%s not equal to succ, but code is %d, response:%s", script->fileName,
status->valuestring, retcode, command); status->valuestring, retcode, command);
cJSON_Delete(root); cJSON_Delete(root);
return 0; return 0;
} }
...@@ -524,27 +523,27 @@ int simParseHttpCommandResult(SScript *script, char *command) { ...@@ -524,27 +523,27 @@ int simParseHttpCommandResult(SScript *script, char *command) {
return -1; return -1;
} }
int rowsize = cJSON_GetArraySize(data); int32_t rowsize = cJSON_GetArraySize(data);
if (rowsize < 0) { if (rowsize < 0) {
simError("script:%s, failed to parse json:data, data size %d, response:%s", script->fileName, rowsize, command); simError("script:%s, failed to parse json:data, data size %d, response:%s", script->fileName, rowsize, command);
cJSON_Delete(root); cJSON_Delete(root);
return -1; return -1;
} }
int rowIndex = 0; int32_t rowIndex = 0;
sprintf(script->rows, "%d", rowsize); sprintf(script->rows, "%d", rowsize);
for (int r = 0; r < rowsize; ++r) { for (int32_t r = 0; r < rowsize; ++r) {
cJSON *row = cJSON_GetArrayItem(data, r); cJSON *row = cJSON_GetArrayItem(data, r);
if (row == NULL) continue; if (row == NULL) continue;
if (rowIndex++ >= 10) break; if (rowIndex++ >= 10) break;
int colsize = cJSON_GetArraySize(row); int32_t colsize = cJSON_GetArraySize(row);
if (colsize < 0) { if (colsize < 0) {
break; break;
} }
colsize = MIN(10, colsize); colsize = MIN(10, colsize);
for (int c = 0; c < colsize; ++c) { for (int32_t c = 0; c < colsize; ++c) {
cJSON *col = cJSON_GetArrayItem(row, c); cJSON *col = cJSON_GetArrayItem(row, c);
if (col->valuestring != NULL) { if (col->valuestring != NULL) {
strcpy(script->data[r][c], col->valuestring); strcpy(script->data[r][c], col->valuestring);
...@@ -561,7 +560,7 @@ int simParseHttpCommandResult(SScript *script, char *command) { ...@@ -561,7 +560,7 @@ int simParseHttpCommandResult(SScript *script, char *command) {
return 0; return 0;
} }
int simExecuteRestFulCommand(SScript *script, char *command) { int32_t simExecuteRestFulCommand(SScript *script, char *command) {
char buf[5000] = {0}; char buf[5000] = {0};
sprintf(buf, "%s 2>/dev/null", command); sprintf(buf, "%s 2>/dev/null", command);
...@@ -571,13 +570,13 @@ int simExecuteRestFulCommand(SScript *script, char *command) { ...@@ -571,13 +570,13 @@ int simExecuteRestFulCommand(SScript *script, char *command) {
return -1; return -1;
} }
int mallocSize = 2000; int32_t mallocSize = 2000;
int alreadyReadSize = 0; int32_t alreadyReadSize = 0;
char* content = malloc(mallocSize); char * content = malloc(mallocSize);
while (!feof(fp)) { while (!feof(fp)) {
int availSize = mallocSize - alreadyReadSize; int32_t availSize = mallocSize - alreadyReadSize;
int len = (int)fread(content + alreadyReadSize, 1, availSize, fp); int32_t len = (int32_t)fread(content + alreadyReadSize, 1, availSize, fp);
if (len >= availSize) { if (len >= availSize) {
alreadyReadSize += len; alreadyReadSize += len;
mallocSize *= 2; mallocSize *= 2;
...@@ -595,10 +594,11 @@ bool simCreateRestFulConnect(SScript *script, char *user, char *pass) { ...@@ -595,10 +594,11 @@ bool simCreateRestFulConnect(SScript *script, char *user, char *pass) {
sprintf(command, "curl 127.0.0.1:6041/rest/login/%s/%s", user, pass); sprintf(command, "curl 127.0.0.1:6041/rest/login/%s/%s", user, pass);
bool success = false; bool success = false;
for (int attempt = 0; attempt < 10; ++attempt) { for (int32_t attempt = 0; attempt < 10; ++attempt) {
success = simExecuteRestFulCommand(script, command) == 0; success = simExecuteRestFulCommand(script, command) == 0;
if (!success) { if (!success) {
simDebug("script:%s, user:%s connect taosd failed:%s, attempt:%d", script->fileName, user, taos_errstr(NULL), attempt); simDebug("script:%s, user:%s connect taosd failed:%s, attempt:%d", script->fileName, user, taos_errstr(NULL),
attempt);
taosMsleep(1000); taosMsleep(1000);
} else { } else {
simDebug("script:%s, user:%s connect taosd successed, attempt:%d", script->fileName, user, attempt); simDebug("script:%s, user:%s connect taosd successed, attempt:%d", script->fileName, user, attempt);
...@@ -607,7 +607,8 @@ bool simCreateRestFulConnect(SScript *script, char *user, char *pass) { ...@@ -607,7 +607,8 @@ bool simCreateRestFulConnect(SScript *script, char *user, char *pass) {
} }
if (!success) { if (!success) {
sprintf(script->error, "lineNum:%d. connect taosd failed:%s", script->lines[script->linePos].lineNum, taos_errstr(NULL)); sprintf(script->error, "lineNum:%d. connect taosd failed:%s", script->lines[script->linePos].lineNum,
taos_errstr(NULL));
return false; return false;
} }
...@@ -619,10 +620,11 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) { ...@@ -619,10 +620,11 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) {
simCloseTaosdConnect(script); simCloseTaosdConnect(script);
void *taos = NULL; void *taos = NULL;
taosMsleep(2000); taosMsleep(2000);
for (int attempt = 0; attempt < 10; ++attempt) { for (int32_t attempt = 0; attempt < 10; ++attempt) {
taos = taos_connect(NULL, user, pass, NULL, tsDnodeShellPort); taos = taos_connect(NULL, user, pass, NULL, tsDnodeShellPort);
if (taos == NULL) { if (taos == NULL) {
simDebug("script:%s, user:%s connect taosd failed:%s, attempt:%d", script->fileName, user, taos_errstr(NULL), attempt); simDebug("script:%s, user:%s connect taosd failed:%s, attempt:%d", script->fileName, user, taos_errstr(NULL),
attempt);
taosMsleep(1000); taosMsleep(1000);
} else { } else {
simDebug("script:%s, user:%s connect taosd successed, attempt:%d", script->fileName, user, attempt); simDebug("script:%s, user:%s connect taosd successed, attempt:%d", script->fileName, user, attempt);
...@@ -631,7 +633,8 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) { ...@@ -631,7 +633,8 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) {
} }
if (taos == NULL) { if (taos == NULL) {
sprintf(script->error, "lineNum:%d. connect taosd failed:%s", script->lines[script->linePos].lineNum, taos_errstr(NULL)); sprintf(script->error, "lineNum:%d. connect taosd failed:%s", script->lines[script->linePos].lineNum,
taos_errstr(NULL));
return false; return false;
} }
...@@ -642,9 +645,9 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) { ...@@ -642,9 +645,9 @@ bool simCreateNativeConnect(SScript *script, char *user, char *pass) {
} }
bool simCreateTaosdConnect(SScript *script, char *rest) { bool simCreateTaosdConnect(SScript *script, char *rest) {
char *user = TSDB_DEFAULT_USER; char * user = TSDB_DEFAULT_USER;
char *token; char * token;
int tokenLen; int32_t tokenLen;
rest = paGetToken(rest, &token, &tokenLen); rest = paGetToken(rest, &token, &tokenLen);
rest = paGetToken(rest, &token, &tokenLen); rest = paGetToken(rest, &token, &tokenLen);
if (tokenLen != 0) { if (tokenLen != 0) {
...@@ -659,26 +662,27 @@ bool simCreateTaosdConnect(SScript *script, char *rest) { ...@@ -659,26 +662,27 @@ bool simCreateTaosdConnect(SScript *script, char *rest) {
} }
bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
char timeStr[30] = {0}; char timeStr[30] = {0};
time_t tt; time_t tt;
struct tm *tp; struct tm *tp;
SCmdLine *line = &script->lines[script->linePos]; SCmdLine * line = &script->lines[script->linePos];
int ret = -1; int32_t ret = -1;
TAOS_RES *pSql = NULL;
TAOS_RES* pSql = NULL; for (int32_t attempt = 0; attempt < 10; ++attempt) {
for (int attempt = 0; attempt < 10; ++attempt) {
simLogSql(rest, false); simLogSql(rest, false);
pSql = taos_query(script->taos, rest); pSql = taos_query(script->taos, rest);
ret = taos_errno(pSql); ret = taos_errno(pSql);
if (ret == TSDB_CODE_MND_TABLE_ALREADY_EXIST || ret == TSDB_CODE_MND_DB_ALREADY_EXIST) { if (ret == TSDB_CODE_MND_TABLE_ALREADY_EXIST || ret == TSDB_CODE_MND_DB_ALREADY_EXIST) {
simDebug("script:%s, taos:%p, %s success, ret:%d:%s", script->fileName, script->taos, rest, ret & 0XFFFF, tstrerror(ret)); simDebug("script:%s, taos:%p, %s success, ret:%d:%s", script->fileName, script->taos, rest, ret & 0XFFFF,
tstrerror(ret));
ret = 0; ret = 0;
break; break;
} else if (ret != 0) { } else if (ret != 0) {
simDebug("script:%s, taos:%p, %s failed, ret:%d:%s, error:%s", simDebug("script:%s, taos:%p, %s failed, ret:%d:%s, error:%s", script->fileName, script->taos, rest, ret & 0XFFFF,
script->fileName, script->taos, rest, ret & 0XFFFF, tstrerror(ret), taos_errstr(pSql)); tstrerror(ret), taos_errstr(pSql));
if (line->errorJump == SQL_JUMP_TRUE) { if (line->errorJump == SQL_JUMP_TRUE) {
script->linePos = line->jump; script->linePos = line->jump;
...@@ -689,7 +693,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { ...@@ -689,7 +693,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
} else { } else {
break; break;
} }
taos_free_result(pSql); taos_free_result(pSql);
} }
...@@ -698,8 +702,8 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { ...@@ -698,8 +702,8 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
return false; return false;
} }
int numOfRows = 0; int32_t numOfRows = 0;
int num_fields = taos_field_count(pSql); int32_t num_fields = taos_field_count(pSql);
if (num_fields != 0) { if (num_fields != 0) {
if (pSql == NULL) { if (pSql == NULL) {
simDebug("script:%s, taos:%p, %s failed, result is null", script->fileName, script->taos, rest); simDebug("script:%s, taos:%p, %s failed, result is null", script->fileName, script->taos, rest);
...@@ -717,9 +721,9 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { ...@@ -717,9 +721,9 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
while ((row = taos_fetch_row(pSql))) { while ((row = taos_fetch_row(pSql))) {
if (numOfRows < MAX_QUERY_ROW_NUM) { if (numOfRows < MAX_QUERY_ROW_NUM) {
TAOS_FIELD *fields = taos_fetch_fields(pSql); TAOS_FIELD *fields = taos_fetch_fields(pSql);
int* length = taos_fetch_lengths(pSql); int32_t * length = taos_fetch_lengths(pSql);
for (int i = 0; i < num_fields; i++) { for (int32_t i = 0; i < num_fields; i++) {
char *value = NULL; char *value = NULL;
if (i < MAX_QUERY_COL_NUM) { if (i < MAX_QUERY_COL_NUM) {
value = script->data[numOfRows][i]; value = script->data[numOfRows][i];
...@@ -735,8 +739,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { ...@@ -735,8 +739,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
switch (fields[i].type) { switch (fields[i].type) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
sprintf(value, "%s", sprintf(value, "%s", ((((int32_t)(*((char *)row[i]))) == 1) ? "1" : "0"));
((((int)(*((char *)row[i]))) == 1) ? "1" : "0"));
break; break;
case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_TINYINT:
sprintf(value, "%d", *((int8_t *)row[i])); sprintf(value, "%d", *((int8_t *)row[i]));
...@@ -779,9 +782,8 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { ...@@ -779,9 +782,8 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
tp = localtime(&tt); tp = localtime(&tt);
strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", tp); strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", tp);
sprintf(value, "%s.%03d", timeStr, sprintf(value, "%s.%03d", timeStr, (int32_t)(*((int64_t *)row[i]) % 1000));
(int)(*((int64_t *)row[i]) % 1000));
break; break;
default: default:
break; break;
...@@ -814,17 +816,16 @@ bool simExecuteRestFulSqlCommand(SScript *script, char *rest) { ...@@ -814,17 +816,16 @@ bool simExecuteRestFulSqlCommand(SScript *script, char *rest) {
char command[4096]; char command[4096];
sprintf(command, "curl -H 'Authorization: Taosd %s' -d \"%s\" 127.0.0.1:6041/rest/sql", script->auth, rest); sprintf(command, "curl -H 'Authorization: Taosd %s' -d \"%s\" 127.0.0.1:6041/rest/sql", script->auth, rest);
int ret = -1; int32_t ret = -1;
for (int attempt = 0; attempt < 10; ++attempt) { for (int32_t attempt = 0; attempt < 10; ++attempt) {
ret = simExecuteRestFulCommand(script, command); ret = simExecuteRestFulCommand(script, command);
if (ret == TSDB_CODE_MND_TABLE_ALREADY_EXIST || if (ret == TSDB_CODE_MND_TABLE_ALREADY_EXIST || ret == TSDB_CODE_MND_DB_ALREADY_EXIST) {
ret == TSDB_CODE_MND_DB_ALREADY_EXIST) { simDebug("script:%s, taos:%p, %s success, ret:%d:%s", script->fileName, script->taos, rest, ret & 0XFFFF,
simDebug("script:%s, taos:%p, %s success, ret:%d:%s", script->fileName, script->taos, rest, ret & 0XFFFF, tstrerror(ret)); tstrerror(ret));
ret = 0; ret = 0;
break; break;
} else if (ret != 0) { } else if (ret != 0) {
simDebug("script:%s, taos:%p, %s failed, ret:%d", simDebug("script:%s, taos:%p, %s failed, ret:%d", script->fileName, script->taos, rest, ret);
script->fileName, script->taos, rest, ret);
if (line->errorJump == SQL_JUMP_TRUE) { if (line->errorJump == SQL_JUMP_TRUE) {
script->linePos = line->jump; script->linePos = line->jump;
...@@ -854,8 +855,8 @@ bool simExecuteSqlImpCmd(SScript *script, char *rest, bool isSlow) { ...@@ -854,8 +855,8 @@ bool simExecuteSqlImpCmd(SScript *script, char *rest, bool isSlow) {
simDebug("script:%s, exec:%s", script->fileName, rest); simDebug("script:%s, exec:%s", script->fileName, rest);
strcpy(script->rows, "-1"); strcpy(script->rows, "-1");
for (int row = 0; row < MAX_QUERY_ROW_NUM; ++row) { for (int32_t row = 0; row < MAX_QUERY_ROW_NUM; ++row) {
for (int col = 0; col < MAX_QUERY_COL_NUM; ++col) { for (int32_t col = 0; col < MAX_QUERY_COL_NUM; ++col) {
strcpy(script->data[row][col], "null"); strcpy(script->data[row][col], "null");
} }
} }
...@@ -903,23 +904,23 @@ bool simExecuteSqlSlowCmd(SScript *script, char *rest) { ...@@ -903,23 +904,23 @@ bool simExecuteSqlSlowCmd(SScript *script, char *rest) {
bool simExecuteRestfulCmd(SScript *script, char *rest) { bool simExecuteRestfulCmd(SScript *script, char *rest) {
FILE *fp = NULL; FILE *fp = NULL;
char filename[256]; char filename[256];
sprintf(filename, "%s/tmp.sql", tsScriptDir); sprintf(filename, "%s/tmp.sql", tsScriptDir);
fp = fopen(filename, "w"); fp = fopen(filename, "w");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "ERROR: failed to open file: %s\n", filename); fprintf(stderr, "ERROR: failed to open file: %s\n", filename);
return false; return false;
} }
char db[64] = {0}; char db[64] = {0};
char tb[64] = {0}; char tb[64] = {0};
char gzip[32] = {0}; char gzip[32] = {0};
int32_t ts; int32_t ts;
int32_t times; int32_t times;
sscanf(rest, "%s %s %d %d %s", db, tb, &ts, &times, gzip); sscanf(rest, "%s %s %d %d %s", db, tb, &ts, &times, gzip);
fprintf(fp, "insert into %s.%s values ", db, tb); fprintf(fp, "insert into %s.%s values ", db, tb);
for (int i = 0; i < times; ++i) { for (int32_t i = 0; i < times; ++i) {
fprintf(fp, "(%d000, %d)", ts + i, ts); fprintf(fp, "(%d000, %d)", ts + i, ts);
} }
fprintf(fp, " \n"); fprintf(fp, " \n");
...@@ -951,8 +952,8 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) { ...@@ -951,8 +952,8 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) {
simDebug("script:%s, exec:%s", script->fileName, rest); simDebug("script:%s, exec:%s", script->fileName, rest);
strcpy(script->rows, "-1"); strcpy(script->rows, "-1");
for (int row = 0; row < MAX_QUERY_ROW_NUM; ++row) { for (int32_t row = 0; row < MAX_QUERY_ROW_NUM; ++row) {
for (int col = 0; col < MAX_QUERY_COL_NUM; ++col) { for (int32_t col = 0; col < MAX_QUERY_COL_NUM; ++col) {
strcpy(script->data[row][col], "null"); strcpy(script->data[row][col], "null");
} }
} }
...@@ -981,27 +982,27 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) { ...@@ -981,27 +982,27 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) {
return true; return true;
} }
int ret; int32_t ret;
TAOS_RES* pSql = NULL; TAOS_RES *pSql = NULL;
if (simAsyncQuery) { if (simAsyncQuery) {
char command[4096]; char command[4096];
sprintf(command, "curl -H 'Authorization: Taosd %s' -d '%s' 127.0.0.1:6041/rest/sql", script->auth, rest); sprintf(command, "curl -H 'Authorization: Taosd %s' -d '%s' 127.0.0.1:6041/rest/sql", script->auth, rest);
ret = simExecuteRestFulCommand(script, command); ret = simExecuteRestFulCommand(script, command);
} } else {
else {
pSql = taos_query(script->taos, rest); pSql = taos_query(script->taos, rest);
ret = taos_errno(pSql); ret = taos_errno(pSql);
taos_free_result(pSql); taos_free_result(pSql);
} }
if (ret != TSDB_CODE_SUCCESS) { if (ret != TSDB_CODE_SUCCESS) {
simDebug("script:%s, taos:%p, %s execute, expect failed, so success, ret:%d:%s", simDebug("script:%s, taos:%p, %s execute, expect failed, so success, ret:%d:%s", script->fileName, script->taos,
script->fileName, script->taos, rest, ret & 0XFFFF, tstrerror(ret)); rest, ret & 0XFFFF, tstrerror(ret));
script->linePos++; script->linePos++;
return true; return true;
} }
sprintf(script->error, "lineNum:%d. sql:%s expect failed, but success, ret:%d:%s", line->lineNum, rest, ret & 0XFFFF, tstrerror(ret)); sprintf(script->error, "lineNum:%d. sql:%s expect failed, but success, ret:%d:%s", line->lineNum, rest, ret & 0XFFFF,
tstrerror(ret));
return false; return false;
} }
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _DEFAULT_SOURCE
#include "os.h" #include "os.h"
#include "tglobal.h" #include "tglobal.h"
#include "sim.h" #include "sim.h"
...@@ -20,15 +21,15 @@ ...@@ -20,15 +21,15 @@
bool simAsyncQuery = false; bool simAsyncQuery = false;
void simHandleSignal(int signo) { void simHandleSignal(int32_t signo) {
simSystemCleanUp(); simSystemCleanUp();
exit(1); exit(1);
} }
int main(int argc, char *argv[]) { int32_t main(int32_t argc, char *argv[]) {
char scriptFile[MAX_FILE_NAME_LEN] = "sim_main_test.sim"; char scriptFile[MAX_FILE_NAME_LEN] = "sim_main_test.sim";
for (int i = 1; i < argc; ++i) { for (int32_t i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-c") == 0 && i < argc - 1) { if (strcmp(argv[i], "-c") == 0 && i < argc - 1) {
tstrncpy(configDir, argv[++i], MAX_FILE_NAME_LEN); tstrncpy(configDir, argv[++i], MAX_FILE_NAME_LEN);
} else if (strcmp(argv[i], "-f") == 0 && i < argc - 1) { } else if (strcmp(argv[i], "-f") == 0 && i < argc - 1) {
...@@ -37,8 +38,7 @@ int main(int argc, char *argv[]) { ...@@ -37,8 +38,7 @@ int main(int argc, char *argv[]) {
simAsyncQuery = true; simAsyncQuery = true;
} else { } else {
printf("usage: %s [options] \n", argv[0]); printf("usage: %s [options] \n", argv[0]);
printf(" [-c config]: config directory, default is: %s\n", printf(" [-c config]: config directory, default is: %s\n", configDir);
configDir);
printf(" [-f script]: script filename\n"); printf(" [-f script]: script filename\n");
exit(0); exit(0);
} }
......
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
* *
*/ */
#define _DEFAULT_SOURCE
#include "os.h" #include "os.h"
#include "sim.h" #include "sim.h"
#include "simParse.h" #include "simParse.h"
...@@ -64,16 +65,16 @@ ...@@ -64,16 +65,16 @@
#undef TAOS_MEM_CHECK #undef TAOS_MEM_CHECK
static SCommand *cmdHashList[MAX_NUM_CMD]; static SCommand *cmdHashList[MAX_NUM_CMD];
static SCmdLine cmdLine[MAX_CMD_LINES]; static SCmdLine cmdLine[MAX_CMD_LINES];
static char parseErr[MAX_ERROR_LEN]; static char parseErr[MAX_ERROR_LEN];
static char optionBuffer[MAX_OPTION_BUFFER]; static char optionBuffer[MAX_OPTION_BUFFER];
static int numOfLines, optionOffset; static int32_t numOfLines, optionOffset;
static SLabel label, dest; static SLabel label, dest;
static SBlock block; static SBlock block;
int simHashCmd(char *token, int tokenLen) { int32_t simHashCmd(char *token, int32_t tokenLen) {
int i; int32_t i;
int hash = 0; int32_t hash = 0;
for (i = 0; i < tokenLen; ++i) hash += token[i]; for (i = 0; i < tokenLen; ++i) hash += token[i];
...@@ -82,8 +83,8 @@ int simHashCmd(char *token, int tokenLen) { ...@@ -82,8 +83,8 @@ int simHashCmd(char *token, int tokenLen) {
return hash; return hash;
} }
SCommand *simCheckCmd(char *token, int tokenLen) { SCommand *simCheckCmd(char *token, int32_t tokenLen) {
int hash; int32_t hash;
SCommand *node; SCommand *node;
hash = simHashCmd(token, tokenLen); hash = simHashCmd(token, tokenLen);
...@@ -102,10 +103,10 @@ SCommand *simCheckCmd(char *token, int tokenLen) { ...@@ -102,10 +103,10 @@ SCommand *simCheckCmd(char *token, int tokenLen) {
} }
void simAddCmdIntoHash(SCommand *pCmd) { void simAddCmdIntoHash(SCommand *pCmd) {
int hash; int32_t hash;
SCommand *node; SCommand *node;
hash = simHashCmd(pCmd->name, (int)strlen(pCmd->name)); hash = simHashCmd(pCmd->name, (int32_t)strlen(pCmd->name));
node = cmdHashList[hash]; node = cmdHashList[hash];
pCmd->next = node; pCmd->next = node;
cmdHashList[hash] = pCmd; cmdHashList[hash] = pCmd;
...@@ -122,7 +123,7 @@ void simResetParser() { ...@@ -122,7 +123,7 @@ void simResetParser() {
} }
SScript *simBuildScriptObj(char *fileName) { SScript *simBuildScriptObj(char *fileName) {
int i, destPos; int32_t i, destPos;
/* process labels */ /* process labels */
...@@ -176,11 +177,11 @@ SScript *simBuildScriptObj(char *fileName) { ...@@ -176,11 +177,11 @@ SScript *simBuildScriptObj(char *fileName) {
} }
SScript *simParseScript(char *fileName) { SScript *simParseScript(char *fileName) {
FILE *fd; FILE * fd;
int tokenLen, lineNum = 0; int32_t tokenLen, lineNum = 0;
char buffer[MAX_LINE_LEN], name[128], *token, *rest; char buffer[MAX_LINE_LEN], name[128], *token, *rest;
SCommand *pCmd; SCommand *pCmd;
SScript *script; SScript * script;
if ((fileName[0] == '.') || (fileName[0] == '/')) { if ((fileName[0] == '.') || (fileName[0] == '/')) {
strcpy(name, fileName); strcpy(name, fileName);
...@@ -199,12 +200,13 @@ SScript *simParseScript(char *fileName) { ...@@ -199,12 +200,13 @@ SScript *simParseScript(char *fileName) {
if (fgets(buffer, sizeof(buffer), fd) == NULL) continue; if (fgets(buffer, sizeof(buffer), fd) == NULL) continue;
lineNum++; lineNum++;
int cmdlen = (int)strlen(buffer); int32_t cmdlen = (int32_t)strlen(buffer);
if (buffer[cmdlen - 1] == '\r' || buffer[cmdlen - 1] == '\n') if (buffer[cmdlen - 1] == '\r' || buffer[cmdlen - 1] == '\n') {
buffer[cmdlen - 1] = 0; buffer[cmdlen - 1] = 0;
}
rest = buffer; rest = buffer;
for (int i = 0; i < cmdlen; ++i) { for (int32_t i = 0; i < cmdlen; ++i) {
if (buffer[i] == '\r' || buffer[i] == '\n') { if (buffer[i] == '\r' || buffer[i] == '\n') {
buffer[i] = ' '; buffer[i] = ' ';
} }
...@@ -249,9 +251,9 @@ SScript *simParseScript(char *fileName) { ...@@ -249,9 +251,9 @@ SScript *simParseScript(char *fileName) {
return script; return script;
} }
int simCheckExpression(char *exp) { int32_t simCheckExpression(char *exp) {
char *op1, *op2, *op, *rest; char * op1, *op2, *op, *rest;
int op1Len, op2Len, opLen; int32_t op1Len, op2Len, opLen;
rest = paGetToken(exp, &op1, &op1Len); rest = paGetToken(exp, &op1, &op1Len);
if (op1Len == 0) { if (op1Len == 0) {
...@@ -282,8 +284,7 @@ int simCheckExpression(char *exp) { ...@@ -282,8 +284,7 @@ int simCheckExpression(char *exp) {
return -1; return -1;
} }
} else if (opLen == 2) { } else if (opLen == 2) {
if (op[1] != '=' || if (op[1] != '=' || (op[0] != '=' && op[0] != '<' && op[0] != '>' && op[0] != '!')) {
(op[0] != '=' && op[0] != '<' && op[0] != '>' && op[0] != '!')) {
sprintf(parseErr, "left side of assignment must be variable"); sprintf(parseErr, "left side of assignment must be variable");
return -1; return -1;
} }
...@@ -294,10 +295,10 @@ int simCheckExpression(char *exp) { ...@@ -294,10 +295,10 @@ int simCheckExpression(char *exp) {
rest = paGetToken(rest, &op, &opLen); rest = paGetToken(rest, &op, &opLen);
if (opLen == 0) return (int)(rest - exp); if (opLen == 0) return (int32_t)(rest - exp);
/* if it is key word "then" */ /* if it is key word "then" */
if (strncmp(op, "then", 4) == 0) return (int)(op - exp); if (strncmp(op, "then", 4) == 0) return (int32_t)(op - exp);
rest = paGetToken(rest, &op2, &op2Len); rest = paGetToken(rest, &op2, &op2Len);
if (op2Len == 0) { if (op2Len == 0) {
...@@ -310,16 +311,15 @@ int simCheckExpression(char *exp) { ...@@ -310,16 +311,15 @@ int simCheckExpression(char *exp) {
return -1; return -1;
} }
if (op[0] == '+' || op[0] == '-' || op[0] == '*' || op[0] == '/' || if (op[0] == '+' || op[0] == '-' || op[0] == '*' || op[0] == '/' || op[0] == '.') {
op[0] == '.') { return (int32_t)(rest - exp);
return (int)(rest - exp);
} }
return -1; return -1;
} }
bool simParseExpression(char *token, int lineNum) { bool simParseExpression(char *token, int32_t lineNum) {
int expLen; int32_t expLen;
expLen = simCheckExpression(token); expLen = simCheckExpression(token);
if (expLen <= 0) return -1; if (expLen <= 0) return -1;
...@@ -335,9 +335,9 @@ bool simParseExpression(char *token, int lineNum) { ...@@ -335,9 +335,9 @@ bool simParseExpression(char *token, int lineNum) {
return true; return true;
} }
bool simParseIfCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseIfCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *ret; char * ret;
int expLen; int32_t expLen;
expLen = simCheckExpression(rest); expLen = simCheckExpression(rest);
...@@ -364,8 +364,8 @@ bool simParseIfCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -364,8 +364,8 @@ bool simParseIfCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseElifCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseElifCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int expLen; int32_t expLen;
expLen = simCheckExpression(rest); expLen = simCheckExpression(rest);
...@@ -382,8 +382,7 @@ bool simParseElifCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -382,8 +382,7 @@ bool simParseElifCmd(char *rest, SCommand *pCmd, int lineNum) {
} }
cmdLine[numOfLines].cmdno = SIM_CMD_GOTO; cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = &(cmdLine[numOfLines].jump);
&(cmdLine[numOfLines].jump);
block.numJump[block.top - 1]++; block.numJump[block.top - 1]++;
numOfLines++; numOfLines++;
...@@ -402,7 +401,7 @@ bool simParseElifCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -402,7 +401,7 @@ bool simParseElifCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseElseCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseElseCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no matching if"); sprintf(parseErr, "no matching if");
return false; return false;
...@@ -414,8 +413,7 @@ bool simParseElseCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -414,8 +413,7 @@ bool simParseElseCmd(char *rest, SCommand *pCmd, int lineNum) {
} }
cmdLine[numOfLines].cmdno = SIM_CMD_GOTO; cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = &(cmdLine[numOfLines].jump);
&(cmdLine[numOfLines].jump);
block.numJump[block.top - 1]++; block.numJump[block.top - 1]++;
numOfLines++; numOfLines++;
...@@ -426,8 +424,8 @@ bool simParseElseCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -426,8 +424,8 @@ bool simParseElseCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseEndiCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseEndiCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int i; int32_t i;
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no matching if"); sprintf(parseErr, "no matching if");
...@@ -441,8 +439,9 @@ bool simParseEndiCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -441,8 +439,9 @@ bool simParseEndiCmd(char *rest, SCommand *pCmd, int lineNum) {
if (block.pos[block.top - 1]) *(block.pos[block.top - 1]) = numOfLines; if (block.pos[block.top - 1]) *(block.pos[block.top - 1]) = numOfLines;
for (i = 0; i < block.numJump[block.top - 1]; ++i) for (i = 0; i < block.numJump[block.top - 1]; ++i) {
*(block.jump[block.top - 1][i]) = numOfLines; *(block.jump[block.top - 1][i]) = numOfLines;
}
block.numJump[block.top - 1] = 0; block.numJump[block.top - 1] = 0;
block.top--; block.top--;
...@@ -450,8 +449,8 @@ bool simParseEndiCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -450,8 +449,8 @@ bool simParseEndiCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseWhileCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseWhileCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int expLen; int32_t expLen;
expLen = simCheckExpression(rest); expLen = simCheckExpression(rest);
...@@ -473,8 +472,8 @@ bool simParseWhileCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -473,8 +472,8 @@ bool simParseWhileCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseEndwCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseEndwCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int i; int32_t i;
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no matching while"); sprintf(parseErr, "no matching while");
...@@ -493,17 +492,18 @@ bool simParseEndwCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -493,17 +492,18 @@ bool simParseEndwCmd(char *rest, SCommand *pCmd, int lineNum) {
*(block.pos[block.top - 1]) = numOfLines; *(block.pos[block.top - 1]) = numOfLines;
for (i = 0; i < block.numJump[block.top - 1]; ++i) for (i = 0; i < block.numJump[block.top - 1]; ++i) {
*(block.jump[block.top - 1][i]) = numOfLines; *(block.jump[block.top - 1][i]) = numOfLines;
}
block.top--; block.top--;
return true; return true;
} }
bool simParseSwitchCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSwitchCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *token; char * token;
int tokenLen; int32_t tokenLen;
rest = paGetToken(rest, &token, &tokenLen); rest = paGetToken(rest, &token, &tokenLen);
if (tokenLen == 0) { if (tokenLen == 0) {
...@@ -524,9 +524,9 @@ bool simParseSwitchCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -524,9 +524,9 @@ bool simParseSwitchCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseCaseCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseCaseCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *token; char * token;
int tokenLen; int32_t tokenLen;
rest = paGetToken(rest, &token, &tokenLen); rest = paGetToken(rest, &token, &tokenLen);
if (tokenLen == 0) { if (tokenLen == 0) {
...@@ -544,16 +544,16 @@ bool simParseCaseCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -544,16 +544,16 @@ bool simParseCaseCmd(char *rest, SCommand *pCmd, int lineNum) {
return false; return false;
} }
if (block.pos[block.top - 1] != NULL) if (block.pos[block.top - 1] != NULL) {
*(block.pos[block.top - 1]) = numOfLines; *(block.pos[block.top - 1]) = numOfLines;
}
block.pos[block.top - 1] = &(cmdLine[numOfLines].jump); block.pos[block.top - 1] = &(cmdLine[numOfLines].jump);
cmdLine[numOfLines].cmdno = SIM_CMD_TEST; cmdLine[numOfLines].cmdno = SIM_CMD_TEST;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
cmdLine[numOfLines].optionOffset = optionOffset; cmdLine[numOfLines].optionOffset = optionOffset;
memcpy(optionBuffer + optionOffset, block.sexp[block.top - 1], memcpy(optionBuffer + optionOffset, block.sexp[block.top - 1], block.sexpLen[block.top - 1]);
block.sexpLen[block.top - 1]);
optionOffset += block.sexpLen[block.top - 1]; optionOffset += block.sexpLen[block.top - 1];
*(optionBuffer + optionOffset++) = ' '; *(optionBuffer + optionOffset++) = ' ';
*(optionBuffer + optionOffset++) = '='; *(optionBuffer + optionOffset++) = '=';
...@@ -567,20 +567,18 @@ bool simParseCaseCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -567,20 +567,18 @@ bool simParseCaseCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseBreakCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseBreakCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no blcok exists"); sprintf(parseErr, "no blcok exists");
return false; return false;
} }
if (block.type[block.top - 1] != BLOCK_SWITCH && if (block.type[block.top - 1] != BLOCK_SWITCH && block.type[block.top - 1] != BLOCK_WHILE) {
block.type[block.top - 1] != BLOCK_WHILE) {
sprintf(parseErr, "not in switch or while block"); sprintf(parseErr, "not in switch or while block");
return false; return false;
} }
block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = &(cmdLine[numOfLines].jump);
&(cmdLine[numOfLines].jump);
block.numJump[block.top - 1]++; block.numJump[block.top - 1]++;
cmdLine[numOfLines].cmdno = SIM_CMD_GOTO; cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
...@@ -590,7 +588,7 @@ bool simParseBreakCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -590,7 +588,7 @@ bool simParseBreakCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseDefaultCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseDefaultCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no matching switch"); sprintf(parseErr, "no matching switch");
return false; return false;
...@@ -601,14 +599,15 @@ bool simParseDefaultCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -601,14 +599,15 @@ bool simParseDefaultCmd(char *rest, SCommand *pCmd, int lineNum) {
return false; return false;
} }
if (block.pos[block.top - 1] != NULL) if (block.pos[block.top - 1] != NULL) {
*(block.pos[block.top - 1]) = numOfLines; *(block.pos[block.top - 1]) = numOfLines;
}
return true; return true;
} }
bool simParseEndsCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseEndsCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int i; int32_t i;
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no matching switch"); sprintf(parseErr, "no matching switch");
...@@ -620,8 +619,9 @@ bool simParseEndsCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -620,8 +619,9 @@ bool simParseEndsCmd(char *rest, SCommand *pCmd, int lineNum) {
return false; return false;
} }
for (i = 0; i < block.numJump[block.top - 1]; ++i) for (i = 0; i < block.numJump[block.top - 1]; ++i) {
*(block.jump[block.top - 1][i]) = numOfLines; *(block.jump[block.top - 1][i]) = numOfLines;
}
block.numJump[block.top - 1] = 0; block.numJump[block.top - 1] = 0;
block.top--; block.top--;
...@@ -629,7 +629,7 @@ bool simParseEndsCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -629,7 +629,7 @@ bool simParseEndsCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseContinueCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseContinueCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
if (block.top < 1) { if (block.top < 1) {
sprintf(parseErr, "no matching while"); sprintf(parseErr, "no matching while");
return false; return false;
...@@ -648,14 +648,14 @@ bool simParseContinueCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -648,14 +648,14 @@ bool simParseContinueCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParsePrintCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParsePrintCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int expLen; int32_t expLen;
rest++; rest++;
cmdLine[numOfLines].cmdno = SIM_CMD_PRINT; cmdLine[numOfLines].cmdno = SIM_CMD_PRINT;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
cmdLine[numOfLines].optionOffset = optionOffset; cmdLine[numOfLines].optionOffset = optionOffset;
expLen = (int)strlen(rest); expLen = (int32_t)strlen(rest);
memcpy(optionBuffer + optionOffset, rest, expLen); memcpy(optionBuffer + optionOffset, rest, expLen);
optionOffset += expLen + 1; optionOffset += expLen + 1;
*(optionBuffer + optionOffset - 1) = 0; *(optionBuffer + optionOffset - 1) = 0;
...@@ -665,8 +665,8 @@ bool simParsePrintCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -665,8 +665,8 @@ bool simParsePrintCmd(char *rest, SCommand *pCmd, int lineNum) {
} }
void simCheckSqlOption(char *rest) { void simCheckSqlOption(char *rest) {
int valueLen; int32_t valueLen;
char *value, *xpos; char * value, *xpos;
xpos = strstr(rest, " -x"); // need a blank xpos = strstr(rest, " -x"); // need a blank
if (xpos) { if (xpos) {
...@@ -682,15 +682,15 @@ void simCheckSqlOption(char *rest) { ...@@ -682,15 +682,15 @@ void simCheckSqlOption(char *rest) {
} }
} }
bool simParseSqlCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSqlCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int expLen; int32_t expLen;
rest++; rest++;
simCheckSqlOption(rest); simCheckSqlOption(rest);
cmdLine[numOfLines].cmdno = SIM_CMD_SQL; cmdLine[numOfLines].cmdno = SIM_CMD_SQL;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
cmdLine[numOfLines].optionOffset = optionOffset; cmdLine[numOfLines].optionOffset = optionOffset;
expLen = (int)strlen(rest); expLen = (int32_t)strlen(rest);
memcpy(optionBuffer + optionOffset, rest, expLen); memcpy(optionBuffer + optionOffset, rest, expLen);
optionOffset += expLen + 1; optionOffset += expLen + 1;
*(optionBuffer + optionOffset - 1) = 0; *(optionBuffer + optionOffset - 1) = 0;
...@@ -699,14 +699,14 @@ bool simParseSqlCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -699,14 +699,14 @@ bool simParseSqlCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseSqlErrorCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSqlErrorCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int expLen; int32_t expLen;
rest++; rest++;
cmdLine[numOfLines].cmdno = SIM_CMD_SQL_ERROR; cmdLine[numOfLines].cmdno = SIM_CMD_SQL_ERROR;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
cmdLine[numOfLines].optionOffset = optionOffset; cmdLine[numOfLines].optionOffset = optionOffset;
expLen = (int)strlen(rest); expLen = (int32_t)strlen(rest);
memcpy(optionBuffer + optionOffset, rest, expLen); memcpy(optionBuffer + optionOffset, rest, expLen);
optionOffset += expLen + 1; optionOffset += expLen + 1;
*(optionBuffer + optionOffset - 1) = 0; *(optionBuffer + optionOffset - 1) = 0;
...@@ -715,26 +715,26 @@ bool simParseSqlErrorCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -715,26 +715,26 @@ bool simParseSqlErrorCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseSqlSlowCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSqlSlowCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
simParseSqlCmd(rest, pCmd, lineNum); simParseSqlCmd(rest, pCmd, lineNum);
cmdLine[numOfLines - 1].cmdno = SIM_CMD_SQL_SLOW; cmdLine[numOfLines - 1].cmdno = SIM_CMD_SQL_SLOW;
return true; return true;
} }
bool simParseRestfulCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseRestfulCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
simParseSqlCmd(rest, pCmd, lineNum); simParseSqlCmd(rest, pCmd, lineNum);
cmdLine[numOfLines - 1].cmdno = SIM_CMD_RESTFUL; cmdLine[numOfLines - 1].cmdno = SIM_CMD_RESTFUL;
return true; return true;
} }
bool simParseSystemCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSystemCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
int expLen; int32_t expLen;
rest++; rest++;
cmdLine[numOfLines].cmdno = SIM_CMD_SYSTEM; cmdLine[numOfLines].cmdno = SIM_CMD_SYSTEM;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
cmdLine[numOfLines].optionOffset = optionOffset; cmdLine[numOfLines].optionOffset = optionOffset;
expLen = (int)strlen(rest); expLen = (int32_t)strlen(rest);
memcpy(optionBuffer + optionOffset, rest, expLen); memcpy(optionBuffer + optionOffset, rest, expLen);
optionOffset += expLen + 1; optionOffset += expLen + 1;
*(optionBuffer + optionOffset - 1) = 0; *(optionBuffer + optionOffset - 1) = 0;
...@@ -743,15 +743,15 @@ bool simParseSystemCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -743,15 +743,15 @@ bool simParseSystemCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseSystemContentCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSystemContentCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
simParseSystemCmd(rest, pCmd, lineNum); simParseSystemCmd(rest, pCmd, lineNum);
cmdLine[numOfLines - 1].cmdno = SIM_CMD_SYSTEM_CONTENT; cmdLine[numOfLines - 1].cmdno = SIM_CMD_SYSTEM_CONTENT;
return true; return true;
} }
bool simParseSleepCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseSleepCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *token; char * token;
int tokenLen; int32_t tokenLen;
cmdLine[numOfLines].cmdno = SIM_CMD_SLEEP; cmdLine[numOfLines].cmdno = SIM_CMD_SLEEP;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
...@@ -768,9 +768,9 @@ bool simParseSleepCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -768,9 +768,9 @@ bool simParseSleepCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseReturnCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseReturnCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *token; char * token;
int tokenLen; int32_t tokenLen;
cmdLine[numOfLines].cmdno = SIM_CMD_RETURN; cmdLine[numOfLines].cmdno = SIM_CMD_RETURN;
cmdLine[numOfLines].lineNum = lineNum; cmdLine[numOfLines].lineNum = lineNum;
...@@ -787,9 +787,9 @@ bool simParseReturnCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -787,9 +787,9 @@ bool simParseReturnCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseGotoCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseGotoCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *token; char * token;
int tokenLen; int32_t tokenLen;
rest = paGetToken(rest, &token, &tokenLen); rest = paGetToken(rest, &token, &tokenLen);
...@@ -810,9 +810,9 @@ bool simParseGotoCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -810,9 +810,9 @@ bool simParseGotoCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseRunCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseRunCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
char *token; char * token;
int tokenLen; int32_t tokenLen;
rest = paGetToken(rest, &token, &tokenLen); rest = paGetToken(rest, &token, &tokenLen);
...@@ -832,14 +832,14 @@ bool simParseRunCmd(char *rest, SCommand *pCmd, int lineNum) { ...@@ -832,14 +832,14 @@ bool simParseRunCmd(char *rest, SCommand *pCmd, int lineNum) {
return true; return true;
} }
bool simParseRunBackCmd(char *rest, SCommand *pCmd, int lineNum) { bool simParseRunBackCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
simParseRunCmd(rest, pCmd, lineNum); simParseRunCmd(rest, pCmd, lineNum);
cmdLine[numOfLines - 1].cmdno = SIM_CMD_RUN_BACK; cmdLine[numOfLines - 1].cmdno = SIM_CMD_RUN_BACK;
return true; return true;
} }
void simInitsimCmdList() { void simInitsimCmdList() {
int cmdno; int32_t cmdno;
memset(simCmdList, 0, SIM_CMD_END * sizeof(SCommand)); memset(simCmdList, 0, SIM_CMD_END * sizeof(SCommand));
/* internal command */ /* internal command */
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _DEFAULT_SOURCE
#include "os.h" #include "os.h"
#include "sim.h" #include "sim.h"
#include "taos.h" #include "taos.h"
...@@ -24,11 +25,11 @@ ...@@ -24,11 +25,11 @@
SScript *simScriptList[MAX_MAIN_SCRIPT_NUM]; SScript *simScriptList[MAX_MAIN_SCRIPT_NUM];
SCommand simCmdList[SIM_CMD_END]; SCommand simCmdList[SIM_CMD_END];
int simScriptPos = -1; int32_t simScriptPos = -1;
int simScriptSucced = 0; int32_t simScriptSucced = 0;
int simDebugFlag = 135; int32_t simDebugFlag = 135;
void simCloseTaosdConnect(SScript *script); void simCloseTaosdConnect(SScript *script);
char simHostName[128]; char simHostName[128];
char *simParseArbitratorName(char *varName) { char *simParseArbitratorName(char *varName) {
static char hostName[140]; static char hostName[140];
...@@ -39,8 +40,8 @@ char *simParseArbitratorName(char *varName) { ...@@ -39,8 +40,8 @@ char *simParseArbitratorName(char *varName) {
char *simParseHostName(char *varName) { char *simParseHostName(char *varName) {
static char hostName[140]; static char hostName[140];
int index = atoi(varName + 8); int32_t index = atoi(varName + 8);
int port = 7100; int32_t port = 7100;
switch (index) { switch (index) {
case 1: case 1:
port = 7100; port = 7100;
...@@ -70,9 +71,9 @@ char *simParseHostName(char *varName) { ...@@ -70,9 +71,9 @@ char *simParseHostName(char *varName) {
port = 7900; port = 7900;
break; break;
} }
sprintf(hostName, "'%s:%d'", simHostName, port); sprintf(hostName, "'%s:%d'", simHostName, port);
//simInfo("hostName:%s", hostName); // simInfo("hostName:%s", hostName);
return hostName; return hostName;
} }
...@@ -88,12 +89,19 @@ void simSystemCleanUp() {} ...@@ -88,12 +89,19 @@ void simSystemCleanUp() {}
void simFreeScript(SScript *script) { void simFreeScript(SScript *script) {
if (script->type == SIM_SCRIPT_TYPE_MAIN) { if (script->type == SIM_SCRIPT_TYPE_MAIN) {
for (int i = 0; i < script->bgScriptLen; ++i) { simInfo("script:%s, background script num:%d, stop them", script->fileName, script->bgScriptLen);
for (int32_t i = 0; i < script->bgScriptLen; ++i) {
SScript *bgScript = script->bgScripts[i]; SScript *bgScript = script->bgScripts[i];
simInfo("script:%s, set stop flag", script->fileName);
bgScript->killed = true; bgScript->killed = true;
if (bgScript->bgPid) {
pthread_join(bgScript->bgPid, NULL);
}
} }
} }
simDebug("script:%s, is freed", script->fileName);
taos_close(script->taos); taos_close(script->taos);
taosTFree(script->lines); taosTFree(script->lines);
taosTFree(script->optionBuffer); taosTFree(script->optionBuffer);
...@@ -103,25 +111,24 @@ void simFreeScript(SScript *script) { ...@@ -103,25 +111,24 @@ void simFreeScript(SScript *script) {
SScript *simProcessCallOver(SScript *script) { SScript *simProcessCallOver(SScript *script) {
if (script->type == SIM_SCRIPT_TYPE_MAIN) { if (script->type == SIM_SCRIPT_TYPE_MAIN) {
if (script->killed) { if (script->killed) {
simInfo("script:" FAILED_PREFIX "%s" FAILED_POSTFIX ", " FAILED_PREFIX simInfo("script:" FAILED_PREFIX "%s" FAILED_POSTFIX ", " FAILED_PREFIX "failed" FAILED_POSTFIX ", error:%s",
"failed" FAILED_POSTFIX ", error:%s", script->fileName, script->error);
script->fileName, script->error);
exit(-1); exit(-1);
} else { } else {
simInfo("script:" SUCCESS_PREFIX "%s" SUCCESS_POSTFIX ", " SUCCESS_PREFIX simInfo("script:" SUCCESS_PREFIX "%s" SUCCESS_POSTFIX ", " SUCCESS_PREFIX "success" SUCCESS_POSTFIX,
"success" SUCCESS_POSTFIX, script->fileName);
script->fileName);
simCloseTaosdConnect(script); simCloseTaosdConnect(script);
simScriptSucced++; simScriptSucced++;
simScriptPos--; simScriptPos--;
simFreeScript(script);
if (simScriptPos == -1) { if (simScriptPos == -1) {
simInfo("----------------------------------------------------------------------"); simInfo("----------------------------------------------------------------------");
simInfo("Simulation Test Done, " SUCCESS_PREFIX "%d" SUCCESS_POSTFIX " Passed:\n", simScriptSucced); simInfo("Simulation Test Done, " SUCCESS_PREFIX "%d" SUCCESS_POSTFIX " Passed:\n", simScriptSucced);
exit(0); exit(0);
} }
simFreeScript(script); return NULL;
return simScriptList[simScriptPos];
} }
} else { } else {
simInfo("script:%s, is stopped by main script", script->fileName); simInfo("script:%s, is stopped by main script", script->fileName);
...@@ -143,11 +150,11 @@ void *simExecuteScript(void *inputScript) { ...@@ -143,11 +150,11 @@ void *simExecuteScript(void *inputScript) {
if (script == NULL) break; if (script == NULL) break;
} else { } else {
SCmdLine *line = &script->lines[script->linePos]; SCmdLine *line = &script->lines[script->linePos];
char *option = script->optionBuffer + line->optionOffset; char * option = script->optionBuffer + line->optionOffset;
simDebug("script:%s, line:%d with option \"%s\"", script->fileName, line->lineNum, option); simDebug("script:%s, line:%d with option \"%s\"", script->fileName, line->lineNum, option);
SCommand *cmd = &simCmdList[line->cmdno]; SCommand *cmd = &simCmdList[line->cmdno];
int ret = (*(cmd->executeCmd))(script, option); int32_t ret = (*(cmd->executeCmd))(script, option);
if (!ret) { if (!ret) {
script->killed = true; script->killed = true;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册