No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / sim / common / callback.c
blobabab4a8c6cd5b4a920d818b6750cca4bef08e660
1 /* Remote target callback routines.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free Software
20 Foundation, 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
23 level. */
25 #ifdef HAVE_CONFIG_H
26 #include "cconfig.h"
27 #endif
28 #include "ansidecl.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #else
37 #ifdef HAVE_STRINGS_H
38 #include <strings.h>
39 #endif
40 #endif
41 #ifdef HAVE_LIMITS_H
42 /* For PIPE_BUF. */
43 #include <limits.h>
44 #endif
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <time.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include "gdb/callback.h"
51 #include "targ-vals.h"
52 /* For xmalloc. */
53 #include "libiberty.h"
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
59 #ifndef PIPE_BUF
60 #define PIPE_BUF 512
61 #endif
63 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
64 broken out of the simulator directory, these are here to not require
65 sim-utils.h. */
66 void sim_cb_printf PARAMS ((host_callback *, const char *, ...));
67 void sim_cb_eprintf PARAMS ((host_callback *, const char *, ...));
69 extern CB_TARGET_DEFS_MAP cb_init_syscall_map[];
70 extern CB_TARGET_DEFS_MAP cb_init_errno_map[];
71 extern CB_TARGET_DEFS_MAP cb_init_open_map[];
73 extern int system PARAMS ((const char *));
75 static int os_init PARAMS ((host_callback *));
76 static int os_shutdown PARAMS ((host_callback *));
77 static int os_unlink PARAMS ((host_callback *, const char *));
78 static long os_time PARAMS ((host_callback *, long *));
79 static int os_system PARAMS ((host_callback *, const char *));
80 static int os_rename PARAMS ((host_callback *, const char *, const char *));
81 static int os_write_stdout PARAMS ((host_callback *, const char *, int));
82 static void os_flush_stdout PARAMS ((host_callback *));
83 static int os_write_stderr PARAMS ((host_callback *, const char *, int));
84 static void os_flush_stderr PARAMS ((host_callback *));
85 static int os_write PARAMS ((host_callback *, int, const char *, int));
86 static int os_read_stdin PARAMS ((host_callback *, char *, int));
87 static int os_read PARAMS ((host_callback *, int, char *, int));
88 static int os_open PARAMS ((host_callback *, const char *, int));
89 static int os_lseek PARAMS ((host_callback *, int, long, int));
90 static int os_isatty PARAMS ((host_callback *, int));
91 static int os_get_errno PARAMS ((host_callback *));
92 static int os_close PARAMS ((host_callback *, int));
93 static void os_vprintf_filtered PARAMS ((host_callback *, const char *, va_list));
94 static void os_evprintf_filtered PARAMS ((host_callback *, const char *, va_list));
95 static void os_error PARAMS ((host_callback *, const char *, ...));
96 static int fdmap PARAMS ((host_callback *, int));
97 static int fdbad PARAMS ((host_callback *, int));
98 static int wrap PARAMS ((host_callback *, int));
100 /* Set the callback copy of errno from what we see now. */
102 static int
103 wrap (p, val)
104 host_callback *p;
105 int val;
107 p->last_errno = errno;
108 return val;
111 /* Make sure the FD provided is ok. If not, return non-zero
112 and set errno. */
114 static int
115 fdbad (p, fd)
116 host_callback *p;
117 int fd;
119 if (fd < 0 || fd > MAX_CALLBACK_FDS || p->fd_buddy[fd] < 0)
121 p->last_errno = EINVAL;
122 return -1;
124 return 0;
127 static int
128 fdmap (p, fd)
129 host_callback *p;
130 int fd;
132 return p->fdmap[fd];
135 static int
136 os_close (p, fd)
137 host_callback *p;
138 int fd;
140 int result;
141 int i, next;
143 result = fdbad (p, fd);
144 if (result)
145 return result;
146 /* If this file descripter has one or more buddies (originals /
147 duplicates from a dup), just remove it from the circular list. */
148 for (i = fd; (next = p->fd_buddy[i]) != fd; )
149 i = next;
150 if (fd != i)
151 p->fd_buddy[i] = p->fd_buddy[fd];
152 else
154 if (p->ispipe[fd])
156 int other = p->ispipe[fd];
157 int reader, writer;
159 if (other > 0)
161 /* Closing the read side. */
162 reader = fd;
163 writer = other;
165 else
167 /* Closing the write side. */
168 writer = fd;
169 reader = -other;
172 /* If there was data in the buffer, make a last "now empty"
173 call, then deallocate data. */
174 if (p->pipe_buffer[writer].buffer != NULL)
176 (*p->pipe_empty) (p, reader, writer);
177 free (p->pipe_buffer[writer].buffer);
178 p->pipe_buffer[writer].buffer = NULL;
181 /* Clear pipe data for this side. */
182 p->pipe_buffer[fd].size = 0;
183 p->ispipe[fd] = 0;
185 /* If this was the first close, mark the other side as the
186 only remaining side. */
187 if (fd != abs (other))
188 p->ispipe[abs (other)] = -other;
189 p->fd_buddy[fd] = -1;
190 return 0;
193 result = wrap (p, close (fdmap (p, fd)));
195 p->fd_buddy[fd] = -1;
197 return result;
201 /* taken from gdb/util.c:notice_quit() - should be in a library */
204 #if defined(__GO32__) || defined (_MSC_VER)
205 static int
206 os_poll_quit (p)
207 host_callback *p;
209 #if defined(__GO32__)
210 int kbhit ();
211 int getkey ();
212 if (kbhit ())
214 int k = getkey ();
215 if (k == 1)
217 return 1;
219 else if (k == 2)
221 return 1;
223 else
225 sim_cb_eprintf (p, "CTRL-A to quit, CTRL-B to quit harder\n");
228 #endif
229 #if defined (_MSC_VER)
230 /* NB - this will not compile! */
231 int k = win32pollquit();
232 if (k == 1)
233 return 1;
234 else if (k == 2)
235 return 1;
236 #endif
237 return 0;
239 #else
240 #define os_poll_quit 0
241 #endif /* defined(__GO32__) || defined(_MSC_VER) */
243 static int
244 os_get_errno (p)
245 host_callback *p;
247 return cb_host_to_target_errno (p, p->last_errno);
251 static int
252 os_isatty (p, fd)
253 host_callback *p;
254 int fd;
256 int result;
258 result = fdbad (p, fd);
259 if (result)
260 return result;
261 result = wrap (p, isatty (fdmap (p, fd)));
263 return result;
266 static int
267 os_lseek (p, fd, off, way)
268 host_callback *p;
269 int fd;
270 long off;
271 int way;
273 int result;
275 result = fdbad (p, fd);
276 if (result)
277 return result;
278 result = lseek (fdmap (p, fd), off, way);
279 return result;
282 static int
283 os_open (p, name, flags)
284 host_callback *p;
285 const char *name;
286 int flags;
288 int i;
289 for (i = 0; i < MAX_CALLBACK_FDS; i++)
291 if (p->fd_buddy[i] < 0)
293 int f = open (name, cb_target_to_host_open (p, flags), 0644);
294 if (f < 0)
296 p->last_errno = errno;
297 return f;
299 p->fd_buddy[i] = i;
300 p->fdmap[i] = f;
301 return i;
304 p->last_errno = EMFILE;
305 return -1;
308 static int
309 os_read (p, fd, buf, len)
310 host_callback *p;
311 int fd;
312 char *buf;
313 int len;
315 int result;
317 result = fdbad (p, fd);
318 if (result)
319 return result;
320 if (p->ispipe[fd])
322 int writer = p->ispipe[fd];
324 /* Can't read from the write-end. */
325 if (writer < 0)
327 p->last_errno = EBADF;
328 return -1;
331 /* Nothing to read if nothing is written. */
332 if (p->pipe_buffer[writer].size == 0)
333 return 0;
335 /* Truncate read request size to buffer size minus what's already
336 read. */
337 if (len > p->pipe_buffer[writer].size - p->pipe_buffer[fd].size)
338 len = p->pipe_buffer[writer].size - p->pipe_buffer[fd].size;
340 memcpy (buf, p->pipe_buffer[writer].buffer + p->pipe_buffer[fd].size,
341 len);
343 /* Account for what we just read. */
344 p->pipe_buffer[fd].size += len;
346 /* If we've read everything, empty and deallocate the buffer and
347 signal buffer-empty to client. (This isn't expected to be a
348 hot path in the simulator, so we don't hold on to the buffer.) */
349 if (p->pipe_buffer[fd].size == p->pipe_buffer[writer].size)
351 free (p->pipe_buffer[writer].buffer);
352 p->pipe_buffer[writer].buffer = NULL;
353 p->pipe_buffer[fd].size = 0;
354 p->pipe_buffer[writer].size = 0;
355 (*p->pipe_empty) (p, fd, writer);
358 return len;
361 result = wrap (p, read (fdmap (p, fd), buf, len));
362 return result;
365 static int
366 os_read_stdin (p, buf, len)
367 host_callback *p;
368 char *buf;
369 int len;
371 return wrap (p, read (0, buf, len));
374 static int
375 os_write (p, fd, buf, len)
376 host_callback *p;
377 int fd;
378 const char *buf;
379 int len;
381 int result;
382 int real_fd;
384 result = fdbad (p, fd);
385 if (result)
386 return result;
388 if (p->ispipe[fd])
390 int reader = -p->ispipe[fd];
392 /* Can't write to the read-end. */
393 if (reader < 0)
395 p->last_errno = EBADF;
396 return -1;
399 /* Can't write to pipe with closed read end.
400 FIXME: We should send a SIGPIPE. */
401 if (reader == fd)
403 p->last_errno = EPIPE;
404 return -1;
407 /* As a sanity-check, we bail out it the buffered contents is much
408 larger than the size of the buffer on the host. We don't want
409 to run out of memory in the simulator due to a target program
410 bug if we can help it. Unfortunately, regarding the value that
411 reaches the simulated program, it's no use returning *less*
412 than the requested amount, because cb_syscall loops calling
413 this function until the whole amount is done. */
414 if (p->pipe_buffer[fd].size + len > 10 * PIPE_BUF)
416 p->last_errno = EFBIG;
417 return -1;
420 p->pipe_buffer[fd].buffer
421 = xrealloc (p->pipe_buffer[fd].buffer, p->pipe_buffer[fd].size + len);
422 memcpy (p->pipe_buffer[fd].buffer + p->pipe_buffer[fd].size,
423 buf, len);
424 p->pipe_buffer[fd].size += len;
426 (*p->pipe_nonempty) (p, reader, fd);
427 return len;
430 real_fd = fdmap (p, fd);
431 switch (real_fd)
433 default:
434 result = wrap (p, write (real_fd, buf, len));
435 break;
436 case 1:
437 result = p->write_stdout (p, buf, len);
438 break;
439 case 2:
440 result = p->write_stderr (p, buf, len);
441 break;
443 return result;
446 static int
447 os_write_stdout (p, buf, len)
448 host_callback *p ATTRIBUTE_UNUSED;
449 const char *buf;
450 int len;
452 return fwrite (buf, 1, len, stdout);
455 static void
456 os_flush_stdout (p)
457 host_callback *p ATTRIBUTE_UNUSED;
459 fflush (stdout);
462 static int
463 os_write_stderr (p, buf, len)
464 host_callback *p ATTRIBUTE_UNUSED;
465 const char *buf;
466 int len;
468 return fwrite (buf, 1, len, stderr);
471 static void
472 os_flush_stderr (p)
473 host_callback *p ATTRIBUTE_UNUSED;
475 fflush (stderr);
478 static int
479 os_rename (p, f1, f2)
480 host_callback *p;
481 const char *f1;
482 const char *f2;
484 return wrap (p, rename (f1, f2));
488 static int
489 os_system (p, s)
490 host_callback *p;
491 const char *s;
493 return wrap (p, system (s));
496 static long
497 os_time (p, t)
498 host_callback *p;
499 long *t;
501 long v = (long)time(NULL);
503 if (t != NULL)
504 *t = v;
505 return wrap (p, v);
509 static int
510 os_unlink (p, f1)
511 host_callback *p;
512 const char *f1;
514 return wrap (p, unlink (f1));
517 static int
518 os_stat (p, file, buf)
519 host_callback *p;
520 const char *file;
521 struct stat *buf;
523 /* ??? There is an issue of when to translate to the target layout.
524 One could do that inside this function, or one could have the
525 caller do it. It's more flexible to let the caller do it, though
526 I'm not sure the flexibility will ever be useful. */
527 return wrap (p, stat (file, buf));
530 static int
531 os_fstat (p, fd, buf)
532 host_callback *p;
533 int fd;
534 struct stat *buf;
536 if (fdbad (p, fd))
537 return -1;
539 if (p->ispipe[fd])
541 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
542 time_t t = (*p->time) (p, NULL);
543 #endif
545 /* We have to fake the struct stat contents, since the pipe is
546 made up in the simulator. */
547 memset (buf, 0, sizeof (*buf));
549 #ifdef HAVE_STRUCT_STAT_ST_MODE
550 buf->st_mode = S_IFIFO;
551 #endif
553 /* If more accurate tracking than current-time is needed (for
554 example, on GNU/Linux we get accurate numbers), the p->time
555 callback (which may be something other than os_time) should
556 happen for each read and write, and we'd need to keep track of
557 atime, ctime and mtime. */
558 #ifdef HAVE_STRUCT_STAT_ST_ATIME
559 buf->st_atime = t;
560 #endif
561 #ifdef HAVE_STRUCT_STAT_ST_CTIME
562 buf->st_ctime = t;
563 #endif
564 #ifdef HAVE_STRUCT_STAT_ST_MTIME
565 buf->st_mtime = t;
566 #endif
567 return 0;
570 /* ??? There is an issue of when to translate to the target layout.
571 One could do that inside this function, or one could have the
572 caller do it. It's more flexible to let the caller do it, though
573 I'm not sure the flexibility will ever be useful. */
574 return wrap (p, fstat (fdmap (p, fd), buf));
577 static int
578 os_lstat (p, file, buf)
579 host_callback *p;
580 const char *file;
581 struct stat *buf;
583 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
584 #ifdef HAVE_LSTAT
585 return wrap (p, lstat (file, buf));
586 #else
587 return wrap (p, stat (file, buf));
588 #endif
591 static int
592 os_ftruncate (p, fd, len)
593 host_callback *p;
594 int fd;
595 long len;
597 int result;
599 result = fdbad (p, fd);
600 if (p->ispipe[fd])
602 p->last_errno = EINVAL;
603 return -1;
605 if (result)
606 return result;
607 #ifdef HAVE_FTRUNCATE
608 result = wrap (p, ftruncate (fdmap (p, fd), len));
609 #else
610 p->last_errno = EINVAL;
611 result = -1;
612 #endif
613 return result;
616 static int
617 os_truncate (p, file, len)
618 host_callback *p;
619 const char *file;
620 long len;
622 #ifdef HAVE_TRUNCATE
623 return wrap (p, truncate (file, len));
624 #else
625 p->last_errno = EINVAL;
626 return -1;
627 #endif
630 static int
631 os_pipe (p, filedes)
632 host_callback *p;
633 int *filedes;
635 int i;
637 /* We deliberately don't use fd 0. It's probably stdin anyway. */
638 for (i = 1; i < MAX_CALLBACK_FDS; i++)
640 int j;
642 if (p->fd_buddy[i] < 0)
643 for (j = i + 1; j < MAX_CALLBACK_FDS; j++)
644 if (p->fd_buddy[j] < 0)
646 /* Found two free fd:s. Set stat to allocated and mark
647 pipeness. */
648 p->fd_buddy[i] = i;
649 p->fd_buddy[j] = j;
650 p->ispipe[i] = j;
651 p->ispipe[j] = -i;
652 filedes[0] = i;
653 filedes[1] = j;
655 /* Poison the FD map to make bugs apparent. */
656 p->fdmap[i] = -1;
657 p->fdmap[j] = -1;
658 return 0;
662 p->last_errno = EMFILE;
663 return -1;
666 /* Stub functions for pipe support. They should always be overridden in
667 targets using the pipe support, but that's up to the target. */
669 /* Called when the simulator says that the pipe at (reader, writer) is
670 now empty (so the writer should leave its waiting state). */
672 static void
673 os_pipe_empty (p, reader, writer)
674 host_callback *p;
675 int reader;
676 int writer;
680 /* Called when the simulator says the pipe at (reader, writer) is now
681 non-empty (so the writer should wait). */
683 static void
684 os_pipe_nonempty (p, reader, writer)
685 host_callback *p;
686 int reader;
687 int writer;
691 static int
692 os_shutdown (p)
693 host_callback *p;
695 int i, next, j;
696 for (i = 0; i < MAX_CALLBACK_FDS; i++)
698 int do_close = 1;
700 /* Zero out all pipe state. Don't call callbacks for non-empty
701 pipes; the target program has likely terminated at this point
702 or we're called at initialization time. */
703 p->ispipe[i] = 0;
704 p->pipe_buffer[i].size = 0;
705 p->pipe_buffer[i].buffer = NULL;
707 next = p->fd_buddy[i];
708 if (next < 0)
709 continue;
712 j = next;
713 if (j == MAX_CALLBACK_FDS)
714 do_close = 0;
715 next = p->fd_buddy[j];
716 p->fd_buddy[j] = -1;
717 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
718 if (next < 0)
720 p->fd_buddy[i] = -1;
721 do_close = 0;
722 break;
725 while (j != i);
726 if (do_close)
727 close (p->fdmap[i]);
729 return 1;
732 static int
733 os_init (p)
734 host_callback *p;
736 int i;
738 os_shutdown (p);
739 for (i = 0; i < 3; i++)
741 p->fdmap[i] = i;
742 p->fd_buddy[i] = i - 1;
744 p->fd_buddy[0] = MAX_CALLBACK_FDS;
745 p->fd_buddy[MAX_CALLBACK_FDS] = 2;
747 p->syscall_map = cb_init_syscall_map;
748 p->errno_map = cb_init_errno_map;
749 p->open_map = cb_init_open_map;
751 return 1;
754 /* DEPRECATED */
756 /* VARARGS */
757 static void
758 os_printf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)
760 va_list args;
761 va_start (args, format);
763 vfprintf (stdout, format, args);
764 va_end (args);
767 /* VARARGS */
768 static void
769 os_vprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)
771 vprintf (format, args);
774 /* VARARGS */
775 static void
776 os_evprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)
778 vfprintf (stderr, format, args);
781 /* VARARGS */
782 static void
783 os_error (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)
785 va_list args;
786 va_start (args, format);
788 vfprintf (stderr, format, args);
789 fprintf (stderr, "\n");
791 va_end (args);
792 exit (1);
795 host_callback default_callback =
797 os_close,
798 os_get_errno,
799 os_isatty,
800 os_lseek,
801 os_open,
802 os_read,
803 os_read_stdin,
804 os_rename,
805 os_system,
806 os_time,
807 os_unlink,
808 os_write,
809 os_write_stdout,
810 os_flush_stdout,
811 os_write_stderr,
812 os_flush_stderr,
814 os_stat,
815 os_fstat,
816 os_lstat,
818 os_ftruncate,
819 os_truncate,
821 os_pipe,
822 os_pipe_empty,
823 os_pipe_nonempty,
825 os_poll_quit,
827 os_shutdown,
828 os_init,
830 os_printf_filtered, /* deprecated */
832 os_vprintf_filtered,
833 os_evprintf_filtered,
834 os_error,
836 0, /* last errno */
838 { 0, }, /* fdmap */
839 { -1, }, /* fd_buddy */
840 { 0, }, /* ispipe */
841 { { 0, 0 }, }, /* pipe_buffer */
843 0, /* syscall_map */
844 0, /* errno_map */
845 0, /* open_map */
846 0, /* signal_map */
847 0, /* stat_map */
849 /* Defaults expected to be overridden at initialization, where needed. */
850 BFD_ENDIAN_UNKNOWN, /* target_endian */
851 4, /* target_sizeof_int */
853 HOST_CALLBACK_MAGIC,
856 /* Read in a file describing the target's system call values.
857 E.g. maybe someone will want to use something other than newlib.
858 This assumes that the basic system call recognition and value passing/
859 returning is supported. So maybe some coding/recompilation will be
860 necessary, but not as much.
862 If an error occurs, the existing mapping is not changed. */
864 CB_RC
865 cb_read_target_syscall_maps (cb, file)
866 host_callback *cb;
867 const char *file;
869 CB_TARGET_DEFS_MAP *syscall_map, *errno_map, *open_map, *signal_map;
870 const char *stat_map;
871 FILE *f;
873 if ((f = fopen (file, "r")) == NULL)
874 return CB_RC_ACCESS;
876 /* ... read in and parse file ... */
878 fclose (f);
879 return CB_RC_NO_MEM; /* FIXME:wip */
881 /* Free storage allocated for any existing maps. */
882 if (cb->syscall_map)
883 free (cb->syscall_map);
884 if (cb->errno_map)
885 free (cb->errno_map);
886 if (cb->open_map)
887 free (cb->open_map);
888 if (cb->signal_map)
889 free (cb->signal_map);
890 if (cb->stat_map)
891 free ((PTR) cb->stat_map);
893 cb->syscall_map = syscall_map;
894 cb->errno_map = errno_map;
895 cb->open_map = open_map;
896 cb->signal_map = signal_map;
897 cb->stat_map = stat_map;
899 return CB_RC_OK;
902 /* Translate the target's version of a syscall number to the host's.
903 This isn't actually the host's version, rather a canonical form.
904 ??? Perhaps this should be renamed to ..._canon_syscall. */
907 cb_target_to_host_syscall (cb, target_val)
908 host_callback *cb;
909 int target_val;
911 CB_TARGET_DEFS_MAP *m;
913 for (m = &cb->syscall_map[0]; m->target_val != -1; ++m)
914 if (m->target_val == target_val)
915 return m->host_val;
917 return -1;
920 /* FIXME: sort tables if large.
921 Alternatively, an obvious improvement for errno conversion is
922 to machine generate a function with a large switch(). */
924 /* Translate the host's version of errno to the target's. */
927 cb_host_to_target_errno (cb, host_val)
928 host_callback *cb;
929 int host_val;
931 CB_TARGET_DEFS_MAP *m;
933 for (m = &cb->errno_map[0]; m->host_val; ++m)
934 if (m->host_val == host_val)
935 return m->target_val;
937 /* ??? Which error to return in this case is up for grabs.
938 Note that some missing values may have standard alternatives.
939 For now return 0 and require caller to deal with it. */
940 return 0;
943 /* Given a set of target bitmasks for the open system call,
944 return the host equivalent.
945 Mapping open flag values is best done by looping so there's no need
946 to machine generate this function. */
949 cb_target_to_host_open (cb, target_val)
950 host_callback *cb;
951 int target_val;
953 int host_val = 0;
954 CB_TARGET_DEFS_MAP *m;
956 for (m = &cb->open_map[0]; m->host_val != -1; ++m)
958 switch (m->target_val)
960 /* O_RDONLY can be (and usually is) 0 which needs to be treated
961 specially. */
962 case TARGET_O_RDONLY :
963 case TARGET_O_WRONLY :
964 case TARGET_O_RDWR :
965 if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR))
966 == m->target_val)
967 host_val |= m->host_val;
968 /* Handle the host/target differentiating between binary and
969 text mode. Only one case is of importance */
970 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
971 host_val |= O_BINARY;
972 #endif
973 break;
974 default :
975 if ((m->target_val & target_val) == m->target_val)
976 host_val |= m->host_val;
977 break;
981 return host_val;
984 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
985 stat struct. */
987 void
988 cb_store_target_endian (cb, p, size, val)
989 host_callback *cb;
990 char *p;
991 int size;
992 long val; /* ??? must be as big as target word size */
994 if (cb->target_endian == BFD_ENDIAN_BIG)
996 p += size;
997 while (size-- > 0)
999 *--p = val;
1000 val >>= 8;
1003 else
1005 while (size-- > 0)
1007 *p++ = val;
1008 val >>= 8;
1013 /* Translate a host's stat struct into a target's.
1014 If HS is NULL, just compute the length of the buffer required,
1015 TS is ignored.
1017 The result is the size of the target's stat struct,
1018 or zero if an error occurred during the translation. */
1021 cb_host_to_target_stat (cb, hs, ts)
1022 host_callback *cb;
1023 const struct stat *hs;
1024 PTR ts;
1026 const char *m = cb->stat_map;
1027 char *p;
1029 if (hs == NULL)
1030 ts = NULL;
1031 p = ts;
1033 while (m)
1035 char *q = strchr (m, ',');
1036 int size;
1038 /* FIXME: Use sscanf? */
1039 if (q == NULL)
1041 /* FIXME: print error message */
1042 return 0;
1044 size = atoi (q + 1);
1045 if (size == 0)
1047 /* FIXME: print error message */
1048 return 0;
1051 if (hs != NULL)
1053 if (0)
1055 /* Defined here to avoid emacs indigestion on a lone "else". */
1056 #undef ST_x
1057 #define ST_x(FLD) \
1058 else if (strncmp (m, #FLD, q - m) == 0) \
1059 cb_store_target_endian (cb, p, size, hs->FLD)
1061 #ifdef HAVE_STRUCT_STAT_ST_DEV
1062 ST_x (st_dev);
1063 #endif
1064 #ifdef HAVE_STRUCT_STAT_ST_INO
1065 ST_x (st_ino);
1066 #endif
1067 #ifdef HAVE_STRUCT_STAT_ST_MODE
1068 ST_x (st_mode);
1069 #endif
1070 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1071 ST_x (st_nlink);
1072 #endif
1073 #ifdef HAVE_STRUCT_STAT_ST_UID
1074 ST_x (st_uid);
1075 #endif
1076 #ifdef HAVE_STRUCT_STAT_ST_GID
1077 ST_x (st_gid);
1078 #endif
1079 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1080 ST_x (st_rdev);
1081 #endif
1082 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1083 ST_x (st_size);
1084 #endif
1085 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1086 ST_x (st_blksize);
1087 #endif
1088 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1089 ST_x (st_blocks);
1090 #endif
1091 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1092 ST_x (st_atime);
1093 #endif
1094 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1095 ST_x (st_mtime);
1096 #endif
1097 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1098 ST_x (st_ctime);
1099 #endif
1100 #undef ST_x
1101 /* FIXME:wip */
1102 else
1103 /* Unsupported field, store 0. */
1104 cb_store_target_endian (cb, p, size, 0);
1107 p += size;
1108 m = strchr (q, ':');
1109 if (m)
1110 ++m;
1113 return p - (char *) ts;
1116 /* Cover functions to the vfprintf callbacks.
1118 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1119 a larger "remote target subsystem"] with a well defined interface, then
1120 one would think that the subsystem would provide these. However, until
1121 one is allowed to create such a subsystem (with its own source tree
1122 independent of any particular user), such a critter can't exist. Thus
1123 these functions are here for the time being. */
1125 void
1126 sim_cb_printf (host_callback *p, const char *fmt, ...)
1128 va_list ap;
1130 va_start (ap, fmt);
1131 p->vprintf_filtered (p, fmt, ap);
1132 va_end (ap);
1135 void
1136 sim_cb_eprintf (host_callback *p, const char *fmt, ...)
1138 va_list ap;
1140 va_start (ap, fmt);
1141 p->evprintf_filtered (p, fmt, ap);
1142 va_end (ap);