Переглянути джерело

Detect unclosed quotes and put '?' symbol instead help generation

Serj Kalichev 1 рік тому
батько
коміт
290260f53f
5 змінених файлів з 31 додано та 10 видалено
  1. 14 4
      bin/klish/interactive.c
  2. 1 1
      plugins/script/script.c
  3. 2 4
      tinyrl/tinyrl.h
  4. 0 1
      tinyrl/tinyrl/private.h
  5. 14 0
      tinyrl/tinyrl/tinyrl.c

+ 14 - 4
bin/klish/interactive.c

@@ -7,6 +7,7 @@
 #include <string.h>
 #include <sys/wait.h>
 #include <errno.h>
+#include <syslog.h>
 
 #include <faux/faux.h>
 #include <faux/str.h>
@@ -458,11 +459,12 @@ static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
 
 static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
 {
-	const char *line = NULL;
+	char *line = NULL;
 	ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
 
-	line = tinyrl_line(tinyrl);
+	line = tinyrl_line_to_pos(tinyrl);
 	ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
+	faux_str_free(line);
 
 	tinyrl_set_busy(tinyrl, BOOL_TRUE);
 
@@ -474,11 +476,19 @@ static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
 
 static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
 {
-	const char *line = NULL;
+	char *line = NULL;
 	ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
 
-	line = tinyrl_line(tinyrl);
+	line = tinyrl_line_to_pos(tinyrl);
+	// If "?" is quoted then it's not special hotkey.
+	// Just insert it into the line.
+	if (faux_str_unclosed_quotes(line, NULL)) {
+		faux_str_free(line);
+		return tinyrl_key_default(tinyrl, key);
+	}
+
 	ktp_session_help(ctx->ktp, line);
+	faux_str_free(line);
 
 	tinyrl_set_busy(tinyrl, BOOL_TRUE);
 

+ 1 - 1
plugins/script/script.c

@@ -72,7 +72,7 @@ static bool_t populate_env_kpargv(const kpargv_t *pargv, const char *prefix)
 	while ((parg = kpargv_pargs_each(&iter))) {
 		char *var = NULL;
 		const char *value = kparg_value(parg);
-		if (!value) // PTYPE contains "__ptype" parg with NULL value
+		if (!value) // PTYPE can contain parg with NULL value
 			continue;
 		entry = kparg_entry(parg);
 		if (kentry_max(entry) > 1) { // Multi

+ 2 - 4
tinyrl/tinyrl.h

@@ -62,7 +62,7 @@ typedef int tinyrl_timeout_fn_t(tinyrl_t *instance);
  */
 typedef bool_t tinyrl_key_func_t(tinyrl_t *instance, unsigned char key);
 
-
+bool_t tinyrl_key_default(tinyrl_t *tinyrl, unsigned char key);
 
 
 tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
@@ -82,6 +82,7 @@ void tinyrl_set_busy(tinyrl_t *tinyrl, bool_t busy);
 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);
+char *tinyrl_line_to_pos(const tinyrl_t *tinyrl);
 bool_t tinyrl_hist_save(const tinyrl_t *tinyrl);
 bool_t tinyrl_hist_restore(tinyrl_t *tinyrl);
 void tinyrl_line_to_hist(tinyrl_t *tinyrl);
@@ -111,9 +112,6 @@ bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len);
 bool_t tinyrl_line_delete(tinyrl_t *tinyrl, size_t start, size_t len);
 bool_t tinyrl_line_replace(tinyrl_t *tinyrl, const char *text);
 
-
-
-
 extern void tinyrl_done(tinyrl_t * instance);
 
 extern void tinyrl_completion_over(tinyrl_t * instance);

+ 0 - 1
tinyrl/tinyrl/private.h

@@ -15,7 +15,6 @@ off_t utf8_move_right(const char *line, off_t cur_pos);
 ssize_t utf8_nsyms(const char *str, size_t len);
 
 // Keys
-bool_t tinyrl_key_default(tinyrl_t *tinyrl, unsigned char key);
 bool_t tinyrl_key_interrupt(tinyrl_t *tinyrl, unsigned char key);
 bool_t tinyrl_key_start_of_line(tinyrl_t *tinyrl, unsigned char key);
 bool_t tinyrl_key_end_of_line(tinyrl_t *tinyrl, unsigned char key);

+ 14 - 0
tinyrl/tinyrl/tinyrl.c

@@ -302,10 +302,24 @@ void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata)
 
 const char *tinyrl_line(const tinyrl_t *tinyrl)
 {
+	assert(tinyrl);
+	if (!tinyrl)
+		return NULL;
 	return tinyrl->line.str;
 }
 
 
+char *tinyrl_line_to_pos(const tinyrl_t *tinyrl)
+{
+	assert(tinyrl);
+	if (!tinyrl)
+		return NULL;
+
+	return faux_str_dupn(tinyrl->line.str, tinyrl->line.pos);
+}
+
+
+
 bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
 {
 	assert(tinyrl);