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 extern int system
PARAMS ((const char *));
49 static int os_init
PARAMS ((host_callback
*));
50 static int os_shutdown
PARAMS ((host_callback
*));
51 static int os_unlink
PARAMS ((host_callback
*, const char *));
52 static long os_time
PARAMS ((host_callback
*, long *));
53 static int os_system
PARAMS ((host_callback
*, const char *));
54 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
55 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
56 static void os_flush_stdout
PARAMS ((host_callback
*));
57 static int os_write_stderr
PARAMS ((host_callback
*, const char *, int));
58 static void os_flush_stderr
PARAMS ((host_callback
*));
59 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
60 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
61 static int os_read
PARAMS ((host_callback
*, int, char *, int));
62 static int os_open
PARAMS ((host_callback
*, const char *, int));
63 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
64 static int os_isatty
PARAMS ((host_callback
*, int));
65 static int os_get_errno
PARAMS ((host_callback
*));
66 static int os_close
PARAMS ((host_callback
*, int));
67 static void os_vprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
68 static void os_evprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
69 static void os_error
PARAMS ((host_callback
*, const char *, ...));
70 static int fdmap
PARAMS ((host_callback
*, int));
71 static int fdbad
PARAMS ((host_callback
*, int));
72 static int wrap
PARAMS ((host_callback
*, int));
74 /* Set the callback copy of errno from what we see now. */
81 p
->last_errno
= errno
;
85 /* Make sure the FD provided is ok. If not, return non-zero
93 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
95 p
->last_errno
= EINVAL
;
116 result
= fdbad (p
, fd
);
119 result
= wrap (p
, close (fdmap (p
, fd
)));
120 if(result
== 0 && !p
->alwaysopen
[fd
])
127 /* taken from gdb/util.c - should be in a library */
130 #if defined(__GO32__) || defined (_WIN32)
149 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
152 #else /* !_MSC_VER */
153 /* NB - this will not compile! */
154 int k
= win32pollquit();
159 #endif /* !_MSC_VER */
163 #define os_poll_quit 0
164 #endif /* defined(__GO32__) || defined(_WIN32) */
170 return host_to_target_errno (p
->last_errno
);
181 result
= fdbad (p
, fd
);
184 result
= wrap (p
, isatty (fdmap (p
, fd
)));
190 os_lseek (p
, fd
, off
, way
)
198 result
= fdbad (p
, fd
);
201 result
= lseek (fdmap (p
, fd
), off
, way
);
206 os_open (p
, name
, flags
)
212 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
216 int f
= open (name
, target_to_host_open (flags
), 0644);
219 p
->last_errno
= errno
;
227 p
->last_errno
= EMFILE
;
232 os_read (p
, fd
, buf
, len
)
240 result
= fdbad (p
, fd
);
243 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
248 os_read_stdin (p
, buf
, len
)
253 return wrap (p
, read (0, buf
, len
));
257 os_write (p
, fd
, buf
, len
)
265 result
= fdbad (p
, fd
);
268 result
= wrap (p
, write (fdmap (p
, fd
), buf
, len
));
273 os_write_stdout (p
, buf
, len
)
278 return fwrite(buf
, 1, len
, stdout
);
289 os_write_stderr (p
, buf
, len
)
294 return fwrite(buf
, 1, len
, stderr
);
305 os_rename (p
, f1
, f2
)
310 return wrap (p
, rename (f1
, f2
));
319 return wrap (p
, system (s
));
327 return wrap (p
, time (t
));
336 return wrap (p
, unlink (f1
));
345 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
347 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
361 for (i
= 0; i
< 3; i
++)
365 p
->alwaysopen
[i
] = 1;
374 #ifdef ANSI_PROTOTYPES
375 os_printf_filtered (host_callback
*p
, const char *format
, ...)
377 os_printf_filtered (p
, va_alist
)
383 #ifdef ANSI_PROTOTYPES
384 va_start (args
, format
);
389 format
= va_arg (args
, char *);
392 vfprintf (stdout
, format
, args
);
398 #ifdef ANSI_PROTOTYPES
399 os_vprintf_filtered (host_callback
*p
, const char *format
, va_list args
)
401 os_vprintf_filtered (p
, format
, args
)
407 vprintf (format
, args
);
412 #ifdef ANSI_PROTOTYPES
413 os_evprintf_filtered (host_callback
*p
, const char *format
, va_list args
)
415 os_evprintf_filtered (p
, format
, args
)
421 vfprintf (stderr
, format
, args
);
426 #ifdef ANSI_PROTOTYPES
427 os_error (host_callback
*p
, const char *format
, ...)
429 os_error (p
, va_alist
)
435 #ifdef ANSI_PROTOTYPES
436 va_start (args
, format
);
441 format
= va_arg (args
, char *);
444 vfprintf (stderr
, format
, args
);
445 fprintf (stderr
, "\n");
451 host_callback default_callback
=
475 os_printf_filtered
, /* depreciated */
478 os_evprintf_filtered
,
485 { 0, }, /* alwaysopen */
490 /* FIXME: Need to add hooks so target can tweak as necessary. */
492 /* FIXME: struct stat conversion is missing. */
494 /* FIXME: sort tables if large.
495 Alternatively, an obvious improvement for errno conversion is
496 to machine generate a function with a large switch(). */
499 host_to_target_errno (host_val
)
504 for (m
= &errno_map
[0]; m
->host_val
; ++m
)
505 if (m
->host_val
== host_val
)
506 return m
->target_val
;
508 /* ??? Which error to return in this case is up for grabs.
509 Note that some missing values may have standard alternatives.
510 For now return 0 and require caller to deal with it. */
514 /* Given a set of target bitmasks for the open system call,
515 return the host equivalent.
516 Mapping open flag values is best done by looping so there's no need
517 to machine generate this function. */
520 target_to_host_open (target_val
)
526 for (m
= &open_map
[0]; m
->host_val
!= -1; ++m
)
528 switch (m
->target_val
)
530 /* O_RDONLY can be (and usually is) 0 which needs to be treated
532 case TARGET_O_RDONLY
:
533 case TARGET_O_WRONLY
:
535 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
537 host_val
|= m
->host_val
;
538 /* Handle the host/target differentiating between binary and
539 text mode. Only one case is of importance */
540 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
541 host_val
|= O_BINARY
;
545 if ((m
->target_val
& target_val
) == m
->target_val
)
546 host_val
|= m
->host_val
;
554 /* Cover functions to the vfprintf callbacks.
556 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
557 a larger "remote target subsystem"] with a well defined interface, then
558 one would think that the subsystem would provide these. However, until
559 one is allowed to create such a subsystem (with its own source tree
560 independent of any particular user), such a critter can't exist. Thus
561 these functions are here for the time being. */
564 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
569 p
->vprintf_filtered (p
, fmt
, ap
);
574 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
579 p
->evprintf_filtered (p
, fmt
, ap
);