]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utilsexc.cpp
3c65b50a732cbc5ba34f234a1401519d1156e122
[wxWidgets.git] / src / motif / utilsexc.cpp
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__
13 // #pragma implementation
14 #endif
15
16 #include "wx/utils.h"
17 #include "wx/app.h"
18 #include "wx/process.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
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
40 #if defined(__AIX__) || defined(__xlC__)
41 #include <sys/socket.h>
42 #include <sys/select.h>
43 #else
44 #ifndef __DATA_GENERAL__
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
56 #ifdef __SVR4__
57 #include <sys/systeminfo.h>
58 #endif
59
60 #ifdef __SOLARIS__
61 // somehow missing from sys/wait.h but in the system's docs
62 extern "C"
63 {
64 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
65 *rusage);
66 }
67 #endif
68
69 #include <sys/time.h>
70 #include <errno.h>
71
72 #include <Xm/Xm.h>
73
74 struct wxLocalProcessData
75 {
76 int pid, end_process;
77 wxProcess *process;
78 };
79
80 #ifdef __SOLARIS__
81 // somehow missing from sys/wait.h but in the system's docs
82 extern "C"
83 {
84 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
85 *rusage);
86 }
87 #endif
88
89 void xt_notify_end_process(XtPointer client, int *fid,
90 XtInputId *id)
91 {
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 */
101 #if !defined(__HPUX__) && !defined(__sgi) && !defined(__SGI__) && !defined(__ALPHA__) && !defined(__SUNCC__)
102 wait4(process_data->pid, NULL, 0, NULL);
103 #else
104 wait3((int *) NULL, 0, (rusage *) NULL);
105 #endif
106
107 XtRemoveInput(*id);
108 if (process_data->process)
109 process_data->process->OnTerminate(process_data->pid, 0); // What should 'status' be?
110
111 process_data->end_process = TRUE;
112 /*
113 if (process_data->pid > 0) // synchronous
114 delete process_data;
115 else
116 process_data->pid = 0;
117 */
118 delete process_data;
119 }
120
121 long wxExecute(char **argv, bool sync, wxProcess *handler)
122 {
123 #ifdef VMS
124 return(0);
125 #else
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 */
134 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
135 pid_t pid = vfork ();
136 #else
137 pid_t pid = fork ();
138 #endif
139
140 if (pid == -1)
141 {
142 return 0;
143 }
144 else if (pid == 0)
145 {
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 */
153 #ifdef _AIX
154 execvp ((const char *)*argv, (const char **)argv);
155 #else
156 execvp (*argv, argv);
157 #endif
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);
167 }
168
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
181 if (sync)
182 {
183 while (!process_data->end_process)
184 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
185
186 if (WIFEXITED(process_data->end_process) != 0)
187 {
188 return WEXITSTATUS(process_data->end_process);
189 }
190 }
191
192 return pid;
193 #endif
194 // end VMS
195 }
196
197 long wxExecute (const wxString& command, bool sync, wxProcess* handler)
198 {
199 #ifdef VMS
200 return(0);
201 #else
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);
219 #endif
220 // VMS
221 }