Procházet zdrojové kódy

Speed up builtin ptype syms.

Builtin ptype syms including completion and help now "silent" i.e.
don't use stout for output and don't fork grabber. It speeds up
output generation for command completions and helping.
Serj Kalichev před 4 měsíci
rodič
revize
9f53525483

+ 4 - 1
klish/kcontext.h

@@ -106,8 +106,11 @@ bool_t kcontext_is_last_pipeline_stage(const kcontext_t *context);
 FAUX_HIDDEN bool_t kcontext_set_is_last_pipeline_stage(kcontext_t *context, bool_t is_last_pipeline_stage);
 
 // Output
+ssize_t kcontext_fwrite(const kcontext_t *context, FILE *stream,
+	const char *line, size_t len);
+int kcontext_fprintf(const kcontext_t *context, FILE *stream,
+	const char *fmt, ...);
 int kcontext_printf(const kcontext_t *context, const char *fmt, ...);
-int kcontext_printf_err(const kcontext_t *context, const char *fmt, ...);
 
 // Wrappers
 kparg_t *kcontext_candidate_parg(const kcontext_t *context);

+ 37 - 28
klish/ksession/kcontext.c

@@ -337,17 +337,20 @@ kplugin_t *kcontext_plugin(const kcontext_t *context)
 }
 
 
-static int kcontext_vprintf(const kcontext_t *context,
-	bool_t is_stderr, const char *fmt, va_list ap)
+ssize_t kcontext_fwrite(const kcontext_t *context, FILE *stream,
+	const char *line, size_t len)
 {
 	const kaction_t *action = NULL;
 	const ksym_t *sym = NULL;
 	faux_buf_t *buf = NULL;
 	int rc = -1;
-	FILE *f = NULL;
 
+	if (len < 1)
+		return 0;
 	if (!context)
 		return -1;
+	if (stream != stdout && stream != stderr)
+		return -1;
 	action = kcontext_action(context);
 	if (!action)
 		return -1;
@@ -355,54 +358,60 @@ static int kcontext_vprintf(const kcontext_t *context,
 	if (!sym)
 		return -1;
 
-	if (is_stderr) {
-		buf = kcontext_buferr(context);
-		f = stderr;
-	} else {
-		buf = kcontext_bufout(context);
-		f = stdout;
-	}
-
 	// "Silent" output
-	if (ksym_sync(sym) &&
-		ksym_silent(sym) &&
-		kcontext_is_last_pipeline_stage(context) &&
-		buf) {
-		char *line = NULL;
-		line = faux_str_vsprintf(fmt, ap);
-		rc = strlen(line);
-		if (rc > 0)
-			faux_buf_write(buf, line, rc);
-		faux_str_free(line);
-	} else {
-		rc = vfprintf(f, fmt, ap);
-		fflush(f);
+	if (ksym_sync(sym) && ksym_silent(sym) &&
+		kcontext_is_last_pipeline_stage(context))
+	{
+		buf = (stream == stderr) ? kcontext_buferr(context) :
+			kcontext_bufout(context);
+		if (!buf)
+			return -1;
+		faux_buf_write(buf, line, len);
+		return len;
 	}
 
+	rc = fwrite(line, 1, len, stream);
+	fflush(stream);
+
 	return rc;
 }
 
 
-int kcontext_printf(const kcontext_t *context, const char *fmt, ...)
+static int kcontext_vprintf(const kcontext_t *context,
+	FILE *stream, const char *fmt, va_list ap)
+{
+	int rc = -1;
+	char *line = NULL;
+
+	line = faux_str_vsprintf(fmt, ap);
+	rc = kcontext_fwrite(context, stream, line, strlen(line));
+	faux_str_free(line);
+
+	return rc;
+}
+
+
+int kcontext_fprintf(const kcontext_t *context, FILE *stream,
+	const char *fmt, ...)
 {
 	int rc = -1;
 	va_list ap;
 
 	va_start(ap, fmt);
-	rc = kcontext_vprintf(context, BOOL_FALSE, fmt, ap);
+	rc = kcontext_vprintf(context, stream, fmt, ap);
 	va_end(ap);
 
 	return rc;
 }
 
 
-int kcontext_printf_err(const kcontext_t *context, const char *fmt, ...)
+int kcontext_printf(const kcontext_t *context, const char *fmt, ...)
 {
 	int rc = -1;
 	va_list ap;
 
 	va_start(ap, fmt);
-	rc = kcontext_vprintf(context, BOOL_TRUE, fmt, ap);
+	rc = kcontext_vprintf(context, stdout, fmt, ap);
 	va_end(ap);
 
 	return rc;

+ 6 - 25
plugins/klish/misc.c

@@ -35,12 +35,12 @@ int klish_tsym(kcontext_t *context)
 
 	script = kcontext_script(context);
 	if (faux_str_is_empty(script)) {
-		printf("[<empty>]\n");
-		fprintf(stderr, "Empty item\n");
+		kcontext_printf(context, "[<empty>]\n");
+		kcontext_fprintf(context, stderr, "Empty item\n");
 		return -1;
 	}
 
-	printf("[%s]\n", script);
+	kcontext_printf(context, "[%s]\n", script);
 
 	return 0;
 }
@@ -55,7 +55,7 @@ int klish_printl(kcontext_t *context)
 	if (faux_str_is_empty(script))
 		script = "";
 
-	printf("%s\n", script);
+	kcontext_printf(context, "%s\n", script);
 
 	return 0;
 }
@@ -70,26 +70,7 @@ int klish_print(kcontext_t *context)
 	if (faux_str_is_empty(script))
 		script = "";
 
-	printf("%s", script);
-	fflush(stdout);
-
-	return 0;
-}
-
-
-// Symbol to show current path
-int klish_pwd(kcontext_t *context)
-{
-	kpath_t *path = NULL;
-	kpath_levels_node_t *iter = NULL;
-	klevel_t *level = NULL;
-
-	path = ksession_path(kcontext_session(context));
-	iter = kpath_iter(path);
-	while ((level = kpath_each(&iter))) {
-		printf("/%s", kentry_name(klevel_entry(level)));
-	}
-	printf("\n");
+	kcontext_printf(context, "%s", script);
 
 	return 0;
 }
