changeset 1144:5fe3b8b3a612

Add a wrapper around system() call named runcmd() which allows easier debugging. Improve the code launching the help browser.
author zas_
date Sat, 15 Nov 2008 10:35:43 +0000
parents 893dfe1eca99
children 3a7af6a8cd5f
files src/fullscreen.c src/misc.c src/misc.h src/remote.c src/window.c
diffstat 5 files changed, 85 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/fullscreen.c	Fri Nov 14 01:10:18 2008 +0000
+++ b/src/fullscreen.c	Sat Nov 15 10:35:43 2008 +0000
@@ -15,6 +15,7 @@
 #include "fullscreen.h"
 
 #include "image.h"
+#include "misc.h"
 #include "ui_fileops.h"
 #include "ui_menu.h"
 #include "ui_misc.h"
@@ -190,7 +191,7 @@
 
 	if (found)
 		{
-		system(XSCREENSAVER_COMMAND);
+		runcmd(XSCREENSAVER_COMMAND);
 		}
 }
 
--- a/src/misc.c	Fri Nov 14 01:10:18 2008 +0000
+++ b/src/misc.c	Sat Nov 15 10:35:43 2008 +0000
@@ -186,4 +186,56 @@
 		}
 	return g_strdup("\"\"");
 }
+
+/* Run a command like system() but may output debug messages. */
+int runcmd(gchar *cmd)
+{
+#if 1
+	return system(cmd);
+	return 0;
+#else
+	/* For debugging purposes */
+	int retval = -1;
+	FILE *in;
+
+	DEBUG_1("Running command: %s", cmd);
+
+	in = popen(cmd, "r");
+	if (in)
+		{
+		int status;
+		const gchar *msg;
+		gchar buf[2048];
+
+		while (fgets(buf, sizeof(buf), in) != NULL )
+			{
+			DEBUG_1("Output: %s", buf);
+			}
+
+		status = pclose(in);
+
+		if (WIFEXITED(status))
+			{
+			msg = "Command terminated with exit code";
+			retval = WEXITSTATUS(status);
+			}
+		else if (WIFSIGNALED(status))
+			{
+			msg = "Command was killed by signal";
+			retval = WTERMSIG(status);
+			}
+		else
+			{
+			msg = "pclose() returned";
+			retval = status;
+			}
+
+		DEBUG_1("%s : %d\n", msg, retval);
+	}
+	
+	return retval;
+#endif
+}
+
+
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
--- a/src/misc.h	Fri Nov 14 01:10:18 2008 +0000
+++ b/src/misc.h	Sat Nov 15 10:35:43 2008 +0000
@@ -19,6 +19,7 @@
 gchar *expand_tilde(const gchar *filename);
 gchar *quoted_value(const gchar *text, const gchar **tail);
 gchar *escquote_value(const gchar *text);
+int runcmd(gchar *cmd);
 
 #endif /* MISC_H */
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
--- a/src/remote.c	Fri Nov 14 01:10:18 2008 +0000
+++ b/src/remote.c	Sat Nov 15 10:35:43 2008 +0000
@@ -743,7 +743,7 @@
 		if (get_debug_level()) g_string_append(command, " --debug");
 
 		g_string_append(command, " &");
-		system(command->str);
+		runcmd(command->str);
 		g_string_free(command, TRUE);
 
 		while (!rc && retry_count > 0)
--- a/src/window.c	Fri Nov 14 01:10:18 2008 +0000
+++ b/src/window.c	Sat Nov 15 10:35:43 2008 +0000
@@ -12,6 +12,7 @@
 #include "main.h"
 #include "window.h"
 
+#include "misc.h"
 #include "pixbuf_util.h"
 #include "ui_fileops.h"
 #include "ui_help.h"
@@ -113,14 +114,15 @@
 	return result;
 }
 
-static void help_browser_command(const gchar *command, const gchar *path)
+static int help_browser_command(const gchar *command, const gchar *path)
 {
 	gchar *result;
 	gchar *buf;
 	gchar *begin;
 	gchar *end;
+	int retval = -1;
 
-	if (!command || !path) return;
+	if (!command || !path) return retval;
 
 	DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
 
@@ -142,9 +144,11 @@
 
 	DEBUG_1("Help command post [%s]", result);
 
-	system(result);
+	retval = runcmd(result);
+	DEBUG_1("Help command exit code: %d", retval);
 
 	g_free(result);
+	return retval;
 }
 
 /*
@@ -177,17 +181,31 @@
 
 static void help_browser_run(void)
 {
-	gchar *path;
-	gchar *result;
+	gchar *name = options->helpers.html_browser.command_name;
+	gchar *cmd = options->helpers.html_browser.command_line;
+	gchar *path = g_build_filename(options->documentation.htmldir, "index.html", NULL);
+	gchar *result = NULL;
 	gint i;
 
-	result = command_result(options->helpers.html_browser.command_name, options->helpers.html_browser.command_line);
-
-	i = 0;
-	while (!result && html_browsers[i])
+	i = 0;	
+	while (!result)
 		{
-		result = command_result(html_browsers[i], html_browsers[i+1]);
-		i += 2;
+		if ((name && *name) || (cmd && *cmd)) {
+			DEBUG_1("Trying browser: name=%s command=%s", name, cmd);
+			result = command_result(name, cmd);
+			DEBUG_1("Result: %s", result);
+			if (result)
+				{
+				int ret = help_browser_command(result, path);
+				
+				if (ret == 0) break;
+				g_free(result);
+				result = NULL;
+			}
+		}
+		if (!html_browsers[i]) break;
+		name = html_browsers[i++];
+		cmd = html_browsers[i++];
 		}
 
 	if (!result)
@@ -196,10 +214,7 @@
 		return;
 		}
 
-	path = g_build_filename(options->documentation.htmldir, "index.html", NULL);
-	help_browser_command(result, path);
 	g_free(path);
-
 	g_free(result);
 }