Bug Fix #426: geeqie -r file:FILE crashes if FILE does not exist
[geeqie.git] / src / remote.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "remote.h"
24
25 #include "collect.h"
26 #include "filedata.h"
27 #include "img-view.h"
28 #include "layout.h"
29 #include "layout_image.h"
30 #include "misc.h"
31 #include "slideshow.h"
32 #include "ui_fileops.h"
33 #include "rcfile.h"
34
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <signal.h>
38 #include <errno.h>
39
40
41 #define SERVER_MAX_CLIENTS 8
42
43 #define REMOTE_SERVER_BACKLOG 4
44
45
46 #ifndef UNIX_PATH_MAX
47 #define UNIX_PATH_MAX 108
48 #endif
49
50
51 static RemoteConnection *remote_client_open(const gchar *path);
52 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
53
54
55 typedef struct _RemoteClient RemoteClient;
56 struct _RemoteClient {
57         gint fd;
58         guint channel_id; /* event source id */
59         RemoteConnection *rc;
60 };
61
62 typedef struct _RemoteData RemoteData;
63 struct _RemoteData {
64         CollectionData *command_collection;
65 };
66
67
68 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
69 {
70         RemoteClient *client = data;
71         RemoteConnection *rc;
72         GIOStatus status = G_IO_STATUS_NORMAL;
73
74         rc = client->rc;
75
76         if (condition & G_IO_IN)
77                 {
78                 gchar *buffer = NULL;
79                 GError *error = NULL;
80                 gsize termpos;
81
82                 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
83                         {
84                         if (buffer)
85                                 {
86                                 buffer[termpos] = '\0';
87
88                                 if (strlen(buffer) > 0)
89                                         {
90                                         if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
91                                         g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
92                                         g_io_channel_flush(source, NULL);
93                                         }
94                                 g_free(buffer);
95
96                                 buffer = NULL;
97                                 }
98                         }
99
100                 if (error)
101                         {
102                         log_printf("error reading socket: %s\n", error->message);
103                         g_error_free(error);
104                         }
105                 }
106
107         if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
108                 {
109                 rc->clients = g_list_remove(rc->clients, client);
110
111                 DEBUG_1("HUP detected, closing client.");
112                 DEBUG_1("client count %d", g_list_length(rc->clients));
113
114                 g_source_remove(client->channel_id);
115                 close(client->fd);
116                 g_free(client);
117                 }
118
119         return TRUE;
120 }
121
122 static void remote_server_client_add(RemoteConnection *rc, gint fd)
123 {
124         RemoteClient *client;
125         GIOChannel *channel;
126
127         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
128                 {
129                 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
130                 close(fd);
131                 return;
132                 }
133
134         client = g_new0(RemoteClient, 1);
135         client->rc = rc;
136         client->fd = fd;
137
138         channel = g_io_channel_unix_new(fd);
139         client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
140                                                  remote_server_client_cb, client, NULL);
141         g_io_channel_unref(channel);
142
143         rc->clients = g_list_append(rc->clients, client);
144         DEBUG_1("client count %d", g_list_length(rc->clients));
145 }
146
147 static void remote_server_clients_close(RemoteConnection *rc)
148 {
149         while (rc->clients)
150                 {
151                 RemoteClient *client = rc->clients->data;
152
153                 rc->clients = g_list_remove(rc->clients, client);
154
155                 g_source_remove(client->channel_id);
156                 close(client->fd);
157                 g_free(client);
158                 }
159 }
160
161 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
162 {
163         RemoteConnection *rc = data;
164         gint fd;
165         guint alen;
166
167         fd = accept(rc->fd, NULL, &alen);
168         if (fd == -1)
169                 {
170                 log_printf("error accepting socket: %s\n", strerror(errno));
171                 return TRUE;
172                 }
173
174         remote_server_client_add(rc, fd);
175
176         return TRUE;
177 }
178
179 static gboolean remote_server_exists(const gchar *path)
180 {
181         RemoteConnection *rc;
182
183         /* verify server up */
184         rc = remote_client_open(path);
185         remote_close(rc);
186
187         if (rc) return TRUE;
188
189         /* unable to connect, remove socket file to free up address */
190         unlink(path);
191         return FALSE;
192 }
193
194 static RemoteConnection *remote_server_open(const gchar *path)
195 {
196         RemoteConnection *rc;
197         struct sockaddr_un addr;
198         gint sun_path_len;
199         gint fd;
200         GIOChannel *channel;
201
202         if (remote_server_exists(path))
203                 {
204                 log_printf("Address already in use: %s\n", path);
205                 return NULL;
206                 }
207
208         fd = socket(PF_UNIX, SOCK_STREAM, 0);
209         if (fd == -1) return NULL;
210
211         addr.sun_family = AF_UNIX;
212         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
213         strncpy(addr.sun_path, path, sun_path_len);
214         if (bind(fd, &addr, sizeof(addr)) == -1 ||
215             listen(fd, REMOTE_SERVER_BACKLOG) == -1)
216                 {
217                 log_printf("error subscribing to socket: %s\n", strerror(errno));
218                 close(fd);
219                 return NULL;
220                 }
221
222         rc = g_new0(RemoteConnection, 1);
223
224         rc->server = TRUE;
225         rc->fd = fd;
226         rc->path = g_strdup(path);
227
228         channel = g_io_channel_unix_new(rc->fd);
229         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
230
231         rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
232                                              remote_server_read_cb, rc, NULL);
233         g_io_channel_unref(channel);
234
235         return rc;
236 }
237
238 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
239 {
240         if (!rc || !rc->server) return;
241
242         rc->read_func = func;
243         rc->read_data = data;
244 }
245
246
247 static RemoteConnection *remote_client_open(const gchar *path)
248 {
249         RemoteConnection *rc;
250         struct stat st;
251         struct sockaddr_un addr;
252         gint sun_path_len;
253         gint fd;
254
255         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
256
257         fd = socket(PF_UNIX, SOCK_STREAM, 0);
258         if (fd == -1) return NULL;
259
260         addr.sun_family = AF_UNIX;
261         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
262         strncpy(addr.sun_path, path, sun_path_len);
263         if (connect(fd, &addr, sizeof(addr)) == -1)
264                 {
265                 DEBUG_1("error connecting to socket: %s", strerror(errno));
266                 close(fd);
267                 return NULL;
268                 }
269
270         rc = g_new0(RemoteConnection, 1);
271         rc->server = FALSE;
272         rc->fd = fd;
273         rc->path = g_strdup(path);
274
275         return rc;
276 }
277
278 static sig_atomic_t sigpipe_occured = FALSE;
279
280 static void sighandler_sigpipe(gint sig)
281 {
282         sigpipe_occured = TRUE;
283 }
284
285 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
286 {
287         struct sigaction new_action, old_action;
288         gboolean ret = FALSE;
289         GError *error = NULL;
290         GIOChannel *channel;
291
292         if (!rc || rc->server) return FALSE;
293         if (!text) return TRUE;
294
295         sigpipe_occured = FALSE;
296
297         new_action.sa_handler = sighandler_sigpipe;
298         sigemptyset(&new_action.sa_mask);
299         new_action.sa_flags = 0;
300
301         /* setup our signal handler */
302         sigaction(SIGPIPE, &new_action, &old_action);
303
304         channel = g_io_channel_unix_new(rc->fd);
305
306         g_io_channel_write_chars(channel, text, -1, NULL, &error);
307         g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
308         g_io_channel_flush(channel, &error);
309
310         if (error)
311                 {
312                 log_printf("error reading socket: %s\n", error->message);
313                 g_error_free(error);
314                 ret = FALSE;;
315                 }
316         else
317                 {
318                 ret = TRUE;
319                 }
320
321         if (ret)
322                 {
323                 gchar *buffer = NULL;
324                 gsize termpos;
325                 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
326                         {
327                         if (buffer)
328                                 {
329                                 if (buffer[0] == '\n') /* empty line finishes the command */
330                                         {
331                                         g_free(buffer);
332                                         fflush(stdout);
333                                         break;
334                                         }
335                                 buffer[termpos] = '\0';
336                                 printf("%s\n", buffer);
337                                 g_free(buffer);
338                                 buffer = NULL;
339                                 }
340                         }
341
342                 if (error)
343                         {
344                         log_printf("error reading socket: %s\n", error->message);
345                         g_error_free(error);
346                         ret = FALSE;
347                         }
348                 }
349
350
351         /* restore the original signal handler */
352         sigaction(SIGPIPE, &old_action, NULL);
353         g_io_channel_unref(channel);
354         return ret;
355 }
356
357 void remote_close(RemoteConnection *rc)
358 {
359         if (!rc) return;
360
361         if (rc->server)
362                 {
363                 remote_server_clients_close(rc);
364
365                 g_source_remove(rc->channel_id);
366                 unlink(rc->path);
367                 }
368
369         if (rc->read_data)
370                 g_free(rc->read_data);
371
372         close(rc->fd);
373
374         g_free(rc->path);
375         g_free(rc);
376 }
377
378 /*
379  *-----------------------------------------------------------------------------
380  * remote functions
381  *-----------------------------------------------------------------------------
382  */
383
384 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
385 {
386         layout_image_next(NULL);
387 }
388
389 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
390 {
391         layout_image_prev(NULL);
392 }
393
394 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
395 {
396         layout_image_first(NULL);
397 }
398
399 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
400 {
401         layout_image_last(NULL);
402 }
403
404 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
405 {
406         layout_image_full_screen_toggle(NULL);
407 }
408
409 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
410 {
411         layout_image_full_screen_start(NULL);
412 }
413
414 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
415 {
416         layout_image_full_screen_stop(NULL);
417 }
418
419 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
420 {
421         GList *list;
422         FileData *dir_fd = file_data_new_dir(text);
423         list = filelist_recursive(dir_fd);
424         file_data_unref(dir_fd);
425         if (!list) return;
426 //printf("length: %d\n", g_list_length(list));
427         layout_image_slideshow_stop(NULL);
428         layout_image_slideshow_start_from_list(NULL, list);
429 }
430
431 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
432 {
433         layout_image_slideshow_toggle(NULL);
434 }
435
436 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
437 {
438         layout_image_slideshow_start(NULL);
439 }
440
441 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
442 {
443         layout_image_slideshow_stop(NULL);
444 }
445
446 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
447 {
448         gdouble n;
449
450         n = g_ascii_strtod(text, NULL);
451         if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
452                 {
453                 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
454                             SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
455                 return;
456                 }
457         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
458 }
459
460 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
461 {
462         gboolean popped;
463         gboolean hidden;
464
465         if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
466                 {
467                 layout_tools_float_set(NULL, popped, FALSE);
468                 }
469 }
470
471 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
472 {
473         gboolean popped;
474         gboolean hidden;
475
476         if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
477                 {
478                 layout_tools_float_set(NULL, popped, TRUE);
479                 }
480 }
481
482 static gboolean gr_quit_idle_cb(gpointer data)
483 {
484         exit_program();
485
486         return FALSE;
487 }
488
489 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
490 {
491         /* schedule exit when idle, if done from within a
492          * remote handler remote_close will crash
493          */
494         g_idle_add(gr_quit_idle_cb, NULL);
495 }
496
497 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
498 {
499         gchar *filename = expand_tilde(text);
500
501         if (isfile(filename))
502                 {
503                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
504                         {
505                         collection_window_new(filename);
506                         }
507                 else
508                         {
509                         layout_set_path(NULL, filename);
510                         }
511                 }
512         else if (isdir(filename))
513                 {
514                 layout_set_path(NULL, filename);
515                 }
516         else
517                 {
518                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
519                 layout_set_path(NULL, homedir());
520                 }
521
522         g_free(filename);
523 }
524
525 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
526 {
527         gchar *filename = expand_tilde(text);
528
529         if (isfile(filename))
530                 {
531                 load_config_from_file(filename, FALSE);
532                 }
533         else
534                 {
535                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
536                 layout_set_path(NULL, homedir());
537                 }
538
539         g_free(filename);
540 }
541
542 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
543 {
544         gchar *filename = expand_tilde(text);
545         FileData *fd = file_data_new_group(filename);
546
547         GList *work;
548         if (fd->parent) fd = fd->parent;
549
550         g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
551         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
552
553         work = fd->sidecar_files;
554
555         while (work)
556                 {
557                 fd = work->data;
558                 work = work->next;
559                 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
560                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
561                 }
562         g_free(filename);
563 }
564
565 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
566 {
567         gchar *filename = expand_tilde(text);
568         FileData *fd = file_data_new_group(filename);
569
570         if (fd->change && fd->change->dest)
571                 {
572                 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
573                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
574                 }
575         g_free(filename);
576 }
577
578 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
579 {
580         gchar *filename = expand_tilde(text);
581
582         view_window_new(file_data_new_group(filename));
583         g_free(filename);
584 }
585
586 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
587 {
588         RemoteData *remote_data = data;
589
590         if (remote_data->command_collection)
591                 {
592                 collection_unref(remote_data->command_collection);
593                 remote_data->command_collection = NULL;
594                 }
595 }
596
597 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
598 {
599         RemoteData *remote_data = data;
600         gboolean new = TRUE;
601
602         if (!remote_data->command_collection)
603                 {
604                 CollectionData *cd;
605
606                 cd = collection_new("");
607
608                 g_free(cd->path);
609                 cd->path = NULL;
610                 g_free(cd->name);
611                 cd->name = g_strdup(_("Command line"));
612
613                 remote_data->command_collection = cd;
614                 }
615         else
616                 {
617                 new = (!collection_get_first(remote_data->command_collection));
618                 }
619
620         if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
621                 {
622                 layout_image_set_collection(NULL, remote_data->command_collection,
623                                             collection_get_first(remote_data->command_collection));
624                 }
625 }
626
627 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
628 {
629         LayoutWindow *lw = NULL;
630
631         if (layout_valid(&lw))
632                 {
633                 gtk_window_present(GTK_WINDOW(lw->window));
634                 }
635 }
636
637 typedef struct _RemoteCommandEntry RemoteCommandEntry;
638 struct _RemoteCommandEntry {
639         gchar *opt_s;
640         gchar *opt_l;
641         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
642         gboolean needs_extra;
643         gboolean prefer_command_line;
644         gchar *parameter;
645         gchar *description;
646 };
647
648 static RemoteCommandEntry remote_commands[] = {
649         /* short, long                  callback,               extra, prefer, parameter, description */
650         { "-n", "--next",               gr_image_next,          FALSE, FALSE, NULL, N_("next image") },
651         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, NULL, N_("previous image") },
652         { NULL, "--first",              gr_image_first,         FALSE, FALSE, NULL, N_("first image") },
653         { NULL, "--last",               gr_image_last,          FALSE, FALSE, NULL, N_("last image") },
654         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  NULL, N_("toggle full screen") },
655         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, NULL, N_("start full screen") },
656         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, NULL, N_("stop full screen") },
657         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  NULL, N_("toggle slide show") },
658         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, NULL, N_("start slide show") },
659         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, NULL, N_("stop slide show") },
660         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
661         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
662         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
663         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
664         { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
665         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
666         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
667         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
668         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE") },
669         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
670         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL, N_("clear command line collection list") },
671         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
672         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
673         { NULL, NULL, NULL, FALSE, FALSE, NULL }
674 };
675
676 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
677 {
678         gboolean match = FALSE;
679         gint i;
680
681         i = 0;
682         while (!match && remote_commands[i].func != NULL)
683                 {
684                 if (remote_commands[i].needs_extra)
685                         {
686                         if (remote_commands[i].opt_s &&
687                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
688                                 {
689                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
690                                 return &remote_commands[i];
691                                 }
692                         else if (remote_commands[i].opt_l &&
693                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
694                                 {
695                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
696                                 return &remote_commands[i];
697                                 }
698                         }
699                 else
700                         {
701                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
702                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
703                                 {
704                                 if (offset) *offset = text;
705                                 return &remote_commands[i];
706                                 }
707                         }
708
709                 i++;
710                 }
711
712         return NULL;
713 }
714
715 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
716 {
717         RemoteCommandEntry *entry;
718         const gchar *offset;
719
720         entry = remote_command_find(text, &offset);
721         if (entry && entry->func)
722                 {
723                 entry->func(offset, channel, data);
724                 }
725         else
726                 {
727                 log_printf("unknown remote command:%s\n", text);
728                 }
729 }
730
731 void remote_help(void)
732 {
733         gint i;
734         gchar *s_opt_param;
735         gchar *l_opt_param;
736
737         print_term(_("Remote command list:\n"));
738
739         i = 0;
740         while (remote_commands[i].func != NULL)
741                 {
742                 if (remote_commands[i].description)
743                         {
744                         s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
745                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
746                         printf_term("  %-11s%-1s %-30s%-s\n",
747                                     (remote_commands[i].opt_s) ? s_opt_param : "",
748                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
749                                     (remote_commands[i].opt_l) ? l_opt_param : "",
750                                     _(remote_commands[i].description));
751                         g_free(s_opt_param);
752                         g_free(l_opt_param);
753                         }
754                 i++;
755                 }
756         printf_term(N_("\n  All other command line parameters are used as plain files if they exists.\n"));
757 }
758
759 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
760 {
761         gint i;
762
763         i = 1;
764         while (i < argc)
765                 {
766                 RemoteCommandEntry *entry;
767
768                 entry = remote_command_find(argv[i], NULL);
769                 if (entry)
770                         {
771                         list = g_list_append(list, argv[i]);
772                         }
773                 else if (errors && !isfile(argv[i]))
774                         {
775                         *errors = g_list_append(*errors, argv[i]);
776                         }
777                 i++;
778                 }
779
780         return list;
781 }
782
783 /**
784  * \param arg_exec Binary (argv0)
785  * \param remote_list Evaluated and recognized remote commands
786  * \param path The current path
787  * \param cmd_list List of all non collections in Path
788  * \param collection_list List of all collections in argv
789  */
790 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
791                     GList *cmd_list, GList *collection_list)
792 {
793         RemoteConnection *rc;
794         gboolean started = FALSE;
795         gchar *buf;
796
797         buf = g_build_filename(get_rc_dir(), ".command", NULL);
798         rc = remote_client_open(buf);
799         if (!rc)
800                 {
801                 GString *command;
802                 GList *work;
803                 gint retry_count = 12;
804                 gboolean blank = FALSE;
805
806                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
807
808                 command = g_string_new(arg_exec);
809
810                 work = remote_list;
811                 while (work)
812                         {
813                         gchar *text;
814                         RemoteCommandEntry *entry;
815
816                         text = work->data;
817                         work = work->next;
818
819                         entry = remote_command_find(text, NULL);
820                         if (entry)
821                                 {
822                                 if (entry->prefer_command_line)
823                                         {
824                                         remote_list = g_list_remove(remote_list, text);
825                                         g_string_append(command, " ");
826                                         g_string_append(command, text);
827                                         }
828                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
829                                         {
830                                         blank = TRUE;
831                                         }
832                                 }
833                         }
834
835                 if (blank || cmd_list || path) g_string_append(command, " --blank");
836                 if (get_debug_level()) g_string_append(command, " --debug");
837
838                 g_string_append(command, " &");
839                 runcmd(command->str);
840                 g_string_free(command, TRUE);
841
842                 while (!rc && retry_count > 0)
843                         {
844                         usleep((retry_count > 10) ? 500000 : 1000000);
845                         rc = remote_client_open(buf);
846                         if (!rc) print_term(".");
847                         retry_count--;
848                         }
849
850                 print_term("\n");
851
852                 started = TRUE;
853                 }
854         g_free(buf);
855
856         if (rc)
857                 {
858                 GList *work;
859                 const gchar *prefix;
860                 gboolean use_path = TRUE;
861                 gboolean sent = FALSE;
862
863                 work = remote_list;
864                 while (work)
865                         {
866                         gchar *text;
867                         RemoteCommandEntry *entry;
868
869                         text = work->data;
870                         work = work->next;
871
872                         entry = remote_command_find(text, NULL);
873                         if (entry &&
874                             entry->opt_l &&
875                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
876
877                         remote_client_send(rc, text);
878
879                         sent = TRUE;
880                         }
881
882                 if (cmd_list && cmd_list->next)
883                         {
884                         prefix = "--list-add:";
885                         remote_client_send(rc, "--list-clear");
886                         }
887                 else
888                         {
889                         prefix = "file:";
890                         }
891
892                 work = cmd_list;
893                 while (work)
894                         {
895                         FileData *fd;
896                         gchar *text;
897
898                         fd = work->data;
899                         work = work->next;
900
901                         text = g_strconcat(prefix, fd->path, NULL);
902                         remote_client_send(rc, text);
903                         g_free(text);
904
905                         sent = TRUE;
906                         }
907
908                 if (path && !cmd_list && use_path)
909                         {
910                         gchar *text;
911
912                         text = g_strdup_printf("file:%s", path);
913                         remote_client_send(rc, text);
914                         g_free(text);
915
916                         sent = TRUE;
917                         }
918
919                 work = collection_list;
920                 while (work)
921                         {
922                         const gchar *name;
923                         gchar *text;
924
925                         name = work->data;
926                         work = work->next;
927
928                         text = g_strdup_printf("file:%s", name);
929                         remote_client_send(rc, text);
930                         g_free(text);
931
932                         sent = TRUE;
933                         }
934
935                 if (!started && !sent)
936                         {
937                         remote_client_send(rc, "raise");
938                         }
939                 }
940         else
941                 {
942                 print_term(_("Remote not available\n"));
943                 }
944
945         _exit(0);
946 }
947
948 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
949 {
950         RemoteConnection *remote_connection = remote_server_open(path);
951         RemoteData *remote_data = g_new(RemoteData, 1);
952
953         remote_data->command_collection = command_collection;
954
955         remote_server_subscribe(remote_connection, remote_cb, remote_data);
956         return remote_connection;
957 }
958 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */