aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/AllCommands.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2015-08-13 12:48:31 +0200
committerMax Kellermann <max@duempel.org>2015-08-13 12:48:31 +0200
commit86e036c3933def22b6ff0eae5bcf9cd7103240e7 (patch)
treee8d812461e22b189cea3b3df908515cebbfe0cd1 /src/command/AllCommands.cxx
parent7652a2986b0d0ad55b2776685130f1c68d7108c7 (diff)
downloadmpd-86e036c3933def22b6ff0eae5bcf9cd7103240e7.tar.gz
mpd-86e036c3933def22b6ff0eae5bcf9cd7103240e7.tar.xz
mpd-86e036c3933def22b6ff0eae5bcf9cd7103240e7.zip
command: pass Response object to command callbacks
Diffstat (limited to 'src/command/AllCommands.cxx')
-rw-r--r--src/command/AllCommands.cxx31
1 files changed, 11 insertions, 20 deletions
diff --git a/src/command/AllCommands.cxx b/src/command/AllCommands.cxx
index 57d4db983..aa60736f3 100644
--- a/src/command/AllCommands.cxx
+++ b/src/command/AllCommands.cxx
@@ -64,15 +64,15 @@ struct command {
unsigned permission;
int min;
int max;
- CommandResult (*handler)(Client &client, Request args);
+ CommandResult (*handler)(Client &client, Request request, Response &response);
};
/* don't be fooled, this is the command handler for "commands" command */
static CommandResult
-handle_commands(Client &client, Request args);
+handle_commands(Client &client, Request request, Response &response);
static CommandResult
-handle_not_commands(Client &client, Request args);
+handle_not_commands(Client &client, Request request, Response &response);
/**
* The command registry.
@@ -257,17 +257,15 @@ PrintUnavailableCommands(Response &r, unsigned permission)
/* don't be fooled, this is the command handler for "commands" command */
static CommandResult
-handle_commands(Client &client, gcc_unused Request args)
+handle_commands(Client &client, gcc_unused Request request, Response &r)
{
- Response r(client);
return PrintAvailableCommands(r, client.partition,
client.GetPermission());
}
static CommandResult
-handle_not_commands(Client &client, gcc_unused Request args)
+handle_not_commands(Client &client, gcc_unused Request request, Response &r)
{
- Response r(client);
return PrintUnavailableCommands(r, client.GetPermission());
}
@@ -308,11 +306,10 @@ command_lookup(const char *name)
}
static bool
-command_check_request(const struct command *cmd, Client &client,
+command_check_request(const struct command *cmd, Response &r,
unsigned permission, Request args)
{
if (cmd->permission != (permission & cmd->permission)) {
- Response r(client);
r.FormatError(ACK_ERROR_PERMISSION,
"you don't have permission for \"%s\"",
cmd->cmd);
@@ -326,18 +323,15 @@ command_check_request(const struct command *cmd, Client &client,
return true;
if (min == max && unsigned(max) != args.size) {
- Response r(client);
r.FormatError(ACK_ERROR_ARG,
"wrong number of arguments for \"%s\"",
cmd->cmd);
return false;
} else if (args.size < unsigned(min)) {
- Response r(client);
r.FormatError(ACK_ERROR_ARG,
"too few arguments for \"%s\"", cmd->cmd);
return false;
} else if (max >= 0 && args.size > unsigned(max)) {
- Response r(client);
r.FormatError(ACK_ERROR_ARG,
"too many arguments for \"%s\"", cmd->cmd);
return false;
@@ -346,14 +340,13 @@ command_check_request(const struct command *cmd, Client &client,
}
static const struct command *
-command_checked_lookup(Client &client, unsigned permission,
+command_checked_lookup(Response &r, unsigned permission,
const char *cmd_name, Request args)
{
current_command = "";
const struct command *cmd = command_lookup(cmd_name);
if (cmd == nullptr) {
- Response r(client);
r.FormatError(ACK_ERROR_UNKNOWN,
"unknown command \"%s\"", cmd_name);
return nullptr;
@@ -361,7 +354,7 @@ command_checked_lookup(Client &client, unsigned permission,
current_command = cmd->cmd;
- if (!command_check_request(cmd, client, permission, args))
+ if (!command_check_request(cmd, r, permission, args))
return nullptr;
return cmd;
@@ -370,6 +363,7 @@ command_checked_lookup(Client &client, unsigned permission,
CommandResult
command_process(Client &client, unsigned num, char *line)
{
+ Response r(client);
Error error;
command_list_num = num;
@@ -385,7 +379,6 @@ command_process(Client &client, unsigned num, char *line)
if (cmd_name == nullptr) {
current_command = "";
- Response r(client);
if (tokenizer.IsEnd())
r.FormatError(ACK_ERROR_UNKNOWN, "No command given");
else
@@ -405,7 +398,6 @@ command_process(Client &client, unsigned num, char *line)
while (true) {
if (args.size == COMMAND_ARGV_MAX) {
- Response r(client);
r.Error(ACK_ERROR_ARG, "Too many arguments");
current_command = nullptr;
return CommandResult::ERROR;
@@ -416,7 +408,6 @@ command_process(Client &client, unsigned num, char *line)
if (tokenizer.IsEnd())
break;
- Response r(client);
r.Error(ACK_ERROR_UNKNOWN, error.GetMessage());
current_command = nullptr;
return CommandResult::ERROR;
@@ -428,11 +419,11 @@ command_process(Client &client, unsigned num, char *line)
/* look up and invoke the command handler */
const struct command *cmd =
- command_checked_lookup(client, client.GetPermission(),
+ command_checked_lookup(r, client.GetPermission(),
cmd_name, args);
CommandResult ret = cmd
- ? cmd->handler(client, args)
+ ? cmd->handler(client, args, r)
: CommandResult::ERROR;
current_command = nullptr;