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 int os_write
PARAMS ((host_callback
*, int, const char *, int));
57 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
58 static int os_read
PARAMS ((host_callback
*, int, char *, int));
59 static int os_open
PARAMS ((host_callback
*, const char *, int));
60 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
61 static int os_isatty
PARAMS ((host_callback
*, int));
62 static int os_get_errno
PARAMS ((host_callback
*));
63 static int os_close
PARAMS ((host_callback
*, int));
64 static int fdmap
PARAMS ((host_callback
*, int));
65 static int fdbad
PARAMS ((host_callback
*, int));
66 static int wrap
PARAMS ((host_callback
*, int));
68 /* Set the callback copy of errno from what we see now. */
75 p
->last_errno
= errno
;
79 /* Make sure the FD provided is ok. If not, return non-zero
87 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
89 p
->last_errno
= EINVAL
;
110 result
= fdbad (p
, fd
);
113 result
= wrap (p
, close (fdmap (p
, fd
)));
121 return host_to_target_errno (p
->last_errno
);
132 result
= fdbad (p
, fd
);
135 result
= wrap (p
, isatty (fdmap (p
, fd
)));
140 os_lseek (p
, fd
, off
, way
)
148 result
= fdbad (p
, fd
);
151 result
= lseek (fdmap (p
, fd
), off
, way
);
156 os_open (p
, name
, flags
)
162 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
166 int f
= open (name
, target_to_host_open (flags
));
169 p
->last_errno
= errno
;
177 p
->last_errno
= EMFILE
;
182 os_read (p
, fd
, buf
, len
)
190 result
= fdbad (p
, fd
);
193 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
198 os_read_stdin (p
, buf
, len
)
203 return wrap (p
, read (0, buf
, len
));
207 os_write (p
, fd
, buf
, len
)
215 result
= fdbad (p
, fd
);
218 result
= wrap (p
, write (fdmap (p
, fd
), buf
, len
));
223 os_write_stdout (p
, buf
, len
)
228 return os_write (p
, 1, buf
, len
);
232 os_rename (p
, f1
, f2
)
237 return wrap (p
, rename (f1
, f2
));
246 return wrap (p
, system (s
));
254 return wrap (p
, time (t
));
263 return wrap (p
, unlink (f1
));
272 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
274 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
288 for (i
= 0; i
< 3; i
++)
292 p
->alwaysopen
[i
] = 1;
299 #ifdef ANSI_PROTOTYPES
300 os_printf_filtered (host_callback
*p
, const char *format
, ...)
302 os_printf_filtered (p
, va_alist
)
308 #ifdef ANSI_PROTOTYPES
309 va_start (args
, format
);
314 format
= va_arg (args
, char *);
317 vprintf (format
, args
);
324 #ifdef ANSI_PROTOTYPES
325 os_error (host_callback
*p
, const char *format
, ...)
327 os_error (p
, va_alist
)
333 #ifdef ANSI_PROTOTYPES
334 va_start (args
, format
);
339 format
= va_arg (args
, char *);
342 vfprintf (stderr
, format
, args
);
343 fprintf (stderr
, "\n");
349 host_callback default_callback
=
374 /* FIXME: Need to add hooks so target can tweak as necessary. */
376 /* FIXME: struct stat conversion is missing. */
378 /* FIXME: sort tables if large.
379 Alternatively, an obvious improvement for errno conversion is
380 to machine generate a function with a large switch(). */
383 host_to_target_errno (host_val
)
388 for (m
= &errno_map
[0]; m
->host_val
; ++m
)
389 if (m
->host_val
== host_val
)
390 return m
->target_val
;
392 /* ??? Which error to return in this case is up for grabs.
393 Note that some missing values may have standard alternatives.
394 For now return 0 and require caller to deal with it. */
398 /* Given a set of target bitmasks for the open system call,
399 return the host equivalent.
400 Mapping open flag values is best done by looping so there's no need
401 to machine generate this function. */
404 target_to_host_open (target_val
)
410 for (m
= &open_map
[0]; m
->host_val
!= -1; ++m
)
412 switch (m
->target_val
)
414 /* O_RDONLY can be (and usually is) 0 which needs to be treated
416 case TARGET_O_RDONLY
:
417 case TARGET_O_WRONLY
:
419 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
421 host_val
|= m
->host_val
;
424 if ((m
->target_val
& target_val
) == m
->target_val
)
425 host_val
|= m
->host_val
;