]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utilsexc.cpp
wxTextFile::Type changed to ::wxTextFileType (portability)
[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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
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
39 #if defined(__AIX__) || defined(__xlC__)
40 #include <sys/socket.h>
41 #include <sys/select.h>
42 #else
43 #ifndef __DATA_GENERAL__
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
55 #ifdef __SVR4__
56 #include <sys/systeminfo.h>
57 #endif
58
59 #ifdef __SOLARIS__
60 // somehow missing from sys/wait.h but in the system's docs
61 extern "C"
62 {
63 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
64 *rusage);
65 }
66 #endif
67
68 #include <sys/time.h>
69 #include <errno.h>
70
71 #include <Xm/Xm.h>
72
73 struct wxLocalProcessData
74 {
75 int pid, end_process;
76 wxProcess *process;
77 };
78
79 #ifdef __SOLARIS__
80 // somehow missing from sys/wait.h but in the system's docs
81 extern "C"
82 {
83 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
84 *rusage);
85 }
86 #endif
87
88 void xt_notify_end_process(XtPointer client, int *fid,
89 XtInputId *id)
90 {
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 */
100 #if !defined(__sgi) && !defined(__ALPHA__)
101 wait4(process_data->pid, NULL, 0, NULL);
102 #else
103 wait3((int *) NULL, 0, (rusage *) NULL);
104 #endif
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;
116 }
117
118 long wxExecute(char **argv, bool sync, wxProcess *handler)
119 {
120 #ifdef VMS
121 return(0);
122 #else
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 */
131 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
132 pid_t pid = vfork ();
133 #else
134 pid_t pid = fork ();
135 #endif
136
137 if (pid == -1)
138 {
139 return 0;
140 }
141 else if (pid == 0)
142 {
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 */
150 #ifdef _AIX
151 execvp ((const char *)*argv, (const char **)argv);
152 #else
153 execvp (*argv, argv);
154 #endif
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);
164 }
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;
189 #endif
190 // end VMS
191 }
192
193 long wxExecute (const wxString& command, bool sync, wxProcess* handler)
194 {
195 #ifdef VMS
196 return(0);
197 #else
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);
215 #endif
216 // VMS
217 }