]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utilsexc.cpp
added missing -univ package control files.
[wxWidgets.git] / src / os2 / utilsexc.cpp
CommitLineData
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
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows license
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
58struct wxExecuteData
59{
60public:
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
74static 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
99MRESULT 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
133extern wxChar wxPanelClassName[];
0e320a79 134
dde11e60
DW
135long wxExecute(
136 const wxString& rCommand
137, bool bSync
138, wxProcess* pHandler
139)
0e320a79 140{
c5fb56c0
DW
141 if (rCommand.IsEmpty())
142 {
9aab8b20 143// cout << "empty command in wxExecute." << endl;
c5fb56c0
DW
144 return 0;
145 }
d90895ac
DW
146
147 // create the process
dde11e60
DW
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
c5fb56c0
DW
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)
d90895ac 172 {
c5fb56c0 173 wxLogSysError(_("Execution of command '%s' failed with error: %ul"), rCommand.c_str(), rc);
d90895ac
DW
174 return 0;
175 }
9aab8b20 176// cout << "Executing: " << rCommand.c_str() << endl;
d90895ac 177 // Alloc data
dde11e60 178 wxExecuteData* pData = new wxExecuteData;
d90895ac 179
dde11e60
DW
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;
d90895ac
DW
187 }
188 else
189 {
190 // may be NULL or not
dde11e60 191 pData->pHandler = pHandler;
d90895ac
DW
192 }
193
dde11e60
DW
194 rc = ::DosCreateThread( &vTID
195 ,(PFNTHREAD)&wxExecuteThread
196 ,(ULONG)pData
197 ,CREATE_READY|STACK_SPARSE
198 ,8192
199 );
200 if (rc != NO_ERROR)
d90895ac
DW
201 {
202 wxLogLastError("CreateThread in wxExecute");
dde11e60 203 delete pData;
d90895ac
DW
204
205 // the process still started up successfully...
dde11e60 206 return vResultCodes.codeTerminate;
d90895ac 207 }
dde11e60 208 if (!bSync)
d90895ac 209 {
d90895ac 210 // return the pid
c5fb56c0
DW
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
dde11e60 215 return vResultCodes.codeTerminate;
d90895ac 216 }
c5fb56c0
DW
217
218 // waiting until command executed
dde11e60 219 ::DosWaitThread(&vTID, DCWW_WAIT);
d90895ac 220
dde11e60
DW
221 ULONG ulExitCode = pData->vResultCodes.codeResult;
222 delete pData;
d90895ac
DW
223
224 // return the exit code
dde11e60 225 return (long)ulExitCode;
0e320a79 226}
d90895ac 227
dde11e60
DW
228long wxExecute(
229 char** ppArgv
230, bool bSync
231, wxProcess* pHandler
232)
d90895ac 233{
dde11e60 234 wxString sCommand;
d90895ac 235
dde11e60 236 while (*ppArgv != NULL)
d90895ac 237 {
dde11e60 238 sCommand << *ppArgv++ << ' ';
d90895ac 239 }
dde11e60
DW
240 sCommand.RemoveLast();
241 return wxExecute( sCommand
242 ,bSync
243 ,pHandler
244 );
d90895ac
DW
245}
246
dde11e60
DW
247bool wxGetFullHostName(
248 wxChar* zBuf
249, int nMaxSize
250)
d90895ac 251{
dde11e60
DW
252#if wxUSE_NET_API
253 char zServer[256];
254 char zComputer[256];
909b4f08 255 unsigned long ulLevel = 0;
9dea36ef
DW
256 unsigned char* zBuffer = NULL;
257 unsigned long ulBuffer = 256;
258 unsigned long* pulTotalAvail = NULL;
dde11e60
DW
259
260 NetBios32GetInfo( (const unsigned char*)zServer
261 ,(const unsigned char*)zComputer
909b4f08 262 ,ulLevel
dde11e60 263 ,zBuffer
909b4f08
DW
264 ,ulBuffer
265 ,pulTotalAvail
dde11e60
DW
266 );
267 strncpy(zBuf, zComputer, nMaxSize);
268 zBuf[nMaxSize] = _T('\0');
269#else
270 strcpy(zBuf, "noname");
271#endif
272 return *zBuf ? TRUE : FALSE;
d90895ac
DW
273 return TRUE;
274}
275