simParse.c 28.3 KB
Newer Older
S
[TD-73]  
slguan 已提交
1 2
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
S
slguan 已提交
3
 *
S
[TD-73]  
slguan 已提交
4 5 6
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
S
slguan 已提交
7
 *
S
[TD-73]  
slguan 已提交
8 9 10 11 12 13 14
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
S
slguan 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

/* Thi file is to parse the simScriptPos, function file and default file,
 *
 * syntax
 *
 *    expression is format like:
 *       $var [=|<|>|==] var
 *     or:
 *       $var = var [+|-|*|/|.] var
 *
 *    if expression
 *      command
 *
 *    if expression then
 *      commands
 *    elif expression
 *      commands
 *    elif expression
 *      commands
 *    else
 *      commands
 *    endi
 *
 *    while expression
 *      commands
 *      continue
 *      break
 *    endw
 *
 *    switch expression
 *      case 1
 *        commands
 *        break
 *      case 2
 *        commands
 *        break
 *      default
 *        commands
 *    ends
 *
 *    label:
 *    goto label
 *
 */

H
Haojun Liao 已提交
60
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
61
#include "os.h"
S
slguan 已提交
62 63 64
#include "sim.h"
#include "simParse.h"
#include "tutil.h"
S
Shengliang Guan 已提交
65
#undef TAOS_MEM_CHECK
S
slguan 已提交
66 67

static SCommand *cmdHashList[MAX_NUM_CMD];
H
Haojun Liao 已提交
68 69 70 71 72 73
static SCmdLine  cmdLine[MAX_CMD_LINES];
static char      parseErr[MAX_ERROR_LEN];
static char      optionBuffer[MAX_OPTION_BUFFER];
static int32_t   numOfLines, optionOffset;
static SLabel    label, dest;
static SBlock    block;
S
slguan 已提交
74

H
Haojun Liao 已提交
75 76 77
int32_t simHashCmd(char *token, int32_t tokenLen) {
  int32_t i;
  int32_t hash = 0;
S
slguan 已提交
78 79 80 81 82 83 84 85

  for (i = 0; i < tokenLen; ++i) hash += token[i];

  hash = hash % MAX_NUM_CMD;

  return hash;
}

H
Haojun Liao 已提交
86 87
SCommand *simCheckCmd(char *token, int32_t tokenLen) {
  int32_t   hash;
S
slguan 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  SCommand *node;

  hash = simHashCmd(token, tokenLen);

  node = cmdHashList[hash];

  while (node) {
    if (node->nlen == tokenLen && strncmp(node->name, token, tokenLen) == 0) {
      return node;
    } else {
      node = node->next;
    }
  }

  return NULL;
}

void simAddCmdIntoHash(SCommand *pCmd) {
H
Haojun Liao 已提交
106
  int32_t   hash;
S
slguan 已提交
107 108
  SCommand *node;

H
Haojun Liao 已提交
109
  hash = simHashCmd(pCmd->name, (int32_t)strlen(pCmd->name));
S
slguan 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  node = cmdHashList[hash];
  pCmd->next = node;
  cmdHashList[hash] = pCmd;
}

void simResetParser() {
  optionOffset = 4;
  numOfLines = 0;
  memset(cmdLine, 0, sizeof(cmdLine));
  memset(optionBuffer, 0, sizeof(optionBuffer));
  memset(&label, 0, sizeof(label));
  memset(&block, 0, sizeof(block));
  memset(&dest, 0, sizeof(dest));
}

