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

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations)
Fri May 2 03:34:58 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.16: +11 -3 lines
Content type: text/x-csrc
Refine

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 char buf[256];
62 unsigned char c, tmp[256];
63 int out = KEY_NULL, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i = 0, j, result;
64 static int len = 0, pos = 0;
65 fd_set testfds;
66 struct timeval timeout;
67
68 if (clear_buf)
69 {
70 pos = 0;
71 len = 0;
72
73 return 0;
74 }
75
76 // Stop on system exit
77 if (SYS_exit)
78 return KEY_NULL;
79
80 if (pos >= len)
81 {
82 pos = 0;
83 len = 0;
84
85 FD_ZERO(&testfds);
86 FD_SET(0, &testfds);
87
88 timeout.tv_sec = 1;
89 timeout.tv_usec = 0;
90
91 result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,
92 (fd_set *)NULL, &timeout);
93
94 if (result == 0)
95 {
96 return KEY_TIMEOUT;
97 }
98 if (result < 0)
99 {
100 log_error("select() error (%d) !\n", result);
101 return KEY_NULL;
102 }
103 if (result > 0)
104 {
105 if (FD_ISSET(0, &testfds))
106 {
107 len = read(0, buf, 255);
108 }
109 }
110
111 // For debug
112 // for (j = 0; j < len; j++)
113 // log_std ("<--[%u]\n", (buf[j] + 256) % 256);
114 }
115
116 while (pos < len)
117 {
118 c = buf[pos++];
119
120 if (c == '\0')
121 {
122 out = c;
123 break;
124 }
125
126 if (c == KEY_CONTROL)
127 {
128 if (in_control == 0)
129 {
130 in_control = 1;
131 i = 0;
132 continue;
133 }
134 }
135
136 if (in_control)
137 {
138 tmp[i++] = c;
139 if (i >= 2)
140 {
141 out = (int)tmp[0] * 256 + tmp[1];
142 in_control = 0;
143 break;
144 }
145 continue;
146 }
147
148 if (c == ESC_KEY)
149 {
150 if (in_esc == 0)
151 {
152 in_esc = 1;
153 in_ascii = 1;
154 i = 0;
155 continue;
156 }
157 else
158 {
159 out = ESC_KEY;
160 in_esc = 0;
161 break;
162 }
163 }
164
165 in_esc = 0;
166
167 if (in_ascii)
168 {
169 tmp[i++] = c;
170 if (c == 'm')
171 {
172 in_ascii = 0;
173 continue;
174 }
175 if (i == 2 && c >= 'A' && c <= 'D')
176 {
177 in_ascii = 0;
178 switch (c)
179 {
180 case 'A':
181 out = KEY_UP;
182 break;
183 case 'B':
184 out = KEY_DOWN;
185 break;
186 case 'C':
187 out = KEY_RIGHT;
188 break;
189 case 'D':
190 out = KEY_LEFT;
191 break;
192 }
193 break;
194 }
195 if (i == 3 && tmp[0] == 91 && tmp[2] == 126)
196 {
197 in_ascii = 0;
198 switch (tmp[1])
199 {
200 case 49:
201 out = KEY_HOME;
202 break;
203 case 51:
204 out = KEY_DEL;
205 break;
206 case 52:
207 out = KEY_END;
208 break;
209 case 53:
210 out = KEY_PGUP;
211 break;
212 case 54:
213 out = KEY_PGDN;
214 break;
215 }
216 break;
217 }
218 continue;
219 }
220
221 out = ((int)c + 256) % 256;
222 break;
223 }
224
225 // for debug
226 // log_std ("-->[%u]\n", out);
227
228 return out;
229 }
230
231 int igetch_t(long int sec)
232 {
233 int ch;
234 time_t t_begin = time(0);
235
236 do
237 {
238 ch = igetch(0);
239 } while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec));
240
241 return ch;
242 }
243
244 int ikbhit()
245 {
246 int len;
247
248 ioctl(0, FIONREAD, &len);
249
250 return len;
251 }

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