esh_history.c 971 B

12345678910111213141516171819202122232425262728293031323334
  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. const 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. return list[list_size - step - 1];
  27. }