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