r898: Applied Bernard Jungen's latest patch:
[rox-filer.git] / ROX-Filer / src / run.c
blob7b4413bdbc6a1885c638c0ae635d8cb3ab54d75d
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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 "main.h"
37 #include "type.h"
38 #include "dir.h"
39 #include "action.h"
40 #include "icon.h"
42 /* Static prototypes */
43 static void write_data(gpointer data, gint fd, GdkInputCondition cond);
44 static gboolean follow_symlink(char *full_path,
45 FilerWindow *filer_window,
46 FilerWindow *src_window);
47 static gboolean open_file(guchar *path, MIME_type *type);
48 static void dir_show_help(DirItem *item, char *path);
50 typedef struct _PipedData PipedData;
52 struct _PipedData
54 guchar *data;
55 gint tag;
56 gulong sent;
57 gulong length;
61 /****************************************************************
62 * EXTERNAL INTERFACE *
63 ****************************************************************/
66 /* An application has been double-clicked (or run in some other way) */
67 void run_app(char *path)
69 GString *apprun;
70 char *argv[] = {NULL, NULL};
72 apprun = g_string_new(path);
73 argv[0] = g_string_append(apprun, "/AppRun")->str;
75 if (!spawn_full(argv, home_dir))
76 report_rox_error(_("Failed to fork() child process"));
78 g_string_free(apprun, TRUE);
81 /* Execute this program, passing all the URIs in the list as arguments.
82 * URIs that are files on the local machine will be passed as simple
83 * pathnames. The uri_list should be freed after this function returns.
85 void run_with_files(char *path, GList *uri_list)
87 char **argv;
88 int argc = 0;
89 struct stat info;
91 if (stat(path, &info))
93 delayed_rox_error(_("Program %s not found - deleted?"), path);
94 return;
97 argv = g_malloc(sizeof(char *) * (g_list_length(uri_list) + 2));
99 if (S_ISDIR(info.st_mode))
100 argv[argc++] = make_path(path, "AppRun")->str;
101 else
102 argv[argc++] = path;
104 while (uri_list)
106 char *uri = (char *) uri_list->data;
107 char *local;
109 local = get_local_path(uri);
110 if (local)
111 argv[argc++] = local;
112 else
113 argv[argc++] = uri;
114 uri_list = uri_list->next;
117 argv[argc++] = NULL;
119 if (!spawn_full(argv, home_dir))
120 delayed_rox_error(_("Failed to fork() child process"));
123 /* Run the program as '<path> -', piping the data to it via stdin.
124 * You can g_free() the data as soon as this returns.
126 void run_with_data(char *path, gpointer data, gulong length)
128 char *argv[] = {NULL, "-", NULL};
129 struct stat info;
130 int fds[2];
131 PipedData *pd;
133 if (stat(path, &info))
135 delayed_rox_error(_("Program %s not found - deleted?"), path);
136 return;
139 if (S_ISDIR(info.st_mode))
140 argv[0] = make_path(path, "AppRun")->str;
141 else
142 argv[0] = path;
144 if (pipe(fds))
146 delayed_rox_error("pipe: %s", g_strerror(errno));
147 return;
149 close_on_exec(fds[1], TRUE);
150 close_on_exec(fds[0], TRUE);
152 switch (fork())
154 case -1:
155 delayed_rox_error("fork: %s", g_strerror(errno));
156 close(fds[1]);
157 break;
158 case 0:
159 /* We are the child */
160 chdir(home_dir);
161 if (dup2(fds[0], 0) == -1)
162 g_warning("dup2() failed: %s\n",
163 g_strerror(errno));
164 else
166 close_on_exec(0, FALSE);
167 if (execv(argv[0], argv))
168 g_warning("execv(%s) failed: %s\n",
169 argv[0], g_strerror(errno));
171 _exit(1);
172 default:
173 /* We are the parent */
174 set_blocking(fds[1], FALSE);
175 pd = g_new(PipedData, 1);
176 pd->data = g_malloc(length);
177 memcpy(pd->data, data, length);
178 pd->length = length;
179 pd->sent = 0;
180 pd->tag = gdk_input_add(fds[1], GDK_INPUT_WRITE,
181 write_data, pd);
182 break;
185 close(fds[0]);
188 /* Load a file, open a directory or run an application. Or, if 'edit' is set:
189 * edit a file, open an application, follow a symlink or mount a device.
191 * filer_window is the window to use for displaying a directory.
192 * NULL will always use a new directory when needed.
193 * src_window is the window to copy options from, or NULL.
195 * Returns TRUE on success.
197 gboolean run_diritem(guchar *full_path,
198 DirItem *item,
199 FilerWindow *filer_window,
200 FilerWindow *src_window,
201 gboolean edit)
203 if (item->flags & ITEM_FLAG_SYMLINK && edit)
204 return follow_symlink(full_path, filer_window, src_window);
206 switch (item->base_type)
208 case TYPE_DIRECTORY:
209 if (item->flags & ITEM_FLAG_APPDIR && !edit)
211 run_app(full_path);
212 return TRUE;
215 if (item->flags & ITEM_FLAG_MOUNT_POINT && edit)
217 GList *paths;
219 paths = g_list_prepend(NULL, full_path);
220 action_mount(paths, filer_window == NULL);
221 g_list_free(paths);
222 if (item->flags & ITEM_FLAG_MOUNTED ||
223 !filer_window)
224 return TRUE;
227 if (filer_window)
228 filer_change_to(filer_window, full_path, NULL);
229 else
230 filer_opendir(full_path, src_window);
231 return TRUE;
232 case TYPE_FILE:
233 if ((item->mime_type == &special_exec) && !edit)
235 char *argv[] = {NULL, NULL};
236 guchar *dir = filer_window ? filer_window->path
237 : NULL;
239 argv[0] = full_path;
241 if (spawn_full(argv, dir))
242 return TRUE;
243 else
245 report_rox_error(
246 _("Failed to fork() child"));
247 return FALSE;
251 return open_file(full_path, edit ? &text_plain
252 : item->mime_type);
253 case TYPE_ERROR:
254 delayed_rox_error(_("File doesn't exist, or I can't "
255 "access it: %s"), full_path);
256 return FALSE;
257 default:
258 delayed_rox_error(
259 _("I don't know how to open '%s'"), full_path);
260 return FALSE;
264 /* Attempt to open this item */
265 gboolean run_by_path(guchar *full_path)
267 gboolean retval;
268 DirItem item;
270 /* XXX: Loads an image - wasteful */
271 diritem_stat(full_path, &item, FALSE);
272 retval = run_diritem(full_path, &item, NULL, NULL, FALSE);
273 diritem_clear(&item);
275 return retval;
278 void show_item_help(guchar *path, DirItem *item)
280 switch (item->base_type)
282 case TYPE_FILE:
283 if (item->flags & ITEM_FLAG_EXEC_FILE)
284 delayed_error(_("Executable file"),
285 _("This is a file with an eXecute bit "
286 "set - it can be run as a program."));
287 else
288 delayed_error(_("File"), _(
289 "This is a data file. Try using the "
290 "Info menu item to find out more..."));
291 break;
292 case TYPE_DIRECTORY:
293 if (item->flags & ITEM_FLAG_MOUNT_POINT)
294 delayed_error(_("Mount point"), _(
295 "A mount point is a directory which another "
296 "filing system can be mounted on. Everything "
297 "on the mounted filesystem then appears to be "
298 "inside the directory."));
299 else
300 dir_show_help(item, path);
301 break;
302 case TYPE_CHAR_DEVICE:
303 case TYPE_BLOCK_DEVICE:
304 delayed_error(_("Device file"), _(
305 "Device files allow you to read from or write "
306 "to a device driver as though it was an "
307 "ordinary file."));
308 break;
309 case TYPE_PIPE:
310 delayed_error(_("Named pipe"), _(
311 "Pipes allow different programs to "
312 "communicate. One program writes data to the "
313 "pipe while another one reads it out again."));
314 break;
315 case TYPE_SOCKET:
316 delayed_error(_("Socket"), _(
317 "Sockets allow processes to communicate."));
318 break;
319 default:
320 delayed_error(_("Unknown type"), _(
321 "I couldn't find out what kind of file this "
322 "is. Maybe it doesn't exist anymore or you "
323 "don't have search permission on the directory "
324 "it's in?"));
325 break;
329 /* Open a directory viewer showing this file, and wink it */
330 void open_to_show(guchar *path)
332 FilerWindow *new;
333 guchar *dir, *slash;
335 g_return_if_fail(path != NULL);
337 dir = g_strdup(path);
338 slash = strrchr(dir, '/');
339 if (slash == dir || !slash)
341 /* Item in the root (or root itself!) */
342 new = filer_opendir("/", NULL);
343 if (new && dir[1])
344 display_set_autoselect(new, dir + 1);
347 else
349 *slash = '\0';
350 new = filer_opendir(dir, NULL);
351 if (new)
352 display_set_autoselect(new, slash + 1);
355 g_free(dir);
358 /* Invoked using -x, this indicates that the filesystem has been modified
359 * and we should look at this item again.
361 void examine(guchar *path)
363 struct stat info;
365 if (mc_stat(path, &info) != 0)
367 /* Deleted? Do a paranoid update of everything... */
368 filer_check_mounted(path);
370 else
372 /* Update directory containing this item... */
373 dir_check_this(path);
375 /* If this is itself a directory then rescan its contents... */
376 if (S_ISDIR(info.st_mode))
377 refresh_dirs(path);
379 /* If it's on the pinboard, update the icon... */
380 icons_may_update(path);
384 /****************************************************************
385 * INTERNAL FUNCTIONS *
386 ****************************************************************/
389 static void write_data(gpointer data, gint fd, GdkInputCondition cond)
391 PipedData *pd = (PipedData *) data;
393 while (pd->sent < pd->length)
395 int sent;
397 sent = write(fd, pd->data + pd->sent, pd->length - pd->sent);
399 if (sent < 0)
401 if (errno == EAGAIN)
402 return;
403 delayed_rox_error(
404 _("Could not send data to program: %s"),
405 g_strerror(errno));
406 goto finish;
409 pd->sent += sent;
412 finish:
413 gdk_input_remove(pd->tag);
414 g_free(pd->data);
415 g_free(pd);
416 close(fd);
419 /* Follow the link 'full_path' and display it in filer_window, or a
420 * new window if that is NULL.
422 static gboolean follow_symlink(char *full_path,
423 FilerWindow *filer_window,
424 FilerWindow *src_window)
426 char *real, *slash;
427 char *new_dir;
428 char path[MAXPATHLEN + 1];
429 int got;
431 got = readlink(full_path, path, MAXPATHLEN);
432 if (got < 0)
434 delayed_rox_error(_("Could not read link: %s"),
435 g_strerror(errno));
436 return FALSE;
439 g_return_val_if_fail(got <= MAXPATHLEN, FALSE);
440 path[got] = '\0';
442 /* Make a relative path absolute */
443 if (path[0] != '/')
445 guchar *tmp;
446 slash = strrchr(full_path, '/');
447 g_return_val_if_fail(slash != NULL, FALSE);
449 tmp = g_strndup(full_path, slash - full_path);
450 real = pathdup(make_path(tmp, path)->str);
451 /* NB: full_path may be invalid here... */
452 g_free(tmp);
454 else
455 real = pathdup(path);
457 slash = strrchr(real, '/');
458 if (!slash)
460 g_free(real);
461 delayed_rox_error(
462 _("Broken symlink (or you don't have permission "
463 "to follow it): %s"), full_path);
464 return FALSE;
467 *slash = '\0';
469 if (*real)
470 new_dir = real;
471 else
472 new_dir = "/";
474 if (filer_window)
475 filer_change_to(filer_window, new_dir, slash + 1);
476 else
478 FilerWindow *new;
480 new = filer_opendir(new_dir, src_window);
481 display_set_autoselect(new, slash + 1);
484 g_free(real);
486 return TRUE;
489 /* Load this file into an appropriate editor */
490 static gboolean open_file(guchar *path, MIME_type *type)
492 g_return_val_if_fail(type != NULL, FALSE);
494 if (type_open(path, type))
495 return TRUE;
497 report_rox_error(
498 _("No run action specified for files of this type (%s/%s) - "
499 "you can set a run action by choosing `Set Run Action' "
500 "from the File menu, or you can just drag the file to an "
501 "application"),
502 type->media_type,
503 type->subtype);
505 return FALSE;
508 /* Show the help for a directory - tries to open App/Help, but if
509 * that doesn't work then it displays a default message.
511 static void dir_show_help(DirItem *item, char *path)
513 char *help_dir;
514 struct stat info;
516 help_dir = g_strconcat(path, "/Help", NULL);
518 if (mc_stat(help_dir, &info) == 0)
519 filer_opendir(help_dir, NULL);
520 else if (item->flags & ITEM_FLAG_APPDIR)
521 delayed_error(_("Application"),
522 _("This is an application directory - you can "
523 "run it as a program, or open it (hold down "
524 "Shift while you open it). Most applications provide "
525 "their own help here, but this one doesn't."));
526 else
527 delayed_error(_("Directory"), _(
528 "This is a directory. It contains an index to "
529 "other items - open it to see the list."));