SScript *simBuildScriptObj(char *fileName) {
H
Haojun Liao 已提交
126
  int32_t i, destPos;
S
slguan 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

  /* process labels */

  cmdLine[numOfLines].cmdno = SIM_CMD_RETURN;
  numOfLines++;

  for (i = 0; i < numOfLines; ++i) {
    cmdLine[i].errorJump = SQL_JUMP_FALSE;
  }

  for (--dest.top; dest.top >= 0; --dest.top) {
    for (i = 0; i < label.top; ++i) {
      if (strcmp(label.label[i], dest.label[(uint8_t)dest.top]) == 0) break;
    }

    if (i == label.top) {
      sprintf(parseErr, "label:%s not defined", dest.label[(uint8_t)dest.top]);
      return NULL;
    }

    destPos = dest.pos[(uint8_t)dest.top];
    cmdLine[destPos].jump = label.pos[i];
    if (cmdLine[destPos].cmdno == SIM_CMD_SQL) {
      cmdLine[destPos].errorJump = SQL_JUMP_TRUE;
    }
  }

  if (block.top != 0) {
    sprintf(parseErr, "mismatched block");
    return NULL;
  }

  for (i = 0; i < numOfLines; ++i) {
    if (cmdLine[i].jump == 0) cmdLine[i].jump = numOfLines;
  }

  SScript *script = malloc(sizeof(SScript));
  memset(script, 0, sizeof(SScript));

  script->type = SIM_SCRIPT_TYPE_MAIN;
  script->numOfLines = numOfLines;
B
Bomin Zhang 已提交
168
  tstrncpy(script->fileName, fileName, sizeof(script->fileName));
S
slguan 已提交
169 170 171 172 173 174 175 176 177 178 179

  script->optionBuffer = malloc(optionOffset);
  memcpy(script->optionBuffer, optionBuffer, optionOffset);

  script->lines = malloc(sizeof(SCmdLine) * numOfLines);
  memcpy(script->lines, cmdLine, sizeof(SCmdLine) * numOfLines);

  return script;
}

SScript *simParseScript(char *fileName) {
H
Haojun Liao 已提交
180 181 182
  FILE *    fd;
  int32_t   tokenLen, lineNum = 0;
  char      buffer[MAX_LINE_LEN], name[128], *token, *rest;
S
slguan 已提交
183
  SCommand *pCmd;
H
Haojun Liao 已提交
184
  SScript * script;
S
slguan 已提交
185 186 187 188

  if ((fileName[0] == '.') || (fileName[0] == '/')) {
    strcpy(name, fileName);
  } else {
S
slguan 已提交
189
    sprintf(name, "%s/%s", tsScriptDir, fileName);
S
slguan 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202
  }

  if ((fd = fopen(name, "r")) == NULL) {
    simError("failed to open file:%s", name);
    return NULL;
  }

  simResetParser();

  while (!feof(fd)) {
    if (fgets(buffer, sizeof(buffer), fd) == NULL) continue;

    lineNum++;
H
Haojun Liao 已提交
203 204
    int32_t cmdlen = (int32_t)strlen(buffer);
    if (buffer[cmdlen - 1] == '\r' || buffer[cmdlen - 1] == '\n') {
S
slguan 已提交
205
      buffer[cmdlen - 1] = 0;
H
Haojun Liao 已提交
206
    }
S
slguan 已提交
207 208
    rest = buffer;

H
Haojun Liao 已提交
209
    for (int32_t i = 0; i < cmdlen; ++i) {
S
slguan 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
      if (buffer[i] == '\r' || buffer[i] == '\n') {
        buffer[i] = ' ';
      }
    }

  again:
    rest = paGetToken(rest, &token, &tokenLen);
    if (tokenLen == 0) continue;

    if (token[tokenLen - 1] == ':') {
      strncpy(label.label[(uint8_t)label.top], token, tokenLen - 1);
      label.pos[(uint8_t)label.top] = numOfLines;
      label.top++;
      goto again;
    }

    if (token[0] == '$') {
      if (!simParseExpression(token, lineNum)) {
        simError("script:%s line:%d %s", fileName, lineNum, parseErr);
        return NULL;
      }
      continue;
    }

    if ((pCmd = simCheckCmd(token, tokenLen)) == NULL) {
      token[tokenLen] = 0;
      simError("script:%s line:%d invalid cmd:%s", fileName, lineNum, token);
      return NULL;
    }

    if (!pCmd->parseCmd(rest, pCmd, lineNum)) {
      simError("script:%s line:%d %s", fileName, lineNum, parseErr);
      return NULL;
    }
  }

  fclose(fd);

  script = simBuildScriptObj(fileName);
  if (script == NULL) simError("script:%s %s", fileName, parseErr);

  return script;
}

