/[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.3 by sysadm, Tue Nov 30 07:10:32 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 = fprintf(stdout, "%c", c);          retval = fprintf(stdout, "%c", c);
33    
34    return retval;          return retval;
35  }  }
36    
37  int  int prints(const char *format, ...)
38  prints(const char * format, ...)  {
39  {          va_list args;
40    va_list args;          int retval;
41    int retval;  
42            va_start(args, format);
43    va_start (args, format);          retval = vfprintf(stdout, format, args);
44    retval = vfprintf (stdout, format, args);          va_end(args);
45    va_end (args);  
46            return retval;
47    return retval;  }
48  }  
49    int iflush()
50  int  {
51  iflush()          int retval;
52  {  
53    int retval;          retval = fflush(stdout);
     
   retval = fflush (stdout);  
     
   return retval;  
 }  
   
 int  
 igetch ()  
 {  
   static char buf[256];  
   unsigned char c, tmp[256];  
   int out = 0, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i = 0, j;  
   static int len = 0 , pos = 0;  
   
   if (pos >= len)  
   {  
     len = s_receive (socket_client, buf, 255, "");  
     pos = 0;  
   
     //For debug  
     //for (j = 0; j < len; j++)  
     //  log_std ("<--[%u]\n", (buf[j] + 256) % 256);  
   }  
   
   while (pos < len)  
     {  
       c = buf[pos++];  
   
       if (c == '\0')  
       {  
         out = c;  
         break;  
       }  
   
       if (c == KEY_CONTROL)  
         {  
           if (in_control == 0)  
             {  
               in_control = 1;  
               i = 0;  
               continue;  
             }  
         }  
         
       if (in_control)  
       {  
         tmp[i++] = c;  
         if (i >= 2)  
         {  
           out = (int)tmp[0] * 256 + tmp[1];  
           in_control = 0;  
           break;  
         }  
         continue;  
       }  
54    
55        if (c == ESC_KEY)          return retval;
56    }
57    
58    int igetch(int clear_buf)
59    {
60            // static input buffer
61            static unsigned char buf[256];
62            static ssize_t len = 0;
63            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            if (in_esc == 0)                  pos = 0;
75              {                  len = 0;
76                in_esc = 1;  
77                in_ascii = 1;                  return 0;
               i = 0;  
               continue;  
             }  
           else  
             {  
               out = ESC_KEY;  
               in_esc = 0;  
               break;  
             }  
78          }          }
79    
80        in_esc = 0;          // 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                    timeout.tv_sec = 1;
96                    timeout.tv_usec = 0;
97    
98                    int result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,
99                                                                      (fd_set *)NULL, &timeout);
100    
101                    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 (in_ascii)          while (pos < len)
124          {          {
125            tmp[i++] = c;                  unsigned char c = buf[pos++];
126            if (c == 'm')  
127              {                  if (c == '\0')
128                in_ascii = 0;                  {
129                continue;                          out = c;
130              }                          break;
131            if (i == 2 && c >= 'A' && c <= 'D')                  }
132              {  
133                in_ascii = 0;                  if (c == KEY_CONTROL)
134                switch (c)                  {
135                  {                          if (in_control == 0)
136                  case 'A':                          {
137                    out = KEY_UP;                                  in_control = 1;
138                    break;                                  i = 0;
139                  case 'B':                                  continue;
140                    out = KEY_DOWN;                          }
141                    break;                  }
142                  case 'C':  
143                    out = KEY_RIGHT;                  if (in_control)
144                    break;                  {
145                  case 'D':                          tmp[i++] = c;
146                    out = KEY_LEFT;                          if (i >= 2)
147                    break;                          {
148                  }                                  out = (int)tmp[0] * 256 + tmp[1];
149                break;                                  in_control = 0;
150              }                                  break;
151            if (i == 3 && tmp[0] == 91 && tmp[2] == 126)                          }
152              {                          continue;
153                in_ascii = 0;                  }
154                switch (tmp[1])  
155                  {                  if (c == ESC_KEY)
156                  case 49:                  {
157                    out = KEY_HOME;                          if (in_esc == 0)
158                    break;                          {
159                  case 52:                                  in_esc = 1;
160                    out = KEY_END;                                  in_ascii = 1;
161                    break;                                  i = 0;
162                  case 53:                                  continue;
163                    out = KEY_PGUP;                          }
164                    break;                          else
165                  case 54:                          {
166                    out = KEY_PGDOWN;                                  out = ESC_KEY;
167                    break;                                  in_esc = 0;
168                  }                                  break;
169                break;                          }
170              }                  }
171            continue;  
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        out = ((int)c + 256) % 256;          // for debug
233        break;          // 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            do
244            {
245                    ch = igetch(0);
246            } while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec));
247    
248            return ch;
249    }
250    
251    int ikbhit()
252    {
253            int len;
254    
255    //for debug          ioctl(0, FIONREAD, &len);
   //log_std ("-->[%u]\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