Advertisement
goroh_kun

Untitled

Oct 30th, 2011
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
  2.     FrameworkCommandCollection::iterator i;
  3.     int argc = 0;
  4.     char *argv[FrameworkListener::CMD_ARGS_MAX]; //←これは16
  5.     char tmp[255];  //←ここで、tmpは255バイト分確保
  6.     char *p = data;
  7.     char *q = tmp;
  8.     bool esc = false;
  9.     bool quote = false;
  10.     int k;
  11.  
  12.     memset(argv, 0, sizeof(argv));
  13.     memset(tmp, 0, sizeof(tmp));
  14.     while(*p) { //←終了条件がpの中身が0でなければ、繰り返す。
  15.         if (*p == '\\') {
  16.             if (esc) {
  17.                 *q++ = '\\';
  18.                 esc = false;
  19.             } else
  20.                 esc = true;
  21.             p++;
  22.             continue;
  23.         } else if (esc) {
  24.             if (*p == '"')
  25.                 *q++ = '"';
  26.             else if (*p == '\\')
  27.                 *q++ = '\\';
  28.             else {
  29.                 cli->sendMsg(500, "Unsupported escape sequence", false);
  30.                 goto out;
  31.             }
  32.             p++;
  33.             esc = false;
  34.             continue;
  35.         }
  36.  
  37.         if (*p == '"') {
  38.             if (quote)
  39.                 quote = false;
  40.             else
  41.                 quote = true;
  42.             p++;
  43.             continue;
  44.         }
  45.  
  46.         *q = *p++;
  47.         if (!quote && *q == ' ') {
  48.             *q = '\0';
  49.             argv[argc++] = strdup(tmp); //←argcのチェックしてない
  50.             memset(tmp, 0, sizeof(tmp));
  51.             q = tmp;
  52.             continue;
  53.         }
  54.         q++;
  55.     }
  56.  
  57.     argv[argc++] = strdup(tmp);
  58. #if 0
  59.     for (k = 0; k < argc; k++) {
  60.         SLOGD("arg[%d] = '%s'", k, argv[k]);
  61.     }
  62. #endif
  63.  
  64.     if (quote) {
  65.         cli->sendMsg(500, "Unclosed quotes error", false);
  66.         goto out;
  67.     }
  68.    
  69.     for (i = mCommands->begin(); i != mCommands->end(); ++i) {
  70.         FrameworkCommand *c = *i;
  71.  
  72.         if (!strcmp(argv[0], c->getCommand())) {
  73.             if (c->runCommand(cli, argc, argv)) {
  74.                 SLOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
  75.             }
  76.             goto out;
  77.         }
  78.     }
  79.  
  80.     cli->sendMsg(500, "Command not recognized", false);
  81. out:
  82.     int j;
  83.     for (j = 0; j < argc; j++)
  84.         free(argv[j]);
  85.     return;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement