def setup_rstart(rhome, args): rstart = ffi.new("Rstart") _protected["rstart"] = rstart SA_NORESTORE = 0 SA_RESTORE = 1 # SA_DEFAULT = 2 SA_NOSAVE = 3 SA_SAVE = 4 SA_SAVEASK = 5 # SA_SUICIDE = 6 lib.R_DefParams(rstart) rstart.R_Quiet = "--quiet" in args rstart.R_Slave = "--slave" in args rstart.R_Interactive = 1 rstart.R_Verbose = "--verbose" in args rstart.LoadSiteFile = "--no-site-file" not in args rstart.LoadInitFile = "--no-init-file" not in args if "--no-restore" in args: rstart.RestoreAction = SA_NORESTORE else: rstart.RestoreAction = SA_RESTORE if "--no-save" in args: rstart.SaveAction = SA_NOSAVE elif "--save" in args: rstart.SaveAction = SA_SAVE else: rstart.SaveAction = SA_SAVEASK rhome = ffi.new("char[]", rhome.encode("utf-8")) _protected["rhome"] = rhome rstart.rhome = rhome home = ffi.new("char[]", ffi.string(lib.getRUser())) _protected["home"] = home rstart.home = home rstart._ReadConsole = ffi.addressof(lib, "cb_read_console_interruptible") rstart._WriteConsole = ffi.NULL rstart.CallBack = ffi.addressof(lib, "cb_polled_events_interruptible") rstart.ShowMessage = ffi.addressof(lib, "cb_show_message") rstart.YesNoCancel = ffi.addressof(lib, "cb_yes_no_cancel") rstart.Busy = ffi.addressof(lib, "cb_busy") # we cannot get it to RGui, otherwise `do_system` will clear the standard handlers rstart.CharacterMode = 1 # RTerm rstart.WriteConsoleEx = ffi.addressof(lib, "cb_write_console_capturable") lib.R_SetParams(rstart)
def cb_read_console(p, buf, buflen, add_history): # cache the code as buflen is limited to 4096 if _code[0]: code = _code[0] else: text = callback.read_console(rconsole2str(ffi.string(p)), add_history) if text is None: return 0 code = utf8tosystem(text) _code[0] = code buf = ffi.cast("char*", buf) nb = min(len(code), buflen - 2) buf[0:nb] = code[0:nb] if nb < buflen - 2: buf[nb] = b'\n' buf[nb + 1] = b'\x00' _code[0] = _code[0][nb:] return 1
def load_constant_error(): return "Cannot load constant {}: {}".format( system2utf8(ffi.string(lib._libR_last_loaded_symbol())), system2utf8(ffi.string(lib._libR_dl_error_message())))
def load_lib_error(): return "Cannot load shared library: {}".format( system2utf8(ffi.string(lib._libR_dl_error_message())))
def rcopy(_, s): # noqa return ffi.string(lib.RAW(s), lib.Rf_length(s))
def _string(s): return text_type(ffi.string(lib.Rf_translateCharUTF8(s)).decode("utf-8"))
def cb_yes_no_cancel(p): return callback.yes_no_cancel(rconsole2str(ffi.string(p)))
def cb_write_console_capturable(buf, bufline, otype): text = rconsole2str(ffi.string(buf)) console.write_console(text, otype)
def cb_show_message(buf): callback.show_message(rconsole2str(ffi.string(buf)))