After resolving all conflicts that came down the
[wxWidgets.git] / src / gtk1 / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 //#ifdef __GNUG__
13 //#pragma implementation "utils.h"
14 //#endif
15
16 #include "wx/utils.h"
17 #include "wx/string.h"
18
19 #include <stdarg.h>
20 #include <dirent.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <netdb.h>
29 #include <signal.h>
30
31 #ifdef __SVR4__
32 #include <sys/systeminfo.h>
33 #endif
34
35 //------------------------------------------------------------------------
36 // misc.
37 //------------------------------------------------------------------------
38
39 void wxBell(void)
40 {
41 gdk_beep();
42 };
43
44 void wxSleep(int nSecs)
45 {
46 sleep(nSecs);
47 };
48
49 int wxKill(long pid, int sig)
50 {
51 return kill(pid, sig);
52 };
53
54 void wxDisplaySize( int *width, int *height )
55 {
56 if (width) *width = gdk_screen_width();
57 if (height) *height = gdk_screen_height();
58 }
59
60 void wxGetMousePosition( int* x, int* y )
61 {
62 wxFAIL_MSG( "GetMousePosition not yet implemented" );
63 if (x) *x = 0;
64 if (y) *y = 0;
65 };
66
67 bool wxColourDisplay(void)
68 {
69 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
70 return TRUE;
71 }
72
73 int wxDisplayDepth(void)
74 {
75 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
76 return 8;
77 }
78
79 //------------------------------------------------------------------------
80 // user and home routines
81 //------------------------------------------------------------------------
82
83 const char* wxGetHomeDir( wxString *home )
84 {
85 *home = wxGetUserHome( wxString() );
86 if (home->IsNull()) *home = "/";
87 return *home;
88 };
89
90 char *wxGetUserHome( const wxString &user )
91 {
92 struct passwd *who = (struct passwd *) NULL;
93
94 if (user.IsNull() || (user== ""))
95 {
96 register char *ptr;
97
98 if ((ptr = getenv("HOME")) != NULL)
99 return ptr;
100 if ((ptr = getenv("USER")) != NULL
101 || (ptr = getenv("LOGNAME")) != NULL) {
102 who = getpwnam(ptr);
103 }
104 // We now make sure the the user exists!
105 if (who == NULL)
106 who = getpwuid(getuid());
107 }
108 else
109 who = getpwnam (user);
110
111 return who ? who->pw_dir : (char*)NULL;
112 };
113
114 //------------------------------------------------------------------------
115 // id routines
116 //------------------------------------------------------------------------
117
118 bool wxGetHostName(char *buf, int sz)
119 {
120 *buf = '\0';
121 #if defined(__SVR4__) && !defined(__sgi)
122 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
123 #else /* BSD Sockets */
124 char name[255];
125 struct hostent *h;
126 // Get hostname
127 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
128 return FALSE;
129 // Get official full name of host
130 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
131 return TRUE;
132 #endif
133 }
134
135 bool wxGetUserId(char *buf, int sz)
136 {
137 struct passwd *who;
138
139 *buf = '\0';
140 if ((who = getpwuid(getuid ())) != NULL) {
141 strncpy (buf, who->pw_name, sz-1);
142 return TRUE;
143 }
144 return FALSE;
145 }
146
147 bool wxGetUserName(char *buf, int sz)
148 {
149 struct passwd *who;
150
151 *buf = '\0';
152 if ((who = getpwuid (getuid ())) != NULL) {
153 strncpy (buf, who->pw_gecos, sz - 1);
154 return TRUE;
155 }
156 return FALSE;
157 }
158
159 //------------------------------------------------------------------------
160 // error and debug output routines
161 //------------------------------------------------------------------------
162
163 void wxDebugMsg( const char *format, ... )
164 {
165 va_list ap;
166 va_start( ap, format );
167 vfprintf( stderr, format, ap );
168 fflush( stderr );
169 va_end(ap);
170 };
171
172 void wxError( const wxString &msg, const wxString &title )
173 {
174 fprintf( stderr, "Error " );
175 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
176 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
177 fprintf( stderr, ".\n" );
178 };
179
180 void wxFatalError( const wxString &msg, const wxString &title )
181 {
182 fprintf( stderr, "Error " );
183 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
184 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
185 fprintf( stderr, ".\n" );
186 exit(1);
187 };
188
189 //------------------------------------------------------------------------
190 // directory routines
191 //------------------------------------------------------------------------
192
193 bool wxDirExists( const wxString& dir )
194 {
195 char buf[500];
196 strcpy( buf, WXSTRINGCAST(dir) );
197 struct stat sbuf;
198 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
199 };
200
201 //------------------------------------------------------------------------
202 // wild character routines
203 //------------------------------------------------------------------------
204
205 bool wxIsWild( const wxString& pattern )
206 {
207 wxString tmp = pattern;
208 char *pat = WXSTRINGCAST(tmp);
209 while (*pat) {
210 switch (*pat++) {
211 case '?': case '*': case '[': case '{':
212 return TRUE;
213 case '\\':
214 if (!*pat++)
215 return FALSE;
216 }
217 }
218 return FALSE;
219 };
220
221
222 bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
223 {
224 wxString tmp1 = pat;
225 char *pattern = WXSTRINGCAST(tmp1);
226 wxString tmp2 = text;
227 char *str = WXSTRINGCAST(tmp2);
228 char c;
229 char *cp;
230 bool done = FALSE, ret_code, ok;
231 // Below is for vi fans
232 const char OB = '{', CB = '}';
233
234 // dot_special means '.' only matches '.'
235 if (dot_special && *str == '.' && *pattern != *str)
236 return FALSE;
237
238 while ((*pattern != '\0') && (!done)
239 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
240 switch (*pattern) {
241 case '\\':
242 pattern++;
243 if (*pattern != '\0')
244 pattern++;
245 break;
246 case '*':
247 pattern++;
248 ret_code = FALSE;
249 while ((*str!='\0')
250 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
251 /*loop*/;
252 if (ret_code) {
253 while (*str != '\0')
254 str++;
255 while (*pattern != '\0')
256 pattern++;
257 }
258 break;
259 case '[':
260 pattern++;
261 repeat:
262 if ((*pattern == '\0') || (*pattern == ']')) {
263 done = TRUE;
264 break;
265 }
266 if (*pattern == '\\') {
267 pattern++;
268 if (*pattern == '\0') {
269 done = TRUE;
270 break;
271 }
272 }
273 if (*(pattern + 1) == '-') {
274 c = *pattern;
275 pattern += 2;
276 if (*pattern == ']') {
277 done = TRUE;
278 break;
279 }
280 if (*pattern == '\\') {
281 pattern++;
282 if (*pattern == '\0') {
283 done = TRUE;
284 break;
285 }
286 }
287 if ((*str < c) || (*str > *pattern)) {
288 pattern++;
289 goto repeat;
290 }
291 } else if (*pattern != *str) {
292 pattern++;
293 goto repeat;
294 }
295 pattern++;
296 while ((*pattern != ']') && (*pattern != '\0')) {
297 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
298 pattern++;
299 pattern++;
300 }
301 if (*pattern != '\0') {
302 pattern++, str++;
303 }
304 break;
305 case '?':
306 pattern++;
307 str++;
308 break;
309 case OB:
310 pattern++;
311 while ((*pattern != CB) && (*pattern != '\0')) {
312 cp = str;
313 ok = TRUE;
314 while (ok && (*cp != '\0') && (*pattern != '\0')
315 && (*pattern != ',') && (*pattern != CB)) {
316 if (*pattern == '\\')
317 pattern++;
318 ok = (*pattern++ == *cp++);
319 }
320 if (*pattern == '\0') {
321 ok = FALSE;
322 done = TRUE;
323 break;
324 } else if (ok) {
325 str = cp;
326 while ((*pattern != CB) && (*pattern != '\0')) {
327 if (*++pattern == '\\') {
328 if (*++pattern == CB)
329 pattern++;
330 }
331 }
332 } else {
333 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
334 if (*++pattern == '\\') {
335 if (*++pattern == CB || *pattern == ',')
336 pattern++;
337 }
338 }
339 }
340 if (*pattern != '\0')
341 pattern++;
342 }
343 break;
344 default:
345 if (*str == *pattern) {
346 str++, pattern++;
347 } else {
348 done = TRUE;
349 }
350 }
351 }
352 while (*pattern == '*')
353 pattern++;
354 return ((*str == '\0') && (*pattern == '\0'));
355 };
356
357 //------------------------------------------------------------------------
358 // subprocess routines
359 //------------------------------------------------------------------------
360
361 typedef struct {
362 gint pid, tag;
363 wxProcess *process;
364 } wxEndProcessData;
365
366 static void GTK_EndProcessDetector(gpointer data, gint source,
367 GdkInputCondition WXUNUSED(condition) )
368 {
369 wxEndProcessData *proc_data = (wxEndProcessData *)data;
370 int pid;
371
372 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
373
374 /* wait4 is not part of any standard, use at own risk
375 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
376 * --- offer@sgi.com */
377 #if !defined(__sgi)
378 wait4(proc_data->pid, NULL, 0, NULL);
379 #else
380 wait3((int *) NULL, 0, (rusage *) NULL);
381 #endif
382
383 close(source);
384 gdk_input_remove(proc_data->tag);
385
386 if (proc_data->process)
387 proc_data->process->OnTerminate(proc_data->pid);
388
389 if (proc_data->pid > 0)
390 delete proc_data;
391 else
392 proc_data->pid = 0;
393 };
394
395 long wxExecute( char **argv, bool sync, wxProcess *process )
396 {
397 wxEndProcessData *data = new wxEndProcessData;
398 int end_proc_detect[2];
399
400 if (*argv == NULL)
401 return 0;
402
403 /* Create pipes */
404 if (pipe(end_proc_detect) == -1) {
405 perror("pipe failed");
406 return 0;
407 }
408
409 /* fork the process */
410 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
411 pid_t pid = vfork();
412 #else
413 pid_t pid = fork();
414 #endif
415 if (pid == -1) {
416 perror ("fork failed");
417 return 0;
418 } else if (pid == 0) {
419 /* Close fd not useful */
420 close(end_proc_detect[0]); // close reading side
421
422 /* child */
423 #ifdef _AIX
424 execvp ((const char *)*argv, (const char **)argv);
425 #else
426 execvp (*argv, argv);
427 #endif
428 if (errno == ENOENT)
429 wxError("command not found", *argv);
430 else
431 perror (*argv);
432 wxError("could not execute", *argv);
433 _exit (-1);
434 }
435
436 close(end_proc_detect[1]); // close writing side
437 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
438 GTK_EndProcessDetector, (gpointer)data);
439 data->pid = pid;
440 if (!sync) {
441 data->process = process;
442 } else {
443 data->process = (wxProcess *) NULL;
444 data->pid = -(data->pid);
445
446 while (data->pid != 0)
447 wxYield();
448
449 delete data;
450 }
451
452 return pid;
453 };
454
455 long wxExecute( const wxString& command, bool sync, wxProcess *process )
456 {
457 if (command.IsNull() || command == "") return FALSE;
458
459 int argc = 0;
460 char *argv[127];
461 char tmp[1024];
462 const char *IFS = " \t\n";
463
464 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
465 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
466 argv[argc++] = strtok (tmp, IFS);
467 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
468 /* loop */ ;
469 return wxExecute(argv, sync, process);
470 };
471
472