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
25 #ifdef ANSI_PROTOTYPES
38 #include "targ-vals.h"
40 static int os_init
PARAMS ((host_callback
*));
41 static int os_shutdown
PARAMS ((host_callback
*));
42 static int os_unlink
PARAMS ((host_callback
*, const char *));
43 static long os_time
PARAMS ((host_callback
*, long *));
44 static int os_system
PARAMS ((host_callback
*, const char *));
45 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
46 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
47 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
48 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
49 static int os_read
PARAMS ((host_callback
*, int, char *, int));
50 static int os_open
PARAMS ((host_callback
*, const char *, int));
51 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
52 static int os_isatty
PARAMS ((host_callback
*, int));
53 static int os_get_errno
PARAMS ((host_callback
*));
54 static int os_close
PARAMS ((host_callback
*, int));
55 static int fdmap
PARAMS ((host_callback
*, int));
56 static int fdbad
PARAMS ((host_callback
*, int));
57 static int wrap
PARAMS ((host_callback
*, int));
59 /* Set the callback copy of errno from what we see now. */
66 p
->last_errno
= errno
;
70 /* Make sure the FD provided is ok. If not, return non-zero
78 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
80 p
->last_errno
= EINVAL
;
101 result
= fdbad (p
, fd
);
104 result
= wrap (p
, close (fdmap (p
, fd
)));
112 return host_to_target_errno (p
->last_errno
);
123 result
= fdbad (p
, fd
);
126 result
= wrap (p
, isatty (fdmap (p
, fd
)));
131 os_lseek (p
, fd
, off
, way
)
139 result
= fdbad (p
, fd
);
142 result
= lseek (fdmap (p
, fd
), off
, way
);
147 os_open (p
, name
, flags
)
153 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
157 int f
= open (name
, target_to_host_open (flags
));
160 p
->last_errno
= errno
;
168 p
->last_errno
= EMFILE
;
173 os_read (p
, fd
, buf
, len
)
181 result
= fdbad (p
, fd
);
184 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
189 os_read_stdin (p
, buf
, len
)
194 return wrap (p
, read (0, buf
, len
));
198 os_write (p
, fd
, buf
, len
)
206 result
= fdbad (p
, fd
);
209 result
= wrap (p
, write (fdmap (p
, fd
), buf
, len
));
214 os_write_stdout (p
, buf
, len
)
219 return os_write (p
, 1, buf
, len
);
223 os_rename (p
, f1
, f2
)
228 return wrap (p
, rename (f1
, f2
));
237 return wrap (p
, system (s
));
245 return wrap (p
, time (t
));
254 return wrap (p
, unlink (f1
));
263 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
265 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
279 for (i
= 0; i
< 3; i
++)
283 p
->alwaysopen
[i
] = 1;
290 #ifdef ANSI_PROTOTYPES
291 os_printf_filtered (host_callback
*p
, const char *format
, ...)
293 os_printf_filtered (p
, va_alist
)
299 #ifdef ANSI_PROTOTYPES
300 va_start (args
, format
);
305 format
= va_arg (args
, char *);
308 vprintf (format
, args
);
315 #ifdef ANSI_PROTOTYPES
316 os_error (host_callback
*p
, const char *format
, ...)
318 os_error (p
, va_alist
)
324 #ifdef ANSI_PROTOTYPES
325 va_start (args
, format
);
330 format
= va_arg (args
, char *);
333 vfprintf (stderr
, format
, args
);
334 fprintf (stderr
, "\n");
340 host_callback default_callback
=
365 /* FIXME: Need to add hooks so target can tweak as necessary. */
367 /* FIXME: struct stat conversion is missing. */
369 /* FIXME: sort tables if large.
370 Alternatively, an obvious improvement for errno conversion is
371 to machine generate a function with a large switch(). */
374 host_to_target_errno (host_val
)
379 for (m
= &errno_map
[0]; m
->host_val
; ++m
)
380 if (m
->host_val
== host_val
)
381 return m
->target_val
;
383 /* ??? Which error to return in this case is up for grabs.
384 Note that some missing values may have standard alternatives.
385 For now return 0 and require caller to deal with it. */
389 /* Given a set of target bitmasks for the open system call,
390 return the host equivalent.
391 Mapping open flag values is best done by looping so there's no need
392 to machine generate this function. */
395 target_to_host_open (target_val
)
401 for (m
= &open_map
[0]; m
->host_val
!= -1; ++m
)
403 switch (m
->target_val
)
405 /* O_RDONLY can be (and usually is) 0 which needs to be treated
407 case TARGET_O_RDONLY
:
408 case TARGET_O_WRONLY
:
410 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
412 host_val
|= m
->host_val
;
415 if ((m
->target_val
& target_val
) == m
->target_val
)
416 host_val
|= m
->host_val
;