Browse Source

tinyrl: tinyrl_line_insert()

Serj Kalichev 3 months ago
parent
commit
cd344f6257
5 changed files with 77 additions and 94 deletions
  1. 2 1
      tinyrl/tinyrl.h
  2. 22 26
      tinyrl/tinyrl/keys.c
  3. 19 18
      tinyrl/tinyrl/private.h
  4. 33 48
      tinyrl/tinyrl/tinyrl.c
  5. 1 1
      tinyrl/vt100/vt100.c

+ 2 - 1
tinyrl/tinyrl.h

@@ -58,7 +58,7 @@ typedef int tinyrl_timeout_fn_t(tinyrl_t *instance);
  *   been performed successfully
  * - BOOL_FALSE if the action was not successful
  */
-typedef bool_t tinyrl_key_func_t(tinyrl_t * instance, int key);
+typedef bool_t tinyrl_key_func_t(tinyrl_t *instance, char key);
 
 
 
@@ -68,6 +68,7 @@ tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
 void tinyrl_free(tinyrl_t *tinyrl);
 
 bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn);
+void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn);
 void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream);
 FILE *tinyrl_istream(const tinyrl_t *tinyrl);
 void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream);

+ 22 - 26
tinyrl/tinyrl/keys.c

@@ -12,26 +12,22 @@
 #include "private.h"
 
 
