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

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Tue Apr 29 03:38:43 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.14: +1 -1 lines
Content type: text/x-csrc
Fix bug

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

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