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

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations)
Wed Apr 30 09:18:19 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.15: +1 -0 lines
Content type: text/x-csrc
Add missing header files
Update compile dependency

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

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