/* -*- Mode: C -*- Scheme48 Stubber C Support Library This code is written by Taylor R. Campbell and placed in the Public Domain. All warranties are disclaimed. */ #include "scheme48.h" #include "s48-stubber.h" #include #include #include #include #include void s48_enter_channel (s48_value *channel_s48_loc, int fd, s48_value status_s48, s48_value id_s48) { int flags, status; STD_UINT_SYSTEM_CALL (syscall_fcntl_read, flags, (fcntl (fd, F_GETFL, 0))); if (0 == (flags & O_NONBLOCK)) { flags |= O_NONBLOCK; STD_UINT_SYSTEM_CALL (syscall_fcntl_write, status, (fcntl (fd, F_SETFL, flags))); } { S48_DECLARE_GC_PROTECT (2); S48_GC_PROTECT_2 (status_s48, id_s48); (* channel_s48_loc) = (s48_add_channel (status_s48, id_s48, fd)); if (! (S48_CHANNEL_P ((* channel_s48_loc)))) { /* Exception index if not channel */ ps_close_fd (((long) fd)); s48_raise_scheme_exception ((s48_extract_fixnum ((* channel_s48_loc))), 0); } S48_GC_UNPROTECT (); } } char * s48_extract_and_copy_string (s48_value string_s48) { char * string = (s48_extract_string (string_s48)); /* Add on one to NUL-terminate. */ size_t length = (1 + (S48_STRING_LENGTH (string_s48))); char * copy = (malloc (length * (sizeof (char)))); if (NULL == copy) { s48_raise_out_of_memory_error (); return NULL; /* NOT REACHED */ } else { strncpy (copy, string, length); return copy; } } char * s48_extract_and_copy_byte_vector (s48_value bv_s48, size_t * length_loc) { char * bv = (s48_extract_byte_vector (bv_s48)); size_t length = (S48_BYTE_VECTOR_LENGTH (bv_s48)); char * copy = (malloc (length * (sizeof (char)))); if (NULL == copy) { s48_raise_out_of_memory_error (); return NULL; /* NOT REACHED */ } else { memcpy (copy, bv, length); (* length_loc) = length; return copy; } } s48_value s48_enter_and_free_string (char * string) { s48_value string_s48 = (s48_enter_string (string)); free (string); return string_s48; } s48_value s48_enter_and_free_byte_vector (char * bv, long length) { s48_value bv_s48 = (s48_enter_byte_vector (bv, length)); free (bv); return bv_s48; }