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"
74 #include "minibuffer.h"
76 int number_of_windows
= 0; /* Quit when this reaches 0 again... */
77 static int to_wakeup_pipe
= -1; /* Write here to get noticed */
79 /* Information about the ROX-Filer process */
82 int ngroups
; /* Number of supplemental groups */
83 gid_t
*supplemental_groups
= NULL
;
85 /* Message to display at the top of each filer window */
86 const gchar
*show_user_message
= NULL
;
89 const char *home_dir
, *app_dir
;
91 GtkTooltips
*tooltips
= NULL
;
94 N_("Copyright (C) 2002 Thomas Leonard.\n" \
95 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
96 "to the extent permitted by law.\n" \
97 "You may redistribute copies of ROX-Filer\n" \
98 "under the terms of the GNU General Public License.\n" \
99 "For more information about these matters, " \
100 "see the file named COPYING.\n")
102 #ifdef HAVE_GETOPT_LONG
103 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
104 # define SHORT_ONLY_WARNING ""
106 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
107 # define SHORT_ONLY_WARNING \
108 _("NOTE: Your system does not support long options - \n" \
109 "you must use the short versions instead.\n\n")
112 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
113 "Open each directory or file listed, or the current working\n" \
114 "directory if no arguments are given.\n\n" \
115 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
116 " -c, --client-id=ID used for session management\n" \
117 " -d, --dir=DIR open DIR as directory (not application)\n" \
118 " -D, --close=DIR close DIR and its subdirectories\n" \
119 " -h, --help display this help and exit\n" \
120 " -l, --left=PANEL open PAN as a left-edge panel\n" \
121 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
122 " -n, --new start a new filer, even if already running\n" \
123 " -o, --override override window manager control of panels\n" \
124 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
125 " -r, --right=PANEL open PAN as a right-edge panel\n" \
126 " -R, --RPC invoke method call read from stdin\n" \
127 " -s, --show=FILE open a directory showing FILE\n" \
128 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
129 " -u, --user show user name in each window \n" \
130 " -v, --version display the version information and exit\n" \
131 " -x, --examine=FILE FILE has changed - re-examine it\n" \
132 "\nThe latest version can be found at:\n" \
133 "\thttp://rox.sourceforge.net\n" \
134 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
136 #define SHORT_OPS "d:t:b:l:r:op:s:hvnux:m:D:R"
138 #ifdef HAVE_GETOPT_LONG
139 static struct option long_opts
[] =
141 {"dir", 1, NULL
, 'd'},
142 {"top", 1, NULL
, 't'},
143 {"bottom", 1, NULL
, 'b'},
144 {"left", 1, NULL
, 'l'},
145 {"override", 0, NULL
, 'o'},
146 {"pinboard", 1, NULL
, 'p'},
147 {"right", 1, NULL
, 'r'},
148 {"help", 0, NULL
, 'h'},
149 {"version", 0, NULL
, 'v'},
150 {"user", 0, NULL
, 'u'},
151 {"new", 0, NULL
, 'n'},
152 {"RPC", 0, NULL
, 'R'},
153 {"show", 1, NULL
, 's'},
154 {"examine", 1, NULL
, 'x'},
155 {"close", 1, NULL
, 'D'},
156 {"mime-type", 1, NULL
, 'm'},
157 {"client-id", 1, NULL
, 'c'},
162 /* Take control of panels away from WM? */
163 gboolean override_redirect
= FALSE
;
165 /* Always start a new filer, even if one seems to be already running */
166 gboolean new_copy
= FALSE
;
168 /* Maps child PIDs to Callback pointers */
169 static GHashTable
*death_callbacks
= NULL
;
170 static gboolean child_died_flag
= FALSE
;
172 Option o_dnd_no_hostnames
;
174 /* Static prototypes */
175 static void show_features(void);
176 static void soap_add(xmlNodePtr body
,
178 xmlChar
*arg1_name
, xmlChar
*arg1_value
,
179 xmlChar
*arg2_name
, xmlChar
*arg2_value
);
180 static void child_died(int signum
);
181 static void child_died_callback(void);
182 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
);
184 /****************************************************************
185 * EXTERNAL INTERFACE *
186 ****************************************************************/
188 /* The value that goes with an option */
189 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
191 /* Parses the command-line to work out what the user wants to do.
192 * Tries to send the request to an already-running copy of the filer.
193 * If that fails, it initialises all the other modules and executes the
196 int main(int argc
, char **argv
)
200 struct sigaction act
;
201 guchar
*tmp
, *dir
, *slash
;
202 gchar
*client_id
= NULL
;
203 gboolean show_user
= FALSE
;
204 xmlDocPtr rpc
, soap_rpc
= NULL
, reply
;
207 home_dir
= g_get_home_dir();
208 home_dir_len
= strlen(home_dir
);
209 app_dir
= g_strdup(getenv("APP_DIR"));
211 /* Get internationalisation up and running. This requires the
212 * choices system, to discover the user's preferred language.
220 g_warning("APP_DIR environment variable was unset!\n"
221 "Use the AppRun script to invoke ROX-Filer...\n");
222 app_dir
= g_get_current_dir();
227 /* Don't pass it on to our child processes... */
232 /* Sometimes we want to take special action when a child
233 * process exits. This hash table is used to convert the
234 * child's PID to the callback function.
236 death_callbacks
= g_hash_table_new(NULL
, NULL
);
238 /* Find out some information about ourself */
241 ngroups
= getgroups(0, NULL
);
244 else if (ngroups
> 0)
246 supplemental_groups
= g_malloc(sizeof(gid_t
) * ngroups
);
247 getgroups(ngroups
, supplemental_groups
);
250 if (argc
== 2 && strcmp(argv
[1], "-v") == 0)
252 /* This is used by install.sh to test if the filer
253 * compiled OK. Do this test before gtk_init so that
254 * we don't need an X server to install.
256 fprintf(stderr
, "ROX-Filer %s\n", VERSION
);
257 fprintf(stderr
, _(COPYING
));
262 /* The idea here is to convert the command-line arguments
264 * We attempt to invoke the call on an already-running copy of
265 * the filer if possible, or execute it ourselves if not.
267 rpc
= soap_new(&body
);
269 /* Note: must do this before checking our options,
270 * otherwise we report an error for Gtk's options.
272 add_default_styles();
273 gtk_init(&argc
, &argv
);
275 /* Process each option in turn */
279 #ifdef HAVE_GETOPT_LONG
281 c
= getopt_long(argc
, argv
, SHORT_OPS
,
282 long_opts
, &long_index
);
284 c
= getopt(argc
, argv
, SHORT_OPS
);
288 break; /* No more options */
296 override_redirect
= TRUE
;
299 g_printerr("ROX-Filer %s\n", VERSION
);
300 g_printerr("%s", _(COPYING
));
304 g_printerr("%s", _(HELP
));
305 g_printerr("%s", _(SHORT_ONLY_WARNING
));
310 /* Argument is a path */
311 tmp
= pathdup(VALUE
);
313 c
== 'D' ? "CloseDir" :
314 c
== 'd' ? "OpenDir" :
315 c
== 'x' ? "Examine" : "Unknown",
321 tmp
= g_strdup(VALUE
);
322 slash
= strrchr(tmp
, '/');
335 soap_add(body
, "Show",
345 /* Argument is a leaf (or starts with /) */
346 soap_add(body
, "Panel", "Name", VALUE
,
347 "Side", c
== 'l' ? "Left" :
350 c
== 'b' ? "Bottom" :
354 soap_add(body
, "Pinboard",
355 "Name", VALUE
, NULL
, NULL
);
364 type
= type_get_type(VALUE
);
365 printf("%s/%s\n", type
->media_type
,
370 client_id
= g_strdup(VALUE
);
373 soap_rpc
= xmlParseFile("-");
375 g_error("Invalid XML in RPC");
383 tooltips
= gtk_tooltips_new();
385 if (euid
== 0 || show_user
)
386 show_user_message
= g_strdup_printf( _("Running as user '%s'"),
389 /* Add each remaining (non-option) argument to the list of files
395 tmp
= pathdup(argv
[i
++]);
397 soap_add(body
, "Run", "Filename", tmp
, NULL
, NULL
);
404 if (body
->xmlChildrenNode
)
405 g_error("Can't use -R with other options - sorry!");
410 else if (!body
->xmlChildrenNode
)
412 /* The user didn't request any action. Open the current
417 dir
= g_get_current_dir();
418 soap_add(body
, "OpenDir", "Filename", dir
, NULL
, NULL
);
422 option_add_int(&o_dnd_no_hostnames
, "dnd_no_hostnames", 1);
424 /* Try to send the request to an already-running copy of the filer */
426 if (remote_init(rpc
, new_copy
))
427 return EXIT_SUCCESS
; /* It worked - exit */
429 /* Put ourselves into the background (so 'rox' always works the
430 * same, whether we're already running or not).
431 * Not for -n, though (helps when debugging).
439 _exit(0); /* Parent exits */
440 /* Otherwise we're the child (or an error occurred - ignore
445 /* Close stdin. We don't need it, and it can cause problems if
446 * a child process wants a password, etc...
450 fd
= open("/dev/null", O_RDONLY
);
459 /* Initialize the rest of the filer... */
480 /* Let everyone update */
483 /* When we get a signal, we can't do much right then. Instead,
484 * we send a char down this pipe, which causes the main loop to
485 * deal with the event next time we're idle.
488 close_on_exec(wakeup_pipe
[0], TRUE
);
489 close_on_exec(wakeup_pipe
[1], TRUE
);
490 gtk_input_add_full(wakeup_pipe
[0], GDK_INPUT_READ
, wake_up_cb
,
492 to_wakeup_pipe
= wakeup_pipe
[1];
494 /* If the pipe is full then we're going to get woken up anyway... */
495 set_blocking(to_wakeup_pipe
, FALSE
);
497 /* Let child processes die */
498 act
.sa_handler
= child_died
;
499 sigemptyset(&act
.sa_mask
);
500 act
.sa_flags
= SA_NOCLDSTOP
;
501 sigaction(SIGCHLD
, &act
, NULL
);
503 /* Ignore SIGPIPE - check for EPIPE errors instead */
504 act
.sa_handler
= SIG_IGN
;
505 sigemptyset(&act
.sa_mask
);
507 sigaction(SIGPIPE
, &act
, NULL
);
509 /* Set up session managament if available */
510 session_init(client_id
);
513 /* Finally, execute the request */
514 reply
= run_soap(rpc
);
518 /* Write the result, if any, to stdout */
519 save_xml_file(reply
, "-");
523 /* Enter the main loop, processing events until all our windows
526 if (number_of_windows
> 0)
532 /* Register a function to be called when process number 'child' dies. */
533 void on_child_death(gint child
, CallbackFn callback
, gpointer data
)
537 g_return_if_fail(callback
!= NULL
);
539 cb
= g_new(Callback
, 1);
541 cb
->callback
= callback
;
544 g_hash_table_insert(death_callbacks
, GINT_TO_POINTER(child
), cb
);
547 /****************************************************************
548 * INTERNAL FUNCTIONS *
549 ****************************************************************/
551 static void show_features(void)
553 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
554 g_printerr("%s... %s\n", _("Large File Support"),
555 #ifdef LARGE_FILE_SUPPORT
561 g_printerr("%s... %s\n", _("GNOME-VFS library"),
565 _("No (gnome-vfs-config not found)")
570 static void soap_add(xmlNodePtr body
,
572 xmlChar
*arg1_name
, xmlChar
*arg1_value
,
573 xmlChar
*arg2_name
, xmlChar
*arg2_value
)
578 rox
= xmlSearchNsByHref(body
->doc
, body
, ROX_NS
);
580 node
= xmlNewChild(body
, rox
, function
, NULL
);
584 xmlNewTextChild(node
, rox
, arg1_name
, arg1_value
);
586 xmlNewTextChild(node
, rox
, arg2_name
, arg2_value
);
590 /* This is called as a signal handler; simply ensures that
591 * child_died_callback() will get called later.
593 static void child_died(int signum
)
595 child_died_flag
= TRUE
;
596 write(to_wakeup_pipe
, "\0", 1); /* Wake up! */
599 static void child_died_callback(void)
604 child_died_flag
= FALSE
;
606 /* Find out which children exited and allow them to die */
611 child
= waitpid(-1, &status
, WNOHANG
);
613 if (child
== 0 || child
== -1)
616 cb
= g_hash_table_lookup(death_callbacks
,
617 GINT_TO_POINTER(child
));
620 cb
->callback(cb
->data
);
621 g_hash_table_remove(death_callbacks
,
622 GINT_TO_POINTER(child
));
629 /* When data is written to_wakeup_pipe, this gets called from the event
630 * loop some time later. Useful for getting out of signal handlers, etc.
632 static void wake_up_cb(gpointer data
, gint source
, GdkInputCondition condition
)
636 read(source
, buf
, BUFLEN
);
639 child_died_callback();