/[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.13 by sysadm, Mon Mar 15 07:30:21 2010 UTC Revision 1.24 by sysadm, Sat May 10 13:17:36 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  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;
   static int len = 0, pos = 0;  
   fd_set testfds;  
   struct timeval timeout;  
   
   //Stop on system exit  
   if (SYS_exit)  
     return KEY_NULL;  
   
   if (pos >= len)  
     {  
       pos = 0;  
       len = 0;  
   
       FD_ZERO (&testfds);  
       FD_SET (0, &testfds);  
65    
66        timeout.tv_sec = 1;          fd_set testfds;
67        timeout.tv_usec = 0;          struct timeval timeout;
68    
69        result = SignalSafeSelect (FD_SETSIZE, &testfds, (fd_set *) NULL,          unsigned char tmp[LINE_BUFFER_LEN];
70                         (fd_set *) NULL, &timeout);          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    
77        if (result == 0)          if (clear_buf)
78          {          {
79            return KEY_TIMEOUT;                  pos = 0;
80          }                  len = 0;
81        if (result < 0)  
82          {                  return 0;
           log_error ("select() error (%d) !\n", result);  
           return KEY_NULL;  
83          }          }
84        if (result > 0)  
85            while (!SYS_server_exit && pos >= len)
86          {          {
87            if (FD_ISSET (0, &testfds))                  FD_ZERO(&testfds);
88              {                  FD_SET(STDIN_FILENO, &testfds);
               len = read (0, buf, 255);  
             }  
         }  
89    
90        //For debug                  timeout.tv_sec = 0;
91        //for (j = 0; j < len; j++)                  timeout.tv_usec = 100 * 1000; // 0.1 second
       //  log_std ("<--[%u]\n", (buf[j] + 256) % 256);  
     }  
   
   while (pos < len)  
     {  
       c = buf[pos++];  
92    
93        if (c == '\0')                  ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
         {  
           out = c;  
           break;  
         }  
94    
95        if (c == KEY_CONTROL)                  if (ret < 0)
96          {                  {
97            if (in_control == 0)                          if (errno != EINTR)
98              {                          {
99                in_control = 1;                                  log_error("Select error in igetch: !\n", errno);
100                i = 0;                                  return KEY_NULL;
101                continue;                          }
102              }                          continue;
103          }                  }
104                    else if (ret == 0)
105                    {
106                            return KEY_TIMEOUT;
107                    }
108    
109        if (in_control)                  if (FD_ISSET(STDIN_FILENO, &testfds))
110          {                  {
111            tmp[i++] = c;                          len = read(STDIN_FILENO, buf, sizeof(buf));
112            if (i >= 2)                          pos = 0;
113              {                          break;
114                out = (int) tmp[0] * 256 + tmp[1];                  }
115                in_control = 0;  
116                break;                  // For debug
117              }                  // for (j = 0; j < len; j++)
118            continue;                  //   log_std ("<--[%u]\n", (buf[j] + 256) % 256);
119          }          }
120    
121        if (c == ESC_KEY)          while (pos < len)
122          {          {
123            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;  
             }  
         }  
124    
125        in_esc = 0;                  if (c == '\0')
126                    {
127                            return KEY_NULL;
128                    }
129    
130        if (in_ascii)                  if (c == KEY_CONTROL)
131          {                  {
132            tmp[i++] = c;                          if (in_control == 0)
133            if (c == 'm')                          {
134              {                                  in_control = 1;
135                in_ascii = 0;                                  i = 0;
136                continue;                                  continue;
137              }                          }
138            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;  
         }  
139    
140        out = ((int) c + 256) % 256;                  if (in_control)
141        break;                  {
142      }                          tmp[i++] = c;
143                            if (i >= 2)
144                            {
145                                    out = (int)tmp[0] * 256 + tmp[1];
146                                    in_control = 0;
147                                    break;
148                            }
149                            continue;
150                    }
151    
152    //for debug                  if (c == ESC_KEY)
153    //log_std ("-->[%u]\n", out);                  {
154                            if (in_esc == 0)
155                            {
156                                    in_esc = 1;
157                                    in_ascii = 1;
158                                    i = 0;
159                                    continue;
160                            }
161                            else
162                            {
163                                    out = ESC_KEY;
164                                    in_esc = 0;
165                                    break;
166                            }
167                    }
168    
169    return out;                  in_esc = 0;
 }  
170    
171  int                  if (in_ascii)
172  igetch_t (long int sec)                  {
173  {                          tmp[i++] = c;
174    int ch;                          if (c == 'm')
175    time_t t_begin = time (0);                          {
176                                    in_ascii = 0;
177                                    continue;
178                            }
179                            if (i == 2 && c >= 'A' && c <= 'D')
180                            {
181                                    in_ascii = 0;
182                                    switch (c)
183                                    {
184                                    case 'A':
185                                            out = KEY_UP;
186                                            break;
187                                    case 'B':
188                                            out = KEY_DOWN;
189                                            break;
190                                    case 'C':
191                                            out = KEY_RIGHT;
192                                            break;
193                                    case 'D':
194                                            out = KEY_LEFT;
195                                            break;
196                                    }
197                                    break;
198                            }
199                            if (i == 3 && tmp[0] == 91 && tmp[2] == 126)
200                            {
201                                    in_ascii = 0;
202                                    switch (tmp[1])
203                                    {
204                                    case 49:
205                                            out = KEY_HOME;
206                                            break;
207                                    case 51:
208                                            out = KEY_DEL;
209                                            break;
210                                    case 52:
211                                            out = KEY_END;
212                                            break;
213                                    case 53:
214                                            out = KEY_PGUP;
215                                            break;
216                                    case 54:
217                                            out = KEY_PGDN;
218                                            break;
219                                    }
220                                    break;
221                            }
222                            continue;
223                    }
224    
225                    out = ((int)c + 256) % 256;
226                    break;
227            }
228    
229    do          // for debug
230      {          // log_std ("-->[%u]\n", out);
       ch = igetch ();  
     }  
   while ((ch == KEY_TIMEOUT || ch == 0xa) && (time (0) - t_begin < sec));  
231    
232    return ch;          return out;
233  }  }
234    
235  int  int igetch_t(long int sec)
 ikbhit ()  
236  {  {
237    int len;          int ch;
238            time_t t_begin = time(0);
239    
240    ioctl (0, FIONREAD, &len);          do
241            {
242                    ch = igetch(0);
243            } while (ch == KEY_TIMEOUT && (time(0) - t_begin < sec));
244    
245    return len;          return ch;
246  }  }


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

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