menu: make copy of line to process since strtok can modify original string

This commit is contained in:
King Kévin 2018-01-22 21:41:52 +01:00
parent ddbd821243
commit 8c983712b0
2 changed files with 6 additions and 3 deletions

View File

@ -27,7 +27,7 @@
#include "menu.h" // definitions for the menu
#include "print.h" // print utilities
bool menu_handle_command(char* line, const struct menu_command_t* command_list, size_t command_list_length)
bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length)
{
// ensure line is not empty
if (!line || 0==strlen(line)) {
@ -39,8 +39,10 @@ bool menu_handle_command(char* line, const struct menu_command_t* command_list,
}
// get command
char* dup = malloc(strlen(line)+1); // buffer to copy the line
strncpy(dup, line, strlen(line)+1); // make a copy of the line since strtok can modify it
const char* delimiter = " "; // words are separated by spaces
char* word = strtok(line, delimiter); // get first word
char* word = strtok(dup, delimiter); // get first word
if (!word) {
return false;
}
@ -90,6 +92,7 @@ bool menu_handle_command(char* line, const struct menu_command_t* command_list,
}
}
free(dup); // free line copy
return command_found;
}

View File

@ -43,7 +43,7 @@ struct menu_command_t {
* @prarm[in] command_list_length number of available commands
* @return if an command corresponding to the line has been found and called
*/
bool menu_handle_command(char* line, const struct menu_command_t* command_list, size_t command_list_length);
bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length);
/** print the commands from the command list
* @param[in] command_list list of available commands
* @prarm[in] command_list_length number of available commands