Silence warnings in ppc simulator
[deliverable/binutils-gdb.git] / sim / common / callback.c
1 /* Host callback routines for GDB.
2 Copyright 1995, 1996 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
20
21 /* This file provides a standard way for targets to talk to the host OS
22 level. */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include "ansidecl.h"
28 #ifdef ANSI_PROTOTYPES
29 #include <stdarg.h>
30 #else
31 #include <varargs.h>
32 #endif
33 #include <stdio.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <time.h>
40 #include "callback.h"
41 #include "targ-vals.h"
42
43 #ifndef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 extern int system PARAMS ((const char *));
48
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));
67
68 /* Set the callback copy of errno from what we see now. */
69
70 static int
71 wrap (p, val)
72 host_callback *p;
73 int val;
74 {
75 p->last_errno = errno;
76 return val;
77 }
78
79 /* Make sure the FD provided is ok. If not, return non-zero
80 and set errno. */
81
82 static int
83 fdbad (p, fd)
84 host_callback *p;
85 int fd;
86 {
87 if (fd < 0 || fd > MAX_CALLBACK_FDS || !p->fdopen[fd])
88 {
89 p->last_errno = EINVAL;
90 return -1;
91 }
92 return 0;
93 }
94
95 static int
96 fdmap (p, fd)
97 host_callback *p;
98 int fd;
99 {
100 return p->fdmap[fd];
101 }
102
103 static int
104 os_close (p, fd)
105 host_callback *p;
106 int fd;
107 {
108 int result;
109
110 result = fdbad (p, fd);
111 if (result)
112 return result;
113 result = wrap (p, close (fdmap (p, fd)));
114 return result;
115 }
116
117 static int
118 os_get_errno (p)
119 host_callback *p;
120 {
121 return host_to_target_errno (p->last_errno);
122 }
123
124
125 static int
126 os_isatty (p, fd)
127 host_callback *p;
128 int fd;
129 {
130 int result;
131
132 result = fdbad (p, fd);
133 if (result)
134 return result;
135 result = wrap (p, isatty (fdmap (p, fd)));
136 return result;
137 }
138
139 static int
140 os_lseek (p, fd, off, way)
141 host_callback *p;
142 int fd;
143 long off;
144 int way;
145 {
146 int result;
147
148 result = fdbad (p, fd);
149 if (result)
150 return result;
151 result = lseek (fdmap (p, fd), off, way);
152 return result;
153 }
154
155 static int
156 os_open (p, name, flags)
157 host_callback *p;
158 const char *name;
159 int flags;
160 {
161 int i;
162 for (i = 0; i < MAX_CALLBACK_FDS; i++)
163 {
164 if (!p->fdopen[i])
165 {
166 int f = open (name, target_to_host_open (flags));
167 if (f < 0)
168 {
169 p->last_errno = errno;
170 return f;
171 }
172 p->fdopen[i] = 1;
173 p->fdmap[i] = f;
174 return i;
175 }
176 }
177 p->last_errno = EMFILE;
178 return -1;
179 }
180
181 static int
182 os_read (p, fd, buf, len)
183 host_callback *p;
184 int fd;
185 char *buf;
186 int len;
187 {
188 int result;
189
190 result = fdbad (p, fd);
191 if (result)
192 return result;
193 result = wrap (p, read (fdmap (p, fd), buf, len));
194 return result;
195 }
196
197 static int
198 os_read_stdin (p, buf, len)
199 host_callback *p;
200 char *buf;
201 int len;
202 {
203 return wrap (p, read (0, buf, len));
204 }
205
206 static int
207 os_write (p, fd, buf, len)
208 host_callback *p;
209 int fd;
210 const char *buf;
211 int len;
212 {
213 int result;
214
215 result = fdbad (p, fd);
216 if (result)
217 return result;
218 result = wrap (p, write (fdmap (p, fd), buf, len));
219 return result;
220 }
221
222 static int
223 os_write_stdout (p, buf, len)
224 host_callback *p;
225 const char *buf;
226 int len;
227 {
228 return os_write (p, 1, buf, len);
229 }
230
231 static int
232 os_rename (p, f1, f2)
233 host_callback *p;
234 const char *f1;
235 const char *f2;
236 {
237 return wrap (p, rename (f1, f2));
238 }
239
240
241 static int
242 os_system (p, s)
243 host_callback *p;
244 const char *s;
245 {
246 return wrap (p, system (s));
247 }
248
249 static long
250 os_time (p, t)
251 host_callback *p;
252 long *t;
253 {
254 return wrap (p, time (t));
255 }
256
257
258 static int
259 os_unlink (p, f1)
260 host_callback *p;
261 const char *f1;
262 {
263 return wrap (p, unlink (f1));
264 }
265
266
267 static int
268 os_shutdown (p)
269 host_callback *p;
270 {
271 int i;
272 for (i = 0; i < MAX_CALLBACK_FDS; i++)
273 {
274 if (p->fdopen[i] && !p->alwaysopen[i]) {
275 close (p->fdmap[i]);
276 p->fdopen[i] = 0;
277 }
278 }
279 return 1;
280 }
281
282 static int
283 os_init(p)
284 host_callback *p;
285 {
286 int i;
287 os_shutdown (p);
288 for (i= 0; i < 3; i++)
289 {
290 p->fdmap[i] = i;
291 p->fdopen[i] = 1;
292 p->alwaysopen[i] = 1;
293 }
294 return 1;
295 }
296
297 /* VARARGS */
298 static void
299 #ifdef ANSI_PROTOTYPES
300 os_printf_filtered (host_callback *p, const char *format, ...)
301 #else
302 os_printf_filtered (p, va_alist)
303 host_callback *p;
304 va_dcl
305 #endif
306 {
307 va_list args;
308 #ifdef ANSI_PROTOTYPES
309 va_start (args, format);
310 #else
311 char *format;
312
313 va_start (args);
314 format = va_arg (args, char *);
315 #endif
316
317 vprintf (format, args);
318
319 va_end (args);
320 }
321
322 /* VARARGS */
323 static void
324 #ifdef ANSI_PROTOTYPES
325 os_error (host_callback *p, const char *format, ...)
326 #else
327 os_error (p, va_alist)
328 host_callback *p;
329 va_dcl
330 #endif
331 {
332 va_list args;
333 #ifdef ANSI_PROTOTYPES
334 va_start (args, format);
335 #else
336 char *format;
337
338 va_start (args);
339 format = va_arg (args, char *);
340 #endif
341
342 vfprintf (stderr, format, args);
343 fprintf (stderr, "\n");
344
345 va_end (args);
346 exit (1);
347 }
348
349 host_callback default_callback =
350 {
351 os_close,
352 os_get_errno,
353 os_isatty,
354 os_lseek,
355 os_open,
356 os_read,
357 os_read_stdin,
358 os_rename,
359 os_system,
360 os_time,
361 os_unlink,
362 os_write,
363 os_write_stdout,
364
365 os_shutdown,
366 os_init,
367
368 os_printf_filtered,
369 os_error,
370
371 0, /* last errno */
372 };
373 \f
374 /* FIXME: Need to add hooks so target can tweak as necessary. */
375
376 /* FIXME: struct stat conversion is missing. */
377
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(). */
381
382 int
383 host_to_target_errno (host_val)
384 int host_val;
385 {
386 target_defs_map *m;
387
388 for (m = &errno_map[0]; m->host_val; ++m)
389 if (m->host_val == host_val)
390 return m->target_val;
391
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. */
395 return 0;
396 }
397
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. */
402
403 int
404 target_to_host_open (target_val)
405 int target_val;
406 {
407 int host_val = 0;
408 target_defs_map *m;
409
410 for (m = &open_map[0]; m->host_val != -1; ++m)
411 {
412 switch (m->target_val)
413 {
414 /* O_RDONLY can be (and usually is) 0 which needs to be treated
415 specially. */
416 case TARGET_O_RDONLY :
417 case TARGET_O_WRONLY :
418 case TARGET_O_RDWR :
419 if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR))
420 == m->target_val)
421 host_val |= m->host_val;
422 break;
423 default :
424 if ((m->target_val & target_val) == m->target_val)
425 host_val |= m->host_val;
426 break;
427 }
428 }
429
430 return host_val;
431 }
This page took 0.04724 seconds and 5 git commands to generate.