]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-io.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / sim / common / sim-io.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4 Copyright (C) 1998, Cygnus Solutions.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 */
21
22
23 #include "sim-main.h"
24 #include "sim-io.h"
25 #include "targ-vals.h"
26
27 #include <errno.h>
28 #if HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif
31
32 #if HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36
37 /* See the file include/callbacks.h for a description */
38
39
40 int
41 sim_io_init(SIM_DESC sd)
42 {
43 return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd));
44 }
45
46
47 int
48 sim_io_shutdown(SIM_DESC sd)
49 {
50 return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd));
51 }
52
53
54 int
55 sim_io_unlink(SIM_DESC sd,
56 const char *f1)
57 {
58 return STATE_CALLBACK (sd)->unlink (STATE_CALLBACK (sd), f1);
59 }
60
61
62 long
63 sim_io_time(SIM_DESC sd,
64 long *t)
65 {
66 return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd), t);
67 }
68
69
70 int
71 sim_io_system(SIM_DESC sd, const char *s)
72 {
73 return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s);
74 }
75
76
77 int
78 sim_io_rename(SIM_DESC sd,
79 const char *f1,
80 const char *f2)
81 {
82 return STATE_CALLBACK (sd)->rename (STATE_CALLBACK (sd), f1, f2);
83 }
84
85
86 int
87 sim_io_write_stdout(SIM_DESC sd,
88 const char *buf,
89 int len)
90 {
91 switch (CURRENT_STDIO) {
92 case DO_USE_STDIO:
93 return STATE_CALLBACK (sd)->write_stdout (STATE_CALLBACK (sd), buf, len);
94 break;
95 case DONT_USE_STDIO:
96 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 1, buf, len);
97 break;
98 default:
99 sim_io_error (sd, "sim_io_write_stdout: unaccounted switch\n");
100 break;
101 }
102 return 0;
103 }
104
105
106 void
107 sim_io_flush_stdout(SIM_DESC sd)
108 {
109 switch (CURRENT_STDIO) {
110 case DO_USE_STDIO:
111 STATE_CALLBACK (sd)->flush_stdout (STATE_CALLBACK (sd));
112 break;
113 case DONT_USE_STDIO:
114 break;
115 default:
116 sim_io_error (sd, "sim_io_flush_stdout: unaccounted switch\n");
117 break;
118 }
119 }
120
121
122 int
123 sim_io_write_stderr(SIM_DESC sd,
124 const char *buf,
125 int len)
126 {
127 switch (CURRENT_STDIO) {
128 case DO_USE_STDIO:
129 return STATE_CALLBACK (sd)->write_stderr (STATE_CALLBACK (sd), buf, len);
130 break;
131 case DONT_USE_STDIO:
132 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 2, buf, len);
133 break;
134 default:
135 sim_io_error (sd, "sim_io_write_stderr: unaccounted switch\n");
136 break;
137 }
138 return 0;
139 }
140
141
142 void
143 sim_io_flush_stderr(SIM_DESC sd)
144 {
145 switch (CURRENT_STDIO) {
146 case DO_USE_STDIO:
147 STATE_CALLBACK (sd)->flush_stderr (STATE_CALLBACK (sd));
148 break;
149 case DONT_USE_STDIO:
150 break;
151 default:
152 sim_io_error (sd, "sim_io_flush_stderr: unaccounted switch\n");
153 break;
154 }
155 }
156
157
158 int
159 sim_io_write(SIM_DESC sd,
160 int fd,
161 const char *buf,
162 int len)
163 {
164 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), fd, buf, len);
165 }
166
167
168 int
169 sim_io_read_stdin(SIM_DESC sd,
170 char *buf,
171 int len)
172 {
173 switch (CURRENT_STDIO) {
174 case DO_USE_STDIO:
175 return STATE_CALLBACK (sd)->read_stdin (STATE_CALLBACK (sd), buf, len);
176 break;
177 case DONT_USE_STDIO:
178 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), 0, buf, len);
179 break;
180 default:
181 sim_io_error (sd, "sim_io_read_stdin: unaccounted switch\n");
182 break;
183 }
184 return 0;
185 }
186
187
188 int
189 sim_io_read(SIM_DESC sd, int fd,
190 char *buf,
191 int len)
192 {
193 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), fd, buf, len);
194 }
195
196
197 int
198 sim_io_open(SIM_DESC sd,
199 const char *name,
200 int flags)
201 {
202 return STATE_CALLBACK (sd)->open (STATE_CALLBACK (sd), name, flags);
203 }
204
205
206 int
207 sim_io_lseek(SIM_DESC sd,
208 int fd,
209 long off,
210 int way)
211 {
212 return STATE_CALLBACK (sd)->lseek (STATE_CALLBACK (sd), fd, off, way);
213 }
214
215
216 int
217 sim_io_isatty(SIM_DESC sd,
218 int fd)
219 {
220 return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd);
221 }
222
223
224 int
225 sim_io_get_errno(SIM_DESC sd)
226 {
227 return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd));
228 }
229
230
231 int
232 sim_io_close(SIM_DESC sd,
233 int fd)
234 {
235 return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd);
236 }
237
238
239 void
240 sim_io_printf(SIM_DESC sd,
241 const char *fmt,
242 ...)
243 {
244 va_list ap;
245 va_start(ap, fmt);
246 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
247 va_end(ap);
248 }
249
250
251 void
252 sim_io_vprintf(SIM_DESC sd,
253 const char *fmt,
254 va_list ap)
255 {
256 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
257 }
258
259
260 void
261 sim_io_eprintf(SIM_DESC sd,
262 const char *fmt,
263 ...)
264 {
265 va_list ap;
266 va_start(ap, fmt);
267 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
268 va_end(ap);
269 }
270
271
272 void
273 sim_io_evprintf(SIM_DESC sd,
274 const char *fmt,
275 va_list ap)
276 {
277 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
278 }
279
280
281 void
282 sim_io_error(SIM_DESC sd,
283 const char *fmt,
284 ...)
285 {
286 if (sd == NULL || STATE_CALLBACK (sd) == NULL) {
287 va_list ap;
288 va_start(ap, fmt);
289 vfprintf (stderr, fmt, ap);
290 va_end(ap);
291 fprintf (stderr, "\n");
292 abort ();
293 }
294 else {
295 va_list ap;
296 va_start(ap, fmt);
297 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
298 va_end(ap);
299 STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), "");
300 }
301 }
302
303
304 void
305 sim_io_poll_quit(SIM_DESC sd)
306 {
307 if (STATE_CALLBACK (sd)->poll_quit != NULL)
308 if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd)))
309 sim_stop (sd);
310 }
311
312
313 /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin().
314
315 FIXME: Should not be calling fcntl() or grubbing around inside of
316 ->fdmap and ->errno.
317
318 FIXME: Some completly new mechanism for handling the general
319 problem of asynchronous IO is needed.
320
321 FIXME: This function does not supress the echoing (ECHO) of input.
322 Consequently polled input is always displayed.
323
324 FIXME: This function does not perform uncooked reads.
325 Consequently, data will not be read until an EOLN character has
326 been entered. A cntrl-d may force the early termination of a line */
327
328
329 int
330 sim_io_poll_read (SIM_DESC sd,
331 int sim_io_fd,
332 char *buf,
333 int sizeof_buf)
334 {
335 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
336 int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd];
337 int flags;
338 int status;
339 int nr_read;
340 int result;
341 STATE_CALLBACK (sd)->last_errno = 0;
342 /* get the old status */
343 flags = fcntl (fd, F_GETFL, 0);
344 if (flags == -1)
345 {
346 perror ("sim_io_poll_read");
347 return 0;
348 }
349 /* temp, disable blocking IO */
350 status = fcntl (fd, F_SETFL, flags | O_NDELAY);
351 if (status == -1)
352 {
353 perror ("sim_io_read_stdin");
354 return 0;
355 }
356 /* try for input */
357 nr_read = read (fd, buf, sizeof_buf);
358 if (nr_read >= 0)
359 {
360 /* printf ("<nr-read=%d>\n", nr_read); */
361 result = nr_read;
362 }
363 else
364 { /* nr_read < 0 */
365 result = -1;
366 STATE_CALLBACK (sd)->last_errno = errno;
367 }
368 /* return to regular vewing */
369 status = fcntl (fd, F_SETFL, flags);
370 if (status == -1)
371 {
372 perror ("sim_io_read_stdin");
373 /* return 0; */
374 }
375 return result;
376 #else
377 return sim_io_read (sd, sim_io_fd, buf, sizeof_buf);
378 #endif
379 }