history.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "sh.h"
  2. /*
  3. ** Save history
  4. ** Add new command to history
  5. ** Link previous and next cmd
  6. */
  7. void save_history(t_e *e, t_hist *new)
  8. {
  9. if (new == NULL)
  10. return ;
  11. go_to_young_hist(e);
  12. e->hist->yng = new;
  13. new->old = e->hist;
  14. e->hist = e->hist->yng;
  15. }
  16. /*
  17. ** Malloc new history list
  18. ** Set cmd value
  19. ** Set previous and next to NULL
  20. */
  21. t_hist *new_hist(char *cmd)
  22. {
  23. t_hist *hist;
  24. if ((hist = (t_hist *)malloc(sizeof(t_hist))) == NULL || cmd == NULL)
  25. return (NULL);
  26. hist->cmd = ft_strdup(cmd);
  27. hist->yng = NULL;
  28. hist->old = NULL;
  29. return (hist);
  30. }
  31. /*
  32. ** Go get new commamd
  33. ** Save new cmd
  34. ** Get new cursor position
  35. */
  36. void move_history(t_e *e, int side)
  37. {
  38. move_on_history(e, side);
  39. display_history(e, side);
  40. if (side == UP || e->hist->yng != NULL)
  41. e->cmd = e->hist->cmd;
  42. e->pos = ft_strlen(e->cmd);
  43. }
  44. /*
  45. ** Don't go to first created (oldest) struct which is empty
  46. ** Don't move pointer if first
  47. ** If yng or old exist, move pointer to it, then display it
  48. */
  49. void move_on_history(t_e *e, int side)
  50. {
  51. if (e->hist == NULL)
  52. return ;
  53. if (side == UP && e->hist->old != NULL \
  54. && e->first == 0 && ft_strcmp(e->hist->old->cmd, "") != 0)
  55. e->hist = e->hist->old;
  56. else if (side == UP && e->hist->cmd != NULL && e->first == 1)
  57. e->first = 0;
  58. else if (side == DOWN && e->hist->yng != NULL)
  59. e->hist = e->hist->yng;
  60. else if (side == DOWN && e->hist->yng == NULL)
  61. e->first = 1;
  62. }