CuVoodoo STM32F1 firmware template
menu.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
20 /* standard libraries */
21 #include <stdint.h> // standard integer types
22 #include <stdbool.h> // boolean types
23 #include <stdlib.h> // standard utilities
24 #include <string.h> // string utilities
25 
26 /* own libraries */
27 #include "menu.h" // definitions for the menu
28 #include "print.h" // print utilities
29 
30 bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length)
31 {
32  // ensure line is not empty
33  if (!line || 0==strlen(line)) {
34  return false;
35  }
36  // ensure command are available
37  if (NULL==command_list || 0==command_list_length) {
38  return false;
39  }
40 
41  // get command
42  char* dup = calloc(strlen(line)+1, sizeof(char)); // buffer to copy the line
43  strncpy(dup, line, strlen(line)+1); // make a copy of the line since strtok can modify it
44  const char* delimiter = " "; // words are separated by spaces
45  char* word = strtok(dup, delimiter); // get first word
46  if (!word) {
47  return false;
48  }
49 
50  // find corresponding command
51  bool command_found = false; // remember if we found the corresponding command
52  for (size_t i=0; i<command_list_length; i++) { // go through available command list
53  struct menu_command_t command = command_list[i]; // get current command
54  if ((1==strlen(word) && command.shortcut==word[0]) || 0==strcmp(word, command.name)) { // check if shortcut or name match
55  command_found = true; // remember we found the command
56  if (command.command_handler) { // ensure there is an command handler
57  if (MENU_ARGUMENT_NONE==command.argument) { // check if argument can be passed
58  (*command.command_handler)(NULL); // call without argument
59  } else { // argument can be passed
60  const char* original_argument = line+strlen(word)+1; // remember the start of the argument in the original line
61  word = strtok(NULL, delimiter); // get next word
62  if (!word) { // no argument provided
63  (*command.command_handler)(NULL); // call without argument
64  } else if (MENU_ARGUMENT_SIGNED==command.argument) { // next argument should be a signed integer
65  int32_t argument = atoi(word); // get signed integer
66  (*command.command_handler)(&argument); // call with argument
67  } else if (MENU_ARGUMENT_UNSIGNED==command.argument) { // next argument should be an unsigned integer
68  uint32_t argument = atoi(word); // get unsigned integer
69  (*command.command_handler)(&argument); // call with argument
70  } else if (MENU_ARGUMENT_FLOAT==command.argument) { // next argument should be a floating point number
71  double argument = atof(word); // get floating point number
72  (*command.command_handler)(&argument); // call with argument
73  } else if (MENU_ARGUMENT_STRING==command.argument) { // next argument should be a string
74  if (delimiter[0]==original_argument[strlen(word)]) { // if there is more than one word
75  word[strlen(word)] = delimiter[0]; // remove the end of string
76  }
77  (*command.command_handler)(word); // call with argument (remaining of the string)
78  }
79  }
80  }
81  break; // stop searching for the command
82  }
83  }
84 
85  // find default command
86  if (!command_found) { // we didn't find the corresponding command
87  for (size_t i=0; i<command_list_length; i++) { // go through available command list
88  struct menu_command_t command = command_list[i]; // get current command
89  if (0==command.shortcut && NULL==command.name) { // check if there is a default command
90  command_found = true; // remember we found the command
91  if (command.command_handler) { // ensure there is an command handler
92  (*command.command_handler)(word); // call with current word
93  }
94  break; // stop searching for the command
95  }
96  }
97  }
98 
99  free(dup); // free line copy
100  return command_found;
101 }
102 
103 void menu_print_commands(const struct menu_command_t* command_list, size_t command_list_length)
104 {
105  for (size_t i=0; i<command_list_length; i++) {
106  struct menu_command_t command = command_list[i];
107  if (command.name) {
108  if (command.shortcut) {
109  printf("%c|", command.shortcut);
110  }
111  printf("%s", command.name);
112  if (MENU_ARGUMENT_NONE!=command.argument && command.argument_description) {
113  printf(" %s", command.argument_description);
114  } else {
115  printf("\t");
116  }
117  if (command.command_description) {
118  printf("\t%s", command.command_description);
119  }
120  printf("\n");
121  }
122  }
123 }
124 
command menu entry
Definition: menu.h:32
char * argument_description
human readable description of the argument it can accept
Definition: menu.h:37
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:36
char * name
complete name of the command (space-free)
Definition: menu.h:34
char shortcut
short command code (0 if not available)
Definition: menu.h:33
void(* command_handler)(void *argument)
function to be called to handle this command
Definition: menu.h:38
char * command_description
human readable description of the command purpose
Definition: menu.h:35