Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

terminal.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * emulateur de terminal
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <sys/select.h>
  13. #include <ncurses.h>
  14. #include "../serial/serial.h"
  15. #include "terminal.h"
  16. extern int verbosity;
  17. int special_dumper(FILE *fp, unsigned char octet);
  18. /* ---------------------------------------------------------------- */
  19. static int interactive(WINDOW *glass, int fd_local, int fd_remote)
  20. {
  21. int flag_exit = 0;
  22. long tstart, tcur;
  23. char ligne[100];
  24. int received;
  25. /* --- variables for select */
  26. struct timeval tv;
  27. fd_set rfds;
  28. int retval,mfd;
  29. #if DEBUG_LEVEL
  30. fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__,
  31. glass, fd_local, fd_remote);
  32. #endif
  33. tstart = time(NULL);
  34. wclear(glass); wrefresh(glass);
  35. mfd = (fd_local>fd_remote ? fd_local : fd_remote) + 1; /* XXX */
  36. #if DEBUG_LEVEL
  37. sprintf(ligne, "%s : mfd is %d\n", __func__, mfd);
  38. waddstr(glass, ligne); wrefresh(glass);
  39. #endif
  40. do {
  41. tcur = time(NULL);
  42. FD_ZERO(&rfds);
  43. FD_SET(fd_local, &rfds); /* stdin */
  44. FD_SET(fd_remote, &rfds); /* teletype */
  45. tv.tv_sec = tv.tv_usec = 0; /* no timeout */
  46. retval = select(mfd, &rfds, NULL, NULL, &tv);
  47. if (-1 == retval) {
  48. sprintf(ligne, "err select %s\n", strerror(errno));
  49. waddstr(glass, ligne); wrefresh(glass);
  50. continue;
  51. }
  52. /*** est-ce la liaison serie ? */
  53. if (FD_ISSET(fd_remote, &rfds)) {
  54. /* get the incoming byte */
  55. received = getbyte(fd_remote);
  56. if (verbosity) {
  57. special_dumper(stderr, received);
  58. fflush(stderr);
  59. }
  60. if ( -1 == received) { /* anti ddos */
  61. sleep(1);
  62. waddstr(glass, " lost connection ?\n");
  63. }
  64. if ('\r' != received) waddch(glass, received);
  65. }
  66. /*** est-ce le yuser avec son clavier ? */
  67. if (FD_ISSET(fd_local, &rfds)) {
  68. noecho();
  69. received = wgetch(glass);
  70. echo();
  71. #if DEBUG_LEVEL > 1
  72. sprintf(ligne, " got $%X '%c'\n", received,
  73. isprint(received) ? received : '?');
  74. waddstr(glass, ligne); wrefresh(glass);
  75. sleep(1);
  76. #endif
  77. /* que doit-on faire avec ce qu'on
  78. vient de recevoir ? */
  79. if (0x04 == received) { /* EOT (end of transmission) */
  80. waddstr(glass, "--- EOT ---\n"); wrefresh(glass);
  81. flag_exit = 1;
  82. }
  83. putbyte(fd_remote, received);
  84. }
  85. wrefresh(glass);
  86. } while (! flag_exit);
  87. sleep(1);
  88. return -1;
  89. }
  90. /* ---------------------------------------------------------------- */
  91. int run_the_terminal(int fd_serial, char *title, WINDOW *win)
  92. {
  93. WINDOW *terminal, *ecran;
  94. int wid_term, hei_term, lin_term, col_term;
  95. int foo;
  96. // char ligne[100];
  97. // unsigned char byte;
  98. int fd_stdin;
  99. lin_term = 4; col_term = 9; /* position */
  100. wid_term = 60; hei_term = 25; /* dimensions */
  101. fd_stdin = fileno(stdin); /* for select or pool */
  102. terminal = newwin(hei_term, wid_term, lin_term, col_term);
  103. if ( NULL==terminal ) return -1;
  104. bordure(terminal, title, 1);
  105. ecran = subwin(terminal, hei_term-2, wid_term-2, lin_term+1, col_term+1);
  106. if ( NULL==ecran ) return -2;
  107. scrollok(ecran, 1);
  108. if (verbosity) {
  109. foo = mvwaddstr(ecran, 2, 14, "SCREEN READY\n");
  110. #if DEBUG_LEVEL > 1
  111. fprintf(stderr, "in '%s', mvwaddstr -> %d\n", __func__, foo);
  112. #endif
  113. wrefresh(ecran); sleep(1);
  114. }
  115. foo = interactive(ecran, fd_stdin, fd_serial);
  116. if (foo) {
  117. fprintf(stderr, "interactive -> %d\n", foo);
  118. sleep(1);
  119. }
  120. delwin(terminal);
  121. touchwin(stdscr); wrefresh(stdscr);
  122. return 0;
  123. }
  124. /* ---------------------------------------------------------------- */
  125. int essai_terminal(int fd, char *txt, int k)
  126. {
  127. int lig, col;
  128. int foo, retv;
  129. #if DEBUG_LEVEL
  130. fprintf(stderr, ">>> %s ( %d '%s' %d )\n", __func__,
  131. fd, txt, k );
  132. #endif
  133. /*** initialisation de curses */
  134. initscr();
  135. nonl(); cbreak(); noecho();
  136. for (lig=0; lig<LINES; lig++) {
  137. for (col=0; col<COLS; col++) {
  138. mvaddch(lig, col, "o+OX"[rand()%4]);
  139. }
  140. }
  141. wrefresh(stdscr);
  142. /*** passons a la partie active */
  143. retv = run_the_terminal(fd, txt, stdscr);
  144. /*** terminaison de curses */
  145. endwin();
  146. fprintf(stderr, "terminal return %d\n", retv);
  147. return 0;
  148. }
  149. /* ---------------------------------------------------------------- */