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