r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / run.c
blobea675cb4dd4bc7ef53053765f54ff1771ea0e1bd
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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)
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 /* run.c */
24 #include "config.h"
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/param.h>
30 #include "global.h"
32 #include "run.h"
33 #include "support.h"
34 #include "gui_support.h"
35 #include "filer.h"
36 #include "display.h"
37 #include "main.h"
38 #include "type.h"
39 #include "dir.h"
40 #include "diritem.h"
41 #include "action.h"
42 #include "icon.h"
44 /* Static prototypes */
45 static void write_data(gpointer data, gint fd, GdkInputCondition cond);
46 static gboolean follow_symlink(const char *full_path,
47 FilerWindow *filer_window,
48 FilerWindow *src_window);
49 static gboolean open_file(const guchar *path, MIME_type *type);
50 static void open_mountpoint(const guchar *full_path, DirItem *item,
51 FilerWindow *filer_window, FilerWindow *src_window,
52 gboolean edit);
54 typedef struct _PipedData PipedData;
56 struct _PipedData
58 guchar *data;
59 gint tag;
60 gulong sent;
61 gulong length;
65 /****************************************************************
66 * EXTERNAL INTERFACE *
67 ****************************************************************/
70 /* An application has been double-clicked (or run in some other way) */
71 void run_app(const char *path)
73 GString *apprun;
74 const char *argv[] = {NULL, NULL};
76 apprun = g_string_new(path);
77 argv[0] = g_string_append(apprun, "/AppRun")->str;
79 rox_spawn(home_dir, argv);
81 g_string_free(apprun, TRUE);
84 /* Execute this program, passing all the URIs in the list as arguments.
85 * URIs that are files on the local machine will be passed as simple
86 * pathnames. The uri_list should be freed after this function returns.
88 void run_with_files(const char *path, GList *uri_list)
90 const char **argv;
91 int argc = 0, i;
92 struct stat info;
94 if (stat(path, &info))
96 delayed_error(_("Program %s not found - deleted?"), path);
97 return;
100 argv = g_malloc(sizeof(char *) * (g_list_length(uri_list) + 2));
102 if (S_ISDIR(info.st_mode))
103 argv[argc++] = make_path(path, "AppRun");
104 else
105 argv[argc++] = path;
107 while (uri_list)
109 const EscapedPath *uri = uri_list->data;
110 char *local;
112 local = get_local_path(uri);
113 if (local)
114 argv[argc++] = local;
115 else
116 argv[argc++] = unescape_uri(uri);
117 uri_list = uri_list->next;
120 argv[argc++] = NULL;
122 rox_spawn(home_dir, argv);
124 for (i = 1; i < argc; i++)
125 g_free((gchar *) argv[i]);
126 g_free(argv);
129 /* Run the program as '<path> -', piping the data to it via stdin.
130 * You can g_free() the data as soon as this returns.
132 void run_with_data(const char *path, gpointer data, gulong length)
134 const char *argv[] = {NULL, "-", NULL};
135 struct stat info;
136 int fds[2];
137 PipedData *pd;
139 if (stat(path, &info))
141 delayed_error(_("Program %s not found - deleted?"), path);
142 return;
145 if (S_ISDIR(info.st_mode))
146 argv[0] = make_path(path, "AppRun");
147 else
148 argv[0] = path;
150 if (pipe(fds))
152 delayed_error("pipe: %s", g_strerror(errno));
153 return;
155 close_on_exec(fds[1], TRUE);
156 close_on_exec(fds[0], TRUE);
158 switch (fork())
160 case -1:
161 delayed_error("fork: %s", g_strerror(errno));
162 close(fds[1]);
163 break;
164 case 0:
165 /* We are the child */
166 chdir(home_dir);
167 if (dup2(fds[0], 0) == -1)
168 g_warning("dup2() failed: %s\n",
169 g_strerror(errno));
170 else
172 close_on_exec(0, FALSE);
173 if (execv(argv[0], (char **) argv))
174 g_warning("execv(%s) failed: %s\n",
175 argv[0], g_strerror(errno));
177 _exit(1);
178 default:
179 /* We are the parent */
180 set_blocking(fds[1], FALSE);
181 pd = g_new(PipedData, 1);
182 pd->data = g_malloc(length);
183 memcpy(pd->data, data, length);
184 pd->length = length;
185 pd->sent = 0;
186 pd->tag = gdk_input_add_full(fds[1], GDK_INPUT_WRITE,
187 write_data, pd, NULL);
188 break;
191 close(fds[0]);
194 /* Splits args into an argument vector, and runs the program. Must be
195 * executable.
197 void run_with_args(const char *path, DirItem *item, const char *args)
199 GError *error = NULL;
200 gchar **argv = NULL;
201 int n_args = 0;
203 if (item->base_type != TYPE_DIRECTORY && item->base_type != TYPE_FILE)
205 delayed_error("Arguments (%s) given for non-executable item %s",
206 args, path);
207 return;
210 if (!g_shell_parse_argv(args, &n_args, &argv, &error))
212 delayed_error("Failed to parse argument string '%s':\n%s",
213 args, error->message);
214 g_error_free(error);
215 return;
218 g_return_if_fail(argv != NULL);
219 g_return_if_fail(error == NULL);
221 argv = g_realloc(argv, (n_args + 2) * sizeof(gchar *));
222 memmove(argv + 1, argv, (n_args + 1) * sizeof(gchar *));
224 if (item->base_type == TYPE_DIRECTORY)
225 argv[0] = g_strconcat(path, "/AppRun", NULL);
226 else
227 argv[0] = g_strdup(path);
229 rox_spawn(home_dir, (const gchar **) argv);
231 g_strfreev(argv);
234 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
235 * edit a file, open an application, follow a symlink or mount a device.
237 * filer_window is the window to use for displaying a directory.
238 * NULL will always use a new directory when needed.
239 * src_window is the window to copy options from, or NULL.
241 * Returns TRUE on success.
243 gboolean run_diritem(const guchar *full_path,
244 DirItem *item,
245 FilerWindow *filer_window,
246 FilerWindow *src_window,
247 gboolean edit)
249 if (item->flags & ITEM_FLAG_SYMLINK && edit)
250 return follow_symlink(full_path, filer_window, src_window);
252 switch (item->base_type)
254 case TYPE_DIRECTORY:
255 if (item->flags & ITEM_FLAG_APPDIR && !edit)
257 run_app(full_path);
258 return TRUE;
261 if (item->flags & ITEM_FLAG_MOUNT_POINT)
263 open_mountpoint(full_path, item,
264 filer_window, src_window, edit);
266 else if (filer_window)
267 filer_change_to(filer_window, full_path, NULL);
268 else
269 filer_opendir(full_path, src_window, NULL);
270 return TRUE;
271 case TYPE_FILE:
272 if (item->mime_type == application_executable && !edit)
274 const char *argv[] = {NULL, NULL};
275 guchar *dir = filer_window
276 ? filer_window->sym_path
277 : NULL;
279 argv[0] = full_path;
281 return rox_spawn(dir, argv) != 0;
284 return open_file(full_path, edit ? text_plain
285 : item->mime_type);
286 case TYPE_ERROR:
287 delayed_error(_("File doesn't exist, or I can't "
288 "access it: %s"), full_path);
289 return FALSE;
290 default:
291 delayed_error(
292 _("I don't know how to open '%s'"), full_path);
293 return FALSE;
297 /* Attempt to open this item */
298 gboolean run_by_path(const guchar *full_path)
300 gboolean retval;
301 DirItem *item;
303 /* XXX: Loads an image - wasteful */
304 item = diritem_new("");
305 diritem_restat(full_path, item, NULL);
306 retval = run_diritem(full_path, item, NULL, NULL, FALSE);
307 diritem_free(item);
309 return retval;
312 /* Open dir/Help, or show a message if missing */
313 void show_help_files(const char *dir)
315 const char *help_dir;
317 help_dir = make_path(dir, "Help");
319 if (file_exists(help_dir))
320 filer_opendir(help_dir, NULL, NULL);
321 else
322 info_message(
323 _("Application:\n"
324 "This is an application directory - you can "
325 "run it as a program, or open it (hold down "
326 "Shift while you open it). Most applications provide "
327 "their own help here, but this one doesn't."));
330 /* Open a directory viewer showing this file, and wink it */
331 void open_to_show(const guchar *path)
333 FilerWindow *new;
334 guchar *dir, *slash;
336 g_return_if_fail(path != NULL);
338 dir = g_strdup(path);
339 slash = strrchr(dir, '/');
340 if (slash == dir || !slash)
342 /* Item in the root (or root itself!) */
343 new = filer_opendir("/", NULL, NULL);
344 if (new && dir[1])
345 display_set_autoselect(new, dir + 1);
347 else
349 *slash = '\0';
350 new = filer_opendir(dir, NULL, NULL);
351 if (new)
353 if (slash[1] == '.')
354 display_set_hidden(new, TRUE);
355 display_set_autoselect(new, slash + 1);
359 g_free(dir);
362 /* Invoked using -x, this indicates that the filesystem has been modified
363 * and we should look at this item again.
365 void examine(const guchar *path)
367 struct stat info;
369 if (mc_stat(path, &info) != 0)
371 /* Deleted? Do a paranoid update of everything... */
372 filer_check_mounted(path);
374 else
376 /* Update directory containing this item... */
377 dir_check_this(path);
379 /* If this is itself a directory then rescan its contents... */
380 if (S_ISDIR(info.st_mode))
381 refresh_dirs(path);
383 /* If it's on the pinboard, update the icon... */
384 icons_may_update(path);
388 /****************************************************************
389 * INTERNAL FUNCTIONS *
390 ****************************************************************/
393 static void write_data(gpointer data, gint fd, GdkInputCondition cond)
395 PipedData *pd = (PipedData *) data;
397 while (pd->sent < pd->length)
399 int sent;
401 sent = write(fd, pd->data + pd->sent, pd->length - pd->sent);
403 if (sent < 0)
405 if (errno == EAGAIN)
406 return;
407 delayed_error(_("Could not send data to program: %s"),
408 g_strerror(errno));
409 goto finish;
412 pd->sent += sent;
415 finish:
416 g_source_remove(pd->tag);
417 g_free(pd->data);
418 g_free(pd);
419 close(fd);
422 /* Follow the link 'full_path' and display it in filer_window, or a
423 * new window if that is NULL.
425 static gboolean follow_symlink(const char *full_path,
426 FilerWindow *filer_window,
427 FilerWindow *src_window)
429 char *real, *slash;
430 char *new_dir;
431 char path[MAXPATHLEN + 1];
432 int got;
434 got = readlink(full_path, path, MAXPATHLEN);
435 if (got < 0)
437 delayed_error(_("Could not read link: %s"),
438 g_strerror(errno));
439 return FALSE;
442 g_return_val_if_fail(got <= MAXPATHLEN, FALSE);
443 path[got] = '\0';
445 /* Make a relative path absolute */
446 if (path[0] != '/')
448 guchar *tmp;
449 slash = strrchr(full_path, '/');
450 g_return_val_if_fail(slash != NULL, FALSE);
452 tmp = g_strndup(full_path, slash - full_path);
453 real = pathdup(make_path(tmp, path));
454 /* NB: full_path may be invalid here... */
455 g_free(tmp);
457 else
458 real = pathdup(path);
460 slash = strrchr(real, '/');
461 if (!slash)
463 g_free(real);
464 delayed_error(
465 _("Broken symlink (or you don't have permission "
466 "to follow it): %s"), full_path);
467 return FALSE;
470 *slash = '\0';
472 if (*real)
473 new_dir = real;
474 else
475 new_dir = "/";
477 if (filer_window)
478 filer_change_to(filer_window, new_dir, slash + 1);
479 else
481 FilerWindow *new;
483 new = filer_opendir(new_dir, src_window, NULL);
484 if (new)
485 display_set_autoselect(new, slash + 1);
488 g_free(real);
490 return TRUE;
493 /* Load this file into an appropriate editor */
494 static gboolean open_file(const guchar *path, MIME_type *type)
496 g_return_val_if_fail(type != NULL, FALSE);
498 if (type_open(path, type))
499 return TRUE;
501 report_error(
502 _("No run action specified for files of this type (%s/%s) - "
503 "you can set a run action by choosing `Set Run Action' "
504 "from the File menu, or you can just drag the file to an "
505 "application"),
506 type->media_type,
507 type->subtype);
509 return FALSE;
512 /* Called like run_diritem, when a mount-point is opened */
513 static void open_mountpoint(const guchar *full_path, DirItem *item,
514 FilerWindow *filer_window, FilerWindow *src_window,
515 gboolean edit)
517 gboolean mounted = (item->flags & ITEM_FLAG_MOUNTED) != 0;
519 if (mounted == edit)
521 GList *paths;
523 paths = g_list_prepend(NULL, (gpointer) full_path);
524 action_mount(paths, filer_window == NULL, -1);
525 g_list_free(paths);
526 if (filer_window && !mounted)
527 filer_change_to(filer_window, full_path, NULL);
529 else
531 if (filer_window)
532 filer_change_to(filer_window, full_path, NULL);
533 else
534 filer_opendir(full_path, src_window, NULL);