/[LeafOK_CVS]/lbbs/src/io.c
ViewVC logotype

Diff of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.36 by sysadm, Wed May 28 10:26:21 2025 UTC Revision 1.45 by sysadm, Tue Jun 17 13:25:49 2025 UTC
# Line 27  Line 27 
27  #include <sys/select.h>  #include <sys/select.h>
28  #include <sys/ioctl.h>  #include <sys/ioctl.h>
29  #include <sys/epoll.h>  #include <sys/epoll.h>
30    #include <libssh/libssh.h>
31    #include <libssh/server.h>
32    #include <libssh/callbacks.h>
33    
34  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
35  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
# Line 88  int outc(char c) Line 91  int outc(char c)
91          return ret;          return ret;
92  }  }
93    
94  int iflush()  int iflush(void)
95  {  {
96          int flags;          int flags;
97          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
# Line 147  int iflush() Line 150  int iflush()
150                          {                          {
151                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error
152                                  {                                  {
153                                          ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset));                                          if (SSH_v2)
154                                            {
155                                                    ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset));
156                                                    if (ret == SSH_ERROR)
157                                                    {
158                                                            log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
159                                                            retry = 0;
160                                                            break;
161                                                    }
162                                            }
163                                            else
164                                            {
165                                                    ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset));
166                                            }
167                                          if (ret < 0)                                          if (ret < 0)
168                                          {                                          {
169                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 249  int igetch(int timeout) Line 265  int igetch(int timeout)
265                  len = 0;                  len = 0;
266                  pos = 0;                  pos = 0;
267    
268                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
269                    {
270                            log_error("SSH channel is closed\n");
271                            loop = 0;
272                            break;
273                    }
274    
275                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
276    
277                  if (nfds < 0)                  if (nfds < 0)
# Line 272  int igetch(int timeout) Line 295  int igetch(int timeout)
295                          {                          {
296                                  while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                  while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error
297                                  {                                  {
298                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                          if (SSH_v2)
299                                            {
300                                                    ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);
301                                                    if (ret == SSH_ERROR)
302                                                    {
303                                                            log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
304                                                            loop = 0;
305                                                            break;
306                                                    }
307                                                    else if (ret == SSH_EOF)
308                                                    {
309                                                            loop = 0;
310                                                            break;
311                                                    }
312                                                    else if (ret == 0)
313                                                    {
314                                                            out = 0;
315                                                            break; // Check whether channel is still open
316                                                    }
317                                            }
318                                            else
319                                            {
320                                                    ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);
321                                            }
322                                          if (ret < 0)                                          if (ret < 0)
323                                          {                                          {
324                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 307  int igetch(int timeout) Line 353  int igetch(int timeout)
353                  }                  }
354    
355                  // For debug                  // For debug
356                  // for (int j = pos; j < len; j++)  #ifdef _DEBUG
357                  // {                  for (int j = pos; j < len; j++)
358                  //      log_std("Debug: <--[%u]\n", (buf[j] + 256) % 256);                  {
359                  // }                          log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);
360                    }
361    #endif
362          }          }
363    
364          fcntl(STDIN_FILENO, F_SETFL, flags);          fcntl(STDIN_FILENO, F_SETFL, flags);
# Line 341  int igetch(int timeout) Line 389  int igetch(int timeout)
389                          continue;                          continue;
390                  }                  }
391    
392                  if (c == ESC_KEY)                  if (c == KEY_ESC)
393                  {                  {
394                          if (in_esc == 0)                          if (in_esc == 0)
395                          {                          {
# Line 352  int igetch(int timeout) Line 400  int igetch(int timeout)
400                          }                          }
401                          else                          else
402                          {                          {
403                                  out = ESC_KEY;                                  out = KEY_CSI;
404                                  in_esc = 0;                                  in_esc = 0;
405                                  break;                                  break;
406                          }                          }
# Line 488  int igetch(int timeout) Line 536  int igetch(int timeout)
536                                  case 49:                                  case 49:
537                                          out = KEY_HOME;                                          out = KEY_HOME;
538                                          break;                                          break;
539                                    case 50:
540                                            out = KEY_INS;
541                                            break;
542                                  case 51:                                  case 51:
543                                          out = KEY_DEL;                                          out = KEY_DEL;
544                                          break;                                          break;
# Line 508  int igetch(int timeout) Line 559  int igetch(int timeout)
559                                          break;                                          break;
560                                  }                                  }
561                          }                          }
562                          if (i == 4 && tmp[0] == 91 && tmp[1] == 49 && tmp[3] == 126)  // Fterm                          if (i == 4 && tmp[0] == 91 && tmp[1] == 49 && tmp[3] == 126) // Fterm
563                          {                          {
564                                  in_ascii = 0;                                  in_ascii = 0;
565                                  switch (tmp[2])                                  switch (tmp[2])
# Line 578  int igetch(int timeout) Line 629  int igetch(int timeout)
629                                  in_ascii = 0;                                  in_ascii = 0;
630                                  switch (tmp[4])                                  switch (tmp[4])
631                                  {                                  {
632                                    case 65:
633                                            out = KEY_CTRL_UP;
634                                            break;
635                                    case 66:
636                                            out = KEY_CTRL_DOWN;
637                                            break;
638                                    case 67:
639                                            out = KEY_CTRL_RIGHT;
640                                            break;
641                                    case 68:
642                                            out = KEY_CTRL_LEFT;
643                                            break;
644                                    case 70:
645                                            out = KEY_CTRL_END;
646                                            break;
647                                    case 72:
648                                            out = KEY_CTRL_HOME;
649                                            break;
650                                  case 80:                                  case 80:
651                                          out = KEY_CTRL_F1;                                          out = KEY_CTRL_F1;
652                                          break;                                          break;
# Line 744  int igetch(int timeout) Line 813  int igetch(int timeout)
813          }          }
814    
815          // for debug          // for debug
816          // if (out != KEY_TIMEOUT && out != KEY_NULL)  #ifdef _DEBUG
817          // {          if (out != KEY_TIMEOUT && out != KEY_NULL)
818          //      log_std ("Debug: -->[0x %x]\n", out);          {
819          // }                  log_common("Debug: -->[0x %x]\n", out);
820            }
821    #endif
822    
823          return out;          return out;
824  }  }
# Line 755  int igetch(int timeout) Line 826  int igetch(int timeout)
826  int igetch_t(int sec)  int igetch_t(int sec)
827  {  {
828          int ch;          int ch;
829          time_t t_begin = time(0);          time_t t_begin = time(NULL);
830    
831          do          do
832          {          {
833                  ch = igetch(100);                  ch = igetch(100);
834          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec));
835    
836          return ch;          return ch;
837  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1