term.c 728 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "term.h"
  3. #include <stdlib.h>
  4. #include <termios.h>
  5. #include <unistd.h>
  6. #include <sys/ioctl.h>
  7. void get_term_dimensions(struct winsize *ws)
  8. {
  9. char *s = getenv("LINES");
  10. if (s != NULL) {
  11. ws->ws_row = atoi(s);
  12. s = getenv("COLUMNS");
  13. if (s != NULL) {
  14. ws->ws_col = atoi(s);
  15. if (ws->ws_row && ws->ws_col)
  16. return;
  17. }
  18. }
  19. #ifdef TIOCGWINSZ
  20. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  21. ws->ws_row && ws->ws_col)
  22. return;
  23. #endif
  24. ws->ws_row = 25;
  25. ws->ws_col = 80;
  26. }
  27. void set_term_quiet_input(struct termios *old)
  28. {
  29. struct termios tc;
  30. tcgetattr(0, old);
  31. tc = *old;
  32. tc.c_lflag &= ~(ICANON | ECHO);
  33. tc.c_cc[VMIN] = 0;
  34. tc.c_cc[VTIME] = 0;
  35. tcsetattr(0, TCSANOW, &tc);
  36. }