menu: pass remaning of line instead of word when argument is a string

This commit is contained in:
King Kévin 2018-03-17 19:24:34 +01:00
parent 8d0dfeef32
commit 70b166c1cc
1 changed files with 7 additions and 3 deletions

View File

@ -13,7 +13,7 @@
*
*/
/** definitions to build menus (code)
* @file menu.h
* @file menu.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
*/
@ -39,7 +39,7 @@ bool menu_handle_command(const char* line, const struct menu_command_t* command_
}
// get command
char* dup = malloc(strlen(line)+1); // buffer to copy the line
char* dup = calloc(strlen(line)+1, sizeof(char)); // 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(dup, delimiter); // get first word
@ -57,6 +57,7 @@ bool menu_handle_command(const char* line, const struct menu_command_t* command_
if (MENU_ARGUMENT_NONE==command.argument) { // check if argument can be passed
(*command.command_handler)(NULL); // call without argument
} else { // argument can be passed
const char* original_argument = line+strlen(word)+1; // remember the start of the argument in the original line
word = strtok(NULL, delimiter); // get next word
if (!word) { // no argument provided
(*command.command_handler)(NULL); // call without argument
@ -70,7 +71,10 @@ bool menu_handle_command(const char* line, const struct menu_command_t* command_
double argument = atof(word); // get floating point number
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_STRING==command.argument) { // next argument should be a string
(*command.command_handler)(word); // call with argument
if (delimiter[0]==original_argument[strlen(word)]) { // if there is more than one word
word[strlen(word)] = delimiter[0]; // remove the end of string
}
(*command.command_handler)(word); // call with argument (remaining of the string)
}
}
}