H
Haojun Liao 已提交
254 255 256
int32_t simCheckExpression(char *exp) {
  char *  op1, *op2, *op, *rest;
  int32_t op1Len, op2Len, opLen;
S
slguan 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

  rest = paGetToken(exp, &op1, &op1Len);
  if (op1Len == 0) {
    sprintf(parseErr, "expression is required");
    return -1;
  }

  rest = paGetToken(rest, &op, &opLen);
  if (opLen == 0) {
    sprintf(parseErr, "operator is missed");
    return -1;
  }

  rest = paGetToken(rest, &op2, &op2Len);
  if (op2Len == 0) {
    sprintf(parseErr, "operand is missed");
    return -1;
  }

  if (opLen == 1) {
    if (op[0] != '=' && op[0] != '<' && op[0] != '>') {
      sprintf(parseErr, "invalid operator:%s", op);
      return -1;
    }

    if (op[0] == '=' && op1[0] != '$') {
      sprintf(parseErr, "left side of assignment must be variable");
      return -1;
    }
  } else if (opLen == 2) {
H
Haojun Liao 已提交
287
    if (op[1] != '=' || (op[0] != '=' && op[0] != '<' && op[0] != '>' && op[0] != '!')) {
S
slguan 已提交
288 289 290 291 292 293 294 295 296 297
      sprintf(parseErr, "left side of assignment must be variable");
      return -1;
    }
  } else {
    sprintf(parseErr, "invalid operator:%s", op);
    return -1;
  }

  rest = paGetToken(rest, &op, &opLen);

H
Haojun Liao 已提交
298
  if (opLen == 0) return (int32_t)(rest - exp);
S
slguan 已提交
299 300

  /* if it is key word "then" */
H
Haojun Liao 已提交
301
  if (strncmp(op, "then", 4) == 0) return (int32_t)(op - exp);
S
slguan 已提交
302 303 304 305 306 307 308 309 310 311 312 313

  rest = paGetToken(rest, &op2, &op2Len);
  if (op2Len == 0) {
    sprintf(parseErr, "operand is missed");
    return -1;
  }

  if (opLen > 1) {
    sprintf(parseErr, "invalid operator:%s", op);
    return -1;
  }

H
Haojun Liao 已提交
314 315
  if (op[0] == '+' || op[0] == '-' || op[0] == '*' || op[0] == '/' || op[0] == '.') {
    return (int32_t)(rest - exp);
S
slguan 已提交
316 317 318 319 320
  }

  return -1;
}

