Beispiel #1
0
static void print_line(char *line)
{

	char *saved_line = NULL;
	int saved_point = 0;

	/* This is readline stuff.
	   - save the cursor position
	   - save the current line contents
	   - set the line to blank
	   - tell readline we're done mucking
	   - print the message
	   - restore the standard prompt
	   - restore the line contents
	   - restore the cursor position
	   - tell readline we're done mucking (again)
	*/
	saved_point = rl_point;
	saved_line = rl_copy_text(0, rl_end);
	rl_set_prompt("");
	rl_replace_line("",0);
	rl_redisplay();
	fprintf(stdout, "%s", line);
	rl_set_prompt(PROMPT);
	rl_replace_line(saved_line, 0);
	rl_point = saved_point;
	rl_redisplay();
	free(saved_line);
}
Beispiel #2
0
static int handle_enter(int x, int y)
{
	char *line = NULL;

	/* Handle when a user presses enter.
	   - Save the contents of the line.
	   - Set the prompt to nothing.
	   - Blank the line.
	   - pass the message to the message handler
	   - rl_copy_text returns malloc'd mem, so free it
	   - restore the prompt
	   - tell readline we're done mucking
	*/
	line = rl_copy_text(0, rl_end);
	rl_set_prompt("");
	rl_replace_line("", 1);
	rl_redisplay();

	handle_msg(line);

	free(line);

	rl_set_prompt(PROMPT);
	rl_redisplay();

	rl_done = 1;
	return 0;
}
Beispiel #3
0
void cli_printv(const char *fmt, va_list args)
{
	SHL_PROTECT_ERRNO;
	_shl_free_ char *line = NULL;
	int point;
	bool async;

	/* In case we print messages during readline() activity, we need to
	 * correctly save and restore RL-internal state. */
	async = is_cli() && !RL_ISSTATE(RL_STATE_DONE);

	if (async) {
		point = rl_point;
		line = rl_copy_text(0, rl_end);
		rl_save_prompt();
		rl_replace_line("", 0);
		rl_redisplay();
	}

	vprintf(fmt, args);

	if (async) {
		rl_restore_prompt();
		rl_replace_line(line, 0);
		rl_point = point;
		rl_redisplay();
	}
}
Beispiel #4
0
void Console::writeAsync(QString string)
{
  int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;

  char *saved_line = NULL;
  int saved_point = 0;
  if (need_hack)
  {
    saved_point = rl_point;
    saved_line = rl_copy_text(0, rl_end);
    rl_save_prompt();
    rl_replace_line("", 0);
    rl_redisplay();
  }

  std::cout << "\r" << string.toStdString();

  if (need_hack)
  {
    rl_restore_prompt();
    rl_replace_line(saved_line, 0);
    rl_point = saved_point;
    rl_redisplay();
    free(saved_line);
  }
}
Beispiel #5
0
void PrintAndLog(char *fmt, ...)
{
	char *saved_line;
	int saved_point;
	va_list argptr, argptr2;
	static FILE *logfile = NULL;
	static int logging=1;

	// lock this section to avoid interlacing prints from different threats
	pthread_mutex_lock(&print_lock);
  
	if (logging && !logfile) {
		logfile=fopen(logfilename, "a");
		if (!logfile) {
			fprintf(stderr, "Can't open logfile, logging disabled!\n");
			logging=0;
		}
	}
	
	int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;

	if (need_hack) {
		saved_point = rl_point;
		saved_line = rl_copy_text(0, rl_end);
		rl_save_prompt();
		rl_replace_line("", 0);
		rl_redisplay();
	}
	
	va_start(argptr, fmt);
	va_copy(argptr2, argptr);
	vprintf(fmt, argptr);
	printf("          "); // cleaning prompt
	va_end(argptr);
	printf("\n");

	if (need_hack) {
		rl_restore_prompt();
		rl_replace_line(saved_line, 0);
		rl_point = saved_point;
		rl_redisplay();
		free(saved_line);
	}
	
	if (logging && logfile) {
		vfprintf(logfile, fmt, argptr2);
		fprintf(logfile,"\n");
		fflush(logfile);
	}
	va_end(argptr2);

	if (flushAfterWrite == 1)  //buzzy
	{
		fflush(NULL);
	}
	//release lock
	pthread_mutex_unlock(&print_lock);  
}
Beispiel #6
0
//------------------------------------------------------------------------------
int getc_impl(FILE* stream)
{
    int printable;
    int alt;
    int i;
    while (1)
    {
        wchar_t wc[2];
        char utf8[4];

        alt = 0;
        i = GETWCH_IMPL(&alt);

        // MSB is set if value represents a printable character.
        printable = (i & 0x80000000);
        i &= ~printable;

        // Treat esc like cmd.exe does - clear the line.
        if (i == 0x1b)
        {
            if (rl_editing_mode == emacs_mode &&
                get_clink_setting_int("esc_clears_line"))
            {
                using_history();
                rl_delete_text(0, rl_end);
                rl_point = 0;
                rl_redisplay();
                continue;
            }
        }

        // Mask off top bits, they're used to track ALT key state.
        if (i < 0x80 || (i == 0xe0 && !printable))
        {
            break;
        }

        // Convert to utf-8 and insert directly into rl's line buffer.
        wc[0] = (wchar_t)i;
        wc[1] = L'\0';
        WideCharToMultiByte(CP_UTF8, 0, wc, -1, utf8, sizeof(utf8), NULL, NULL);

        rl_insert_text(utf8);
        rl_redisplay();
    }

    alt = RL_ISSTATE(RL_STATE_MOREINPUT) ? 0 : alt;
    alt = alt ? 0x80 : 0;
    return i|alt;
}
Beispiel #7
0
 void printlog(int c) {
     char* saved_line;
     int saved_point;
     saved_point = rl_point;
     saved_line = rl_copy_text(0, rl_end);
     rl_set_prompt("");
     rl_replace_line("", 0);
     rl_redisplay();
     //printf("Message: %d\n", c);
     rl_set_prompt(prompt);
     rl_replace_line(saved_line, 0);
     rl_point = saved_point;
     rl_redisplay();
     free(saved_line);
 }
