r214: Added support for mc's Virtual File System.
[rox-filer.git] / ROX-Filer / src / main.c
blobc8d94ef4bf6394043a4258824eec341092bb8013
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>
32 #ifdef HAVE_GETOPT_LONG
33 # include <getopt.h>
34 #endif
36 #include <gtk/gtk.h>
37 #include "collection.h"
39 #include "main.h"
40 #include "support.h"
41 #include "gui_support.h"
42 #include "filer.h"
43 #include "mount.h"
44 #include "menu.h"
45 #include "dnd.h"
46 #include "options.h"
47 #include "choices.h"
48 #include "savebox.h"
49 #include "type.h"
50 #include "pixmaps.h"
51 #include "dir.h"
52 #include "action.h"
54 int number_of_windows = 0; /* Quit when this reaches 0 again... */
55 int to_error_log = -1; /* Write here to log errors */
57 uid_t euid;
58 gid_t egid;
59 int ngroups; /* Number of supplemental groups */
60 gid_t *supplemental_groups = NULL;
61 char *home_dir;
63 #define VERSION "ROX-Filer 0.1.16\n" \
64 "Copyright (C) 1999 Thomas Leonard.\n" \
65 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
66 "to the extent permitted by law.\n" \
67 "You may redistribute copies of ROX-Filer\n" \
68 "under the terms of the GNU General Public License.\n" \
69 "For more information about these matters, " \
70 "see the file named COPYING.\n"
72 #ifdef HAVE_GETOPT_LONG
73 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
74 # define SHORT_ONLY_WARNING ""
75 #else
76 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
77 # define SHORT_ONLY_WARNING \
78 "NOTE: Your system does not support long options - \n" \
79 "you must use the short versions instead.\n\n"
80 #endif
82 #define SHORT_OPS "t:b:ohv"
84 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
85 "Open filer windows showing each directory listed, or $HOME \n" \
86 "if no directories are given.\n\n" \
87 " -h, --help display this help and exit\n" \
88 " -v, --version display the version information and exit\n" \
89 " -t, --top [DIR] open DIR as a top-edge panel\n" \
90 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
91 " -o, --override override window manager control of panels\n" \
92 "\n" SHORT_ONLY_WARNING \
93 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
95 #ifdef HAVE_GETOPT_LONG
96 static struct option long_opts[] =
98 {"top", 1, NULL, 't'},
99 {"bottom", 1, NULL, 'b'},
100 {"override", 0, NULL, 'o'},
101 {"help", 0, NULL, 'h'},
102 {"version", 0, NULL, 'v'},
103 {NULL, 0, NULL, 0},
105 #endif
107 /* Take control of panels away from WM? */
108 gboolean override_redirect = FALSE;
110 static void child_died(int signum)
112 int status;
113 int child;
115 /* Find out which children exited and allow them to die */
118 child = waitpid(-1, &status, WNOHANG);
120 if (child == 0 || child == -1)
121 return;
123 /* fprintf(stderr, "Child %d exited\n", child); */
125 } while (1);
128 #define BUFLEN 40
129 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
131 char buf[BUFLEN];
132 static GtkWidget *log = NULL;
133 static GtkWidget *window = NULL;
134 ssize_t len;
136 if (!window)
138 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
139 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
140 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
141 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
142 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
143 gtk_widget_hide, GTK_OBJECT(window));
144 log = gtk_text_new(NULL, NULL);
145 gtk_container_add(GTK_CONTAINER(window), log);
148 if (!GTK_WIDGET_MAPPED(window))
149 gtk_widget_show_all(window);
151 len = read(source, buf, BUFLEN);
152 if (len > 0)
153 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
156 int main(int argc, char **argv)
158 int stderr_pipe[2];
159 struct sigaction act;
160 GList *panel_dirs = NULL;
161 GList *panel_sides = NULL;
163 home_dir = g_get_home_dir();
165 mc_vfs_init();
166 gtk_init(&argc, &argv);
168 while (1)
170 int c;
171 #ifdef HAVE_GETOPT_LONG
172 int long_index;
173 c = getopt_long(argc, argv, SHORT_OPS,
174 long_opts, &long_index);
175 #else
176 c = getopt(argc, argv, SHORT_OPS);
177 #endif
179 if (c == EOF)
180 break; /* No more options */
182 switch (c)
184 case 'o':
185 override_redirect = TRUE;
186 break;
187 case 'v':
188 printf(VERSION);
189 return EXIT_SUCCESS;
190 case 'h':
191 printf(HELP);
192 return EXIT_SUCCESS;
193 case 't':
194 case 'b':
195 panel_sides = g_list_prepend(panel_sides,
196 (gpointer) c);
197 panel_dirs = g_list_prepend(panel_dirs,
198 *optarg == '=' ? optarg + 1
199 : optarg);
200 break;
201 default:
202 printf(USAGE);
203 return EXIT_FAILURE;
207 choices_init("ROX-Filer");
209 gui_support_init();
210 pixmaps_init();
211 dir_init();
212 menu_init();
213 dnd_init();
214 filer_init();
215 mount_init();
216 options_init();
217 savebox_init();
218 type_init();
219 action_init();
221 options_load();
223 /* Let child processes die */
224 act.sa_handler = child_died;
225 sigemptyset(&act.sa_mask);
226 act.sa_flags = SA_NOCLDSTOP;
227 sigaction(SIGCHLD, &act, NULL);
229 /* Ignore SIGPIPE - check for EPIPE errors instead */
230 act.sa_handler = SIG_IGN;
231 sigemptyset(&act.sa_mask);
232 act.sa_flags = 0;
233 sigaction(SIGPIPE, &act, NULL);
235 euid = geteuid();
236 egid = getegid();
237 ngroups = getgroups(0, NULL);
238 if (ngroups < 0)
239 ngroups = 0;
240 else if (ngroups > 0)
242 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
243 getgroups(ngroups, supplemental_groups);
246 if (euid == 0)
248 if (get_choice("!!!DANGER!!!",
249 "Running ROX-Filer as root is VERY dangerous. If I "
250 "had a warranty (I don't) then doing this would "
251 "void it", 2,
252 "Don't click here", "Quit") != 0)
253 exit(EXIT_SUCCESS);
256 if (optind == argc && !panel_dirs)
257 filer_opendir(home_dir, PANEL_NO);
258 else
260 int i = optind;
261 GList *dir = panel_dirs;
262 GList *side = panel_sides;
264 while (dir)
266 int c = (int) side->data;
268 filer_opendir((char *) dir->data,
269 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
270 dir = dir->next;
271 side = side->next;
274 g_list_free(dir);
275 g_list_free(side);
277 while (i < argc)
278 filer_opendir(argv[i++], PANEL_NO);
281 pipe(stderr_pipe);
282 close_on_exec(stderr_pipe[0], TRUE);
283 close_on_exec(stderr_pipe[1], TRUE);
284 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
285 to_error_log = stderr_pipe[1];
287 gtk_main();
289 return EXIT_SUCCESS;