H
Haojun Liao 已提交
321 322
bool simParseExpression(char *token, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

  expLen = simCheckExpression(token);
  if (expLen <= 0) return -1;

  cmdLine[numOfLines].cmdno = SIM_CMD_EXP;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
  memcpy(optionBuffer + optionOffset, token, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
338 339 340
bool simParseIfCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  ret;
  int32_t expLen;
S
slguan 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

  expLen = simCheckExpression(rest);

  if (expLen <= 0) return -1;

  ret = rest + expLen;

  if (strncmp(ret, "then", 4) == 0) {
    block.type[(uint8_t)block.top] = BLOCK_IF;
    block.pos[(uint8_t)block.top] = &cmdLine[numOfLines].jump;
    block.top++;
  } else {
    cmdLine[numOfLines].jump = numOfLines + 2;
  }

  cmdLine[numOfLines].optionOffset = optionOffset;
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;
  cmdLine[numOfLines].cmdno = SIM_CMD_TEST;
  cmdLine[numOfLines].lineNum = lineNum;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
367 368
bool simParseElifCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

  expLen = simCheckExpression(rest);

  if (expLen <= 0) return -1;

  if (block.top < 1) {
    sprintf(parseErr, "no matching if");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_IF) {
    sprintf(parseErr, "no matched if block");
    return false;
  }

  cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
H
Haojun Liao 已提交
385
  block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = &(cmdLine[numOfLines].jump);
S
slguan 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  block.numJump[block.top - 1]++;

  numOfLines++;

  *(block.pos[block.top - 1]) = numOfLines;
  block.pos[block.top - 1] = &cmdLine[numOfLines].jump;

  cmdLine[numOfLines].optionOffset = optionOffset;
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;
  cmdLine[numOfLines].cmdno = SIM_CMD_TEST;
  cmdLine[numOfLines].lineNum = lineNum;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
404
bool simParseElseCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
405 406 407 408 409 410 411 412 413 414 415
  if (block.top < 1) {
    sprintf(parseErr, "no matching if");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_IF) {
    sprintf(parseErr, "no matched if block");
    return false;
  }

  cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
H
Haojun Liao 已提交
416
  block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = &(cmdLine[numOfLines].jump);
S
slguan 已提交
417 418 419 420 421 422 423 424 425 426
  block.numJump[block.top - 1]++;

  numOfLines++;

  *(block.pos[block.top - 1]) = numOfLines;
  block.pos[block.top - 1] = NULL;

  return true;
}

H
Haojun Liao 已提交
427 428
bool simParseEndiCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t i;
S
slguan 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441

  if (block.top < 1) {
    sprintf(parseErr, "no matching if");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_IF) {
    sprintf(parseErr, "no matched if block");
    return false;
  }

  if (block.pos[block.top - 1]) *(block.pos[block.top - 1]) = numOfLines;

H
Haojun Liao 已提交
442
  for (i = 0; i < block.numJump[block.top - 1]; ++i) {
S
slguan 已提交
443
    *(block.jump[block.top - 1][i]) = numOfLines;
H
Haojun Liao 已提交
444
  }
S
slguan 已提交
445 446 447 448 449 450 451

  block.numJump[block.top - 1] = 0;
  block.top--;

  return true;
}

H
Haojun Liao 已提交
452 453
bool simParseWhileCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

  expLen = simCheckExpression(rest);

  if (expLen <= 0) return false;

  block.type[(uint8_t)block.top] = BLOCK_WHILE;
  block.pos[(uint8_t)block.top] = &(cmdLine[numOfLines].jump);
  block.back[(uint8_t)block.top] = numOfLines;
  block.top++;

  cmdLine[numOfLines].optionOffset = optionOffset;
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;
  cmdLine[numOfLines].cmdno = SIM_CMD_TEST;
  cmdLine[numOfLines].lineNum = lineNum;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
475 476
bool simParseEndwCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t i;
S
slguan 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

  if (block.top < 1) {
    sprintf(parseErr, "no matching while");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_WHILE) {
    sprintf(parseErr, "no matched while block");
    return false;
  }

  cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
  cmdLine[numOfLines].jump = block.back[block.top - 1];
  cmdLine[numOfLines].lineNum = lineNum;
  numOfLines++;

  *(block.pos[block.top - 1]) = numOfLines;

H
Haojun Liao 已提交
495
  for (i = 0; i < block.numJump[block.top - 1]; ++i) {
S
slguan 已提交
496
    *(block.jump[block.top - 1][i]) = numOfLines;
H
Haojun Liao 已提交
497
  }
S
slguan 已提交
498 499 500 501 502 503

  block.top--;

  return true;
}

H
Haojun Liao 已提交
504 505 506
bool simParseSwitchCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  token;
  int32_t tokenLen;
S
slguan 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

  rest = paGetToken(rest, &token, &tokenLen);
  if (tokenLen == 0) {
    sprintf(parseErr, "switch should be followed by variable");
    return false;
  }

  if (token[0] != '$') {
    sprintf(parseErr, "switch must be followed by variable");
    return false;
  }

  memcpy(block.sexp[(uint8_t)block.top], token, tokenLen);
  block.sexpLen[(uint8_t)block.top] = tokenLen;
  block.type[(uint8_t)block.top] = BLOCK_SWITCH;
  block.top++;

  return true;
}

