1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Refactored command lookup

This commit is contained in:
James Booth 2012-08-15 00:42:38 +01:00
parent 254866e8b9
commit 4e18d65977

View File

@ -48,6 +48,7 @@ struct cmd_t {
// helpers // helpers
static struct cmd_t * _cmd_get_command(const char * const command);
static gboolean _handle_command(const char * const command, static gboolean _handle_command(const char * const command,
const char * const inp); const char * const inp);
static void _update_presence(const jabber_presence_t presence, static void _update_presence(const jabber_presence_t presence,
@ -414,32 +415,45 @@ cmd_get_help_list_status(void)
return result; return result;
} }
static gboolean static struct cmd_t *
_handle_command(const char * const command, const char * const inp) _cmd_get_command(const char * const command)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < ARRAY_SIZE(main_commands); i++) { for (i = 0; i < ARRAY_SIZE(main_commands); i++) {
struct cmd_t *pcmd = main_commands+i; struct cmd_t *pcmd = main_commands+i;
if (strcmp(pcmd->cmd, command) == 0) { if (strcmp(pcmd->cmd, command) == 0) {
return (pcmd->func(inp, pcmd->help)); return pcmd;
} }
} }
for (i = 0; i < ARRAY_SIZE(setting_commands); i++) { for (i = 0; i < ARRAY_SIZE(setting_commands); i++) {
struct cmd_t *pcmd = setting_commands+i; struct cmd_t *pcmd = setting_commands+i;
if (strcmp(pcmd->cmd, command) == 0) { if (strcmp(pcmd->cmd, command) == 0) {
return (pcmd->func(inp, pcmd->help)); return pcmd;
} }
} }
for (i = 0; i < ARRAY_SIZE(status_commands); i++) { for (i = 0; i < ARRAY_SIZE(status_commands); i++) {
struct cmd_t *pcmd = status_commands+i; struct cmd_t *pcmd = status_commands+i;
if (strcmp(pcmd->cmd, command) == 0) { if (strcmp(pcmd->cmd, command) == 0) {
return (pcmd->func(inp, pcmd->help)); return pcmd;
} }
} }
return _cmd_default(inp); return NULL;
}
static gboolean
_handle_command(const char * const command, const char * const inp)
{
struct cmd_t *cmd = _cmd_get_command(command);
if (cmd != NULL) {
return (cmd->func(inp, cmd->help));
} else {
return _cmd_default(inp);
}
} }
static gboolean static gboolean
@ -492,38 +506,21 @@ _cmd_help(const char * const inp, struct cmd_help_t help)
if (strcmp(inp, "/help") == 0) { if (strcmp(inp, "/help") == 0) {
cons_help(); cons_help();
} else { } else {
char *cmd; char *cmd = strndup(inp+6, strlen(inp)-6);
cmd = strndup(inp+6, strlen(inp)-6);
char cmd_with_slash[1 + strlen(cmd) + 1]; char cmd_with_slash[1 + strlen(cmd) + 1];
sprintf(cmd_with_slash, "/%s", cmd); sprintf(cmd_with_slash, "/%s", cmd);
const gchar **help_text = NULL; const gchar **help_text = NULL;
struct cmd_t *command = _cmd_get_command(cmd_with_slash);
unsigned int i; if (command != NULL) {
for (i = 0; i < ARRAY_SIZE(main_commands); i++) { help_text = command->help.long_help;
struct cmd_t *pcmd = main_commands+i;
if (strcmp(pcmd->cmd, cmd_with_slash) == 0) {
help_text = pcmd->help.long_help;
}
} }
for (i = 0; i < ARRAY_SIZE(setting_commands); i++) {
struct cmd_t *pcmd = setting_commands+i;
if (strcmp(pcmd->cmd, cmd_with_slash) == 0) {
help_text = pcmd->help.long_help;
}
}
for (i = 0; i < ARRAY_SIZE(status_commands); i++) {
struct cmd_t *pcmd = status_commands+i;
if (strcmp(pcmd->cmd, cmd_with_slash) == 0) {
help_text = pcmd->help.long_help;
}
}
cons_show(""); cons_show("");
if (help_text != NULL) { if (help_text != NULL) {
int i;
for (i = 0; help_text[i] != NULL; i++) { for (i = 0; help_text[i] != NULL; i++) {
cons_show(help_text[i]); cons_show(help_text[i]);
} }