/[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.5 by sysadm, Thu Mar 17 10:48:46 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, ...)
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)  
   {  
     pos = 0;  
   
     //len = s_receive (socket_client, buf, 255, "");  
     len = read (0, buf, 255);  
   
     //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 (in_ascii)          if (pos >= len)
85          {          {
86            tmp[i++] = c;                  fd_set testfds;
87            if (c == 'm')                  struct timeval timeout;
88              {  
89                in_ascii = 0;                  pos = 0;
90                continue;                  len = 0;
91              }  
92            if (i == 2 && c >= 'A' && c <= 'D')                  FD_ZERO(&testfds);
93              {                  FD_SET(0, &testfds);
94                in_ascii = 0;  
95                switch (c)                  timeout.tv_sec = 1;
96                  {                  timeout.tv_usec = 0;
97                  case 'A':  
98                    out = KEY_UP;                  int result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,
99                    break;                                                                    (fd_set *)NULL, &timeout);
100                  case 'B':  
101                    out = KEY_DOWN;                  if (result == 0)
102                    break;                  {
103                  case 'C':                          return KEY_TIMEOUT;
104                    out = KEY_RIGHT;                  }
105                    break;                  if (result < 0)
106                  case 'D':                  {
107                    out = KEY_LEFT;                          log_error("select() error (%d) !\n", result);
108                    break;                          return KEY_NULL;
109                  }                  }
110                break;                  if (result > 0)
111              }                  {
112            if (i == 3 && tmp[0] == 91 && tmp[2] == 126)                          if (FD_ISSET(0, &testfds))
113              {                          {
114                in_ascii = 0;                                  len = read(0, buf, 255);
115                switch (tmp[1])                          }
116                  {                  }
117                  case 49:  
118                    out = KEY_HOME;                  // For debug
119                    break;                  // for (j = 0; j < len; j++)
120                  case 51:                  //   log_std ("<--[%u]\n", (buf[j] + 256) % 256);
                   out = KEY_DEL;  
                   break;  
                 case 52:  
                   out = KEY_END;  
                   break;  
                 case 53:  
                   out = KEY_PGUP;  
                   break;  
                 case 54:  
                   out = KEY_PGDOWN;  
                   break;  
                 }  
               break;  
             }  
           continue;  
121          }          }
122    
123        out = ((int)c + 256) % 256;          while (pos < len)
124        break;          {
125      }                  unsigned char c = buf[pos++];
126    
127    //for debug                  if (c == '\0')
128    //log_std ("-->[%u]\n", out);                  {
129                            out = c;
130                            break;
131                    }
132    
133                    if (c == KEY_CONTROL)
134                    {
135                            if (in_control == 0)
136                            {
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    return out;                  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            // 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            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  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