H
Haojun Liao 已提交
527 528 529
bool simParseCaseCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  token;
  int32_t tokenLen;
S
slguan 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

  rest = paGetToken(rest, &token, &tokenLen);
  if (tokenLen == 0) {
    sprintf(parseErr, "case should be followed by value");
    return false;
  }

  if (block.top < 1) {
    sprintf(parseErr, "no matching switch");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_SWITCH) {
    sprintf(parseErr, "case not matched");
    return false;
  }

H
Haojun Liao 已提交
547
  if (block.pos[block.top - 1] != NULL) {
S
slguan 已提交
548
    *(block.pos[block.top - 1]) = numOfLines;
H
Haojun Liao 已提交
549
  }
S
slguan 已提交
550 551 552 553 554 555

  block.pos[block.top - 1] = &(cmdLine[numOfLines].jump);

  cmdLine[numOfLines].cmdno = SIM_CMD_TEST;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
H
Haojun Liao 已提交
556
  memcpy(optionBuffer + optionOffset, block.sexp[block.top - 1], block.sexpLen[block.top - 1]);
S
slguan 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569
  optionOffset += block.sexpLen[block.top - 1];
  *(optionBuffer + optionOffset++) = ' ';
  *(optionBuffer + optionOffset++) = '=';
  *(optionBuffer + optionOffset++) = '=';
  *(optionBuffer + optionOffset++) = ' ';
  memcpy(optionBuffer + optionOffset, token, tokenLen);
  optionOffset += tokenLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
570
bool simParseBreakCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
571 572 573 574 575
  if (block.top < 1) {
    sprintf(parseErr, "no blcok exists");
    return false;
  }

H
Haojun Liao 已提交
576
  if (block.type[block.top - 1] != BLOCK_SWITCH && block.type[block.top - 1] != BLOCK_WHILE) {
S
slguan 已提交
577 578 579 580
    sprintf(parseErr, "not in switch or while block");
    return false;
  }

H
Haojun Liao 已提交
581
  block.jump[block.top - 1][(uint8_t)block.numJump[block.top - 1]] = &(cmdLine[numOfLines].jump);
S
slguan 已提交
582 583 584 585 586 587 588 589 590
  block.numJump[block.top - 1]++;

  cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
  cmdLine[numOfLines].lineNum = lineNum;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
591
bool simParseDefaultCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
592 593 594 595 596 597 598 599 600 601
  if (block.top < 1) {
    sprintf(parseErr, "no matching switch");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_SWITCH) {
    sprintf(parseErr, "default should be matched with switch");
    return false;
  }

H
Haojun Liao 已提交
602
  if (block.pos[block.top - 1] != NULL) {
S
slguan 已提交
603
    *(block.pos[block.top - 1]) = numOfLines;
H
Haojun Liao 已提交
604
  }
S
slguan 已提交
605 606 607 608

  return true;
}

H
Haojun Liao 已提交
609 610
bool simParseEndsCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t i;
S
slguan 已提交
611 612 613 614 615 616 617 618 619 620 621

  if (block.top < 1) {
    sprintf(parseErr, "no matching switch");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_SWITCH) {
    sprintf(parseErr, "ends should be matched with switch");
    return false;
  }

H
Haojun Liao 已提交
622
  for (i = 0; i < block.numJump[block.top - 1]; ++i) {
S
slguan 已提交
623
    *(block.jump[block.top - 1][i]) = numOfLines;
H
Haojun Liao 已提交
624
  }
S
slguan 已提交
625 626 627 628 629 630 631

  block.numJump[block.top - 1] = 0;
  block.top--;

  return true;
}

