From 133e30318809f4d684cc92429f28775a46dd97aa Mon Sep 17 00:00:00 2001 From: Yoon Date: Fri, 6 Apr 2018 13:54:35 +0900 Subject: [PATCH] Apply @fortrue's comments --- src/Command.cpp | 3 +++ src/Command.h | 3 ++- src/Conf.cpp | 25 ++++++++----------------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/Command.cpp b/src/Command.cpp index 5821613..7f3b9f5 100644 --- a/src/Command.cpp +++ b/src/Command.cpp @@ -191,6 +191,9 @@ void Command::init() } void Command::addCustomCommand(const Command *p) { + if (Sentinel >= AvailableCommands) { + Throw(InitFail, "too many custom commands(>%d)", MaxCustomCommands); + } if (nullptr != find(p->name)) { Throw(InitFail, "custom command %s is duplicated", p->name); } diff --git a/src/Command.h b/src/Command.h index 5555621..474df61 100644 --- a/src/Command.h +++ b/src/Command.h @@ -191,7 +191,8 @@ public: SubMsg, MaxCommands, - AvailableCommands = MaxCommands + 128, + MaxCustomCommands = 16, + AvailableCommands = MaxCommands + MaxCustomCommands, }; enum Mode { diff --git a/src/Conf.cpp b/src/Conf.cpp index ff38ee1..c95d921 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -37,7 +37,7 @@ void CustomCommandConf::init(CustomCommandConf&c, const char* name, const int ty c.name = name; c.cmd.type = (Command::Type)type; c.cmd.name = c.name.c_str(); - c.cmd.minArgs = 1; + c.cmd.minArgs = 2; c.cmd.maxArgs = 1; 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); } 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{}); auto& cc = mCustomCommands.back(); CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel); @@ -388,9 +385,13 @@ void Conf::setCustomCommand(const ConfParser::Node* node) node->file, node->line); } for (;s ; s = s->next) { - setInt(cc.cmd.minArgs, "minArgs", s, 1); - setInt(cc.cmd.maxArgs, "maxArgs", s, 1, 9999); - setCommandMode(cc.cmd.mode, "mode", s); + if (setInt(cc.cmd.minArgs, "minArgs", s, 2)) { + } else if (setInt(cc.cmd.maxArgs, "maxArgs", s, 2, 9999)) { + } 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); } @@ -413,16 +414,6 @@ bool Conf::setCommandMode(int& mode, const char* name, const ConfParser::Node* n mode |= Command::Read; } else if ((strcasecmp(mask.c_str(), "Admin") == 0)) { 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)) { mode |= Command::KeyAt2; } else if ((strcasecmp(mask.c_str(), "KeyAt3") == 0)) {