@@ -176,7 +157,7 @@ int klish_prompt(kcontext_t *context)
 	if (pos > start)
 		faux_str_catn(&prompt, start, pos - start);
 
-	kcontext_printf(context, "%s", prompt);
+	kcontext_fwrite(context, stdout, prompt, strlen(prompt));
 	faux_str_free(prompt);
 
 	return 0;

+ 19 - 0
plugins/klish/nav.c

@@ -167,3 +167,22 @@ int klish_nav(kcontext_t *context)
 
 	return 0;
 }
+
+
+// Symbol to show current path
+int klish_pwd(kcontext_t *context)
+{
+	kpath_t *path = NULL;
+	kpath_levels_node_t *iter = NULL;
+	klevel_t *level = NULL;
+
+	path = ksession_path(kcontext_session(context));
+	iter = kpath_iter(path);
+	while ((level = kpath_each(&iter))) {
+		kcontext_printf(context, "/%s",
+			kentry_name(klevel_entry(level)));
+	}
+	kcontext_printf(context, "\n");
+
+	return 0;
+}

+ 14 - 9
plugins/klish/plugin_init.c

@@ -28,11 +28,12 @@ int kplugin_klish_init(kcontext_t *context)
 	// Misc
 	kplugin_add_syms(plugin, ksym_new_ext("nop", klish_nop,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
-	kplugin_add_syms(plugin, ksym_new("tsym", klish_tsym));
-	kplugin_add_syms(plugin, ksym_new("print", klish_print));
-	kplugin_add_syms(plugin, ksym_new("printl", klish_printl));
-	kplugin_add_syms(plugin, ksym_new_ext("pwd", klish_pwd,
-		KSYM_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
+	kplugin_add_syms(plugin, ksym_new_ext("tsym", klish_tsym,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
+	kplugin_add_syms(plugin, ksym_new_ext("print", klish_print,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
+	kplugin_add_syms(plugin, ksym_new_ext("printl", klish_printl,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
 	kplugin_add_syms(plugin, ksym_new_ext("prompt", klish_prompt,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
 
@@ -45,16 +46,20 @@ int kplugin_klish_init(kcontext_t *context)
 	// actions will be fork()-ed so it can't change current path.
 	kplugin_add_syms(plugin, ksym_new_ext("nav", klish_nav,
 		KSYM_PERMANENT, KSYM_SYNC, KSYM_SILENT));
+	kplugin_add_syms(plugin, ksym_new_ext("pwd", klish_pwd,
+		KSYM_PERMANENT, KSYM_SYNC, KSYM_SILENT));
 
 	// PTYPEs
 	// These PTYPEs are simple and fast so set SYNC flag
 	kplugin_add_syms(plugin, ksym_new_ext("COMMAND", klish_ptype_COMMAND,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
-	kplugin_add_syms(plugin, ksym_new_ext("completion_COMMAND", klish_completion_COMMAND,
-		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
+	kplugin_add_syms(plugin, ksym_new_ext("completion_COMMAND",
+		klish_completion_COMMAND,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
 	kplugin_add_syms(plugin, ksym_new_ext("help_COMMAND", klish_help_COMMAND,
-		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_NONSILENT));
-	kplugin_add_syms(plugin, ksym_new_ext("COMMAND_CASE", klish_ptype_COMMAND_CASE,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
+	kplugin_add_syms(plugin, ksym_new_ext("COMMAND_CASE",
+		klish_ptype_COMMAND_CASE,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));
 	kplugin_add_syms(plugin, ksym_new_ext("INT", klish_ptype_INT,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC, KSYM_SILENT));

+ 1 - 1
plugins/klish/private.h

@@ -16,7 +16,6 @@ int klish_nop(kcontext_t *context);
 int klish_tsym(kcontext_t *context);
 int klish_print(kcontext_t *context);
 int klish_printl(kcontext_t *context);
-int klish_pwd(kcontext_t *context);
 int klish_prompt(kcontext_t *context);
 
 // Log
@@ -24,6 +23,7 @@ int klish_syslog(kcontext_t *context);
 
 // Navigation
 int klish_nav(kcontext_t *context);
+int klish_pwd(kcontext_t *context);
 
 // PTYPEs
 int klish_ptype_COMMAND(kcontext_t *context);

+ 2 - 2
plugins/klish/ptypes.c

@@ -61,7 +61,7 @@ int klish_completion_COMMAND(kcontext_t *context)
 	if (!command_name)
 		return 0;
 
-	printf("%s\n", command_name);
+	kcontext_printf(context, "%s\n", command_name);
 
 	return 0;
 }
@@ -97,7 +97,7 @@ int klish_help_COMMAND(kcontext_t *context)
 		help_text = kentry_name(entry);
 	assert(help_text);
 
-	printf("%s\n%s\n", command_name, help_text);
+	kcontext_printf(context, "%s\n%s\n", command_name, help_text);
 
 	return 0;
 }