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

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Sat May 7 12:08:28 2005 UTC (20 years, 10 months ago) by sysadm
Branch: MAIN
CVS Tags: lbbs_1-0-0-0_MIL
Changes since 1.11: +5 -1 lines
Content type: text/x-csrc
*** empty log message ***

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

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