1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4 Copyright (C) 1998, Cygnus Solutions.
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.
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.
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.
25 #include "targ-vals.h"
36 /* Define the rate at which the simulator should poll the host
38 #ifndef POLL_QUIT_INTERVAL
39 #define POLL_QUIT_INTERVAL 0x10
42 static int poll_quit_count = POLL_QUIT_INTERVAL;
44 /* See the file include/callbacks.h for a description */
48 sim_io_init(SIM_DESC sd)
50 return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd));
55 sim_io_shutdown(SIM_DESC sd)
57 return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd));
62 sim_io_unlink(SIM_DESC sd,
65 return STATE_CALLBACK (sd)->unlink (STATE_CALLBACK (sd), f1);
70 sim_io_time(SIM_DESC sd,
73 return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd), t);
78 sim_io_system(SIM_DESC sd, const char *s)
80 return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s);
85 sim_io_rename(SIM_DESC sd,
89 return STATE_CALLBACK (sd)->rename (STATE_CALLBACK (sd), f1, f2);
94 sim_io_write_stdout(SIM_DESC sd,
98 switch (CURRENT_STDIO) {
100 return STATE_CALLBACK (sd)->write_stdout (STATE_CALLBACK (sd), buf, len);
103 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 1, buf, len);
106 sim_io_error (sd, "sim_io_write_stdout: unaccounted switch\n");
114 sim_io_flush_stdout(SIM_DESC sd)
116 switch (CURRENT_STDIO) {
118 STATE_CALLBACK (sd)->flush_stdout (STATE_CALLBACK (sd));
123 sim_io_error (sd, "sim_io_flush_stdout: unaccounted switch\n");
130 sim_io_write_stderr(SIM_DESC sd,
134 switch (CURRENT_STDIO) {
136 return STATE_CALLBACK (sd)->write_stderr (STATE_CALLBACK (sd), buf, len);
139 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 2, buf, len);
142 sim_io_error (sd, "sim_io_write_stderr: unaccounted switch\n");
150 sim_io_flush_stderr(SIM_DESC sd)
152 switch (CURRENT_STDIO) {
154 STATE_CALLBACK (sd)->flush_stderr (STATE_CALLBACK (sd));
159 sim_io_error (sd, "sim_io_flush_stderr: unaccounted switch\n");
166 sim_io_write(SIM_DESC sd,
171 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), fd, buf, len);
176 sim_io_read_stdin(SIM_DESC sd,
180 switch (CURRENT_STDIO) {
182 return STATE_CALLBACK (sd)->read_stdin (STATE_CALLBACK (sd), buf, len);
185 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), 0, buf, len);
188 sim_io_error (sd, "sim_io_read_stdin: unaccounted switch\n");
196 sim_io_read(SIM_DESC sd, int fd,
200 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), fd, buf, len);
205 sim_io_open(SIM_DESC sd,
209 return STATE_CALLBACK (sd)->open (STATE_CALLBACK (sd), name, flags);
214 sim_io_lseek(SIM_DESC sd,
219 return STATE_CALLBACK (sd)->lseek (STATE_CALLBACK (sd), fd, off, way);
224 sim_io_isatty(SIM_DESC sd,
227 return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd);
232 sim_io_get_errno(SIM_DESC sd)
234 return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd));
239 sim_io_close(SIM_DESC sd,
242 return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd);
247 sim_io_printf(SIM_DESC sd,
253 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
259 sim_io_vprintf(SIM_DESC sd,
263 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
268 sim_io_eprintf(SIM_DESC sd,
274 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
280 sim_io_evprintf(SIM_DESC sd,
284 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
289 sim_io_error(SIM_DESC sd,
293 if (sd == NULL || STATE_CALLBACK (sd) == NULL) {
296 vfprintf (stderr, fmt, ap);
298 fprintf (stderr, "\n");
304 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
306 STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), "");
312 sim_io_poll_quit(SIM_DESC sd)
314 if (STATE_CALLBACK (sd)->poll_quit != NULL && poll_quit_count-- < 0)
316 poll_quit_count = POLL_QUIT_INTERVAL;
317 if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd)))
323 /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin().
325 FIXME: Should not be calling fcntl() or grubbing around inside of
328 FIXME: Some completly new mechanism for handling the general
329 problem of asynchronous IO is needed.
331 FIXME: This function does not supress the echoing (ECHO) of input.
332 Consequently polled input is always displayed.
334 FIXME: This function does not perform uncooked reads.
335 Consequently, data will not be read until an EOLN character has
336 been entered. A cntrl-d may force the early termination of a line */
340 sim_io_poll_read (SIM_DESC sd,
345 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
346 int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd];
351 STATE_CALLBACK (sd)->last_errno = 0;
352 /* get the old status */
353 flags = fcntl (fd, F_GETFL, 0);
356 perror ("sim_io_poll_read");
359 /* temp, disable blocking IO */
360 status = fcntl (fd, F_SETFL, flags | O_NDELAY);
363 perror ("sim_io_read_stdin");
367 nr_read = read (fd, buf, sizeof_buf);
370 /* printf ("<nr-read=%d>\n", nr_read); */
376 STATE_CALLBACK (sd)->last_errno = errno;
378 /* return to regular vewing */
379 status = fcntl (fd, F_SETFL, flags);
382 perror ("sim_io_read_stdin");
387 return sim_io_read (sd, sim_io_fd, buf, sizeof_buf);