+typedef void (*user_input_cb)(const char *value, gpointer user_data);
+
+struct user_input_data {
+ GString *str;
+ user_input_cb cb;
+ gpointer user_data;
+ bool hidden;
+ int fd;
+ struct termios saved_termios;
+};
+
+static void user_callback(struct user_input_data *data)
+{
+ char *value;
+
+ if (data->hidden) {
+ ssize_t len;
+
+ len = write(data->fd, "\n", 1);
+ if (len < 0)
+ return;
+ }
+
+ tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
+
+ close(data->fd);
+
+ value = g_string_free(data->str, FALSE);
+
+ if (data->cb)
+ data->cb(value, data->user_data);
+
+ g_free(value);
+
+ g_free(data);
+}
+
+static gboolean keyboard_input(GIOChannel *channel, GIOCondition condition,
+ gpointer user_data)
+{
+ struct user_input_data *data = user_data;
+ char buf[1];
+ int len;
+
+ len = read(data->fd, buf, 1);
+
+ if (len != 1)
+ return TRUE;
+
+ if (buf[0] == '\n') {
+ user_callback(data);
+ return FALSE;
+ }
+
+ g_string_append_c(data->str, buf[0]);
+
+ if (data->hidden)
+ len = write(data->fd, "*", 1);
+
+ return TRUE;
+}
+
+static bool user_input(const char *label, bool hidden,
+ user_input_cb func, gpointer user_data)
+{
+ struct user_input_data *data;
+ struct termios new_termios;
+ GIOChannel *channel;
+ guint watch;
+ ssize_t len;
+
+ data = g_try_new0(struct user_input_data, 1);
+ if (!data)
+ return false;
+
+ data->str = g_string_sized_new(32);
+ data->cb = func;
+ data->user_data = user_data;
+ data->hidden = hidden;
+
+ data->fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
+ if (data->fd < 0)
+ goto error;
+
+ if (tcgetattr(data->fd, &data->saved_termios) < 0) {
+ close(data->fd);
+ goto error;
+ }
+
+ new_termios = data->saved_termios;
+ if (data->hidden)
+ new_termios.c_lflag &= ~(ICANON|ECHO);
+ else
+ new_termios.c_lflag &= ~ICANON;
+ new_termios.c_cc[VMIN] = 1;
+ new_termios.c_cc[VTIME] = 0;
+
+ tcsetattr(data->fd, TCSADRAIN, &new_termios);
+
+ channel = g_io_channel_unix_new(data->fd);
+ g_io_channel_set_encoding(channel, NULL, NULL);
+ g_io_channel_set_buffered(channel, FALSE);
+ watch = g_io_add_watch(channel, G_IO_IN, keyboard_input, data);
+ g_io_channel_unref(channel);
+
+ if (watch == 0)
+ goto error;
+
+ len = write(data->fd, label, strlen(label));
+ if (len < 0)
+ goto error;
+
+ len = write(data->fd, ": ", 2);
+ if (len < 0)
+ goto error;
+
+ return true;
+
+error:
+ g_string_free(data->str, TRUE);
+ g_free(data);
+
+ return false;
+}
+
+static void password_callback(const char *value, gpointer user_data)
+{
+ struct wispr_session *wispr = user_data;
+
+ g_free(wispr->password);
+ wispr->password = g_strdup(value);
+
+ printf("\n");
+
+ execute_login(wispr);
+}
+
+static void username_callback(const char *value, gpointer user_data)
+{
+ struct wispr_session *wispr = user_data;
+
+ g_free(wispr->username);
+ wispr->username = g_strdup(value);
+
+ if (!wispr->password) {
+ user_input("Password", true, password_callback, wispr);
+ return;
+ }
+
+ printf("\n");
+
+ execute_login(wispr);
+}
+
+static bool wispr_input(const guint8 **data, gsize *length,