]> git.saurik.com Git - wxWidgets.git/blame - src/motif/utilsexc.cpp
thread fixes for MSW (samples doesn't compile currently under !MSW, will
[wxWidgets.git] / src / motif / utilsexc.cpp
CommitLineData
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
JS
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
61extern "C"
62{
63 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
64 *rusage);
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
73struct 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
81extern "C"
82{
83 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
84 *rusage);
85}
86#endif
4bb6408c
JS
87
88void xt_notify_end_process(XtPointer client, int *fid,
89 XtInputId *id)
90{
8ef6a930
GL
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__)
8ef6a930
GL
101 wait4(process_data->pid, NULL, 0, NULL);
102#else
103 wait3((int *) NULL, 0, (rusage *) NULL);
104#endif
4bb6408c
JS
105
106 XtRemoveInput(*id);
8ef6a930
GL
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 118long wxExecute(char **argv, bool sync, wxProcess *handler)
4bb6408c
JS
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
8ef6a930
GL
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;
4bb6408c
JS
171
172 close(proc_link[1]);
173 XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(), proc_link[0],
174 (XtPointer *) XtInputReadMask,
175 (XtInputCallbackProc) xt_notify_end_process,
8ef6a930 176 (XtPointer) process_data);
4bb6408c
JS
177
178 if (sync) {
8ef6a930 179 while (!process_data->end_process)
4bb6408c
JS
180 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
181
8ef6a930
GL
182 if (WIFEXITED(process_data->end_process) != 0)
183 return WEXITSTATUS(process_data->end_process);
4bb6408c
JS
184 }
185
8ef6a930
GL
186 delete process_data;
187
4bb6408c
JS
188 return pid;
189#endif
190 // end VMS
191}
192
8ef6a930 193long wxExecute (const wxString& command, bool sync, wxProcess* handler)
4bb6408c
JS
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
8ef6a930 214 return wxExecute(argv, sync, handler);
4bb6408c
JS
215#endif
216 // VMS
217}