r328: Changed the install script so that the CVS directories don't get installed.
[rox-filer.git] / ROX-Filer / src / main.c
bloba8c06ea90950184077d8138683c5e81e7124d917
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;
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 death_callbacks = g_hash_table_new(NULL, NULL);
245 #ifdef HAVE_LIBVFS
246 mc_vfs_init();
247 #endif
248 gtk_init(&argc, &argv);
250 while (1)
252 int c;
253 #ifdef HAVE_GETOPT_LONG
254 int long_index;
255 c = getopt_long(argc, argv, SHORT_OPS,
256 long_opts, &long_index);
257 #else
258 c = getopt(argc, argv, SHORT_OPS);
259 #endif
261 if (c == EOF)
262 break; /* No more options */
264 switch (c)
266 case 'n':
267 new_copy = TRUE;
268 break;
269 case 'o':
270 override_redirect = TRUE;
271 break;
272 case 'v':
273 fprintf(stderr, "ROX-Filer %s\n", VERSION);
274 fprintf(stderr, _(COPYING));
275 show_features();
276 return EXIT_SUCCESS;
277 case 'h':
278 fprintf(stderr, _(HELP));
279 fprintf(stderr, _(SHORT_ONLY_WARNING));
280 return EXIT_SUCCESS;
281 case 't':
282 case 'b':
283 g_string_append_c(to_open, '<');
284 g_string_append_c(to_open, c);
285 g_string_append_c(to_open, '>');
286 tmp = pathdup(VALUE);
287 g_string_append(to_open, tmp);
288 g_free(tmp);
289 break;
290 case 'p':
291 g_string_append(to_open, "<p>");
292 g_string_append(to_open, VALUE);
293 break;
294 default:
295 printf(_(USAGE));
296 return EXIT_FAILURE;
300 i = optind;
301 while (i < argc)
303 tmp = pathdup(argv[i++]);
305 g_string_append(to_open, "<f>");
306 g_string_append(to_open, tmp);
309 if (to_open->len == 0)
311 guchar *dir;
313 dir = g_get_current_dir();
314 g_string_sprintf(to_open, "<d>%s", dir);
315 g_free(dir);
318 gui_support_init();
319 if (remote_init(to_open, new_copy))
320 return EXIT_SUCCESS; /* Already running */
321 pixmaps_init();
323 dir_init();
324 menu_init();
325 dnd_init();
326 filer_init();
327 display_init();
328 mount_init();
329 options_init();
330 type_init();
331 action_init();
333 options_load();
335 /* Let child processes die */
336 act.sa_handler = child_died;
337 sigemptyset(&act.sa_mask);
338 act.sa_flags = SA_NOCLDSTOP;
339 sigaction(SIGCHLD, &act, NULL);
341 /* Ignore SIGPIPE - check for EPIPE errors instead */
342 act.sa_handler = SIG_IGN;
343 sigemptyset(&act.sa_mask);
344 act.sa_flags = 0;
345 sigaction(SIGPIPE, &act, NULL);
347 euid = geteuid();
348 egid = getegid();
349 ngroups = getgroups(0, NULL);
350 if (ngroups < 0)
351 ngroups = 0;
352 else if (ngroups > 0)
354 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
355 getgroups(ngroups, supplemental_groups);
358 if (euid == 0)
360 if (get_choice(_("!!!DANGER!!!"),
361 _("Running ROX-Filer as root is VERY dangerous. If I "
362 "had a warranty (I don't) then doing this would "
363 "void it"), 2,
364 _("Don't click here"), _("Quit")) != 0)
365 exit(EXIT_SUCCESS);
368 run_list(to_open->str);
369 g_string_free(to_open, TRUE);
371 pipe(stderr_pipe);
372 close_on_exec(stderr_pipe[0], TRUE);
373 close_on_exec(stderr_pipe[1], TRUE);
374 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
375 to_error_log = stderr_pipe[1];
377 if (number_of_windows > 0)
378 gtk_main();
380 return EXIT_SUCCESS;
383 static void show_features(void)
385 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
386 g_printerr("%s... %s\n", _("VFS support"),
387 #ifdef HAVE_LIBVFS
388 _("Yes")
389 #else
390 _("No (couldn't find a valid libvfs)")
391 #endif
393 g_printerr("%s... %s\n", _("ImLib support"),
394 #ifdef HAVE_IMLIB
395 _("Yes")
396 #else
397 _("No (the imlib-config command didn't work)")
398 #endif
402 /* Register a function to be called when process number 'child' dies. */
403 void on_child_death(int child, CallbackFn callback, gpointer data)
405 Callback *cb;
407 g_return_if_fail(callback != NULL);
409 cb = g_new(Callback, 1);
411 cb->callback = callback;
412 cb->data = data;
414 g_hash_table_insert(death_callbacks, (gpointer) child, cb);