Example #1
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;
}
Example #2
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);
}
Example #3
0
void io_set_prompt(char* newPrompt) {
	if (newPrompt == NULL) {
		free(newPrompt);
	}
	if (prog_running) {
		rl_set_prompt(newPrompt);
	} else {
		rl_set_prompt("");
	}
	rl_redisplay();
}
Example #4
0
void
ConsoleUI::processOutput() {
	FILE *stream = (rl_outstream == NULL) ? stdout : rl_outstream;
	int point, mark;
	char *buffer = NULL;
	char *prompt = NULL;

	// Save readline's state.
	point = rl_point;
	mark = rl_mark;
	if (rl_line_buffer != NULL) {
		buffer = strdup(rl_line_buffer);
	}
	if (rl_prompt != NULL) {
		prompt = strdup(rl_prompt);
	}
	rl_replace_line("", 0);
	rl_point = rl_mark = 0;
	rl_set_prompt("");
	rl_display_prompt = NULL;
	rl_redisplay();

	// If there was already a prompt (previous printed message didn't
	// contain a newline), then print it to the screen and clear the
	// prompt.
	if (prompt != NULL) {
		if (prompt[0] != '\0') {
			fputs(prompt, stream);
		}
		free(prompt);
		prompt = NULL;
	}
	// Make sure the prompt color will be set to default.
	prompt = strdup("\e[0m");

	while (!output.empty()) {
		char *msg = output.front();
		size_t len;

		len = strlen(msg);
		if (output.size() == 1 && len > 0 && msg[len - 1] != '\n') {
			// This is the last message and it doesn't end with a newline.
			// Use this message as prompt.
			char buf[1024 * 32];
			// Reset the prompt color.
			snprintf(buf, sizeof(buf) - 1, "%s\e[0m", msg);
			rl_set_prompt(buf);

			// Prevent prompt from being set to an empty string.
			if (prompt != NULL) {
				free(prompt);
				prompt = NULL;
			}
		} else {
Example #5
0
static void handle_line_fake(char *line)
{
	/* We want to ignore when readline says it has the end of a message, as we'll
	   only care if the user presses ENTER.
	*/
	if (line != NULL) {
		rl_set_prompt(PROMPT);
		rl_already_prompted = 1;
	} else {
		rl_set_prompt("");
		rl_replace_line("", 0);
		rl_redisplay();
		rl_set_prompt(PROMPT);
	}
}
Example #6
0
File: io.c Project: dpc/xmppconsole
static void handle_line_fake(char* line) {
    if (line != NULL) {
        if (prog_running) {
            rl_set_prompt(prompt);
            rl_already_prompted = 1;
        }
        return;
    }
    prog_running = 0;

    rl_set_prompt("");
    rl_replace_line("", 0);
    rl_redisplay();
    rl_set_prompt(prompt);
}
Example #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);
 }
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);
	}
}
Example #9
0
void set_prompt(const char *prompt) {
#ifdef HAVE_RL_SET_PROMPT
	rl_set_prompt(prompt);
#else
	rl_expand_prompt((char *)prompt);
#endif
}
Example #10
0
void readline_constructor_impl::impl::set_prompt(std::string const& prompt)
{
  assert(current_readline == this);
  prompt_ = prompt;
  rl_set_prompt(prompt_.c_str());
  queue_redisplay();
}
Example #11
0
int	 tio_get_one_line(char *buffer, size_t len, int timeout, Flist stack)
{
	int	ret = 0;
	int	go 	= 1;
	char prompt[MAX_TOKEN];

	if (buffer) {
		*prompt = 0;
		if (*buffer) {
			strcpy(prompt, buffer);
		}
		memset(buffer, 0, len);
    	if (timeout) {
        	go = tio_wait_for_char(timeout);
    	}
    	if (go) {
    		rl_set_prompt((const char *)prompt);
    		char	*line = readline((const char *)prompt);
			if(line) {
				if (*line) {
					DEBUGLOG(DEBUGLOG_LEVEL_DEBUG, line);
					strcpy(buffer, line);
					add_history(line);
				}
				free(line);
			}
    	}
	}
	ret = (int)strlen(buffer);
	return(ret);
}
Example #12
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;
}
Example #13
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;
}
Example #14
0
void
cli_infos_loop_stop (cli_infos_t *infos)
{
	if (infos->mode == CLI_EXECUTION_MODE_SHELL) {
		rl_set_prompt (NULL);
	}
	infos->status = CLI_ACTION_STATUS_FINISH;
}
Example #15
0
void
cli_context_loop_stop (cli_context_t *ctx)
{
	if (ctx->mode == CLI_EXECUTION_MODE_SHELL) {
		rl_set_prompt (NULL);
	}
	ctx->status = CLI_ACTION_STATUS_FINISH;
}
Example #16
0
File: rline.cpp Project: cgdb/cgdb
int rline_set_prompt(struct rline *rline, const char *prompt)
{
    if (!rline)
        return -1;

    rl_set_prompt(prompt);

    return 0;
}
Example #17
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
}
Example #18
0
static PyObject *
set_prompt(PyObject *self, PyObject *args)
{
	char *s;
	if (!PyArg_ParseTuple(args, "s:set_prompt", &s))
		return NULL;
	rl_set_prompt(s);
	Py_INCREF(Py_None);
	return Py_None;
}
Example #19
0
File: io.c Project: dpc/xmppconsole
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();

    /* force readline to think that the current line was "eaten" and executed */
    rl_done = 1;
    return 0;
}
Example #20
0
File: io.c Project: dpc/xmppconsole
static void io_async_print(print_func func, void* data) {
    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();
    (*func)(data);
    if (prog_running) {
        rl_set_prompt(prompt);
        rl_replace_line(saved_line, 0);
        rl_point = saved_point;
        rl_redisplay();
    } else {
        /* FIXME: This leaves prompt on /quit. Fix it. */
        rl_redisplay();
    }
    free(saved_line);
}
Example #21
0
void ConsoleReader::run() {
    init();
#ifndef NOINPUT
    while (m->running) {
        const char *line = readline("");
        if (!line) {
           break;
        }
        rl_set_prompt("");
        rl_replace_line("", 0);
        m->server->dispatchConsoleCommand(line, m->server->getCommandSender());
    }
    exit(0);
#endif
}
Example #22
0
File: io.c Project: dpc/xmppconsole
void io_prompt_set(net_status_t st) {
    switch (st) {
    case NET_ST_DISCONNECTED:
        prompt = "__> ";
        break;
    case NET_ST_OFFLINE:
        prompt = "=_> ";
        break;
    case NET_ST_ONLINE:
        prompt = "==> ";
        break;
    default:
        prompt = "=?> ";
        break;
    }
    rl_set_prompt(prompt);
    rl_redisplay();
}
Example #23
0
void ssc_input_set_prompt(const char* prompt)
{
#if USE_READLINE
  ssc_input_prompt = prompt;

  if (strcmp(rl_prompt, prompt)) {
    rl_set_prompt(prompt);
  }
#else
  int refresh = 0;

  if (strcmp(prompt, ssc_input_prompt)) 
    refresh = 1;

  ssc_input_prompt = prompt;
  
  if (refresh)
    ssc_input_refresh();
#endif
}
Example #24
0
static char *cli_append_multiline(char *line)
{
	bool complete = false;
	size_t len;
	char *s;

	if (line == NULL && multiline == NULL) {
		eof = true;
		return NULL;
	}

	len = strlen(line);
	if (line[len - 1] == '\\') {
		line[len - 1] = '\0';
		len--;
	} else if (multiline == NULL)
		return line;
	else
		complete = 1;

	if (multiline == NULL) {
		multiline = line;
		rl_save_prompt();
		rl_clear_message();
		rl_set_prompt(".... ");
	} else {
		len += strlen(multiline);
		s = xmalloc(len + 1);
		snprintf(s, len + 1, "%s%s", multiline, line);
		xfree(multiline);
		multiline = s;
	}
	line = NULL;

	if (complete) {
		line = multiline;
		multiline = NULL;
		rl_restore_prompt();
	}
	return line;
}
Example #25
0
int main(int argc, char *argv[])
{
	/* We always require a file, will guess a nick if none provided.*/
	if(argc < 2) {
		fprintf(stderr, "%s v%s - a small chat system for multiple users\n", argv[0], VERSION);
		fprintf(stderr, "usage: %s file [nick]\n", argv[0]);
		return 1;
	}

	if(argv[2] == NULL) {
		/* If no nick is provided, pick based on the username. */
		getlogin_r(nick, MAX_NICK_SIZE);
	} else {
		/* Copy from argv and make sure its null-terminated.*/
		strncpy(nick, argv[2], MAX_NICK_SIZE);
		nick[MAX_NICK_SIZE-1] = '\0';
	}

	/* Open the file. Always write at the end. */
	ctrl = fopen(argv[1], "a+");
	if(ctrl == NULL) {
		fprintf(stderr, "Unable to open %s!\n", argv[1]);
		return 1;
	}

	/* We don't want to see past messages, as they could be loooooong.*/
	fseek(ctrl, 0L, SEEK_END);
	/* This is now our "last read" position. */
	last_read_pos = ftell(ctrl);

	/* Tell readline to let us know when the user hits enter. */
	rl_bind_key(RETURN, handle_enter);

	/* Setup the fake handler for when readline thinks we're done. */
	rl_callback_handler_install(PROMPT, handle_line_fake);

	/* Done with setup!
	   Now let everyone know the user has arrived. */
	write_status("joined");

	/* Until we decide to quit, tell readline to grab characters if they're available. */
	while(cont) {
		get_input();
		usleep(10000);
		check_msgs();
		usleep(10000);
	}

	/* We're quitting now. Say goodbye. */
	write_status("left");

	/* Clean up for readline now */
	rl_unbind_key(RETURN);
	rl_callback_handler_remove();

	/* Being a good citizen. Closing file handles. */
	fclose(ctrl);

	/* Clean up screen. */
	rl_set_prompt("");
	rl_redisplay();

	return 0;
}
Example #26
0
int main(int argc,char *argv[])
{
	int c;
	int port=-1;
	int len=0;
	char *ptr;
	char bfr[8192];

	while((c=getopt(argc,argv,"p:"))!=-1)switch(c)
	{
	case 'p':
		port=atoi(optarg);
		break;
	}

	if(port<1||port>65535)
	{
		fprintf(stderr,"Illegal port\n");
		return 1;
	}

	p[0].fd=0;
	p[0].events=POLLIN;
	if((p[1].fd=mklisten(port))==-1)return 1;
	p[1].events=POLLIN;
	p[2].fd=-1;
	p[2].events=POLLIN;

	rl_bind_key ('\t',rl_insert);
	rl_callback_handler_install("> ",docmd);

	while(running)
	{
		p[2].revents=0;
		if(poll(p,p[2].fd==-1?2:3,-1)<=0)continue;
		if(p[0].revents&POLLHUP)break;
		if(p[0].revents&POLLIN)rl_callback_read_char();
		if(p[1].revents&POLLIN)
			if((c=accept(p[1].fd,NULL,NULL))!=-1)
		{
			if(p[2].fd!=-1)close(p[2].fd);
			p[2].revents=0;
			p[2].fd=c;
			len=0;
			c=1;
			ioctl(p[2].fd,FIONBIO,&c);
#if defined(TCP_KEEPCNT) && defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
			c=2;
			setsockopt(p[2].fd,IPPROTO_TCP,TCP_KEEPCNT,
				&c,sizeof(c));
			c=90;
			setsockopt(p[2].fd,IPPROTO_TCP,TCP_KEEPIDLE,
				&c,sizeof(c));
			c=90;
			setsockopt(p[2].fd,IPPROTO_TCP,TCP_KEEPINTVL,
				&c,sizeof(c));
			c=1;
			setsockopt(p[2].fd,SOL_SOCKET,SO_KEEPALIVE,
				&c,sizeof(c));
#endif
		}
		if(p[2].revents&POLLHUP)
		{
			close(p[2].fd);
			p[2].fd=-1;
		}
		else if(p[2].revents&POLLIN)
		{
			if((c=read(p[2].fd,bfr+len,sizeof(bfr)-len))<=0)
			{
				close(p[2].fd);
				p[2].fd=-1;
			}
			else
			{
				len+=c;

				for(ptr=bfr,c=0;c<len;c++)if(bfr[c]=='$')
				{
					bfr[c]=0;
					printf("\n%s$",ptr);
					ptr=bfr+c+1;
				}

				if(ptr!=bfr)
				{
					printf("\n");
					rl_on_new_line();
					rl_redisplay();

					if(ptr<bfr+len)
					{
						len=bfr+len-ptr;
						memmove(bfr,ptr,len);
					}
					else len=0;
				}

				if(len==sizeof(bfr))
				{
					close(p[2].fd);
					p[2].fd=-1;
				}
			}
		}
	}

	close(p[1].fd);
	if(p[2].fd!=-1)
	{
		close(p[2].fd);
		p[2].fd=-1;
	}

	rl_set_prompt("");
	rl_replace_line("",0);
	rl_redisplay();
	rl_callback_handler_remove();

	return 0;
}
Example #27
0
int cli_init(sd_bus *bus, const struct cli_cmd *cmds)
{
	static const int sigs[] = {
		SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGPIPE, SIGCHLD, 0
	};
	unsigned int i;
	sigset_t mask;
	int r;

	if (cli_event)
		return cli_EINVAL();

	r = sd_event_default(&cli_event);
	if (r < 0) {
		cli_vERR(r);
		goto error;
	}

	cli_cmds = cmds;
	cli_bus = bus;

	r = sd_bus_attach_event(cli_bus, cli_event, 0);
	if (r < 0) {
		cli_vERR(r);
		goto error;
	}

	for (i = 0; sigs[i]; ++i) {
		sigemptyset(&mask);
		sigaddset(&mask, sigs[i]);
		sigprocmask(SIG_BLOCK, &mask, NULL);

		r = sd_event_add_signal(cli_event,
					&cli_sigs[i],
					sigs[i],
					cli_signal_fn,
					NULL);
		if (r < 0) {
			cli_vERR(r);
			goto error;
		}
	}

	r = sd_event_add_io(cli_event,
			    &cli_stdin,
			    fileno(stdin),
			    EPOLLHUP | EPOLLERR | EPOLLIN,
			    cli_stdin_fn,
			    NULL);
	if (r < 0) {
		cli_vERR(r);
		goto error;
	}

	cli_rl = true;

	rl_erase_empty_line = 1;
	rl_callback_handler_install(NULL, cli_handler_fn);

	rl_set_prompt(CLI_PROMPT);
	printf("\r");
	rl_on_new_line();
	rl_redisplay();

	return 0;

error:
	cli_destroy();
	return r;
}
Example #28
0
File: loop.c Project: tkosgrabar/tg
int loop (void) {
  on_start ();
  read_auth_file ();
  readline_active = 1;
  rl_set_prompt ("");

  assert (DC_list[dc_working_num]);
  if (auth_state == 0) {
    DC_working = DC_list[dc_working_num];
    assert (!DC_working->auth_key_id);
    dc_authorize (DC_working);
    assert (DC_working->auth_key_id);
    auth_state = 100;
    write_auth_file ();
  }
  
  if (verbosity) {
    logprintf ("Requesting info about DC...\n");
  }
  do_help_get_config ();
  net_loop (0, mcs);
  if (verbosity) {
    logprintf ("DC_info: %d new DC got\n", new_dc_num);
  }
  int i;
  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) {
    dc_authorize (DC_list[i]);
    assert (DC_list[i]->auth_key_id);
    write_auth_file ();
  }

  if (auth_state == 100) {
    if (!default_username) {
      size_t size = 0;
      char *user = 0;

      if (!user) {
        printf ("Telephone number (with '+' sign): ");         
        if (net_getline (&user, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        set_default_username (user);
      }
    }
    int res = do_auth_check_phone (default_username);
    assert (res >= 0);
    logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered");
    if (res > 0) {
      do_send_code (default_username);
      char *code = 0;
      size_t size = 0;
      printf ("Code from sms: ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (do_send_code_result (code) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
      auth_state = 300;
    } else {
      printf ("User is not registered. Do you want to register? [Y/n] ");
      char *code;
      size_t size;
      if (net_getline (&code, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      if (!*code || *code == 'y') {
        printf ("Ok, starting registartion.\n");
      } else {
        printf ("Then try again\n");
        exit (EXIT_SUCCESS);
      }
      char *first_name;
      printf ("Name: ");
      if (net_getline (&first_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      char *last_name;
      printf ("Name: ");
      if (net_getline (&last_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }

      int dc_num = do_get_nearest_dc ();
      assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]);
      dc_working_num = dc_num;
      DC_working = DC_list[dc_working_num];
      
      do_send_code (default_username);
      printf ("Code from sms: ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (do_send_code_result_auth (code, first_name, last_name) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
      auth_state = 300;
    }
  }

  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) {
    do_export_auth (i);
    do_import_auth (i);
    DC_list[i]->has_auth = 1;
    write_auth_file ();
  }
  write_auth_file ();

  fflush (stdin);
  fflush (stdout);
  fflush (stderr);

  rl_callback_handler_install (get_default_prompt (), interpreter);
  rl_attempted_completion_function = (CPPFunction *) complete_text;
  rl_completion_entry_function = complete_none;

  do_get_dialog_list ();

  return main_loop ();
}
Example #29
0
static void set_state(enum state st)
{
	conn_state = st;
	rl_set_prompt(get_prompt());
}
Example #30
0
void io_prompt_set(char *prompt) {
	rl_set_prompt(prompt);
	rl_redisplay();
}