📄 callback.c
字号:
static intos_shutdown (p) host_callback *p;{ int i, next, j; for (i = 0; i < MAX_CALLBACK_FDS; i++) { int do_close = 1; next = p->fd_buddy[i]; if (next < 0) continue; do { j = next; if (j == MAX_CALLBACK_FDS) do_close = 0; next = p->fd_buddy[j]; p->fd_buddy[j] = -1; /* At the initial call of os_init, we got -1, 0, 0, 0, ... */ if (next < 0) { p->fd_buddy[i] = -1; do_close = 0; break; } } while (j != i); if (do_close) close (p->fdmap[i]); } return 1;}static intos_init (p) host_callback *p;{ int i; os_shutdown (p); for (i = 0; i < 3; i++) { p->fdmap[i] = i; p->fd_buddy[i] = i - 1; } p->fd_buddy[0] = MAX_CALLBACK_FDS; p->fd_buddy[MAX_CALLBACK_FDS] = 2; p->syscall_map = cb_init_syscall_map; p->errno_map = cb_init_errno_map; p->open_map = cb_init_open_map; return 1;}/* DEPRECATED *//* VARARGS */static void#ifdef ANSI_PROTOTYPESos_printf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)#elseos_printf_filtered (p, va_alist) host_callback *p; va_dcl#endif{ va_list args;#ifdef ANSI_PROTOTYPES va_start (args, format);#else char *format; va_start (args); format = va_arg (args, char *);#endif vfprintf (stdout, format, args); va_end (args);}/* VARARGS */static void#ifdef ANSI_PROTOTYPESos_vprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)#elseos_vprintf_filtered (p, format, args) host_callback *p; const char *format; va_list args;#endif{ vprintf (format, args);}/* VARARGS */static void#ifdef ANSI_PROTOTYPESos_evprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)#elseos_evprintf_filtered (p, format, args) host_callback *p; const char *format; va_list args;#endif{ vfprintf (stderr, format, args);}/* VARARGS */static void#ifdef ANSI_PROTOTYPESos_error (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)#elseos_error (p, va_alist) host_callback *p; va_dcl#endif{ va_list args;#ifdef ANSI_PROTOTYPES va_start (args, format);#else char *format; va_start (args); format = va_arg (args, char *);#endif vfprintf (stderr, format, args); fprintf (stderr, "\n"); va_end (args); exit (1);}host_callback default_callback ={ os_close, os_get_errno, os_isatty, os_lseek, os_open, os_read, os_read_stdin, os_rename, os_system, os_time, os_unlink, os_write, os_write_stdout, os_flush_stdout, os_write_stderr, os_flush_stderr, os_stat, os_fstat, os_ftruncate, os_truncate, os_poll_quit, os_shutdown, os_init, os_printf_filtered, /* deprecated */ os_vprintf_filtered, os_evprintf_filtered, os_error, 0, /* last errno */ { 0, }, /* fdmap */ { -1, }, /* fd_buddy */ 0, /* syscall_map */ 0, /* errno_map */ 0, /* open_map */ 0, /* signal_map */ 0, /* stat_map */ HOST_CALLBACK_MAGIC,};/* Read in a file describing the target's system call values. E.g. maybe someone will want to use something other than newlib. This assumes that the basic system call recognition and value passing/ returning is supported. So maybe some coding/recompilation will be necessary, but not as much. If an error occurs, the existing mapping is not changed. */CB_RCcb_read_target_syscall_maps (cb, file) host_callback *cb; const char *file;{ CB_TARGET_DEFS_MAP *syscall_map, *errno_map, *open_map, *signal_map; const char *stat_map; FILE *f; if ((f = fopen (file, "r")) == NULL) return CB_RC_ACCESS; /* ... read in and parse file ... */ fclose (f); return CB_RC_NO_MEM; /* FIXME:wip */ /* Free storage allocated for any existing maps. */ if (cb->syscall_map) free (cb->syscall_map); if (cb->errno_map) free (cb->errno_map); if (cb->open_map) free (cb->open_map); if (cb->signal_map) free (cb->signal_map); if (cb->stat_map) free ((PTR) cb->stat_map); cb->syscall_map = syscall_map; cb->errno_map = errno_map; cb->open_map = open_map; cb->signal_map = signal_map; cb->stat_map = stat_map; return CB_RC_OK;}/* Translate the target's version of a syscall number to the host's. This isn't actually the host's version, rather a canonical form. ??? Perhaps this should be renamed to ..._canon_syscall. */intcb_target_to_host_syscall (cb, target_val) host_callback *cb; int target_val;{ CB_TARGET_DEFS_MAP *m; for (m = &cb->syscall_map[0]; m->target_val != -1; ++m) if (m->target_val == target_val) return m->host_val; return -1;}/* FIXME: sort tables if large. Alternatively, an obvious improvement for errno conversion is to machine generate a function with a large switch(). *//* Translate the host's version of errno to the target's. */intcb_host_to_target_errno (cb, host_val) host_callback *cb; int host_val;{ CB_TARGET_DEFS_MAP *m; for (m = &cb->errno_map[0]; m->host_val; ++m) if (m->host_val == host_val) return m->target_val; /* ??? Which error to return in this case is up for grabs. Note that some missing values may have standard alternatives. For now return 0 and require caller to deal with it. */ return 0;}/* Given a set of target bitmasks for the open system call, return the host equivalent. Mapping open flag values is best done by looping so there's no need to machine generate this function. */intcb_target_to_host_open (cb, target_val) host_callback *cb; int target_val;{ int host_val = 0; CB_TARGET_DEFS_MAP *m; for (m = &cb->open_map[0]; m->host_val != -1; ++m) { switch (m->target_val) { /* O_RDONLY can be (and usually is) 0 which needs to be treated specially. */ case TARGET_O_RDONLY : case TARGET_O_WRONLY : case TARGET_O_RDWR : if ((target_val & (TARGET_O_RDONLY | TARGET_O_WRONLY | TARGET_O_RDWR)) == m->target_val) host_val |= m->host_val; /* Handle the host/target differentiating between binary and text mode. Only one case is of importance */#if ! defined (TARGET_O_BINARY) && defined (O_BINARY) host_val |= O_BINARY;#endif break; default : if ((m->target_val & target_val) == m->target_val) host_val |= m->host_val; break; } } return host_val;}/* Utility for cb_host_to_target_stat to store values in the target's stat struct. */static voidstore (p, size, val, big_p) char *p; int size; long val; /* ??? must be as big as target word size */ int big_p;{ if (big_p) { p += size; while (size-- > 0) { *--p = val; val >>= 8; } } else { while (size-- > 0) { *p++ = val; val >>= 8; } }}/* Translate a host's stat struct into a target's. If HS is NULL, just compute the length of the buffer required, TS is ignored. The result is the size of the target's stat struct, or zero if an error occurred during the translation. */intcb_host_to_target_stat (cb, hs, ts) host_callback *cb; const struct stat *hs; PTR ts;{ const char *m = cb->stat_map; char *p; int big_p = 0; if (hs == NULL) ts = NULL; p = ts; while (m) { char *q = strchr (m, ','); int size; /* FIXME: Use sscanf? */ if (q == NULL) { /* FIXME: print error message */ return 0; } size = atoi (q + 1); if (size == 0) { /* FIXME: print error message */ return 0; } if (hs != NULL) { if (strncmp (m, "st_dev", q - m) == 0) store (p, size, hs->st_dev, big_p); else if (strncmp (m, "st_ino", q - m) == 0) store (p, size, hs->st_ino, big_p); /* FIXME:wip */ else store (p, size, 0, big_p); /* unsupported field, store 0 */ } p += size; m = strchr (q, ':'); if (m) ++m; } return p - (char *) ts;}/* Cover functions to the vfprintf callbacks. ??? If one thinks of the callbacks as a subsystem onto itself [or part of a larger "remote target subsystem"] with a well defined interface, then one would think that the subsystem would provide these. However, until one is allowed to create such a subsystem (with its own source tree independent of any particular user), such a critter can't exist. Thus these functions are here for the time being. */voidsim_cb_printf (host_callback *p, const char *fmt, ...){ va_list ap; va_start (ap, fmt); p->vprintf_filtered (p, fmt, ap); va_end (ap);}voidsim_cb_eprintf (host_callback *p, const char *fmt, ...){ va_list ap; va_start (ap, fmt); p->evprintf_filtered (p, fmt, ap); va_end (ap);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -