Adding upstream version 6.02~pre8+dfsg.
[syslinux-debian/hramrach.git] / com32 / hdt / hdt-cli.c
blob216b6bde6a527929ce167028df81495ac220344d
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Erwan Velu - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * -----------------------------------------------------------------------
29 #include <stdlib.h>
30 #include <string.h>
31 #include <syslinux/config.h>
32 #include <getkey.h>
33 #include <acpi/acpi.h>
34 #include "hdt-cli.h"
35 #include "hdt-common.h"
37 struct cli_mode_descr *list_modes[] = {
38 &hdt_mode,
39 &dmi_mode,
40 &syslinux_mode,
41 &pxe_mode,
42 &kernel_mode,
43 &cpu_mode,
44 &pci_mode,
45 &vesa_mode,
46 &disk_mode,
47 &vpd_mode,
48 &memory_mode,
49 &acpi_mode,
50 NULL,
54 * .aliases = {"q", "quit"} won't work since it is an array of pointers, not an
55 * array of variables. There is no easy way around it besides declaring the arrays of
56 * strings first.
58 const char *exit_aliases[] = { "q", "quit" };
59 const char *help_aliases[] = { "h", "?" };
61 /* List of aliases */
62 struct cli_alias hdt_aliases[] = {
64 .command = CLI_EXIT,
65 .nb_aliases = 2,
66 .aliases = exit_aliases,
69 .command = CLI_HELP,
70 .nb_aliases = 2,
71 .aliases = help_aliases,
75 struct cli_mode_descr *current_mode;
76 int autocomplete_backlog;
78 struct autocomplete_list {
79 char autocomplete_token[MAX_LINE_SIZE];
80 struct autocomplete_list *next;
82 struct autocomplete_list *autocomplete_head = NULL;
83 struct autocomplete_list *autocomplete_tail = NULL;
84 struct autocomplete_list *autocomplete_last_seen = NULL;
86 static void autocomplete_add_token_to_list(const char *token)
88 struct autocomplete_list *new = malloc(sizeof(struct autocomplete_list));
90 strlcpy(new->autocomplete_token, token, sizeof(new->autocomplete_token));
91 new->next = NULL;
92 autocomplete_backlog++;
94 if (autocomplete_tail != NULL)
95 autocomplete_tail->next = new;
96 if (autocomplete_head == NULL)
97 autocomplete_head = new;
98 autocomplete_tail = new;
101 static void autocomplete_destroy_list(void)
103 struct autocomplete_list *tmp = NULL;
105 while (autocomplete_head != NULL) {
106 tmp = autocomplete_head->next;
107 free(autocomplete_head);
108 autocomplete_head = tmp;
110 autocomplete_backlog = 0;
111 autocomplete_tail = NULL;
112 autocomplete_last_seen = NULL;
116 * set_mode - set the current mode of the cli
117 * @mode: mode to set
119 * Unlike cli_set_mode, this function is not used by the cli directly.
121 void set_mode(cli_mode_t mode, struct s_hardware *hardware)
123 int i = 0;
125 switch (mode) {
126 case EXIT_MODE:
127 hdt_cli.mode = mode;
128 break;
129 case HDT_MODE:
130 hdt_cli.mode = mode;
131 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_HDT);
132 break;
133 case PXE_MODE:
134 if (hardware->sv->filesystem != SYSLINUX_FS_PXELINUX) {
135 more_printf("You are not currently using PXELINUX\n");
136 break;
138 hdt_cli.mode = mode;
139 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_PXE);
140 break;
141 case KERNEL_MODE:
142 hdt_cli.mode = mode;
143 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_KERNEL);
144 break;
145 case SYSLINUX_MODE:
146 hdt_cli.mode = mode;
147 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_SYSLINUX);
148 break;
149 case VESA_MODE:
150 hdt_cli.mode = mode;
151 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_VESA);
152 break;
153 case PCI_MODE:
154 hdt_cli.mode = mode;
155 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_PCI);
156 break;
157 case CPU_MODE:
158 hdt_cli.mode = mode;
159 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_CPU);
160 break;
161 case DMI_MODE:
162 if (!hardware->is_dmi_valid) {
163 more_printf("No valid DMI table found, exiting.\n");
164 break;
166 hdt_cli.mode = mode;
167 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_DMI);
168 break;
169 case DISK_MODE:
170 hdt_cli.mode = mode;
171 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_DISK);
172 break;
173 case VPD_MODE:
174 if (!hardware->is_vpd_valid) {
175 more_printf("No valid VPD table found, exiting.\n");
176 break;
178 hdt_cli.mode = mode;
179 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_VPD);
180 break;
181 case MEMORY_MODE:
182 hdt_cli.mode = mode;
183 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_MEMORY);
184 break;
185 case ACPI_MODE:
186 hdt_cli.mode = mode;
187 snprintf(hdt_cli.prompt, sizeof(hdt_cli.prompt), "%s> ", CLI_ACPI);
188 break;
189 default:
190 /* Invalid mode */
191 more_printf("Unknown mode, please choose among:\n");
192 while (list_modes[i]) {
193 more_printf("\t%s\n", list_modes[i]->name);
194 i++;
198 find_cli_mode_descr(hdt_cli.mode, &current_mode);
199 /* There is not cli_mode_descr struct for the exit mode */
200 if (current_mode == NULL && hdt_cli.mode != EXIT_MODE) {
201 /* Shouldn't get here... */
202 more_printf("!!! BUG: Mode '%d' unknown.\n", hdt_cli.mode);
207 * mode_s_to_mode_t - given a mode string, return the cli_mode_t representation
209 cli_mode_t mode_s_to_mode_t(char *name)
211 int i = 0;
213 while (list_modes[i]) {
214 if (!strncmp(name, list_modes[i]->name, sizeof(list_modes[i]->name)))
215 break;
216 i++;
219 if (!list_modes[i])
220 return INVALID_MODE;
221 else
222 return list_modes[i]->mode;
226 * find_cli_mode_descr - find the cli_mode_descr struct associated to a mode
227 * @mode: mode to look for
228 * @mode_found: store the mode if found, NULL otherwise
230 * Given a mode name, return a pointer to the associated cli_mode_descr
231 * structure.
232 * Note: the current mode name is stored in hdt_cli.mode.
234 void find_cli_mode_descr(cli_mode_t mode, struct cli_mode_descr **mode_found)
236 int i = 0;
238 while (list_modes[i] && list_modes[i]->mode != mode)
239 i++;
241 /* Shouldn't get here... */
242 if (!list_modes[i])
243 *mode_found = NULL;
244 else
245 *mode_found = list_modes[i];
249 * expand_aliases - resolve aliases mapping
250 * @line: command line to parse
251 * @command: first token in the line
252 * @module: second token in the line
253 * @argc: number of arguments
254 * @argv: array of arguments
256 * We maintain a small list of static alises to enhance user experience.
257 * Only commands can be aliased (first token). Otherwise it can become really hairy...
259 static void expand_aliases(char *line __unused, char **command, char **module,
260 int *argc, char **argv)
262 struct cli_mode_descr *mode;
263 int i, j;
265 find_cli_mode_descr(mode_s_to_mode_t(*command), &mode);
266 if (mode != NULL && *module == NULL) {
268 * The user specified a mode instead of `set mode...', e.g.
269 * `dmi' instead of `set mode dmi'
272 /* *argv is NULL since *module is NULL */
273 *argc = 1;
274 *argv = malloc(*argc * sizeof(char *));
275 argv[0] = malloc((sizeof(*command) + 1) * sizeof(char));
276 strlcpy(argv[0], *command, sizeof(*command) + 1);
277 dprintf("CLI DEBUG: ALIAS %s ", *command);
279 strlcpy(*command, CLI_SET, sizeof(CLI_SET)); /* set */
281 *module = malloc(sizeof(CLI_MODE) * sizeof(char));
282 strlcpy(*module, CLI_MODE, sizeof(CLI_MODE)); /* mode */
284 dprintf("--> %s %s %s\n", *command, *module, argv[0]);
285 goto out;
288 /* Simple aliases mapping a single command to another one */
289 for (i = 0; i < MAX_ALIASES; i++) {
290 for (j = 0; j < hdt_aliases[i].nb_aliases; j++) {
291 if (!strncmp(*command, hdt_aliases[i].aliases[j],
292 sizeof(hdt_aliases[i].aliases[j]))) {
293 dprintf("CLI DEBUG: ALIAS %s ", *command);
294 strlcpy(*command, hdt_aliases[i].command,
295 sizeof(hdt_aliases[i].command) + 1);
296 dprintf("--> %s\n", *command);
297 goto out; /* Don't allow chaining aliases */
301 return;
303 out:
304 dprintf("CLI DEBUG: New parameters:\n");
305 dprintf("CLI DEBUG: command = %s\n", *command);
306 dprintf("CLI DEBUG: module = %s\n", *module);
307 dprintf("CLI DEBUG: argc = %d\n", *argc);
308 for (i = 0; i < *argc; i++)
309 dprintf("CLI DEBUG: argv[%d] = %s\n", i, argv[0]);
310 return;
314 * parse_command_line - low level parser for the command line
315 * @line: command line to parse
316 * @command: first token in the line
317 * @module: second token in the line
318 * @argc: number of arguments
319 * @argv: array of arguments
321 * The format of the command line is:
322 * <main command> [<module on which to operate> [<args>]]
323 * command is always malloc'ed (even for an empty line)
325 static void parse_command_line(char *line, char **command, char **module,
326 int *argc, char **argv)
328 int argc_iter = 0, args_pos = 0, token_found = 0, token_len = 0;
329 int args_len = 0;
330 char *pch = NULL, *pch_next = NULL, *tmp_pch_next = NULL;
332 *command = NULL;
333 *module = NULL;
334 *argc = 0;
336 pch = line;
337 while (pch != NULL) {
338 pch_next = strchr(pch + 1, ' ');
339 tmp_pch_next = pch_next;
342 * Skip whitespaces if the user entered
343 * 'set mode foo' for 'set mode foo'
344 * ^ ^
345 * |___|___ pch
346 * |___ pch_next <- wrong!
348 * We still keep the position into tmp_pch_next to compute
349 * the lenght of the current token.
351 while (pch_next != NULL && !strncmp(pch_next, CLI_SPACE, 1))
352 pch_next++;
354 /* End of line guaranteed to be zeroed */
355 if (pch_next == NULL) {
356 token_len = (int)(strchr(pch + 1, '\0') - pch);
357 args_len = token_len;
358 } else {
359 token_len = (int)(tmp_pch_next - pch);
360 args_len = (int)(pch_next - pch);
363 if (token_found == 0) {
364 /* Main command to execute */
365 *command = malloc((token_len + 1) * sizeof(char));
366 strlcpy(*command, pch, token_len);
367 (*command)[token_len] = '\0';
368 dprintf("CLI DEBUG parse: command = %s\n", *command);
369 args_pos += args_len;
370 } else if (token_found == 1) {
371 /* Module */
372 *module = malloc((token_len + 1) * sizeof(char));
373 strlcpy(*module, pch, token_len);
374 (*module)[token_len] = '\0';
375 dprintf("CLI DEBUG parse: module = %s\n", *module);
376 args_pos += args_len;
377 } else
378 (*argc)++;
380 token_found++;
381 pch = pch_next;
383 dprintf("CLI DEBUG parse: argc = %d\n", *argc);
385 /* Skip arguments handling if none is supplied */
386 if (!*argc)
387 return;
389 /* Transform the arguments string into an array */
390 *argv = malloc(*argc * sizeof(char *));
391 pch = strtok(line + args_pos, CLI_SPACE);
392 while (pch != NULL) {
393 dprintf("CLI DEBUG parse: argv[%d] = %s\n", argc_iter, pch);
394 argv[argc_iter] = malloc(strlen(pch) * sizeof(char));
395 strlcpy(argv[argc_iter], pch, strlen(pch));
396 argc_iter++;
397 pch = strtok(NULL, CLI_SPACE);
399 * strtok(NULL, CLI_SPACE) over a stream of spaces
400 * will return an empty string
402 while (pch != NULL && !strncmp(pch, "", 1))
403 pch = strtok(NULL, CLI_SPACE);
408 * find_cli_callback_descr - find a callback in a list of modules
409 * @module_name: Name of the module to find
410 * @modules_list: Lits of modules among which to find @module_name
411 * @module_found: Pointer to the matched module, NULL if not found
413 * Given a module name and a list of possible modules, find the corresponding
414 * module structure that matches the module name and store it in @module_found.
416 void find_cli_callback_descr(const char *module_name,
417 struct cli_module_descr *modules_list,
418 struct cli_callback_descr **module_found)
420 int modules_iter = 0;
422 if (modules_list == NULL)
423 goto not_found;
425 /* Find the callback to execute */
426 while (modules_list->modules[modules_iter].name &&
427 strcmp(module_name, modules_list->modules[modules_iter].name) != 0)
428 modules_iter++;
430 if (modules_list->modules[modules_iter].name) {
431 *module_found = &(modules_list->modules[modules_iter]);
432 dprintf("CLI DEBUG: module %s found\n", (*module_found)->name);
433 return;
436 not_found:
437 *module_found = NULL;
438 return;
442 * autocomplete_command - print matching commands
443 * @command: Beginning of the command
445 * Given a string @command, print all availables commands starting with
446 * @command. Commands are found within the list of commands for the current
447 * mode and the hdt mode (if the current mode is not hdt).
449 static void autocomplete_command(char *command)
451 int j = 0;
452 struct cli_callback_descr *associated_module = NULL;
454 /* First take care of the two special commands: 'show' and 'set' */
455 if (strncmp(CLI_SHOW, command, strlen(command)) == 0) {
456 printf("%s\n", CLI_SHOW);
457 autocomplete_add_token_to_list(CLI_SHOW);
459 if (strncmp(CLI_SET, command, strlen(command)) == 0) {
460 printf("%s\n", CLI_SET);
461 autocomplete_add_token_to_list(CLI_SET);
465 * Then, go through the modes for the special case
466 * '<mode>' -> 'set mode <mode>'
468 while (list_modes[j]) {
469 if (strncmp(list_modes[j]->name, command, strlen(command)) == 0) {
470 printf("%s\n", list_modes[j]->name);
471 autocomplete_add_token_to_list(list_modes[j]->name);
473 j++;
477 * Let's go now through the list of default_modules for the current mode
478 * (single token commands for the current_mode)
480 j = 0;
481 if (current_mode->default_modules && current_mode->default_modules->modules) {
482 while (current_mode->default_modules->modules[j].name) {
483 if (strncmp(current_mode->default_modules->modules[j].name,
484 command, strlen(command)) == 0) {
485 printf("%s\n", current_mode->default_modules->modules[j].name);
486 autocomplete_add_token_to_list(current_mode->default_modules->
487 modules[j].name);
489 j++;
494 * Finally, if the current_mode is not hdt, list the available
495 * default_modules of hdt (these are always available from any mode).
497 if (current_mode->mode == HDT_MODE)
498 return;
500 if (!hdt_mode.default_modules || !hdt_mode.default_modules->modules)
501 return;
503 j = 0;
504 while (hdt_mode.default_modules &&
505 hdt_mode.default_modules->modules[j].name) {
507 * Any default command that is present in hdt mode but
508 * not in the current mode is available. A default
509 * command can be redefined in the current mode though.
510 * This next call tests this use case: if it is
511 * overwritten, do not print it again.
513 find_cli_callback_descr(hdt_mode.default_modules->modules[j].name,
514 current_mode->default_modules,
515 &associated_module);
516 if (associated_module == NULL &&
517 strncmp(command,
518 hdt_mode.default_modules->modules[j].name,
519 strlen(command)) == 0) {
520 printf("%s\n", hdt_mode.default_modules->modules[j].name);
521 autocomplete_add_token_to_list(hdt_mode.default_modules->modules[j].
522 name);
524 j++;
529 * autocomplete_module - print matching modules
530 * @command: Command on the command line (not NULL)
531 * @module: Beginning of the module
533 * Given a command @command and a string @module, print all availables modules
534 * starting with @module for command @command. Commands are found within the
535 * list of commands for the current mode and the hdt mode (if the current mode
536 * is not hdt).
538 static void autocomplete_module(char *command, char *module)
540 int j = 0;
541 char autocomplete_full_line[MAX_LINE_SIZE];
543 if (strncmp(CLI_SHOW, command, strlen(command)) == 0) {
544 if (!current_mode->show_modules || !current_mode->show_modules->modules)
545 return;
547 while (current_mode->show_modules->modules[j].name) {
548 if (strncmp(current_mode->show_modules->modules[j].name,
549 module, strlen(module)) == 0) {
550 printf("%s\n", current_mode->show_modules->modules[j].name);
551 sprintf(autocomplete_full_line, "%s %s",
552 CLI_SHOW, current_mode->show_modules->modules[j].name);
553 autocomplete_add_token_to_list(autocomplete_full_line);
555 j++;
557 } else if (strncmp(CLI_SET, command, strlen(command)) == 0) {
558 j = 0;
559 if (!current_mode->set_modules || !current_mode->set_modules->modules)
560 return;
562 while (current_mode->set_modules->modules[j].name) {
563 if (strncmp(current_mode->set_modules->modules[j].name,
564 module, strlen(module)) == 0) {
565 printf("%s\n", current_mode->set_modules->modules[j].name);
566 sprintf(autocomplete_full_line, "%s %s",
567 CLI_SET, current_mode->set_modules->modules[j].name);
568 autocomplete_add_token_to_list(autocomplete_full_line);
570 j++;
576 * autocomplete - find possible matches for a command line
577 * @line: command line to parse
579 static void autocomplete(char *line)
581 int i;
582 int argc = 0;
583 char *command = NULL, *module = NULL;
584 char **argv = NULL;
586 parse_command_line(line, &command, &module, &argc, argv);
588 dprintf("CLI DEBUG autocomplete: before checking args\n");
589 /* If the user specified arguments, there is nothing we can complete */
590 if (argc != 0)
591 goto out;
593 /* No argument, (the start of) a module has been specified */
594 if (module != NULL) {
595 autocomplete_module(command, module);
596 free(module);
597 goto out;
600 /* No argument, no module, (the start of) a command has been specified */
601 if (command != NULL) {
602 autocomplete_command(command);
603 free(command);
604 goto out;
607 out:
608 /* Let's not forget to clean ourselves */
609 for (i = 0; i < argc; i++)
610 free(argv[i]);
611 if (argc > 0)
612 free(argv);
613 return;
617 * exec_command - main logic to map the command line to callbacks
619 static void exec_command(char *line, struct s_hardware *hardware)
621 int argc, i = 0;
622 char *command = NULL, *module = NULL;
623 char **argv = NULL;
624 struct cli_callback_descr *current_module = NULL;
626 /* This will allocate memory for command and module */
627 parse_command_line(line, &command, &module, &argc, argv);
629 dprintf("CLI DEBUG exec: Checking for aliases\n");
631 * Expand shortcuts, if needed
632 * This will allocate memory for argc/argv
634 expand_aliases(line, &command, &module, &argc, argv);
636 find_cli_callback_descr(command, current_mode->default_modules,
637 &current_module);
639 if ((module == NULL) || (current_module->nomodule == true)) {
640 dprintf("CLI DEBUG exec : single command detected\n");
642 * A single word was specified: look at the list of default
643 * commands in the current mode to see if there is a match.
644 * If not, it may be a generic function (exit, help, ...). These
645 * are stored in the list of default commands of the hdt mode.
648 /* First of all it the command doesn't need module, let's rework the arguments */
649 if ((current_module->nomodule == true) && ( module != NULL)) {
650 dprintf("CLI_DEBUG exec: Reworking arguments with argc=%d\n",argc);
651 char **new_argv=NULL;
652 new_argv=malloc((argc + 2)*sizeof(char *));
653 for (int argc_iter=0; argc_iter<argc; argc_iter++) {
654 dprintf("CLI_DEBUG exec rework : copy %d to %d (%s)\n",argc_iter,argc_iter+1,argv[argc_iter]);
655 new_argv[argc_iter+1] = malloc(strlen(argv[argc_iter]));
656 strlcpy(new_argv[argc_iter+1], argv[argc_iter], strlen(argv[argc_iter]));
657 free(argv[argc_iter]);
659 new_argv[0] = malloc(strlen(module)*sizeof(char));
660 strlcpy(new_argv[0], module, strlen(module));
661 argc++;
662 free(argv);
663 argv=new_argv;
666 if (current_module != NULL)
667 current_module->exec(argc, argv, hardware);
668 else if (!strncmp(command, CLI_SHOW, sizeof(CLI_SHOW) - 1) &&
669 current_mode->show_modules != NULL &&
670 current_mode->show_modules->default_callback != NULL)
671 current_mode->show_modules->default_callback(argc, argv, hardware);
672 else if (!strncmp(command, CLI_SET, sizeof(CLI_SET) - 1) &&
673 current_mode->set_modules != NULL &&
674 current_mode->set_modules->default_callback != NULL)
675 current_mode->set_modules->default_callback(argc, argv, hardware);
676 else {
677 find_cli_callback_descr(command, hdt_mode.default_modules,
678 &current_module);
679 if (current_module != NULL)
680 current_module->exec(argc, argv, hardware);
681 else
682 more_printf("unknown command: '%s'\n", command);
684 } else {
686 * A module has been specified! We now need to find the type of command.
688 * The syntax of the cli is the following:
689 * <type of command> <module on which to operate> <args>
690 * e.g.
691 * dmi> show system
692 * dmi> show bank 1
693 * dmi> show memory 0 1
694 * pci> show device 12
695 * hdt> set mode dmi
697 if (!strncmp(command, CLI_SHOW, sizeof(CLI_SHOW) - 1)) {
698 dprintf("CLI DEBUG exec: %s command detected\n", CLI_SHOW);
699 /* Look first for a 'show' callback in the current mode */
700 find_cli_callback_descr(module, current_mode->show_modules,
701 &current_module);
702 /* Execute the callback, if found */
703 if (current_module != NULL)
704 current_module->exec(argc, argv, hardware);
705 else {
706 dprintf("CLI DEBUG exec: Looking for callback\n");
707 /* Look now for a 'show' callback in the hdt mode */
708 find_cli_callback_descr(module, hdt_mode.show_modules,
709 &current_module);
710 /* Execute the callback, if found */
711 if (current_module != NULL)
712 current_module->exec(argc, argv, hardware);
713 else
714 printf("unknown module: '%s'\n", module);
716 } else if (!strncmp(command, CLI_SET, sizeof(CLI_SET) - 1)) {
717 dprintf("CLI DEBUG exec : %s command detected\n", CLI_SET);
718 /* Look now for a 'set' callback in the hdt mode */
719 find_cli_callback_descr(module, current_mode->set_modules,
720 &current_module);
721 /* Execute the callback, if found */
722 if (current_module != NULL)
723 current_module->exec(argc, argv, hardware);
724 else {
725 /* Look now for a 'set' callback in the hdt mode */
726 find_cli_callback_descr(module, hdt_mode.set_modules,
727 &current_module);
728 /* Execute the callback, if found */
729 if (current_module != NULL)
730 current_module->exec(argc, argv, hardware);
731 else
732 printf("unknown module: '%s'\n", module);
737 /* Let's not forget to clean ourselves */
738 if (command != NULL)
739 free(command);
740 if (module != NULL)
741 free(module);
742 for (i = 0; i < argc; i++)
743 free(argv[i]);
744 if (argc > 0)
745 free(argv);
748 static void reset_prompt(void)
750 /* No need to display the prompt if we exit */
751 if (hdt_cli.mode != EXIT_MODE) {
752 printf("%s", hdt_cli.prompt);
753 /* Reset the line */
754 hdt_cli.cursor_pos = 0;
758 void start_auto_mode(struct s_hardware *hardware)
760 char *mypch;
761 int nb_commands = 0;
762 char *commands[MAX_NB_AUTO_COMMANDS];
764 more_printf("\nEntering Auto mode\n");
766 /* Protecting the auto_label from the strtok modifications */
767 char *temp = strdup(hardware->auto_label);
769 /* Searching & saving all commands */
770 mypch = strtok(temp, AUTO_SEPARATOR);
771 while (mypch != NULL) {
772 if ((strlen(remove_spaces(mypch)) > 0) &&
773 (remove_spaces(mypch)[0] != AUTO_SEPARATOR[0])) {
774 nb_commands++;
775 if ((commands[nb_commands] = malloc(AUTO_COMMAND_SIZE)) != NULL) {
776 sprintf(commands[nb_commands], "%s", remove_spaces(mypch));
777 } else
778 nb_commands--;
780 mypch = strtok(NULL, AUTO_SEPARATOR);
783 /* Executing found commands */
784 for (int i = 1; i <= nb_commands; i++) {
785 if (commands[i]) {
786 if (!quiet)
787 more_printf("%s%s\n", hdt_cli.prompt, commands[i]);
788 exec_command(commands[i], hardware);
789 free(commands[i]);
793 if (!quiet)
794 more_printf("\nExiting Auto mode\n");
796 more_printf("\n");
799 void print_history(int argc, char **argv, struct s_hardware * hardware)
801 (void)argc;
802 (void)argv;
803 (void)hardware;
805 reset_more_printf();
806 for (int i = 1; i <= MAX_HISTORY_SIZE; i++) {
807 if (i == hdt_cli.history_pos) {
808 more_printf("*%d:'%s'\n", i, hdt_cli.history[i]);
809 continue;
811 if (strlen(hdt_cli.history[i]) == 0)
812 continue;
813 more_printf(" %d:'%s'\n", i, hdt_cli.history[i]);
817 /* Code that manages the cli mode */
818 void start_cli_mode(struct s_hardware *hardware)
820 int current_key = 0;
821 int future_history_pos = 1; /* position of the next position in the history */
822 int current_future_history_pos = 1; /* Temp variable */
823 bool display_history = true; /* Temp Variable */
824 char temp_command[MAX_LINE_SIZE];
826 hdt_cli.cursor_pos = 0;
827 memset(hdt_cli.history, 0, sizeof(hdt_cli.history));
828 hdt_cli.history_pos = 1;
829 hdt_cli.max_history_pos = 1;
831 /* Find the mode selected */
832 set_mode(HDT_MODE, hardware);
833 find_cli_mode_descr(hdt_cli.mode, &current_mode);
834 if (current_mode == NULL) {
835 /* Shouldn't get here... */
836 more_printf("!!! BUG: Mode '%d' unknown.\n", hdt_cli.mode);
837 return;
840 /* Start the auto mode if the command line is set */
841 if (strlen(hardware->auto_label) > 0) {
842 start_auto_mode(hardware);
845 more_printf("Entering CLI mode\n");
847 reset_prompt();
849 while (hdt_cli.mode != EXIT_MODE) {
851 /* Display the cursor */
852 display_cursor(true);
854 /* Let's put the cursor blinking until we get an input */
855 set_cursor_blink(true);
857 /* We wait endlessly for a keyboard input */
858 current_key = get_key(stdin, 0);
860 /* We have to cancel the blinking mode to prevent
861 * input text to blink */
862 set_cursor_blink(false);
864 /* Reset autocomplete buffer unless TAB is pressed */
865 if (current_key != KEY_TAB)
866 autocomplete_destroy_list();
868 switch (current_key) {
869 /* clear until then end of line */
870 case KEY_CTRL('k'):
871 /* Clear the end of the line */
872 clear_end_of_line();
873 memset(&INPUT[hdt_cli.cursor_pos], 0,
874 strlen(INPUT) - hdt_cli.cursor_pos);
875 break;
877 case KEY_CTRL('c'):
878 printf("\n");
879 reset_prompt();
880 break;
882 case KEY_LEFT:
883 if (hdt_cli.cursor_pos > 0) {
884 move_cursor_left(1);
885 hdt_cli.cursor_pos--;
887 break;
889 case KEY_RIGHT:
890 if (hdt_cli.cursor_pos < (int)strlen(INPUT)) {
891 move_cursor_right(1);
892 hdt_cli.cursor_pos++;
894 break;
896 case KEY_CTRL('e'):
897 case KEY_END:
898 /* Calling with a 0 value will make the cursor move */
899 /* So, let's move the cursor only if needed */
900 if ((strlen(INPUT) - hdt_cli.cursor_pos) > 0) {
901 /* Return to the begining of line */
902 move_cursor_right(strlen(INPUT) - hdt_cli.cursor_pos);
903 hdt_cli.cursor_pos = strlen(INPUT);
905 break;
907 case KEY_CTRL('a'):
908 case KEY_HOME:
909 /* Calling with a 0 value will make the cursor move */
910 /* So, let's move the cursor only if needed */
911 if (hdt_cli.cursor_pos > 0) {
912 /* Return to the begining of line */
913 move_cursor_left(hdt_cli.cursor_pos);
914 hdt_cli.cursor_pos = 0;
916 break;
918 case KEY_UP:
920 /* Saving future position */
921 current_future_history_pos = future_history_pos;
923 /* We have to compute the next position */
924 if (future_history_pos == 1) {
925 future_history_pos = MAX_HISTORY_SIZE;
926 } else {
927 future_history_pos--;
930 /* Does the next position is valid */
931 if (strlen(hdt_cli.history[future_history_pos]) == 0) {
932 /* Position is invalid, restoring position */
933 future_history_pos = current_future_history_pos;
934 break;
937 /* Let's make that future position the one we use */
938 memset(INPUT, 0, sizeof(INPUT));
939 strlcpy(INPUT, hdt_cli.history[future_history_pos], sizeof(INPUT));
941 /* Clear the line */
942 clear_line();
944 /* Move to the begining of line */
945 move_cursor_to_column(0);
947 reset_prompt();
948 printf("%s", INPUT);
949 hdt_cli.cursor_pos = strlen(INPUT);
950 break;
952 case KEY_DOWN:
953 display_history = true;
955 /* Saving future position */
956 current_future_history_pos = future_history_pos;
958 if (future_history_pos == MAX_HISTORY_SIZE) {
959 future_history_pos = 1;
960 } else {
961 future_history_pos++;
964 /* Does the next position is valid */
965 if (strlen(hdt_cli.history[future_history_pos]) == 0)
966 display_history = false;
968 /* An exception is made to reach the last empty line */
969 if (future_history_pos == hdt_cli.max_history_pos)
970 display_history = true;
972 if (display_history == false) {
973 /* Position is invalid, restoring position */
974 future_history_pos = current_future_history_pos;
975 break;
978 /* Let's make that future position the one we use */
979 memset(INPUT, 0, sizeof(INPUT));
980 strlcpy(INPUT, hdt_cli.history[future_history_pos], sizeof(INPUT));
982 /* Clear the line */
983 clear_line();
985 /* Move to the begining of line */
986 move_cursor_to_column(0);
988 reset_prompt();
989 printf("%s", INPUT);
990 hdt_cli.cursor_pos = strlen(INPUT);
991 break;
993 case KEY_TAB:
994 if (autocomplete_backlog) {
995 clear_line();
996 /* Move to the begining of line */
997 move_cursor_to_column(0);
998 reset_prompt();
999 printf("%s", autocomplete_last_seen->autocomplete_token);
1000 strlcpy(INPUT,
1001 autocomplete_last_seen->autocomplete_token,
1002 sizeof(INPUT));
1003 hdt_cli.cursor_pos = strlen(INPUT);
1005 /* Cycle through the list */
1006 autocomplete_last_seen = autocomplete_last_seen->next;
1007 if (autocomplete_last_seen == NULL)
1008 autocomplete_last_seen = autocomplete_head;
1009 } else {
1010 printf("\n");
1011 autocomplete(skip_spaces(INPUT));
1012 autocomplete_last_seen = autocomplete_head;
1014 printf("%s%s", hdt_cli.prompt, INPUT);
1016 break;
1018 case KEY_ENTER:
1019 printf("\n");
1020 if (strlen(remove_spaces(INPUT)) < 1) {
1021 reset_prompt();
1022 break;
1024 exec_command(remove_spaces(INPUT), hardware);
1025 hdt_cli.history_pos++;
1027 /* Did we reach the end of the history ?*/
1028 if (hdt_cli.history_pos > MAX_HISTORY_SIZE) {
1029 /* Let's return at the beginning */
1030 hdt_cli.history_pos = 1;
1033 /* Does the next position is already used ?
1034 * If yes, we are cycling in history */
1035 if (strlen(INPUT) > 0) {
1036 /* Let's clean that entry */
1037 memset(&INPUT,0,sizeof(INPUT));
1040 future_history_pos = hdt_cli.history_pos;
1041 if (hdt_cli.history_pos > hdt_cli.max_history_pos)
1042 hdt_cli.max_history_pos = hdt_cli.history_pos;
1043 reset_prompt();
1044 break;
1046 case KEY_CTRL('d'):
1047 case KEY_DELETE:
1048 /* No need to delete when input is empty */
1049 if (strlen(INPUT) == 0)
1050 break;
1051 /* Don't delete when cursor is at the end of the line */
1052 if (hdt_cli.cursor_pos >= strlen(INPUT))
1053 break;
1055 for (int c = hdt_cli.cursor_pos; c < (int)strlen(INPUT) - 1; c++)
1056 INPUT[c] = INPUT[c + 1];
1057 INPUT[strlen(INPUT) - 1] = '\0';
1059 /* Clear the end of the line */
1060 clear_end_of_line();
1062 /* Print the resulting buffer */
1063 printf("%s", INPUT + hdt_cli.cursor_pos);
1065 /* Replace the cursor at the proper place */
1066 if (strlen(INPUT + hdt_cli.cursor_pos) > 0)
1067 move_cursor_left(strlen(INPUT + hdt_cli.cursor_pos));
1068 break;
1070 case KEY_DEL:
1071 case KEY_BACKSPACE:
1072 /* Don't delete prompt */
1073 if (hdt_cli.cursor_pos == 0)
1074 break;
1076 for (int c = hdt_cli.cursor_pos - 1;
1077 c < (int)strlen(INPUT) - 1; c++)
1078 INPUT[c] = INPUT[c + 1];
1079 INPUT[strlen(INPUT) - 1] = '\0';
1081 /* Get one char back */
1082 move_cursor_left(1);
1084 /* Clear the end of the line */
1085 clear_end_of_line();
1087 /* Print the resulting buffer */
1088 printf("%s", INPUT + hdt_cli.cursor_pos - 1);
1090 /* Realing to a char before the place we were */
1091 hdt_cli.cursor_pos--;
1092 move_cursor_to_column(strlen(hdt_cli.prompt) + hdt_cli.cursor_pos +
1095 break;
1097 case KEY_F1:
1098 printf("\n");
1099 exec_command(CLI_HELP, hardware);
1100 reset_prompt();
1101 break;
1103 default:
1104 if ((current_key < 0x20) || (current_key > 0x7e))
1105 break;
1106 /* Prevent overflow */
1107 if (hdt_cli.cursor_pos > MAX_LINE_SIZE - 2)
1108 break;
1109 /* If we aren't at the end of the input line, let's insert */
1110 if (hdt_cli.cursor_pos < (int)strlen(INPUT)) {
1111 char key[2];
1112 int trailing_chars = strlen(INPUT) - hdt_cli.cursor_pos;
1113 memset(temp_command, 0, sizeof(temp_command));
1114 strlcpy(temp_command, INPUT, hdt_cli.cursor_pos);
1115 sprintf(key, "%c", current_key);
1116 strncat(temp_command, key, 1);
1117 strncat(temp_command,
1118 INPUT + hdt_cli.cursor_pos, trailing_chars);
1119 memset(INPUT, 0, sizeof(INPUT));
1120 snprintf(INPUT, sizeof(INPUT), "%s", temp_command);
1122 /* Clear the end of the line */
1123 clear_end_of_line();
1125 /* Print the resulting buffer */
1126 printf("%s", INPUT + hdt_cli.cursor_pos);
1128 /* Return where we must put the new char */
1129 move_cursor_left(trailing_chars);
1131 } else {
1132 putchar(current_key);
1133 INPUT[hdt_cli.cursor_pos] = current_key;
1135 hdt_cli.cursor_pos++;
1136 break;