]> git.saurik.com Git - wxWidgets.git/blob - src/unix/utilsunx.cpp
fixed LaTex error
[wxWidgets.git] / src / unix / utilsunx.cpp
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"
23
24 #include "wx/utils.h"
25 #include "wx/process.h"
26 #include "wx/thread.h"
27
28 #include "wx/unix/execute.h"
29
30 #include <stdarg.h>
31 #include <dirent.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <sys/wait.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <signal.h>
41 #include <fcntl.h> // for O_WRONLY and friends
42 #include <time.h> // nanosleep() and/or usleep()
43 #include <ctype.h> // isspace()
44
45 // JACS: needed for FD_SETSIZE
46 #include <sys/time.h>
47
48 #ifdef HAVE_UNAME
49 #include <sys/utsname.h> // for uname()
50 #endif // HAVE_UNAME
51
52 // ----------------------------------------------------------------------------
53 // conditional compilation
54 // ----------------------------------------------------------------------------
55
56 // many versions of Unices have this function, but it is not defined in system
57 // headers - please add your system here if it is the case for your OS.
58 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
59 #if !defined(HAVE_USLEEP) && \
60 (defined(__SUN__) && !defined(__SunOs_5_6) && \
61 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
62 defined(__osf__) || defined(__EMX__)
63 extern "C"
64 {
65 #ifdef __SUN__
66 int usleep(unsigned int usec);
67 #else // !Sun
68 #ifdef __EMX__
69 /* I copied this from the XFree86 diffs. AV. */
70 #define INCL_DOSPROCESS
71 #include <os2.h>
72 inline void usleep(unsigned long delay)
73 {
74 DosSleep(delay ? (delay/1000l) : 1l);
75 }
76 #else // !Sun && !EMX
77 void usleep(unsigned long usec);
78 #endif
79 #endif // Sun/EMX/Something else
80 };
81
82 #define HAVE_USLEEP 1
83 #endif // Unices without usleep()
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // sleeping
91 // ----------------------------------------------------------------------------
92
93 void wxSleep(int nSecs)
94 {
95 sleep(nSecs);
96 }
97
98 void wxUsleep(unsigned long milliseconds)
99 {
100 #ifdef HAVE_NANOSLEEP
101 timespec tmReq;
102 tmReq.tv_sec = milliseconds / 1000;
103 tmReq.tv_nsec = (milliseconds % 1000) * 1000 * 1000;
104
105 // we're not interested in remaining time nor in return value
106 (void)nanosleep(&tmReq, (timespec *)NULL);
107 #elif defined( HAVE_USLEEP )
108 // uncomment this if you feel brave or if you are sure that your version
109 // of Solaris has a safe usleep() function but please notice that usleep()
110 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
111 // documented as MT-Safe
112 #if defined(__SUN__) && wxUSE_THREADS
113 #error "usleep() cannot be used in MT programs under Solaris."
114 #endif // Sun
115
116 usleep(milliseconds * 1000); // usleep(3) wants microseconds
117 #else // !sleep function
118 #error "usleep() or nanosleep() function required for wxUsleep"
119 #endif // sleep function
120 }
121
122 // ----------------------------------------------------------------------------
123 // process management
124 // ----------------------------------------------------------------------------
125
126 int wxKill(long pid, wxSignal sig)
127 {
128 return kill(pid, (int)sig);
129 }
130
131 #define WXEXECUTE_NARGS 127
132
133 long wxExecute( const wxString& command, bool sync, wxProcess *process )
134 {
135 wxCHECK_MSG( !command.IsEmpty(), 0, wxT("can't exec empty command") );
136
137 int argc = 0;
138 wxChar *argv[WXEXECUTE_NARGS];
139 wxString argument;
140 const wxChar *cptr = command.c_str();
141 wxChar quotechar = wxT('\0'); // is arg quoted?
142 bool escaped = FALSE;
143
144 // split the command line in arguments
145 do
146 {
147 argument=wxT("");
148 quotechar = wxT('\0');
149
150 // eat leading whitespace:
151 while ( wxIsspace(*cptr) )
152 cptr++;
153
154 if ( *cptr == wxT('\'') || *cptr == wxT('"') )
155 quotechar = *cptr++;
156
157 do
158 {
159 if ( *cptr == wxT('\\') && ! escaped )
160 {
161 escaped = TRUE;
162 cptr++;
163 continue;
164 }
165
166 // all other characters:
167 argument += *cptr++;
168 escaped = FALSE;
169
170 // have we reached the end of the argument?
171 if ( (*cptr == quotechar && ! escaped)
172 || (quotechar == wxT('\0') && wxIsspace(*cptr))
173 || *cptr == wxT('\0') )
174 {
175 wxASSERT_MSG( argc < WXEXECUTE_NARGS,
176 wxT("too many arguments in wxExecute") );
177
178 argv[argc] = new wxChar[argument.length() + 1];
179 wxStrcpy(argv[argc], argument.c_str());
180 argc++;
181
182 // if not at end of buffer, swallow last character:
183 if(*cptr)
184 cptr++;
185
186 break; // done with this one, start over
187 }
188 } while(*cptr);
189 } while(*cptr);
190 argv[argc] = NULL;
191
192 // do execute the command
193 long lRc = wxExecute(argv, sync, process);
194
195 // clean up
196 argc = 0;
197 while( argv[argc] )
198 delete [] argv[argc++];
199
200 return lRc;
201 }
202
203 bool wxShell(const wxString& command)
204 {
205 wxString cmd;
206 if ( !!command )
207 cmd.Printf(wxT("xterm -e %s"), command.c_str());
208 else
209 cmd = command;
210
211 return wxExecute(cmd) != 0;
212 }
213
214 void wxHandleProcessTermination(wxEndProcessData *proc_data)
215 {
216 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
217
218 // waitpid is POSIX so should be available everywhere, however on older
219 // systems wait() might be used instead in a loop (until the right pid
220 // terminates)
221 int status = 0;
222 int rc;
223
224 // wait for child termination and if waitpid() was interrupted, try again
225 do
226 {
227 rc = waitpid(pid, &status, 0);
228 }
229 while ( rc == -1 && errno == EINTR );
230
231
232 if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) )
233 {
234 wxLogSysError(_("Waiting for subprocess termination failed"));
235 /* AFAIK, this can only happen if something went wrong within
236 wxGTK, i.e. due to a race condition or some serious bug.
237 After having fixed the order of statements in
238 GTK_EndProcessDetector(). (KB)
239 */
240 }
241 else
242 {
243 // notify user about termination if required
244 if (proc_data->process)
245 {
246 proc_data->process->OnTerminate(proc_data->pid,
247 WEXITSTATUS(status));
248 }
249 // clean up
250 if ( proc_data->pid > 0 )
251 {
252 delete proc_data;
253 }
254 else
255 {
256 // wxExecute() will know about it
257 proc_data->exitcode = status;
258
259 proc_data->pid = 0;
260 }
261 }
262 }
263
264 long wxExecute( wxChar **argv, bool sync, wxProcess *process )
265 {
266 wxCHECK_MSG( *argv, 0, wxT("can't exec empty command") );
267
268 #if wxUSE_UNICODE
269 int mb_argc = 0;
270 char *mb_argv[WXEXECUTE_NARGS];
271
272 while (argv[mb_argc])
273 {
274 wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]);
275 mb_argv[mb_argc] = strdup(mb_arg);
276 mb_argc++;
277 }
278 mb_argv[mb_argc] = (char *) NULL;
279
280 // this macro will free memory we used above
281 #define ARGS_CLEANUP \
282 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
283 free(mb_argv[mb_argc])
284 #else // ANSI
285 // no need for cleanup
286 #define ARGS_CLEANUP
287
288 wxChar **mb_argv = argv;
289 #endif // Unicode/ANSI
290
291 #if wxUSE_GUI
292 // create pipes
293 int end_proc_detect[2];
294 if (pipe(end_proc_detect) == -1)
295 {
296 wxLogSysError( _("Pipe creation failed") );
297
298 ARGS_CLEANUP;
299
300 return 0;
301 }
302 #endif // wxUSE_GUI
303
304 // fork the process
305 #ifdef HAVE_VFORK
306 pid_t pid = vfork();
307 #else
308 pid_t pid = fork();
309 #endif
310 if (pid == -1)
311 {
312 wxLogSysError( _("Fork failed") );
313
314 ARGS_CLEANUP;
315
316 return 0;
317 }
318 else if (pid == 0)
319 {
320 #if wxUSE_GUI
321 // we're in child
322 close(end_proc_detect[0]); // close reading side
323 #endif // wxUSE_GUI
324
325 // These three lines close the open file descriptors to to avoid any
326 // input/output which might block the process or irritate the user. If
327 // one wants proper IO for the subprocess, the right thing to do is
328 // to start an xterm executing it.
329 if (sync == 0)
330 {
331 // leave stderr opened, it won't do any hurm
332 for ( int fd = 0; fd < FD_SETSIZE; fd++ )
333 {
334 #if wxUSE_GUI
335 if ( fd == end_proc_detect[1] )
336 continue;
337 #endif // wxUSE_GUI
338
339 if ( fd != STDERR_FILENO )
340 close(fd);
341 }
342 }
343
344 #if 0
345 close(STDERR_FILENO);
346
347 // some programs complain about stderr not being open, so redirect
348 // them:
349 open("/dev/null", O_RDONLY); // stdin
350 open("/dev/null", O_WRONLY); // stdout
351 open("/dev/null", O_WRONLY); // stderr
352 #endif
353
354 execvp (*mb_argv, mb_argv);
355
356 // there is no return after successful exec()
357 wxFprintf(stderr, _("Can't execute '%s'\n"), *argv);
358
359 _exit(-1);
360 }
361 else
362 {
363 #if wxUSE_GUI
364 wxEndProcessData *data = new wxEndProcessData;
365
366
367 ARGS_CLEANUP;
368
369 if ( sync )
370 {
371 wxASSERT_MSG( !process, wxT("wxProcess param ignored for sync exec") );
372 data->process = NULL;
373
374 // sync execution: indicate it by negating the pid
375 data->pid = -pid;
376 data->tag = wxAddProcessCallback(data, end_proc_detect[0]);
377 // we're in parent
378 close(end_proc_detect[1]); // close writing side
379
380 // it will be set to 0 from GTK_EndProcessDetector
381 while (data->pid != 0)
382 wxYield();
383
384 int exitcode = data->exitcode;
385
386 delete data;
387
388 return exitcode;
389 }
390 else
391 {
392 // async execution, nothing special to do - caller will be
393 // notified about the process termination if process != NULL, data
394 // will be deleted in GTK_EndProcessDetector
395 data->process = process;
396 data->pid = pid;
397 data->tag = wxAddProcessCallback(data, end_proc_detect[0]);
398 // we're in parent
399 close(end_proc_detect[1]); // close writing side
400
401 return pid;
402 }
403 #else // !wxUSE_GUI
404 wxASSERT_MSG( sync, wxT("async execution not supported yet") );
405
406 int exitcode = 0;
407 if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) )
408 {
409 wxLogSysError(_("Waiting for subprocess termination failed"));
410 }
411
412 return exitcode;
413 #endif // wxUSE_GUI
414 }
415 return 0;
416
417 #undef ARGS_CLEANUP
418 }
419
420 // ----------------------------------------------------------------------------
421 // file and directory functions
422 // ----------------------------------------------------------------------------
423
424 const wxChar* wxGetHomeDir( wxString *home )
425 {
426 *home = wxGetUserHome( wxString() );
427 if ( home->IsEmpty() )
428 *home = wxT("/");
429
430 return home->c_str();
431 }
432
433 #if wxUSE_UNICODE
434 const wxMB2WXbuf wxGetUserHome( const wxString &user )
435 #else // just for binary compatibility -- there is no 'const' here
436 char *wxGetUserHome( const wxString &user )
437 #endif
438 {
439 struct passwd *who = (struct passwd *) NULL;
440
441 if ( !user )
442 {
443 wxChar *ptr;
444
445 if ((ptr = wxGetenv(wxT("HOME"))) != NULL)
446 {
447 return ptr;
448 }
449 if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL)
450 {
451 who = getpwnam(wxConvertWX2MB(ptr));
452 }
453
454 // We now make sure the the user exists!
455 if (who == NULL)
456 {
457 who = getpwuid(getuid());
458 }
459 }
460 else
461 {
462 who = getpwnam (user.mb_str());
463 }
464
465 return wxConvertMB2WX(who ? who->pw_dir : 0);
466 }
467
468 // ----------------------------------------------------------------------------
469 // network and user id routines
470 // ----------------------------------------------------------------------------
471
472 // retrieve either the hostname or FQDN depending on platform (caller must
473 // check whether it's one or the other, this is why this function is for
474 // private use only)
475 static bool wxGetHostNameInternal(wxChar *buf, int sz)
476 {
477 wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") );
478
479 *buf = wxT('\0');
480
481 // we're using uname() which is POSIX instead of less standard sysinfo()
482 #if defined(HAVE_UNAME)
483 struct utsname uts;
484 bool ok = uname(&uts) != -1;
485 if ( ok )
486 {
487 wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1);
488 buf[sz] = wxT('\0');
489 }
490 #elif defined(HAVE_GETHOSTNAME)
491 bool ok = gethostname(buf, sz) != -1;
492 #else // no uname, no gethostname
493 wxFAIL_MSG(wxT("don't know host name for this machine"));
494
495 bool ok = FALSE;
496 #endif // uname/gethostname
497
498 if ( !ok )
499 {
500 wxLogSysError(_("Cannot get the hostname"));
501 }
502
503 return ok;
504 }
505
506 bool wxGetHostName(wxChar *buf, int sz)
507 {
508 bool ok = wxGetHostNameInternal(buf, sz);
509
510 if ( ok )
511 {
512 // BSD systems return the FQDN, we only want the hostname, so extract
513 // it (we consider that dots are domain separators)
514 wxChar *dot = wxStrchr(buf, wxT('.'));
515 if ( dot )
516 {
517 // nuke it
518 *dot = wxT('\0');
519 }
520 }
521
522 return ok;
523 }
524
525 bool wxGetFullHostName(wxChar *buf, int sz)
526 {
527 bool ok = wxGetHostNameInternal(buf, sz);
528
529 if ( ok )
530 {
531 if ( !wxStrchr(buf, wxT('.')) )
532 {
533 struct hostent *host = gethostbyname(wxConvertWX2MB(buf));
534 if ( !host )
535 {
536 wxLogSysError(_("Cannot get the official hostname"));
537
538 ok = FALSE;
539 }
540 else
541 {
542 // the canonical name
543 wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz);
544 }
545 }
546 //else: it's already a FQDN (BSD behaves this way)
547 }
548
549 return ok;
550 }
551
552 bool wxGetUserId(wxChar *buf, int sz)
553 {
554 struct passwd *who;
555
556 *buf = wxT('\0');
557 if ((who = getpwuid(getuid ())) != NULL)
558 {
559 wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
560 return TRUE;
561 }
562
563 return FALSE;
564 }
565
566 bool wxGetUserName(wxChar *buf, int sz)
567 {
568 struct passwd *who;
569 char *comma;
570
571 *buf = wxT('\0');
572 if ((who = getpwuid (getuid ())) != NULL) {
573 #ifndef __VMS__
574 comma = strchr(who->pw_gecos, ',');
575 if (comma)
576 *comma = '\0'; // cut off non-name comment fields
577 wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1);
578 #else
579 wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
580 #endif
581 return TRUE;
582 }
583
584 return FALSE;
585 }
586
587 wxString wxGetOsDescription()
588 {
589 #ifndef WXWIN_OS_DESCRIPTION
590 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
591 #else
592 return WXWIN_OS_DESCRIPTION;
593 #endif
594 }
595
596 // ----------------------------------------------------------------------------
597 // error and debug output routines (deprecated, use wxLog)
598 // ----------------------------------------------------------------------------
599
600 void wxDebugMsg( const char *format, ... )
601 {
602 va_list ap;
603 va_start( ap, format );
604 vfprintf( stderr, format, ap );
605 fflush( stderr );
606 va_end(ap);
607 }
608
609 void wxError( const wxString &msg, const wxString &title )
610 {
611 wxFprintf( stderr, _("Error ") );
612 if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) );
613 if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) );
614 wxFprintf( stderr, wxT(".\n") );
615 }
616
617 void wxFatalError( const wxString &msg, const wxString &title )
618 {
619 wxFprintf( stderr, _("Error ") );
620 if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) );
621 if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) );
622 wxFprintf( stderr, wxT(".\n") );
623 exit(3); // the same exit code as for abort()
624 }
625