def handle_stdlib_h(self, state): state.macros["EXIT_SUCCESS"] = Macro(rightside="0") state.macros["EXIT_FAILURE"] = Macro(rightside="1") state.funcs["abort"] = CWrapValue(lambda: self.interpreter._abort(), returnType=CVoidType, name="abort") state.funcs["exit"] = CWrapValue( lambda s: self.interpreter._exit(s.value), returnType=CVoidType, name="exit" # int ) state.funcs["malloc"] = CWrapValue( lambda s: self.interpreter._malloc(s.value), returnType=ctypes.c_void_p, name="malloc" # size_t ) state.funcs["realloc"] = CWrapValue( lambda (p, s): self.interpreter._realloc(_ctype_ptr_get_value(p), s.value), # void*, size_t returnType=ctypes.c_void_p, name="realloc", ) state.funcs["free"] = CWrapValue( lambda p: self.interpreter._free(_ctype_ptr_get_value(p)), returnType=CVoidType, name="free" # void* ) wrapCFunc( state, "strtoul", restype=ctypes.c_ulong, argtypes=(ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.c_int), ) state.funcs["atoi"] = CWrapValue( lambda x: ctypes.c_int(int(ctypes.cast(x, ctypes.c_char_p).value)), returnType=ctypes.c_int, name="atoi" ) state.funcs["getenv"] = CWrapValue( lambda x: _fixCArg(ctypes.c_char_p(os.getenv(ctypes.cast(x, ctypes.c_char_p).value))), returnType=CPointerType(ctypes.c_byte), name="getenv", )
def signal(sig, f): sig = sig.value import signal if isinstance(f, CWrapValue): f = f.value def sig_handler(sig, stack_frame): return f(sig) if isinstance(f, ctypes._CFuncPtr): if _ctype_ptr_get_value(f) == 0: # place-holder for SIG_DFL sig_handler = signal.SIG_DFL elif _ctype_ptr_get_value(f) == 1: # place-holder for SIG_IGN sig_handler = signal.SIG_IGN old_action = signal.signal(sig, sig_handler) # TODO: need to use helpers.makeFuncPtr for old_action. # And maybe handle SIG_DFL/SIG_IGN cases? return 0 # place-holder for SIG_DFL
def handle_stdlib_h(self, state): state.macros["EXIT_SUCCESS"] = Macro(rightside="0") state.macros["EXIT_FAILURE"] = Macro(rightside="1") state.funcs["abort"] = CWrapValue( lambda: self.interpreter._abort(), returnType=CVoidType, name="abort" ) state.funcs["exit"] = CWrapValue( lambda s: self.interpreter._exit(s.value), # int returnType=CVoidType, name="exit" ) state.funcs["malloc"] = CWrapValue( lambda s: self.interpreter._malloc(s.value), # size_t returnType=ctypes.c_void_p, name="malloc" ) state.funcs["realloc"] = CWrapValue( lambda ps: self.interpreter._realloc(_ctype_ptr_get_value(ps[0]), ps[1].value), # void*, size_t returnType=ctypes.c_void_p, name="realloc" ) state.funcs["free"] = CWrapValue( lambda p: self.interpreter._free(_ctype_ptr_get_value(p)), # void* returnType=CVoidType, name="free" ) wrapCFunc(state, "strtoul", restype=ctypes.c_ulong, argtypes=(ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.c_int)) state.funcs["atoi"] = CWrapValue( lambda x: ctypes.c_int(int(ctypes.cast(x, ctypes.c_char_p).value)), returnType=ctypes.c_int, name="atoi" ) state.funcs["getenv"] = CWrapValue( lambda x: _fixCArg(ctypes.c_char_p(os.getenv(ctypes.cast(x, ctypes.c_char_p).value))), returnType=CPointerType(ctypes.c_byte), name="getenv" )