/[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.8 by sysadm, Mon Mar 21 17:08:21 2005 UTC Revision 1.21 by sysadm, Fri May 9 11:21:11 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"
21  #include <stdio.h>  #include <stdio.h>
22  #include <stdarg.h>  #include <stdarg.h>
23    #include <time.h>
24    #include <fcntl.h>
25    #include <unistd.h>
26  #include <sys/ioctl.h>  #include <sys/ioctl.h>
27    
28  int  int outc(char c)
 outc (char c)  
29  {  {
30    int retval;          int retval;
31    
32    retval = fprintf (stdout, "%c", c);          retval = fprintf(stdout, "%c", c);
33    
34    return retval;          return retval;
35  }  }
36    
37  int  int prints(const char *format, ...)
 prints (const char *format, ...)  
38  {  {
39    va_list args;          va_list args;
40    int retval;          int retval;
41    
42    va_start (args, format);          va_start(args, format);
43    retval = vfprintf (stdout, format, args);          retval = vfprintf(stdout, format, args);
44    va_end (args);          va_end(args);
45    
46    return retval;          return retval;
47  }  }
48    
49  int  int iflush()
 iflush ()  
50  {  {
51    int retval;          int retval;
52    
53    retval = fflush (stdout);          retval = fflush(stdout);
54    
55    return retval;          return retval;
56  }  }
57    
58  int  int igetch(int clear_buf)
 igetch ()  
59  {  {
60    static char buf[256];          // static input buffer
61    unsigned char c, tmp[256];          static unsigned char buf[256];
62    int out = KEY_NULL, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i =          static ssize_t len = 0;
63      0, j;          static int pos = 0;
64    static int len = 0, pos = 0;  
65            fd_set testfds;
66    if (pos >= len)          struct timeval timeout;
67      {  
68        pos = 0;          unsigned char tmp[256];
69            int ret;
70        //len = s_receive (socket_client, buf, 255, "");          int out = KEY_NULL;
71        len = read (0, buf, 255);          int in_esc = 0;
72            int in_ascii = 0;
73        //For debug          int in_control = 0;
74        //for (j = 0; j < len; j++)          int i = 0;
       //  log_std ("<--[%u]\n", (buf[j] + 256) % 256);  
     }  
   
   while (pos < len)  
     {  
       c = buf[pos++];  
75    
76        if (c == '\0')          if (clear_buf)
77          {          {
78            out = c;                  pos = 0;
79            break;                  len = 0;
         }  
80    
81        if (c == KEY_CONTROL)                  return 0;
         {  
           if (in_control == 0)  
             {  
               in_control = 1;  
               i = 0;  
               continue;  
             }  
82          }          }
83    
84        if (in_control)          while (!SYS_server_exit && pos >= len)
85          {          {
86            tmp[i++] = c;                  FD_ZERO(&testfds);
87            if (i >= 2)                  FD_SET(STDIN_FILENO, &testfds);
88              {  
89                out = (int) tmp[0] * 256 + tmp[1];                  timeout.tv_sec = 1;
90                in_control = 0;                  timeout.tv_usec = 0;
91                break;  
92              }                  ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
93            continue;  
94                    if (ret < 0)
95                    {
96                            if (errno != EINTR)
97                            {
98                                    log_error("Select error in igetch: !\n", errno);
99                                    return KEY_NULL;
100                            }
101                            continue;
102                    }
103                    else if (ret == 0)
104                    {
105                            return KEY_TIMEOUT;
106                    }
107    
108                    if (FD_ISSET(STDIN_FILENO, &testfds))
109                    {
110                            len = read(STDIN_FILENO, buf, 255);
111                            pos = 0;
112                            break;
113                    }
114    
115                    // For debug
116                    // for (j = 0; j < len; j++)
117                    //   log_std ("<--[%u]\n", (buf[j] + 256) % 256);
118          }          }
119    
120        if (c == ESC_KEY)          while (pos < len)
121          {          {
122            if (in_esc == 0)                  unsigned char c = buf[pos++];
123              {  
124                in_esc = 1;                  if (c == '\0')
125                in_ascii = 1;                  {
126                i = 0;                          out = c;
127                continue;                          break;
128              }                  }
           else  
             {  
               out = ESC_KEY;  
               in_esc = 0;  
               break;  
             }  
         }  
129    
130        in_esc = 0;                  if (c == KEY_CONTROL)
131                    {
132                            if (in_control == 0)
133                            {
134                                    in_control = 1;
135                                    i = 0;
136                                    continue;
137                            }
138                    }
139    
140        if (in_ascii)                  if (in_control)
141          {                  {
142            tmp[i++] = c;                          tmp[i++] = c;
143            if (c == 'm')                          if (i >= 2)
144              {                          {
145                in_ascii = 0;                                  out = (int)tmp[0] * 256 + tmp[1];
146                continue;                                  in_control = 0;
147              }                                  break;
148            if (i == 2 && c >= 'A' && c <= 'D')                          }
149              {                          continue;
150                in_ascii = 0;                  }
151                switch (c)  
152                  {                  if (c == ESC_KEY)
153                  case 'A':                  {
154                    out = KEY_UP;                          if (in_esc == 0)
155                    break;                          {
156                  case 'B':                                  in_esc = 1;
157                    out = KEY_DOWN;                                  in_ascii = 1;
158                    break;                                  i = 0;
159                  case 'C':                                  continue;
160                    out = KEY_RIGHT;                          }
161                    break;                          else
162                  case 'D':                          {
163                    out = KEY_LEFT;                                  out = ESC_KEY;
164                    break;                                  in_esc = 0;
165                  }                                  break;
166                break;                          }
167              }                  }
168            if (i == 3 && tmp[0] == 91 && tmp[2] == 126)  
169              {                  in_esc = 0;
170                in_ascii = 0;  
171                switch (tmp[1])                  if (in_ascii)
172                  {                  {
173                  case 49:                          tmp[i++] = c;
174                    out = KEY_HOME;                          if (c == 'm')
175                    break;                          {
176                  case 51:                                  in_ascii = 0;
177                    out = KEY_DEL;                                  continue;
178                    break;                          }
179                  case 52:                          if (i == 2 && c >= 'A' && c <= 'D')
180                    out = KEY_END;                          {
181                    break;                                  in_ascii = 0;
182                  case 53:                                  switch (c)
183                    out = KEY_PGUP;                                  {
184                    break;                                  case 'A':
185                  case 54:                                          out = KEY_UP;
186                    out = KEY_PGDN;                                          break;
187                    break;                                  case 'B':
188                  }                                          out = KEY_DOWN;
189                break;                                          break;
190              }                                  case 'C':
191            continue;                                          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;                  out = ((int)c + 256) % 256;
226        break;                  break;
227      }          }
228    
229    //for debug          // for debug
230    //log_std ("-->[%u]\n", out);          // log_std ("-->[%u]\n", out);
231    
232    return out;          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 || ch == 0xa) && (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