/* 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 .
*
*/
/** definitions to build menus (code)
* @file menu.c
* @author King Kévin
* @date 2018
*/
/* standard libraries */
#include // standard integer types
#include // boolean types
#include // standard utilities
#include // string utilities
/* own libraries */
#include "menu.h" // definitions for the menu
#include "print.h" // print utilities
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)) {
return false;
}
// ensure command are available
if (NULL==command_list || 0==command_list_length) {
return false;
}
// get command
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
if (!word) {
return false;
}
// find corresponding command
bool command_found = false; // remember if we found the corresponding command
for (size_t i=0; i