/[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.1 by sysadm, Fri Oct 22 15:21:28 2004 UTC Revision 1.27 by sysadm, Sun May 11 11:33:44 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 <errno.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/select.h>
27    #include <sys/ioctl.h>
28    
29  int  int outc(char c)
 outc(char c)  
30  {  {
31    int retval;          int retval;
32    
33    retval = printf("%c",c);          retval = fprintf(stdout, "%c", c);
34    
35    return retval;          return retval;
36  }  }
37    
38  int  int prints(const char *format, ...)
 prints(const char * format, ...)  
39  {  {
40    va_list args;          va_list args;
41    int retval;          int retval;
42    
43    va_start (args, format);          va_start(args, format);
44    retval = vfprintf (stdout, format, args);          retval = vfprintf(stdout, format, args);
45    va_end (args);          va_end(args);
46    
47    return retval;          return retval;
48  }  }
49    
50  int  int iflush()
 iflush()  
51  {  {
52    int retval;          int retval;
     
   retval = fflush (stdout);  
     
   return retval;  
 }  
53    
54  int          retval = fflush(stdout);
55  Ctrl (char c)  
56  {          return retval;
   return (c - 'A' + 1);  
57  }  }
58    
59  int  int igetch(int clear_buf)
 igetch ()  
60  {  {
61    char c, tmp[256];          // static input buffer
62    static char buf[256];          static unsigned char buf[LINE_BUFFER_LEN];
63    int out = 0, loop = 1, in_esc = 0, in_ascii = 0, i = 0;          static ssize_t len = 0;
64    static int len =0 , pos = 0;          static int pos = 0;
65    
66    if (pos >= len)          fd_set testfds;
67    {          struct timeval timeout;
68      len = s_receive (socket_client, buf, 255, "");  
69      pos = 0;          unsigned char tmp[LINE_BUFFER_LEN];
70    }          int ret;
71            int out = '\0';
72    while (pos < len)          int in_esc = 0;
73      {          int in_ascii = 0;
74        c = buf[pos++];          int in_control = 0;
75            int i = 0;
76        if (c == '\0')          int flags;
       {  
         out = c;  
         break;  
       }  
77    
78        if (c == ESC_KEY)          if (clear_buf)
79          {          {
80            if (in_esc == 0)                  pos = 0;
81              {                  len = 0;
82                in_esc = 1;  
83                in_ascii = 1;                  return '\0';
               i = 0;  
               continue;  
             }  
           else  
             {  
               out = ESC_KEY;  
               in_esc = 0;  
               break;  
             }  
84          }          }
85    
86        in_esc = 0;          while (!SYS_server_exit && pos >= len)
87            {
88                    FD_ZERO(&testfds);
89                    FD_SET(STDIN_FILENO, &testfds);
90    
91                    timeout.tv_sec = 0;
92                    timeout.tv_usec = 100 * 1000; // 0.1 second
93    
94                    ret = select(STDIN_FILENO + 1, &testfds, NULL, NULL, &timeout);
95    
96                    if (ret < 0)
97                    {
98                            if (errno != EINTR)
99                            {
100                                    log_error("select() error (%d) !\n", errno);
101                                    return KEY_NULL;
102                            }
103                            continue;
104                    }
105                    else if (ret == 0)
106                    {
107                            return KEY_TIMEOUT;
108                    }
109    
110                    if (FD_ISSET(STDIN_FILENO, &testfds))
111                    {
112                            flags = fcntl(STDIN_FILENO, F_GETFL, 0);
113                            fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);
114    
115                            len = read(STDIN_FILENO, buf, sizeof(buf));
116                            if (len < 0)
117                            {
118                                    if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
119                                    {
120                                            log_error("read(STDIN) error (%d)\n", errno);
121                                    }
122                            }
123                            else if (len == 0)
124                            {
125                                    out = KEY_NULL; // broken pipe
126                            }
127    
128                            pos = 0;
129    
130                            fcntl(STDIN_FILENO, F_SETFL, flags);
131    
132                            break;
133                    }
134    
135                    // For debug
136                    // for (j = 0; j < len; j++)
137                    //   log_std ("<--[%u]\n", (buf[j] + 256) % 256);
138            }
139    
140        if (in_ascii)          while (pos < len)
141          {          {
142            tmp[i++] = c;                  unsigned char c = buf[pos++];
143            if (c == 'm')  
144              {                  if (c == KEY_CONTROL)
145                in_ascii = 0;                  {
146                continue;                          if (in_control == 0)
147              }                          {
148            if (i == 2 && c >= 'A' && c <= 'D')                                  in_control = 1;
149              {                                  i = 0;
150                in_ascii = 0;                                  continue;
151                switch (c)                          }
152                  {                  }
153                  case 'A':  
154                    out = KEY_UP;                  if (in_control)
155                    break;                  {
156                  case 'B':                          tmp[i++] = c;
157                    out = KEY_DOWN;                          if (i >= 2)
158                    break;                          {
159                  case 'C':                                  out = (int)tmp[0] * 256 + tmp[1];
160                    out = KEY_LEFT;                                  in_control = 0;
161                    break;                                  break;
162                  case 'D':                          }
163                    out = KEY_RIGHT;                          continue;
164                    break;                  }
165                  }  
166                break;                  if (c == ESC_KEY)
167              }                  {
168            continue;                          if (in_esc == 0)
169                            {
170                                    in_esc = 1;
171                                    in_ascii = 1;
172                                    i = 0;
173                                    continue;
174                            }
175                            else
176                            {
177                                    out = ESC_KEY;
178                                    in_esc = 0;
179                                    break;
180                            }
181                    }
182    
183                    in_esc = 0;
184    
185                    if (in_ascii)
186                    {
187                            tmp[i++] = c;
188                            if (c == 'm')
189                            {
190                                    in_ascii = 0;
191                                    continue;
192                            }
193                            if (i == 2 && c >= 'A' && c <= 'D')
194                            {
195                                    in_ascii = 0;
196                                    switch (c)
197                                    {
198                                    case 'A':
199                                            out = KEY_UP;
200                                            break;
201                                    case 'B':
202                                            out = KEY_DOWN;
203                                            break;
204                                    case 'C':
205                                            out = KEY_RIGHT;
206                                            break;
207                                    case 'D':
208                                            out = KEY_LEFT;
209                                            break;
210                                    }
211                                    break;
212                            }
213                            if (i == 3 && tmp[0] == 91 && tmp[2] == 126)
214                            {
215                                    in_ascii = 0;
216                                    switch (tmp[1])
217                                    {
218                                    case 49:
219                                            out = KEY_HOME;
220                                            break;
221                                    case 51:
222                                            out = KEY_DEL;
223                                            break;
224                                    case 52:
225                                            out = KEY_END;
226                                            break;
227                                    case 53:
228                                            out = KEY_PGUP;
229                                            break;
230                                    case 54:
231                                            out = KEY_PGDN;
232                                            break;
233                                    }
234                                    break;
235                            }
236                            continue;
237                    }
238    
239                    out = ((int)c + 256) % 256;
240                    break;
241          }          }
242    
243        out = ((int)c + 256) % 256;          // for debug
244        break;          // log_std ("-->[%u]\n", out);
     }  
245    
246    //for debug          return out;
247    //log_std ("[%d]\n", out);  }
248    
249    int igetch_t(long int sec)
250    {
251            int ch;
252            time_t t_begin = time(0);
253    
254            do
255            {
256                    ch = igetch(0);
257            } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));
258    
259    return out;          return ch;
260  }  }


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

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