4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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 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) 2003 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 new copy; for debugging the filer\n" \
122 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
123 " -r, --right=PANEL open PAN as a right-edge panel\n" \
124 " -R, --RPC invoke method call read from stdin\n" \
125 " -s, --show=FILE open a directory showing FILE\n" \
126 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
127 " -u, --user show user name in each window \n" \
128 " -v, --version display the version information and exit\n" \
129 " -x, --examine=FILE FILE has changed - re-examine it\n" \
130 "\nReport bugs to <tal197@users.sourceforge.net>.\n" \
131 "Home page (including updated versions): http://rox.sourceforge.net/\n")
133 #define SHORT_OPS "c:d:t:b:l:r:op:s:hvnux:m:D:R"
135 #ifdef HAVE_GETOPT_LONG
136 static struct option long_opts
[] =
138 {"dir", 1, NULL
, 'd'},
139 {"top", 1, NULL
, 't'},
140 {"bottom", 1, NULL
, 'b'},
141 {"left", 1, NULL
, 'l'},
142 {"override", 0, NULL
, 'o'},
143 {"pinboard", 1, NULL
, 'p'},
144 {"right", 1, NULL
, 'r'},
145 {"help", 0, NULL
, 'h'},
146 {"version", 0, NULL
, 'v'},
147 {"user", 0, NULL
, 'u'},
148 {"new", 0, NULL
, 'n'},
149 {"RPC", 0, NULL
, 'R'},
150 {"show", 1, NULL
, 's'},
151 {"examine", 1, NULL
, 'x'},
152 {"close", 1, NULL
, 'D'},
153 {"mime-type", 1, NULL
, 'm'},
154 {"client-id", 1, NULL
, 'c'},
159 /* Take control of panels away from WM? */
160 Option o_override_redirect
;
162 /* Always start a new filer, even if one seems to be already running */
163 gboolean new_copy
= FALSE
;
165 /* Maps child PIDs to Callback pointers */
166 static GHashTable
*death_callbacks
= NULL
;
167 static gboolean child_died_flag
= FALSE
;
169 Option o_dnd_no_hostnames
;
171 /* Static prototypes */
172 static void show_features(void);
173 static void soap_add(xmlNodePtr body
,
175 const xmlChar
*arg1_name
, const xmlChar
*arg1_value
,
176 const xmlChar
*arg2_name
, const xmlChar
*arg2_value
);
177 static void child_died(int signum
);
178 static void child_died_callback(void);
179 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
);
181 /****************************************************************
182 * EXTERNAL INTERFACE *
183 ****************************************************************/
185 /* The value that goes with an option */
186 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
188 /* Parses the command-line to work out what the user wants to do.
189 * Tries to send the request to an already-running copy of the filer.
190 * If that fails, it initialises all the other modules and executes the
193 int main(int argc
, char **argv
)
197 struct sigaction act
;
199 gchar
*client_id
= NULL
;
200 gboolean show_user
= FALSE
;
201 xmlDocPtr rpc
, soap_rpc
= NULL
, reply
;
204 home_dir
= g_get_home_dir();
205 home_dir_len
= strlen(home_dir
);
206 app_dir
= g_strdup(getenv("APP_DIR"));
208 /* Get internationalisation up and running. This requires the
209 * choices system, to discover the user's preferred language.
217 g_warning("APP_DIR environment variable was unset!\n"
218 "Use the AppRun script to invoke ROX-Filer...\n");
219 app_dir
= g_get_current_dir();
224 /* Don't pass it on to our child processes... */
229 /* Sometimes we want to take special action when a child
230 * process exits. This hash table is used to convert the
231 * child's PID to the callback function.
233 death_callbacks
= g_hash_table_new(NULL
, NULL
);
235 /* Find out some information about ourself */
238 ngroups
= getgroups(0, NULL
);
241 else if (ngroups
> 0)
243 supplemental_groups
= g_malloc(sizeof(gid_t
) * ngroups
);
244 getgroups(ngroups
, supplemental_groups
);
247 if (argc
== 2 && strcmp(argv
[1], "-v") == 0)
249 /* This is used by install.sh to test if the filer
250 * compiled OK. Do this test before gtk_init so that
251 * we don't need an X server to install.
253 g_print("ROX-Filer %s\n", VERSION
);
259 option_add_int(&o_override_redirect
, "override_redirect", FALSE
);
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 info_message(_("The -o argument is no longer "
310 "used. You can turn on override "
311 "redirect from the Options box "
315 g_print("ROX-Filer %s\n", VERSION
);
316 g_print("%s", _(COPYING
));
320 g_print("%s", _(HELP
));
321 g_print("%s", _(SHORT_ONLY_WARNING
));
326 /* Argument is a path */
327 if (c
== 'd' && VALUE
[0] == '/')
328 tmp
= g_strdup(VALUE
);
330 tmp
= pathdup(VALUE
);
332 c
== 'D' ? "CloseDir" :
333 c
== 'd' ? "OpenDir" :
334 c
== 'x' ? "Examine" : "Unknown",
340 tmp
= g_path_get_dirname(VALUE
);
347 soap_add(body
, "Show",
348 "Directory", dir
? dir
: tmp
,
349 "Leafname", g_basename(VALUE
));
357 /* Argument is a leaf (or starts with /) */
358 soap_add(body
, "Panel", "Name", VALUE
,
359 "Side", c
== 'l' ? "Left" :
362 c
== 'b' ? "Bottom" :
366 soap_add(body
, "Pinboard",
367 "Name", VALUE
, NULL
, NULL
);
376 type
= type_get_type(VALUE
);
377 printf("%s/%s\n", type
->media_type
,
382 client_id
= g_strdup(VALUE
);
385 soap_rpc
= xmlParseFile("-");
387 g_error("Invalid XML in RPC");
395 tooltips
= gtk_tooltips_new();
397 if (euid
== 0 || show_user
)
398 show_user_message
= g_strdup_printf(_("Running as user '%s'"),
401 /* Add each remaining (non-option) argument to the list of files
407 tmp
= pathdup(argv
[i
++]);
409 soap_add(body
, "Run", "Filename", tmp
, NULL
, NULL
);
416 if (body
->xmlChildrenNode
)
417 g_error("Can't use -R with other options - sorry!");
422 else if (!body
->xmlChildrenNode
)
424 /* The user didn't request any action. Open the current
429 dir
= g_get_current_dir();
430 soap_add(body
, "OpenDir", "Filename", dir
, NULL
, NULL
);
434 option_add_int(&o_dnd_no_hostnames
, "dnd_no_hostnames", 1);
436 /* Try to send the request to an already-running copy of the filer */
438 if (remote_init(rpc
, new_copy
))
439 return EXIT_SUCCESS
; /* It worked - exit */
441 /* Put ourselves into the background (so 'rox' always works the
442 * same, whether we're already running or not).
443 * Not for -n, though (helps when debugging).
451 _exit(0); /* Parent exits */
452 /* Otherwise we're the child (or an error occurred - ignore
457 /* Close stdin. We don't need it, and it can cause problems if
458 * a child process wants a password, etc...
462 fd
= open("/dev/null", O_RDONLY
);
471 /* Initialize the rest of the filer... */
491 /* Let everyone update */
494 /* When we get a signal, we can't do much right then. Instead,
495 * we send a char down this pipe, which causes the main loop to
496 * deal with the event next time we're idle.
499 close_on_exec(wakeup_pipe
[0], TRUE
);
500 close_on_exec(wakeup_pipe
[1], TRUE
);
501 gtk_input_add_full(wakeup_pipe
[0], GDK_INPUT_READ
, wake_up_cb
,
503 to_wakeup_pipe
= wakeup_pipe
[1];
505 /* If the pipe is full then we're going to get woken up anyway... */
506 set_blocking(to_wakeup_pipe
, FALSE
);
508 /* Let child processes die */
509 act
.sa_handler
= child_died
;
510 sigemptyset(&act
.sa_mask
);
511 act
.sa_flags
= SA_NOCLDSTOP
;
512 sigaction(SIGCHLD
, &act
, NULL
);
514 /* Ignore SIGPIPE - check for EPIPE errors instead */
515 act
.sa_handler
= SIG_IGN
;
516 sigemptyset(&act
.sa_mask
);
518 sigaction(SIGPIPE
, &act
, NULL
);
520 /* Set up session managament if available */
521 session_init(client_id
);
524 /* Finally, execute the request */
525 reply
= run_soap(rpc
);
529 /* Write the result, if any, to stdout */
530 save_xml_file(reply
, "-");
534 /* Enter the main loop, processing events until all our windows
537 if (number_of_windows
> 0)
543 /* Register a function to be called when process number 'child' dies. */
544 void on_child_death(gint child
, CallbackFn callback
, gpointer data
)
548 g_return_if_fail(callback
!= NULL
);
550 cb
= g_new(Callback
, 1);
552 cb
->callback
= callback
;
555 g_hash_table_insert(death_callbacks
, GINT_TO_POINTER(child
), cb
);
558 void one_less_window(void)
560 if (--number_of_windows
< 1)
564 /****************************************************************
565 * INTERNAL FUNCTIONS *
566 ****************************************************************/
568 static void show_features(void)
571 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION
);
572 g_print(_("Running with GTK version %d.%d.%d\n"),
576 g_print("\n-- %s --\n\n", _("features set at compile time"));
577 g_print("%s... %s\n", _("Large File Support"),
578 #ifdef LARGE_FILE_SUPPORT
585 g_print("%s... %s\n", _("GNOME-VFS library"),
586 # ifdef WITH_GNOMEVFS
589 _("No (gnome-vfs-config not found)")
593 g_print("%s... %s\n", _("Dnotify support"),
602 static void soap_add(xmlNodePtr body
,
604 const xmlChar
*arg1_name
, const xmlChar
*arg1_value
,
605 const xmlChar
*arg2_name
, const xmlChar
*arg2_value
)
610 rox
= xmlSearchNsByHref(body
->doc
, body
, ROX_NS
);
612 node
= xmlNewChild(body
, rox
, function
, NULL
);
616 xmlNewTextChild(node
, rox
, arg1_name
, arg1_value
);
618 xmlNewTextChild(node
, rox
, arg2_name
, arg2_value
);
622 /* This is called as a signal handler; simply ensures that
623 * child_died_callback() will get called later.
625 static void child_died(int signum
)
627 child_died_flag
= TRUE
;
628 write(to_wakeup_pipe
, "\0", 1); /* Wake up! */
631 static void child_died_callback(void)
636 child_died_flag
= FALSE
;
638 /* Find out which children exited and allow them to die */
643 child
= waitpid(-1, &status
, WNOHANG
);
645 if (child
== 0 || child
== -1)
648 cb
= g_hash_table_lookup(death_callbacks
,
649 GINT_TO_POINTER(child
));
652 cb
->callback(cb
->data
);
653 g_hash_table_remove(death_callbacks
,
654 GINT_TO_POINTER(child
));
661 /* When data is written to_wakeup_pipe, this gets called from the event
662 * loop some time later. Useful for getting out of signal handlers, etc.
664 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
)
668 read(source
, buf
, BUFLEN
);
671 child_died_callback();