Browse Source

tinyrl: Key enter

Serj Kalichev 2 months ago
parent
commit
1ca3b5fa20
4 changed files with 136 additions and 91 deletions
  1. 26 3
      bin/klish/interactive.c
  2. 13 27
      tinyrl/tinyrl.h
  3. 1 8
      tinyrl/tinyrl/private.h
  4. 96 53
      tinyrl/tinyrl/tinyrl.c

+ 26 - 3
bin/klish/interactive.c

@@ -22,6 +22,9 @@ bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
 static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
 	void *associated_data, void *user_data);
 
+// Keys
+static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
+
 
 int klish_interactive_shell(ktp_session_t *ktp)
 {
@@ -39,6 +42,9 @@ int klish_interactive_shell(ktp_session_t *ktp)
 	fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
 
 	tinyrl = tinyrl_new(stdin, stdout, NULL, 0);
+	tinyrl_set_prompt(tinyrl, "$ ");
+	tinyrl_set_udata(tinyrl, &ctx);
+	tinyrl_bind_key(tinyrl, KEY_CR, tinyrl_key_enter);
 
 	ctx.ktp = ktp;
 	ctx.tinyrl = tinyrl;
@@ -62,14 +68,12 @@ int klish_interactive_shell(ktp_session_t *ktp)
 
 bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
 {
-	printf("CMD ACK CB\n");
-
 	// Happy compiler
 	ktp = ktp;
 	msg = msg;
 	udata = udata;
 
-	ktp_session_set_done(ktp, BOOL_TRUE);
+//	ktp_session_set_done(ktp, BOOL_TRUE);
 
 	return BOOL_TRUE;
 }
@@ -86,3 +90,22 @@ static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
 	return BOOL_TRUE;
 }
 
+
+static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
+{
+	const char *line = NULL;
+	ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
+
+	tinyrl_multi_crlf(tinyrl);
+
+	line = tinyrl_line(tinyrl);
+	if (line) {
+		printf("cmd = %s\n", line);
+		ktp_session_cmd(ctx->ktp, line, NULL, BOOL_FALSE);
+	}
+
+	tinyrl_reset_line_state(tinyrl);
+	tinyrl_reset_line(tinyrl);
+
+	return BOOL_TRUE;
+}

+ 13 - 27
tinyrl/tinyrl.h

@@ -5,6 +5,8 @@
 
 #include <faux/faux.h>
 
+#include "tinyrl/vt100.h"
+
 C_DECL_BEGIN
 
 typedef struct tinyrl_s tinyrl_t;
@@ -75,23 +77,26 @@ void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream);
 FILE *tinyrl_ostream(const tinyrl_t *tinyrl);
 void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8);
 bool_t tinyrl_utf8(const tinyrl_t *tinyrl);
+void tinyrl_set_prompt(tinyrl_t *tinyrl, const char *prompt);
+const char *tinyrl_prompt(const tinyrl_t *tinyrl);
+const char *tinyrl_line(const tinyrl_t *tinyrl);
 bool_t tinyrl_hist_save(const tinyrl_t *tinyrl);
 bool_t tinyrl_hist_restore(tinyrl_t *tinyrl);
+void *tinyrl_udata(const tinyrl_t *tinyrl);
+void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata);
 void tty_raw_mode(tinyrl_t *tinyrl);
 void tty_restore_mode(tinyrl_t *tinyrl);
 int tinyrl_read(tinyrl_t *tinyrl);
