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