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