Ejemplo n.º 1
0
static char *
nmc_readline_helper (const char *prompt)
{
	char *str;
	int b;

readline_mark:
	/* We are in readline -> Ctrl-C should not quit nmcli */
	nmc_set_in_readline (TRUE);
	str = readline (prompt);
	/* We are outside readline -> Ctrl-C should quit nmcli */
	nmc_set_in_readline (FALSE);

	/* Check for an I/O error by attempting to peek into the input buffer.
	 * Readline just inserts newlines when errors occur so we need to check ourselves. */
	if (ioctl (0, FIONREAD, &b) == -1) {
		g_free (str);
		str = NULL;
	}

	/* Add string to the history */
	if (str && *str)
		add_history (str);

	/*-- React on Ctrl-C and Ctrl-D --*/
	/* We quit on Ctrl-D when line is empty */
	if (str == NULL) {
		/* Send SIGQUIT to itself */
		nmc_set_sigquit_internal ();
		kill (getpid (), SIGQUIT);
		/* Sleep in this thread so that we don't do anything else until exit */
		for (;;)
			sleep (3);
	}
	/* Ctrl-C */
	if (nmc_seen_sigint ()) {
		nmc_clear_sigint ();
		if (nm_cli.in_editor || *str) {
			/* In editor, or the line is not empty */
			/* Call readline again to get new prompt (repeat) */
			g_free (str);
			goto readline_mark;
		} else {
			/* Not in editor and line is empty */
			/* Send SIGQUIT to itself */
			nmc_set_sigquit_internal ();
			kill (getpid (), SIGQUIT);
			/* Sleep in this thread so that we don't do anything else until exit */
			for (;;)
				sleep (3);
		}
	}

	/* Return NULL, not empty string */
	if (str && *str == '\0') {
		g_free (str);
		str = NULL;
	}
	return str;
}
Ejemplo n.º 2
0
/**
 * nmc_readline:
 * @prompt_fmt: prompt to print (telling user what to enter). It is standard
 *   printf() format string
 * @...: a list of arguments according to the @prompt_fmt format string
 *
 * Wrapper around libreadline's readline() function.
 * If user pressed Ctrl-C, readline() is called again (if not in editor and
 * line is empty, nmcli will quit).
 * If user pressed Ctrl-D on empty line, nmcli will quit.
 *
 * Returns: the user provided string. In case the user entered empty string,
 * this function returns NULL.
 */
char *
nmc_readline (const char *prompt_fmt, ...)
{
	va_list args;
	char *prompt, *str;

	va_start (args, prompt_fmt);
	prompt = g_strdup_vprintf (prompt_fmt, args);
	va_end (args);

readline_mark:
	/* We are in readline -> Ctrl-C should not quit nmcli */
	nmc_set_in_readline (TRUE);
	str = readline (prompt);
	/* We are outside readline -> Ctrl-C should quit nmcli */
	nmc_set_in_readline (FALSE);

	/* Add string to the history */
	if (str && *str)
		add_history (str);

	/*-- React on Ctrl-C and Ctrl-D --*/
	/* We quit on Ctrl-D when line is empty */
	if (str == NULL) {
		/* Send SIGQUIT to itself */
		nmc_set_sigquit_internal ();
		kill (getpid (), SIGQUIT);
		/* Sleep in this thread so that we don't do anything else until exit */
		for (;;)
			sleep (3);
	}
	/* Ctrl-C */
	if (nmc_seen_sigint ()) {
		nmc_clear_sigint ();
		if (nm_cli.in_editor || *str) {
			/* In editor, or the line is not empty */
			/* Call readline again to get new prompt (repeat) */
			g_free (str);
			goto readline_mark;
		} else {
			/* Not in editor and line is empty */
			/* Send SIGQUIT to itself */
			nmc_set_sigquit_internal ();
			kill (getpid (), SIGQUIT);
			/* Sleep in this thread so that we don't do anything else until exit */
			for (;;)
				sleep (3);
		}
	}
	g_free (prompt);

	/* Return NULL, not empty string */
	if (str && *str == '\0') {
		g_free (str);
		str = NULL;
	}
	return str;
}
Ejemplo n.º 3
0
static int
event_hook_for_readline (void)
{
	/* Make readline() exit on SIGINT */
	if (nmc_seen_sigint ()) {
		rl_echo_signal_char (SIGINT);
		rl_stuff_char ('\n');
	}
	return 0;
}