]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utilsexec.cpp | |
3 | // Purpose: Execution-related utilities | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
55acd85e | 13 | // #pragma implementation |
4bb6408c JS |
14 | #endif |
15 | ||
16 | #include "wx/utils.h" | |
17 | #include "wx/app.h" | |
5b6aa0ff | 18 | #include "wx/process.h" |
4bb6408c JS |
19 | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
7fe7d506 | 23 | #include <errno.h> |
4bb6408c JS |
24 | |
25 | #ifdef VMS | |
26 | /*steve*/ | |
27 | #ifdef __HIDE_FORBIDDEN_NAMES | |
28 | #undefine __HIDE_FORBIDDEN_NAMES | |
29 | #endif | |
30 | #include <socket.h> | |
31 | #ifdef VAX | |
32 | /*because 'noshare' is not valid in vax C++*/ | |
33 | #define CC$VAXCSHR 1 | |
34 | #endif | |
35 | #include <unixlib.h> | |
36 | #define unlink DELETE | |
37 | ||
38 | #else | |
39 | ||
5dcf05ae | 40 | #if defined(__AIX__) || defined(__xlC__) |
4bb6408c JS |
41 | #include <sys/socket.h> |
42 | #include <sys/select.h> | |
43 | #else | |
5dcf05ae | 44 | #ifndef __DATA_GENERAL__ |
4bb6408c JS |
45 | #include <sys/syscall.h> |
46 | #endif | |
47 | #endif | |
48 | ||
49 | #include <sys/wait.h> | |
50 | #include <unistd.h> | |
51 | #include <dirent.h> | |
52 | #include <pwd.h> | |
53 | ||
54 | #endif | |
55 | ||
6b037754 | 56 | #ifdef __SVR4__ |
2d120f83 | 57 | #include <sys/systeminfo.h> |
6b037754 JS |
58 | #endif |
59 | ||
60 | #ifdef __SOLARIS__ | |
61 | // somehow missing from sys/wait.h but in the system's docs | |
62 | extern "C" | |
63 | { | |
2d120f83 JS |
64 | pid_t wait4(pid_t pid, int *statusp, int options, struct rusage |
65 | *rusage); | |
6b037754 JS |
66 | } |
67 | #endif | |
68 | ||
4bb6408c | 69 | #include <sys/time.h> |
6b037754 | 70 | #include <errno.h> |
4bb6408c JS |
71 | |
72 | #include <Xm/Xm.h> | |
73 | ||
8ef6a930 GL |
74 | struct wxLocalProcessData |
75 | { | |
2d120f83 JS |
76 | int pid, end_process; |
77 | wxProcess *process; | |
8ef6a930 GL |
78 | }; |
79 | ||
80 | #ifdef __SOLARIS__ | |
81 | // somehow missing from sys/wait.h but in the system's docs | |
82 | extern "C" | |
83 | { | |
2d120f83 JS |
84 | pid_t wait4(pid_t pid, int *statusp, int options, struct rusage |
85 | *rusage); | |
8ef6a930 GL |
86 | } |
87 | #endif | |
4bb6408c JS |
88 | |
89 | void xt_notify_end_process(XtPointer client, int *fid, | |
2d120f83 | 90 | XtInputId *id) |
4bb6408c | 91 | { |
2d120f83 JS |
92 | wxLocalProcessData *process_data = (wxLocalProcessData *)client; |
93 | ||
94 | int pid; | |
95 | ||
96 | pid = (process_data->pid > 0) ? process_data->pid : -(process_data->pid); | |
97 | ||
98 | /* wait4 is not part of any standard, use at own risk | |
99 | * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-) | |
100 | * --- offer@sgi.com */ | |
b23386b2 | 101 | #if !defined(__HPUX__) && !defined(__sgi) && !defined(__SGI__) && !defined(__ALPHA__) && !defined(__SUNCC__) |
2d120f83 | 102 | wait4(process_data->pid, NULL, 0, NULL); |
8ef6a930 | 103 | #else |
2d120f83 | 104 | wait3((int *) NULL, 0, (rusage *) NULL); |
8ef6a930 | 105 | #endif |
2d120f83 JS |
106 | |
107 | XtRemoveInput(*id); | |
108 | if (process_data->process) | |
5b6aa0ff | 109 | process_data->process->OnTerminate(process_data->pid, 0); // What should 'status' be? |
2d120f83 JS |
110 | |
111 | process_data->end_process = TRUE; | |
2e35f56f | 112 | /* |
0492c5a0 | 113 | if (process_data->pid > 0) // synchronous |
2d120f83 JS |
114 | delete process_data; |
115 | else | |
116 | process_data->pid = 0; | |
2e35f56f JS |
117 | */ |
118 | delete process_data; | |
4bb6408c JS |
119 | } |
120 | ||
8ef6a930 | 121 | long wxExecute(char **argv, bool sync, wxProcess *handler) |
4bb6408c JS |
122 | { |
123 | #ifdef VMS | |
2d120f83 | 124 | return(0); |
4bb6408c | 125 | #else |
2d120f83 JS |
126 | if (*argv == NULL) |
127 | return 0; // Nothing??? | |
128 | ||
129 | int proc_link[2]; | |
130 | if (pipe(proc_link)) | |
131 | return 0; | |
132 | ||
133 | /* fork the process */ | |
4bb6408c | 134 | #if defined(sun) || defined(__ultrix) || defined(__bsdi__) |
2d120f83 | 135 | pid_t pid = vfork (); |
4bb6408c | 136 | #else |
2d120f83 | 137 | pid_t pid = fork (); |
4bb6408c | 138 | #endif |
2d120f83 JS |
139 | |
140 | if (pid == -1) | |
4bb6408c | 141 | { |
2d120f83 | 142 | return 0; |
4bb6408c | 143 | } |
2d120f83 | 144 | else if (pid == 0) |
4bb6408c | 145 | { |
2d120f83 JS |
146 | /* GUILHEM: Close all fds when sync == 0 */ |
147 | if (sync == 0) | |
148 | for (int fd=0;fd<FD_SETSIZE;fd++) { | |
149 | if (proc_link[1] != fd) | |
150 | close(fd); | |
151 | } | |
152 | /* child */ | |
4bb6408c | 153 | #ifdef _AIX |
2d120f83 | 154 | execvp ((const char *)*argv, (const char **)argv); |
4bb6408c | 155 | #else |
2d120f83 | 156 | execvp (*argv, argv); |
4bb6408c | 157 | #endif |
2d120f83 JS |
158 | /* GUILHEM: Reopen output stream */ |
159 | // open("/dev/console", O_WRONLY); | |
160 | /* GUILHEM: End */ | |
161 | if (errno == ENOENT) | |
162 | printf ("%s: command not found\n", *argv); | |
163 | else | |
164 | perror (*argv); | |
165 | printf ("wxWindows: could not execute '%s'\n", *argv); | |
166 | _exit (-1); | |
4bb6408c | 167 | } |
0492c5a0 | 168 | |
2d120f83 JS |
169 | wxLocalProcessData *process_data = new wxLocalProcessData; |
170 | ||
171 | process_data->end_process = 0; | |
172 | process_data->process = handler; | |
173 | process_data->pid = (sync) ? pid : -pid; | |
174 | ||
175 | close(proc_link[1]); | |
176 | XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(), proc_link[0], | |
177 | (XtPointer *) XtInputReadMask, | |
178 | (XtInputCallbackProc) xt_notify_end_process, | |
179 | (XtPointer) process_data); | |
180 | ||
0492c5a0 | 181 | if (sync) |
33b64e6f | 182 | { |
0492c5a0 JS |
183 | while (!process_data->end_process) |
184 | XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); | |
185 | ||
186 | if (WIFEXITED(process_data->end_process) != 0) | |
187 | { | |
0492c5a0 JS |
188 | return WEXITSTATUS(process_data->end_process); |
189 | } | |
2d120f83 | 190 | } |
2d120f83 JS |
191 | |
192 | return pid; | |
4bb6408c | 193 | #endif |
2d120f83 | 194 | // end VMS |
4bb6408c JS |
195 | } |
196 | ||
8ef6a930 | 197 | long wxExecute (const wxString& command, bool sync, wxProcess* handler) |
4bb6408c JS |
198 | { |
199 | #ifdef VMS | |
2d120f83 | 200 | return(0); |
4bb6408c | 201 | #else |
2d120f83 JS |
202 | if (command.IsNull() || command == "") |
203 | return 0; // Nothing to do | |
204 | ||
205 | // Run a program the recomended way under X (XView) | |
206 | int argc = 0; | |
207 | char *argv[127]; | |
208 | char tmp[1024]; | |
209 | const char *IFS = " \t\n"; | |
210 | ||
211 | // Build argument vector | |
212 | strncpy (tmp, (const char*) command, sizeof (tmp) / sizeof (char) - 1); | |
213 | tmp[sizeof (tmp) / sizeof (char) - 1] = '\0'; | |
214 | argv[argc++] = strtok (tmp, IFS); | |
215 | while ((argv[argc++] = strtok (NULL, IFS)) != NULL) | |
216 | /* loop */ ; | |
217 | ||
218 | return wxExecute(argv, sync, handler); | |
4bb6408c | 219 | #endif |
2d120f83 | 220 | // VMS |
4bb6408c | 221 | } |