chiark / gitweb /
Imported Debian patch 1.0.0-6
[e16] / src / actions.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2007 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "E.h"
25 #include "desktops.h"
26 #include "file.h"
27 #include "user.h"
28
29 static void
30 StartupIdExport(void)
31 {
32    char                buf[128];
33    Desk               *dsk;
34    int                 ax, ay;
35
36    dsk = DesksGetCurrent();
37    DeskGetArea(dsk, &ax, &ay);
38
39    Esnprintf(buf, sizeof(buf), "e16/%d:%d:%d,%d", Mode.apps.startup_id,
40              dsk->num, ax, ay);
41    Esetenv("DESKTOP_STARTUP_ID", buf);
42 }
43
44 static void
45 ExecSetupEnv(int flags)
46 {
47    int                 fd;
48
49    setsid();
50
51    /* Close all file descriptors except the std 3 */
52    for (fd = 3; fd < 1024; fd++)
53       close(fd);
54
55    /* Set up env stuff */
56    if (flags & EXEC_SET_LANG)
57       LangExport();
58    if (flags & EXEC_SET_STARTUP_ID)
59       StartupIdExport();
60
61 #if USE_ROOTHACKLIB
62    if (Mode.wm.window)
63       Esetenv("LD_PRELOAD", ENLIGHTENMENT_LIB "/libhack.so");
64 #endif
65 }
66
67 int
68 execApplication(const char *params, int flags)
69 {
70    char                exe[FILEPATH_LEN_MAX];
71    char               *sh;
72    char               *path;
73    char               *real_exec;
74
75    if (!params)
76       return -1;
77
78    sscanf(params, "%4000s", exe);
79    if (exe[0] == '\0')
80       return -1;
81
82    Mode.apps.startup_id++;
83    if (fork())
84       return 0;
85
86    ExecSetupEnv(flags);
87
88    sh = usershell(getuid());
89
90    if (path_canexec(exe))
91      {
92         real_exec = EMALLOC(char, strlen(params) + 6);
93
94         if (!real_exec)
95            return -1;
96         sprintf(real_exec, "exec %s", params);
97
98         execl(sh, sh, "-c", real_exec, NULL);
99         /* We should not get here - invalid shell? */
100      }
101
102    if (!Mode.wm.startup)
103      {
104         path = path_test(exe, EFILE_ANY);
105         if (!path)
106           {
107              /* absolute path */
108              if (isabspath(exe))
109                 AlertOK(_("There was an error running the program:\n"
110                           "%s\n"
111                           "This program could not be executed.\n"
112                           "This is because the file does not exist.\n"), exe);
113              /* relative path */
114              else
115                 AlertOK(_("There was an error running the program:\n"
116                           "%s\n"
117                           "This program could not be executed.\n"
118                           "This is most probably because this "
119                           "program is not in the\n"
120                           "path for your shell which is %s. "
121                           "I suggest you read the manual\n"
122                           "page for that shell and read up how to "
123                           "change or add to your\n"
124                           "execution path.\n"), exe, sh);
125           }
126         else
127            /* it is a node on the filing sys */
128           {
129              /* it's a file */
130              if (isfile(path))
131                {
132                   /* can execute it */
133                   if (canexec(path))
134                      AlertOK(_("There was an error running the program:\n"
135                                "%s\n"
136                                "This program could not be executed.\n"
137                                "I am unsure as to why you could not "
138                                "do this. The file exists,\n"
139                                "is a file, and you are allowed to "
140                                "execute it. I suggest you look\n"
141                                "into this.\n"), path);
142                   /* not executable file */
143                   else
144                      AlertOK(_("There was an error running the program:\n"
145                                "%s\n"
146                                "This program could not be executed.\n"
147                                "This is because the file exists, is a "
148                                "file, but you are unable\n"
149                                "to execute it because you do not "
150                                "have execute " "access to this file.\n"), path);
151                }
152              /* it's not a file */
153              else
154                {
155                   /* its a dir */
156                   if (isdir(path))
157                      AlertOK(_("There was an error running the program:\n"
158                                "%s\n"
159                                "This program could not be executed.\n"
160                                "This is because the file is in fact "
161                                "a directory.\n"), path);
162                   /* its not a file or a dir */
163                   else
164                      AlertOK(_("There was an error running the program:\n"
165                                "%s\n"
166                                "This program could not be executed.\n"
167                                "This is because the file is not a "
168                                "regular file.\n"), path);
169                }
170              Efree(path);
171           }
172      }
173    exit(100);
174 }
175
176 void
177 Espawn(int argc __UNUSED__, char **argv)
178 {
179    if (!argv || !argv[0])
180       return;
181
182    if (fork())
183       return;
184
185    ExecSetupEnv(EXEC_SET_LANG);
186
187    execvp(argv[0], argv);
188
189    AlertOK(_("There was an error running the program:\n%s\n"), argv[0]);
190    exit(100);
191 }
192
193 void
194 EspawnCmd(const char *cmd)
195 {
196    int                 argc;
197    char              **argv;
198
199    argv = StrlistDecodeEscaped(cmd, &argc);
200    Espawn(argc, argv);
201    StrlistFree(argv, argc);
202 }