summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartyn Russell <martyn@lanedo.com>2014-10-06 13:49:45 +0100
committerMartyn Russell <martyn@lanedo.com>2014-12-10 15:51:35 +0000
commitb193d8e7d8512845351796b7ee30491f50089704 (patch)
tree4f082080f4da01a03d43c5e04f5c751df4ce419a
parent746068aa81babc13df8c1b7b02e45fe73aa37e25 (diff)
downloadtracker-b193d8e7d8512845351796b7ee30491f50089704.tar.gz
tracker: Added 'help' command handler which loads man pages
-rw-r--r--src/tracker/tracker-help.c102
-rw-r--r--src/tracker/tracker-help.h27
2 files changed, 129 insertions, 0 deletions
diff --git a/src/tracker/tracker-help.c b/src/tracker/tracker-help.c
new file mode 100644
index 000000000..e1db902eb
--- /dev/null
+++ b/src/tracker/tracker-help.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2014, Lanedo <martyn@lanedo.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include <unistd.h>
+#include <errno.h>
+
+#include <glib/gi18n.h>
+
+#include "tracker-help.h"
+
+static void
+setup_man_path (void)
+{
+ GString *new_path = g_string_new ("");
+ const char *old_path = g_getenv ("MANPATH");
+
+ /* We should always put ':' after our path. If there is no
+ * old_path, the ':' at the end will let 'man' to try
+ * system-wide paths after ours to find the manual page. If
+ * there is old_path, we need ':' as delimiter. */
+ g_string_append (new_path, MANDIR);
+ g_string_append_c (new_path, ':');
+
+ if (old_path) {
+ g_string_append (new_path, old_path);
+ }
+
+ g_setenv ("MANPATH", new_path->str, TRUE);
+
+ g_string_free (new_path, TRUE);
+}
+
+static int
+exec_man_man (const char *path, const char *page)
+{
+ if (!path) {
+ path = "man";
+ }
+
+ execlp (path, "man", page, (char *) NULL);
+ g_warning(_("failed to exec '%s': %s"), path, strerror (errno));
+
+ return -1;
+}
+
+static int
+exec_man_cmd (const char *cmd, const char *page)
+{
+ gchar *shell_cmd;
+
+ shell_cmd = g_strdup_printf ("%s %s", cmd, page);
+ execl ("/bin/sh", "sh", "-c", shell_cmd, (char *) NULL);
+ g_warning (_("failed to exec '%s': %s"), cmd, strerror (errno));
+ g_free (shell_cmd);
+
+ return -1;
+}
+
+static char *
+cmd_to_page (const char *cmd)
+{
+ if (!cmd) {
+ return g_strdup ("tracker");
+ } else if (g_str_has_prefix (cmd, "tracker")) {
+ return g_strdup (cmd);
+ } else {
+ return g_strdup_printf ("tracker-%s", cmd);
+ }
+}
+
+int
+tracker_help_show_man_page (const char *cmd)
+{
+ const char *page = cmd_to_page (cmd);
+
+ setup_man_path ();
+
+ if (0) {
+ exec_man_cmd ("man", page);
+ }
+
+ return exec_man_man ("man", page);
+}
+
diff --git a/src/tracker/tracker-help.h b/src/tracker/tracker-help.h
new file mode 100644
index 000000000..2afc7ea9f
--- /dev/null
+++ b/src/tracker/tracker-help.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2014, Lanedo <martyn@lanedo.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <glib.h>
+
+#ifndef __TRACKER_HELP_H__
+#define __TRACKER_HELP_H__
+
+int tracker_help_show_man_page (const char *cmd);
+
+#endif /* __TRACKER_HELP_H__ */