Passify GCC. Convert 0x0LL to something more portable in the FP code.
[deliverable/binutils-gdb.git] / sim / common / callback.c
1 /* Host callback routines for GDB.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* This file provides a standard way for targets to talk to the host OS
22 level. */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include "ansidecl.h"
28 #ifdef ANSI_PROTOTYPES
29 #include <stdarg.h>
30 #else
31 #include <varargs.h>
32 #endif
33 #include <stdio.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <time.h>
40 #include "callback.h"
41 #include "targ-vals.h"
42
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
48 broken out of the simulator directory, these are here to not require
49 sim-utils.h. */
50 void sim_cb_printf PARAMS ((host_callback *, const char *, ...));
51 void sim_cb_eprintf PARAMS ((host_callback *, const char *, ...));
52
53 extern int system PARAMS ((const char *));
54
55 static int os_init PARAMS ((host_callback *));
56 static int os_shutdown PARAMS ((host_callback *));
57 static int os_unlink PARAMS ((host_callback *, const char *));
58 static long os_time PARAMS ((host_callback *, long *));
59 static int os_system PARAMS ((host_callback *, const char *));
60 static int os_rename PARAMS ((host_callback *, const char *, const char *));
61 static int os_write_stdout PARAMS ((host_callback *, const char *, int));
62 static void os_flush_stdout PARAMS ((host_callback *));
63 static int os_write_stderr PARAMS ((host_callback *, const char *, int));
64 static void os_flush_stderr PARAMS ((host_callback *));
65 static int os_write PARAMS ((host_callback *, int, const char *, int));
66 static int os_read_stdin PARAMS ((host_callback *, char *, int));
67 static int os_read PARAMS ((host_callback *, int, char *, int));
68 static int os_open PARAMS ((host_callback *, const char *, int));
69 static int os_lseek PARAMS ((host_callback *, int, long, int));
70 static int os_isatty PARAMS ((host_callback *, int));
71 static int os_get_errno PARAMS ((host_callback *));
72 static int os_close PARAMS ((host_callback *, int));
73 static void os_vprintf_filtered PARAMS ((host_callback *, const char *, va_list));
74 static void os_evprintf_filtered PARAMS ((host_callback *, const char *, va_list));
75 static void os_error PARAMS ((host_callback *, const char *, ...));
76 static int fdmap PARAMS ((host_callback *, int));
77 static int fdbad PARAMS ((host_callback *, int));
78 static int wrap PARAMS ((host_callback *, int));
79
80 /* Set the callback copy of errno from what we see now. */
81
82 static int
83 wrap (p, val)
84 host_callback *p;
85 int val;
86 {
87 p->last_errno = errno;
88 return val;
89 }
90
91 /* Make sure the FD provided is ok. If not, return non-zero
92 and set errno. */
93
94 static int
95 fdbad (p, fd)
96 host_callback *p;
97 int fd;
98 {
99 if (fd < 0 || fd > MAX_CALLBACK_FDS || !p->fdopen[fd])
100 {
101 p->last_errno = EINVAL;
102 return -1;
103 }
104 return 0;
105 }
106
107 static int
108 fdmap (p, fd)
109 host_callback *p;
110 int fd;
111 {
112 return p->fdmap[fd];
113 }
114
115 static int
116 os_close (p, fd)
117 host_callback *p;
118 int fd;
119 {
120 int result;
121
122 result = fdbad (p, fd);
123 if (result)
124 return result;
125 result = wrap (p, close (fdmap (p, fd)));
126 if(result == 0 && !p->alwaysopen[fd])
127 p->fdopen[fd] = 0;
128
129 return result;
130 }
131
132
133 /* taken from gdb/util.c - should be in a library */
134
135
136 #if defined(__GO32__) || defined (_WIN32)
137 static int
138 os_poll_quit (p)
139 host_callback *p;
140 {
141 #ifndef _MSC_VER
142 int kbhit ();
143 int getkey ();
144 if (kbhit ())
145 {
146 int k = getkey ();
147 if (k == 1)
148 {
149 return 1;
150 }
151 else if (k == 2)
152 {
153 return 1;
154 }
155 else
156 {
157 sim_cb_eprintf (p, "CTRL-A to quit, CTRL-B to quit harder\n");
158 }
159 }
160 #else /* !_MSC_VER */
161 /* NB - this will not compile! */
162 int k = win32pollquit();
163 if (k == 1)
164 return 1;
165 else if (k == 2)
166 return 1;
167 #endif /* !_MSC_VER */
168 return 0;
169 }
170 #else
171 #define os_poll_quit 0
172 #endif /* defined(__GO32__) || defined(_WIN32) */
173
174 static int
175 os_get_errno (p)
176 host_callback *p;
177 {
178 return host_to_target_errno (p->last_errno);
179 }
180
181
182 static int
183 os_isatty (p, fd)
184 host_callback *p;
185 int fd;
186 {
187 int result;
188
189 result = fdbad (p, fd);
190 if (result)
191 return result;
192 result = wrap (p, isatty (fdmap (p, fd)));
193
194 return result;
195 }
196
197 static int
198 os_lseek (p, fd, off, way)
199 host_callback *p;
200 int fd;
201 long off;
202 int way;
203 {
204 int result;
205
206 result = fdbad (p, fd);
207 if (result)
208 return result;
209 result = lseek (fdmap (p, fd), off, way);
210 return result;
211 }
212
213 static int
214 os_open (p, name, flags)
215 host_callback *p;
216 const char *name;
217 int flags;
218 {
219 int i;
220 for (i = 0; i < MAX_CALLBACK_FDS; i++)
221 {
222 if (!p->fdopen[i])
223 {
224 int f = open (name, target_to_host_open (flags), 0644);
225 if (f < 0)
226 {
227 p->last_errno = errno;
228 return f;
229 }
230 p->fdopen[i] = 1;
231 p->fdmap[i] = f;
232 return i;
233 }
234 }
235 p->last_errno = EMFILE;
236 return -1;
237 }
238
239 static int
240 os_read (p, fd, buf, len)
241 host_callback *p;
242 int fd;
243 char *buf;
244 int len;
245 {
246 int result;
247
248 result = fdbad (p, fd);
249 if (result)
250 return result;
251 result = wrap (p, read (fdmap (p, fd), buf, len));
252 return result;
253 }
254
255 static int
256 os_read_stdin (p, buf, len)
257 host_callback *p;
258 char *buf;
259 int len;
260 {
261 return wrap (p, read (0, buf, len));
262 }
263
264 static int
265 os_write (p, fd, buf, len)
266 host_callback *p;
267 int fd;
268 const char *buf;
269 int len;
270 {
271 int result;
272
273 result = fdbad (p, fd);
274 if (result)
275 return result;
276 result = wrap (p, write (fdmap (p, fd), buf, len));
277 return result;
278 }
279
280 static int
281 os_write_stdout (p, buf, len)
282 host_callback *p;
283 const char *buf;
284 int len;
285 {
286 return fwrite(buf, 1, len, stdout);
287 }
288
289 static void
290 os_flush_stdout (p)
291 host_callback *p;
292 {
293 fflush (stdout);
294 }
295
296 static int
297 os_write_stderr (p, buf, len)
298 host_callback *p;
299 const char *buf;
300 int len;
301 {
302 return fwrite(buf, 1, len, stderr);
303 }
304
305 static void
306 os_flush_stderr (p)
307 host_callback *p;
308 {
309 fflush (stderr);
310 }
311
312 static int
313 os_rename (p, f1, f2)
314 host_callback *p;
315 const char *f1;
316 const char *f2;
317 {
318 return wrap (p, rename (f1, f2));
319 }
320
321
322 static int
323 os_system (p, s)
324 host_callback *p;
325 const char *s;
326 {
327 return wrap (p, system (s));
328 }
329
330 static long
331 os_time (p, t)
332 host_callback *p;
333 long *t;
334 {
335 return wrap (p, time (t));
336 }
337
338
339 static int
340 os_unlink (p, f1)
341 host_callback *p;
342 const char *f1;
343 {
344 return wrap (p, unlink (f1));
345 }
346
347
348 static int
349 os_shutdown (p)
350 host_callback *p;
351 {
352 int i;
353 for (i = 0; i < MAX_CALLBACK_FDS; i++)
354 {
355 if (p->fdopen[i] && !p->alwaysopen[i]) {
356 close (p->fdmap[i]);
357 p->fdopen[i] = 0;
358 }
359 }
360 return 1;
361 }
362
363 static int
364 os_init(p)
365 host_callback *p;
366 {
367 int i;
368 os_shutdown (p);
369 for (i= 0; i < 3; i++)
370 {
371 p->fdmap[i] = i;
372 p->fdopen[i] = 1;
373 p->alwaysopen[i] = 1;
374 }
375 return 1;
376 }
377
378 /* DEPRECIATED */
379
380 /* VARARGS */
381 static void
382 #ifdef ANSI_PROTOTYPES
383 os_printf_filtered (host_callback *p, const char *format, ...)
384 #else
385 os_printf_filtered (p, va_alist)
386 host_callback *p;
387 va_dcl
388 #endif
389 {
390 va_list args;
391 #ifdef ANSI_PROTOTYPES
392 va_start (args, format);
393 #else
394 char *format;
395
396 va_start (args);
397 format = va_arg (args, char *);
398 #endif
399
400 vfprintf (stdout, format, args);
401 va_end (args);
402 }
403
404 /* VARARGS */
405 static void
406 #ifdef ANSI_PROTOTYPES
407 os_vprintf_filtered (host_callback *p, const char *format, va_list args)
408 #else
409 os_vprintf_filtered (p, format, args)
410 host_callback *p;
411 const char *format;
412 va_list args;
413 #endif
414 {
415 vprintf (format, args);
416 }
417
418 /* VARARGS */
419 static void
420 #ifdef ANSI_PROTOTYPES
421 os_evprintf_filtered (host_callback *p, const char *format, va_list args)
422 #else
423 os_evprintf_filtered (p, format, args)
424 host_callback *p;
425 const char *format;
426 va_list args;
427 #endif
428 {
429 vfprintf (stderr, format, args);
430 }
431
432 /* VARARGS */
433 static void
434 #ifdef ANSI_PROTOTYPES
435 os_error (host_callback *p, const char *format, ...)
436 #else
437 os_error (p, va_alist)
438 host_callback *p;
439 va_dcl
440 #endif
441 {
442 va_list args;
443 #ifdef ANSI_PROTOTYPES
444 va_start (args, format);
445 #else
446 char *format;
447
448 va_start (args);
449 format = va_arg (args, char *);
450 #endif
451
452 vfprintf (stderr, format, args);
453 fprintf (stderr, "\n");
454
455 va_end (args);
456 exit (1);
457 }
458
459 host_callback default_callback =
460 {
461 os_close,
462 os_get_errno,
463 os_isatty,
464 os_lseek,
465 os_open,
466 os_read,
467 os_read_stdin,
468 os_rename,
469 os_system,
470 os_time,
471 os_unlink,
472 os_write,
473 os_write_stdout,
474 os_flush_stdout,
475 os_write_stderr,
476 os_flush_stderr,
477
478 os_poll_quit,
479
480 os_shutdown,
481 os_init,
482
483 os_printf_filtered, /* depreciated */
484
485 os_vprintf_filtered,
486 os_evprintf_filtered,
487 os_error,
488
489 0, /* last errno */
490
491 { 0, }, /* fdmap */
492 { 0, }, /* fdopen */
493 { 0, }, /* alwaysopen */
494
495 HOST_CALLBACK_MAGIC,
496 };
497 \f
498 /* FIXME: Need to add hooks so target can tweak as necessary. */
499
500 /* FIXME: struct stat conversion is missing. */
501
502 /* FIXME: sort tables if large.
503 Alternatively, an obvious improvement for errno conversion is
504 to machine generate a function with a large switch(). */
505
506 int
507 host_to_target_errno (host_val)
508 int host_val;
509 {
510 target_defs_map *m;
511
512 for (m = &errno_map[0]; m->host_val; ++m)
513 if (m->host_val == host_val)
514 return m->target_val;
515
516 /* ??? Which error to return in this case is up for grabs.
517 Note that some missing values may have standard alternatives.
518 For now return 0 and require caller to deal with it. */
519 return 0;
520 }
521
522 /* Given a set of target bitmasks for the open system call,
523 return the host equivalent.
524 Mapping open flag values is best done by looping so there's no need
525 to machine generate this function. */
526
527 int
528 target_to_host_open (target_val)
529 int target_val;
530 {
531 int host_val = 0;
532 target_defs_map *m;
533
534 for (m = &open_map[0]; m->host_val != -1; ++m)
535 {
536 switch (m->target_val)
537 {
538 /* O_RDONLY can be (and usually is) 0 which needs to be treated
539 specially. */
540 case TARGET_O_RDONLY :
541 case TARGET_O_WRONLY :
542 case TARGET_O_RDWR :
543 if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR))
544 == m->target_val)
545 host_val |= m->host_val;
546 /* Handle the host/target differentiating between binary and
547 text mode. Only one case is of importance */
548 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
549 host_val |= O_BINARY;
550 #endif
551 break;
552 default :
553 if ((m->target_val & target_val) == m->target_val)
554 host_val |= m->host_val;
555 break;
556 }
557 }
558
559 return host_val;
560 }
561 \f
562 /* Cover functions to the vfprintf callbacks.
563
564 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
565 a larger "remote target subsystem"] with a well defined interface, then
566 one would think that the subsystem would provide these. However, until
567 one is allowed to create such a subsystem (with its own source tree
568 independent of any particular user), such a critter can't exist. Thus
569 these functions are here for the time being. */
570
571 void
572 sim_cb_printf (host_callback *p, const char *fmt, ...)
573 {
574 va_list ap;
575
576 va_start (ap, fmt);
577 p->vprintf_filtered (p, fmt, ap);
578 va_end (ap);
579 }
580
581 void
582 sim_cb_eprintf (host_callback *p, const char *fmt, ...)
583 {
584 va_list ap;
585
586 va_start (ap, fmt);
587 p->evprintf_filtered (p, fmt, ap);
588 va_end (ap);
589 }
This page took 0.054697 seconds and 5 git commands to generate.