/[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.14 by sysadm, Mon Apr 28 03:30:59 2025 UTC Revision 1.25 by sysadm, Sat May 10 15:23:42 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                          io.c  -  description                                                          io.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "io.h"  #include "io.h"
18    #include "log.h"
19  #include "common.h"  #include "common.h"
20  #include "tcplib.h"  #include <errno.h>
21  #include <stdio.h>  #include <stdio.h>
22  #include <stdarg.h>  #include <stdarg.h>
23  #include <time.h>  #include <time.h>
24  #include <fcntl.h>  #include <fcntl.h>
25  #include <unistd.h>  #include <unistd.h>
26    #include <sys/select.h>
27  #include <sys/ioctl.h>  #include <sys/ioctl.h>
28    
29  int outc(char c)  int outc(char c)
# Line 55  int iflush() Line 56  int iflush()
56          return retval;          return retval;
57  }  }
58    
59  int igetch()  int igetch(int clear_buf)
60  {  {
61          static char buf[256];          // static input buffer
62          unsigned char c, tmp[256];          static unsigned char buf[LINE_BUFFER_LEN];
63          int out = KEY_NULL, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i = 0, j, result;          static ssize_t len = 0;
64          static int len = 0, pos = 0;          static int pos = 0;
65    
66          fd_set testfds;          fd_set testfds;
67          struct timeval timeout;          struct timeval timeout;
68    
69          // Stop on system exit          unsigned char tmp[LINE_BUFFER_LEN];
70          if (SYS_exit)          int ret;
71                  return KEY_NULL;          int out = KEY_NULL;
72            int in_esc = 0;
73            int in_ascii = 0;
74            int in_control = 0;
75            int i = 0;
76    
77          if (pos >= len)          if (clear_buf)
78          {          {
79                  pos = 0;                  pos = 0;
80                  len = 0;                  len = 0;
81    
82                    return 0;
83            }
84    
85            while (!SYS_server_exit && pos >= len)
86            {
87                  FD_ZERO(&testfds);                  FD_ZERO(&testfds);
88                  FD_SET(0, &testfds);                  FD_SET(STDIN_FILENO, &testfds);
89    
90                  timeout.tv_sec = 1;                  timeout.tv_sec = 0;
91                  timeout.tv_usec = 0;                  timeout.tv_usec = 100 * 1000; // 0.1 second
92    
93                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,                  ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
                                                                   (fd_set *)NULL, &timeout);  
94    
95                  if (result == 0)                  if (ret < 0)
96                  {                  {
97                          return KEY_TIMEOUT;                          if (errno != EINTR)
98                            {
99                                    log_error("Select error in igetch: !\n", errno);
100                                    return KEY_NULL;
101                            }
102                            continue;
103                  }                  }
104                  if (result < 0)                  else if (ret == 0)
105                  {                  {
106                          log_error("select() error (%d) !\n", result);                          return KEY_TIMEOUT;
                         return KEY_NULL;  
107                  }                  }
108                  if (result > 0)  
109                    if (FD_ISSET(STDIN_FILENO, &testfds))
110                  {                  {
111                          if (FD_ISSET(0, &testfds))                          len = read(STDIN_FILENO, buf, sizeof(buf));
112                          {                          pos = 0;
113                                  len = read(0, buf, 255);                          break;
                         }  
114                  }                  }
115    
116                  // For debug                  // For debug
# Line 106  int igetch() Line 120  int igetch()
120    
121          while (pos < len)          while (pos < len)
122          {          {
123                  c = buf[pos++];                  unsigned char c = buf[pos++];
124    
125                  if (c == '\0')                  if (c == '\0')
126                  {                  {
127                          out = c;                          return KEY_NULL;
                         break;  
128                  }                  }
129    
130                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
# Line 226  int igetch_t(long int sec) Line 239  int igetch_t(long int sec)
239    
240          do          do
241          {          {
242                  ch = igetch();                  ch = igetch(0);
243          } while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));
244    
245          return ch;          return ch;
246  }  }
   
 int ikbhit()  
 {  
         int len;  
   
         ioctl(0, FIONREAD, &len);  
   
         return len;  
 }  


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

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