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