/[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.34 by sysadm, Tue May 20 12:07:06 2025 UTC Revision 1.46 by sysadm, Wed Jun 18 04:29:18 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 58  int prints(const char *format, ...) Line 61  int prints(const char *format, ...)
61                  {                  {
62                          errno = EAGAIN;                          errno = EAGAIN;
63                          ret = (BUFSIZ - stdout_buf_len - ret);                          ret = (BUFSIZ - stdout_buf_len - ret);
64                            log_error("Output buffer is full, additional %d is required\n", ret);
65                  }                  }
66          }          }
67    
# Line 87  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 146  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 159  int iflush() Line 176  int iflush()
176                                                  }                                                  }
177                                                  else                                                  else
178                                                  {                                                  {
179    #ifdef _DEBUG
180                                                          log_error("write(STDOUT) error (%d)\n", errno);                                                          log_error("write(STDOUT) error (%d)\n", errno);
181    #endif
182                                                          retry = 0;                                                          retry = 0;
183                                                          break;                                                          break;
184                                                  }                                                  }
# Line 248  int igetch(int timeout) Line 267  int igetch(int timeout)
267                  len = 0;                  len = 0;
268                  pos = 0;                  pos = 0;
269    
270                    if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
271                    {
272                            log_error("SSH channel is closed\n");
273                            loop = 0;
274                            break;
275                    }
276    
277                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
278    
279                  if (nfds < 0)                  if (nfds < 0)
# Line 271  int igetch(int timeout) Line 297  int igetch(int timeout)
297                          {                          {
298                                  while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                  while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error
299                                  {                                  {
300                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                          if (SSH_v2)
301                                            {
302                                                    ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);
303                                                    if (ret == SSH_ERROR)
304                                                    {
305                                                            log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
306                                                            loop = 0;
307                                                            break;
308                                                    }
309                                                    else if (ret == SSH_EOF)
310                                                    {
311                                                            loop = 0;
312                                                            break;
313                                                    }
314                                                    else if (ret == 0)
315                                                    {
316                                                            out = 0;
317                                                            break; // Check whether channel is still open
318                                                    }
319                                            }
320                                            else
321                                            {
322                                                    ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);
323                                            }
324                                          if (ret < 0)                                          if (ret < 0)
325                                          {                                          {
326                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 286  int igetch(int timeout) Line 335  int igetch(int timeout)
335                                                  }                                                  }
336                                                  else                                                  else
337                                                  {                                                  {
338    #ifdef _DEBUG
339                                                          log_error("read(STDIN) error (%d)\n", errno);                                                          log_error("read(STDIN) error (%d)\n", errno);
340    #endif
341                                                          loop = 0;                                                          loop = 0;
342                                                          break;                                                          break;
343                                                  }                                                  }
# Line 306  int igetch(int timeout) Line 357  int igetch(int timeout)
357                  }                  }
358    
359                  // For debug                  // For debug
360                  // for (int j = pos; j < len; j++)  #ifdef _DEBUG
361                  // {                  for (int j = pos; j < len; j++)
362                  //      log_std("Debug: <--[%u]\n", (buf[j] + 256) % 256);                  {
363                  // }                          log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);
364                    }
365    #endif
366          }          }
367    
368          fcntl(STDIN_FILENO, F_SETFL, flags);          fcntl(STDIN_FILENO, F_SETFL, flags);
# Line 340  int igetch(int timeout) Line 393  int igetch(int timeout)
393                          continue;                          continue;
394                  }                  }
395    
396                  if (c == ESC_KEY)                  if (c == KEY_ESC)
397                  {                  {
398                          if (in_esc == 0)                          if (in_esc == 0)
399                          {                          {
# Line 351  int igetch(int timeout) Line 404  int igetch(int timeout)
404                          }                          }
405                          else                          else
406                          {                          {
407                                  out = ESC_KEY;                                  out = KEY_CSI;
408                                  in_esc = 0;                                  in_esc = 0;
409                                  break;                                  break;
410                          }                          }
# Line 487  int igetch(int timeout) Line 540  int igetch(int timeout)
540                                  case 49:                                  case 49:
541                                          out = KEY_HOME;                                          out = KEY_HOME;
542                                          break;                                          break;
543                                    case 50:
544                                            out = KEY_INS;
545                                            break;
546                                  case 51:                                  case 51:
547                                          out = KEY_DEL;                                          out = KEY_DEL;
548                                          break;                                          break;
# Line 507  int igetch(int timeout) Line 563  int igetch(int timeout)
563                                          break;                                          break;
564                                  }                                  }
565                          }                          }
566                          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
567                          {                          {
568                                  in_ascii = 0;                                  in_ascii = 0;
569                                  switch (tmp[2])                                  switch (tmp[2])
# Line 577  int igetch(int timeout) Line 633  int igetch(int timeout)
633                                  in_ascii = 0;                                  in_ascii = 0;
634                                  switch (tmp[4])                                  switch (tmp[4])
635                                  {                                  {
636                                    case 65:
637                                            out = KEY_CTRL_UP;
638                                            break;
639                                    case 66:
640                                            out = KEY_CTRL_DOWN;
641                                            break;
642                                    case 67:
643                                            out = KEY_CTRL_RIGHT;
644                                            break;
645                                    case 68:
646                                            out = KEY_CTRL_LEFT;
647                                            break;
648                                    case 70:
649                                            out = KEY_CTRL_END;
650                                            break;
651                                    case 72:
652                                            out = KEY_CTRL_HOME;
653                                            break;
654                                  case 80:                                  case 80:
655                                          out = KEY_CTRL_F1;                                          out = KEY_CTRL_F1;
656                                          break;                                          break;
# Line 736  int igetch(int timeout) Line 810  int igetch(int timeout)
810                  log_error("close(epoll) error (%d)\n");                  log_error("close(epoll) error (%d)\n");
811          }          }
812    
813            // For ESC key
814            if (out == 0 && in_esc)
815            {
816                    out = KEY_ESC;
817            }
818    
819          // for debug          // for debug
820          // if (out != KEY_TIMEOUT && out != KEY_NULL)  #ifdef _DEBUG
821          // {          if (out != KEY_TIMEOUT && out != KEY_NULL)
822          //      log_std ("Debug: -->[0x %x]\n", out);          {
823          // }                  log_common("Debug: -->[0x %x]\n", out);
824            }
825    #endif
826    
827          return out;          return out;
828  }  }
# Line 748  int igetch(int timeout) Line 830  int igetch(int timeout)
830  int igetch_t(int sec)  int igetch_t(int sec)
831  {  {
832          int ch;          int ch;
833          time_t t_begin = time(0);          time_t t_begin = time(NULL);
834    
835          do          do
836          {          {
837                  ch = igetch(100);                  ch = igetch(100);
838          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec));
839    
840          return ch;          return ch;
841  }  }


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

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