]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
"const" added to wxAceelTable ctor
[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 char* wxGetHomeDir( char *dest )
84 {
85 wxString tmp = wxGetUserHome( wxString() );
86 if (tmp.IsNull())
87 strcpy( wxBuffer, "/" );
88 else
89 strcpy( wxBuffer, tmp );
90 if (dest) strcpy( dest, WXSTRINGCAST tmp );
91 return wxBuffer;
92 };
93
94 char *wxGetUserHome( const wxString &user )
95 {
96 struct passwd *who = (struct passwd *) NULL;
97
98 if (user.IsNull() || (user== ""))
99 {
100 register char *ptr;
101
102 if ((ptr = getenv("HOME")) != NULL)
103 return ptr;
104 if ((ptr = getenv("USER")) != NULL
105 || (ptr = getenv("LOGNAME")) != NULL) {
106 who = getpwnam(ptr);
107 }
108 // We now make sure the the user exists!
109 if (who == NULL)
110 who = getpwuid(getuid());
111 }
112 else
113 who = getpwnam (user);
114
115 return who ? who->pw_dir : (char*)NULL;
116 };
117
118 //------------------------------------------------------------------------
119 // id routines
120 //------------------------------------------------------------------------
121
122 bool wxGetHostName(char *buf, int sz)
123 {
124 *buf = '\0';
125 #if defined(__SVR4__) && !defined(__sgi)
126 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
127 #else /* BSD Sockets */
128 char name[255];
129 struct hostent *h;
130 // Get hostname
131 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
132 return FALSE;
133 // Get official full name of host
134 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
135 return TRUE;
136 #endif
137 }
138
139 bool wxGetUserId(char *buf, int sz)
140 {
141 struct passwd *who;
142
143 *buf = '\0';
144 if ((who = getpwuid(getuid ())) != NULL) {
145 strncpy (buf, who->pw_name, sz-1);
146 return TRUE;
147 }
148 return FALSE;
149 }
150
151 bool wxGetUserName(char *buf, int sz)
152 {
153 struct passwd *who;
154
155 *buf = '\0';
156 if ((who = getpwuid (getuid ())) != NULL) {
157 strncpy (buf, who->pw_gecos, sz - 1);
158 return TRUE;
159 }
160 return FALSE;
161 }
162
163 //------------------------------------------------------------------------
164 // error and debug output routines
165 //------------------------------------------------------------------------
166
167 void wxDebugMsg( const char *format, ... )
168 {
169 va_list ap;
170 va_start( ap, format );
171 vfprintf( stderr, format, ap );
172 fflush( stderr );
173 va_end(ap);
174 };
175
176 void wxError( const wxString &msg, const wxString &title )
177 {
178 fprintf( stderr, "Error " );
179 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
180 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
181 fprintf( stderr, ".\n" );
182 };
183
184 void wxFatalError( const wxString &msg, const wxString &title )
185 {
186 fprintf( stderr, "Error " );
187 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
188 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
189 fprintf( stderr, ".\n" );
190 exit(1);
191 };
192
193 //------------------------------------------------------------------------
194 // directory routines
195 //------------------------------------------------------------------------
196
197 bool wxDirExists( const wxString& dir )
198 {
199 char buf[500];
200 strcpy( buf, WXSTRINGCAST(dir) );
201 struct stat sbuf;
202 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
203 };
204
205 //------------------------------------------------------------------------
206 // wild character routines
207 //------------------------------------------------------------------------
208
209 bool wxIsWild( const wxString& pattern )
210 {
211 wxString tmp = pattern;
212 char *pat = WXSTRINGCAST(tmp);
213 while (*pat) {
214 switch (*pat++) {
215 case '?': case '*': case '[': case '{':
216 return TRUE;
217 case '\\':
218 if (!*pat++)
219 return FALSE;
220 }
221 }
222 return FALSE;
223 };
224
225
226 bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
227 {
228 wxString tmp1 = pat;
229 char *pattern = WXSTRINGCAST(tmp1);
230 wxString tmp2 = text;
231 char *str = WXSTRINGCAST(tmp2);
232 char c;
233 char *cp;
234 bool done = FALSE, ret_code, ok;
235 // Below is for vi fans
236 const char OB = '{', CB = '}';
237
238 // dot_special means '.' only matches '.'
239 if (dot_special && *str == '.' && *pattern != *str)
240 return FALSE;
241
242 while ((*pattern != '\0') && (!done)
243 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
244 switch (*pattern) {
245 case '\\':
246 pattern++;
247 if (*pattern != '\0')
248 pattern++;
249 break;
250 case '*':
251 pattern++;
252 ret_code = FALSE;
253 while ((*str!='\0')
254 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
255 /*loop*/;
256 if (ret_code) {
257 while (*str != '\0')
258 str++;
259 while (*pattern != '\0')
260 pattern++;
261 }
262 break;
263 case '[':
264 pattern++;
265 repeat:
266 if ((*pattern == '\0') || (*pattern == ']')) {
267 done = TRUE;
268 break;
269 }
270 if (*pattern == '\\') {
271 pattern++;
272 if (*pattern == '\0') {
273 done = TRUE;
274 break;
275 }
276 }
277 if (*(pattern + 1) == '-') {
278 c = *pattern;
279 pattern += 2;
280 if (*pattern == ']') {
281 done = TRUE;
282 break;
283 }
284 if (*pattern == '\\') {
285 pattern++;
286 if (*pattern == '\0') {
287 done = TRUE;
288 break;
289 }
290 }
291 if ((*str < c) || (*str > *pattern)) {
292 pattern++;
293 goto repeat;
294 }
295 } else if (*pattern != *str) {
296 pattern++;
297 goto repeat;
298 }
299 pattern++;
300 while ((*pattern != ']') && (*pattern != '\0')) {
301 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
302 pattern++;
303 pattern++;
304 }
305 if (*pattern != '\0') {
306 pattern++, str++;
307 }
308 break;
309 case '?':
310 pattern++;
311 str++;
312 break;
313 case OB:
314 pattern++;
315 while ((*pattern != CB) && (*pattern != '\0')) {
316 cp = str;
317 ok = TRUE;
318 while (ok && (*cp != '\0') && (*pattern != '\0')
319 && (*pattern != ',') && (*pattern != CB)) {
320 if (*pattern == '\\')
321 pattern++;
322 ok = (*pattern++ == *cp++);
323 }
324 if (*pattern == '\0') {
325 ok = FALSE;
326 done = TRUE;
327 break;
328 } else if (ok) {
329 str = cp;
330 while ((*pattern != CB) && (*pattern != '\0')) {
331 if (*++pattern == '\\') {
332 if (*++pattern == CB)
333 pattern++;
334 }
335 }
336 } else {
337 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
338 if (*++pattern == '\\') {
339 if (*++pattern == CB || *pattern == ',')
340 pattern++;
341 }
342 }
343 }
344 if (*pattern != '\0')
345 pattern++;
346 }
347 break;
348 default:
349 if (*str == *pattern) {
350 str++, pattern++;
351 } else {
352 done = TRUE;
353 }
354 }
355 }
356 while (*pattern == '*')
357 pattern++;
358 return ((*str == '\0') && (*pattern == '\0'));
359 };
360
361 //------------------------------------------------------------------------
362 // subprocess routines
363 //------------------------------------------------------------------------
364
365 typedef struct {
366 gint pid, tag;
367 wxProcess *process;
368 } wxEndProcessData;
369
370 static void GTK_EndProcessDetector(gpointer data, gint source,
371 GdkInputCondition WXUNUSED(condition) )
372 {
373 wxEndProcessData *proc_data = (wxEndProcessData *)data;
374 int pid;
375
376 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
377
378 /* wait4 is not part of any standard, use at own risk
379 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
380 * --- offer@sgi.com */
381 #if !defined(__sgi)
382 wait4(proc_data->pid, NULL, 0, NULL);
383 #else
384 wait3((int *) NULL, 0, (rusage *) NULL);
385 #endif
386
387 close(source);
388 gdk_input_remove(proc_data->tag);
389
390 if (proc_data->process)
391 proc_data->process->OnTerminate(proc_data->pid);
392
393 if (proc_data->pid > 0)
394 delete proc_data;
395 else
396 proc_data->pid = 0;
397 };
398
399 long wxExecute( char **argv, bool sync, wxProcess *process )
400 {
401 wxEndProcessData *data = new wxEndProcessData;
402 int end_proc_detect[2];
403
404 if (*argv == NULL)
405 return 0;
406
407 /* Create pipes */
408 if (pipe(end_proc_detect) == -1) {
409 perror("pipe failed");
410 return 0;
411 }
412
413 /* fork the process */
414 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
415 pid_t pid = vfork();
416 #else
417 pid_t pid = fork();
418 #endif
419 if (pid == -1) {
420 perror ("fork failed");
421 return 0;
422 } else if (pid == 0) {
423 /* Close fd not useful */
424 close(end_proc_detect[0]); // close reading side
425
426 /* child */
427 #ifdef _AIX
428 execvp ((const char *)*argv, (const char **)argv);
429 #else
430 execvp (*argv, argv);
431 #endif
432 if (errno == ENOENT)
433 wxError("command not found", *argv);
434 else
435 perror (*argv);
436 wxError("could not execute", *argv);
437 _exit (-1);
438 }
439
440 close(end_proc_detect[1]); // close writing side
441 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
442 GTK_EndProcessDetector, (gpointer)data);
443 data->pid = pid;
444 if (!sync) {
445 data->process = process;
446 } else {
447 data->process = (wxProcess *) NULL;
448 data->pid = -(data->pid);
449
450 while (data->pid != 0)
451 wxYield();
452
453 delete data;
454 }
455
456 return pid;
457 };
458
459 long wxExecute( const wxString& command, bool sync, wxProcess *process )
460 {
461 if (command.IsNull() || command == "") return FALSE;
462
463 int argc = 0;
464 char *argv[127];
465 char tmp[1024];
466 const char *IFS = " \t\n";
467
468 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
469 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
470 argv[argc++] = strtok (tmp, IFS);
471 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
472 /* loop */ ;
473 return wxExecute(argv, sync, process);
474 };
475
476