Add a wrapper around system() call named runcmd() which allows easier debugging....
authorLaurent Monin <geeqie@norz.org>
Sat, 15 Nov 2008 10:35:43 +0000 (10:35 +0000)
committerLaurent Monin <geeqie@norz.org>
Sat, 15 Nov 2008 10:35:43 +0000 (10:35 +0000)
src/fullscreen.c
src/misc.c
src/misc.h
src/remote.c
src/window.c

index f484826..be82d13 100644 (file)
@@ -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 @@ static void fullscreen_saver_deactivate(void)
 
        if (found)
                {
-               system(XSCREENSAVER_COMMAND);
+               runcmd(XSCREENSAVER_COMMAND);
                }
 }
 
index b7a650f..e132bc1 100644 (file)
@@ -186,4 +186,56 @@ gchar *escquote_value(const gchar *text)
                }
        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: */
index d29e0cf..0df203b 100644 (file)
@@ -19,6 +19,7 @@ gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive);
 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: */
index 8bc4507..19ba426 100644 (file)
@@ -743,7 +743,7 @@ void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path
                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)
index 763dccf..4e14b32 100644 (file)
@@ -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 @@ static gchar *command_result(const gchar *binary, const gchar *command)
        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 @@ static void help_browser_command(const gchar *command, const gchar *path)
 
        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 gchar *html_browsers[] =
 
 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 @@ static void help_browser_run(void)
                return;
                }
 
-       path = g_build_filename(options->documentation.htmldir, "index.html", NULL);
-       help_browser_command(result, path);
        g_free(path);
-
        g_free(result);
 }