/[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.2 by sysadm, Sat Oct 23 18:41:41 2004 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>
27    
28  int  int outc(char c)
 outc(char c)  
29  {  {
30    int retval;          int retval;
31    
32    retval = printf("%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    char c, tmp[256];          // static input buffer
61    static char buf[256];          static unsigned char buf[256];
62    int out = 0, loop = 1, in_esc = 0, in_ascii = 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 (clear_buf)
73            {
74                    pos = 0;
75                    len = 0;
76    
77                    return 0;
78            }
79    
80            // Stop on system exit
81            if (SYS_exit)
82                    return KEY_NULL;
83    
84            if (pos >= len)
85            {
86                    fd_set testfds;
87                    struct timeval timeout;
88    
89                    pos = 0;
90                    len = 0;
91    
92                    FD_ZERO(&testfds);
93                    FD_SET(0, &testfds);
94    
95    if (pos >= len)                  timeout.tv_sec = 1;
96    {                  timeout.tv_usec = 0;
     len = s_receive (socket_client, buf, 255, "");  
     pos = 0;  
97    
98      //For debug                  int result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,
99      //for (j = 0; j < len; j++)                                                                    (fd_set *)NULL, &timeout);
     //  log_std ("[%d]\n", buf[j]);  
   }  
100    
101    while (pos < len)                  if (result == 0)
102      {                  {
103        c = buf[pos++];                          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        if (c == '\0')                  // For debug
119        {                  // for (j = 0; j < len; j++)
120          out = c;                  //   log_std ("<--[%u]\n", (buf[j] + 256) % 256);
121          break;          }
       }  
122    
123        if (c == ESC_KEY)          while (pos < len)
124          {          {
125            if (in_esc == 0)                  unsigned char c = buf[pos++];
126              {  
127                in_esc = 1;                  if (c == '\0')
128                in_ascii = 1;                  {
129                i = 0;                          out = c;
130                continue;                          break;
131              }                  }
132            else  
133              {                  if (c == KEY_CONTROL)
134                out = ESC_KEY;                  {
135                in_esc = 0;                          if (in_control == 0)
136                break;                          {
137              }                                  in_control = 1;
138                                    i = 0;
139                                    continue;
140                            }
141                    }
142    
143                    if (in_control)
144                    {
145                            tmp[i++] = c;
146                            if (i >= 2)
147                            {
148                                    out = (int)tmp[0] * 256 + tmp[1];
149                                    in_control = 0;
150                                    break;
151                            }
152                            continue;
153                    }
154    
155                    if (c == ESC_KEY)
156                    {
157                            if (in_esc == 0)
158                            {
159                                    in_esc = 1;
160                                    in_ascii = 1;
161                                    i = 0;
162                                    continue;
163                            }
164                            else
165                            {
166                                    out = ESC_KEY;
167                                    in_esc = 0;
168                                    break;
169                            }
170                    }
171    
172                    in_esc = 0;
173    
174                    if (in_ascii)
175                    {
176                            tmp[i++] = c;
177                            if (c == 'm')
178                            {
179                                    in_ascii = 0;
180                                    continue;
181                            }
182                            if (i == 2 && c >= 'A' && c <= 'D')
183                            {
184                                    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        in_esc = 0;          // for debug
233            // log_std ("-->[%u]\n", out);
234    
235            return out;
236    }
237    
238    int igetch_t(long int sec)
239    {
240            int ch;
241            time_t t_begin = time(0);
242    
243        if (in_ascii)          do
244          {          {
245            tmp[i++] = c;                  ch = igetch(0);
246            if (c == 'm')          } while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec));
             {  
               in_ascii = 0;  
               continue;  
             }  
           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 52:  
                   out = KEY_END;  
                   break;  
                 case 53:  
                   out = KEY_PGUP;  
                   break;  
                 case 54:  
                   out = KEY_PGDOWN;  
                   break;  
                 }  
               break;  
             }  
           continue;  
         }  
247    
248        out = ((int)c + 256) % 256;          return ch;
249        break;  }
250      }  
251    int ikbhit()
252    {
253            int len;
254    
255    //for debug          ioctl(0, FIONREAD, &len);
   //log_std ("[%d]\n", out);  
256    
257    return out;          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