1 /* Host callback routines for GDB.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB.
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.
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.
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. */
21 /* This file provides a standard way for targets to talk to the host OS
28 #ifdef ANSI_PROTOTYPES
41 #include "targ-vals.h"
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
50 void sim_cb_printf
PARAMS ((host_callback
*, const char *, ...));
51 void sim_cb_eprintf
PARAMS ((host_callback
*, const char *, ...));
53 extern int system
PARAMS ((const char *));
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));
80 /* Set the callback copy of errno from what we see now. */
87 p
->last_errno
= errno
;
91 /* Make sure the FD provided is ok. If not, return non-zero
99 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
101 p
->last_errno
= EINVAL
;
122 result
= fdbad (p
, fd
);
125 result
= wrap (p
, close (fdmap (p
, fd
)));
126 if(result
== 0 && !p
->alwaysopen
[fd
])
133 /* taken from gdb/util.c - should be in a library */
136 #if defined(__GO32__) || defined (_WIN32)
157 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
160 #else /* !_MSC_VER */
161 /* NB - this will not compile! */
162 int k
= win32pollquit();
167 #endif /* !_MSC_VER */
171 #define os_poll_quit 0
172 #endif /* defined(__GO32__) || defined(_WIN32) */
178 return host_to_target_errno (p
->last_errno
);
189 result
= fdbad (p
, fd
);
192 result
= wrap (p
, isatty (fdmap (p
, fd
)));
198 os_lseek (p
, fd
, off
, way
)
206 result
= fdbad (p
, fd
);
209 result
= lseek (fdmap (p
, fd
), off
, way
);
214 os_open (p
, name
, flags
)
220 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
224 int f
= open (name
, target_to_host_open (flags
), 0644);
227 p
->last_errno
= errno
;
235 p
->last_errno
= EMFILE
;
240 os_read (p
, fd
, buf
, len
)
248 result
= fdbad (p
, fd
);
251 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
256 os_read_stdin (p
, buf
, len
)
261 return wrap (p
, read (0, buf
, len
));
265 os_write (p
, fd
, buf
, len
)
273 result
= fdbad (p
, fd
);
276 result
= wrap (p
, write (fdmap (p
, fd
), buf
, len
));
281 os_write_stdout (p
, buf
, len
)
286 return fwrite(buf
, 1, len
, stdout
);
297 os_write_stderr (p
, buf
, len
)
302 return fwrite(buf
, 1, len
, stderr
);
313 os_rename (p
, f1
, f2
)
318 return wrap (p
, rename (f1
, f2
));
327 return wrap (p
, system (s
));
335 return wrap (p
, time (t
));
344 return wrap (p
, unlink (f1
));
353 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
355 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
369 for (i
= 0; i
< 3; i
++)
373 p
->alwaysopen
[i
] = 1;
382 #ifdef ANSI_PROTOTYPES
383 os_printf_filtered (host_callback
*p
, const char *format
, ...)
385 os_printf_filtered (p
, va_alist
)
391 #ifdef ANSI_PROTOTYPES
392 va_start (args
, format
);
397 format
= va_arg (args
, char *);
400 vfprintf (stdout
, format
, args
);
406 #ifdef ANSI_PROTOTYPES
407 os_vprintf_filtered (host_callback
*p
, const char *format
, va_list args
)
409 os_vprintf_filtered (p
, format
, args
)
415 vprintf (format
, args
);
420 #ifdef ANSI_PROTOTYPES
421 os_evprintf_filtered (host_callback
*p
, const char *format
, va_list args
)
423 os_evprintf_filtered (p
, format
, args
)
429 vfprintf (stderr
, format
, args
);
434 #ifdef ANSI_PROTOTYPES
435 os_error (host_callback
*p
, const char *format
, ...)
437 os_error (p
, va_alist
)
443 #ifdef ANSI_PROTOTYPES
444 va_start (args
, format
);
449 format
= va_arg (args
, char *);
452 vfprintf (stderr
, format
, args
);
453 fprintf (stderr
, "\n");
459 host_callback default_callback
=
483 os_printf_filtered
, /* depreciated */
486 os_evprintf_filtered
,
493 { 0, }, /* alwaysopen */
498 /* FIXME: Need to add hooks so target can tweak as necessary. */
500 /* FIXME: struct stat conversion is missing. */
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(). */
507 host_to_target_errno (host_val
)
512 for (m
= &errno_map
[0]; m
->host_val
; ++m
)
513 if (m
->host_val
== host_val
)
514 return m
->target_val
;
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. */
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. */
528 target_to_host_open (target_val
)
534 for (m
= &open_map
[0]; m
->host_val
!= -1; ++m
)
536 switch (m
->target_val
)
538 /* O_RDONLY can be (and usually is) 0 which needs to be treated
540 case TARGET_O_RDONLY
:
541 case TARGET_O_WRONLY
:
543 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
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
;
553 if ((m
->target_val
& target_val
) == m
->target_val
)
554 host_val
|= m
->host_val
;
562 /* Cover functions to the vfprintf callbacks.
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. */
572 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
577 p
->vprintf_filtered (p
, fmt
, ap
);
582 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
587 p
->evprintf_filtered (p
, fmt
, ap
);