]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utilsexc.cpp
i18n fixes
[wxWidgets.git] / src / motif / utilsexc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilsexec.cpp
3 // Purpose: Execution-related utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 // #pragma implementation
14 #endif
15
16 #include "wx/utils.h"
17 #include "wx/app.h"
18 #include "wx/process.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #ifdef VMS
26 /*steve*/
27 #ifdef __HIDE_FORBIDDEN_NAMES
28 #undefine __HIDE_FORBIDDEN_NAMES
29 #endif
30 #include <socket.h>
31 #ifdef VAX
32 /*because 'noshare' is not valid in vax C++*/
33 #define CC$VAXCSHR 1
34 #endif
35 #include <unixlib.h>
36 #define unlink DELETE
37
38 #else
39
40 #if defined(__AIX__) || defined(__xlC__)
41 #include <sys/socket.h>
42 #include <sys/select.h>
43 #else
44 #ifndef __DATA_GENERAL__
45 #include <sys/syscall.h>
46 #endif
47 #endif
48
49 #include <sys/wait.h>
50 #include <unistd.h>
51 #include <dirent.h>
52 #include <pwd.h>
53
54 #endif
55
56 #ifdef __SVR4__
57 #include <sys/systeminfo.h>
58 #endif
59
60 #ifdef __SOLARIS__
61 // somehow missing from sys/wait.h but in the system's docs
62 extern "C"
63 {
64 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
65 *rusage);
66 }
67 #endif
68
69 #include <sys/time.h>
70 #include <errno.h>
71
72 #include <Xm/Xm.h>
73
74 struct wxLocalProcessData
75 {
76 int pid, end_process;
77 wxProcess *process;
78 };
79
80 #ifdef __SOLARIS__
81 // somehow missing from sys/wait.h but in the system's docs
82 extern "C"
83 {
84 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
85 *rusage);
86 }
87 #endif
88
89 void xt_notify_end_process(XtPointer client, int *fid,
90 XtInputId *id)
91 {
92 wxLocalProcessData *process_data = (wxLocalProcessData *)client;
93
94 int pid;
95
96 pid = (process_data->pid > 0) ? process_data->pid : -(process_data->pid);
97
98 /* wait4 is not part of any standard, use at own risk
99 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
100 * --- offer@sgi.com */
101 #if !defined(__sgi) && !defined(__SGI__) && !defined(__ALPHA__) && !defined(__SUNCC__)
102 wait4(process_data->pid, NULL, 0, NULL);
103 #else
104 wait3((int *) NULL, 0, (rusage *) NULL);
105 #endif
106
107 XtRemoveInput(*id);
108 if (process_data->process)
109 process_data->process->OnTerminate(process_data->pid, 0); // What should 'status' be?
110
111 process_data->end_process = TRUE;
112
113 /* double deletion!
114 if (process_data->pid > 0)
115 delete process_data;
116 else
117 */
118 process_data->pid = 0;
119 }
120
121 long wxExecute(char **argv, bool sync, wxProcess *handler)
122 {
123 #ifdef VMS
124 return(0);
125 #else
126 if (*argv == NULL)
127 return 0; // Nothing???
128
129 int proc_link[2];
130 if (pipe(proc_link))
131 return 0;
132
133 /* fork the process */
134 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
135 pid_t pid = vfork ();
136 #else
137 pid_t pid = fork ();
138 #endif
139
140 if (pid == -1)
141 {
142 return 0;
143 }
144 else if (pid == 0)
145 {
146 /* GUILHEM: Close all fds when sync == 0 */
147 if (sync == 0)
148 for (int fd=0;fd<FD_SETSIZE;fd++) {
149 if (proc_link[1] != fd)
150 close(fd);
151 }
152 /* child */
153 #ifdef _AIX
154 execvp ((const char *)*argv, (const char **)argv);
155 #else
156 execvp (*argv, argv);
157 #endif
158 /* GUILHEM: Reopen output stream */
159 // open("/dev/console", O_WRONLY);
160 /* GUILHEM: End */
161 if (errno == ENOENT)
162 printf ("%s: command not found\n", *argv);
163 else
164 perror (*argv);
165 printf ("wxWindows: could not execute '%s'\n", *argv);
166 _exit (-1);
167 }
168 if (!sync)
169 return pid;
170
171 wxLocalProcessData *process_data = new wxLocalProcessData;
172
173 process_data->end_process = 0;
174 process_data->process = handler;
175 process_data->pid = (sync) ? pid : -pid;
176
177 close(proc_link[1]);
178 XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(), proc_link[0],
179 (XtPointer *) XtInputReadMask,
180 (XtInputCallbackProc) xt_notify_end_process,
181 (XtPointer) process_data);
182
183 while (!process_data->end_process)
184 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
185
186 if (WIFEXITED(process_data->end_process) != 0)
187 {
188 delete process_data;
189 return WEXITSTATUS(process_data->end_process);
190 }
191
192 delete process_data;
193
194 return pid;
195 #endif
196 // end VMS
197 }
198
199 long wxExecute (const wxString& command, bool sync, wxProcess* handler)
200 {
201 #ifdef VMS
202 return(0);
203 #else
204 if (command.IsNull() || command == "")
205 return 0; // Nothing to do
206
207 // Run a program the recomended way under X (XView)
208 int argc = 0;
209 char *argv[127];
210 char tmp[1024];
211 const char *IFS = " \t\n";
212
213 // Build argument vector
214 strncpy (tmp, (const char*) command, sizeof (tmp) / sizeof (char) - 1);
215 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
216 argv[argc++] = strtok (tmp, IFS);
217 while ((argv[argc++] = strtok (NULL, IFS)) != NULL)
218 /* loop */ ;
219
220 return wxExecute(argv, sync, handler);
221 #endif
222 // VMS
223 }