/[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.9 by sysadm, Tue Mar 22 08:19:11 2005 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   ***************************************************************************/   ***************************************************************************/
16    
17  #include "io.h"  #include "io.h"
18    #include "log.h"
19  #include "common.h"  #include "common.h"
20    #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  int outc(char c)
 outc (char c)  
30  {  {
31    int retval;          int retval;
32    
33    retval = fprintf (stdout, "%c", c);          retval = fprintf(stdout, "%c", c);
34    
35    return retval;          return retval;
36  }  }
37    
38  int  int prints(const char *format, ...)
 prints (const char *format, ...)  
39  {  {
40    va_list args;          va_list args;
41    int retval;          int retval;
42    
43    va_start (args, format);          va_start(args, format);
44    retval = vfprintf (stdout, format, args);          retval = vfprintf(stdout, format, args);
45    va_end (args);          va_end(args);
46    
47    return retval;          return retval;
48  }  }
49    
50  int  int iflush()
 iflush ()  
51  {  {
52    int retval;          int retval;
53    
54    retval = fflush (stdout);          retval = fflush(stdout);
55    
56    return retval;          return retval;
57  }  }
58    
59  int  int igetch(int clear_buf)
 igetch ()  
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 =          static ssize_t len = 0;
64      0, j, result;          static int pos = 0;
65    static int len = 0, pos = 0;  
66    fd_set inputs, testfds;          fd_set testfds;
67    struct timeval timeout;          struct timeval timeout;
68    
69    if (pos >= len)          unsigned char tmp[LINE_BUFFER_LEN];
70      {          int ret;
71        pos = 0;          int out = KEY_NULL;
72        len = 0;          int in_esc = 0;
73            int in_ascii = 0;
74        FD_ZERO (&inputs);          int in_control = 0;
75        FD_SET (0, &inputs);          int i = 0;
76            int flags;
       testfds = inputs;  
       timeout.tv_sec = 1;  
       timeout.tv_usec = 0;  
77    
78        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,          if (clear_buf)
                        (fd_set *) NULL, &timeout);  
   
       switch (result)  
79          {          {
80          case 0:                  pos = 0;
81            out = KEY_TIMEOUT;                  len = 0;
           break;  
         case -1:  
           log_error ("select() error!\n");  
           break;  
         default:  
           if (FD_ISSET (0, &testfds))  
             {  
               len = read (0, buf, 255);  
             }  
           break;  
         }  
   
       //For debug  
       //for (j = 0; j < len; j++)  
       //  log_std ("<--[%u]\n", (buf[j] + 256) % 256);  
     }  
   
   while (pos < len)  
     {  
       c = buf[pos++];  
82    
83        if (c == '\0')                  return 0;
         {  
           out = c;  
           break;  
84          }          }
85    
86        if (c == KEY_CONTROL)          while (!SYS_server_exit && pos >= len)
87          {          {
88            if (in_control == 0)                  FD_ZERO(&testfds);
89              {                  FD_SET(STDIN_FILENO, &testfds);
               in_control = 1;  
               i = 0;  
               continue;  
             }  
         }  
90    
91        if (in_control)                  timeout.tv_sec = 0;
92          {                  timeout.tv_usec = 100 * 1000; // 0.1 second
93            tmp[i++] = c;  
94            if (i >= 2)                  ret = select(STDIN_FILENO + 1, &testfds, NULL, NULL, &timeout);
95              {  
96                out = (int) tmp[0] * 256 + tmp[1];                  if (ret < 0)
97                in_control = 0;                  {
98                break;                          if (errno != EINTR)
99              }                          {
100            continue;                                  log_error("Select error in igetch: !\n", errno);
101                                    return KEY_NULL;
102                            }
103                            continue;
104                    }
105                    else if (ret == 0)
106                    {
107                            return KEY_TIMEOUT;
108                    }
109    
110                    if (FD_ISSET(STDIN_FILENO, &testfds))
111                    {
112                            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                                    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
132                    // for (j = 0; j < len; j++)
133                    //   log_std ("<--[%u]\n", (buf[j] + 256) % 256);
134          }          }
135    
136        if (c == ESC_KEY)          while (pos < len)
137          {          {
138            if (in_esc == 0)                  unsigned char c = buf[pos++];
             {  
               in_esc = 1;  
               in_ascii = 1;  
               i = 0;  
               continue;  
             }  
           else  
             {  
               out = ESC_KEY;  
               in_esc = 0;  
               break;  
             }  
         }  
139    
140        in_esc = 0;                  if (c == '\0')
141                    {
142                            return KEY_NULL;
143                    }
144    
145        if (in_ascii)                  if (c == KEY_CONTROL)
146          {                  {
147            tmp[i++] = c;                          if (in_control == 0)
148            if (c == 'm')                          {
149              {                                  in_control = 1;
150                in_ascii = 0;                                  i = 0;
151                continue;                                  continue;
152              }                          }
153            if (i == 2 && c >= 'A' && c <= 'D')                  }
             {  
               in_ascii = 0;  
               switch (c)  
                 {  
                 case 'A':  
                   out = KEY_UP;  
                   break;  
                 case 'B':  
                   out = KEY_DOWN;  
                   break;  
                 case 'C':  
                   out = KEY_RIGHT;  
                   break;  
                 case 'D':  
                   out = KEY_LEFT;  
                   break;  
                 }  
               break;  
             }  
           if (i == 3 && tmp[0] == 91 && tmp[2] == 126)  
             {  
               in_ascii = 0;  
               switch (tmp[1])  
                 {  
                 case 49:  
                   out = KEY_HOME;  
                   break;  
                 case 51:  
                   out = KEY_DEL;  
                   break;  
                 case 52:  
                   out = KEY_END;  
                   break;  
                 case 53:  
                   out = KEY_PGUP;  
                   break;  
                 case 54:  
                   out = KEY_PGDN;  
                   break;  
                 }  
               break;  
             }  
           continue;  
         }  
154    
155        out = ((int) c + 256) % 256;                  if (in_control)
156        break;                  {
157      }                          tmp[i++] = c;
158                            if (i >= 2)
159                            {
160                                    out = (int)tmp[0] * 256 + tmp[1];
161                                    in_control = 0;
162                                    break;
163                            }
164                            continue;
165                    }
166    
167    //for debug                  if (c == ESC_KEY)
168    //log_std ("-->[%u]\n", out);                  {
169                            if (in_esc == 0)
170                            {
171                                    in_esc = 1;
172                                    in_ascii = 1;
173                                    i = 0;
174                                    continue;
175                            }
176                            else
177                            {
178                                    out = ESC_KEY;
179                                    in_esc = 0;
180                                    break;
181                            }
182                    }
183    
184    return out;                  in_esc = 0;
 }  
