r333: In the path entry minibuffer, an exact match is favoured over any other
[rox-filer.git] / ROX-Filer / src / main.c
blobd1bd22146b0a574df8f550a5e35865740f0a7536
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <pwd.h>
32 #include <grp.h>
34 #ifdef HAVE_GETOPT_LONG
35 # include <getopt.h>
36 #endif
38 #include <gtk/gtk.h>
39 #include "collection.h"
41 #include "main.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "filer.h"
45 #include "display.h"
46 #include "mount.h"
47 #include "menu.h"
48 #include "dnd.h"
49 #include "options.h"
50 #include "choices.h"
51 #include "type.h"
52 #include "pixmaps.h"
53 #include "dir.h"
54 #include "action.h"
55 #include "i18n.h"
56 #include "remote.h"
57 #include "pinboard.h"
58 #include "run.h"
60 int number_of_windows = 0; /* Quit when this reaches 0 again... */
61 int to_error_log = -1; /* Write here to log errors */
63 uid_t euid;
64 gid_t egid;
65 int ngroups; /* Number of supplemental groups */
66 gid_t *supplemental_groups = NULL;
67 char *home_dir, *app_dir;
69 /* Static prototypes */
70 static void show_features(void);
73 #define COPYING \
74 N_("Copyright (C) 2000 Thomas Leonard.\n" \
75 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
76 "to the extent permitted by law.\n" \
77 "You may redistribute copies of ROX-Filer\n" \
78 "under the terms of the GNU General Public License.\n" \
79 "For more information about these matters, " \
80 "see the file named COPYING.\n")
82 #ifdef HAVE_GETOPT_LONG
83 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
84 # define SHORT_ONLY_WARNING ""
85 #else
86 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
87 # define SHORT_ONLY_WARNING \
88 _("NOTE: Your system does not support long options - \n" \
89 "you must use the short versions instead.\n\n")
90 #endif
92 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
93 "Open each directory or file listed, or the current working\n" \
94 "directory if no arguments are given.\n\n" \
95 " -b, --bottom=DIR open DIR as a bottom-edge panel\n" \
96 " -h, --help display this help and exit\n" \
97 " -n, --new start a new filer, even if already running\n" \
98 " -o, --override override window manager control of panels\n" \
99 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
100 " -t, --top=DIR open DIR as a top-edge panel\n" \
101 " -v, --version display the version information and exit\n" \
102 "\nThe latest version can be found at:\n" \
103 "\thttp://rox.sourceforge.net\n" \
104 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
106 #define SHORT_OPS "t:b:op:hvn"
108 #ifdef HAVE_GETOPT_LONG
109 static struct option long_opts[] =
111 {"top", 1, NULL, 't'},
112 {"bottom", 1, NULL, 'b'},
113 {"override", 0, NULL, 'o'},
114 {"pinboard", 1, NULL, 'p'},
115 {"help", 0, NULL, 'h'},
116 {"version", 0, NULL, 'v'},
117 {"new", 0, NULL, 'n'},
118 {NULL, 0, NULL, 0},
120 #endif
122 /* Take control of panels away from WM? */
123 gboolean override_redirect = FALSE;
125 /* Always start a new filer, even if one seems to be already running */
126 gboolean new_copy = FALSE;
128 /* Maps child PIDs to Callback pointers */
129 static GHashTable *death_callbacks = NULL;
130 static gboolean child_died_flag = FALSE;
132 /* This is called as a signal handler; simply ensures that
133 * child_died_callback() will get called later.
135 static void child_died(int signum)
137 child_died_flag = TRUE;
138 write(to_error_log, '\0', 1); /* Wake up! */
141 static void child_died_callback(void)
143 int status;
144 int child;
146 child_died_flag = FALSE;
148 /* Find out which children exited and allow them to die */
151 Callback *cb;
153 child = waitpid(-1, &status, WNOHANG);
155 if (child == 0 || child == -1)
156 return;
158 cb = g_hash_table_lookup(death_callbacks, (gpointer) child);
159 if (cb)
161 cb->callback(cb->data);
162 g_hash_table_remove(death_callbacks, (gpointer) child);
165 } while (1);
168 #define BUFLEN 40
169 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
171 char buf[BUFLEN];
172 static GtkWidget *log = NULL;
173 static GtkWidget *window = NULL;
174 ssize_t len;
176 if (!window)
178 GtkWidget *hbox, *scrollbar;
180 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
181 gtk_window_set_title(GTK_WINDOW(window),
182 _("ROX-Filer message log"));
183 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
184 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
185 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
186 gtk_widget_hide, GTK_OBJECT(window));
189 hbox = gtk_hbox_new(FALSE, 0);
190 gtk_container_add(GTK_CONTAINER(window), hbox);
191 scrollbar = gtk_vscrollbar_new(NULL);
193 log = gtk_text_new(NULL,
194 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
195 gtk_box_pack_start(GTK_BOX(hbox), log, TRUE, TRUE, 0);
196 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
199 len = read(source, buf, BUFLEN);
201 if (child_died_flag)
203 child_died_callback();
204 if (len == 1 && !*buf)
205 return;
208 if (len > 0)
210 if (!GTK_WIDGET_MAPPED(window))
211 gtk_widget_show_all(window);
212 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
216 /* The value that goes with an option */
217 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
219 int main(int argc, char **argv)
221 int stderr_pipe[2];
222 int i;
223 struct sigaction act;
224 guchar *tmp;
226 /* This is a list of \0 separated strings. Each string starts with a
227 * character indicating what kind of operation to perform:
229 * fFILE open this file (or directory)
230 * dDIR open this dir (even if it looks like an app)
231 * pPIN display this pinboard
232 * tDIR open DIR as a top-panel
233 * bDIR open DIR as a bottom-panel
235 GString *to_open;
237 to_open = g_string_new(NULL);
239 choices_init();
240 i18n_init();
242 home_dir = g_get_home_dir();
243 app_dir = g_strdup(getenv("APP_DIR"));
244 if (app_dir)
245 unsetenv("APP_DIR");
246 else
248 g_warning("APP_DIR environment variable was unset!\n"
249 "Use the AppRun script to invoke ROX-Filer...\n");
250 app_dir = g_get_current_dir();
253 death_callbacks = g_hash_table_new(NULL, NULL);
255 #ifdef HAVE_LIBVFS
256 mc_vfs_init();
257 #endif
258 gtk_init(&argc, &argv);
260 while (1)
262 int c;
263 #ifdef HAVE_GETOPT_LONG
264 int long_index;
265 c = getopt_long(argc, argv, SHORT_OPS,
266 long_opts, &long_index);
267 #else
268 c = getopt(argc, argv, SHORT_OPS);
269 #endif
271 if (c == EOF)
272 break; /* No more options */
274 switch (c)
276 case 'n':
277 new_copy = TRUE;
278 break;
279 case 'o':
280 override_redirect = TRUE;
281 break;
282 case 'v':
283 fprintf(stderr, "ROX-Filer %s\n", VERSION);
284 fprintf(stderr, _(COPYING));
285 show_features();
286 return EXIT_SUCCESS;
287 case 'h':
288 fprintf(stderr, _(HELP));
289 fprintf(stderr, _(SHORT_ONLY_WARNING));
290 return EXIT_SUCCESS;
291 case 't':
292 case 'b':
293 g_string_append_c(to_open, '<');
294 g_string_append_c(to_open, c);
295 g_string_append_c(to_open, '>');
296 tmp = pathdup(VALUE);
297 g_string_append(to_open, tmp);
298 g_free(tmp);
299 break;
300 case 'p':
301 g_string_append(to_open, "<p>");
302 g_string_append(to_open, VALUE);
303 break;
304 default:
305 printf(_(USAGE));
306 return EXIT_FAILURE;
310 i = optind;
311 while (i < argc)
313 tmp = pathdup(argv[i++]);
315 g_string_append(to_open, "<f>");
316 g_string_append(to_open, tmp);
319 if (to_open->len == 0)
321 guchar *dir;
323 dir = g_get_current_dir();
324 g_string_sprintf(to_open, "<d>%s", dir);
325 g_free(dir);
328 gui_support_init();
329 if (remote_init(to_open, new_copy))
330 return EXIT_SUCCESS; /* Already running */
331 pixmaps_init();
333 dir_init();
334 menu_init();
335 dnd_init();
336 filer_init();
337 display_init();
338 mount_init();
339 options_init();
340 type_init();
341 action_init();
343 options_load();
345 /* Let child processes die */
346 act.sa_handler = child_died;
347 sigemptyset(&act.sa_mask);
348 act.sa_flags = SA_NOCLDSTOP;
349 sigaction(SIGCHLD, &act, NULL);
351 /* Ignore SIGPIPE - check for EPIPE errors instead */
352 act.sa_handler = SIG_IGN;
353 sigemptyset(&act.sa_mask);
354 act.sa_flags = 0;
355 sigaction(SIGPIPE, &act, NULL);
357 euid = geteuid();
358 egid = getegid();
359 ngroups = getgroups(0, NULL);
360 if (ngroups < 0)
361 ngroups = 0;
362 else if (ngroups > 0)
364 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
365 getgroups(ngroups, supplemental_groups);
368 if (euid == 0)
370 if (get_choice(_("!!!DANGER!!!"),
371 _("Running ROX-Filer as root is VERY dangerous. If I "
372 "had a warranty (I don't) then doing this would "
373 "void it"), 2,
374 _("Don't click here"), _("Quit")) != 0)
375 exit(EXIT_SUCCESS);
378 run_list(to_open->str);
379 g_string_free(to_open, TRUE);
381 pipe(stderr_pipe);
382 close_on_exec(stderr_pipe[0], TRUE);
383 close_on_exec(stderr_pipe[1], TRUE);
384 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
385 to_error_log = stderr_pipe[1];
387 if (number_of_windows > 0)
388 gtk_main();
390 return EXIT_SUCCESS;
393 static void show_features(void)
395 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
396 g_printerr("%s... %s\n", _("VFS support"),
397 #ifdef HAVE_LIBVFS
398 _("Yes")
399 #else
400 _("No (couldn't find a valid libvfs)")
401 #endif
403 g_printerr("%s... %s\n", _("ImLib support"),
404 #ifdef HAVE_IMLIB
405 _("Yes")
406 #else
407 _("No (the imlib-config command didn't work)")
408 #endif
412 /* Register a function to be called when process number 'child' dies. */
413 void on_child_death(int child, CallbackFn callback, gpointer data)
415 Callback *cb;
417 g_return_if_fail(callback != NULL);
419 cb = g_new(Callback, 1);
421 cb->callback = callback;
422 cb->data = data;
424 g_hash_table_insert(death_callbacks, (gpointer) child, cb);