Change command interpreter semantics.

pull/4/head
Richard Barry 14 years ago
parent 59fbe1da22
commit 49f726cf25

@ -71,7 +71,7 @@ typedef struct xCOMMAND_INPUT_LIST
* The callback function that is executed when "help" is entered. This is the
* only default command that is always present.
*/
static const signed char *prvHelpCommand( void );
static portBASE_TYPE prvHelpCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen );
/* The definition of the "help" command. This command is always at the front
of the list of registered commands. */
@ -132,10 +132,10 @@ portBASE_TYPE xReturn = pdFAIL;
}
/*-----------------------------------------------------------*/
const signed char *pcCmdIntProcessCommand( const signed char * const pcCommandInput )
portBASE_TYPE xCmdIntProcessCommand( const signed char * const pcCommandInput, signed char * pcWriteBuffer, size_t xWriteBufferLen )
{
static const xCommandLineInputListItem *pxCommand = NULL;
signed const char *pcReturn = NULL;
portBASE_TYPE xReturn;
/* Note: This function is not re-entrant. It must not be called from more
thank one task. */
@ -156,52 +156,53 @@ signed const char *pcReturn = NULL;
if( pxCommand != NULL )
{
pcReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter();
/* Call the callback function that is registered to this command. */
xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen );
/* If no strings were returned, then all the strings that are going to
be returned by the current command have already been returned, and
pxCommand can be reset to NULL ready to search for the next entered
command. */
if( pcReturn == NULL )
/* If xReturn is pdFALSE, then no further strings will be returned
after this one, and pxCommand can be reset to NULL ready to search
for the next entered command. */
if( xReturn == pdFALSE )
{
pxCommand = NULL;
}
}
else
{
pcReturn = ( const signed char * const ) "Command not recognised. Available commands are listed below.\r\n\r\n";
/* Print out the help string. */
pxCommand = &xRegisteredCommands;
strncpy( ( char * ) pcWriteBuffer, ( const char * const ) "Command not recognised. Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
xReturn = pdFALSE;
}
return pcReturn;
return xReturn;
}
/*-----------------------------------------------------------*/
static const signed char *prvHelpCommand( void )
static portBASE_TYPE prvHelpCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen )
{
static const xCommandLineInputListItem * pxCommand = &xRegisteredCommands;
signed const char *pcReturn;
static const xCommandLineInputListItem * pxCommand = NULL;
signed portBASE_TYPE xReturn;
/* pxCommand will be NULL if all the commands in the list have already been
returned. */
if( pxCommand != NULL )
if( pxCommand == NULL )
{
/* Return the next command help string, before moving the pointer on to
the next command in the list. */
pcReturn = pxCommand->pxCommandLineDefinition->pcHelpString;
pxCommand = pxCommand->pxNext;
/* Reset the pxCommand pointer back to the start of the list. */
pxCommand = &xRegisteredCommands;
}
/* Return the next command help string, before moving the pointer on to
the next command in the list. */
strncpy( ( char * ) pcWriteBuffer, ( const char * ) pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
pxCommand = pxCommand->pxNext;
if( pxCommand == NULL )
{
/* There are no more commands in the list, so there will be no more
strings to return after this one and pdFALSE should be returned. */
xReturn = pdFALSE;
}
else
{
/* Reset the pointer back to the start of the list. */
pxCommand = &xRegisteredCommands;
/* Return NULL to show that there are no more strings to return. */
pcReturn = NULL;
xReturn = pdTRUE;
}
return pcReturn;
}
return xReturn;
}

@ -56,15 +56,17 @@
/* The prototype to which callback functions used to process command line
commands must comply. This type will change when commands with parameters
are included. */
typedef const signed char * (*pdCOMMAND_LINE_CALLBACK)( void );
are included. pcWriteBuffer is a buffer into which the output from executing
the command can be written, xWriteBufferLen is the length, in bytes, of the
pcWriteBuffer buffer. */
typedef portBASE_TYPE (*pdCOMMAND_LINE_CALLBACK)( signed char *pcWriteBuffer, size_t xWriteBufferLen );
/* The structure that defines command line commands. A command line command
should be defined by declaring a const structure of this type. */
typedef struct xCOMMAND_LINE_INPUT
{
const signed char * const pcCommand; /* The command that causes pxCommandInterpreter to be executed. For example "help". Must be all lower case. */
const signed char * const pcHelpString; /* String that describes how to use the command. Should start with the command itself, and end with "\r\n". For exxample "help: Returns a list of all the commands\r\n". */
const signed char * const pcHelpString; /* String that describes how to use the command. Should start with the command itself, and end with "\r\n". For example "help: Returns a list of all the commands\r\n". */
const pdCOMMAND_LINE_CALLBACK pxCommandInterpreter; /* A pointer to the callback function that will return the output generated by the command. */
} xCommandLineInput;
@ -77,18 +79,17 @@ typedef struct xCOMMAND_LINE_INPUT
portBASE_TYPE xCmdIntRegisterCommand( const xCommandLineInput * const pxCommandToRegister );
/*
* Runns the command interpreter for the command string "pcCommandInput". If
* pcCommandInput is valid (the command has been registered) a string will be
* returned, and pcCmdIntProcessCommand must then be called repeatedly until
* NULL is returned. If pcCommand pcCommandInput is not valid (the command is
* not recognised as a registered command) then an error message will be
* returned - and again pcCmdIntProcessCommand() must be called repeatedly
* until NULL is returned.
* Runs the command interpreter for the command string "pcCommandInput". Any
* output generated by running the command will be placed into pcWriteBuffer.
* xWriteBufferLen must indicate the size, in bytes, of the buffer pointed to
* by pcWriteBuffer.
*
* xCmdIntProcessCommand should be called repeatedly until it returns pdFALSE.
*
* pcCmdIntProcessCommand is not reentrant. It must not be called from more
* than one task - or at least - by more than one task at a time.
*/
const signed char *pcCmdIntProcessCommand( const signed char * const pcCommandInput );
portBASE_TYPE xCmdIntProcessCommand( const signed char * const pcCommandInput, signed char * pcWriteBuffer, size_t xWriteBufferLen );
#endif /* COMMAND_INTERPRETER_H */

Loading…
Cancel
Save