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


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

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