H
Haojun Liao 已提交
632
bool simParseContinueCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
  if (block.top < 1) {
    sprintf(parseErr, "no matching while");
    return false;
  }

  if (block.type[block.top - 1] != BLOCK_WHILE) {
    sprintf(parseErr, "continue should be matched with while cmd");
    return false;
  }

  cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].jump = block.back[block.top - 1];

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
651 652
bool simParsePrintCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
653 654 655 656 657

  rest++;
  cmdLine[numOfLines].cmdno = SIM_CMD_PRINT;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
H
Haojun Liao 已提交
658
  expLen = (int32_t)strlen(rest);
S
slguan 已提交
659 660 661 662 663 664 665 666 667
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

void simCheckSqlOption(char *rest) {
H
Haojun Liao 已提交
668 669
  int32_t valueLen;
  char *  value, *xpos;
S
slguan 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

  xpos = strstr(rest, " -x");  // need a blank
  if (xpos) {
    paGetToken(xpos + 3, &value, &valueLen);
    if (valueLen != 0) {
      memcpy(dest.label[(uint8_t)dest.top], value, valueLen);
      dest.label[(uint8_t)dest.top][valueLen] = 0;
      dest.pos[(uint8_t)dest.top] = numOfLines;
      dest.top++;

      *xpos = 0;
    }
  }
}

H
Haojun Liao 已提交
685 686
bool simParseSqlCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
687 688 689 690 691 692

  rest++;
  simCheckSqlOption(rest);
  cmdLine[numOfLines].cmdno = SIM_CMD_SQL;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
H
Haojun Liao 已提交
693
  expLen = (int32_t)strlen(rest);
S
slguan 已提交
694 695 696 697 698 699 700 701
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
702 703
bool simParseSqlErrorCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
704 705 706 707 708

  rest++;
  cmdLine[numOfLines].cmdno = SIM_CMD_SQL_ERROR;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
H
Haojun Liao 已提交
709
  expLen = (int32_t)strlen(rest);
S
slguan 已提交
710 711 712 713 714 715 716 717
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
718
bool simParseSqlSlowCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
719 720 721 722 723
  simParseSqlCmd(rest, pCmd, lineNum);
  cmdLine[numOfLines - 1].cmdno = SIM_CMD_SQL_SLOW;
  return true;
}

H
Haojun Liao 已提交
724
bool simParseRestfulCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
TD-1225  
Shengliang Guan 已提交
725 726 727 728 729
  simParseSqlCmd(rest, pCmd, lineNum);
  cmdLine[numOfLines - 1].cmdno = SIM_CMD_RESTFUL;
  return true;
}

H
Haojun Liao 已提交
730 731
bool simParseSystemCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  int32_t expLen;
S
slguan 已提交
732 733 734 735 736

  rest++;
  cmdLine[numOfLines].cmdno = SIM_CMD_SYSTEM;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
H
Haojun Liao 已提交
737
  expLen = (int32_t)strlen(rest);
S
slguan 已提交
738 739 740 741 742 743 744 745
  memcpy(optionBuffer + optionOffset, rest, expLen);
  optionOffset += expLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
746
bool simParseSystemContentCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
747 748 749 750 751
  simParseSystemCmd(rest, pCmd, lineNum);
  cmdLine[numOfLines - 1].cmdno = SIM_CMD_SYSTEM_CONTENT;
  return true;
}

H
Haojun Liao 已提交
752 753 754
bool simParseSleepCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  token;
  int32_t tokenLen;
S
slguan 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

  cmdLine[numOfLines].cmdno = SIM_CMD_SLEEP;
  cmdLine[numOfLines].lineNum = lineNum;

  paGetToken(rest, &token, &tokenLen);
  if (tokenLen > 0) {
    cmdLine[numOfLines].optionOffset = optionOffset;
    memcpy(optionBuffer + optionOffset, token, tokenLen);
    optionOffset += tokenLen + 1;
    *(optionBuffer + optionOffset - 1) = 0;
  }

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
771 772 773
bool simParseReturnCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  token;
  int32_t tokenLen;
