1 /* Host callback routines for GDB.
2 Copyright 1995 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. */
22 /* This file provides a standard way for targets to talk to the host OS
25 This interface will probably need a bit more banging to make it
26 smooth. Currently the simulator uses this file to provide the
27 callbacks for itself when it's built standalone, which is rather
30 #ifndef INSIDE_SIMULATOR
36 #ifdef ANSI_PROTOTYPES
47 static int os_init
PARAMS ((host_callback
*));
48 static int os_shutdown
PARAMS ((host_callback
*));
49 static int os_unlink
PARAMS ((host_callback
*, const char *));
50 static long os_time
PARAMS ((host_callback
*, long *));
51 static int os_system
PARAMS ((host_callback
*, const char *));
52 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
53 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
54 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
55 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
56 static int os_read
PARAMS ((host_callback
*, int, char *, int));
57 static int os_open
PARAMS ((host_callback
*, const char *, int));
58 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
59 static int os_isatty
PARAMS ((host_callback
*, int));
60 static int os_get_errno
PARAMS ((host_callback
*));
61 static int os_close
PARAMS ((host_callback
*, int));
62 static int fdmap
PARAMS ((host_callback
*, int));
63 static int fdbad
PARAMS ((host_callback
*, int));
64 static int wrap
PARAMS ((host_callback
*, int));
66 /* Set the callback copy of errno from what we see now. */
72 p
->last_errno
= errno
;
76 /* Make sure the FD provided is ok. If not, return non-zero
84 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
86 p
->last_errno
= EINVAL
;
107 result
= fdbad (p
, fd
);
110 result
= wrap (p
, close (fdmap (p
, fd
)));
118 /* !!! fixme, translate from host to taget errno value */
119 return p
->last_errno
;
130 result
= fdbad (p
, fd
);
133 result
= wrap (p
, isatty (fdmap (p
, fd
)));
138 os_lseek (p
, fd
, off
, way
)
146 result
= fdbad (p
, fd
);
149 result
= lseek (fdmap (p
, fd
), off
, way
);
154 os_open (p
, name
, flags
)
160 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
164 int f
= open (name
, flags
);
167 p
->last_errno
= errno
;
175 p
->last_errno
= EMFILE
;
180 os_read (p
, fd
, buf
, len
)
188 result
= fdbad (p
, fd
);
191 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
196 os_read_stdin (p
, buf
, len
)
201 return wrap (p
, read (0, buf
, len
));
205 os_write (p
, fd
, buf
, len
)
213 result
= fdbad (p
, fd
);
216 result
= wrap (p
, write (fdmap (p
, fd
), buf
, len
));
220 /* ignore the grossness of INSIDE_SIMULATOR, it will go away one day. */
223 os_write_stdout (p
, buf
, len
)
228 #ifdef INSIDE_SIMULATOR
229 return os_write (p
, 1, buf
, len
);
233 for (i
= 0; i
< len
; i
++)
237 if (target_output_hook
)
238 target_output_hook (b
);
240 fputs_filtered (b
, gdb_stdout
);
247 os_rename (p
, f1
, f2
)
252 return wrap (p
, rename (f1
, f2
));
261 return wrap (p
, system (s
));
269 return wrap (p
, time (t
));
278 return wrap (p
, unlink (f1
));
287 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
289 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
303 for (i
= 0; i
< 3; i
++)
307 p
->alwaysopen
[i
] = 1;
314 This bit is ugly. When the interface has settled down I'll
315 move the whole file into sim/common and remove this bit. */
319 #ifdef ANSI_PROTOTYPES
320 os_printf_filtered (host_callback
*p
, const char *format
, ...)
322 os_printf_filtered (p
, va_alist
)
328 #ifdef ANSI_PROTOTYPES
329 va_start (args
, format
);
334 format
= va_arg (args
, char *);
337 #ifdef INSIDE_SIMULATOR
338 vprintf (format
, args
);
340 vfprintf_filtered (stdout
, format
, args
);
346 host_callback default_callback
=