]>
Commit | Line | Data |
---|---|---|
518b5d2f VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utilsunx.cpp | |
3 | // Purpose: generic Unix implementation of many wx functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
18 | #include "wx/defs.h" | |
19 | #include "wx/string.h" | |
20 | ||
21 | #include "wx/intl.h" | |
22 | #include "wx/log.h" | |
a37a5a73 | 23 | #include "wx/app.h" |
518b5d2f VZ |
24 | |
25 | #include "wx/utils.h" | |
26 | #include "wx/process.h" | |
bdc72a22 | 27 | #include "wx/thread.h" |
518b5d2f | 28 | |
8b33ae2d GL |
29 | #include "wx/stream.h" |
30 | ||
6dc6fda6 VZ |
31 | #if wxUSE_GUI |
32 | #include "wx/unix/execute.h" | |
33 | #endif | |
518b5d2f | 34 | |
f6bcfd97 BP |
35 | // SGI signal.h defines signal handler arguments differently depending on |
36 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
37 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
38 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
39 | #endif // SGI hack | |
40 | ||
518b5d2f VZ |
41 | #include <stdarg.h> |
42 | #include <dirent.h> | |
43 | #include <string.h> | |
44 | #include <sys/stat.h> | |
45 | #include <sys/types.h> | |
46 | #include <unistd.h> | |
47 | #include <sys/wait.h> | |
48 | #include <pwd.h> | |
49 | #include <errno.h> | |
50 | #include <netdb.h> | |
51 | #include <signal.h> | |
52 | #include <fcntl.h> // for O_WRONLY and friends | |
53 | #include <time.h> // nanosleep() and/or usleep() | |
fad866f4 | 54 | #include <ctype.h> // isspace() |
b12915c1 | 55 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 56 | |
0fcdf6dc | 57 | #ifdef HAVE_UNAME |
518b5d2f VZ |
58 | #include <sys/utsname.h> // for uname() |
59 | #endif // HAVE_UNAME | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // conditional compilation | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | // many versions of Unices have this function, but it is not defined in system | |
66 | // headers - please add your system here if it is the case for your OS. | |
67 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b VZ |
68 | #if !defined(HAVE_USLEEP) && \ |
69 | (defined(__SUN__) && !defined(__SunOs_5_6) && \ | |
518b5d2f | 70 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
fd9811b1 | 71 | defined(__osf__) || defined(__EMX__) |
518b5d2f VZ |
72 | extern "C" |
73 | { | |
1363811b VZ |
74 | #ifdef __SUN__ |
75 | int usleep(unsigned int usec); | |
76 | #else // !Sun | |
bdc72a22 VZ |
77 | #ifdef __EMX__ |
78 | /* I copied this from the XFree86 diffs. AV. */ | |
79 | #define INCL_DOSPROCESS | |
80 | #include <os2.h> | |
81 | inline void usleep(unsigned long delay) | |
82 | { | |
83 | DosSleep(delay ? (delay/1000l) : 1l); | |
84 | } | |
85 | #else // !Sun && !EMX | |
86 | void usleep(unsigned long usec); | |
87 | #endif | |
e6daf794 | 88 | #endif // Sun/EMX/Something else |
518b5d2f | 89 | }; |
bdc72a22 VZ |
90 | |
91 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
92 | #endif // Unices without usleep() |
93 | ||
518b5d2f VZ |
94 | // ============================================================================ |
95 | // implementation | |
96 | // ============================================================================ | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // sleeping | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | void wxSleep(int nSecs) | |
103 | { | |
104 | sleep(nSecs); | |
105 | } | |
106 | ||
107 | void wxUsleep(unsigned long milliseconds) | |
108 | { | |
b12915c1 | 109 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 110 | timespec tmReq; |
13111b2a | 111 | tmReq.tv_sec = (time_t)(milliseconds / 1000); |
518b5d2f VZ |
112 | tmReq.tv_nsec = (milliseconds % 1000) * 1000 * 1000; |
113 | ||
114 | // we're not interested in remaining time nor in return value | |
115 | (void)nanosleep(&tmReq, (timespec *)NULL); | |
b12915c1 | 116 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
117 | // uncomment this if you feel brave or if you are sure that your version |
118 | // of Solaris has a safe usleep() function but please notice that usleep() | |
119 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
120 | // documented as MT-Safe | |
ea18eed9 | 121 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
122 | #error "usleep() cannot be used in MT programs under Solaris." |
123 | #endif // Sun | |
124 | ||
125 | usleep(milliseconds * 1000); // usleep(3) wants microseconds | |
b12915c1 VZ |
126 | #elif defined(HAVE_SLEEP) |
127 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
128 | sleep(milliseconds * 1000); | |
518b5d2f VZ |
129 | #else // !sleep function |
130 | #error "usleep() or nanosleep() function required for wxUsleep" | |
131 | #endif // sleep function | |
132 | } | |
133 | ||
134 | // ---------------------------------------------------------------------------- | |
135 | // process management | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
0fb67cd1 | 138 | int wxKill(long pid, wxSignal sig) |
518b5d2f | 139 | { |
13111b2a | 140 | return kill((pid_t)pid, (int)sig); |
518b5d2f VZ |
141 | } |
142 | ||
fad866f4 KB |
143 | #define WXEXECUTE_NARGS 127 |
144 | ||
518b5d2f VZ |
145 | long wxExecute( const wxString& command, bool sync, wxProcess *process ) |
146 | { | |
223d09f6 | 147 | wxCHECK_MSG( !command.IsEmpty(), 0, wxT("can't exec empty command") ); |
518b5d2f VZ |
148 | |
149 | int argc = 0; | |
05079acc | 150 | wxChar *argv[WXEXECUTE_NARGS]; |
fad866f4 | 151 | wxString argument; |
05079acc | 152 | const wxChar *cptr = command.c_str(); |
223d09f6 | 153 | wxChar quotechar = wxT('\0'); // is arg quoted? |
fad866f4 | 154 | bool escaped = FALSE; |
518b5d2f | 155 | |
0ed9a934 | 156 | // split the command line in arguments |
fad866f4 KB |
157 | do |
158 | { | |
223d09f6 KB |
159 | argument=wxT(""); |
160 | quotechar = wxT('\0'); | |
0ed9a934 | 161 | |
fad866f4 | 162 | // eat leading whitespace: |
05079acc | 163 | while ( wxIsspace(*cptr) ) |
fad866f4 | 164 | cptr++; |
0ed9a934 | 165 | |
223d09f6 | 166 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) |
fad866f4 | 167 | quotechar = *cptr++; |
0ed9a934 | 168 | |
fad866f4 KB |
169 | do |
170 | { | |
223d09f6 | 171 | if ( *cptr == wxT('\\') && ! escaped ) |
fad866f4 KB |
172 | { |
173 | escaped = TRUE; | |
174 | cptr++; | |
175 | continue; | |
176 | } | |
0ed9a934 | 177 | |
fad866f4 | 178 | // all other characters: |
0ed9a934 | 179 | argument += *cptr++; |
fad866f4 | 180 | escaped = FALSE; |
0ed9a934 VZ |
181 | |
182 | // have we reached the end of the argument? | |
183 | if ( (*cptr == quotechar && ! escaped) | |
223d09f6 KB |
184 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) |
185 | || *cptr == wxT('\0') ) | |
fad866f4 | 186 | { |
0ed9a934 | 187 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, |
223d09f6 | 188 | wxT("too many arguments in wxExecute") ); |
0ed9a934 | 189 | |
05079acc OK |
190 | argv[argc] = new wxChar[argument.length() + 1]; |
191 | wxStrcpy(argv[argc], argument.c_str()); | |
fad866f4 | 192 | argc++; |
0ed9a934 | 193 | |
fad866f4 | 194 | // if not at end of buffer, swallow last character: |
0ed9a934 VZ |
195 | if(*cptr) |
196 | cptr++; | |
197 | ||
fad866f4 KB |
198 | break; // done with this one, start over |
199 | } | |
0ed9a934 VZ |
200 | } while(*cptr); |
201 | } while(*cptr); | |
fad866f4 | 202 | argv[argc] = NULL; |
0ed9a934 VZ |
203 | |
204 | // do execute the command | |
518b5d2f VZ |
205 | long lRc = wxExecute(argv, sync, process); |
206 | ||
0ed9a934 | 207 | // clean up |
fad866f4 | 208 | argc = 0; |
0ed9a934 | 209 | while( argv[argc] ) |
fad866f4 | 210 | delete [] argv[argc++]; |
518b5d2f VZ |
211 | |
212 | return lRc; | |
213 | } | |
214 | ||
2c8e4738 VZ |
215 | // ---------------------------------------------------------------------------- |
216 | // wxShell | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
219 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
220 | { |
221 | wxString cmd; | |
cd6ce4a9 | 222 | if ( !command ) |
2c8e4738 VZ |
223 | { |
224 | // just an interactive shell | |
cd6ce4a9 | 225 | cmd = _T("xterm"); |
2c8e4738 | 226 | } |
518b5d2f | 227 | else |
2c8e4738 VZ |
228 | { |
229 | // execute command in a shell | |
230 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
231 | } | |
232 | ||
233 | return cmd; | |
234 | } | |
235 | ||
236 | bool wxShell(const wxString& command) | |
237 | { | |
238 | return wxExecute(wxMakeShellCommand(command), TRUE /* sync */) == 0; | |
239 | } | |
240 | ||
241 | bool wxShell(const wxString& command, wxArrayString& output) | |
242 | { | |
243 | wxCHECK_MSG( !!command, FALSE, _T("can't exec shell non interactively") ); | |
518b5d2f | 244 | |
2c8e4738 | 245 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
246 | } |
247 | ||
6dc6fda6 VZ |
248 | #if wxUSE_GUI |
249 | ||
518b5d2f VZ |
250 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
251 | { | |
252 | int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid); | |
253 | ||
0ed9a934 VZ |
254 | // waitpid is POSIX so should be available everywhere, however on older |
255 | // systems wait() might be used instead in a loop (until the right pid | |
256 | // terminates) | |
518b5d2f | 257 | int status = 0; |
ab857a4e KB |
258 | int rc; |
259 | ||
bdc72a22 | 260 | // wait for child termination and if waitpid() was interrupted, try again |
ab857a4e | 261 | do |
bdc72a22 | 262 | { |
ab857a4e | 263 | rc = waitpid(pid, &status, 0); |
bdc72a22 VZ |
264 | } |
265 | while ( rc == -1 && errno == EINTR ); | |
266 | ||
ab857a4e | 267 | |
ab857a4e | 268 | if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) ) |
0ed9a934 | 269 | { |
ab857a4e KB |
270 | wxLogSysError(_("Waiting for subprocess termination failed")); |
271 | /* AFAIK, this can only happen if something went wrong within | |
bdc72a22 | 272 | wxGTK, i.e. due to a race condition or some serious bug. |
ab857a4e KB |
273 | After having fixed the order of statements in |
274 | GTK_EndProcessDetector(). (KB) | |
275 | */ | |
0ed9a934 VZ |
276 | } |
277 | else | |
278 | { | |
279 | // notify user about termination if required | |
280 | if (proc_data->process) | |
281 | { | |
282 | proc_data->process->OnTerminate(proc_data->pid, | |
283 | WEXITSTATUS(status)); | |
284 | } | |
ab857a4e KB |
285 | // clean up |
286 | if ( proc_data->pid > 0 ) | |
287 | { | |
288 | delete proc_data; | |
289 | } | |
290 | else | |
291 | { | |
292 | // wxExecute() will know about it | |
293 | proc_data->exitcode = status; | |
bdc72a22 | 294 | |
ab857a4e KB |
295 | proc_data->pid = 0; |
296 | } | |
518b5d2f VZ |
297 | } |
298 | } | |
299 | ||
6dc6fda6 VZ |
300 | #endif // wxUSE_GUI |
301 | ||
cd6ce4a9 VZ |
302 | // ---------------------------------------------------------------------------- |
303 | // wxStream classes to support IO redirection in wxExecute | |
304 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 305 | |
cd6ce4a9 VZ |
306 | class wxProcessFileInputStream : public wxInputStream |
307 | { | |
308 | public: | |
309 | wxProcessFileInputStream(int fd) { m_fd = fd; } | |
310 | ~wxProcessFileInputStream() { close(m_fd); } | |
8b33ae2d | 311 | |
cd6ce4a9 | 312 | virtual bool Eof() const; |
8b33ae2d | 313 | |
cd6ce4a9 | 314 | protected: |
8b33ae2d GL |
315 | size_t OnSysRead(void *buffer, size_t bufsize); |
316 | ||
cd6ce4a9 | 317 | protected: |
8b33ae2d GL |
318 | int m_fd; |
319 | }; | |
320 | ||
cd6ce4a9 VZ |
321 | class wxProcessFileOutputStream : public wxOutputStream |
322 | { | |
323 | public: | |
324 | wxProcessFileOutputStream(int fd) { m_fd = fd; } | |
325 | ~wxProcessFileOutputStream() { close(m_fd); } | |
8b33ae2d | 326 | |
cd6ce4a9 | 327 | protected: |
8b33ae2d GL |
328 | size_t OnSysWrite(const void *buffer, size_t bufsize); |
329 | ||
cd6ce4a9 | 330 | protected: |
8b33ae2d GL |
331 | int m_fd; |
332 | }; | |
333 | ||
cd6ce4a9 | 334 | bool wxProcessFileInputStream::Eof() const |
8b33ae2d | 335 | { |
cd6ce4a9 VZ |
336 | if ( m_lasterror == wxSTREAM_EOF ) |
337 | return TRUE; | |
338 | ||
339 | // check if there is any input available | |
340 | struct timeval tv; | |
341 | tv.tv_sec = 0; | |
342 | tv.tv_usec = 0; | |
343 | ||
344 | fd_set readfds; | |
345 | FD_ZERO(&readfds); | |
346 | FD_SET(m_fd, &readfds); | |
347 | switch ( select(m_fd + 1, &readfds, NULL, NULL, &tv) ) | |
348 | { | |
349 | case -1: | |
350 | wxLogSysError(_("Impossible to get child process input")); | |
351 | // fall through | |
8b33ae2d | 352 | |
cd6ce4a9 VZ |
353 | case 0: |
354 | return TRUE; | |
8b33ae2d | 355 | |
cd6ce4a9 VZ |
356 | default: |
357 | wxFAIL_MSG(_T("unexpected select() return value")); | |
358 | // still fall through | |
359 | ||
360 | case 1: | |
361 | // input available: check if there is any | |
362 | return wxInputStream::Eof(); | |
8b33ae2d | 363 | } |
8b33ae2d GL |
364 | } |
365 | ||
cd6ce4a9 | 366 | size_t wxProcessFileInputStream::OnSysRead(void *buffer, size_t bufsize) |
8b33ae2d | 367 | { |
cd6ce4a9 VZ |
368 | int ret = read(m_fd, buffer, bufsize); |
369 | if ( ret == 0 ) | |
370 | { | |
371 | m_lasterror = wxSTREAM_EOF; | |
372 | } | |
373 | else if ( ret == -1 ) | |
374 | { | |
375 | m_lasterror = wxSTREAM_READ_ERROR; | |
376 | ret = 0; | |
377 | } | |
378 | else | |
379 | { | |
380 | m_lasterror = wxSTREAM_NOERROR; | |
381 | } | |
8b33ae2d | 382 | |
cd6ce4a9 | 383 | return ret; |
8b33ae2d GL |
384 | } |
385 | ||
386 | size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer, size_t bufsize) | |
387 | { | |
cd6ce4a9 VZ |
388 | int ret = write(m_fd, buffer, bufsize); |
389 | if ( ret == -1 ) | |
390 | { | |
391 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
392 | ret = 0; | |
8b33ae2d | 393 | } |
cd6ce4a9 VZ |
394 | else |
395 | { | |
396 | m_lasterror = wxSTREAM_NOERROR; | |
397 | } | |
398 | ||
8b33ae2d GL |
399 | return ret; |
400 | } | |
401 | ||
6dc6fda6 VZ |
402 | long wxExecute(wxChar **argv, |
403 | bool sync, | |
cd6ce4a9 | 404 | wxProcess *process) |
518b5d2f | 405 | { |
f6bcfd97 BP |
406 | // for the sync execution, we return -1 to indicate failure, but for async |
407 | // cse we return 0 which is never a valid PID | |
408 | long errorRetCode = sync ? -1 : 0; | |
409 | ||
410 | wxCHECK_MSG( *argv, errorRetCode, wxT("can't exec empty command") ); | |
518b5d2f | 411 | |
05079acc OK |
412 | #if wxUSE_UNICODE |
413 | int mb_argc = 0; | |
414 | char *mb_argv[WXEXECUTE_NARGS]; | |
415 | ||
e90c1d2a VZ |
416 | while (argv[mb_argc]) |
417 | { | |
cd6ce4a9 VZ |
418 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
419 | mb_argv[mb_argc] = strdup(mb_arg); | |
420 | mb_argc++; | |
05079acc OK |
421 | } |
422 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
423 | |
424 | // this macro will free memory we used above | |
425 | #define ARGS_CLEANUP \ | |
345b0247 | 426 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
427 | free(mb_argv[mb_argc]) |
428 | #else // ANSI | |
429 | // no need for cleanup | |
430 | #define ARGS_CLEANUP | |
431 | ||
05079acc | 432 | wxChar **mb_argv = argv; |
e90c1d2a | 433 | #endif // Unicode/ANSI |
518b5d2f | 434 | |
e90c1d2a | 435 | #if wxUSE_GUI |
518b5d2f | 436 | // create pipes |
e90c1d2a | 437 | int end_proc_detect[2]; |
cd6ce4a9 | 438 | if ( pipe(end_proc_detect) == -1 ) |
518b5d2f VZ |
439 | { |
440 | wxLogSysError( _("Pipe creation failed") ); | |
cd6ce4a9 | 441 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
442 | |
443 | ARGS_CLEANUP; | |
444 | ||
f6bcfd97 | 445 | return errorRetCode; |
518b5d2f | 446 | } |
e90c1d2a | 447 | #endif // wxUSE_GUI |
518b5d2f | 448 | |
f6bcfd97 BP |
449 | // pipes for inter process communication |
450 | int pipeIn[2], // stdin | |
451 | pipeOut[2], // stdout | |
452 | pipeErr[2]; // stderr | |
453 | ||
cd6ce4a9 | 454 | pipeIn[0] = pipeIn[1] = |
f6bcfd97 BP |
455 | pipeOut[0] = pipeOut[1] = |
456 | pipeErr[0] = pipeErr[1] = -1; | |
cd6ce4a9 VZ |
457 | |
458 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 459 | { |
f6bcfd97 | 460 | if ( pipe(pipeIn) == -1 || pipe(pipeOut) == -1 || pipe(pipeErr) == -1 ) |
8b33ae2d | 461 | { |
cd6ce4a9 VZ |
462 | #if wxUSE_GUI |
463 | // free previously allocated resources | |
8b33ae2d GL |
464 | close(end_proc_detect[0]); |
465 | close(end_proc_detect[1]); | |
cd6ce4a9 VZ |
466 | #endif // wxUSE_GUI |
467 | ||
468 | wxLogSysError( _("Pipe creation failed") ); | |
469 | wxLogError( _("Failed to execute '%s'\n"), *argv ); | |
8b33ae2d GL |
470 | |
471 | ARGS_CLEANUP; | |
472 | ||
f6bcfd97 | 473 | return errorRetCode; |
8b33ae2d GL |
474 | } |
475 | } | |
8b33ae2d | 476 | |
518b5d2f | 477 | // fork the process |
0fcdf6dc | 478 | #ifdef HAVE_VFORK |
518b5d2f VZ |
479 | pid_t pid = vfork(); |
480 | #else | |
481 | pid_t pid = fork(); | |
482 | #endif | |
cd6ce4a9 VZ |
483 | |
484 | if ( pid == -1 ) // error? | |
518b5d2f | 485 | { |
8b33ae2d GL |
486 | #if wxUSE_GUI |
487 | close(end_proc_detect[0]); | |
488 | close(end_proc_detect[1]); | |
cd6ce4a9 VZ |
489 | close(pipeIn[0]); |
490 | close(pipeIn[1]); | |
491 | close(pipeOut[0]); | |
492 | close(pipeOut[1]); | |
f6bcfd97 BP |
493 | close(pipeErr[0]); |
494 | close(pipeErr[1]); | |
cd6ce4a9 VZ |
495 | #endif // wxUSE_GUI |
496 | ||
518b5d2f | 497 | wxLogSysError( _("Fork failed") ); |
e90c1d2a VZ |
498 | |
499 | ARGS_CLEANUP; | |
500 | ||
f6bcfd97 | 501 | return errorRetCode; |
518b5d2f | 502 | } |
cd6ce4a9 | 503 | else if ( pid == 0 ) // we're in child |
518b5d2f | 504 | { |
e90c1d2a | 505 | #if wxUSE_GUI |
518b5d2f | 506 | close(end_proc_detect[0]); // close reading side |
e90c1d2a | 507 | #endif // wxUSE_GUI |
518b5d2f | 508 | |
cd6ce4a9 | 509 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 510 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
511 | // one wants proper IO for the subprocess, the right thing to do is to |
512 | // start an xterm executing it. | |
513 | if ( !sync ) | |
518b5d2f | 514 | { |
518b5d2f VZ |
515 | for ( int fd = 0; fd < FD_SETSIZE; fd++ ) |
516 | { | |
f6bcfd97 | 517 | if ( fd == pipeIn[0] || fd == pipeOut[1] || fd == pipeErr[1] |
e90c1d2a | 518 | #if wxUSE_GUI |
cd6ce4a9 | 519 | || fd == end_proc_detect[1] |
e90c1d2a | 520 | #endif // wxUSE_GUI |
cd6ce4a9 VZ |
521 | ) |
522 | { | |
523 | // don't close this one, we still need it | |
524 | continue; | |
525 | } | |
e90c1d2a | 526 | |
cd6ce4a9 | 527 | // leave stderr opened too, it won't do any hurm |
e90c1d2a | 528 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
529 | close(fd); |
530 | } | |
531 | } | |
532 | ||
f6bcfd97 | 533 | // redirect stdio, stdout and stderr |
cd6ce4a9 VZ |
534 | if ( pipeIn[0] != -1 ) |
535 | { | |
536 | if ( dup2(pipeIn[0], STDIN_FILENO) == -1 || | |
f6bcfd97 BP |
537 | dup2(pipeOut[1], STDOUT_FILENO) == -1 || |
538 | dup2(pipeErr[1], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 539 | { |
f6bcfd97 | 540 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 541 | } |
518b5d2f | 542 | |
cd6ce4a9 VZ |
543 | close(pipeIn[0]); |
544 | close(pipeOut[1]); | |
f6bcfd97 | 545 | close(pipeErr[1]); |
cd6ce4a9 | 546 | } |
518b5d2f | 547 | |
05079acc | 548 | execvp (*mb_argv, mb_argv); |
518b5d2f VZ |
549 | |
550 | // there is no return after successful exec() | |
518b5d2f VZ |
551 | _exit(-1); |
552 | } | |
cd6ce4a9 | 553 | else // we're in parent |
518b5d2f | 554 | { |
cd6ce4a9 VZ |
555 | ARGS_CLEANUP; |
556 | ||
557 | // pipe initialization: construction of the wxStreams | |
558 | if ( process && process->IsRedirected() ) | |
559 | { | |
560 | // These two streams are relative to this process. | |
561 | wxOutputStream *outStream = new wxProcessFileOutputStream(pipeIn[1]); | |
562 | wxInputStream *inStream = new wxProcessFileInputStream(pipeOut[0]); | |
f6bcfd97 BP |
563 | wxInputStream *errStream = new wxProcessFileInputStream(pipeErr[0]); |
564 | ||
cd6ce4a9 VZ |
565 | close(pipeIn[0]); // close reading side |
566 | close(pipeOut[1]); // close writing side | |
f6bcfd97 | 567 | close(pipeErr[1]); // close writing side |
cd6ce4a9 | 568 | |
f6bcfd97 | 569 | process->SetPipeStreams(inStream, outStream, errStream); |
cd6ce4a9 VZ |
570 | } |
571 | ||
e90c1d2a | 572 | #if wxUSE_GUI |
518b5d2f | 573 | wxEndProcessData *data = new wxEndProcessData; |
ab857a4e | 574 | |
518b5d2f VZ |
575 | if ( sync ) |
576 | { | |
cd6ce4a9 VZ |
577 | // we may have process for capturing the program output, but it's |
578 | // not used in wxEndProcessData in the case of sync execution | |
518b5d2f VZ |
579 | data->process = NULL; |
580 | ||
581 | // sync execution: indicate it by negating the pid | |
cd6ce4a9 VZ |
582 | data->pid = -pid; |
583 | data->tag = wxAddProcessCallback(data, end_proc_detect[0]); | |
584 | ||
ab857a4e | 585 | close(end_proc_detect[1]); // close writing side |
518b5d2f | 586 | |
cd6ce4a9 VZ |
587 | wxBusyCursor bc; |
588 | wxWindowDisabler wd; | |
589 | ||
518b5d2f VZ |
590 | // it will be set to 0 from GTK_EndProcessDetector |
591 | while (data->pid != 0) | |
592 | wxYield(); | |
593 | ||
594 | int exitcode = data->exitcode; | |
595 | ||
596 | delete data; | |
597 | ||
598 | return exitcode; | |
599 | } | |
cd6ce4a9 | 600 | else // async execution |
518b5d2f VZ |
601 | { |
602 | // async execution, nothing special to do - caller will be | |
ab857a4e | 603 | // notified about the process termination if process != NULL, data |
518b5d2f | 604 | // will be deleted in GTK_EndProcessDetector |
8b33ae2d GL |
605 | data->process = process; |
606 | data->pid = pid; | |
607 | data->tag = wxAddProcessCallback(data, end_proc_detect[0]); | |
cd6ce4a9 | 608 | |
ab857a4e | 609 | close(end_proc_detect[1]); // close writing side |
518b5d2f VZ |
610 | |
611 | return pid; | |
612 | } | |
e90c1d2a | 613 | #else // !wxUSE_GUI |
223d09f6 | 614 | wxASSERT_MSG( sync, wxT("async execution not supported yet") ); |
e90c1d2a VZ |
615 | |
616 | int exitcode = 0; | |
617 | if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) ) | |
618 | { | |
619 | wxLogSysError(_("Waiting for subprocess termination failed")); | |
620 | } | |
621 | ||
622 | return exitcode; | |
623 | #endif // wxUSE_GUI | |
518b5d2f VZ |
624 | } |
625 | } | |
626 | ||
f6bcfd97 BP |
627 | #undef ARGS_CLEANUP |
628 | ||
518b5d2f VZ |
629 | // ---------------------------------------------------------------------------- |
630 | // file and directory functions | |
631 | // ---------------------------------------------------------------------------- | |
632 | ||
05079acc | 633 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f VZ |
634 | { |
635 | *home = wxGetUserHome( wxString() ); | |
636 | if ( home->IsEmpty() ) | |
223d09f6 | 637 | *home = wxT("/"); |
518b5d2f VZ |
638 | |
639 | return home->c_str(); | |
640 | } | |
641 | ||
05079acc OK |
642 | #if wxUSE_UNICODE |
643 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 644 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 645 | char *wxGetUserHome( const wxString &user ) |
05079acc | 646 | #endif |
518b5d2f VZ |
647 | { |
648 | struct passwd *who = (struct passwd *) NULL; | |
649 | ||
0fb67cd1 | 650 | if ( !user ) |
518b5d2f | 651 | { |
e90c1d2a | 652 | wxChar *ptr; |
518b5d2f | 653 | |
223d09f6 | 654 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f VZ |
655 | { |
656 | return ptr; | |
657 | } | |
223d09f6 | 658 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 659 | { |
e90c1d2a | 660 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
661 | } |
662 | ||
663 | // We now make sure the the user exists! | |
664 | if (who == NULL) | |
665 | { | |
666 | who = getpwuid(getuid()); | |
667 | } | |
668 | } | |
669 | else | |
670 | { | |
05079acc | 671 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
672 | } |
673 | ||
af111fc3 | 674 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
675 | } |
676 | ||
677 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 678 | // network and user id routines |
518b5d2f VZ |
679 | // ---------------------------------------------------------------------------- |
680 | ||
0fb67cd1 VZ |
681 | // retrieve either the hostname or FQDN depending on platform (caller must |
682 | // check whether it's one or the other, this is why this function is for | |
683 | // private use only) | |
05079acc | 684 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 685 | { |
223d09f6 | 686 | wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 687 | |
223d09f6 | 688 | *buf = wxT('\0'); |
518b5d2f VZ |
689 | |
690 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
691 | #if defined(HAVE_UNAME) | |
cc743a6f | 692 | struct utsname uts; |
518b5d2f VZ |
693 | bool ok = uname(&uts) != -1; |
694 | if ( ok ) | |
695 | { | |
e90c1d2a | 696 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 697 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
698 | } |
699 | #elif defined(HAVE_GETHOSTNAME) | |
700 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 701 | #else // no uname, no gethostname |
223d09f6 | 702 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f VZ |
703 | |
704 | bool ok = FALSE; | |
0fb67cd1 | 705 | #endif // uname/gethostname |
518b5d2f VZ |
706 | |
707 | if ( !ok ) | |
708 | { | |
709 | wxLogSysError(_("Cannot get the hostname")); | |
710 | } | |
711 | ||
712 | return ok; | |
713 | } | |
714 | ||
05079acc | 715 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
716 | { |
717 | bool ok = wxGetHostNameInternal(buf, sz); | |
718 | ||
719 | if ( ok ) | |
720 | { | |
721 | // BSD systems return the FQDN, we only want the hostname, so extract | |
722 | // it (we consider that dots are domain separators) | |
223d09f6 | 723 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
724 | if ( dot ) |
725 | { | |
726 | // nuke it | |
223d09f6 | 727 | *dot = wxT('\0'); |
0fb67cd1 VZ |
728 | } |
729 | } | |
730 | ||
731 | return ok; | |
732 | } | |
733 | ||
05079acc | 734 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
735 | { |
736 | bool ok = wxGetHostNameInternal(buf, sz); | |
737 | ||
738 | if ( ok ) | |
739 | { | |
223d09f6 | 740 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 741 | { |
e90c1d2a | 742 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
743 | if ( !host ) |
744 | { | |
745 | wxLogSysError(_("Cannot get the official hostname")); | |
746 | ||
747 | ok = FALSE; | |
748 | } | |
749 | else | |
750 | { | |
751 | // the canonical name | |
e90c1d2a | 752 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
753 | } |
754 | } | |
755 | //else: it's already a FQDN (BSD behaves this way) | |
756 | } | |
757 | ||
758 | return ok; | |
759 | } | |
760 | ||
05079acc | 761 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
762 | { |
763 | struct passwd *who; | |
764 | ||
223d09f6 | 765 | *buf = wxT('\0'); |
518b5d2f VZ |
766 | if ((who = getpwuid(getuid ())) != NULL) |
767 | { | |
e90c1d2a | 768 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
518b5d2f VZ |
769 | return TRUE; |
770 | } | |
771 | ||
772 | return FALSE; | |
773 | } | |
774 | ||
05079acc | 775 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
776 | { |
777 | struct passwd *who; | |
518b5d2f | 778 | |
223d09f6 | 779 | *buf = wxT('\0'); |
b12915c1 VZ |
780 | if ((who = getpwuid (getuid ())) != NULL) |
781 | { | |
782 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 783 | #ifdef HAVE_PW_GECOS |
b12915c1 | 784 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
785 | if (comma) |
786 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 787 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 788 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 789 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 790 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
518b5d2f VZ |
791 | return TRUE; |
792 | } | |
793 | ||
794 | return FALSE; | |
795 | } | |
796 | ||
bdc72a22 VZ |
797 | wxString wxGetOsDescription() |
798 | { | |
799 | #ifndef WXWIN_OS_DESCRIPTION | |
800 | #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure | |
801 | #else | |
802 | return WXWIN_OS_DESCRIPTION; | |
803 | #endif | |
804 | } | |
805 | ||
bd3277fe VZ |
806 | // this function returns the GUI toolkit version in GUI programs, but OS |
807 | // version in non-GUI ones | |
808 | #if !wxUSE_GUI | |
809 | ||
810 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
811 | { | |
812 | int major, minor; | |
813 | char name[256]; | |
814 | ||
815 | if ( sscanf(WXWIN_OS_DESCRIPTION, "%s %d.%d", name, &major, &minor) != 3 ) | |
816 | { | |
817 | // unreckognized uname string format | |
818 | major = minor = -1; | |
819 | } | |
820 | ||
821 | if ( majorVsn ) | |
822 | *majorVsn = major; | |
823 | if ( minorVsn ) | |
824 | *minorVsn = minor; | |
825 | ||
826 | return wxUNIX; | |
827 | } | |
828 | ||
829 | #endif // !wxUSE_GUI | |
830 | ||
831 | long wxGetFreeMemory() | |
832 | { | |
833 | #if defined(__LINUX__) | |
834 | // get it from /proc/meminfo | |
835 | FILE *fp = fopen("/proc/meminfo", "r"); | |
836 | if ( fp ) | |
837 | { | |
838 | long memFree = -1; | |
839 | ||
840 | char buf[1024]; | |
841 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
842 | { | |
843 | long memTotal, memUsed; | |
844 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
845 | } | |
846 | ||
847 | fclose(fp); | |
848 | ||
849 | return memFree; | |
850 | } | |
851 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
852 | return sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE); | |
853 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably | |
854 | #endif | |
855 | ||
856 | // can't find it out | |
857 | return -1; | |
858 | } | |
859 | ||
a37a5a73 VZ |
860 | // ---------------------------------------------------------------------------- |
861 | // signal handling | |
862 | // ---------------------------------------------------------------------------- | |
863 | ||
864 | #if wxUSE_ON_FATAL_EXCEPTION | |
865 | ||
866 | #include <signal.h> | |
867 | ||
f6bcfd97 | 868 | static void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
869 | { |
870 | if ( wxTheApp ) | |
871 | { | |
872 | // give the user a chance to do something special about this | |
873 | wxTheApp->OnFatalException(); | |
874 | } | |
875 | ||
876 | abort(); | |
877 | } | |
878 | ||
879 | bool wxHandleFatalExceptions(bool doit) | |
880 | { | |
881 | // old sig handlers | |
882 | static bool s_savedHandlers = FALSE; | |
883 | static struct sigaction s_handlerFPE, | |
884 | s_handlerILL, | |
885 | s_handlerBUS, | |
886 | s_handlerSEGV; | |
887 | ||
888 | bool ok = TRUE; | |
889 | if ( doit && !s_savedHandlers ) | |
890 | { | |
891 | // install the signal handler | |
892 | struct sigaction act; | |
893 | ||
894 | // some systems extend it with non std fields, so zero everything | |
895 | memset(&act, 0, sizeof(act)); | |
896 | ||
897 | act.sa_handler = wxFatalSignalHandler; | |
898 | sigemptyset(&act.sa_mask); | |
899 | act.sa_flags = 0; | |
900 | ||
901 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
902 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
903 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
904 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
905 | if ( !ok ) | |
906 | { | |
907 | wxLogDebug(_T("Failed to install our signal handler.")); | |
908 | } | |
909 | ||
910 | s_savedHandlers = TRUE; | |
911 | } | |
912 | else if ( s_savedHandlers ) | |
913 | { | |
914 | // uninstall the signal handler | |
915 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
916 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
917 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
918 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
919 | if ( !ok ) | |
920 | { | |
921 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
922 | } | |
923 | ||
924 | s_savedHandlers = FALSE; | |
925 | } | |
926 | //else: nothing to do | |
927 | ||
928 | return ok; | |
929 | } | |
930 | ||
931 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
932 | ||
518b5d2f VZ |
933 | // ---------------------------------------------------------------------------- |
934 | // error and debug output routines (deprecated, use wxLog) | |
935 | // ---------------------------------------------------------------------------- | |
936 | ||
937 | void wxDebugMsg( const char *format, ... ) | |
938 | { | |
939 | va_list ap; | |
940 | va_start( ap, format ); | |
941 | vfprintf( stderr, format, ap ); | |
942 | fflush( stderr ); | |
943 | va_end(ap); | |
944 | } | |
945 | ||
946 | void wxError( const wxString &msg, const wxString &title ) | |
947 | { | |
05079acc | 948 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
949 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
950 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
951 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
952 | } |
953 | ||
954 | void wxFatalError( const wxString &msg, const wxString &title ) | |
955 | { | |
05079acc | 956 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
957 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
958 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
959 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
960 | exit(3); // the same exit code as for abort() |
961 | } | |
93ccaed8 | 962 |