S
slguan 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

  cmdLine[numOfLines].cmdno = SIM_CMD_RETURN;
  cmdLine[numOfLines].lineNum = lineNum;

  paGetToken(rest, &token, &tokenLen);
  if (tokenLen > 0) {
    cmdLine[numOfLines].optionOffset = optionOffset;
    memcpy(optionBuffer + optionOffset, token, tokenLen);
    optionOffset += tokenLen + 1;
    *(optionBuffer + optionOffset - 1) = 0;
  }

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
790 791 792
bool simParseGotoCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  token;
  int32_t tokenLen;
S
slguan 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812

  rest = paGetToken(rest, &token, &tokenLen);

  if (tokenLen == 0) {
    sprintf(parseErr, "label should be followed by goto cmd");
    return false;
  }

  memcpy(dest.label[(uint8_t)dest.top], token, tokenLen);
  dest.label[(uint8_t)dest.top][tokenLen] = 0;
  dest.pos[(uint8_t)dest.top] = numOfLines;
  dest.top++;

  cmdLine[numOfLines].cmdno = SIM_CMD_GOTO;
  cmdLine[numOfLines].lineNum = lineNum;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
813 814 815
bool simParseRunCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
  char *  token;
  int32_t tokenLen;
S
slguan 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

  rest = paGetToken(rest, &token, &tokenLen);

  if (tokenLen == 0) {
    sprintf(parseErr, "file name should be followed by run cmd");
    return false;
  }

  cmdLine[numOfLines].cmdno = SIM_CMD_RUN;
  cmdLine[numOfLines].lineNum = lineNum;
  cmdLine[numOfLines].optionOffset = optionOffset;
  memcpy(optionBuffer + optionOffset, token, tokenLen);
  optionOffset += tokenLen + 1;
  *(optionBuffer + optionOffset - 1) = 0;

  numOfLines++;
  return true;
}

H
Haojun Liao 已提交
835
bool simParseRunBackCmd(char *rest, SCommand *pCmd, int32_t lineNum) {
S
slguan 已提交
836 837 838 839 840 841
  simParseRunCmd(rest, pCmd, lineNum);
  cmdLine[numOfLines - 1].cmdno = SIM_CMD_RUN_BACK;
  return true;
}

void simInitsimCmdList() {
H
Haojun Liao 已提交
842
  int32_t cmdno;
S
slguan 已提交
843 844 845 846 847 848
  memset(simCmdList, 0, SIM_CMD_END * sizeof(SCommand));

  /* internal command */
  cmdno = SIM_CMD_EXP;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "exp");
849
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
850 851 852 853 854 855
  simCmdList[cmdno].parseCmd = NULL;
  simCmdList[cmdno].executeCmd = simExecuteExpCmd;

  cmdno = SIM_CMD_IF;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "if");
856
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
857 858 859 860 861 862 863
  simCmdList[cmdno].parseCmd = simParseIfCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_ELIF;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "elif");
864
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
865 866 867 868 869 870 871
  simCmdList[cmdno].parseCmd = simParseElifCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_ELSE;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "else");
872
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
873 874 875 876 877 878 879
  simCmdList[cmdno].parseCmd = simParseElseCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_ENDI;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "endi");
880
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
881 882 883 884 885 886 887
  simCmdList[cmdno].parseCmd = simParseEndiCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_WHILE;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "while");
888
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
889 890 891 892 893 894 895
  simCmdList[cmdno].parseCmd = simParseWhileCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_ENDW;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "endw");
896
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
897 898 899 900 901 902 903
  simCmdList[cmdno].parseCmd = simParseEndwCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SWITCH;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "switch");
904
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
905 906 907 908 909 910 911
  simCmdList[cmdno].parseCmd = simParseSwitchCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_CASE;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "case");
912
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
913 914 915 916 917 918 919
  simCmdList[cmdno].parseCmd = simParseCaseCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_DEFAULT;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "default");
920
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
921 922 923 924 925 926 927
  simCmdList[cmdno].parseCmd = simParseDefaultCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_BREAK;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "break");
