Apply @fortrue's comments

This commit is contained in:
Yoon 2018-04-06 13:54:35 +09:00
parent 5d9cd99270
commit 133e303188
3 changed files with 13 additions and 18 deletions

View File

@ -191,6 +191,9 @@ void Command::init()
} }
void Command::addCustomCommand(const Command *p) { void Command::addCustomCommand(const Command *p) {
if (Sentinel >= AvailableCommands) {
Throw(InitFail, "too many custom commands(>%d)", MaxCustomCommands);
}
if (nullptr != find(p->name)) { if (nullptr != find(p->name)) {
Throw(InitFail, "custom command %s is duplicated", p->name); Throw(InitFail, "custom command %s is duplicated", p->name);
} }

View File

@ -191,7 +191,8 @@ public:
SubMsg, SubMsg,
MaxCommands, MaxCommands,
AvailableCommands = MaxCommands + 128, MaxCustomCommands = 16,
AvailableCommands = MaxCommands + MaxCustomCommands,
}; };
enum Mode enum Mode
{ {

View File

@ -37,7 +37,7 @@ void CustomCommandConf::init(CustomCommandConf&c, const char* name, const int ty
c.name = name; c.name = name;
c.cmd.type = (Command::Type)type; c.cmd.type = (Command::Type)type;
c.cmd.name = c.name.c_str(); c.cmd.name = c.name.c_str();
c.cmd.minArgs = 1; c.cmd.minArgs = 2;
c.cmd.maxArgs = 1; c.cmd.maxArgs = 1;
c.cmd.mode = Command::Write; c.cmd.mode = Command::Write;
} }
@ -376,9 +376,6 @@ void Conf::setCustomCommand(const ConfParser::Node* node)
Throw(InvalidValue, "%s:%d CustomCommand require scope value", node->file, node->line); Throw(InvalidValue, "%s:%d CustomCommand require scope value", node->file, node->line);
} }
for (auto p = node->sub; p; p = p->next) { for (auto p = node->sub; p; p = p->next) {
if (Command::Sentinel >= Command::AvailableCommands) {
Throw(InvalidValue, "%s:%d Too many custom commands", node->file, node->line);
}
mCustomCommands.push_back(CustomCommandConf{}); mCustomCommands.push_back(CustomCommandConf{});
auto& cc = mCustomCommands.back(); auto& cc = mCustomCommands.back();
CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel); CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel);
@ -388,9 +385,13 @@ void Conf::setCustomCommand(const ConfParser::Node* node)
node->file, node->line); node->file, node->line);
} }
for (;s ; s = s->next) { for (;s ; s = s->next) {
setInt(cc.cmd.minArgs, "minArgs", s, 1); if (setInt(cc.cmd.minArgs, "minArgs", s, 2)) {
setInt(cc.cmd.maxArgs, "maxArgs", s, 1, 9999); } else if (setInt(cc.cmd.maxArgs, "maxArgs", s, 2, 9999)) {
setCommandMode(cc.cmd.mode, "mode", s); } else if (setCommandMode(cc.cmd.mode, "mode", s)) {
} else {
Throw(UnknownKey, "%s:%d unknown key %s",
s->file, s->line, s->key.c_str());
}
} }
Command::addCustomCommand(&cc.cmd); Command::addCustomCommand(&cc.cmd);
} }
@ -413,16 +414,6 @@ bool Conf::setCommandMode(int& mode, const char* name, const ConfParser::Node* n
mode |= Command::Read; mode |= Command::Read;
} else if ((strcasecmp(mask.c_str(), "Admin") == 0)) { } else if ((strcasecmp(mask.c_str(), "Admin") == 0)) {
mode |= Command::Admin; mode |= Command::Admin;
} else if ((strcasecmp(mask.c_str(), "Private") == 0)) {
mode |= Command::Private;
} else if ((strcasecmp(mask.c_str(), "NoKey") == 0)) {
mode |= Command::NoKey;
} else if ((strcasecmp(mask.c_str(), "MultiKey") == 0)) {
mode |= Command::MultiKey;
} else if ((strcasecmp(mask.c_str(), "SMultiKey") == 0)) {
mode |= Command::SMultiKey;
} else if ((strcasecmp(mask.c_str(), "MultiKeyVal") == 0)) {
mode |= Command::MultiKeyVal;
} else if ((strcasecmp(mask.c_str(), "KeyAt2") == 0)) { } else if ((strcasecmp(mask.c_str(), "KeyAt2") == 0)) {
mode |= Command::KeyAt2; mode |= Command::KeyAt2;
} else if ((strcasecmp(mask.c_str(), "KeyAt3") == 0)) { } else if ((strcasecmp(mask.c_str(), "KeyAt3") == 0)) {