/[LeafOK_CVS]/lbbs/src/io.c
ViewVC logotype

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations)
Mon May 5 14:27:57 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.18: +2 -2 lines
Content type: text/x-csrc
Refine for strict GCC error check

1 /***************************************************************************
2 io.c - description
3 -------------------
4 begin : Mon Oct 18 2004
5 copyright : (C) 2004 by Leaflet
6 email : leaflet@leafok.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "io.h"
19 #include "log.h"
20 #include "common.h"
21 #include "tcplib.h"
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <time.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28
29 int outc(char c)
30 {
31 int retval;
32
33 retval = fprintf(stdout, "%c", c);
34
35 return retval;
36 }
37
38 int prints(const char *format, ...)
39 {
40 va_list args;
41 int retval;
42
43 va_start(args, format);
44 retval = vfprintf(stdout, format, args);
45 va_end(args);
46
47 return retval;
48 }
49
50 int iflush()
51 {
52 int retval;
53
54 retval = fflush(stdout);
55
56 return retval;
57 }
58
59 int igetch(int clear_buf)
60 {
61 // static input buffer
62 static unsigned char buf[256];
63 static ssize_t len = 0;
64 static int pos = 0;
65
66 unsigned char tmp[256];
67 int out = KEY_NULL;
68 int in_esc = 0;
69 int in_ascii = 0;
70 int in_control = 0;
71 int i = 0;
72
73 if (clear_buf)
74 {
75 pos = 0;
76 len = 0;
77
78 return 0;
79 }
80
81 // Stop on system exit
82 if (SYS_exit)
83 return KEY_NULL;
84
85 if (pos >= len)
86 {
87 fd_set testfds;
88 struct timeval timeout;
89
90 pos = 0;
91 len = 0;
92
93 FD_ZERO(&testfds);
94 FD_SET(0, &testfds);
95
96 timeout.tv_sec = 1;
97 timeout.tv_usec = 0;
98
99 int result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,
100 (fd_set *)NULL, &timeout);
101
102 if (result == 0)
103 {
104 return KEY_TIMEOUT;
105 }
106 if (result < 0)
107 {
108 log_error("select() error (%d) !\n", result);
109 return KEY_NULL;
110 }
111 if (result > 0)
112 {
113 if (FD_ISSET(0, &testfds))
114 {
115 len = read(0, buf, 255);
116 }
117 }
118
119 // For debug
120 // for (j = 0; j < len; j++)
121 // log_std ("<--[%u]\n", (buf[j] + 256) % 256);
122 }
123
124 while (pos < len)
125 {
126 unsigned char c = buf[pos++];
127
128 if (c == '\0')
129 {
130 out = c;
131 break;
132 }
133
134 if (c == KEY_CONTROL)
135 {
136 if (in_control == 0)
137 {
138 in_control = 1;
139 i = 0;
140 continue;
141 }
142 }
143
144 if (in_control)
145 {
146 tmp[i++] = c;
147 if (i >= 2)
148 {
149 out = (int)tmp[0] * 256 + tmp[1];
150 in_control = 0;
151 break;
152 }
153 continue;
154 }
155
156 if (c == ESC_KEY)
157 {
158 if (in_esc == 0)
159 {
160 in_esc = 1;
161 in_ascii = 1;
162 i = 0;
163 continue;
164 }
165 else
166 {
167 out = ESC_KEY;
168 in_esc = 0;
169 break;
170 }
171 }
172
173 in_esc = 0;
174
175 if (in_ascii)
176 {
177 tmp[i++] = c;
178 if (c == 'm')
179 {
180 in_ascii = 0;
181 continue;
182 }
183 if (i == 2 && c >= 'A' && c <= 'D')
184 {
185 in_ascii = 0;
186 switch (c)
187 {
188 case 'A':
189 out = KEY_UP;
190 break;
191 case 'B':
192 out = KEY_DOWN;
193 break;
194 case 'C':
195 out = KEY_RIGHT;
196 break;
197 case 'D':
198 out = KEY_LEFT;
199 break;
200 }
201 break;
202 }
203 if (i == 3 && tmp[0] == 91 && tmp[2] == 126)
204 {
205 in_ascii = 0;
206 switch (tmp[1])
207 {
208 case 49:
209 out = KEY_HOME;
210 break;
211 case 51:
212 out = KEY_DEL;
213 break;
214 case 52:
215 out = KEY_END;
216 break;
217 case 53:
218 out = KEY_PGUP;
219 break;
220 case 54:
221 out = KEY_PGDN;
222 break;
223 }
224 break;
225 }
226 continue;
227 }
228
229 out = ((int)c + 256) % 256;
230 break;
231 }
232
233 // for debug
234 // log_std ("-->[%u]\n", out);
235
236 return out;
237 }
238
239 int igetch_t(long int sec)
240 {
241 int ch;
242 time_t t_begin = time(0);
243
244 do
245 {
246 ch = igetch(0);
247 } while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec));
248
249 return ch;
250 }
251
252 int ikbhit()
253 {
254 int len;
255
256 ioctl(0, FIONREAD, &len);
257
258 return len;
259 }

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