]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/utilsexc.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Execution-related utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
18 #include "wx/process.h"
27 #ifdef __HIDE_FORBIDDEN_NAMES
28 #undefine __HIDE_FORBIDDEN_NAMES
32 /*because 'noshare' is not valid in vax C++*/
40 #if defined(__AIX__) || defined(__xlC__)
41 #include <sys/socket.h>
42 #include <sys/select.h>
44 #ifndef __DATA_GENERAL__
45 #include <sys/syscall.h>
57 #include <sys/systeminfo.h>
61 // somehow missing from sys/wait.h but in the system's docs
64 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
74 struct wxLocalProcessData
81 // somehow missing from sys/wait.h but in the system's docs
84 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
89 void wxUsleep(unsigned long milliseconds
)
91 #if defined(HAVE_NANOSLEEP)
93 tmReq
.tv_sec
= milliseconds
/ 1000;
94 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
96 // we're not interested in remaining time nor in return value
97 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
98 #elif defined(HAVE_USLEEP)
99 // uncomment this if you feel brave or if you are sure that your version
100 // of Solaris has a safe usleep() function but please notice that usleep()
101 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
102 // documented as MT-Safe
103 #if defined(__SUN__) && defined(wxUSE_THREADS)
104 #error "usleep() cannot be used in MT programs under Solaris."
107 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
108 #else // !sleep function
109 #error "usleep() or nanosleep() function required for wxUsleep"
110 #endif // sleep function
113 void xt_notify_end_process(XtPointer client
, int *fid
,
116 wxLocalProcessData
*process_data
= (wxLocalProcessData
*)client
;
120 pid
= (process_data
->pid
> 0) ? process_data
->pid
: -(process_data
->pid
);
122 /* wait4 is not part of any standard, use at own risk
123 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
124 * --- offer@sgi.com */
125 #if !defined(__HPUX__) && !defined(__sgi) && !defined(__SGI__) && !defined(__ALPHA__) && !defined(__SUNCC__)
126 wait4(process_data
->pid
, NULL
, 0, NULL
);
128 wait3((int *) NULL
, 0, (rusage
*) NULL
);
132 if (process_data
->process
)
133 process_data
->process
->OnTerminate(process_data
->pid
, 0); // What should 'status' be?
135 process_data
->end_process
= TRUE
;
137 if (process_data->pid > 0) // synchronous
140 process_data->pid = 0;
145 long wxExecute(char **argv
, bool sync
, wxProcess
*handler
)
151 return 0; // Nothing???
157 /* fork the process */
158 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
159 pid_t pid
= vfork ();
170 /* GUILHEM: Close all fds when sync == 0 */
172 for (int fd
=0;fd
<FD_SETSIZE
;fd
++) {
173 if (proc_link
[1] != fd
)
178 execvp ((const char *)*argv
, (const char **)argv
);
180 execvp (*argv
, argv
);
182 /* GUILHEM: Reopen output stream */
183 // open("/dev/console", O_WRONLY);
186 printf ("%s: command not found\n", *argv
);
189 printf ("wxWindows: could not execute '%s'\n", *argv
);
193 wxLocalProcessData
*process_data
= new wxLocalProcessData
;
195 process_data
->end_process
= 0;
196 process_data
->process
= handler
;
197 process_data
->pid
= (sync
) ? pid
: -pid
;
200 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(), proc_link
[0],
201 (XtPointer
*) XtInputReadMask
,
202 (XtInputCallbackProc
) xt_notify_end_process
,
203 (XtPointer
) process_data
);
207 while (!process_data
->end_process
)
208 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
210 if (WIFEXITED(process_data
->end_process
) != 0)
212 return WEXITSTATUS(process_data
->end_process
);
221 long wxExecute (const wxString
& command
, bool sync
, wxProcess
* handler
)
226 if (command
.IsNull() || command
== "")
227 return 0; // Nothing to do
229 // Run a program the recomended way under X (XView)
233 const char *IFS
= " \t\n";
235 // Build argument vector
236 strncpy (tmp
, (const char*) command
, sizeof (tmp
) / sizeof (char) - 1);
237 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
238 argv
[argc
++] = strtok (tmp
, IFS
);
239 while ((argv
[argc
++] = strtok (NULL
, IFS
)) != NULL
)
242 return wxExecute(argv
, sync
, handler
);