+void tinyrl_redisplay(tinyrl_t *tinyrl);
+void tinyrl_save_last(tinyrl_t *tinyrl);
+void tinyrl_reset_line_state(tinyrl_t *tinyrl);
+void tinyrl_reset_line(tinyrl_t *tinyrl);
+void tinyrl_crlf(const tinyrl_t *tinyrl);
+void tinyrl_multi_crlf(const tinyrl_t *tinyrl);
 
 
 
 
-
-
-/*lint -esym(534,tinyrl_printf)  Ignoring return value of function */
-extern int tinyrl_printf(const tinyrl_t * instance, const char *fmt, ...);
-
-extern const char *tinyrl__get_prompt(const tinyrl_t * instance);
-extern void tinyrl__set_prompt(tinyrl_t *instance, const char *prompt);
-
 extern void tinyrl_done(tinyrl_t * instance);
 
 extern void tinyrl_completion_over(tinyrl_t * instance);
@@ -100,35 +105,19 @@ extern void tinyrl_completion_error_over(tinyrl_t * instance);
 
 extern bool_t tinyrl_is_completion_error_over(const tinyrl_t * instance);
 
-extern void *tinyrl__get_context(const tinyrl_t * instance);
 
 /**
  * This operation returns the current line in use by the tinyrl instance
  * NB. the pointer will become invalid after any further operation on the 
  * instance.
  */
-extern const char *tinyrl__get_line(const tinyrl_t * instance);
-extern bool_t tinyrl__get_utf8(const tinyrl_t * instance);
-extern void tinyrl__set_utf8(tinyrl_t * instance, bool_t utf8);
-extern void tinyrl__set_hotkey_fn(tinyrl_t *instance,
-	tinyrl_key_func_t *fn);
-extern char *tinyrl_readline(tinyrl_t *instance, void *context);
-extern char *tinyrl_forceline(tinyrl_t *instance, 
-	void *context, const char *line);
 extern void tinyrl_delete_matches(char **instance);
 extern char **tinyrl_completion(tinyrl_t *instance,
 	const char *line, unsigned start, unsigned end,
 	tinyrl_compentry_func_t *generator);
-extern void tinyrl_crlf(const tinyrl_t * instance);
-extern void tinyrl_multi_crlf(const tinyrl_t * instance);
 extern void tinyrl_ding(const tinyrl_t * instance);
 
-extern void tinyrl_reset_line_state(tinyrl_t * instance);
 
-extern bool_t tinyrl_insert_text(tinyrl_t * instance, const char *text);
-extern void
-tinyrl_delete_text(tinyrl_t * instance, unsigned start, unsigned end);
-extern void tinyrl_redisplay(tinyrl_t * instance);
 
 extern void
 tinyrl_replace_line(tinyrl_t * instance, const char *text, int clear_undo);
@@ -215,9 +204,6 @@ extern void tinyrl_limit_line_length(
          */
 					    unsigned length);
 
-extern unsigned tinyrl__get_width(const tinyrl_t *instance);
-extern unsigned tinyrl__get_height(const tinyrl_t *instance);
-
 C_DECL_END
 
 #endif				/* _tinyrl_tinyrl_h */

+ 1 - 8
tinyrl/tinyrl/private.h