Beispiel #8
0
void PrintAndLog(char *fmt, ...)
{
	char *saved_line;
	int saved_point;
  va_list argptr, argptr2;
  static FILE *logfile = NULL;
  static int logging=1;

  if (logging && !logfile) {
    logfile=fopen(logfilename, "a");
    if (!logfile) {
      fprintf(stderr, "Can't open logfile, logging disabled!\n");
      logging=0;
    }
  }
	
	int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;

	if (need_hack) {
		saved_point = rl_point;
		saved_line = rl_copy_text(0, rl_end);
		rl_save_prompt();
		rl_replace_line("", 0);
		rl_redisplay();
	}
	
  va_start(argptr, fmt);
  va_copy(argptr2, argptr);
  vprintf(fmt, argptr);
  printf("          "); // cleaning prompt
  va_end(argptr);
  printf("\n");

	if (need_hack) {
		rl_restore_prompt();
		rl_replace_line(saved_line, 0);
		rl_point = saved_point;
		rl_redisplay();
		free(saved_line);
	}
	
  if (logging && logfile) {
    vfprintf(logfile, fmt, argptr2);
    fprintf(logfile,"\n");
    fflush(logfile);
  }
  va_end(argptr2);
}
Beispiel #9
0
/*
 * window_write()
 *
 * dopisuje liniê do bufora danego okna.
 */
