esh_history.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "esh_history.h"
  2. void EShAddCommandToHistory(char* command) {
  3. int list_size = esh_info_global->history_command_list_size;
  4. char** list = esh_info_global->history_command_list;
  5. if (list_size == esh_info_global->history_limit) {
  6. for (int i = 0; i <= list_size / 2; ++i) {
  7. free(list[i]);
  8. }
  9. for (int i = list_size / 2 + 1; i < list_size; ++i) {
  10. list[i - (list_size / 2 + 1)] = list[i];
  11. list[i] = NULL;
  12. }
  13. esh_info_global->history_command_list_size -= list_size / 2 + 1;
  14. list_size -= list_size / 2 + 1;
  15. }
  16. list[list_size] = malloc((strlen(command) + 1) * sizeof(char));
  17. strcpy(list[list_size], command);
  18. ++esh_info_global->history_command_list_size;
  19. }
  20. char* EShReceiveCommandFromHistory(int step) {
  21. int list_size = esh_info_global->history_command_list_size;
  22. char** list = esh_info_global->history_command_list;
  23. if (step >= list_size) {
  24. return NULL;
  25. }
  26. char* fragment = malloc((strlen(list[list_size - step - 1]) + 1) * sizeof(char));
  27. strcpy(fragment, list[list_size - step - 1]);
  28. return fragment;
  29. }