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
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 * -----------------------------------------------------------------------
31 #include <syslinux/config.h>
33 #include <acpi/acpi.h>
35 #include "hdt-common.h"
37 struct cli_mode_descr
*list_modes
[] = {
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
58 const char *exit_aliases
[] = { "q", "quit" };
59 const char *help_aliases
[] = { "h", "?" };
62 struct cli_alias hdt_aliases
[] = {
66 .aliases
= exit_aliases
,
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
));
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
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
)
131 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_HDT
);
134 if (hardware
->sv
->filesystem
!= SYSLINUX_FS_PXELINUX
) {
135 more_printf("You are not currently using PXELINUX\n");
139 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_PXE
);
143 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_KERNEL
);
147 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_SYSLINUX
);
151 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_VESA
);
155 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_PCI
);
159 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_CPU
);
162 if (!hardware
->is_dmi_valid
) {
163 more_printf("No valid DMI table found, exiting.\n");
167 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_DMI
);
171 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_DISK
);
174 if (!hardware
->is_vpd_valid
) {
175 more_printf("No valid VPD table found, exiting.\n");
179 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_VPD
);
183 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_MEMORY
);
187 snprintf(hdt_cli
.prompt
, sizeof(hdt_cli
.prompt
), "%s> ", CLI_ACPI
);
191 more_printf("Unknown mode, please choose among:\n");
192 while (list_modes
[i
]) {
193 more_printf("\t%s\n", list_modes
[i
]->name
);
198 find_cli_mode_descr(hdt_cli
.mode
, ¤t_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
)
213 while (list_modes
[i
]) {
214 if (!strncmp(name
, list_modes
[i
]->name
, sizeof(list_modes
[i
]->name
)))
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
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
)
238 while (list_modes
[i
] && list_modes
[i
]->mode
!= mode
)
241 /* Shouldn't get here... */
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
;
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 */
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]);
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 */
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]);
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;
330 char *pch
= NULL
, *pch_next
= NULL
, *tmp_pch_next
= NULL
;
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'
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))
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
;
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) {
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
;
383 dprintf("CLI DEBUG parse: argc = %d\n", *argc
);
385 /* Skip arguments handling if none is supplied */
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
));
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
)
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)
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
);
437 *module_found
= NULL
;
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
)
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
);
477 * Let's go now through the list of default_modules for the current mode
478 * (single token commands for the current_mode)
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
->
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
)
500 if (!hdt_mode
.default_modules
|| !hdt_mode
.default_modules
->modules
)
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
,
516 if (associated_module
== NULL
&&
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
].
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
538 static void autocomplete_module(char *command
, char *module
)
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
)
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
);
557 } else if (strncmp(CLI_SET
, command
, strlen(command
)) == 0) {
559 if (!current_mode
->set_modules
|| !current_mode
->set_modules
->modules
)
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
);
576 * autocomplete - find possible matches for a command line
577 * @line: command line to parse
579 static void autocomplete(char *line
)
583 char *command
= NULL
, *module
= 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 */
593 /* No argument, (the start of) a module has been specified */
594 if (module
!= NULL
) {
595 autocomplete_module(command
, module
);
600 /* No argument, no module, (the start of) a command has been specified */
601 if (command
!= NULL
) {
602 autocomplete_command(command
);
608 /* Let's not forget to clean ourselves */
609 for (i
= 0; i
< argc
; i
++)
617 * exec_command - main logic to map the command line to callbacks
619 static void exec_command(char *line
, struct s_hardware
*hardware
)
622 char *command
= NULL
, *module
= 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
,
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
));
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
);
677 find_cli_callback_descr(command
, hdt_mode
.default_modules
,
679 if (current_module
!= NULL
)
680 current_module
->exec(argc
, argv
, hardware
);
682 more_printf("unknown command: '%s'\n", command
);
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>
693 * dmi> show memory 0 1
694 * pci> show device 12
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
,
702 /* Execute the callback, if found */
703 if (current_module
!= NULL
)
704 current_module
->exec(argc
, argv
, hardware
);
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
,
710 /* Execute the callback, if found */
711 if (current_module
!= NULL
)
712 current_module
->exec(argc
, argv
, hardware
);
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
,
721 /* Execute the callback, if found */
722 if (current_module
!= NULL
)
723 current_module
->exec(argc
, argv
, hardware
);
725 /* Look now for a 'set' callback in the hdt mode */
726 find_cli_callback_descr(module
, hdt_mode
.set_modules
,
728 /* Execute the callback, if found */
729 if (current_module
!= NULL
)
730 current_module
->exec(argc
, argv
, hardware
);
732 printf("unknown module: '%s'\n", module
);
737 /* Let's not forget to clean ourselves */
742 for (i
= 0; i
< argc
; i
++)
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
);
754 hdt_cli
.cursor_pos
= 0;
758 void start_auto_mode(struct s_hardware
*hardware
)
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])) {
775 if ((commands
[nb_commands
] = malloc(AUTO_COMMAND_SIZE
)) != NULL
) {
776 sprintf(commands
[nb_commands
], "%s", remove_spaces(mypch
));
780 mypch
= strtok(NULL
, AUTO_SEPARATOR
);
783 /* Executing found commands */
784 for (int i
= 1; i
<= nb_commands
; i
++) {
787 more_printf("%s%s\n", hdt_cli
.prompt
, commands
[i
]);
788 exec_command(commands
[i
], hardware
);
794 more_printf("\nExiting Auto mode\n");
799 void print_history(int argc
, char **argv
, struct s_hardware
* hardware
)
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
]);
811 if (strlen(hdt_cli
.history
[i
]) == 0)
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
)
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
, ¤t_mode
);
834 if (current_mode
== NULL
) {
835 /* Shouldn't get here... */
836 more_printf("!!! BUG: Mode '%d' unknown.\n", hdt_cli
.mode
);
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");
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 */
871 /* Clear the end of the line */
873 memset(&INPUT
[hdt_cli
.cursor_pos
], 0,
874 strlen(INPUT
) - hdt_cli
.cursor_pos
);
883 if (hdt_cli
.cursor_pos
> 0) {
885 hdt_cli
.cursor_pos
--;
890 if (hdt_cli
.cursor_pos
< (int)strlen(INPUT
)) {
891 move_cursor_right(1);
892 hdt_cli
.cursor_pos
++;
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
);
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;
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
;
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
;
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
));
944 /* Move to the begining of line */
945 move_cursor_to_column(0);
949 hdt_cli
.cursor_pos
= strlen(INPUT
);
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;
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
;
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
));
985 /* Move to the begining of line */
986 move_cursor_to_column(0);
990 hdt_cli
.cursor_pos
= strlen(INPUT
);
994 if (autocomplete_backlog
) {
996 /* Move to the begining of line */
997 move_cursor_to_column(0);
999 printf("%s", autocomplete_last_seen
->autocomplete_token
);
1001 autocomplete_last_seen
->autocomplete_token
,
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
;
1011 autocomplete(skip_spaces(INPUT
));
1012 autocomplete_last_seen
= autocomplete_head
;
1014 printf("%s%s", hdt_cli
.prompt
, INPUT
);
1020 if (strlen(remove_spaces(INPUT
)) < 1) {
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
;
1048 /* No need to delete when input is empty */
1049 if (strlen(INPUT
) == 0)
1051 /* Don't delete when cursor is at the end of the line */
1052 if (hdt_cli
.cursor_pos
>= strlen(INPUT
))
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
));
1072 /* Don't delete prompt */
1073 if (hdt_cli
.cursor_pos
== 0)
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
+
1099 exec_command(CLI_HELP
, hardware
);
1104 if ((current_key
< 0x20) || (current_key
> 0x7e))
1106 /* Prevent overflow */
1107 if (hdt_cli
.cursor_pos
> MAX_LINE_SIZE
- 2)
1109 /* If we aren't at the end of the input line, let's insert */
1110 if (hdt_cli
.cursor_pos
< (int)strlen(INPUT
)) {
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
);
1132 putchar(current_key
);
1133 INPUT
[hdt_cli
.cursor_pos
] = current_key
;
1135 hdt_cli
.cursor_pos
++;