@@ -63,6 +63,7 @@ struct tinyrl_s {
 	char *prompt;
 	size_t prompt_len; // strlen()
 	size_t prompt_chars; // Symbol positions
+	void *udata; // Arbitrary user data
 
 	// Input processing vars. Input is processed char by char so
 	// the current state of processing is necessary.
@@ -82,14 +83,6 @@ struct tinyrl_s {
 	int state;
 #define RL_STATE_COMPLETING (0x00000001)
 	char *kill_string;
-	void *context;		/* context supplied by caller
-				 * to tinyrl_readline()
-				 */
 	char echo_char;
 	bool_t echo_enabled;
-	char *last_buffer;	/* hold record of the previous
-				buffer for redisplay purposes */
-	unsigned int last_point; /* hold record of the previous
-				cursor position for redisplay purposes */
-	unsigned int last_line_size; /* The length of last_buffer */
 };

+ 96 - 53
tinyrl/tinyrl/tinyrl.c

@@ -32,6 +32,9 @@ tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
 	faux_bzero(&tinyrl->line, sizeof(tinyrl->line));
 	tinyrl_line_extend(tinyrl, LINE_CHUNK);
 
+	// Last line
+	tinyrl_reset_line_state(tinyrl);
+
 	// Input processing vars
 	tinyrl->utf8_cont = 0;
 	tinyrl->esc_cont = BOOL_FALSE;
@@ -39,9 +42,7 @@ tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
 	tinyrl->esc_p = tinyrl->esc_seq;
 
 	// Prompt
-	tinyrl->prompt = faux_str_dup("> ");
-	tinyrl->prompt_len = strlen(tinyrl->prompt);
-	tinyrl->prompt_chars = utf8_nsyms(tinyrl->prompt, tinyrl->prompt_len);
+	tinyrl_set_prompt(tinyrl, "> ");
 
 	// Key handlers
 	for (i = 0; i < NUM_HANDLERS; i++) {
@@ -73,9 +74,6 @@ tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
 	tinyrl->kill_string = NULL;
 	tinyrl->echo_char = '\0';
 	tinyrl->echo_enabled = BOOL_TRUE;
-	tinyrl->last_buffer = NULL;
-	tinyrl->last_point = 0;
-	tinyrl->last_line_size = 0;
 	tinyrl->utf8 = BOOL_TRUE;
 
 	// VT100 terminal
@@ -104,12 +102,10 @@ void tinyrl_free(tinyrl_t *tinyrl)
 
 	hist_free(tinyrl->hist);
 	vt100_free(tinyrl->term);
-
 	faux_str_free(tinyrl->prompt);
+	tinyrl_reset_line_state(tinyrl);
 
-	faux_str_free(tinyrl->buffer);
-	faux_str_free(tinyrl->kill_string);
-	faux_str_free(tinyrl->last_buffer);
+//	faux_str_free(tinyrl->kill_string);
 
 	faux_free(tinyrl);
 }
@@ -228,6 +224,56 @@ void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8)
 }
 
 
+const char *tinyrl_prompt(const tinyrl_t *tinyrl)
+{
+	assert(tinyrl);
+	if (!tinyrl)
+		return NULL;
+
+	return tinyrl->prompt;
+}
+
+
+void tinyrl_set_prompt(tinyrl_t *tinyrl, const char *prompt)
+{
+	assert(tinyrl);
+	if (!tinyrl)
+		return;
+
+	if (tinyrl->prompt)
+		faux_str_free(tinyrl->prompt);
+	tinyrl->prompt = faux_str_dup(prompt);
+	tinyrl->prompt_len = strlen(tinyrl->prompt);
+	tinyrl->prompt_chars = utf8_nsyms(tinyrl->prompt, tinyrl->prompt_len);
+}
+
+
+void *tinyrl_udata(const tinyrl_t *tinyrl)
+{
+	assert(tinyrl);
+	if (!tinyrl)
+		return NULL;
+
+	return tinyrl->udata;
+}
+
+
+void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata)
+{
+	assert(tinyrl);
+	if (!tinyrl)
+		return;
+
+	tinyrl->udata = udata;
+}
+
+
+const char *tinyrl_line(const tinyrl_t *tinyrl)
+{
+	return tinyrl->line.str;
+}
+
+
 bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
 {
 	assert(tinyrl);
@@ -518,6 +564,27 @@ static size_t str_equal_part(const tinyrl_t *tinyrl,
 }
 
 
+void tinyrl_save_last(tinyrl_t *tinyrl)
+{
+	faux_str_free(tinyrl->last.str);
+	tinyrl->last = tinyrl->line;
+	tinyrl->last.str = faux_str_dup(tinyrl->line.str);
+}
+
+
+void tinyrl_reset_line_state(tinyrl_t *tinyrl)
+{
+	faux_str_free(tinyrl->last.str);
+	faux_bzero(&tinyrl->last, sizeof(tinyrl->last));
+}
+
+
+void tinyrl_reset_line(tinyrl_t *tinyrl)
+{
+	tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
+}
+
+
 void tinyrl_redisplay(tinyrl_t *tinyrl)
 {
 	size_t width = vt100_width(tinyrl->term);
@@ -556,8 +623,6 @@ void tinyrl_redisplay(tinyrl_t *tinyrl)
 	// Move the cursor to the insertion point
 	if (tinyrl->line.pos < tinyrl->line.len) {
 		size_t pos_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.pos);
-//		count = utf8_nsyms(tinyrl, tinyrl->line + tinyrl->point,
-//			line_size - tinyrl->point);
 		move_cursor(tinyrl, tinyrl->prompt_chars + line_chars,
 			tinyrl->prompt_chars + pos_chars);
 	}
@@ -566,14 +631,30 @@ void tinyrl_redisplay(tinyrl_t *tinyrl)
 	vt100_oflush(tinyrl->term);
 
 	// Save the last line buffer
-	faux_str_free(tinyrl->last.str);
-	tinyrl->last = tinyrl->line;
-	tinyrl->last.str = faux_str_dup(tinyrl->line.str);
+	tinyrl_save_last(tinyrl);
 
 	tinyrl->width = width;
 }
 
 
