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