mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-12-28 02:35:28 +00:00
Clean up console module
Code cleanup and reformatting. No functional changes. BUG=chrome-os-partner:15579 BRANCH=none TEST=compile code; type a debug command like 'help list'. Change-Id: I641215ba3333628f658eec86d9d99718c43b111f Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/36387
This commit is contained in:
@@ -17,11 +17,18 @@
|
||||
|
||||
static char input_buf[80]; /* Current console command line */
|
||||
|
||||
/* Splits a line of input into words. Stores the count of words in
|
||||
* <argc>. Stores pointers to the words in <argv>, which must be at
|
||||
* least <max_argc> long. If more than <max_argc> words are found,
|
||||
* discards the excess and returns EC_ERROR_OVERFLOW. */
|
||||
static int split_words(char *input, int max_argc, int *argc, char **argv)
|
||||
/**
|
||||
* Split a line of input into words.
|
||||
*
|
||||
* @param input Input line; modified to add nulls after each word.
|
||||
* @param argc Destination for number of words.
|
||||
* @param argv Destination array for pointers to words; must be at
|
||||
* least MAX_ARGS_PER_COMMAND entries long.
|
||||
*
|
||||
* @return EC_SUCCESS. If more than MAX_ARGS_PER_COMMAND words are found,
|
||||
* discards the excess and returns EC_ERROR_OVERFLOW.
|
||||
*/
|
||||
static int split_words(char *input, int *argc, char **argv)
|
||||
{
|
||||
char *c;
|
||||
int in_word = 0;
|
||||
@@ -37,26 +44,31 @@ static int split_words(char *input, int max_argc, int *argc, char **argv)
|
||||
in_word = 0;
|
||||
}
|
||||
} else if (*c == '#') {
|
||||
/* After the hash sign is comment, ignored.
|
||||
* TODO: Need more logic to suuport escaping. */
|
||||
/* Comments start with hash and go to end of line */
|
||||
break;
|
||||
} else {
|
||||
if (!in_word) {
|
||||
/* Starting a new word */
|
||||
if (*argc >= max_argc)
|
||||
return EC_ERROR_OVERFLOW;
|
||||
} else if (!in_word) {
|
||||
/* Starting a new word */
|
||||
if (*argc >= MAX_ARGS_PER_COMMAND)
|
||||
return EC_ERROR_OVERFLOW;
|
||||
|
||||
argv[*argc] = c;
|
||||
in_word = 1;
|
||||
}
|
||||
argv[*argc] = c;
|
||||
in_word = 1;
|
||||
}
|
||||
}
|
||||
return EC_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* Find a command by name. Returns the command structure, or NULL if no match
|
||||
* found. */
|
||||
/**
|
||||
* Find a command by name.
|
||||
*
|
||||
* Allows partial matches, as long as the partial match is unique to one
|
||||
* command. So "foo" will match "foobar" as long as there isn't also a
|
||||
* command "food".
|
||||
*
|
||||
* @param name Command name to find.
|
||||
*
|
||||
* @return A pointer to the command structure, or NULL if no match found.
|
||||
*/
|
||||
static const struct console_command *find_command(char *name)
|
||||
{
|
||||
const struct console_command *cmd, *match = NULL;
|
||||
@@ -73,9 +85,13 @@ static const struct console_command *find_command(char *name)
|
||||
return match;
|
||||
}
|
||||
|
||||
|
||||
/* Handle a line of input containing a single command. Modifies the input
|
||||
* string during parsing. */
|
||||
/**
|
||||
* Handle a line of input containing a single command.
|
||||
*
|
||||
* @param input Input buffer; modified during parsing.
|
||||
*
|
||||
* @return EC_SUCCESS, or non-zero if error.
|
||||
*/
|
||||
static int handle_command(char *input)
|
||||
{
|
||||
const struct console_command *cmd;
|
||||
@@ -84,7 +100,7 @@ static int handle_command(char *input)
|
||||
int rv;
|
||||
|
||||
/* Split input into words. Ignore words past our limit. */
|
||||
split_words(input, MAX_ARGS_PER_COMMAND, &argc, argv);
|
||||
split_words(input, &argc, argv);
|
||||
|
||||
/* If no command, nothing to do */
|
||||
if (!argc)
|
||||
@@ -119,25 +135,20 @@ static int handle_command(char *input)
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
static int console_init(void)
|
||||
static void console_init(void)
|
||||
{
|
||||
*input_buf = '\0';
|
||||
uart_set_console_mode(1);
|
||||
ccprintf("Console is enabled; type HELP for help.\n");
|
||||
ccputs(PROMPT);
|
||||
|
||||
/* TODO: restore channel list from EEPROM */
|
||||
|
||||
return EC_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* handle a console command */
|
||||
static void console_process(void)
|
||||
{
|
||||
/* Process all the pending commands. Need to do this all at once
|
||||
* since our interrupt may have been triggered multiple times. */
|
||||
/*
|
||||
* Process all pending console commands. Need to do this all at once
|
||||
* since our interrupt may have been triggered multiple times.
|
||||
*/
|
||||
while (uart_peek('\n') >= 0) {
|
||||
uart_gets(input_buf, sizeof(input_buf));
|
||||
handle_command(input_buf);
|
||||
@@ -145,14 +156,12 @@ static void console_process(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void console_has_input(void)
|
||||
{
|
||||
/* Wake up the console task */
|
||||
task_wake(TASK_ID_CONSOLE);
|
||||
}
|
||||
|
||||
|
||||
void console_task(void)
|
||||
{
|
||||
console_init();
|
||||
@@ -164,7 +173,6 @@ void console_task(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Console commands */
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
/* Default to all channels active */
|
||||
#ifndef CC_DEFAULT
|
||||
#define CC_DEFAULT CC_ALL
|
||||
#define CC_DEFAULT CC_ALL
|
||||
#endif
|
||||
static uint32_t channel_mask = CC_DEFAULT;
|
||||
|
||||
@@ -54,7 +54,6 @@ int cputs(enum console_channel channel, const char *outstr)
|
||||
return uart_puts(outstr);
|
||||
}
|
||||
|
||||
|
||||
int cprintf(enum console_channel channel, const char *format, ...)
|
||||
{
|
||||
int rv;
|
||||
@@ -70,7 +69,6 @@ int cprintf(enum console_channel channel, const char *format, ...)
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
void cflush(void)
|
||||
{
|
||||
uart_flush_output();
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef __CROS_EC_CONSOLE_H
|
||||
#define __CROS_EC_CONSOLE_H
|
||||
|
||||
#include "board.h"
|
||||
#include "common.h"
|
||||
|
||||
/* Console command; used by DECLARE_CONSOLE_COMMAND macro. */
|
||||
@@ -25,7 +24,6 @@ struct console_command {
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
/* Console channels */
|
||||
enum console_channel {
|
||||
CC_COMMAND = 0, /* Console command (interactive I/O). Use this only
|
||||
@@ -61,16 +59,29 @@ enum console_channel {
|
||||
/* Mask to use to enable all channels */
|
||||
#define CC_ALL 0xffffffffUL
|
||||
|
||||
|
||||
/* Put a string to the console channel. */
|
||||
/**
|
||||
* Put a string to the console channel.
|
||||
*
|
||||
* @param channel Output chanel
|
||||
* @param outstr String to write
|
||||
*
|
||||
* @return non-zero if output was truncated.
|
||||
*/
|
||||
int cputs(enum console_channel channel, const char *outstr);
|
||||
|
||||
/* Print formatted output to the console channel.
|
||||
/**
|
||||
* Print formatted output to the console channel.
|
||||
*
|
||||
* See printf.h for valid formatting codes. */
|
||||
* @param channel Output chanel
|
||||
* @param format Format string; see printf.h for valid formatting codes
|
||||
*
|
||||
* @return non-zero if output was truncated.
|
||||
*/
|
||||
int cprintf(enum console_channel channel, const char *format, ...);
|
||||
|
||||
/* Flush the console output for all channels. */
|
||||
/**
|
||||
* Flush the console output for all channels.
|
||||
*/
|
||||
void cflush(void);
|
||||
|
||||
/* Convenience macros for printing to the command channel.
|
||||
@@ -82,14 +93,22 @@ void cflush(void);
|
||||
* http://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html */
|
||||
#define ccprintf(format, args...) cprintf(CC_COMMAND, format, ## args)
|
||||
|
||||
|
||||
/* Called by UART when a line of input is pending. */
|
||||
/**
|
||||
* Called by UART when a line of input is pending.
|
||||
*/
|
||||
void console_has_input(void);
|
||||
|
||||
|
||||
/*
|
||||
* Register a console command handler. Note that `name' must never be a
|
||||
* beginning of another existing command name.
|
||||
/**
|
||||
* Register a console command handler.
|
||||
*
|
||||
* @param name Command name; must not be the beginning of another
|
||||
* existing command name. Note this is NOT in quotes
|
||||
* so it can be concatenated to form a struct name.
|
||||
* @param routine Command handling routine, of the form
|
||||
* int handler(int argc, char **argv)
|
||||
* @param argdesc String describing arguments to command; NULL if none.
|
||||
* @param shorthelp String with one-line description of command.
|
||||
* @param longhelp String with long description of command.
|
||||
*/
|
||||
#ifdef CONFIG_CONSOLE_CMDHELP
|
||||
#define DECLARE_CONSOLE_COMMAND(name, routine, argdesc, shorthelp, longhelp) \
|
||||
|
||||
Reference in New Issue
Block a user