+void tinyrl_crlf(const tinyrl_t *tinyrl)
+{
+	vt100_printf(tinyrl->term, "\n");
+}
+
+
+// Jump to first free line after current multiline input
+void tinyrl_multi_crlf(const tinyrl_t *tinyrl)
+{
+	size_t full_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.len);
+	size_t pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
+
+	move_cursor(tinyrl, tinyrl->prompt_chars + pos_chars,
+		tinyrl->prompt_chars + full_chars);
+	tinyrl_crlf(tinyrl);
+	vt100_oflush(tinyrl->term);
+}
+
 #if 0
 
 /*----------------------------------------------------------------------- */
@@ -596,19 +677,6 @@ static void changed_line(tinyrl_t * tinyrl)
 
 
 
-/*-------------------------------------------------------- */
-/* Jump to first free line after current multiline input   */
-void tinyrl_multi_crlf(const tinyrl_t * tinyrl)
-{
-	unsigned int line_size = strlen(tinyrl->last_buffer);
-	unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, line_size);
-	unsigned int count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
-
-	tinyrl_internal_position(tinyrl, tinyrl->prompt_len + line_len,
-		- (line_len - count), tinyrl->width);
-	tinyrl_crlf(tinyrl);
-	tinyrl_vt100_oflush(tinyrl->term);
-}
 
 
 
@@ -733,10 +801,6 @@ void tinyrl_delete_matches(char **tinyrl)
 }
 
 /*-------------------------------------------------------- */
-void tinyrl_crlf(const tinyrl_t * tinyrl)
-{
-	tinyrl_vt100_printf(tinyrl->term, "\n");
-}
 
 /*-------------------------------------------------------- */
 /*
@@ -931,27 +995,6 @@ void tinyrl_disable_echo(tinyrl_t * tinyrl, char echo_char)
 }
 
 
-/*-------------------------------------------------------- */
-const char *tinyrl__get_prompt(const tinyrl_t * tinyrl)
-{
-	return tinyrl->prompt;
-}
-
-/*-------------------------------------------------------- */
-void tinyrl__set_prompt(tinyrl_t *tinyrl, const char *prompt)
-{
-	if (tinyrl->prompt) {
-		lub_string_free(tinyrl->prompt);
-		tinyrl->prompt_size = 0;
-		tinyrl->prompt_len = 0;
-	}
-	tinyrl->prompt = lub_string_dup(prompt);
-	if (tinyrl->prompt) {
-		tinyrl->prompt_size = strlen(tinyrl->prompt);
-		tinyrl->prompt_len = utf8_nsyms(tinyrl, tinyrl->prompt,
-			tinyrl->prompt_size);
-	}
-}