185    
186  int                  if (in_ascii)
187  igetch_t (long int sec)                  {
188  {                          tmp[i++] = c;
189    int ch;                          if (c == 'm')
190    time_t t_begin = time(0);                          {
191                                      in_ascii = 0;
192    do {                                  continue;
193      ch = igetch ();                          }
194    } while ((ch == KEY_TIMEOUT) && (time(0) - t_begin < sec));                          if (i == 2 && c >= 'A' && c <= 'D')
195                            {
196                                    in_ascii = 0;
197                                    switch (c)
198                                    {
199                                    case 'A':
200                                            out = KEY_UP;
201                                            break;
202                                    case 'B':
203                                            out = KEY_DOWN;
204                                            break;
205                                    case 'C':
206                                            out = KEY_RIGHT;
207                                            break;
208                                    case 'D':
209                                            out = KEY_LEFT;
210                                            break;
211                                    }
212                                    break;
213                            }
214                            if (i == 3 && tmp[0] == 91 && tmp[2] == 126)
215                            {
216                                    in_ascii = 0;
217                                    switch (tmp[1])
218                                    {
219                                    case 49:
220                                            out = KEY_HOME;
221                                            break;
222                                    case 51:
223                                            out = KEY_DEL;
224                                            break;
225                                    case 52:
226                                            out = KEY_END;
227                                            break;
228                                    case 53:
229                                            out = KEY_PGUP;
230                                            break;
231                                    case 54:
232                                            out = KEY_PGDN;
233                                            break;
234                                    }
235                                    break;
236                            }
237                            continue;
238                    }
239    
240    return ch;                  out = ((int)c + 256) % 256;
241                    break;
242            }
243    
244            // for debug
245            // log_std ("-->[%u]\n", out);
246    
247            return out;
248  }  }
249    
250  int  int igetch_t(long int sec)
 ikbhit ()  
251  {  {
252    int len;          int ch;
253            time_t t_begin = time(0);
254    
255    ioctl (0, FIONREAD, &len);          do
256            {
257                    ch = igetch(0);
258            } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));
259    
260    return len;          return ch;
261  }  }


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

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