]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
Fix for TextCtrl problem as reported by Vegh
[wxWidgets.git] / src / gtk1 / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 //#ifdef __GNUG__
12 //#pragma implementation "utils.h"
13 //#endif
14
15 #include "wx/utils.h"
16 #include "wx/string.h"
17
18 #include "wx/intl.h"
19 #include "wx/log.h"
20
21 #include <stdarg.h>
22 #include <dirent.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <sys/wait.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <signal.h>
32
33
34 #ifdef __SVR4__
35 #include <sys/systeminfo.h>
36 #endif
37
38 #ifdef __SOLARIS__
39 // somehow missing from sys/wait.h but in the system's docs
40 extern "C"
41 {
42 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
43 *rusage);
44 }
45 #endif
46
47 //------------------------------------------------------------------------
48 // misc.
49 //------------------------------------------------------------------------
50
51 void wxBell(void)
52 {
53 gdk_beep();
54 };
55
56 void wxSleep(int nSecs)
57 {
58 sleep(nSecs);
59 };
60
61 int wxKill(long pid, int sig)
62 {
63 return kill(pid, sig);
64 };
65
66 void wxDisplaySize( int *width, int *height )
67 {
68 if (width) *width = gdk_screen_width();
69 if (height) *height = gdk_screen_height();
70 }
71
72 void wxGetMousePosition( int* x, int* y )
73 {
74 wxFAIL_MSG( "GetMousePosition not yet implemented" );
75 if (x) *x = 0;
76 if (y) *y = 0;
77 };
78
79 bool wxColourDisplay(void)
80 {
81 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
82 return TRUE;
83 }
84
85 int wxDisplayDepth(void)
86 {
87 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
88 return 8;
89 }
90
91 //------------------------------------------------------------------------
92 // user and home routines
93 //------------------------------------------------------------------------
94
95 const char* wxGetHomeDir( wxString *home )
96 {
97 *home = wxGetUserHome( wxString() );
98 if (home->IsNull()) *home = "/";
99 return *home;
100 };
101
102 char *wxGetUserHome( const wxString &user )
103 {
104 struct passwd *who = (struct passwd *) NULL;
105
106 if (user.IsNull() || (user== ""))
107 {
108 register char *ptr;
109
110 if ((ptr = getenv("HOME")) != NULL)
111 return ptr;
112 if ((ptr = getenv("USER")) != NULL
113 || (ptr = getenv("LOGNAME")) != NULL) {
114 who = getpwnam(ptr);
115 }
116 // We now make sure the the user exists!
117 if (who == NULL)
118 who = getpwuid(getuid());
119 }
120 else
121 who = getpwnam (user);
122
123 return who ? who->pw_dir : (char*)NULL;
124 };
125
126 //------------------------------------------------------------------------
127 // id routines
128 //------------------------------------------------------------------------
129
130 bool wxGetHostName(char *buf, int sz)
131 {
132 *buf = '\0';
133 #if defined(__SVR4__) && !defined(__sgi)
134 //KB: does this return the fully qualified host.domain name?
135 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
136 #else /* BSD Sockets */
137 char name[255], domain[255];
138 //struct hostent *h;
139 // Get hostname
140 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
141 return FALSE;
142 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
143 return FALSE;
144 // Get official full name of host
145 // doesn't return the full qualified name, replaced by following
146 // code (KB)
147 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
148 if((unsigned)sz > strlen(name)+strlen(domain)+1)
149 {
150 strcpy(buf, name);
151 if(strcmp(domain,"(none)") == 0) // standalone machine
152 {
153 strcat(buf,".");
154 strcat(buf,domain);
155 }
156 }
157 else
158 return FALSE;
159 return TRUE;
160 #endif
161 }
162
163 bool wxGetUserId(char *buf, int sz)
164 {
165 struct passwd *who;
166
167 *buf = '\0';
168 if ((who = getpwuid(getuid ())) != NULL) {
169 strncpy (buf, who->pw_name, sz-1);
170 return TRUE;
171 }
172 return FALSE;
173 }
174
175 bool wxGetUserName(char *buf, int sz)
176 {
177 struct passwd *who;
178 char *comma;
179
180 *buf = '\0';
181 if ((who = getpwuid (getuid ())) != NULL) {
182 comma = strchr(who->pw_gecos,'c');
183 if(comma) *comma = '\0'; // cut off non-name comment fields
184 strncpy (buf, who->pw_gecos, sz - 1);
185 return TRUE;
186 }
187 return FALSE;
188 }
189
190 //------------------------------------------------------------------------
191 // error and debug output routines
192 //------------------------------------------------------------------------
193
194 void wxDebugMsg( const char *format, ... )
195 {
196 va_list ap;
197 va_start( ap, format );
198 vfprintf( stderr, format, ap );
199 fflush( stderr );
200 va_end(ap);
201 };
202
203 void wxError( const wxString &msg, const wxString &title )
204 {
205 fprintf( stderr, "Error " );
206 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
207 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
208 fprintf( stderr, ".\n" );
209 };
210
211 void wxFatalError( const wxString &msg, const wxString &title )
212 {
213 fprintf( stderr, "Error " );
214 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
215 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
216 fprintf( stderr, ".\n" );
217 exit(3); // the same exit code as for abort()
218 };
219
220 //------------------------------------------------------------------------
221 // directory routines
222 //------------------------------------------------------------------------
223
224 bool wxDirExists( const wxString& dir )
225 {
226 char buf[500];
227 strcpy( buf, WXSTRINGCAST(dir) );
228 struct stat sbuf;
229 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
230 };
231
232 //------------------------------------------------------------------------
233 // subprocess routines
234 //------------------------------------------------------------------------
235
236 struct wxEndProcessData
237 {
238 gint pid, tag;
239 wxProcess *process;
240 };
241
242 static void GTK_EndProcessDetector(gpointer data, gint source,
243 GdkInputCondition WXUNUSED(condition) )
244 {
245 wxEndProcessData *proc_data = (wxEndProcessData *)data;
246 int pid;
247
248 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
249
250 /* wait4 is not part of any standard, use at own risk
251 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
252 * --- offer@sgi.com */
253 #if !defined(__sgi)
254 wait4(proc_data->pid, NULL, 0, NULL);
255 #else
256 wait3((int *) NULL, 0, (rusage *) NULL);
257 #endif
258
259 close(source);
260 gdk_input_remove(proc_data->tag);
261
262 if (proc_data->process)
263 proc_data->process->OnTerminate(proc_data->pid);
264
265 if (proc_data->pid > 0)
266 delete proc_data;
267 else
268 proc_data->pid = 0;
269 };
270
271 long wxExecute( char **argv, bool sync, wxProcess *process )
272 {
273 wxEndProcessData *data = new wxEndProcessData;
274 int end_proc_detect[2];
275
276 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
277
278 /* Create pipes */
279 if (pipe(end_proc_detect) == -1) {
280 wxLogSysError(_("Pipe creation failed"));
281 return 0;
282 }
283
284 /* fork the process */
285 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
286 pid_t pid = vfork();
287 #else
288 pid_t pid = fork();
289 #endif
290 if (pid == -1) {
291 // error
292 wxLogSysError(_("Fork failed"));
293 return 0;
294 }
295 else if (pid == 0) {
296 // we're in child
297 close(end_proc_detect[0]); // close reading side
298
299 #ifdef _AIX
300 execvp ((const char *)*argv, (const char **)argv);
301 #else
302 execvp (*argv, argv);
303 #endif
304 // there is no return after successful exec()
305 wxLogSysError(_("Can't execute '%s'"), *argv);
306
307 _exit(-1);
308 }
309 else {
310 // we're in parent
311 close(end_proc_detect[1]); // close writing side
312 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
313 GTK_EndProcessDetector, (gpointer)data);
314 data->pid = pid;
315 if (!sync) {
316 data->process = process;
317 }
318 else {
319 data->process = (wxProcess *) NULL;
320 data->pid = -(data->pid);
321
322 while (data->pid != 0)
323 wxYield();
324
325 delete data;
326 }
327
328 // @@@ our return value indicates success even if execvp() in the child
329 // failed!
330 return pid;
331 }
332 };
333
334 long wxExecute( const wxString& command, bool sync, wxProcess *process )
335 {
336 static const char *IFS = " \t\n";
337
338 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
339
340 int argc = 0;
341 char *argv[127];
342 char *tmp = new char[command.Len() + 1];
343 strcpy(tmp, command);
344
345 argv[argc++] = strtok(tmp, IFS);
346 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
347 /* loop */ ;
348
349 long lRc = wxExecute(argv, sync, process);
350
351 delete [] tmp;
352
353 return lRc;
354 };