Index: uspace/lib/clui/tinput.c
===================================================================
--- uspace/lib/clui/tinput.c	(revision 5db9084cb9b925c6758f4bc0fac5c31d7125ff35)
+++ uspace/lib/clui/tinput.c	(revision 9f1362d4e7eaaca0f48042dc579d07341d14e38a)
@@ -40,6 +40,5 @@
 #include <assert.h>
 #include <bool.h>
-
-#include "tinput.h"
+#include <tinput.h>
 
 /** Seek direction */
@@ -49,16 +48,16 @@
 } seek_dir_t;
 
-static void tinput_init(tinput_t *ti);
-static void tinput_insert_string(tinput_t *ti, const char *str);
-static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb);
-static bool tinput_sel_active(tinput_t *ti);
-static void tinput_sel_all(tinput_t *ti);
-static void tinput_sel_delete(tinput_t *ti);
-static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev);
-static void tinput_key_shift(tinput_t *ti, console_event_t *ev);
-static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev);
-static void tinput_key_unmod(tinput_t *ti, console_event_t *ev);
-static void tinput_pre_seek(tinput_t *ti, bool shift_held);
-static void tinput_post_seek(tinput_t *ti, bool shift_held);
+static void tinput_init(tinput_t *);
+static void tinput_insert_string(tinput_t *, const char *);
+static void tinput_sel_get_bounds(tinput_t *, size_t *, size_t *);
+static bool tinput_sel_active(tinput_t *);
+static void tinput_sel_all(tinput_t *);
+static void tinput_sel_delete(tinput_t *);
+static void tinput_key_ctrl(tinput_t *, console_event_t *);
+static void tinput_key_shift(tinput_t *, console_event_t *);
+static void tinput_key_ctrl_shift(tinput_t *, console_event_t *);
+static void tinput_key_unmod(tinput_t *, console_event_t *);
+static void tinput_pre_seek(tinput_t *, bool);
+static void tinput_post_seek(tinput_t *, bool);
 
 /** Create a new text input field. */
@@ -66,9 +65,9 @@
 {
 	tinput_t *ti;
-
+	
 	ti = malloc(sizeof(tinput_t));
 	if (ti == NULL)
 		return NULL;
-
+	
 	tinput_init(ti);
 	return ti;
@@ -81,17 +80,17 @@
 }
 
