Пример #1
0
void processEvents()
{
#ifdef __APPLE__
   R_ProcessEvents();

   // pickup X11 graphics device events (if any) via X11 input handler
   fd_set* what = R_checkActivity(0,1);
   if (what != NULL)
      R_runHandlers(R_InputHandlers, what);
#else
   // check for activity on standard input handlers (but ignore stdin).
   // return immediately if there is no input currently available
   fd_set* what = R_checkActivity(0,1);

   // run handlers on the input (or run the polled event handler if there
   // is no input currently available)
   R_runHandlers(R_InputHandlers, what);
#endif
}
Пример #2
0
void rcall_process_events()
{
    if (!R_is_ready) return;

    R_is_ready = 0;

    // FIXME: a dirty fix to prevent segfault right after a sigint
    R_interrupts_pending = 0;

#ifdef __APPLE__
    R_ProcessEvents();
#endif

    fd_set* what = R_checkActivity(0,1);
    if (what != NULL) R_runHandlers(R_InputHandlers, what);

    R_is_ready = 1;
}
Пример #3
0
	/* Note that q() uses stifle_history, but here we do not want
	 * to truncate the active history when saving during a session */
#ifdef HAVE_HISTORY_TRUNCATE_FILE
	R_setupHistory(); /* re-read the history size */
	err = history_truncate_file(file, R_HistorySize);
	if(err) warning(_("problem in truncating the history file"));
#endif
    } else errorcall(call, _("no history available to save"));
#else
    errorcall(call, _("no history available to save"));
#endif
}

void attribute_hidden Rstd_addhistory(SEXP call, SEXP op, SEXP args, SEXP env)
{
    SEXP stamp;
    int i;

    checkArity(op, args);
    stamp = CAR(args);
    if (!isString(stamp))
	errorcall(call, _("invalid timestamp"));
#if defined(HAVE_LIBREADLINE) && defined(HAVE_READLINE_HISTORY_H)
    if(R_Interactive && UsingReadline)
	for (i = 0; i < LENGTH(stamp); i++)
	    add_history(CHAR(STRING_ELT(stamp, i))); /* ASCII */
# endif
}


#define R_MIN(a, b) ((a) < (b) ? (a) : (b))

