No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / sim / common / sim-io.c
blobd1aab44fb76bdaa065689bdff7cd9226bbaf0aab
1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "sim-main.h"
26 #include "sim-io.h"
27 #include "targ-vals.h"
29 #include <errno.h>
30 #if HAVE_FCNTL_H
31 #include <fcntl.h>
32 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 /* Define the rate at which the simulator should poll the host
39 for a quit. */
40 #ifndef POLL_QUIT_INTERVAL
41 #define POLL_QUIT_INTERVAL 0x10
42 #endif
44 static int poll_quit_count = POLL_QUIT_INTERVAL;
46 /* See the file include/callbacks.h for a description */
49 int
50 sim_io_init(SIM_DESC sd)
52 return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd));
56 int
57 sim_io_shutdown(SIM_DESC sd)
59 return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd));
63 int
64 sim_io_unlink(SIM_DESC sd,
65 const char *f1)
67 return STATE_CALLBACK (sd)->unlink (STATE_CALLBACK (sd), f1);
71 long
72 sim_io_time(SIM_DESC sd,
73 long *t)
75 return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd), t);
79 int
80 sim_io_system(SIM_DESC sd, const char *s)
82 return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s);
86 int
87 sim_io_rename(SIM_DESC sd,
88 const char *f1,
89 const char *f2)
91 return STATE_CALLBACK (sd)->rename (STATE_CALLBACK (sd), f1, f2);
95 int
96 sim_io_write_stdout(SIM_DESC sd,
97 const char *buf,
98 int len)
100 switch (CURRENT_STDIO) {
101 case DO_USE_STDIO:
102 return STATE_CALLBACK (sd)->write_stdout (STATE_CALLBACK (sd), buf, len);
103 break;
104 case DONT_USE_STDIO:
105 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 1, buf, len);
106 break;
107 default:
108 sim_io_error (sd, "sim_io_write_stdout: unaccounted switch\n");
109 break;
111 return 0;
115 void
116 sim_io_flush_stdout(SIM_DESC sd)
118 switch (CURRENT_STDIO) {
119 case DO_USE_STDIO:
120 STATE_CALLBACK (sd)->flush_stdout (STATE_CALLBACK (sd));
121 break;
122 case DONT_USE_STDIO:
123 break;
124 default:
125 sim_io_error (sd, "sim_io_flush_stdout: unaccounted switch\n");
126 break;
132 sim_io_write_stderr(SIM_DESC sd,
133 const char *buf,
134 int len)
136 switch (CURRENT_STDIO) {
137 case DO_USE_STDIO:
138 return STATE_CALLBACK (sd)->write_stderr (STATE_CALLBACK (sd), buf, len);
139 break;
140 case DONT_USE_STDIO:
141 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 2, buf, len);
142 break;
143 default:
144 sim_io_error (sd, "sim_io_write_stderr: unaccounted switch\n");
145 break;
147 return 0;
151 void
152 sim_io_flush_stderr(SIM_DESC sd)
154 switch (CURRENT_STDIO) {
155 case DO_USE_STDIO:
156 STATE_CALLBACK (sd)->flush_stderr (STATE_CALLBACK (sd));
157 break;
158 case DONT_USE_STDIO:
159 break;
160 default:
161 sim_io_error (sd, "sim_io_flush_stderr: unaccounted switch\n");
162 break;
168 sim_io_write(SIM_DESC sd,
169 int fd,
170 const char *buf,
171 int len)
173 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), fd, buf, len);
178 sim_io_read_stdin(SIM_DESC sd,
179 char *buf,
180 int len)
182 switch (CURRENT_STDIO) {
183 case DO_USE_STDIO:
184 return STATE_CALLBACK (sd)->read_stdin (STATE_CALLBACK (sd), buf, len);
185 break;
186 case DONT_USE_STDIO:
187 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), 0, buf, len);
188 break;
189 default:
190 sim_io_error (sd, "sim_io_read_stdin: unaccounted switch\n");
191 break;
193 return 0;
198 sim_io_read(SIM_DESC sd, int fd,
199 char *buf,
200 int len)
202 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), fd, buf, len);
207 sim_io_open(SIM_DESC sd,
208 const char *name,
209 int flags)
211 return STATE_CALLBACK (sd)->open (STATE_CALLBACK (sd), name, flags);
216 sim_io_lseek(SIM_DESC sd,
217 int fd,
218 long off,
219 int way)
221 return STATE_CALLBACK (sd)->lseek (STATE_CALLBACK (sd), fd, off, way);
226 sim_io_isatty(SIM_DESC sd,
227 int fd)
229 return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd);
234 sim_io_get_errno(SIM_DESC sd)
236 return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd));
241 sim_io_close(SIM_DESC sd,
242 int fd)
244 return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd);
248 void
249 sim_io_printf(SIM_DESC sd,
250 const char *fmt,
251 ...)
253 va_list ap;
254 va_start(ap, fmt);
255 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
256 va_end(ap);
260 void
261 sim_io_vprintf(SIM_DESC sd,
262 const char *fmt,
263 va_list ap)
265 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
269 void
270 sim_io_eprintf(SIM_DESC sd,
271 const char *fmt,
272 ...)
274 va_list ap;
275 va_start(ap, fmt);
276 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
277 va_end(ap);
281 void
282 sim_io_evprintf(SIM_DESC sd,
283 const char *fmt,
284 va_list ap)
286 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
290 void
291 sim_io_error(SIM_DESC sd,
292 const char *fmt,
293 ...)
295 if (sd == NULL || STATE_CALLBACK (sd) == NULL) {
296 va_list ap;
297 va_start(ap, fmt);
298 vfprintf (stderr, fmt, ap);
299 va_end(ap);
300 fprintf (stderr, "\n");
301 abort ();
303 else {
304 va_list ap;
305 va_start(ap, fmt);
306 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
307 va_end(ap);
308 STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), "");
313 void
314 sim_io_poll_quit(SIM_DESC sd)
316 if (STATE_CALLBACK (sd)->poll_quit != NULL && poll_quit_count-- < 0)
318 poll_quit_count = POLL_QUIT_INTERVAL;
319 if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd)))
320 sim_stop (sd);
325 /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin().
327 FIXME: Should not be calling fcntl() or grubbing around inside of
328 ->fdmap and ->errno.
330 FIXME: Some completly new mechanism for handling the general
331 problem of asynchronous IO is needed.
333 FIXME: This function does not supress the echoing (ECHO) of input.
334 Consequently polled input is always displayed.
336 FIXME: This function does not perform uncooked reads.
337 Consequently, data will not be read until an EOLN character has
338 been entered. A cntrl-d may force the early termination of a line */
342 sim_io_poll_read (SIM_DESC sd,
343 int sim_io_fd,
344 char *buf,
345 int sizeof_buf)
347 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
348 int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd];
349 int flags;
350 int status;
351 int nr_read;
352 int result;
353 STATE_CALLBACK (sd)->last_errno = 0;
354 /* get the old status */
355 flags = fcntl (fd, F_GETFL, 0);
356 if (flags == -1)
358 perror ("sim_io_poll_read");
359 return 0;
361 /* temp, disable blocking IO */
362 status = fcntl (fd, F_SETFL, flags | O_NDELAY);
363 if (status == -1)
365 perror ("sim_io_read_stdin");
366 return 0;
368 /* try for input */
369 nr_read = read (fd, buf, sizeof_buf);
370 if (nr_read >= 0)
372 /* printf ("<nr-read=%d>\n", nr_read); */
373 result = nr_read;
375 else
376 { /* nr_read < 0 */
377 result = -1;
378 STATE_CALLBACK (sd)->last_errno = errno;
380 /* return to regular vewing */
381 status = fcntl (fd, F_SETFL, flags);
382 if (status == -1)
384 perror ("sim_io_read_stdin");
385 /* return 0; */
387 return result;
388 #else
389 return sim_io_read (sd, sim_io_fd, buf, sizeof_buf);
390 #endif