r4299: Use a monospace font for the size and permissions columsn in the list view
[rox-filer/translations.git] / ROX-Filer / src / run.c
blob16030dd1f2058c4c2232813144af923545a0b63f
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);
53 static gboolean run_desktop(const char *full_path,
54 const char **args, const char *dir);
55 static gboolean type_open(const char *path, MIME_type *type);
57 typedef struct _PipedData PipedData;
59 struct _PipedData
61 guchar *data;
62 gint tag;
63 gulong sent;
64 gulong length;
68 /****************************************************************
69 * EXTERNAL INTERFACE *
70 ****************************************************************/
73 /* An application has been double-clicked (or run in some other way) */
74 void run_app(const char *path)
76 GString *apprun;
77 const char *argv[] = {NULL, NULL};
79 apprun = g_string_new(path);
80 argv[0] = g_string_append(apprun, "/AppRun")->str;
82 rox_spawn(home_dir, argv);
84 g_string_free(apprun, TRUE);
87 /* Execute this program, passing all the URIs in the list as arguments.
88 * URIs that are files on the local machine will be passed as simple
89 * pathnames. The uri_list should be freed after this function returns.
91 void run_with_files(const char *path, GList *uri_list)
93 const char **argv;
94 int argc = 0, i;
95 struct stat info;
96 MIME_type *type;
98 if (stat(path, &info))
100 delayed_error(_("Program %s not found - deleted?"), path);
101 return;
104 argv = g_malloc(sizeof(char *) * (g_list_length(uri_list) + 2));
106 if (S_ISDIR(info.st_mode))
107 argv[argc++] = make_path(path, "AppRun");
108 else
109 argv[argc++] = path;
111 while (uri_list)
113 const EscapedPath *uri = uri_list->data;
114 char *local;
116 local = get_local_path(uri);
117 if (local)
118 argv[argc++] = local;
119 else
120 argv[argc++] = unescape_uri(uri);
121 uri_list = uri_list->next;
124 argv[argc++] = NULL;
126 type = type_from_path(argv[0]);
127 if (type && type == application_x_desktop)
129 run_desktop(argv[0], argv + 1, home_dir);
131 else
133 rox_spawn(home_dir, argv);
136 for (i = 1; i < argc; i++)
137 g_free((gchar *) argv[i]);
138 g_free(argv);
141 /* Run the program as '<path> -', piping the data to it via stdin.
142 * You can g_free() the data as soon as this returns.
144 void run_with_data(const char *path, gpointer data, gulong length)
146 const char *argv[] = {NULL, "-", NULL};
147 struct stat info;
148 int fds[2];
149 PipedData *pd;
151 if (stat(path, &info))
153 delayed_error(_("Program %s not found - deleted?"), path);
154 return;
157 if (S_ISDIR(info.st_mode))
158 argv[0] = make_path(path, "AppRun");
159 else
160 argv[0] = path;
162 if (pipe(fds))
164 delayed_error("pipe: %s", g_strerror(errno));
165 return;
167 close_on_exec(fds[1], TRUE);
168 close_on_exec(fds[0], TRUE);
170 switch (fork())
172 case -1:
173 delayed_error("fork: %s", g_strerror(errno));
174 close(fds[1]);
175 break;
176 case 0:
177 /* We are the child */
178 chdir(home_dir);
179 if (dup2(fds[0], 0) == -1)
180 g_warning("dup2() failed: %s\n",
181 g_strerror(errno));
182 else
184 close_on_exec(0, FALSE);
185 if (execv(argv[0], (char **) argv))
186 g_warning("execv(%s) failed: %s\n",
187 argv[0], g_strerror(errno));
189 _exit(1);
190 default:
191 /* We are the parent */
192 set_blocking(fds[1], FALSE);
193 pd = g_new(PipedData, 1);
194 pd->data = g_malloc(length);
195 memcpy(pd->data, data, length);
196 pd->length = length;
197 pd->sent = 0;
198 pd->tag = gdk_input_add_full(fds[1], GDK_INPUT_WRITE,
199 write_data, pd, NULL);
200 break;
203 close(fds[0]);
206 /* Splits args into an argument vector, and runs the program. Must be
207 * executable.
209 void run_with_args(const char *path, DirItem *item, const char *args)
211 GError *error = NULL;
212 gchar **argv = NULL;
213 int n_args = 0;
215 if (item->base_type != TYPE_DIRECTORY && item->base_type != TYPE_FILE)
217 delayed_error("Arguments (%s) given for non-executable item %s",
218 args, path);
219 return;
222 if (!g_shell_parse_argv(args, &n_args, &argv, &error))
224 delayed_error("Failed to parse argument string '%s':\n%s",
225 args, error->message);
226 g_error_free(error);
227 return;
230 g_return_if_fail(argv != NULL);
231 g_return_if_fail(error == NULL);
233 argv = g_realloc(argv, (n_args + 2) * sizeof(gchar *));
234 memmove(argv + 1, argv, (n_args + 1) * sizeof(gchar *));
236 if (item->base_type == TYPE_DIRECTORY)
237 argv[0] = g_strconcat(path, "/AppRun", NULL);
238 else
239 argv[0] = g_strdup(path);
241 rox_spawn(home_dir, (const gchar **) argv);
243 g_strfreev(argv);
246 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
247 * edit a file, open an application, follow a symlink or mount a device.
249 * filer_window is the window to use for displaying a directory.
250 * NULL will always use a new directory when needed.
251 * src_window is the window to copy options from, or NULL.
253 * Returns TRUE on success.
255 gboolean run_diritem(const guchar *full_path,
256 DirItem *item,
257 FilerWindow *filer_window,
258 FilerWindow *src_window,
259 gboolean edit)
261 if (item->flags & ITEM_FLAG_SYMLINK && edit)
262 return follow_symlink(full_path, filer_window, src_window);
264 switch (item->base_type)
266 case TYPE_DIRECTORY:
267 if (item->flags & ITEM_FLAG_APPDIR && !edit)
269 run_app(full_path);
270 return TRUE;
273 if (item->flags & ITEM_FLAG_MOUNT_POINT)
275 open_mountpoint(full_path, item,
276 filer_window, src_window, edit);
278 else if (filer_window)
279 filer_change_to(filer_window, full_path, NULL);
280 else
281 filer_opendir(full_path, src_window, NULL);
282 return TRUE;
283 case TYPE_FILE:
284 if (EXECUTABLE_FILE(item) && !edit)
286 const char *argv[] = {NULL, NULL};
287 guchar *dir = filer_window
288 ? filer_window->sym_path
289 : NULL;
291 if (item->mime_type == application_x_desktop)
292 return run_desktop(full_path,
293 NULL, dir);
294 else
295 argv[0] = full_path;
297 return rox_spawn(dir, argv) != 0;
300 return open_file(full_path, edit ? text_plain
301 : item->mime_type);
302 case TYPE_ERROR:
303 delayed_error(_("File doesn't exist, or I can't "
304 "access it: %s"), full_path);
305 return FALSE;
306 default:
307 delayed_error(
308 _("I don't know how to open '%s'"), full_path);
309 return FALSE;
313 /* Attempt to open this item */
314 gboolean run_by_path(const guchar *full_path)
316 gboolean retval;
317 DirItem *item;
319 /* XXX: Loads an image - wasteful */
320 item = diritem_new("");
321 diritem_restat(full_path, item, NULL);
322 retval = run_diritem(full_path, item, NULL, NULL, FALSE);
323 diritem_free(item);
325 return retval;
328 /* Open dir/Help, or show a message if missing */
329 void show_help_files(const char *dir)
331 const char *help_dir;
333 help_dir = make_path(dir, "Help");
335 if (file_exists(help_dir))
336 filer_opendir(help_dir, NULL, NULL);
337 else
338 info_message(
339 _("Application:\n"
340 "This is an application directory - you can "
341 "run it as a program, or open it (hold down "
342 "Shift while you open it). Most applications provide "
343 "their own help here, but this one doesn't."));
346 /* Open a directory viewer showing this file, and wink it */
347 void open_to_show(const guchar *path)
349 FilerWindow *new;
350 guchar *dir, *slash;
352 g_return_if_fail(path != NULL);
354 dir = g_strdup(path);
355 slash = strrchr(dir, '/');
356 if (slash == dir || !slash)
358 /* Item in the root (or root itself!) */
359 new = filer_opendir("/", NULL, NULL);
360 if (new && dir[1])
361 display_set_autoselect(new, dir + 1);
363 else
365 *slash = '\0';
366 new = filer_opendir(dir, NULL, NULL);
367 if (new)
369 if (slash[1] == '.')
370 display_set_hidden(new, TRUE);
371 display_set_autoselect(new, slash + 1);
375 g_free(dir);
378 /* Invoked using -x, this indicates that the filesystem has been modified
379 * and we should look at this item again.
381 void examine(const guchar *path)
383 struct stat info;
385 if (mc_stat(path, &info) != 0)
387 /* Deleted? Do a paranoid update of everything... */
388 filer_check_mounted(path);
390 else
392 /* Update directory containing this item... */
393 dir_check_this(path);
395 /* If this is itself a directory then rescan its contents... */
396 if (S_ISDIR(info.st_mode))
397 refresh_dirs(path);
399 /* If it's on the pinboard, update the icon... */
400 icons_may_update(path);
404 /****************************************************************
405 * INTERNAL FUNCTIONS *
406 ****************************************************************/
409 static void write_data(gpointer data, gint fd, GdkInputCondition cond)
411 PipedData *pd = (PipedData *) data;
413 while (pd->sent < pd->length)
415 int sent;
417 sent = write(fd, pd->data + pd->sent, pd->length - pd->sent);
419 if (sent < 0)
421 if (errno == EAGAIN)
422 return;
423 delayed_error(_("Could not send data to program: %s"),
424 g_strerror(errno));
425 goto finish;
428 pd->sent += sent;
431 finish:
432 g_source_remove(pd->tag);
433 g_free(pd->data);
434 g_free(pd);
435 close(fd);
438 /* Follow the link 'full_path' and display it in filer_window, or a
439 * new window if that is NULL.
441 static gboolean follow_symlink(const char *full_path,
442 FilerWindow *filer_window,
443 FilerWindow *src_window)
445 char *real, *slash;
446 char *new_dir;
447 char path[MAXPATHLEN + 1];
448 int got;
450 got = readlink(full_path, path, MAXPATHLEN);
451 if (got < 0)
453 delayed_error(_("Could not read link: %s"),
454 g_strerror(errno));
455 return FALSE;
458 g_return_val_if_fail(got <= MAXPATHLEN, FALSE);
459 path[got] = '\0';
461 /* Make a relative path absolute */
462 if (path[0] != '/')
464 guchar *tmp;
465 slash = strrchr(full_path, '/');
466 g_return_val_if_fail(slash != NULL, FALSE);
468 tmp = g_strndup(full_path, slash - full_path);
469 real = pathdup(make_path(tmp, path));
470 /* NB: full_path may be invalid here... */
471 g_free(tmp);
473 else
474 real = pathdup(path);
476 slash = strrchr(real, '/');
477 if (!slash)
479 g_free(real);
480 delayed_error(
481 _("Broken symlink (or you don't have permission "
482 "to follow it): %s"), full_path);
483 return FALSE;
486 *slash = '\0';
488 if (*real)
489 new_dir = real;
490 else
491 new_dir = "/";
493 if (filer_window)
494 filer_change_to(filer_window, new_dir, slash + 1);
495 else
497 FilerWindow *new;
499 new = filer_opendir(new_dir, src_window, NULL);
500 if (new)
501 display_set_autoselect(new, slash + 1);
504 g_free(real);
506 return TRUE;
509 /* Load this file into an appropriate editor */
510 static gboolean open_file(const guchar *path, MIME_type *type)
512 g_return_val_if_fail(type != NULL, FALSE);
514 if (type_open(path, type))
515 return TRUE;
517 report_error(
518 _("No run action specified for files of this type (%s/%s) - "
519 "you can set a run action by choosing `Set Run Action' "
520 "from the File menu, or you can just drag the file to an "
521 "application.%s"),
522 type->media_type,
523 type->subtype,
524 type->executable ? _("\n\nNote: If this is a computer program which "
525 "you want to run, you need to set the execute bit "
526 "by choosing Permissions from the File menu.")
527 : "");
529 return FALSE;
532 /* Called like run_diritem, when a mount-point is opened */
533 static void open_mountpoint(const guchar *full_path, DirItem *item,
534 FilerWindow *filer_window, FilerWindow *src_window,
535 gboolean edit)
537 gboolean mounted = (item->flags & ITEM_FLAG_MOUNTED) != 0;
539 if (mounted == edit)
541 GList *paths;
543 paths = g_list_prepend(NULL, (gpointer) full_path);
544 action_mount(paths, filer_window == NULL, !mounted, -1);
545 g_list_free(paths);
546 if (filer_window && !mounted)
547 filer_change_to(filer_window, full_path, NULL);
549 else
551 if (filer_window)
552 filer_change_to(filer_window, full_path, NULL);
553 else
554 filer_opendir(full_path, src_window, NULL);
558 /* full_path is a .desktop file. Execute the application, using the Exec line
559 * from the file.
560 * Returns TRUE on success.
562 static gboolean run_desktop(const char *full_path,
563 const char **args,
564 const char *dir)
566 GError *error = NULL;
567 char *exec = NULL;
568 gint argc = 0;
569 gchar **argv = NULL;
570 GPtrArray *expanded = NULL;
571 int i;
572 gboolean success = FALSE;
574 exec = get_value_from_desktop_file(full_path, "Desktop Entry", "Exec",
575 &error);
576 if (error)
578 delayed_error("Failed to parse .desktop file '%s':\n%s",
579 full_path, error->message);
580 goto err;
583 if (!exec)
585 delayed_error("Can't find Exec command in .desktop file '%s'",
586 full_path);
587 goto err;
590 if (!g_shell_parse_argv(exec, &argc, &argv, &error))
592 delayed_error("Failed to parse '%s' from '%s':\n%s",
593 exec, full_path, error->message);
594 goto err;
597 expanded = g_ptr_array_new();
598 for (i = 0; i < argc; i++)
600 const char *src = argv[i];
602 if (src[0] == '%' && src[1] != '\0' && src[2] == '\0')
604 /* We should treat these four differently. */
605 if (src[1] == 'f' || src[1] == 'F' ||
606 src[1] == 'u' || src[1] == 'U')
608 int j;
609 for (j = 0; args && args[j]; j++)
610 g_ptr_array_add(expanded, g_strdup(args[j]));
612 else
614 delayed_error("Unsupported escape character in '%s' in '%s'",
615 exec, full_path);
616 goto err;
619 else
621 g_ptr_array_add(expanded, g_strdup(src));
624 g_ptr_array_add(expanded, NULL);
626 success = rox_spawn(dir, (const gchar **) expanded->pdata);
627 err:
628 if (error != NULL)
629 g_error_free(error);
630 if (exec != NULL)
631 g_free(exec);
632 if (argv != NULL)
633 g_strfreev(argv);
634 if (expanded != NULL)
636 g_ptr_array_foreach(expanded, (GFunc) g_free, NULL);
637 g_ptr_array_free(expanded, TRUE);
640 return success;
643 /* Returns FALSE is no run action is set for this type. */
644 static gboolean type_open(const char *path, MIME_type *type)
646 gchar *argv[] = {NULL, NULL, NULL};
647 char *open;
648 struct stat info;
650 argv[1] = (char *) path;
652 open = handler_for(type);
653 if (!open)
654 return FALSE;
656 if (stat(open, &info))
658 report_error("stat(%s): %s", open, g_strerror(errno));
659 g_free(open);
660 return TRUE;
663 if (info.st_mode & S_IWOTH)
665 gchar *choices_dir;
666 GList *paths;
668 report_error(_("Executable '%s' is world-writeable! Refusing "
669 "to run. Please change the permissions now (this "
670 "problem may have been caused by a bug in earlier "
671 "versions of the filer).\n\n"
672 "Having (non-symlink) run actions world-writeable "
673 "means that other people who use your computer can "
674 "replace your run actions with malicious versions.\n\n"
675 "If you trust everyone who could write to these files "
676 "then you needn't worry. Otherwise, you should check, "
677 "or even just delete, all the existing run actions."),
678 open);
679 choices_dir = g_path_get_dirname(open);
680 paths = g_list_append(NULL, choices_dir);
681 action_chmod(paths, TRUE, _("go-w (Fix security problem)"));
682 g_free(choices_dir);
683 g_list_free(paths);
684 g_free(open);
685 return TRUE;
688 if (S_ISDIR(info.st_mode))
690 argv[0] = g_strconcat(open, "/AppRun", NULL);
691 rox_spawn(home_dir, (const gchar **) argv) != 0;
693 else if (type_get_type(open) == application_x_desktop)
695 argv[0] = open;
696 run_desktop(open, (const char **) (argv + 1), home_dir);
698 else
700 argv[0] = open;
701 rox_spawn(home_dir, (const gchar **) argv) != 0;
704 if (argv[0] != open)
705 g_free(argv[0]);
707 g_free(open);
709 return TRUE;