changeset 2881:9e4e9ef1e68e

Addl fix #619: Man page disagrees with --remote-help https://github.com/BestImageViewer/geeqie/issues/619 The remote commands --file: and --view: will now display images in paths relative to the folder the remote command is executed from
author Colin Clark <colin.clark@cclark.uk>
date Sun, 06 Jan 2019 14:28:19 +0000
parents 288ea5f33bf7
children 0149f1ddb297
files src/main.c src/remote.c
diffstat 2 files changed, 62 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.c	Sat Jan 05 20:08:50 2019 +0000
+++ b/src/main.c	Sun Jan 06 14:28:19 2019 +0000
@@ -222,6 +222,8 @@
 	gboolean remote_do = FALSE;
 	gchar *first_dir = NULL;
 	gchar *app_lock;
+	gchar *pwd;
+	gchar *current_dir;
 
 	command_line = g_new0(CommandLine, 1);
 
@@ -442,7 +444,16 @@
 			printf_term(TRUE, _("\nUse --remote-help for valid remote options.\n"));
 			}
 
+		/* prepend the current dir the remote command was made from,
+		 * for use by any remote command that needs it
+		 */
+		current_dir = g_get_current_dir();
+		pwd = g_strconcat("--PWD:", current_dir, NULL);
+		remote_list = g_list_prepend(remote_list, pwd);
+
 		remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
+		g_free(pwd);
+		g_free(current_dir);
 		}
 	g_list_free(remote_list);
 
--- a/src/remote.c	Sat Jan 05 20:08:50 2019 +0000
+++ b/src/remote.c	Sun Jan 06 14:28:19 2019 +0000
@@ -73,6 +73,38 @@
 	CollectionData *command_collection;
 };
 
+/* Remote commands from main.c are prepended with the current dir the remote
+ * command was made from. Some remote commands require this. The
+ * value is stored here
+ */
+static gchar *pwd = NULL;
+
+/**
+ * @brief Ensures file path is absolute.
+ * @param[in] filename Filepath, absolute or relative to calling directory
+ * @returns absolute path
+ * 
+ * If first character of input filepath is not the directory
+ * separator, assume it as a relative path and prepend
+ * the directory the remote command was initiated from
+ * 
+ * Return value must be freed with g_free()
+ */
+static gchar *set_pwd(gchar *filename)
+{
+	gchar *temp;
+
+	if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
+		{
+		temp = g_build_filename(pwd, filename, NULL);
+		}
+	else
+		{
+		temp = g_strdup(filename);
+		}
+
+	return temp;
+}
 
 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
 {
@@ -614,7 +646,10 @@
 
 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
 {
-	gchar *filename = expand_tilde(text);
+	gchar *filename;
+	gchar *tilde_filename = expand_tilde(text);
+
+	filename = set_pwd(tilde_filename);
 
 	if (isfile(filename))
 		{
@@ -638,6 +673,7 @@
 		}
 
 	g_free(filename);
+	g_free(tilde_filename);
 }
 
 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
@@ -974,10 +1010,14 @@
 
 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
 {
-	gchar *filename = expand_tilde(text);
+	gchar *filename;
+	gchar *tilde_filename = expand_tilde(text);
+
+	filename = set_pwd(tilde_filename);
 
 	view_window_new(file_data_new_group(filename));
 	g_free(filename);
+	g_free(tilde_filename);
 }
 
 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
@@ -1031,6 +1071,14 @@
 		}
 }
 
+static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
+{
+	LayoutWindow *lw = NULL;
+
+	g_free(pwd);
+	pwd = g_strdup(text);
+}
+
 #ifdef HAVE_LUA
 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
 {
@@ -1126,6 +1174,7 @@
 #ifdef HAVE_LUA
 	{ NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
 #endif
+	{ NULL, "--PWD:",               gr_pwd,                 TRUE, FALSE, N_("<PWD>"), N_("for internal use only") },
 	{ NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
 };