]>
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 | ||
50567b69 | 138 | int wxKill(long pid, wxSignal sig, wxKillError *rc) |
518b5d2f | 139 | { |
50567b69 VZ |
140 | int err = kill((pid_t)pid, (int)sig); |
141 | if ( rc ) | |
142 | { | |
143 | switch ( err ) | |
144 | { | |
145 | case 0: | |
146 | *rc = wxKILL_OK; | |
147 | break; | |
148 | ||
149 | case EINVAL: | |
150 | *rc = wxKILL_BAD_SIGNAL; | |
151 | break; | |
152 | ||
153 | case EPERM: | |
154 | *rc = wxKILL_ACCESS_DENIED; | |
155 | break; | |
156 | ||
157 | case ESRCH: | |
158 | *rc = wxKILL_NO_PROCESS; | |
159 | break; | |
160 | ||
161 | default: | |
162 | // this goes against Unix98 docs so log it | |
163 | wxLogDebug(_T("unexpected kill(2) return value %d"), err); | |
164 | ||
165 | // something else... | |
166 | *rc = wxKILL_ERROR; | |
167 | } | |
168 | } | |
169 | ||
170 | return err; | |
518b5d2f VZ |
171 | } |
172 | ||
fad866f4 KB |
173 | #define WXEXECUTE_NARGS 127 |
174 | ||
518b5d2f VZ |
175 | long wxExecute( const wxString& command, bool sync, wxProcess *process ) |
176 | { | |
223d09f6 | 177 | wxCHECK_MSG( !command.IsEmpty(), 0, wxT("can't exec empty command") ); |
518b5d2f VZ |
178 | |
179 | int argc = 0; | |
05079acc | 180 | wxChar *argv[WXEXECUTE_NARGS]; |
fad866f4 | 181 | wxString argument; |
05079acc | 182 | const wxChar *cptr = command.c_str(); |
223d09f6 | 183 | wxChar quotechar = wxT('\0'); // is arg quoted? |
fad866f4 | 184 | bool escaped = FALSE; |
518b5d2f | 185 | |
0ed9a934 | 186 | // split the command line in arguments |
fad866f4 KB |
187 | do |
188 | { | |
223d09f6 KB |
189 | argument=wxT(""); |
190 | quotechar = wxT('\0'); | |
0ed9a934 | 191 | |
fad866f4 | 192 | // eat leading whitespace: |
05079acc | 193 | while ( wxIsspace(*cptr) ) |
fad866f4 | 194 | cptr++; |
0ed9a934 | 195 | |
223d09f6 | 196 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) |
fad866f4 | 197 | quotechar = *cptr++; |
0ed9a934 | 198 | |
fad866f4 KB |
199 | do |
200 | { | |
223d09f6 | 201 | if ( *cptr == wxT('\\') && ! escaped ) |
fad866f4 KB |
202 | { |
203 | escaped = TRUE; | |
204 | cptr++; | |
205 | continue; | |
206 | } | |
0ed9a934 | 207 | |
fad866f4 | 208 | // all other characters: |
0ed9a934 | 209 | argument += *cptr++; |
fad866f4 | 210 | escaped = FALSE; |
0ed9a934 VZ |
211 | |
212 | // have we reached the end of the argument? | |
213 | if ( (*cptr == quotechar && ! escaped) | |
223d09f6 KB |
214 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) |
215 | || *cptr == wxT('\0') ) | |
fad866f4 | 216 | { |
0ed9a934 | 217 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, |
223d09f6 | 218 | wxT("too many arguments in wxExecute") ); |
0ed9a934 | 219 | |
05079acc OK |
220 | argv[argc] = new wxChar[argument.length() + 1]; |
221 | wxStrcpy(argv[argc], argument.c_str()); | |
fad866f4 | 222 | argc++; |
0ed9a934 | 223 | |
fad866f4 | 224 | // if not at end of buffer, swallow last character: |
0ed9a934 VZ |
225 | if(*cptr) |
226 | cptr++; | |
227 | ||
fad866f4 KB |
228 | break; // done with this one, start over |
229 | } | |
0ed9a934 VZ |
230 | } while(*cptr); |
231 | } while(*cptr); | |
fad866f4 | 232 | argv[argc] = NULL; |
0ed9a934 VZ |
233 | |
234 | // do execute the command | |
518b5d2f VZ |
235 | long lRc = wxExecute(argv, sync, process); |
236 | ||
0ed9a934 | 237 | // clean up |
fad866f4 | 238 | argc = 0; |
0ed9a934 | 239 | while( argv[argc] ) |
fad866f4 | 240 | delete [] argv[argc++]; |
518b5d2f VZ |
241 | |
242 | return lRc; | |
243 | } | |
244 | ||
2c8e4738 VZ |
245 | // ---------------------------------------------------------------------------- |
246 | // wxShell | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
249 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
250 | { |
251 | wxString cmd; | |
cd6ce4a9 | 252 | if ( !command ) |
2c8e4738 VZ |
253 | { |
254 | // just an interactive shell | |
cd6ce4a9 | 255 | cmd = _T("xterm"); |
2c8e4738 | 256 | } |
518b5d2f | 257 | else |
2c8e4738 VZ |
258 | { |
259 | // execute command in a shell | |
260 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
261 | } | |
262 | ||
263 | return cmd; | |
264 | } | |
265 | ||
266 | bool wxShell(const wxString& command) | |
267 | { | |
268 | return wxExecute(wxMakeShellCommand(command), TRUE /* sync */) == 0; | |
269 | } | |
270 | ||
271 | bool wxShell(const wxString& command, wxArrayString& output) | |
272 | { | |
273 | wxCHECK_MSG( !!command, FALSE, _T("can't exec shell non interactively") ); | |
518b5d2f | 274 | |
2c8e4738 | 275 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
276 | } |
277 | ||
6dc6fda6 VZ |
278 | #if wxUSE_GUI |
279 | ||
518b5d2f VZ |
280 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
281 | { | |
282 | int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid); | |
283 | ||
0ed9a934 VZ |
284 | // waitpid is POSIX so should be available everywhere, however on older |
285 | // systems wait() might be used instead in a loop (until the right pid | |
286 | // terminates) | |
518b5d2f | 287 | int status = 0; |
ab857a4e KB |
288 | int rc; |
289 | ||
bdc72a22 | 290 | // wait for child termination and if waitpid() was interrupted, try again |
ab857a4e | 291 | do |
bdc72a22 | 292 | { |
ab857a4e | 293 | rc = waitpid(pid, &status, 0); |
bdc72a22 VZ |
294 | } |
295 | while ( rc == -1 && errno == EINTR ); | |
296 | ||
ab857a4e | 297 | |
ab857a4e | 298 | if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) ) |
0ed9a934 | 299 | { |
ab857a4e KB |
300 | wxLogSysError(_("Waiting for subprocess termination failed")); |
301 | /* AFAIK, this can only happen if something went wrong within | |
bdc72a22 | 302 | wxGTK, i.e. due to a race condition or some serious bug. |
ab857a4e KB |
303 | After having fixed the order of statements in |
304 | GTK_EndProcessDetector(). (KB) | |
305 | */ | |
0ed9a934 VZ |
306 | } |
307 | else | |
308 | { | |
309 | // notify user about termination if required | |
310 | if (proc_data->process) | |
311 | { | |
312 | proc_data->process->OnTerminate(proc_data->pid, | |
313 | WEXITSTATUS(status)); | |
314 | } | |
ab857a4e KB |
315 | // clean up |
316 | if ( proc_data->pid > 0 ) | |
317 | { | |
318 | delete proc_data; | |
319 | } | |
320 | else | |
321 | { | |
322 | // wxExecute() will know about it | |
323 | proc_data->exitcode = status; | |
bdc72a22 | 324 | |
ab857a4e KB |
325 | proc_data->pid = 0; |
326 | } | |
518b5d2f VZ |
327 | } |
328 | } | |
329 | ||
6dc6fda6 VZ |
330 | #endif // wxUSE_GUI |
331 | ||
cd6ce4a9 VZ |
332 | // ---------------------------------------------------------------------------- |
333 | // wxStream classes to support IO redirection in wxExecute | |
334 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 335 | |
cd6ce4a9 VZ |
336 | class wxProcessFileInputStream : public wxInputStream |
337 | { | |
338 | public: | |
339 | wxProcessFileInputStream(int fd) { m_fd = fd; } | |
340 | ~wxProcessFileInputStream() { close(m_fd); } | |
8b33ae2d | 341 | |
cd6ce4a9 | 342 | virtual bool Eof() const; |
8b33ae2d | 343 | |
cd6ce4a9 | 344 | protected: |
8b33ae2d GL |
345 | size_t OnSysRead(void *buffer, size_t bufsize); |
346 | ||
cd6ce4a9 | 347 | protected: |
8b33ae2d GL |
348 | int m_fd; |
349 | }; | |
350 | ||
cd6ce4a9 VZ |
351 | class wxProcessFileOutputStream : public wxOutputStream |
352 | { | |
353 | public: | |
354 | wxProcessFileOutputStream(int fd) { m_fd = fd; } | |
355 | ~wxProcessFileOutputStream() { close(m_fd); } | |
8b33ae2d | 356 | |
cd6ce4a9 | 357 | protected: |
8b33ae2d GL |
358 | size_t OnSysWrite(const void *buffer, size_t bufsize); |
359 | ||
cd6ce4a9 | 360 | protected: |
8b33ae2d GL |
361 | int m_fd; |
362 | }; | |
363 | ||
cd6ce4a9 | 364 | bool wxProcessFileInputStream::Eof() const |
8b33ae2d | 365 | { |
cd6ce4a9 VZ |
366 | if ( m_lasterror == wxSTREAM_EOF ) |
367 | return TRUE; | |
368 | ||
369 | // check if there is any input available | |
370 | struct timeval tv; | |
371 | tv.tv_sec = 0; | |
372 | tv.tv_usec = 0; | |
373 | ||
374 | fd_set readfds; | |
375 | FD_ZERO(&readfds); | |
376 | FD_SET(m_fd, &readfds); | |
377 | switch ( select(m_fd + 1, &readfds, NULL, NULL, &tv) ) | |
378 | { | |
379 | case -1: | |
380 | wxLogSysError(_("Impossible to get child process input")); | |
381 | // fall through | |
8b33ae2d | 382 | |
cd6ce4a9 VZ |
383 | case 0: |
384 | return TRUE; | |
8b33ae2d | 385 | |
cd6ce4a9 VZ |
386 | default: |
387 | wxFAIL_MSG(_T("unexpected select() return value")); | |
388 | // still fall through | |
389 | ||
390 | case 1: | |
391 | // input available: check if there is any | |
392 | return wxInputStream::Eof(); | |
8b33ae2d | 393 | } |
8b33ae2d GL |
394 | } |
395 | ||
cd6ce4a9 | 396 | size_t wxProcessFileInputStream::OnSysRead(void *buffer, size_t bufsize) |
8b33ae2d | 397 | { |
cd6ce4a9 VZ |
398 | int ret = read(m_fd, buffer, bufsize); |
399 | if ( ret == 0 ) | |
400 | { | |
401 | m_lasterror = wxSTREAM_EOF; | |
402 | } | |
403 | else if ( ret == -1 ) | |
404 | { | |
405 | m_lasterror = wxSTREAM_READ_ERROR; | |
406 | ret = 0; | |
407 | } | |
408 | else | |
409 | { | |
410 | m_lasterror = wxSTREAM_NOERROR; | |
411 | } | |
8b33ae2d | 412 | |
cd6ce4a9 | 413 | return ret; |
8b33ae2d GL |
414 | } |
415 | ||
416 | size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer, size_t bufsize) | |
417 | { | |
cd6ce4a9 VZ |
418 | int ret = write(m_fd, buffer, bufsize); |
419 | if ( ret == -1 ) | |
420 | { | |
421 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
422 | ret = 0; | |
8b33ae2d | 423 | } |
cd6ce4a9 VZ |
424 | else |
425 | { | |
426 | m_lasterror = wxSTREAM_NOERROR; | |
427 | } | |
428 | ||
8b33ae2d GL |
429 | return ret; |
430 | } | |
431 | ||
6dc6fda6 VZ |
432 | long wxExecute(wxChar **argv, |
433 | bool sync, | |
cd6ce4a9 | 434 | wxProcess *process) |
518b5d2f | 435 | { |
f6bcfd97 | 436 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
437 | // case we return 0 which is never a valid PID |
438 | // | |
439 | // we define this as a macro, not a variable, to avoid compiler warnings | |
440 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
441 | #define ERROR_RETURN_CODE ((sync) ? -1 : 0) | |
f6bcfd97 | 442 | |
accb3257 | 443 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 444 | |
05079acc OK |
445 | #if wxUSE_UNICODE |
446 | int mb_argc = 0; | |
447 | char *mb_argv[WXEXECUTE_NARGS]; | |
448 | ||
e90c1d2a VZ |
449 | while (argv[mb_argc]) |
450 | { | |
cd6ce4a9 VZ |
451 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
452 | mb_argv[mb_argc] = strdup(mb_arg); | |
453 | mb_argc++; | |
05079acc OK |
454 | } |
455 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
456 | |
457 | // this macro will free memory we used above | |
458 | #define ARGS_CLEANUP \ | |
345b0247 | 459 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
460 | free(mb_argv[mb_argc]) |
461 | #else // ANSI | |
462 | // no need for cleanup | |
463 | #define ARGS_CLEANUP | |
464 | ||
05079acc | 465 | wxChar **mb_argv = argv; |
e90c1d2a | 466 | #endif // Unicode/ANSI |
518b5d2f | 467 | |
e90c1d2a | 468 | #if wxUSE_GUI |
518b5d2f | 469 | // create pipes |
e90c1d2a | 470 | int end_proc_detect[2]; |
cd6ce4a9 | 471 | if ( pipe(end_proc_detect) == -1 ) |
518b5d2f VZ |
472 | { |
473 | wxLogSysError( _("Pipe creation failed") ); | |
cd6ce4a9 | 474 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
475 | |
476 | ARGS_CLEANUP; | |
477 | ||
accb3257 | 478 | return ERROR_RETURN_CODE; |
518b5d2f | 479 | } |
e90c1d2a | 480 | #endif // wxUSE_GUI |
518b5d2f | 481 | |
f6bcfd97 BP |
482 | // pipes for inter process communication |
483 | int pipeIn[2], // stdin | |
484 | pipeOut[2], // stdout | |
485 | pipeErr[2]; // stderr | |
486 | ||
cd6ce4a9 | 487 | pipeIn[0] = pipeIn[1] = |
f6bcfd97 BP |
488 | pipeOut[0] = pipeOut[1] = |
489 | pipeErr[0] = pipeErr[1] = -1; | |
cd6ce4a9 VZ |
490 | |
491 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 492 | { |
f6bcfd97 | 493 | if ( pipe(pipeIn) == -1 || pipe(pipeOut) == -1 || pipe(pipeErr) == -1 ) |
8b33ae2d | 494 | { |
cd6ce4a9 VZ |
495 | #if wxUSE_GUI |
496 | // free previously allocated resources | |
8b33ae2d GL |
497 | close(end_proc_detect[0]); |
498 | close(end_proc_detect[1]); | |
cd6ce4a9 VZ |
499 | #endif // wxUSE_GUI |
500 | ||
501 | wxLogSysError( _("Pipe creation failed") ); | |
502 | wxLogError( _("Failed to execute '%s'\n"), *argv ); | |
8b33ae2d GL |
503 | |
504 | ARGS_CLEANUP; | |
505 | ||
accb3257 | 506 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
507 | } |
508 | } | |
8b33ae2d | 509 | |
518b5d2f | 510 | // fork the process |
0fcdf6dc | 511 | #ifdef HAVE_VFORK |
518b5d2f VZ |
512 | pid_t pid = vfork(); |
513 | #else | |
514 | pid_t pid = fork(); | |
515 | #endif | |
cd6ce4a9 VZ |
516 | |
517 | if ( pid == -1 ) // error? | |
518b5d2f | 518 | { |
8b33ae2d GL |
519 | #if wxUSE_GUI |
520 | close(end_proc_detect[0]); | |
521 | close(end_proc_detect[1]); | |
cd6ce4a9 VZ |
522 | close(pipeIn[0]); |
523 | close(pipeIn[1]); | |
524 | close(pipeOut[0]); | |
525 | close(pipeOut[1]); | |
f6bcfd97 BP |
526 | close(pipeErr[0]); |
527 | close(pipeErr[1]); | |
cd6ce4a9 VZ |
528 | #endif // wxUSE_GUI |
529 | ||
518b5d2f | 530 | wxLogSysError( _("Fork failed") ); |
e90c1d2a VZ |
531 | |
532 | ARGS_CLEANUP; | |
533 | ||
accb3257 | 534 | return ERROR_RETURN_CODE; |
518b5d2f | 535 | } |
cd6ce4a9 | 536 | else if ( pid == 0 ) // we're in child |
518b5d2f | 537 | { |
e90c1d2a | 538 | #if wxUSE_GUI |
518b5d2f | 539 | close(end_proc_detect[0]); // close reading side |
e90c1d2a | 540 | #endif // wxUSE_GUI |
518b5d2f | 541 | |
cd6ce4a9 | 542 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 543 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
544 | // one wants proper IO for the subprocess, the right thing to do is to |
545 | // start an xterm executing it. | |
546 | if ( !sync ) | |
518b5d2f | 547 | { |
518b5d2f VZ |
548 | for ( int fd = 0; fd < FD_SETSIZE; fd++ ) |
549 | { | |
f6bcfd97 | 550 | if ( fd == pipeIn[0] || fd == pipeOut[1] || fd == pipeErr[1] |
e90c1d2a | 551 | #if wxUSE_GUI |
cd6ce4a9 | 552 | || fd == end_proc_detect[1] |
e90c1d2a | 553 | #endif // wxUSE_GUI |
cd6ce4a9 VZ |
554 | ) |
555 | { | |
556 | // don't close this one, we still need it | |
557 | continue; | |
558 | } | |
e90c1d2a | 559 | |
cd6ce4a9 | 560 | // leave stderr opened too, it won't do any hurm |
e90c1d2a | 561 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
562 | close(fd); |
563 | } | |
564 | } | |
565 | ||
f6bcfd97 | 566 | // redirect stdio, stdout and stderr |
cd6ce4a9 VZ |
567 | if ( pipeIn[0] != -1 ) |
568 | { | |
569 | if ( dup2(pipeIn[0], STDIN_FILENO) == -1 || | |
f6bcfd97 BP |
570 | dup2(pipeOut[1], STDOUT_FILENO) == -1 || |
571 | dup2(pipeErr[1], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 572 | { |
f6bcfd97 | 573 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 574 | } |
518b5d2f | 575 | |
cd6ce4a9 VZ |
576 | close(pipeIn[0]); |
577 | close(pipeOut[1]); | |
f6bcfd97 | 578 | close(pipeErr[1]); |
cd6ce4a9 | 579 | } |
518b5d2f | 580 | |
05079acc | 581 | execvp (*mb_argv, mb_argv); |
518b5d2f VZ |
582 | |
583 | // there is no return after successful exec() | |
518b5d2f VZ |
584 | _exit(-1); |
585 | } | |
cd6ce4a9 | 586 | else // we're in parent |
518b5d2f | 587 | { |
cd6ce4a9 VZ |
588 | ARGS_CLEANUP; |
589 | ||
590 | // pipe initialization: construction of the wxStreams | |
591 | if ( process && process->IsRedirected() ) | |
592 | { | |
593 | // These two streams are relative to this process. | |
594 | wxOutputStream *outStream = new wxProcessFileOutputStream(pipeIn[1]); | |
595 | wxInputStream *inStream = new wxProcessFileInputStream(pipeOut[0]); | |
f6bcfd97 BP |
596 | wxInputStream *errStream = new wxProcessFileInputStream(pipeErr[0]); |
597 | ||
cd6ce4a9 VZ |
598 | close(pipeIn[0]); // close reading side |
599 | close(pipeOut[1]); // close writing side | |
f6bcfd97 | 600 | close(pipeErr[1]); // close writing side |
cd6ce4a9 | 601 | |
f6bcfd97 | 602 | process->SetPipeStreams(inStream, outStream, errStream); |
cd6ce4a9 VZ |
603 | } |
604 | ||
e90c1d2a | 605 | #if wxUSE_GUI |
518b5d2f | 606 | wxEndProcessData *data = new wxEndProcessData; |
ab857a4e | 607 | |
518b5d2f VZ |
608 | if ( sync ) |
609 | { | |
cd6ce4a9 VZ |
610 | // we may have process for capturing the program output, but it's |
611 | // not used in wxEndProcessData in the case of sync execution | |
518b5d2f VZ |
612 | data->process = NULL; |
613 | ||
614 | // sync execution: indicate it by negating the pid | |
cd6ce4a9 VZ |
615 | data->pid = -pid; |
616 | data->tag = wxAddProcessCallback(data, end_proc_detect[0]); | |
617 | ||
ab857a4e | 618 | close(end_proc_detect[1]); // close writing side |
518b5d2f | 619 | |
cd6ce4a9 VZ |
620 | wxBusyCursor bc; |
621 | wxWindowDisabler wd; | |
622 | ||
518b5d2f VZ |
623 | // it will be set to 0 from GTK_EndProcessDetector |
624 | while (data->pid != 0) | |
625 | wxYield(); | |
626 | ||
627 | int exitcode = data->exitcode; | |
628 | ||
629 | delete data; | |
630 | ||
631 | return exitcode; | |
632 | } | |
cd6ce4a9 | 633 | else // async execution |
518b5d2f VZ |
634 | { |
635 | // async execution, nothing special to do - caller will be | |
ab857a4e | 636 | // notified about the process termination if process != NULL, data |
518b5d2f | 637 | // will be deleted in GTK_EndProcessDetector |
8b33ae2d GL |
638 | data->process = process; |
639 | data->pid = pid; | |
640 | data->tag = wxAddProcessCallback(data, end_proc_detect[0]); | |
cd6ce4a9 | 641 | |
ab857a4e | 642 | close(end_proc_detect[1]); // close writing side |
518b5d2f VZ |
643 | |
644 | return pid; | |
645 | } | |
e90c1d2a | 646 | #else // !wxUSE_GUI |
223d09f6 | 647 | wxASSERT_MSG( sync, wxT("async execution not supported yet") ); |
e90c1d2a VZ |
648 | |
649 | int exitcode = 0; | |
650 | if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) ) | |
651 | { | |
652 | wxLogSysError(_("Waiting for subprocess termination failed")); | |
653 | } | |
654 | ||
655 | return exitcode; | |
656 | #endif // wxUSE_GUI | |
518b5d2f | 657 | } |
57681e5b JJ |
658 | #ifdef __VMS |
659 | // VMS does not recognise exit as a return and complains about | |
660 | // a missing return | |
661 | // I think VMS is wrong in this | |
662 | // JJ | |
663 | return 0; | |
664 | #endif | |
518b5d2f VZ |
665 | } |
666 | ||
accb3257 | 667 | #undef ERROR_RETURN_CODE |
f6bcfd97 BP |
668 | #undef ARGS_CLEANUP |
669 | ||
518b5d2f VZ |
670 | // ---------------------------------------------------------------------------- |
671 | // file and directory functions | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
05079acc | 674 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f VZ |
675 | { |
676 | *home = wxGetUserHome( wxString() ); | |
181cbcf4 | 677 | wxString tmp; |
518b5d2f | 678 | if ( home->IsEmpty() ) |
223d09f6 | 679 | *home = wxT("/"); |
181cbcf4 JJ |
680 | #ifdef __VMS |
681 | tmp = *home; | |
682 | if ( tmp.Last() != wxT(']')) | |
683 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
684 | #endif | |
518b5d2f VZ |
685 | return home->c_str(); |
686 | } | |
687 | ||
05079acc OK |
688 | #if wxUSE_UNICODE |
689 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 690 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 691 | char *wxGetUserHome( const wxString &user ) |
05079acc | 692 | #endif |
518b5d2f VZ |
693 | { |
694 | struct passwd *who = (struct passwd *) NULL; | |
695 | ||
0fb67cd1 | 696 | if ( !user ) |
518b5d2f | 697 | { |
e90c1d2a | 698 | wxChar *ptr; |
518b5d2f | 699 | |
223d09f6 | 700 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f VZ |
701 | { |
702 | return ptr; | |
703 | } | |
223d09f6 | 704 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 705 | { |
e90c1d2a | 706 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
707 | } |
708 | ||
709 | // We now make sure the the user exists! | |
710 | if (who == NULL) | |
711 | { | |
712 | who = getpwuid(getuid()); | |
713 | } | |
714 | } | |
715 | else | |
716 | { | |
05079acc | 717 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
718 | } |
719 | ||
af111fc3 | 720 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
721 | } |
722 | ||
723 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 724 | // network and user id routines |
518b5d2f VZ |
725 | // ---------------------------------------------------------------------------- |
726 | ||
0fb67cd1 VZ |
727 | // retrieve either the hostname or FQDN depending on platform (caller must |
728 | // check whether it's one or the other, this is why this function is for | |
729 | // private use only) | |
05079acc | 730 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 731 | { |
223d09f6 | 732 | wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 733 | |
223d09f6 | 734 | *buf = wxT('\0'); |
518b5d2f VZ |
735 | |
736 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
737 | #if defined(HAVE_UNAME) | |
cc743a6f | 738 | struct utsname uts; |
518b5d2f VZ |
739 | bool ok = uname(&uts) != -1; |
740 | if ( ok ) | |
741 | { | |
e90c1d2a | 742 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 743 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
744 | } |
745 | #elif defined(HAVE_GETHOSTNAME) | |
746 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 747 | #else // no uname, no gethostname |
223d09f6 | 748 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f VZ |
749 | |
750 | bool ok = FALSE; | |
0fb67cd1 | 751 | #endif // uname/gethostname |
518b5d2f VZ |
752 | |
753 | if ( !ok ) | |
754 | { | |
755 | wxLogSysError(_("Cannot get the hostname")); | |
756 | } | |
757 | ||
758 | return ok; | |
759 | } | |
760 | ||
05079acc | 761 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
762 | { |
763 | bool ok = wxGetHostNameInternal(buf, sz); | |
764 | ||
765 | if ( ok ) | |
766 | { | |
767 | // BSD systems return the FQDN, we only want the hostname, so extract | |
768 | // it (we consider that dots are domain separators) | |
223d09f6 | 769 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
770 | if ( dot ) |
771 | { | |
772 | // nuke it | |
223d09f6 | 773 | *dot = wxT('\0'); |
0fb67cd1 VZ |
774 | } |
775 | } | |
776 | ||
777 | return ok; | |
778 | } | |
779 | ||
05079acc | 780 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
781 | { |
782 | bool ok = wxGetHostNameInternal(buf, sz); | |
783 | ||
784 | if ( ok ) | |
785 | { | |
223d09f6 | 786 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 787 | { |
e90c1d2a | 788 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
789 | if ( !host ) |
790 | { | |
791 | wxLogSysError(_("Cannot get the official hostname")); | |
792 | ||
793 | ok = FALSE; | |
794 | } | |
795 | else | |
796 | { | |
797 | // the canonical name | |
e90c1d2a | 798 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
799 | } |
800 | } | |
801 | //else: it's already a FQDN (BSD behaves this way) | |
802 | } | |
803 | ||
804 | return ok; | |
805 | } | |
806 | ||
05079acc | 807 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
808 | { |
809 | struct passwd *who; | |
810 | ||
223d09f6 | 811 | *buf = wxT('\0'); |
518b5d2f VZ |
812 | if ((who = getpwuid(getuid ())) != NULL) |
813 | { | |
e90c1d2a | 814 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
518b5d2f VZ |
815 | return TRUE; |
816 | } | |
817 | ||
818 | return FALSE; | |
819 | } | |
820 | ||
05079acc | 821 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
822 | { |
823 | struct passwd *who; | |
518b5d2f | 824 | |
223d09f6 | 825 | *buf = wxT('\0'); |
b12915c1 VZ |
826 | if ((who = getpwuid (getuid ())) != NULL) |
827 | { | |
828 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 829 | #ifdef HAVE_PW_GECOS |
b12915c1 | 830 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
831 | if (comma) |
832 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 833 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 834 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 835 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 836 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
518b5d2f VZ |
837 | return TRUE; |
838 | } | |
839 | ||
840 | return FALSE; | |
841 | } | |
842 | ||
bdc72a22 VZ |
843 | wxString wxGetOsDescription() |
844 | { | |
845 | #ifndef WXWIN_OS_DESCRIPTION | |
846 | #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure | |
847 | #else | |
848 | return WXWIN_OS_DESCRIPTION; | |
849 | #endif | |
850 | } | |
851 | ||
bd3277fe VZ |
852 | // this function returns the GUI toolkit version in GUI programs, but OS |
853 | // version in non-GUI ones | |
854 | #if !wxUSE_GUI | |
855 | ||
856 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
857 | { | |
858 | int major, minor; | |
859 | char name[256]; | |
860 | ||
861 | if ( sscanf(WXWIN_OS_DESCRIPTION, "%s %d.%d", name, &major, &minor) != 3 ) | |
862 | { | |
863 | // unreckognized uname string format | |
864 | major = minor = -1; | |
865 | } | |
866 | ||
867 | if ( majorVsn ) | |
868 | *majorVsn = major; | |
869 | if ( minorVsn ) | |
870 | *minorVsn = minor; | |
871 | ||
872 | return wxUNIX; | |
873 | } | |
874 | ||
875 | #endif // !wxUSE_GUI | |
876 | ||
877 | long wxGetFreeMemory() | |
878 | { | |
879 | #if defined(__LINUX__) | |
880 | // get it from /proc/meminfo | |
881 | FILE *fp = fopen("/proc/meminfo", "r"); | |
882 | if ( fp ) | |
883 | { | |
884 | long memFree = -1; | |
885 | ||
886 | char buf[1024]; | |
887 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
888 | { | |
889 | long memTotal, memUsed; | |
890 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
891 | } | |
892 | ||
893 | fclose(fp); | |
894 | ||
895 | return memFree; | |
896 | } | |
897 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
898 | return sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE); | |
899 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably | |
900 | #endif | |
901 | ||
902 | // can't find it out | |
903 | return -1; | |
904 | } | |
905 | ||
8fd0d89b VZ |
906 | // ---------------------------------------------------------------------------- |
907 | // env vars | |
908 | // ---------------------------------------------------------------------------- | |
909 | ||
97b305b7 | 910 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
911 | { |
912 | // wxGetenv is defined as getenv() | |
913 | wxChar *p = wxGetenv(var); | |
914 | if ( !p ) | |
915 | return FALSE; | |
916 | ||
917 | if ( value ) | |
918 | { | |
919 | *value = p; | |
920 | } | |
921 | ||
922 | return TRUE; | |
923 | } | |
924 | ||
8fd0d89b VZ |
925 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
926 | { | |
927 | #if defined(HAVE_SETENV) | |
928 | return setenv(variable.mb_str(), value ? wxString(value).mb_str().data() | |
929 | : NULL, 1 /* overwrite */) == 0; | |
930 | #elif defined(HAVE_PUTENV) | |
931 | wxString s = variable; | |
932 | if ( value ) | |
933 | s << _T('=') << value; | |
934 | ||
935 | // transform to ANSI | |
936 | const char *p = s.mb_str(); | |
937 | ||
938 | // the string will be free()d by libc | |
939 | char *buf = (char *)malloc(strlen(p) + 1); | |
940 | strcpy(buf, p); | |
941 | ||
942 | return putenv(buf) == 0; | |
943 | #else // no way to set an env var | |
944 | return FALSE; | |
945 | #endif | |
946 | } | |
947 | ||
a37a5a73 VZ |
948 | // ---------------------------------------------------------------------------- |
949 | // signal handling | |
950 | // ---------------------------------------------------------------------------- | |
951 | ||
952 | #if wxUSE_ON_FATAL_EXCEPTION | |
953 | ||
954 | #include <signal.h> | |
955 | ||
f6bcfd97 | 956 | static void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
957 | { |
958 | if ( wxTheApp ) | |
959 | { | |
960 | // give the user a chance to do something special about this | |
961 | wxTheApp->OnFatalException(); | |
962 | } | |
963 | ||
964 | abort(); | |
965 | } | |
966 | ||
967 | bool wxHandleFatalExceptions(bool doit) | |
968 | { | |
969 | // old sig handlers | |
970 | static bool s_savedHandlers = FALSE; | |
971 | static struct sigaction s_handlerFPE, | |
972 | s_handlerILL, | |
973 | s_handlerBUS, | |
974 | s_handlerSEGV; | |
975 | ||
976 | bool ok = TRUE; | |
977 | if ( doit && !s_savedHandlers ) | |
978 | { | |
979 | // install the signal handler | |
980 | struct sigaction act; | |
981 | ||
982 | // some systems extend it with non std fields, so zero everything | |
983 | memset(&act, 0, sizeof(act)); | |
984 | ||
985 | act.sa_handler = wxFatalSignalHandler; | |
986 | sigemptyset(&act.sa_mask); | |
987 | act.sa_flags = 0; | |
988 | ||
989 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
990 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
991 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
992 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
993 | if ( !ok ) | |
994 | { | |
995 | wxLogDebug(_T("Failed to install our signal handler.")); | |
996 | } | |
997 | ||
998 | s_savedHandlers = TRUE; | |
999 | } | |
1000 | else if ( s_savedHandlers ) | |
1001 | { | |
1002 | // uninstall the signal handler | |
1003 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1004 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1005 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1006 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1007 | if ( !ok ) | |
1008 | { | |
1009 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1010 | } | |
1011 | ||
1012 | s_savedHandlers = FALSE; | |
1013 | } | |
1014 | //else: nothing to do | |
1015 | ||
1016 | return ok; | |
1017 | } | |
1018 | ||
1019 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1020 | ||
518b5d2f VZ |
1021 | // ---------------------------------------------------------------------------- |
1022 | // error and debug output routines (deprecated, use wxLog) | |
1023 | // ---------------------------------------------------------------------------- | |
1024 | ||
1025 | void wxDebugMsg( const char *format, ... ) | |
1026 | { | |
1027 | va_list ap; | |
1028 | va_start( ap, format ); | |
1029 | vfprintf( stderr, format, ap ); | |
1030 | fflush( stderr ); | |
1031 | va_end(ap); | |
1032 | } | |
1033 | ||
1034 | void wxError( const wxString &msg, const wxString &title ) | |
1035 | { | |
05079acc | 1036 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1037 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1038 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1039 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1040 | } |
1041 | ||
1042 | void wxFatalError( const wxString &msg, const wxString &title ) | |
1043 | { | |
05079acc | 1044 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1045 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1046 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1047 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1048 | exit(3); // the same exit code as for abort() |
1049 | } | |
93ccaed8 | 1050 |