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

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Tue May 6 05:31:26 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.19: +3 -4 lines
Content type: text/x-csrc
Update copyright and license information

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

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