term.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "sh.h"
  2. /*
  3. ** Get TERM env var value
  4. ** If it doesn't exist write error message and exit
  5. ** Get termios struct
  6. ** Fill struct with term capacities
  7. ** Unset cannonic, echo and signal modes
  8. ** Set cannonic and echo modes
  9. ** Apply modifications
  10. */
  11. void set_term_modes(int set)
  12. {
  13. char *name_term;
  14. struct termios term;
  15. if ((name_term = getenv("TERM")) == NULL)
  16. error_message("termcaps", "No env var TERM specified", "exit");
  17. if (tgetent(NULL, name_term) == -1)
  18. error_message("termcaps", "tegetent error", "exit");
  19. if (tcgetattr(0, &term) == -1)
  20. exit(0);
  21. if (set == 1)
  22. {
  23. term.c_lflag = (ICANON | ECHO | ISIG);
  24. term_action("ei");
  25. }
  26. else
  27. {
  28. term.c_lflag &= ~(ICANON);
  29. term.c_lflag &= ~(ECHO);
  30. term.c_cc[VMIN] = 1;
  31. term.c_cc[VTIME] = 0;
  32. }
  33. if (tcsetattr(0, TCSADRAIN, &term) == -1)
  34. exit(0);
  35. }
  36. /*
  37. ** Malloc history stuct
  38. ** Display prompt.
  39. ** read on standard stream
  40. ** Save history
  41. ** if herething not executed:
  42. ** - detect blocks types (for cotes)
  43. ** - split at ';' and execute
  44. ** free cmd var
  45. */
  46. void prompt_get_cmd_keys(t_e *e)
  47. {
  48. int herething;
  49. e->hist = new_hist("");
  50. e->d = 0;
  51. while (1)
  52. {
  53. e->pos = 0;
  54. e->cmd = ft_strnew(0);
  55. e->plen = ft_strlen(PROMPT);
  56. ft_putstr(PROMPT);
  57. e->first = 1;
  58. set_term_modes(0);
  59. herething = read_std(e);
  60. set_term_modes(1);
  61. if (ft_strcmp(e->cmd, "") != 0)
  62. save_history(e, new_hist(e->cmd));
  63. if (herething == 0)
  64. {
  65. ft_putchar('\n');
  66. init_detect_blocks(e, e->cmd);
  67. comma_split(e, e->cmd);
  68. }
  69. ft_strdel(&(e->cmd));
  70. }
  71. }
  72. /*
  73. ** Read on standard stream.
  74. ** Join one buffer of one char in cmd var
  75. ** Save command
  76. ** If enter key pressed, break and execute cmd
  77. */
  78. int read_std(t_e *e)
  79. {
  80. int ret;
  81. int heret;
  82. e->key = ft_strnew(4);
  83. while ((ret = read(0, e->key, 4)) > 0)
  84. {
  85. calculate_values(e);
  86. if (ft_isprint(e->key[0]) == 1)
  87. {
  88. e->key[ret] = '\0';
  89. ft_putstr(e->key);
  90. insert(e);
  91. }
  92. else
  93. handle_keys(e, 1);
  94. if (e->key[0] == 10)
  95. {
  96. heret = herething(e);
  97. break ;
  98. }
  99. }
  100. return (heret);
  101. }
  102. /*
  103. ** Get cmd len
  104. ** Get screen sizes
  105. */
  106. void calculate_values(t_e *e)
  107. {
  108. struct winsize *w;
  109. e->len = ft_strlen(e->cmd);
  110. w = (struct winsize*)malloc(sizeof(struct winsize));
  111. ioctl(STDOUT_FILENO, TIOCGWINSZ, w);
  112. e->col_nbr = w->ws_col;
  113. free(w);
  114. e->maxline = (e->plen + e->len) / e->col_nbr;
  115. }