928
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
929 930 931 932 933 934 935
  simCmdList[cmdno].parseCmd = simParseBreakCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_CONTINUE;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "continue");
936
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
937 938 939 940 941 942 943
  simCmdList[cmdno].parseCmd = simParseContinueCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_ENDS;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "ends");
944
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
945 946 947 948 949 950 951
  simCmdList[cmdno].parseCmd = simParseEndsCmd;
  simCmdList[cmdno].executeCmd = NULL;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SLEEP;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "sleep");
952
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
953 954 955 956 957 958 959
  simCmdList[cmdno].parseCmd = simParseSleepCmd;
  simCmdList[cmdno].executeCmd = simExecuteSleepCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_GOTO;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "goto");
960
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
961 962 963 964 965 966 967
  simCmdList[cmdno].parseCmd = simParseGotoCmd;
  simCmdList[cmdno].executeCmd = simExecuteGotoCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_RUN;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "run");
968
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
969 970 971 972 973 974 975
  simCmdList[cmdno].parseCmd = simParseRunCmd;
  simCmdList[cmdno].executeCmd = simExecuteRunCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_RUN_BACK;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "run_back");
976
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
977 978 979 980 981 982 983
  simCmdList[cmdno].parseCmd = simParseRunBackCmd;
  simCmdList[cmdno].executeCmd = simExecuteRunBackCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SYSTEM;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "system");
984
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
985 986 987 988 989 990 991
  simCmdList[cmdno].parseCmd = simParseSystemCmd;
  simCmdList[cmdno].executeCmd = simExecuteSystemCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SYSTEM_CONTENT;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "system_content");
992
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
993 994 995 996 997 998 999
  simCmdList[cmdno].parseCmd = simParseSystemContentCmd;
  simCmdList[cmdno].executeCmd = simExecuteSystemContentCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_PRINT;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "print");
1000
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
1001 1002 1003 1004 1005 1006 1007
  simCmdList[cmdno].parseCmd = simParsePrintCmd;
  simCmdList[cmdno].executeCmd = simExecutePrintCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SQL;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "sql");
1008
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
1009 1010 1011 1012 1013 1014 1015
  simCmdList[cmdno].parseCmd = simParseSqlCmd;
  simCmdList[cmdno].executeCmd = simExecuteSqlCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SQL_ERROR;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "sql_error");
1016
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
1017 1018 1019 1020 1021 1022 1023
  simCmdList[cmdno].parseCmd = simParseSqlErrorCmd;
  simCmdList[cmdno].executeCmd = simExecuteSqlErrorCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

  cmdno = SIM_CMD_SQL_SLOW;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "sql_slow");
1024
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
1025 1026 1027 1028
  simCmdList[cmdno].parseCmd = simParseSqlSlowCmd;
  simCmdList[cmdno].executeCmd = simExecuteSqlSlowCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

S
TD-1225  
Shengliang Guan 已提交
1029 1030 1031 1032 1033 1034 1035 1036
  cmdno = SIM_CMD_RESTFUL;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "restful");
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
  simCmdList[cmdno].parseCmd = simParseRestfulCmd;
  simCmdList[cmdno].executeCmd = simExecuteRestfulCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));

S
slguan 已提交
1037 1038 1039 1040
  /* test is only an internal command */
  cmdno = SIM_CMD_TEST;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "test");
1041
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
1042 1043 1044 1045 1046 1047
  simCmdList[cmdno].parseCmd = NULL;
  simCmdList[cmdno].executeCmd = simExecuteTestCmd;

  cmdno = SIM_CMD_RETURN;
  simCmdList[cmdno].cmdno = cmdno;
  strcpy(simCmdList[cmdno].name, "return");
1048
  simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name);
S
slguan 已提交
1049 1050 1051 1052
  simCmdList[cmdno].parseCmd = simParseReturnCmd;
  simCmdList[cmdno].executeCmd = simExecuteReturnCmd;
  simAddCmdIntoHash(&(simCmdList[cmdno]));
}