-bool_t tinyrl_key_default(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_default(tinyrl_t * tinyrl, char key)
 {
-	bool_t result = BOOL_FALSE;
-/*
 	if (key > 31) {
-		char tmp[2];
-		tmp[0] = (key & 0xFF), tmp[1] = '\0';
-		// inject tinyrl text into the buffer 
-		result = tinyrl_insert_text(tinyrl, tmp);
+		// Inject new char to the line
+		tinyrl_line_insert(tinyrl, &key, 1);
 	} else {
-		// Call the external hotkey analyzer 
+		// Call the external hotkey analyzer
 		if (tinyrl->hotkey_fn)
 			tinyrl->hotkey_fn(tinyrl, key);
 	}
-*/
-	return result;
+
+	return BOOL_TRUE;
 }
 
 
-bool_t tinyrl_key_interrupt(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_interrupt(tinyrl_t * tinyrl, char key)
 {
 /*
 	tinyrl_crlf(tinyrl);
@@ -44,7 +40,7 @@ bool_t tinyrl_key_interrupt(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_start_of_line(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_start_of_line(tinyrl_t * tinyrl, char key)
 {
 /*
 	// set the insertion point to the start of the line 
@@ -56,7 +52,7 @@ bool_t tinyrl_key_start_of_line(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_end_of_line(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_end_of_line(tinyrl_t * tinyrl, char key)
 {
 /*
 	// set the insertion point to the end of the line 
@@ -68,7 +64,7 @@ bool_t tinyrl_key_end_of_line(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_kill(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_kill(tinyrl_t * tinyrl, char key)
 {
 /*
 	// release any old kill string 
@@ -86,7 +82,7 @@ bool_t tinyrl_key_kill(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_yank(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_yank(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -101,7 +97,7 @@ bool_t tinyrl_key_yank(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_crlf(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_crlf(tinyrl_t * tinyrl, char key)
 {
 /*
 	tinyrl_crlf(tinyrl);
@@ -113,7 +109,7 @@ bool_t tinyrl_key_crlf(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_up(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_up(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -140,7 +136,7 @@ bool_t tinyrl_key_up(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_down(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_down(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -167,7 +163,7 @@ bool_t tinyrl_key_down(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_left(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_left(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -183,7 +179,7 @@ bool_t tinyrl_key_left(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_right(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_right(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -199,7 +195,7 @@ bool_t tinyrl_key_right(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, int key)
+bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -216,7 +212,7 @@ bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_backword(tinyrl_t *tinyrl, int key)
+bool_t tinyrl_key_backword(tinyrl_t *tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -236,7 +232,7 @@ bool_t tinyrl_key_backword(tinyrl_t *tinyrl, int key)
 	return result;
 }
 
-bool_t tinyrl_key_delete(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_delete(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*
@@ -253,7 +249,7 @@ bool_t tinyrl_key_delete(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_clear_screen(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_clear_screen(tinyrl_t * tinyrl, char key)
 {
 /*
 	tinyrl_vt100_clear_screen(tinyrl->term);
@@ -268,7 +264,7 @@ bool_t tinyrl_key_clear_screen(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_erase_line(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_erase_line(tinyrl_t * tinyrl, char key)
 {
 /*	unsigned int end;
 
@@ -299,7 +295,7 @@ bool_t tinyrl_key_erase_line(tinyrl_t * tinyrl, int key)
 }
 
 
-bool_t tinyrl_key_tab(tinyrl_t * tinyrl, int key)
+bool_t tinyrl_key_tab(tinyrl_t * tinyrl, char key)
 {
 	bool_t result = BOOL_FALSE;
 /*

+ 19 - 18
tinyrl/tinyrl/private.h

@@ -14,26 +14,27 @@ char *utf8_move_left(const char *line, char *cur_pos);
 char *utf8_move_right(const char *line, char *cur_pos);
 
 // Keys
-bool_t tinyrl_key_default(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_interrupt(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_start_of_line(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_end_of_line(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_kill(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_yank(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_crlf(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_up(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_down(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_left(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_right(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, int key);
-bool_t tinyrl_key_backword(tinyrl_t *tinyrl, int key);
-bool_t tinyrl_key_delete(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_clear_screen(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_erase_line(tinyrl_t * tinyrl, int key);
-bool_t tinyrl_key_tab(tinyrl_t * tinyrl, int key);
+bool_t tinyrl_key_default(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_interrupt(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_start_of_line(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_end_of_line(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_kill(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_yank(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_crlf(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_up(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_down(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_left(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_right(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, char key);
+bool_t tinyrl_key_backword(tinyrl_t *tinyrl, char key);
+bool_t tinyrl_key_delete(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_clear_screen(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_erase_line(tinyrl_t * tinyrl, char key);
+bool_t tinyrl_key_tab(tinyrl_t * tinyrl, char key);
 
 // Tinyrl
-bool_t tinyrl_extend_line(tinyrl_t *tinyrl, size_t len);
+bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len);
+bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len);
 bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq);
 
 

+ 33 - 48
tinyrl/tinyrl/tinyrl.c

@@ -30,7 +30,7 @@ tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
 
 	// Line
 	faux_bzero(&tinyrl->line, sizeof(tinyrl->line));
-	tinyrl_extend_line(tinyrl, LINE_CHUNK);
+	tinyrl_line_extend(tinyrl, LINE_CHUNK);
 
 	// Input processing vars
 	tinyrl->utf8_cont = 0;
@@ -163,6 +163,12 @@ bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn)
 }
 
 
+void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn)
+{
+	tinyrl->hotkey_fn = fn;
+}
+
+
 void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream)
 {
 	assert(tinyrl);
@@ -301,6 +307,7 @@ static bool_t process_char(tinyrl_t *tinyrl, char key)
 	// all bytes for the current multibyte character
 //	if (!utf8_cont)
 //		tinyrl_redisplay(tinyrl);
+printf("%s\n", tinyrl->line.str);
 
 	return BOOL_TRUE;
 }
@@ -331,7 +338,7 @@ int tinyrl_read(tinyrl_t *tinyrl)
  * possibly reallocating it if necessary. The function returns BOOL_TRUE
  * if the line is successfully extended, BOOL_FALSE if not.
  */
-bool_t tinyrl_extend_line(tinyrl_t *tinyrl, size_t len)
+bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
 {
 	char *new_buf = NULL;
 	size_t new_size = 0;
@@ -401,6 +408,30 @@ bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
 }
 
 
+bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
+{
+	size_t new_size = tinyrl->line.len + len + 1;
+
+	if (len == 0)
+		return BOOL_TRUE;
+
+	tinyrl_line_extend(tinyrl, new_size);
+
+	if (tinyrl->line.pos < tinyrl->line.len) {
+		memmove(tinyrl->line.str + tinyrl->line.pos + len,
+			tinyrl->line.str + tinyrl->line.pos,
+			len);
+	}
+
+	memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
+	tinyrl->line.pos += len;
+	tinyrl->line.len += len;
+	tinyrl->line.str[tinyrl->line.len] = '\0';
+
+	return BOOL_TRUE;
+}
+
+
 #if 0
 
 /*----------------------------------------------------------------------- */
@@ -748,46 +779,6 @@ char *tinyrl_forceline(tinyrl_t * tinyrl, void *context, const char *line)
 }
 
 
-/*----------------------------------------------------------------------- */
-/*
- * Insert text into the line at the current cursor position.
- */
-bool_t tinyrl_insert_text(tinyrl_t * tinyrl, const char *text)
-{
-	unsigned int delta = strlen(text);
-
-	/*
-	 * If the client wants to change the line ensure that the line and buffer
-	 * references are in sync
-	 */
-	changed_line(tinyrl);
-
-	if ((delta + tinyrl->end) > (tinyrl->buffer_size)) {
-		/* extend the current buffer */
-		if (BOOL_FALSE ==
-			tinyrl_extend_line_buffer(tinyrl, tinyrl->end + delta))
-			return BOOL_FALSE;
-	}
-
-	if (tinyrl->point < tinyrl->end) {
-		/* move the current text to the right (including the terminator) */
-		memmove(&tinyrl->buffer[tinyrl->point + delta],
-			&tinyrl->buffer[tinyrl->point],
-			(tinyrl->end - tinyrl->point) + 1);
-	} else {
-		/* terminate the string */
-		tinyrl->buffer[tinyrl->end + delta] = '\0';
-	}
-
-	/* insert the new text */
-	strncpy(&tinyrl->buffer[tinyrl->point], text, delta);
-
-	/* now update the indexes */
-	tinyrl->point += delta;
-	tinyrl->end += delta;
-
-	return BOOL_TRUE;
-}
 
 /*----------------------------------------------------------------------- */
 /* 
@@ -1179,12 +1170,6 @@ void tinyrl__set_prompt(tinyrl_t *tinyrl, const char *prompt)
 }
 
 
-/*-------------------------------------------------------- */
-void tinyrl__set_hotkey_fn(tinyrl_t *tinyrl,
-	tinyrl_key_func_t *fn)
-{
-	tinyrl->hotkey_fn = fn;
-}
 
 /*-------------------------------------------------------- */
 bool_t tinyrl_is_quoting(const tinyrl_t * tinyrl)

+ 1 - 1
tinyrl/vt100/vt100.c

@@ -151,7 +151,7 @@ int vt100_getchar(const vt100_t *vt100, char *c)
 		return -1;
 	}
 
-	return read(fileno(vt100->istream), &c, 1);
+	return read(fileno(vt100->istream), c, 1);
 }