2018-01-21 23:35:22 +01:00
|
|
|
/* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/** definitions to build menus (code)
|
|
|
|
* @file menu.h
|
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
|
|
* @date 2018
|
|
|
|
*/
|
|
|
|
/* standard libraries */
|
|
|
|
#include <stdint.h> // standard integer types
|
|
|
|
#include <stdbool.h> // boolean types
|
|
|
|
#include <stdlib.h> // standard utilities
|
|
|
|
#include <string.h> // string utilities
|
|
|
|
|
|
|
|
/* own libraries */
|
|
|
|
#include "menu.h" // definitions for the menu
|
|
|
|
#include "print.h" // print utilities
|
|
|
|
|
2018-01-22 21:41:52 +01:00
|
|
|
bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length)
|
2018-01-21 23:35:22 +01:00
|
|
|
{
|
|
|
|
// ensure line is not empty
|
|
|
|
if (!line || 0==strlen(line)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ensure command are available
|
|
|
|
if (NULL==command_list || 0==command_list_length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get command
|
2018-01-22 21:41:52 +01:00
|
|
|
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
|
2018-01-21 23:35:22 +01:00
|
|
|
const char* delimiter = " "; // words are separated by spaces
|
2018-01-22 21:41:52 +01:00
|
|
|
char* word = strtok(dup, delimiter); // get first word
|
2018-01-21 23:35:22 +01:00
|
|
|
if (!word) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find corresponding command
|
|
|
|
bool command_found = false; // remember if we found the corresponding command
|
|
|
|
for (size_t i=0; i<command_list_length; i++) { // go through available command list
|
|
|
|
struct menu_command_t command = command_list[i]; // get current command
|
|
|
|
if ((1==strlen(word) && command.shortcut==word[0]) || 0==strcmp(word, command.name)) { // check if shortcut or name match
|
|
|
|
command_found = true; // remember we found the command
|
|
|
|
if (command.command_handler) { // ensure there is an command handler
|
|
|
|
if (MENU_ARGUMENT_NONE==command.argument) { // check if argument can be passed
|
|
|
|
(*command.command_handler)(NULL); // call without argument
|
|
|
|
} else { // argument can be passed
|
|
|
|
word = strtok(NULL, delimiter); // get next word
|
|
|
|
if (!word) { // no argument provided
|
|
|
|
(*command.command_handler)(NULL); // call without argument
|
|
|
|
} else if (MENU_ARGUMENT_SIGNED==command.argument) { // next argument should be a signed integer
|
|
|
|
int32_t argument = atoi(word); // get signed integer
|
|
|
|
(*command.command_handler)(&argument); // call with argument
|
|
|
|
} else if (MENU_ARGUMENT_UNSIGNED==command.argument) { // next argument should be an unsigned integer
|
|
|
|
uint32_t argument = atoi(word); // get unsigned integer
|
|
|
|
(*command.command_handler)(&argument); // call with argument
|
|
|
|
} else if (MENU_ARGUMENT_FLOAT==command.argument) { // next argument should be a floating point number
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break; // stop searching for the command
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find default command
|
|
|
|
if (!command_found) { // we didn't find the corresponding command
|
|
|
|
for (size_t i=0; i<command_list_length; i++) { // go through available command list
|
|
|
|
struct menu_command_t command = command_list[i]; // get current command
|
|
|
|
if (0==command.shortcut && NULL==command.name) { // check if there is a default command
|
|
|
|
command_found = true; // remember we found the command
|
|
|
|
if (command.command_handler) { // ensure there is an command handler
|
|
|
|
(*command.command_handler)(word); // call with current word
|
|
|
|
}
|
|
|
|
break; // stop searching for the command
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 21:41:52 +01:00
|
|
|
free(dup); // free line copy
|
2018-01-21 23:35:22 +01:00
|
|
|
return command_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_print_commands(const struct menu_command_t* command_list, size_t command_list_length)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<command_list_length; i++) {
|
|
|
|
struct menu_command_t command = command_list[i];
|
|
|
|
if (command.name) {
|
|
|
|
if (command.shortcut) {
|
|
|
|
printf("%c|", command.shortcut);
|
|
|
|
}
|
|
|
|
printf("%s", command.name);
|
|
|
|
if (MENU_ARGUMENT_NONE!=command.argument && command.argument_description) {
|
|
|
|
printf(" %s", command.argument_description);
|
2018-02-06 17:25:36 +01:00
|
|
|
} else {
|
|
|
|
printf("\t");
|
2018-01-21 23:35:22 +01:00
|
|
|
}
|
|
|
|
if (command.command_description) {
|
|
|
|
printf("\t%s", command.command_description);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|