int window_write(int id, const /*locale*/ char *line)
{
	window_t *w = window_exist(id);
	readline_window_t *r = readline_window(w);
	int i = 1;

	if (!line || !w)
		return -1;

	/* je¶li ca³y bufor zajêty, zwolnij pierwsz± liniê i przesuñ do góry */
	if (r->line[MAX_LINES_PER_SCREEN - 1]) {
		xfree(r->line[0]);
		memmove(&(r->line[0]), &(r->line[1]), sizeof(char *) * (MAX_LINES_PER_SCREEN - 1));
		r->line[MAX_LINES_PER_SCREEN - 1] = xstrdup(line);
	} else {
		/* znajd¼ pierwsz± woln± liniê i siê wpisz. */
		for (i = 0; i < MAX_LINES_PER_SCREEN; i++)
			if (!r->line[i]) {
				r->line[i] = xstrdup(line);
				break;
			}
	}

	if (w != window_current) {
		set_prompt(current_prompt());
		rl_redisplay();
	}
	
	return 0;
}
Beispiel #10
0
int
cli_rl_out (struct cli_state *state, const char *fmt, va_list ap)
{
        int tmp_rl_point = rl_point;
        int            n = rl_end;
        int            i = 0;
        int            ret = 0;

        if (rl_end >= 0 ) {
                rl_kill_text (0, rl_end);
                rl_redisplay ();
        }

        printf ("\r");

        for (i = 0; i <= strlen (state->prompt); i++)
                printf (" ");

        printf ("\r");

        ret = vprintf (fmt, ap);

        printf ("\n");
        fflush(stdout);

        if (n) {
                rl_do_undo ();
                rl_point = tmp_rl_point;
                rl_reset_line_state ();
        }

        return ret;
}
Beispiel #11
0
void
async_printf (const char *fmt, va_list ap)
{
        int tmp_rl_point = rl_point;
        int n = rl_end;
        unsigned int i;

        if (rl_end >= 0 ) {
                rl_kill_text (0, rl_end);
                rl_redisplay ();
        }
        printf ("\r");
        for (i=0 ; i<=strlen (state.prompt) ; i++)
                printf (" ");
        printf ("\r");
        vprintf (fmt, ap);
        printf ("\n");
        fflush(stdout);
        if (n) {
                rl_do_undo ();
                rl_point = tmp_rl_point;
                rl_reset_line_state ();
        }
        rl_forced_update_display ();
}
static int
match_paren(int x, int k)
{
  int tmp;
  fd_set readset;
  struct timeval timeout;
  
  rl_insert(x, k);

  /* Did we just insert a quoted paren?  If so, then don't bounce.  */
  if (rl_point - 1 >= 1
      && rl_line_buffer[rl_point - 2] == '\\')
    return 0;

  /* tmp = 200000 */
  timeout.tv_sec = 0 /* tmp / 1000000 */ ; 
  timeout.tv_usec = 200000 /* tmp % 1000000 */ ;
  FD_ZERO(&readset);
  FD_SET(fileno(rl_instream), &readset);
  
  if(rl_point > 1) {
    tmp = rl_point;
    rl_point = find_matching_paren(k);
    if(rl_point > -1) {
      rl_redisplay();
      select(1, &readset, NULL, NULL, &timeout);
    }
    rl_point = tmp;
  }

  return 0;
}
void edit_deinit(const char *history_file,
		 int (*filter_cb)(void *ctx, const char *cmd))
{
	rl_set_prompt("");
	rl_replace_line("", 0);
	rl_redisplay();
	rl_callback_handler_remove();
	readline_free_completions();

	eloop_unregister_read_sock(STDIN_FILENO);

	if (history_file) {
		/* Save command history, excluding lines that may contain
		 * passwords. */
		HIST_ENTRY *h;
		history_set_pos(0);
		while ((h = current_history())) {
			char *p = h->line;
			while (*p == ' ' || *p == '\t')
				p++;
			if (filter_cb && filter_cb(edit_cb_ctx, p)) {
				h = remove_history(where_history());
				if (h) {
					free(h->line);
					free(h->data);
					free(h);
				} else
					next_history();
			} else
				next_history();
		}
		write_history(history_file);
	}
}
Beispiel #14
0
int io_handle_enter(int x, int y) {
	char* line = NULL;

	line = rl_copy_text(0, rl_end);
	rl_set_prompt("");
	rl_replace_line("", 1);
	rl_redisplay();

	//cmd_execute(line);

	if (strcmp(line, "") != 0) {
		add_history(line);
	}
	free(line);

	/* rl_set_prompt(prompt); */
	/* rl_redisplay(); */


	// wip - clear line on enter
	wmove(winCommandMode, 1, 1);
	//wprintw(winCommandMode, "      ");
	wclrtoeol(winCommandMode);
	wmove(winCommandMode, 1, 1);
	wprintw(winCommandMode, ">");
	wrefresh(winCommandMode);

	/* force readline to think that the current line was "eaten" and executed */
	rl_done = 1;
	return 0;
}
Beispiel #15
0
void
restore_rl_state()
{
  
  char *newprompt;


  move_cursor_to_start_of_prompt(impatient_prompt ? ERASE : DONT_ERASE);

  cook_prompt_if_necessary();
  newprompt =  mark_invisible(saved_rl_state.cooked_prompt); /* bracket (colour) control sequences with \001 and \002 */  
  rl_expand_prompt(newprompt);
  mirror_slaves_echo_mode();    /* don't show passwords etc */
  
  DPRINTF1(DEBUG_READLINE,"newprompt now %s", mangle_string_for_debug_log(newprompt,MANGLE_LENGTH));
  rl_callback_handler_install(newprompt, &line_handler);
  DPRINTF0(DEBUG_AD_HOC, "freeing newprompt");
  free(newprompt);             /* readline docs don't say it, but we can free newprompt now (readline apparently
                                  uses its own copy) */
  rl_insert_text(saved_rl_state.input_buffer);
  rl_point = saved_rl_state.point;
  saved_rl_state.already_saved = 0;
  DPRINTF0(DEBUG_AD_HOC, "Starting redisplay");
  rl_redisplay(); 
  rl_prep_terminal(1);
  prompt_is_still_uncooked =  FALSE; /* has been done right now */
}
Beispiel #16
0
static void cli_handler_fn(char *input)
{
	_shl_free_ char *original = input;
	_shl_strv_free_ char **args = NULL;
	int r;

	if (!input) {
		rl_insert_text("quit");
		rl_redisplay();
		rl_crlf();
		sd_event_exit(cli_event, 0);
		return;
	}

	r = shl_qstr_tokenize(input, &args);
	if (r < 0)
		return cli_vENOMEM();
	else if (!r)
		return;

	add_history(original);
	r = cli_do(cli_cmds, args, r);
	if (r != -EAGAIN)
		return;

	cli_printf("Command not found\n");
}
Beispiel #17
0
static PyObject *
redisplay(PyObject *self, PyObject *noarg)
{
	rl_redisplay();
	Py_INCREF(Py_None);
	return Py_None;
}
Beispiel #18
0
void cli_destroy(void)
{
	unsigned int i;

	if (!cli_event)
		return;

	if (cli_rl) {
		cli_rl = false;

		rl_replace_line("", 0);
		rl_crlf();
		rl_on_new_line();
		rl_redisplay();

		rl_message("");
		rl_callback_handler_remove();
	}

	sd_event_source_unref(cli_stdin);
	cli_stdin = NULL;

	for (i = 0; cli_sigs[i]; ++i) {
		sd_event_source_unref(cli_sigs[i]);
		cli_sigs[i] = NULL;
	}

	cli_cmds = NULL;
	sd_bus_detach_event(cli_bus);
	cli_bus = NULL;
	sd_event_unref(cli_event);
	cli_event = NULL;
}
Beispiel #19
0
int
cli_rl_err (struct cli_state *state, const char *fmt, va_list ap)
{
        int tmp_rl_point = rl_point;
        int            n = rl_end;
        int            ret = 0;

        if (rl_end >= 0 ) {
                rl_kill_text (0, rl_end);
                rl_redisplay ();
        }

        fprintf (stderr, "\r%*s\r", (int)strlen (state->prompt), "");

        ret = vfprintf (stderr, fmt, ap);

        fprintf (stderr, "\n");
        fflush(stderr);

        if (n) {
                rl_do_undo ();
                rl_point = tmp_rl_point;
                rl_reset_line_state ();
        }

        return ret;
}
Beispiel #20
0
void
input_stop_list(void)
{
  /* Reprint the currently edited line after listing */
  rl_on_new_line();
  rl_redisplay();
}
Beispiel #21
0
char *ssc_input_read_string(char *str, int size)
{
#if USE_READLINE
  char *input;

  /* disable readline callbacks */
  if (ssc_input_handler_f)
    rl_callback_handler_remove();

  rl_reset_line_state();

  /* read a string a feed to 'str' */
  input = readline(ssc_input_prompt);
  strncpy(str, input, size - 1);
  str[size - 1] = 0;

  /* free the copy malloc()'ed by readline */
  free(input);

  /* reinstall the func */
  if (ssc_input_handler_f)
    rl_callback_handler_install(ssc_input_prompt, ssc_input_handler_f);
  
  rl_redisplay();

  return str;
#else
  return fgets(str, size, stdin);
#endif
}
Beispiel #22
0
void rl_printf(const char *fmt, ...)
{
	va_list args;
	bool save_input;
	char *saved_line;
	int saved_point;

	save_input = !RL_ISSTATE(RL_STATE_DONE);

	if (save_input) {
		saved_point = rl_point;
		saved_line = rl_copy_text(0, rl_end);
		rl_save_prompt();
		rl_replace_line("", 0);
		rl_redisplay();
	}

	va_start(args, fmt);
	vprintf(fmt, args);
	va_end(args);

	if (save_input) {
		rl_restore_prompt();
		rl_replace_line(saved_line, 0);
		rl_point = saved_point;
		rl_forced_update_display();
		free(saved_line);
	}
}
Beispiel #23
0
void *
ConsoleUI::threadMain(void *arg) {
	rl_callback_handler_install("", ConsoleUICallbacks::lineRead);
	while (!quit) {
		while (canRead()) {
			lineProcessed = false;
			rl_callback_read_char();
			if (lineProcessed && rl_prompt != NULL && rl_prompt[0] != '\0') {
				// If a line has been processed, reset the prompt
				// so we don't see it again after an Enter.
				rl_set_prompt("");
				rl_display_prompt = NULL;
				rl_redisplay();
			}
		}

		pthread_mutex_lock(&outputLock);
		if (!output.empty()) {
			processOutput();
			pthread_cond_broadcast(&outputCond);
		}
		pthread_mutex_unlock(&outputLock);

		usleep(10000);
	}
	rl_callback_handler_remove();
	return NULL;
}
Beispiel #24
0
/* server option completion function */
static char *
rl_complete_set(char *text, int state)
{
	static struct ph_option *phopt;
	static int textlen;
	int debug;

	if (!state)
	{
		phopt = NULL;
		textlen = strlen(text);
		if (!ph_is_optionlist_cached(ph))
		{
			get_option("debug", &debug);
			if (debug)
			{
				putchar('\n');
				rl_on_new_line();
			}
			if (ph_retrieve_options(ph) == -1)
			{
				perror("ph_retrieve_options()");
				return NULL;
			}
			if (debug)
				rl_redisplay();
		}
	}

	while (ph_option_iterate(ph, &phopt) == 1)
		if (strncasecmp(phopt->po_option, text, textlen) == 0)
			return strdup(phopt->po_option);

	return NULL;
}
Beispiel #25
0
static void
input_hide(void)
{
  input_hidden_end = rl_end;
  rl_end = 0;
  rl_expand_prompt("");
  rl_redisplay();
}
Beispiel #26
0
// Call whenever we generate output, so that the prompt is updated
static inline void
wake_input_thread(void){
	if(input_tid){
		pthread_kill(*input_tid,SIGWINCH);
		rl_redisplay(); // FIXME probably need call from readline context
	}
	pthread_mutex_unlock(&promptlock);
}
Beispiel #27
0
void ConsoleReader::println(const string &s) {
#ifndef NOINPUT
    m->lock.lock();
    int savedPoint = rl_point;
    char *savedLine = rl_copy_text(0, rl_end);
    rl_set_prompt("");
    rl_replace_line("", 0);
    rl_redisplay();
    m->cout << s << '\n';
    rl_set_prompt(">");
    rl_replace_line(savedLine, 0);
    rl_point = savedPoint;
    rl_redisplay();
    m->lock.unLock();
#else
    puts(s.c_str());
#endif
}
Beispiel #28
0
std::streamsize Console::ReadlineStreambuf::xsputn(const char* s, std::streamsize count)
{
    int saved_point = rl_point;
    char *saved_line = rl_copy_text(0, rl_end);
    rl_save_prompt();
    rl_replace_line("", 0);
    rl_redisplay();
    
    outStream.sputn(s, count);
    
    rl_restore_prompt();
    rl_replace_line(saved_line, 0);
    rl_point = saved_point;
    rl_redisplay();
    free(saved_line);
    
    return count;
}
Beispiel #29
0
void handle_sigint(int signo)
{
#ifdef HAVE_ORIG_READLINE
	rl_reset_line_state();
	rl_replace_line("", 0);
	rl_crlf();
#endif
	rl_redisplay();
	return;
}
Beispiel #30
0
void ssc_input_refresh(void)
{
#if USE_READLINE
  rl_reset_line_state();
  rl_redisplay();
#else
  printf("%s", ssc_input_prompt); 
  fflush(stdout);
#endif
}