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)
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
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
25 * New to the code and feeling lost? Read global.h now.
32 #include <sys/types.h>
40 #include <libxml/parser.h>
42 #ifdef HAVE_GETOPT_LONG
52 #include "gui_support.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 */
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
;
88 const char *home_dir
, *app_dir
;
90 GtkTooltips
*tooltips
= NULL
;
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 ""
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")
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 a new filer, even if already running\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'},
161 /* Take control of panels away from WM? */
162 gboolean override_redirect
= FALSE
;
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
,
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
195 int main(int argc
, char **argv
)
199 struct sigaction act
;
201 gchar
*client_id
= NULL
;
202 gboolean show_user
= FALSE
;
203 xmlDocPtr rpc
, soap_rpc
= NULL
, reply
;
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.
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();
226 /* Don't pass it on to our child processes... */
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 */
240 ngroups
= getgroups(0, NULL
);
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
));
261 /* The idea here is to convert the command-line arguments
263 * We attempt to invoke the call on an already-running copy of
264 * the filer if possible, or execute it ourselves if not.
266 rpc
= soap_new(&body
);
268 /* Note: must do this before checking our options,
269 * otherwise we report an error for Gtk's options.
271 gtk_init(&argc
, &argv
);
272 /* Set a default style for the collection widget */
273 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
274 " bg[NORMAL] = \"#f3f3f3\"\n"
275 " fg[NORMAL] = \"#000000\"\n"
276 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
277 " fg[INSENSITIVE] = \"#000000\"\n"
279 "style \"rox-default-pinboard-style\" {\n"
280 " bg[NORMAL] = \"#666666\"\n"
282 "widget \"rox-pinboard\" style : gtk "
283 "\"rox-default-pinboard-style\"\n"
285 "class \"Collection\" style : gtk "
286 "\"rox-default-collection-style\"\n");
288 /* Process each option in turn */
292 #ifdef HAVE_GETOPT_LONG
294 c
= getopt_long(argc
, argv
, SHORT_OPS
,
295 long_opts
, &long_index
);
297 c
= getopt(argc
, argv
, SHORT_OPS
);
301 break; /* No more options */
309 override_redirect
= TRUE
;
312 g_printerr("ROX-Filer %s\n", VERSION
);
313 g_printerr("%s", _(COPYING
));
317 g_printerr("%s", _(HELP
));
318 g_printerr("%s", _(SHORT_ONLY_WARNING
));
323 /* Argument is a path */
324 if (c
== 'd' && VALUE
[0] == '/')
325 tmp
= g_strdup(VALUE
);
327 tmp
= pathdup(VALUE
);
329 c
== 'D' ? "CloseDir" :
330 c
== 'd' ? "OpenDir" :
331 c
== 'x' ? "Examine" : "Unknown",
337 tmp
= g_dirname(VALUE
);
344 soap_add(body
, "Show",
345 "Directory", dir
? dir
: tmp
,
346 "Leafname", g_basename(VALUE
));
354 /* Argument is a leaf (or starts with /) */
355 soap_add(body
, "Panel", "Name", VALUE
,
356 "Side", c
== 'l' ? "Left" :
359 c
== 'b' ? "Bottom" :
363 soap_add(body
, "Pinboard",
364 "Name", VALUE
, NULL
, NULL
);
373 type
= type_get_type(VALUE
);
374 printf("%s/%s\n", type
->media_type
,
379 client_id
= g_strdup(VALUE
);
382 soap_rpc
= xmlParseFile("-");
384 g_error("Invalid XML in RPC");
392 tooltips
= gtk_tooltips_new();
394 if (euid
== 0 || show_user
)
395 show_user_message
= g_strdup_printf(_("Running as user '%s'"),
398 /* Add each remaining (non-option) argument to the list of files
404 tmp
= pathdup(argv
[i
++]);
406 soap_add(body
, "Run", "Filename", tmp
, NULL
, NULL
);
413 if (body
->xmlChildrenNode
)
414 g_error("Can't use -R with other options - sorry!");
419 else if (!body
->xmlChildrenNode
)
421 /* The user didn't request any action. Open the current
426 dir
= g_get_current_dir();
427 soap_add(body
, "OpenDir", "Filename", dir
, NULL
, NULL
);
431 option_add_int(&o_dnd_no_hostnames
, "dnd_no_hostnames", 1);
433 /* Try to send the request to an already-running copy of the filer */
435 if (remote_init(rpc
, new_copy
))
436 return EXIT_SUCCESS
; /* It worked - exit */
438 /* Put ourselves into the background (so 'rox' always works the
439 * same, whether we're already running or not).
440 * Not for -n, though (helps when debugging).
448 _exit(0); /* Parent exits */
449 /* Otherwise we're the child (or an error occurred - ignore
454 /* Close stdin. We don't need it, and it can cause problems if
455 * a child process wants a password, etc...
459 fd
= open("/dev/null", O_RDONLY
);
468 /* Initialize the rest of the filer... */
488 /* Let everyone update */
491 /* When we get a signal, we can't do much right then. Instead,
492 * we send a char down this pipe, which causes the main loop to
493 * deal with the event next time we're idle.
496 close_on_exec(wakeup_pipe
[0], TRUE
);
497 close_on_exec(wakeup_pipe
[1], TRUE
);
498 gtk_input_add_full(wakeup_pipe
[0], GDK_INPUT_READ
, wake_up_cb
,
500 to_wakeup_pipe
= wakeup_pipe
[1];
502 /* If the pipe is full then we're going to get woken up anyway... */
503 set_blocking(to_wakeup_pipe
, FALSE
);
505 /* Let child processes die */
506 act
.sa_handler
= child_died
;
507 sigemptyset(&act
.sa_mask
);
508 act
.sa_flags
= SA_NOCLDSTOP
;
509 sigaction(SIGCHLD
, &act
, NULL
);
511 /* Ignore SIGPIPE - check for EPIPE errors instead */
512 act
.sa_handler
= SIG_IGN
;
513 sigemptyset(&act
.sa_mask
);
515 sigaction(SIGPIPE
, &act
, NULL
);
517 /* Set up session managament if available */
518 session_init(client_id
);
521 /* Finally, execute the request */
522 reply
= run_soap(rpc
);
526 /* Write the result, if any, to stdout */
527 save_xml_file(reply
, "-");
531 /* Enter the main loop, processing events until all our windows
534 if (number_of_windows
> 0)
540 /* Register a function to be called when process number 'child' dies. */
541 void on_child_death(gint child
, CallbackFn callback
, gpointer data
)
545 g_return_if_fail(callback
!= NULL
);
547 cb
= g_new(Callback
, 1);
549 cb
->callback
= callback
;
552 g_hash_table_insert(death_callbacks
, GINT_TO_POINTER(child
), cb
);
555 void one_less_window(void)
557 if (--number_of_windows
< 1)
561 /****************************************************************
562 * INTERNAL FUNCTIONS *
563 ****************************************************************/
565 static void show_features(void)
567 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
568 g_printerr("%s... %s\n", _("Large File Support"),
569 #ifdef LARGE_FILE_SUPPORT
576 g_printerr("%s... %s\n", _("GNOME-VFS library"),
577 # ifdef WITH_GNOMEVFS
580 _("No (gnome-vfs-config not found)")
586 static void soap_add(xmlNodePtr body
,
588 const xmlChar
*arg1_name
, const xmlChar
*arg1_value
,
589 const xmlChar
*arg2_name
, const xmlChar
*arg2_value
)
594 rox
= xmlSearchNsByHref(body
->doc
, body
, ROX_NS
);
596 node
= xmlNewChild(body
, rox
, function
, NULL
);
600 xmlNewTextChild(node
, rox
, arg1_name
, arg1_value
);
602 xmlNewTextChild(node
, rox
, arg2_name
, arg2_value
);
606 /* This is called as a signal handler; simply ensures that
607 * child_died_callback() will get called later.
609 static void child_died(int signum
)
611 child_died_flag
= TRUE
;
612 write(to_wakeup_pipe
, "\0", 1); /* Wake up! */
615 static void child_died_callback(void)
620 child_died_flag
= FALSE
;
622 /* Find out which children exited and allow them to die */
627 child
= waitpid(-1, &status
, WNOHANG
);
629 if (child
== 0 || child
== -1)
632 cb
= g_hash_table_lookup(death_callbacks
,
633 GINT_TO_POINTER(child
));
636 cb
->callback(cb
->data
);
637 g_hash_table_remove(death_callbacks
,
638 GINT_TO_POINTER(child
));
645 /* When data is written to_wakeup_pipe, this gets called from the event
646 * loop some time later. Useful for getting out of signal handlers, etc.
648 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
)
652 read(source
, buf
, BUFLEN
);
655 child_died_callback();