
Untitled
By:
goroh_kun on
Oct 30th, 2011 | syntax:
C++ | size: 2.25 KB | hits: 492 | expires: Never
void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
FrameworkCommandCollection::iterator i;
int argc = 0;
char *argv[FrameworkListener::CMD_ARGS_MAX]; //←これは16
char tmp[255]; //←ここで、tmpは255バイト分確保
char *p = data;
char *q = tmp;
bool esc = false;
bool quote = false;
int k;
memset(argv, 0, sizeof(argv));
memset(tmp, 0, sizeof(tmp));
while(*p) { //←終了条件がpの中身が0でなければ、繰り返す。
if (*p == '\\') {
if (esc) {
*q++ = '\\';
esc = false;
} else
esc = true;
p++;
continue;
} else if (esc) {
if (*p == '"')
*q++ = '"';
else if (*p == '\\')
*q++ = '\\';
else {
cli->sendMsg(500, "Unsupported escape sequence", false);
goto out;
}
p++;
esc = false;
continue;
}
if (*p == '"') {
if (quote)
quote = false;
else
quote = true;
p++;
continue;
}
*q = *p++;
if (!quote && *q == ' ') {
*q = '\0';
argv[argc++] = strdup(tmp); //←argcのチェックしてない
memset(tmp, 0, sizeof(tmp));
q = tmp;
continue;
}
q++;
}
argv[argc++] = strdup(tmp);
#if 0
for (k = 0; k < argc; k++) {
SLOGD("arg[%d] = '%s'", k, argv[k]);
}
#endif
if (quote) {
cli->sendMsg(500, "Unclosed quotes error", false);
goto out;
}
for (i = mCommands->begin(); i != mCommands->end(); ++i) {
FrameworkCommand *c = *i;
if (!strcmp(argv[0], c->getCommand())) {
if (c->runCommand(cli, argc, argv)) {
SLOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
}
goto out;
}
}
cli->sendMsg(500, "Command not recognized", false);
out:
int j;
for (j = 0; j < argc; j++)
free(argv[j]);
return;
}