1 /* Host callback routines for GDB.
2 Copyright 1995, 1996 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 p->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)
211 for (i = 0; i < MAX_CALLBACK_FDS; i++)
215 int f = open (name, target_to_host_open (flags), 0644);
218 p->last_errno = errno;
226 p->last_errno = EMFILE;
231 os_read (p, fd, buf, len)
239 result = fdbad (p, fd);
242 result = wrap (p, read (fdmap (p, fd), buf, len));
247 os_read_stdin (p, buf, len)
252 return wrap (p, read (0, buf, len));
256 os_write (p, fd, buf, len)
264 result = fdbad (p, fd);
267 result = wrap (p, write (fdmap (p, fd), buf, len));
272 os_write_stdout (p, buf, len)
277 return fwrite(buf, 1, len, stdout);
288 os_write_stderr (p, buf, len)
293 return fwrite(buf, 1, len, stderr);
304 os_rename (p, f1, f2)
309 return wrap (p, rename (f1, f2));
318 return wrap (p, system (s));
326 return wrap (p, time (t));
335 return wrap (p, unlink (f1));
344 for (i = 0; i < MAX_CALLBACK_FDS; i++)
346 if (p->fdopen[i] && !p->alwaysopen[i]) {
360 for (i= 0; i < 3; i++)
364 p->alwaysopen[i] = 1;
373 #ifdef ANSI_PROTOTYPES
374 os_printf_filtered (host_callback *p, const char *format, ...)
376 os_printf_filtered (p, va_alist)
382 #ifdef ANSI_PROTOTYPES
383 va_start (args, format);
388 format = va_arg (args, char *);
391 vfprintf (stdout, format, args);
397 #ifdef ANSI_PROTOTYPES
398 os_vprintf_filtered (host_callback *p, const char *format, va_list args)
400 os_vprintf_filtered (p, format, args)
406 vprintf (format, args);
411 #ifdef ANSI_PROTOTYPES
412 os_evprintf_filtered (host_callback *p, const char *format, va_list args)
414 os_evprintf_filtered (p, format, args)
420 vfprintf (stderr, format, args);
425 #ifdef ANSI_PROTOTYPES
426 os_error (host_callback *p, const char *format, ...)
428 os_error (p, va_alist)
434 #ifdef ANSI_PROTOTYPES
435 va_start (args, format);
440 format = va_arg (args, char *);
443 vfprintf (stderr, format, args);
444 fprintf (stderr, "\n");
450 host_callback default_callback =
474 os_printf_filtered, /* depreciated */
477 os_evprintf_filtered,
484 { 0, }, /* alwaysopen */
489 /* FIXME: Need to add hooks so target can tweak as necessary. */
491 /* FIXME: struct stat conversion is missing. */
493 /* FIXME: sort tables if large.
494 Alternatively, an obvious improvement for errno conversion is
495 to machine generate a function with a large switch(). */
498 host_to_target_errno (host_val)
503 for (m = &errno_map[0]; m->host_val; ++m)
504 if (m->host_val == host_val)
505 return m->target_val;
507 /* ??? Which error to return in this case is up for grabs.
508 Note that some missing values may have standard alternatives.
509 For now return 0 and require caller to deal with it. */
513 /* Given a set of target bitmasks for the open system call,
514 return the host equivalent.
515 Mapping open flag values is best done by looping so there's no need
516 to machine generate this function. */
519 target_to_host_open (target_val)
525 for (m = &open_map[0]; m->host_val != -1; ++m)
527 switch (m->target_val)
529 /* O_RDONLY can be (and usually is) 0 which needs to be treated
531 case TARGET_O_RDONLY :
532 case TARGET_O_WRONLY :
534 if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR))
536 host_val |= m->host_val;
539 if ((m->target_val & target_val) == m->target_val)
540 host_val |= m->host_val;