diff --git a/common/console.c b/common/console.c index 50ca6da2d4..cdb5daf5e6 100644 --- a/common/console.c +++ b/common/console.c @@ -128,14 +128,18 @@ static int split_words(char *input, int max_argc, int *argc, char **argv) * no match found. */ static const struct console_command *find_command(char *name) { - const struct console_command *cmd; + const struct console_command *cmd, *match = NULL; + int match_length = strlen(name); for (cmd = __cmds; cmd < __cmds_end; cmd++) { - if (!strcasecmp(name, cmd->name)) - return cmd; + if (!strncasecmp(name, cmd->name, match_length)) { + if (match) + return NULL; + match = cmd; + } } - return NULL; + return match; } @@ -159,7 +163,7 @@ static int handle_command(char *input) if (cmd) return cmd->handler(argc, argv); - ccprintf("Command '%s' not found.\n", argv[0]); + ccprintf("Command '%s' either not found or ambiguous.\n", argv[0]); return EC_ERROR_UNKNOWN; } @@ -293,7 +297,7 @@ static int command_ch(int argc, char **argv) } /* Otherwise, print help */ - ccputs("Usage: ch [newmask]\n"); + ccputs("Usage: chan [newmask]\n"); return EC_ERROR_INVAL; }; -DECLARE_CONSOLE_COMMAND(ch, command_ch); +DECLARE_CONSOLE_COMMAND(chan, command_ch); diff --git a/common/thermal.c b/common/thermal.c index de98f47e00..154ddbbfb0 100644 --- a/common/thermal.c +++ b/common/thermal.c @@ -238,7 +238,7 @@ static int command_thermal_config(int argc, char **argv) int sensor_type, threshold_id, value; if (argc != 2 && argc != 4) { - ccputs("Usage: thermal " + ccputs("Usage: thermalconf " "[ ]\n"); return EC_ERROR_UNKNOWN; } @@ -273,7 +273,7 @@ static int command_thermal_config(int argc, char **argv) return EC_SUCCESS; } -DECLARE_CONSOLE_COMMAND(thermal, command_thermal_config); +DECLARE_CONSOLE_COMMAND(thermalconf, command_thermal_config); static int command_fan_config(int argc, char **argv) diff --git a/common/util.c b/common/util.c index 47e9ba1226..b8afdf45c9 100644 --- a/common/util.c +++ b/common/util.c @@ -54,6 +54,22 @@ int strcasecmp(const char *s1, const char *s2) } +int strncasecmp(const char *s1, const char *s2, int size) +{ + int diff; + + if (!size) + return 0; + + do { + diff = tolower(*s1) - tolower(*s2); + if (diff) + return diff; + } while (*(s1++) && *(s2++) && --size); + return 0; +} + + int atoi(const char *nptr) { int result = 0; diff --git a/include/console.h b/include/console.h index 78fc6d8df4..c2785cc545 100644 --- a/include/console.h +++ b/include/console.h @@ -76,7 +76,10 @@ void cflush(void); void console_has_input(void); -/* Register a console command handler */ +/* + * Register a console command handler. Note that `name' must never be a + * beginning of another existing command name. + */ #define DECLARE_CONSOLE_COMMAND(name, routine) \ static const char __con_cmd_label_##name[] = #name; \ const struct console_command __con_cmd_##name \ diff --git a/include/util.h b/include/util.h index 3fb0657061..6bbb8dd7df 100644 --- a/include/util.h +++ b/include/util.h @@ -66,6 +66,7 @@ void *memcpy(void *dest, const void *src, int len); void *memset(void *dest, int c, int len); void *memmove(void *dest, const void *src, int len); int strcasecmp(const char *s1, const char *s2); +int strncasecmp(const char *s1, const char *s2, int size); int strlen(const char *s); int strtoi(const char *nptr, char **endptr, int base);