double currentTime(void); /* from datetime.c */
SEXP attribute_hidden do_syssleep(SEXP call, SEXP op, SEXP args, SEXP rho)
{
    int Timeout;
    double tm, timeint, start, elapsed;

    checkArity(op, args);
    timeint = asReal(CAR(args));
    if (ISNAN(timeint) || timeint < 0)
	errorcall(call, _("invalid '%s' value"), "time");
    tm = timeint * 1e6;

    start = currentTime();
    for (;;) {
	fd_set *what;
	tm = R_MIN(tm, 2e9); /* avoid integer overflow */
	
	int wt = -1;
	if (R_wait_usec > 0) wt = R_wait_usec;
	if (Rg_wait_usec > 0 && (wt < 0 || wt > Rg_wait_usec))
	    wt = Rg_wait_usec;
	Timeout = (int) (wt > 0 ? R_MIN(tm, wt) : tm);
	what = R_checkActivity(Timeout, 1);

	/* For polling, elapsed time limit ... */
	R_CheckUserInterrupt();
	/* Time up? */
	elapsed = currentTime() - start;
	if(elapsed >= timeint) break;

	/* Nope, service pending events */
	R_runHandlers(R_InputHandlers, what);

	/* Servicing events might take some time, so recheck: */
	elapsed = currentTime() - start;
	if(elapsed >= timeint) break;

	tm = 1e6*(timeint - elapsed);
    }

    return R_NilValue;
}
Пример #4
0
void
EmbeddedR_ProcessEvents()
{
  if (! RINTERF_HASARGSSET()) {
    printf("R should not process events before being initialized.");
    return;
  }
  if (RINTERF_ISBUSY()) {
    printf("Concurrent access to R is not allowed.");
    return;
  }
  // setlock
  RStatus = RStatus | RINTERF_ISBUSY();
#if defined(HAVE_AQUA) || (defined(Win32) || defined(Win64))
  /* Can the call to R_ProcessEvents somehow fail ? */
  R_ProcessEvents();
#endif
#if ! (defined(Win32) || defined(Win64))
  R_runHandlers(R_InputHandlers, R_checkActivity(0, 1));
#endif
  // freelock
  RStatus = RStatus ^ RINTERF_ISBUSY();
}
Пример #5
0
int attribute_hidden
Rstd_ReadConsole(const char *prompt, unsigned char *buf, int len,
		 int addtohistory)
{
    if(!R_Interactive) {
	size_t ll;
	int err = 0;
	if (!R_Slave) {
	    fputs(prompt, stdout);
	    fflush(stdout); /* make sure prompt is output */
	}
	if (fgets((char *)buf, len, ifp ? ifp: stdin) == NULL)
	    return 0;
	ll = strlen((char *)buf);
	/* remove CR in CRLF ending */
	if (ll >= 2 && buf[ll - 1] == '\n' && buf[ll - 2] == '\r') {
	    buf[ll - 2] = '\n';
	    buf[--ll] = '\0';
	}
	/* translate if necessary */
	if(strlen(R_StdinEnc) && strcmp(R_StdinEnc, "native.enc")) {
	    size_t res, inb = strlen((char *)buf), onb = len;
	    /* NB: this is somewhat dangerous.  R's main loop and
	       scan will not call it with a larger value, but
	       contributed code might. */
	    char obuf[CONSOLE_BUFFER_SIZE+1];
	    const char *ib = (const char *)buf;
	    char *ob = obuf;
	    if(!cd) {
		cd = Riconv_open("", R_StdinEnc);
		if(cd == (void *)-1) error(_("encoding '%s' is not recognised"), R_StdinEnc);
	    }
	    res = Riconv(cd, &ib, &inb, &ob, &onb);
	    *ob = '\0';
	    err = res == (size_t)(-1);
	    /* errors lead to part of the input line being ignored */
	    if(err) printf(_("<ERROR: re-encoding failure from encoding '%s'>\n"),
			   R_StdinEnc);
	    strncpy((char *)buf, obuf, len);
	}
/* according to system.txt, should be terminated in \n, so check this
   at eof and error */
	if ((err || feof(ifp ? ifp : stdin))
	    && (ll == 0 || buf[ll - 1] != '\n') && ll < len) {
	    buf[ll++] = '\n'; buf[ll] = '\0';
	}
	if (!R_Slave) {
	    fputs((char *)buf, stdout);
	    fflush(stdout);
	}
	return 1;
    }
    else {
#ifdef HAVE_LIBREADLINE
	R_ReadlineData rl_data;
	if (UsingReadline) {
	    rl_data.readline_gotaline = 0;
	    rl_data.readline_buf = buf;
	    rl_data.readline_addtohistory = addtohistory;
	    rl_data.readline_len = len;
	    rl_data.readline_eof = 0;
	    rl_data.prev = rl_top;
	    rl_top = &rl_data;
	    /* Allow conditional parsing of the ~/.inputrc file. */
	    rl_readline_name = "R";
	    pushReadline(prompt, readline_handler);
#ifdef HAVE_RL_COMPLETION_MATCHES
	    initialize_rlcompletion();
#endif
	}
	else
#endif /* HAVE_LIBREADLINE */
	{
	    fputs(prompt, stdout);
	    fflush(stdout);
	}

	if(R_InputHandlers == NULL)
	    initStdinHandler();

	for (;;) {
	    fd_set *what;

	    int wt = -1;
	    if (R_wait_usec > 0) wt = R_wait_usec;
	    if (Rg_wait_usec > 0 && (wt < 0 || wt > Rg_wait_usec))
		wt = Rg_wait_usec;
	    what = R_checkActivityEx(wt, 0, handleInterrupt);
	    /* This is slightly clumsy. We have advertised the
	     * convention that R_wait_usec == 0 means "wait forever",
	     * but we also need to enable R_checkActivity to return
	     * immediately. */

	    R_runHandlers(R_InputHandlers, what);
	    if (what == NULL)
		continue;
	    if (FD_ISSET(fileno(stdin), what)) {
		/* We could make this a regular handler, but we need
		 * to pass additional arguments. */
#ifdef HAVE_LIBREADLINE
		if (UsingReadline) {
		    rl_callback_read_char();
		    if(rl_data.readline_eof || rl_data.readline_gotaline) {
			rl_top = rl_data.prev;
			return(rl_data.readline_eof ? 0 : 1);
		    }
		}
		else
#endif /* HAVE_LIBREADLINE */
		{
		    if(fgets((char *)buf, len, stdin) == NULL)
			return 0;
		    else
			return 1;
		}
	    }
	}
    }
}