-static void tinput_display_tail(tinput_t *ti, int start, int pad)
-{
-	static wchar_t dbuf[INPUT_MAX_SIZE + 1];
-	int sa, sb;
-	int i, p;
-
+static void tinput_display_tail(tinput_t *ti, size_t start, size_t pad)
+{
+	wchar_t dbuf[INPUT_MAX_SIZE + 1];
+	
+	size_t sa;
+	size_t sb;
 	tinput_sel_get_bounds(ti, &sa, &sb);
-
-	console_goto(fphone(stdout), (ti->col0 + start) % ti->con_cols,
+	
+	console_set_pos(fphone(stdout), (ti->col0 + start) % ti->con_cols,
 	    ti->row0 + (ti->col0 + start) / ti->con_cols);
-	console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
-
-	p = start;
+	console_set_style(fphone(stdout), STYLE_NORMAL);
+	
+	size_t p = start;
 	if (p < sa) {
 		memcpy(dbuf, ti->buffer + p, (sa - p) * sizeof(wchar_t));
@@ -100,8 +99,8 @@
 		p = sa;
 	}
-
+	
 	if (p < sb) {
 		fflush(stdout);
-		console_set_color(fphone(stdout), COLOR_BLACK, COLOR_RED, 0);
+		console_set_style(fphone(stdout), STYLE_SELECTED);
 		memcpy(dbuf, ti->buffer + p,
 		    (sb - p) * sizeof(wchar_t));
@@ -110,8 +109,8 @@
 		p = sb;
 	}
-
+	
 	fflush(stdout);
-	console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
-
+	console_set_style(fphone(stdout), STYLE_NORMAL);
+	
 	if (p < ti->nc) {
 		memcpy(dbuf, ti->buffer + p,
@@ -120,7 +119,8 @@
 		printf("%ls", dbuf);
 	}
-
-	for (i = 0; i < pad; ++i)
+	
+	for (p = 0; p < pad; p++)
 		putchar(' ');
+	
 	fflush(stdout);
 }
@@ -133,5 +133,5 @@
 static void tinput_position_caret(tinput_t *ti)
 {
-	console_goto(fphone(stdout), (ti->col0 + ti->pos) % ti->con_cols,
+	console_set_pos(fphone(stdout), (ti->col0 + ti->pos) % ti->con_cols,
 	    ti->row0 + (ti->col0 + ti->pos) / ti->con_cols);
 }
@@ -140,33 +140,31 @@
 static void tinput_update_origin(tinput_t *ti)
 {
-	int width, rows;
-
-	width = ti->col0 + ti->nc;
-	rows = (width / ti->con_cols) + 1;
-
+	ipcarg_t width = ti->col0 + ti->nc;
+	ipcarg_t rows = (width / ti->con_cols) + 1;
+	
 	/* Update row0 if the screen scrolled. */
 	if (ti->row0 + rows > ti->con_rows)
-		ti->row0 = ti->con_rows - rows;	
+		ti->row0 = ti->con_rows - rows;
 }
 
 static void tinput_insert_char(tinput_t *ti, wchar_t c)
 {
-	int i;
-	int new_width, new_height;
-
 	if (ti->nc == INPUT_MAX_SIZE)
 		return;
-
-	new_width = ti->col0 + ti->nc + 1;
+	
+	ipcarg_t new_width = ti->col0 + ti->nc + 1;
 	if (new_width % ti->con_cols == 0) {
 		/* Advancing to new line. */
-		new_height = (new_width / ti->con_cols) + 1;
-		if (new_height >= ti->con_rows)
-			return; /* Disallow text longer than 1 page for now. */
-	}
-
-	for (i = ti->nc; i > ti->pos; --i)
+		ipcarg_t new_height = (new_width / ti->con_cols) + 1;
+		if (new_height >= ti->con_rows) {
+			/* Disallow text longer than 1 page for now. */
+			return;
+		}
+	}
+	
+	size_t i;
+	for (i = ti->nc; i > ti->pos; i--)
 		ti->buffer[i] = ti->buffer[i - 1];
-
+	
 	ti->buffer[ti->pos] = c;
 	ti->pos += 1;
@@ -174,5 +172,5 @@
 	ti->buffer[ti->nc] = '\0';
 	ti->sel_start = ti->pos;
-
+	
 	tinput_display_tail(ti, ti->pos - 1, 0);
 	tinput_update_origin(ti);
@@ -182,41 +180,41 @@
 static void tinput_insert_string(tinput_t *ti, const char *str)
 {
-	int i;
-	int new_width, new_height;
-	int ilen;
-	wchar_t c;
-	size_t off;
-
-	ilen = min((ssize_t) str_length(str), INPUT_MAX_SIZE - ti->nc);
+	size_t ilen = min(str_length(str), INPUT_MAX_SIZE - ti->nc);
 	if (ilen == 0)
 		return;
-
-	new_width = ti->col0 + ti->nc + ilen;
-	new_height = (new_width / ti->con_cols) + 1;
-	if (new_height >= ti->con_rows)
-		return; /* Disallow text longer than 1 page for now. */
-
-	for (i = ti->nc - 1; i >= ti->pos; --i)
-		ti->buffer[i + ilen] = ti->buffer[i];
-
-	off = 0; i = 0;
+	
+	ipcarg_t new_width = ti->col0 + ti->nc + ilen;
+	ipcarg_t new_height = (new_width / ti->con_cols) + 1;
+	if (new_height >= ti->con_rows) {
+		/* Disallow text longer than 1 page for now. */
+		return;
+	}
+	
+	if (ti->nc > 0) {
+		size_t i;
+		for (i = ti->nc; i > ti->pos; i--)
+			ti->buffer[i + ilen - 1] = ti->buffer[i - 1];
+	}
+	
+	size_t off = 0;
+	size_t i = 0;
 	while (i < ilen) {
-		c = str_decode(str, &off, STR_NO_LIMIT);
+		wchar_t c = str_decode(str, &off, STR_NO_LIMIT);
 		if (c == '\0')
 			break;
-
+		
 		/* Filter out non-printable chars. */
 		if (c < 32)
 			c = 32;
-
+		
 		ti->buffer[ti->pos + i] = c;
-		++i;
-	}
-
+		i++;
+	}
+	
 	ti->pos += ilen;
 	ti->nc += ilen;
 	ti->buffer[ti->nc] = '\0';
 	ti->sel_start = ti->pos;
-
+	
 	tinput_display_tail(ti, ti->pos - ilen, 0);
 	tinput_update_origin(ti);
@@ -226,21 +224,21 @@
 static void tinput_backspace(tinput_t *ti)
 {
-	int i;
-
 	if (tinput_sel_active(ti)) {
 		tinput_sel_delete(ti);
 		return;
 	}
-
+	
 	if (ti->pos == 0)
 		return;
-
-	for (i = ti->pos; i < ti->nc; ++i)
+	
+	size_t i;
+	for (i = ti->pos; i < ti->nc; i++)
 		ti->buffer[i - 1] = ti->buffer[i];
+	
 	ti->pos -= 1;
 	ti->nc -= 1;
 	ti->buffer[ti->nc] = '\0';
 	ti->sel_start = ti->pos;
-
+	
 	tinput_display_tail(ti, ti->pos, 1);
 	tinput_position_caret(ti);
@@ -253,11 +251,11 @@
 		return;
 	}
-
+	
 	if (ti->pos == ti->nc)
 		return;
-
+	
 	ti->pos += 1;
 	ti->sel_start = ti->pos;
-
+	
 	tinput_backspace(ti);
 }
@@ -266,5 +264,5 @@
 {
 	tinput_pre_seek(ti, shift_held);
-
+	
 	if (dir == seek_forward) {
 		if (ti->pos < ti->nc)
@@ -274,5 +272,5 @@
 			ti->pos -= 1;
 	}
-
+	
 	tinput_post_seek(ti, shift_held);
 }
@@ -281,17 +279,17 @@
 {
 	tinput_pre_seek(ti, shift_held);
-
+	
 	if (dir == seek_forward) {
 		if (ti->pos == ti->nc)
 			return;
-
-		while (1) {
+		
+		while (true) {
 			ti->pos += 1;
-
+			
 			if (ti->pos == ti->nc)
 				break;
-
-			if (ti->buffer[ti->pos - 1] == ' ' &&
-			    ti->buffer[ti->pos] != ' ')
+			
+			if ((ti->buffer[ti->pos - 1] == ' ') &&
+			    (ti->buffer[ti->pos] != ' '))
 				break;
 		}
@@ -299,18 +297,18 @@
 		if (ti->pos == 0)
 			return;
-
-		while (1) {
+		
+		while (true) {
 			ti->pos -= 1;
-
+			
 			if (ti->pos == 0)
 				break;
-
+			
 			if (ti->buffer[ti->pos - 1] == ' ' &&
 			    ti->buffer[ti->pos] != ' ')
 				break;
 		}
-
-	}
-
+	
+	}
+	
 	tinput_post_seek(ti, shift_held);
 }
@@ -319,13 +317,13 @@
 {
 	tinput_pre_seek(ti, shift_held);
-
+	
 	if (dir == seek_forward) {
 		if (ti->pos + ti->con_cols <= ti->nc)
 			ti->pos = ti->pos + ti->con_cols;
 	} else {
-		if (ti->pos - ti->con_cols >= 0)
+		if (ti->pos >= ti->con_cols)
 			ti->pos = ti->pos - ti->con_cols;
 	}
-
+	
 	tinput_post_seek(ti, shift_held);
 }
@@ -334,10 +332,10 @@
 {
 	tinput_pre_seek(ti, shift_held);
-
+	
 	if (dir == seek_backward)
 		ti->pos = 0;
 	else
 		ti->pos = ti->nc;
-
+	
 	tinput_post_seek(ti, shift_held);
 }
@@ -345,5 +343,5 @@
 static void tinput_pre_seek(tinput_t *ti, bool shift_held)
 {
-	if (tinput_sel_active(ti) && !shift_held) {
+	if ((tinput_sel_active(ti)) && (!shift_held)) {
 		/* Unselect and redraw. */
 		ti->sel_start = ti->pos;
@@ -362,4 +360,5 @@
 		ti->sel_start = ti->pos;
 	}
+	
 	tinput_position_caret(ti);
 }
@@ -367,6 +366,4 @@
 static void tinput_history_insert(tinput_t *ti, char *str)
 {
-	int i;
-
 	if (ti->hnum < HISTORY_LEN) {
 		ti->hnum += 1;
@@ -375,10 +372,11 @@
 			free(ti->history[HISTORY_LEN]);
 	}
-
-	for (i = ti->hnum; i > 1; --i)
+	
+	size_t i;
+	for (i = ti->hnum; i > 1; i--)
 		ti->history[i] = ti->history[i - 1];
-
+	
 	ti->history[1] = str_dup(str);
-
+	
 	if (ti->history[0] != NULL) {
 		free(ti->history[0]);
@@ -395,5 +393,5 @@
 }
 
-static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb)
+static void tinput_sel_get_bounds(tinput_t *ti, size_t *sa, size_t *sb)
 {
 	if (ti->sel_start < ti->pos) {
@@ -408,5 +406,5 @@
 static bool tinput_sel_active(tinput_t *ti)
 {
-	return ti->sel_start != ti->pos;
+	return (ti->sel_start != ti->pos);
 }
 
@@ -421,16 +419,18 @@
 static void tinput_sel_delete(tinput_t *ti)
 {
-	int sa, sb;
-
+	size_t sa;
+	size_t sb;
+	
 	tinput_sel_get_bounds(ti, &sa, &sb);
 	if (sa == sb)
 		return;
-
+	
 	memmove(ti->buffer + sa, ti->buffer + sb,
 	    (ti->nc - sb) * sizeof(wchar_t));
+	
 	ti->pos = ti->sel_start = sa;
 	ti->nc -= (sb - sa);
 	ti->buffer[ti->nc] = '\0';
-
+	
 	tinput_display_tail(ti, sa, sb - sa);
 	tinput_position_caret(ti);
@@ -439,9 +439,11 @@
 static void tinput_sel_copy_to_cb(tinput_t *ti)
 {
-	int sa, sb;
+	size_t sa;
+	size_t sb;
+	
+	tinput_sel_get_bounds(ti, &sa, &sb);
+	
 	char *str;
-
-	tinput_sel_get_bounds(ti, &sa, &sb);
-
+	
 	if (sb < ti->nc) {
 		wchar_t tmp_c = ti->buffer[sb];
@@ -454,13 +456,14 @@
 	if (str == NULL)
 		goto error;
-
+	
 	if (clipboard_put_str(str) != EOK)
 		goto error;
-
+	
 	free(str);
 	return;
+	
 error:
+	/* TODO: Give the user some kind of warning. */
 	return;
-	/* TODO: Give the user some warning. */
 }
 
@@ -468,10 +471,11 @@
 {
 	char *str;
-	int rc;
-
-	rc = clipboard_get_str(&str);
-	if (rc != EOK || str == NULL)
-		return; /* TODO: Give the user some warning. */
-
+	int rc = clipboard_get_str(&str);
+	
+	if ((rc != EOK) || (str == NULL)) {
+		/* TODO: Give the user some kind of warning. */
+		return;
+	}
+	
 	tinput_insert_string(ti, str);
 	free(str);
@@ -480,20 +484,24 @@
 static void tinput_history_seek(tinput_t *ti, int offs)
 {
-	int pad;
-
-	if (ti->hpos + offs < 0 || ti->hpos + offs > ti->hnum)
-		return;
-
+	if (offs >= 0) {
+		if (ti->hpos + offs > ti->hnum)
+			return;
+	} else {
+		if (ti->hpos < (size_t) -offs)
+			return;
+	}
+	
 	if (ti->history[ti->hpos] != NULL) {
 		free(ti->history[ti->hpos]);
 		ti->history[ti->hpos] = NULL;
 	}
-
+	
 	ti->history[ti->hpos] = tinput_get_str(ti);
 	ti->hpos += offs;
-
-	pad = ti->nc - str_length(ti->history[ti->hpos]);
-	if (pad < 0) pad = 0;
-
+	
+	int pad = (int) ti->nc - str_length(ti->history[ti->hpos]);
+	if (pad < 0)
+		pad = 0;
+	
 	tinput_set_str(ti, ti->history[ti->hpos]);
 	tinput_display_tail(ti, 0, pad);
@@ -515,55 +523,54 @@
 /** Read in one line of input.
  *
- * @param ti	Text input.
- * @param dstr	Place to save pointer to new string.
- * @return	EOK on success, ENOENT if user requested abort, EIO
- *		if communication with console failed.
+ * @param ti   Text input.
+ * @param dstr Place to save pointer to new string.
+ *
+ * @return EOK on success
+ * @return ENOENT if user requested abort
+ * @return EIO if communication with console failed
+ *
  */
 int tinput_read(tinput_t *ti, char **dstr)
 {
-	console_event_t ev;
-	char *str;
-
 	fflush(stdout);
-
 	if (console_get_size(fphone(stdin), &ti->con_cols, &ti->con_rows) != EOK)
 		return EIO;
+	
 	if (console_get_pos(fphone(stdin), &ti->col0, &ti->row0) != EOK)
 		return EIO;
-
-	ti->pos = ti->sel_start = 0;
+	
+	ti->pos = 0;
+	ti->sel_start = 0;
 	ti->nc = 0;
 	ti->buffer[0] = '\0';
 	ti->done = false;
 	ti->exit_clui = false;
-
+	
 	while (!ti->done) {
 		fflush(stdout);
+		
+		console_event_t ev;
 		if (!console_get_event(fphone(stdin), &ev))
 			return EIO;
-
+		
 		if (ev.type != KEY_PRESS)
 			continue;
-
-		if ((ev.mods & KM_CTRL) != 0 &&
-		    (ev.mods & (KM_ALT | KM_SHIFT)) == 0) {
+		
+		if (((ev.mods & KM_CTRL) != 0) &&
+		    ((ev.mods & (KM_ALT | KM_SHIFT)) == 0))
 			tinput_key_ctrl(ti, &ev);
-		}
-
-		if ((ev.mods & KM_SHIFT) != 0 &&
-		    (ev.mods & (KM_CTRL | KM_ALT)) == 0) {
+		
+		if (((ev.mods & KM_SHIFT) != 0) &&
+		    ((ev.mods & (KM_CTRL | KM_ALT)) == 0))
 			tinput_key_shift(ti, &ev);
-		}
-
-		if ((ev.mods & KM_CTRL) != 0 &&
-		    (ev.mods & KM_SHIFT) != 0 &&
-		    (ev.mods & KM_ALT) == 0) {
+		
+		if (((ev.mods & KM_CTRL) != 0) &&
+		    ((ev.mods & KM_SHIFT) != 0) &&
+		    ((ev.mods & KM_ALT) == 0))
 			tinput_key_ctrl_shift(ti, &ev);
-		}
-
-		if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
+		
+		if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0)
 			tinput_key_unmod(ti, &ev);
-		}
-
+		
 		if (ev.c >= ' ') {
 			tinput_sel_delete(ti);
@@ -571,18 +578,18 @@
 		}
 	}
-
+	
 	if (ti->exit_clui)
 		return ENOENT;
-
+	
 	ti->pos = ti->nc;
 	tinput_position_caret(ti);
 	putchar('\n');
-
-	str = tinput_get_str(ti);
+	
+	char *str = tinput_get_str(ti);
 	if (str_cmp(str, "") != 0)
 		tinput_history_insert(ti, str);
-
+	
 	ti->hpos = 0;
-
+	
 	*dstr = str;
 	return EOK;
@@ -700,5 +707,5 @@
 		break;
 	case KC_UP:
-		tinput_history_seek(ti, +1);
+		tinput_history_seek(ti, 1);
 		break;
 	case KC_DOWN:
