/[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.17 by sysadm, Fri May 2 03:34:58 2025 UTC Revision 1.26 by sysadm, Sun May 11 04:09:08 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   ***************************************************************************/   ***************************************************************************/
# Line 18  Line 17 
17  #include "io.h"  #include "io.h"
18  #include "log.h"  #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 58  int iflush() Line 58  int iflush()
58    
59  int igetch(int clear_buf)  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            unsigned char tmp[LINE_BUFFER_LEN];
70            int ret;
71            int out = KEY_NULL;
72            int in_esc = 0;
73            int in_ascii = 0;
74            int in_control = 0;
75            int i = 0;
76            int flags;
77    
78          if (clear_buf)          if (clear_buf)
79          {          {
80                  pos = 0;                  pos = 0;
# Line 73  int igetch(int clear_buf) Line 83  int igetch(int clear_buf)
83                  return 0;                  return 0;
84          }          }
85    
86          // Stop on system exit          while (!SYS_server_exit && pos >= len)
         if (SYS_exit)  
                 return KEY_NULL;  
   
         if (pos >= len)  
87          {          {
                 pos = 0;  
                 len = 0;  
   
88                  FD_ZERO(&testfds);                  FD_ZERO(&testfds);
89                  FD_SET(0, &testfds);                  FD_SET(STDIN_FILENO, &testfds);
90    
91                  timeout.tv_sec = 1;                  timeout.tv_sec = 0;
92                  timeout.tv_usec = 0;                  timeout.tv_usec = 100 * 1000; // 0.1 second
93    
94                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,                  ret = select(STDIN_FILENO + 1, &testfds, NULL, NULL, &timeout);
                                                                   (fd_set *)NULL, &timeout);  
95    
96                  if (result == 0)                  if (ret < 0)
97                  {                  {
98                          return KEY_TIMEOUT;                          if (errno != EINTR)
99                            {
100                                    log_error("Select error in igetch: !\n", errno);
101                                    return KEY_NULL;
102                            }
103                            continue;
104                  }                  }
105                  if (result < 0)                  else if (ret == 0)
106                  {                  {
107                          log_error("select() error (%d) !\n", result);                          return KEY_TIMEOUT;
                         return KEY_NULL;  
108                  }                  }
109                  if (result > 0)  
110                    if (FD_ISSET(STDIN_FILENO, &testfds))
111                  {                  {
112                          if (FD_ISSET(0, &testfds))                          flags = fcntl(STDIN_FILENO, F_GETFL, 0);
113                            fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);
114    
115                            len = read(STDIN_FILENO, buf, sizeof(buf));
116                            if (len < 0)
117                          {                          {
118                                  len = read(0, buf, 255);                                  if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
119                                    {
120                                            log_error("Read socket error (%d)\n", errno);
121                                    }
122                          }                          }
123    
124                            pos = 0;
125    
126                            fcntl(STDIN_FILENO, F_SETFL, flags);
127    
128                            break;
129                  }                  }
130    
131                  // For debug                  // For debug
# Line 115  int igetch(int clear_buf) Line 135  int igetch(int clear_buf)
135    
136          while (pos < len)          while (pos < len)
137          {          {
138                  c = buf[pos++];                  unsigned char c = buf[pos++];
139    
140                  if (c == '\0')                  if (c == '\0')
141                  {                  {
142                          out = c;                          return KEY_NULL;
                         break;  
143                  }                  }
144    
145                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
# Line 236  int igetch_t(long int sec) Line 255  int igetch_t(long int sec)
255          do          do
256          {          {
257                  ch = igetch(0);                  ch = igetch(0);
258          } while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));
259    
260          return ch;          return ch;
261  }  }
   
 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