]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utilsexec.cpp | |
d90895ac DW |
3 | // Purpose: Various utilities |
4 | // Author: David Webster | |
0e320a79 | 5 | // Modified by: |
d90895ac | 6 | // Created: 10/17/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
d90895ac | 8 | // Copyright: (c) David Webster |
6aa89a22 | 9 | // Licence: wxWindows licence |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
d90895ac DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
0e320a79 | 14 | |
d90895ac DW |
15 | #ifndef WX_PRECOMP |
16 | #include "wx/setup.h" | |
0e320a79 | 17 | #include "wx/utils.h" |
d90895ac DW |
18 | #include "wx/app.h" |
19 | #include "wx/intl.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/log.h" | |
23 | ||
24 | #include "wx/process.h" | |
25 | ||
26 | #include "wx/os2/private.h" | |
27 | ||
f38374d0 | 28 | #define PURE_32 |
9aab8b20 | 29 | #ifndef __EMX__ |
f38374d0 DW |
30 | #include <upm.h> |
31 | #include <netcons.h> | |
32 | #include <netbios.h> | |
9aab8b20 | 33 | #endif |
c5fb56c0 | 34 | |
d90895ac | 35 | #include <ctype.h> |
9aab8b20 SN |
36 | #ifdef __EMX__ |
37 | #include <dirent.h> | |
9aab8b20 | 38 | #endif |
d90895ac DW |
39 | |
40 | #include <sys/stat.h> | |
41 | #include <io.h> | |
0e320a79 DW |
42 | |
43 | #include <stdio.h> | |
44 | #include <stdlib.h> | |
45 | #include <string.h> | |
d90895ac DW |
46 | #include <errno.h> |
47 | #include <stdarg.h> | |
48 | ||
9aab8b20 | 49 | |
d90895ac DW |
50 | // this message is sent when the process we're waiting for terminates |
51 | #define wxWM_PROC_TERMINATED (WM_USER + 10000) | |
52 | ||
dde11e60 DW |
53 | #ifndef NO_ERROR |
54 | # define NO_ERROR 0 | |
55 | #endif | |
56 | ||
d90895ac DW |
57 | // structure describing the process we're being waiting for |
58 | struct wxExecuteData | |
59 | { | |
60 | public: | |
61 | ~wxExecuteData() | |
62 | { | |
9aab8b20 | 63 | // cout << "Closing thread: " << endl; |
dde11e60 | 64 | DosExit(EXIT_PROCESS, 0); |
d90895ac DW |
65 | } |
66 | ||
dde11e60 DW |
67 | HWND hWnd; // window to send wxWM_PROC_TERMINATED to [not used] |
68 | RESULTCODES vResultCodes; | |
69 | wxProcess* pHandler; | |
70 | ULONG ulExitCode; // the exit code of the process | |
71 | bool bState; // set to FALSE when the process finishes | |
d90895ac DW |
72 | }; |
73 | ||
dde11e60 DW |
74 | static ULONG wxExecuteThread( |
75 | wxExecuteData* pData | |
76 | ) | |
d90895ac | 77 | { |
dde11e60 DW |
78 | ULONG ulRc; |
79 | PID vPidChild; | |
80 | ||
9aab8b20 | 81 | // cout << "Executing thread: " << endl; |
c5fb56c0 DW |
82 | |
83 | ulRc = ::DosWaitChild( DCWA_PROCESSTREE | |
84 | ,DCWW_NOWAIT | |
dde11e60 DW |
85 | ,&pData->vResultCodes |
86 | ,&vPidChild | |
87 | ,pData->vResultCodes.codeTerminate // process PID to look at | |
88 | ); | |
89 | if (ulRc != NO_ERROR) | |
d90895ac | 90 | { |
dde11e60 | 91 | wxLogLastError("DosWaitChild"); |
d90895ac | 92 | } |
dde11e60 | 93 | delete pData; |
d90895ac DW |
94 | return 0; |
95 | } | |
96 | ||
c5fb56c0 DW |
97 | // window procedure of a hidden window which is created just to receive |
98 | // the notification message when a process exits | |
dde11e60 DW |
99 | MRESULT APIENTRY wxExecuteWindowCbk( |
100 | HWND hWnd | |
101 | , ULONG ulMessage | |
102 | , MPARAM wParam | |
103 | , MPARAM lParam | |
104 | ) | |
d90895ac | 105 | { |
dde11e60 | 106 | if (ulMessage == wxWM_PROC_TERMINATED) |
d90895ac | 107 | { |
dde11e60 | 108 | wxExecuteData* pData = (wxExecuteData *)lParam; |
d90895ac | 109 | |
dde11e60 | 110 | if (pData->pHandler) |
d90895ac | 111 | { |
dde11e60 DW |
112 | pData->pHandler->OnTerminate( (int)pData->vResultCodes.codeTerminate |
113 | ,(int)pData->vResultCodes.codeResult | |
114 | ); | |
d90895ac DW |
115 | } |
116 | ||
dde11e60 | 117 | if (pData->bState) |
d90895ac DW |
118 | { |
119 | // we're executing synchronously, tell the waiting thread | |
120 | // that the process finished | |
dde11e60 | 121 | pData->bState = 0; |
d90895ac DW |
122 | } |
123 | else | |
124 | { | |
125 | // asynchronous execution - we should do the clean up | |
dde11e60 | 126 | delete pData; |
d90895ac | 127 | } |
dde11e60 | 128 | ::WinDestroyWindow(hWnd); // we don't need it any more |
d90895ac | 129 | } |
d90895ac DW |
130 | return 0; |
131 | } | |
132 | ||
dde11e60 DW |
133 | long wxExecute( |
134 | const wxString& rCommand | |
171d29f9 | 135 | , int flags |
dde11e60 DW |
136 | , wxProcess* pHandler |
137 | ) | |
0e320a79 | 138 | { |
c5fb56c0 DW |
139 | if (rCommand.IsEmpty()) |
140 | { | |
9aab8b20 | 141 | // cout << "empty command in wxExecute." << endl; |
c5fb56c0 DW |
142 | return 0; |
143 | } | |
d90895ac DW |
144 | |
145 | // create the process | |
dde11e60 DW |
146 | UCHAR vLoadError[CCHMAXPATH] = {0}; |
147 | RESULTCODES vResultCodes = {0}; | |
148 | ULONG ulExecFlag; | |
149 | PSZ zArgs = NULL; | |
150 | PSZ zEnvs = NULL; | |
dde11e60 | 151 | APIRET rc; |
dde11e60 DW |
152 | TID vTID; |
153 | ||
171d29f9 | 154 | if (flags & wxEXEC_SYNC) |
dde11e60 DW |
155 | ulExecFlag = EXEC_SYNC; |
156 | else | |
157 | ulExecFlag = EXEC_ASYNCRESULT; | |
158 | ||
c5fb56c0 DW |
159 | rc = ::DosExecPgm( (PCHAR)vLoadError |
160 | ,sizeof(vLoadError) | |
161 | ,ulExecFlag | |
162 | ,zArgs | |
163 | ,zEnvs | |
164 | ,&vResultCodes | |
165 | ,(PSZ)rCommand.c_str() | |
166 | ); | |
167 | if (rc != NO_ERROR) | |
d90895ac | 168 | { |
c5fb56c0 | 169 | wxLogSysError(_("Execution of command '%s' failed with error: %ul"), rCommand.c_str(), rc); |
d90895ac DW |
170 | return 0; |
171 | } | |
9aab8b20 | 172 | // cout << "Executing: " << rCommand.c_str() << endl; |
d90895ac | 173 | // Alloc data |
dde11e60 | 174 | wxExecuteData* pData = new wxExecuteData; |
d90895ac | 175 | |
dde11e60 DW |
176 | pData->vResultCodes = vResultCodes; |
177 | pData->hWnd = NULLHANDLE; | |
171d29f9 VZ |
178 | pData->bState = (flags & wxEXEC_SYNC) != 0; |
179 | if (flags & wxEXEC_SYNC) | |
dde11e60 DW |
180 | { |
181 | wxASSERT_MSG(!pHandler, wxT("wxProcess param ignored for sync execution")); | |
182 | pData->pHandler = NULL; | |
d90895ac DW |
183 | } |
184 | else | |
185 | { | |
186 | // may be NULL or not | |
dde11e60 | 187 | pData->pHandler = pHandler; |
d90895ac DW |
188 | } |
189 | ||
dde11e60 DW |
190 | rc = ::DosCreateThread( &vTID |
191 | ,(PFNTHREAD)&wxExecuteThread | |
192 | ,(ULONG)pData | |
193 | ,CREATE_READY|STACK_SPARSE | |
194 | ,8192 | |
195 | ); | |
196 | if (rc != NO_ERROR) | |
d90895ac DW |
197 | { |
198 | wxLogLastError("CreateThread in wxExecute"); | |
dde11e60 | 199 | delete pData; |
d90895ac DW |
200 | |
201 | // the process still started up successfully... | |
dde11e60 | 202 | return vResultCodes.codeTerminate; |
d90895ac | 203 | } |
171d29f9 | 204 | if (!(flags & wxEXEC_SYNC)) |
d90895ac | 205 | { |
d90895ac | 206 | // return the pid |
c5fb56c0 DW |
207 | // warning: don't exit your app unless you actively |
208 | // kill and cleanup you child processes | |
209 | // Maybe detach the process here??? | |
210 | // If cmd.exe need to pass DETACH to detach the process here | |
dde11e60 | 211 | return vResultCodes.codeTerminate; |
d90895ac | 212 | } |
c5fb56c0 DW |
213 | |
214 | // waiting until command executed | |
dde11e60 | 215 | ::DosWaitThread(&vTID, DCWW_WAIT); |
d90895ac | 216 | |
dde11e60 DW |
217 | ULONG ulExitCode = pData->vResultCodes.codeResult; |
218 | delete pData; | |
d90895ac DW |
219 | |
220 | // return the exit code | |
dde11e60 | 221 | return (long)ulExitCode; |
0e320a79 | 222 | } |
d90895ac | 223 | |
dde11e60 DW |
224 | long wxExecute( |
225 | char** ppArgv | |
171d29f9 | 226 | , int flags |
dde11e60 DW |
227 | , wxProcess* pHandler |
228 | ) | |
d90895ac | 229 | { |
dde11e60 | 230 | wxString sCommand; |
d90895ac | 231 | |
dde11e60 | 232 | while (*ppArgv != NULL) |
d90895ac | 233 | { |
dde11e60 | 234 | sCommand << *ppArgv++ << ' '; |
d90895ac | 235 | } |
dde11e60 DW |
236 | sCommand.RemoveLast(); |
237 | return wxExecute( sCommand | |
171d29f9 | 238 | ,flags |
dde11e60 DW |
239 | ,pHandler |
240 | ); | |
d90895ac DW |
241 | } |
242 | ||
dde11e60 DW |
243 | bool wxGetFullHostName( |
244 | wxChar* zBuf | |
245 | , int nMaxSize | |
246 | ) | |
d90895ac | 247 | { |
dde11e60 DW |
248 | #if wxUSE_NET_API |
249 | char zServer[256]; | |
250 | char zComputer[256]; | |
909b4f08 | 251 | unsigned long ulLevel = 0; |
9dea36ef DW |
252 | unsigned char* zBuffer = NULL; |
253 | unsigned long ulBuffer = 256; | |
254 | unsigned long* pulTotalAvail = NULL; | |
dde11e60 DW |
255 | |
256 | NetBios32GetInfo( (const unsigned char*)zServer | |
257 | ,(const unsigned char*)zComputer | |
909b4f08 | 258 | ,ulLevel |
dde11e60 | 259 | ,zBuffer |
909b4f08 DW |
260 | ,ulBuffer |
261 | ,pulTotalAvail | |
dde11e60 DW |
262 | ); |
263 | strncpy(zBuf, zComputer, nMaxSize); | |
264 | zBuf[nMaxSize] = _T('\0'); | |
265 | #else | |
266 | strcpy(zBuf, "noname"); | |
267 | #endif | |
268 | return *zBuf ? TRUE : FALSE; | |
d90895ac DW |
269 | return TRUE; |
270 | } | |
271 |