123456789101112131415161718192021222324252627282930313233343536 |
- #include "esh_history.h"
- void EShAddCommandToHistory(char* command) {
- int list_size = esh_info_global->history_command_list_size;
- char** list = esh_info_global->history_command_list;
- if (list_size == esh_info_global->history_limit) {
- for (int i = 0; i <= list_size / 2; ++i) {
- free(list[i]);
- }
- for (int i = list_size / 2 + 1; i < list_size; ++i) {
- list[i - (list_size / 2 + 1)] = list[i];
- list[i] = NULL;
- }
- esh_info_global->history_command_list_size -= list_size / 2 + 1;
- list_size -= list_size / 2 + 1;
- }
- list[list_size] = malloc((strlen(command) + 1) * sizeof(char));
- strcpy(list[list_size], command);
- ++esh_info_global->history_command_list_size;
- }
- char* EShReceiveCommandFromHistory(int step) {
- int list_size = esh_info_global->history_command_list_size;
- char** list = esh_info_global->history_command_list;
- if (step >= list_size) {
- return NULL;
- }
- char* fragment = malloc((strlen(list[list_size - step - 1]) + 1) * sizeof(char));
- strcpy(fragment, list[list_size - step - 1]);
- return fragment;
- }
|