r1996: Cope slightly better with invalid filenames in various places (reported by
[rox-filer.git] / ROX-Filer / src / main.c
bloba54e94a10ef24adea0d7d964c1f91b2490097f65
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* main.c - parses command-line options and parameters, plus some global
23 * housekeeping.
25 * New to the code and feeling lost? Read global.h now.
28 #include "config.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <pwd.h>
39 #include <grp.h>
40 #include <libxml/parser.h>
42 #ifdef HAVE_GETOPT_LONG
43 # include <getopt.h>
44 #endif
46 #include <gtk/gtk.h>
48 #include "global.h"
50 #include "main.h"
51 #include "support.h"
52 #include "gui_support.h"
53 #include "filer.h"
54 #include "display.h"
55 #include "mount.h"
56 #include "menu.h"
57 #include "dnd.h"
58 #include "options.h"
59 #include "choices.h"
60 #include "type.h"
61 #include "pixmaps.h"
62 #include "dir.h"
63 #include "diritem.h"
64 #include "action.h"
65 #include "i18n.h"
66 #include "remote.h"
67 #include "pinboard.h"
68 #include "run.h"
69 #include "toolbar.h"
70 #include "bind.h"
71 #include "panel.h"
72 #include "session.h"
73 #include "minibuffer.h"
75 int number_of_windows = 0; /* Quit when this reaches 0 again... */
76 static int to_wakeup_pipe = -1; /* Write here to get noticed */
78 /* Information about the ROX-Filer process */
79 uid_t euid;
80 gid_t egid;
81 int ngroups; /* Number of supplemental groups */
82 gid_t *supplemental_groups = NULL;
84 /* Message to display at the top of each filer window */
85 const gchar *show_user_message = NULL;
87 int home_dir_len;
88 const char *home_dir, *app_dir;
90 GtkTooltips *tooltips = NULL;
92 #define COPYING \
93 N_("Copyright (C) 2002 Thomas Leonard.\n" \
94 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
95 "to the extent permitted by law.\n" \
96 "You may redistribute copies of ROX-Filer\n" \
97 "under the terms of the GNU General Public License.\n" \
98 "For more information about these matters, " \
99 "see the file named COPYING.\n")
101 #ifdef HAVE_GETOPT_LONG
102 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
103 # define SHORT_ONLY_WARNING ""
104 #else
105 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
106 # define SHORT_ONLY_WARNING \
107 _("NOTE: Your system does not support long options - \n" \
108 "you must use the short versions instead.\n\n")
109 #endif
111 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
112 "Open each directory or file listed, or the current working\n" \
113 "directory if no arguments are given.\n\n" \
114 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
115 " -c, --client-id=ID used for session management\n" \
116 " -d, --dir=DIR open DIR as directory (not application)\n" \
117 " -D, --close=DIR close DIR and its subdirectories\n" \
118 " -h, --help display this help and exit\n" \
119 " -l, --left=PANEL open PAN as a left-edge panel\n" \
120 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
121 " -n, --new start new copy; for debugging the filer\n" \
122 " -o, --override override window manager control of panels\n" \
123 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
124 " -r, --right=PANEL open PAN as a right-edge panel\n" \
125 " -R, --RPC invoke method call read from stdin\n" \
126 " -s, --show=FILE open a directory showing FILE\n" \
127 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
128 " -u, --user show user name in each window \n" \
129 " -v, --version display the version information and exit\n" \
130 " -x, --examine=FILE FILE has changed - re-examine it\n" \
131 "\nThe latest version can be found at:\n" \
132 "\thttp://rox.sourceforge.net\n" \
133 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
135 #define SHORT_OPS "c:d:t:b:l:r:op:s:hvnux:m:D:R"
137 #ifdef HAVE_GETOPT_LONG
138 static struct option long_opts[] =
140 {"dir", 1, NULL, 'd'},
141 {"top", 1, NULL, 't'},
142 {"bottom", 1, NULL, 'b'},
143 {"left", 1, NULL, 'l'},
144 {"override", 0, NULL, 'o'},
145 {"pinboard", 1, NULL, 'p'},
146 {"right", 1, NULL, 'r'},
147 {"help", 0, NULL, 'h'},
148 {"version", 0, NULL, 'v'},
149 {"user", 0, NULL, 'u'},
150 {"new", 0, NULL, 'n'},
151 {"RPC", 0, NULL, 'R'},
152 {"show", 1, NULL, 's'},
153 {"examine", 1, NULL, 'x'},
154 {"close", 1, NULL, 'D'},
155 {"mime-type", 1, NULL, 'm'},
156 {"client-id", 1, NULL, 'c'},
157 {NULL, 0, NULL, 0},
159 #endif
161 /* Take control of panels away from WM? */
162 Option o_override_redirect;
164 /* Always start a new filer, even if one seems to be already running */
165 gboolean new_copy = FALSE;
167 /* Maps child PIDs to Callback pointers */
168 static GHashTable *death_callbacks = NULL;
169 static gboolean child_died_flag = FALSE;
171 Option o_dnd_no_hostnames;
173 /* Static prototypes */
174 static void show_features(void);
175 static void soap_add(xmlNodePtr body,
176 xmlChar *function,
177 const xmlChar *arg1_name, const xmlChar *arg1_value,
178 const xmlChar *arg2_name, const xmlChar *arg2_value);
179 static void child_died(int signum);
180 static void child_died_callback(void);
181 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
183 /****************************************************************
184 * EXTERNAL INTERFACE *
185 ****************************************************************/
187 /* The value that goes with an option */
188 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
190 /* Parses the command-line to work out what the user wants to do.
191 * Tries to send the request to an already-running copy of the filer.
192 * If that fails, it initialises all the other modules and executes the
193 * request itself.
195 int main(int argc, char **argv)
197 int wakeup_pipe[2];
198 int i;
199 struct sigaction act;
200 guchar *tmp, *dir;
201 gchar *client_id = NULL;
202 gboolean show_user = FALSE;
203 xmlDocPtr rpc, soap_rpc = NULL, reply;
204 xmlNodePtr body;
206 home_dir = g_get_home_dir();
207 home_dir_len = strlen(home_dir);
208 app_dir = g_strdup(getenv("APP_DIR"));
210 /* Get internationalisation up and running. This requires the
211 * choices system, to discover the user's preferred language.
213 choices_init();
214 options_init();
215 i18n_init();
217 if (!app_dir)
219 g_warning("APP_DIR environment variable was unset!\n"
220 "Use the AppRun script to invoke ROX-Filer...\n");
221 app_dir = g_get_current_dir();
223 #ifdef HAVE_UNSETENV
224 else
226 /* Don't pass it on to our child processes... */
227 unsetenv("APP_DIR");
229 #endif
231 /* Sometimes we want to take special action when a child
232 * process exits. This hash table is used to convert the
233 * child's PID to the callback function.
235 death_callbacks = g_hash_table_new(NULL, NULL);
237 /* Find out some information about ourself */
238 euid = geteuid();
239 egid = getegid();
240 ngroups = getgroups(0, NULL);
241 if (ngroups < 0)
242 ngroups = 0;
243 else if (ngroups > 0)
245 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
246 getgroups(ngroups, supplemental_groups);
249 if (argc == 2 && strcmp(argv[1], "-v") == 0)
251 /* This is used by install.sh to test if the filer
252 * compiled OK. Do this test before gtk_init so that
253 * we don't need an X server to install.
255 g_printerr("ROX-Filer %s\n", VERSION);
256 g_printerr(_(COPYING));
257 show_features();
258 return EXIT_SUCCESS;
261 option_add_int(&o_override_redirect, "override_redirect", FALSE);
263 /* The idea here is to convert the command-line arguments
264 * into a SOAP RPC.
265 * We attempt to invoke the call on an already-running copy of
266 * the filer if possible, or execute it ourselves if not.
268 rpc = soap_new(&body);
270 /* Note: must do this before checking our options,
271 * otherwise we report an error for Gtk's options.
273 gtk_init(&argc, &argv);
274 /* Set a default style for the collection widget */
275 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
276 " bg[NORMAL] = \"#f3f3f3\"\n"
277 " fg[NORMAL] = \"#000000\"\n"
278 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
279 " fg[INSENSITIVE] = \"#000000\"\n"
280 "}\n"
281 "style \"rox-default-pinboard-style\" {\n"
282 " bg[NORMAL] = \"#666666\"\n"
283 "}\n"
284 "widget \"rox-pinboard\" style : gtk "
285 "\"rox-default-pinboard-style\"\n"
287 "class \"Collection\" style : gtk "
288 "\"rox-default-collection-style\"\n");
290 /* Process each option in turn */
291 while (1)
293 int c;
294 #ifdef HAVE_GETOPT_LONG
295 int long_index;
296 c = getopt_long(argc, argv, SHORT_OPS,
297 long_opts, &long_index);
298 #else
299 c = getopt(argc, argv, SHORT_OPS);
300 #endif
302 if (c == EOF)
303 break; /* No more options */
305 switch (c)
307 case 'n':
308 new_copy = TRUE;
309 break;
310 case 'o':
311 info_message(_("The -o argument is no longer "
312 "used. You can turn on override "
313 "redirect from the Options box "
314 "instead."));
315 break;
316 case 'v':
317 g_printerr("ROX-Filer %s\n", VERSION);
318 g_printerr("%s", _(COPYING));
319 show_features();
320 return EXIT_SUCCESS;
321 case 'h':
322 g_printerr("%s", _(HELP));
323 g_printerr("%s", _(SHORT_ONLY_WARNING));
324 return EXIT_SUCCESS;
325 case 'D':
326 case 'd':
327 case 'x':
328 /* Argument is a path */
329 if (c == 'd' && VALUE[0] == '/')
330 tmp = g_strdup(VALUE);
331 else
332 tmp = pathdup(VALUE);
333 soap_add(body,
334 c == 'D' ? "CloseDir" :
335 c == 'd' ? "OpenDir" :
336 c == 'x' ? "Examine" : "Unknown",
337 "Filename", tmp,
338 NULL, NULL);
339 g_free(tmp);
340 break;
341 case 's':
342 tmp = g_dirname(VALUE);
344 if (tmp[0] == '/')
345 dir = NULL;
346 else
347 dir = pathdup(tmp);
349 soap_add(body, "Show",
350 "Directory", dir ? dir : tmp,
351 "Leafname", g_basename(VALUE));
352 g_free(tmp);
353 g_free(dir);
354 break;
355 case 'l':
356 case 'r':
357 case 't':
358 case 'b':
359 /* Argument is a leaf (or starts with /) */
360 soap_add(body, "Panel", "Name", VALUE,
361 "Side", c == 'l' ? "Left" :
362 c == 'r' ? "Right" :
363 c == 't' ? "Top" :
364 c == 'b' ? "Bottom" :
365 "Unkown");
366 break;
367 case 'p':
368 soap_add(body, "Pinboard",
369 "Name", VALUE, NULL, NULL);
370 break;
371 case 'u':
372 show_user = TRUE;
373 break;
374 case 'm':
376 MIME_type *type;
377 type_init();
378 type = type_get_type(VALUE);
379 printf("%s/%s\n", type->media_type,
380 type->subtype);
381 return EXIT_SUCCESS;
383 case 'c':
384 client_id = g_strdup(VALUE);
385 break;
386 case 'R':
387 soap_rpc = xmlParseFile("-");
388 if (!soap_rpc)
389 g_error("Invalid XML in RPC");
390 break;
391 default:
392 printf(_(USAGE));
393 return EXIT_FAILURE;
397 tooltips = gtk_tooltips_new();
399 if (euid == 0 || show_user)
400 show_user_message = g_strdup_printf(_("Running as user '%s'"),
401 user_name(euid));
403 /* Add each remaining (non-option) argument to the list of files
404 * to run.
406 i = optind;
407 while (i < argc)
409 tmp = pathdup(argv[i++]);
411 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
413 g_free(tmp);
416 if (soap_rpc)
418 if (body->xmlChildrenNode)
419 g_error("Can't use -R with other options - sorry!");
420 xmlFreeDoc(rpc);
421 body = NULL;
422 rpc = soap_rpc;
424 else if (!body->xmlChildrenNode)
426 /* The user didn't request any action. Open the current
427 * directory.
429 guchar *dir;
431 dir = g_get_current_dir();
432 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
433 g_free(dir);
436 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
438 /* Try to send the request to an already-running copy of the filer */
439 gui_support_init();
440 if (remote_init(rpc, new_copy))
441 return EXIT_SUCCESS; /* It worked - exit */
443 /* Put ourselves into the background (so 'rox' always works the
444 * same, whether we're already running or not).
445 * Not for -n, though (helps when debugging).
447 if (!new_copy)
449 pid_t child;
451 child = fork();
452 if (child > 0)
453 _exit(0); /* Parent exits */
454 /* Otherwise we're the child (or an error occurred - ignore
455 * it!).
459 /* Close stdin. We don't need it, and it can cause problems if
460 * a child process wants a password, etc...
463 int fd;
464 fd = open("/dev/null", O_RDONLY);
465 if (fd > 0)
467 close(0);
468 dup2(fd, 0);
469 close(fd);
473 /* Initialize the rest of the filer... */
475 pixmaps_init();
477 dnd_init();
478 bind_init();
479 dir_init();
480 diritem_init();
481 menu_init();
482 minibuffer_init();
483 filer_init();
484 toolbar_init();
485 display_init();
486 mount_init();
487 type_init();
488 action_init();
490 pinboard_init();
491 panel_init();
493 /* Let everyone update */
494 options_notify();
496 /* When we get a signal, we can't do much right then. Instead,
497 * we send a char down this pipe, which causes the main loop to
498 * deal with the event next time we're idle.
500 pipe(wakeup_pipe);
501 close_on_exec(wakeup_pipe[0], TRUE);
502 close_on_exec(wakeup_pipe[1], TRUE);
503 gtk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
504 NULL, NULL, NULL);
505 to_wakeup_pipe = wakeup_pipe[1];
507 /* If the pipe is full then we're going to get woken up anyway... */
508 set_blocking(to_wakeup_pipe, FALSE);
510 /* Let child processes die */
511 act.sa_handler = child_died;
512 sigemptyset(&act.sa_mask);
513 act.sa_flags = SA_NOCLDSTOP;
514 sigaction(SIGCHLD, &act, NULL);
516 /* Ignore SIGPIPE - check for EPIPE errors instead */
517 act.sa_handler = SIG_IGN;
518 sigemptyset(&act.sa_mask);
519 act.sa_flags = 0;
520 sigaction(SIGPIPE, &act, NULL);
522 /* Set up session managament if available */
523 session_init(client_id);
524 g_free(client_id);
526 /* Finally, execute the request */
527 reply = run_soap(rpc);
528 xmlFreeDoc(rpc);
529 if (reply)
531 /* Write the result, if any, to stdout */
532 save_xml_file(reply, "-");
533 xmlFreeDoc(reply);
536 /* Enter the main loop, processing events until all our windows
537 * are closed.
539 if (number_of_windows > 0)
540 gtk_main();
542 return EXIT_SUCCESS;
545 /* Register a function to be called when process number 'child' dies. */
546 void on_child_death(gint child, CallbackFn callback, gpointer data)
548 Callback *cb;
550 g_return_if_fail(callback != NULL);
552 cb = g_new(Callback, 1);
554 cb->callback = callback;
555 cb->data = data;
557 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
560 void one_less_window(void)
562 if (--number_of_windows < 1)
563 gtk_main_quit();
566 /****************************************************************
567 * INTERNAL FUNCTIONS *
568 ****************************************************************/
570 static void show_features(void)
572 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
573 g_printerr("%s... %s\n", _("Large File Support"),
574 #ifdef LARGE_FILE_SUPPORT
575 _("Yes")
576 #else
577 _("No")
578 #endif
580 #if 0
581 g_printerr("%s... %s\n", _("GNOME-VFS library"),
582 # ifdef WITH_GNOMEVFS
583 _("Yes")
584 # else
585 _("No (gnome-vfs-config not found)")
586 # endif
588 #endif
591 static void soap_add(xmlNodePtr body,
592 xmlChar *function,
593 const xmlChar *arg1_name, const xmlChar *arg1_value,
594 const xmlChar *arg2_name, const xmlChar *arg2_value)
596 xmlNodePtr node;
597 xmlNs *rox;
599 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
601 node = xmlNewChild(body, rox, function, NULL);
603 if (arg1_name)
605 xmlNewTextChild(node, rox, arg1_name, arg1_value);
606 if (arg2_name)
607 xmlNewTextChild(node, rox, arg2_name, arg2_value);
611 /* This is called as a signal handler; simply ensures that
612 * child_died_callback() will get called later.
614 static void child_died(int signum)
616 child_died_flag = TRUE;
617 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
620 static void child_died_callback(void)
622 int status;
623 gint child;
625 child_died_flag = FALSE;
627 /* Find out which children exited and allow them to die */
630 Callback *cb;
632 child = waitpid(-1, &status, WNOHANG);
634 if (child == 0 || child == -1)
635 return;
637 cb = g_hash_table_lookup(death_callbacks,
638 GINT_TO_POINTER(child));
639 if (cb)
641 cb->callback(cb->data);
642 g_hash_table_remove(death_callbacks,
643 GINT_TO_POINTER(child));
646 } while (1);
649 #define BUFLEN 40
650 /* When data is written to_wakeup_pipe, this gets called from the event
651 * loop some time later. Useful for getting out of signal handlers, etc.
653 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
655 char buf[BUFLEN];
657 read(source, buf, BUFLEN);
659 if (child_died_flag)
660 child_died_callback();