Skip to content

Commit

Permalink
Add a wrapper around system() call named runcmd() which allows easier…
Browse files Browse the repository at this point in the history
… debugging. Improve the code launching the help browser.
  • Loading branch information
Laurent Monin committed Nov 15, 2008
1 parent f56852b commit e5dd414
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/fullscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -190,7 +191,7 @@ static void fullscreen_saver_deactivate(void)

if (found)
{
system(XSCREENSAVER_COMMAND);
runcmd(XSCREENSAVER_COMMAND);
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: */
1 change: 1 addition & 0 deletions src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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: */
2 changes: 1 addition & 1 deletion src/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 29 additions & 14 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

/*
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down

0 comments on commit e5dd414

Please sign in to comment.