]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
blind fix for GCC compilation when #pragmas are honored
[wxWidgets.git] / src / os2 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: David Webster
5 // Modified by:
6 // Created: 09/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
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 #endif //WX_PRECOMP
20
21 #include "wx/os2/private.h"
22 #include "wx/intl.h"
23 #include "wx/apptrait.h"
24
25 #include <ctype.h>
26 #ifdef __EMX__
27 #include <dirent.h>
28 #endif
29
30 #include "wx/log.h"
31
32 #include <io.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <stdarg.h>
39
40 #define PURE_32
41
42 #ifndef __EMX__
43 #include <upm.h>
44 #include <netcons.h>
45 #include <netbios.h>
46 #endif
47
48 static const wxChar WX_SECTION[] = _T("wxWidgets");
49 static const wxChar eHOSTNAME[] = _T("HostName");
50 static const wxChar eUSERID[] = _T("UserId");
51 static const wxChar eUSERNAME[] = _T("UserName");
52
53 // For the following functions we SHOULD fill in support
54 // for Windows-NT (which I don't know) as I assume it begin
55 // a POSIX Unix (so claims MS) that it has some special
56 // functions beyond those provided by WinSock
57
58 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
59 bool wxGetHostName(
60 wxChar* zBuf
61 , int nMaxSize
62 )
63 {
64 #if wxUSE_NET_API
65 char zServer[256];
66 char zComputer[256];
67 unsigned long ulLevel = 0;
68 unsigned char* zBuffer = NULL;
69 unsigned long ulBuffer = 256;
70 unsigned long* pulTotalAvail = NULL;
71
72 NetBios32GetInfo( (const unsigned char*)zServer
73 ,(const unsigned char*)zComputer
74 ,ulLevel
75 ,zBuffer
76 ,ulBuffer
77 ,pulTotalAvail
78 );
79 strcpy(zBuf, zServer);
80 #else
81 wxChar* zSysname;
82 const wxChar* zDefaultHost = _T("noname");
83
84 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL)
85 {
86 ::PrfQueryProfileString( HINI_PROFILE
87 ,(PSZ)WX_SECTION
88 ,(PSZ)eHOSTNAME
89 ,(PSZ)zDefaultHost
90 ,(void*)zBuf
91 ,(ULONG)nMaxSize - 1
92 );
93 }
94 else
95 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
96 zBuf[nMaxSize] = _T('\0');
97 #endif
98 return *zBuf ? TRUE : FALSE;
99 }
100
101 // Get user ID e.g. jacs
102 bool wxGetUserId(
103 wxChar* zBuf
104 , int nType
105 )
106 {
107 #if defined(__VISAGECPP__)
108 long lrc;
109 // UPM procs return 0 on success
110 lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType);
111 if (lrc == 0) return TRUE;
112 #endif
113 return FALSE;
114 }
115
116 bool wxGetUserName(
117 wxChar* zBuf
118 , int nMaxSize
119 )
120 {
121 #ifdef USE_NET_API
122 wxGetUserId( zBuf
123 ,nMaxSize
124 );
125 #else
126 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
127 #endif
128 return TRUE;
129 }
130
131 int wxKill(
132 long lPid
133 , wxSignal eSig
134 , wxKillError* peError
135 , int flags
136 )
137 {
138 return((int)::DosKillProcess(0, (PID)lPid));
139 }
140
141 //
142 // Execute a program in an Interactive Shell
143 //
144 bool wxShell(
145 const wxString& rCommand
146 )
147 {
148 wxChar* zShell = _T("CMD.EXE");
149 wxString sInputs;
150 STARTDATA SData = {0};
151 PSZ PgmTitle = "Command Shell";
152 APIRET rc;
153 PID vPid = 0;
154 ULONG ulSessID = 0;
155 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
156 RESULTCODES vResult;
157
158 SData.Length = sizeof(STARTDATA);
159 SData.Related = SSF_RELATED_INDEPENDENT;
160 SData.FgBg = SSF_FGBG_FORE;
161 SData.TraceOpt = SSF_TRACEOPT_NONE;
162 SData.PgmTitle = PgmTitle;
163 SData.PgmName = zShell;
164
165 sInputs = "/C " + rCommand;
166 SData.PgmInputs = (BYTE*)sInputs.c_str();
167 SData.TermQ = 0;
168 SData.Environment = 0;
169 SData.InheritOpt = SSF_INHERTOPT_SHELL;
170 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
171 SData.IconFile = 0;
172 SData.PgmHandle = 0;
173 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
174 SData.InitXPos = 30;
175 SData.InitYPos = 40;
176 SData.InitXSize = 200;
177 SData.InitYSize = 140;
178 SData.Reserved = 0;
179 SData.ObjectBuffer = (char*)achObjBuf;
180 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
181
182 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
183 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
184 {
185 PTIB ptib;
186 PPIB ppib;
187
188 ::DosGetInfoBlocks(&ptib, &ppib);
189
190 ::DosWaitChild( DCWA_PROCESS
191 ,DCWW_WAIT
192 ,&vResult
193 ,&ppib->pib_ulpid
194 ,vPid
195 );
196 }
197 return (rc != 0);
198 }
199
200 // Shutdown or reboot the PC
201 bool wxShutdown(wxShutdownFlags wFlags)
202 {
203 // TODO
204 return FALSE;
205 }
206
207 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
208 long wxGetFreeMemory()
209 {
210 void* pMemptr = NULL;
211 ULONG lSize;
212 ULONG lMemFlags;
213 APIRET rc;
214
215 lMemFlags = PAG_FREE;
216 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
217 if (rc != 0)
218 return -1L;
219 return (long)lSize;
220 }
221
222 // ----------------------------------------------------------------------------
223 // env vars
224 // ----------------------------------------------------------------------------
225
226 bool wxGetEnv(const wxString& var, wxString *value)
227 {
228 // wxGetenv is defined as getenv()
229 wxChar *p = wxGetenv(var);
230 if ( !p )
231 return FALSE;
232
233 if ( value )
234 {
235 *value = p;
236 }
237
238 return TRUE;
239 }
240
241 bool wxSetEnv(const wxString& variable, const wxChar *value)
242 {
243 #if defined(HAVE_SETENV)
244 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
245 : NULL, 1 /* overwrite */) == 0;
246 #elif defined(HAVE_PUTENV)
247 wxString s = variable;
248 if ( value )
249 s << _T('=') << value;
250
251 // transform to ANSI
252 const char *p = s.mb_str();
253
254 // the string will be free()d by libc
255 char *buf = (char *)malloc(strlen(p) + 1);
256 strcpy(buf, p);
257
258 return putenv(buf) == 0;
259 #else // no way to set an env var
260 return FALSE;
261 #endif
262 }
263
264 void wxMilliSleep(
265 unsigned long ulMilliseconds
266 )
267 {
268 ::DosSleep(ulMilliseconds);
269 }
270
271 void wxMicroSleep(
272 unsigned long ulMicroseconds
273 )
274 {
275 ::DosSleep(ulMicroseconds/1000);
276 }
277
278 void wxSleep(
279 int nSecs
280 )
281 {
282 ::DosSleep(1000 * nSecs);
283 }
284
285 // Consume all events until no more left
286 void wxFlushEvents()
287 {
288 // wxYield();
289 }
290
291 #if WXWIN_COMPATIBILITY_2_2
292
293 // Output a debug mess., in a system dependent fashion.
294 void wxDebugMsg(
295 const wxChar* zFmt ...
296 )
297 {
298 va_list vAp;
299 static wxChar zBuffer[512];
300
301 if (!wxTheApp->GetWantDebugOutput())
302 return ;
303 va_start(vAp, zFmt);
304 sprintf(zBuffer, zFmt, vAp) ;
305 va_end(vAp);
306 }
307
308 // Non-fatal error: pop up message box and (possibly) continue
309 void wxError(
310 const wxString& rMsg
311 , const wxString& rTitle
312 )
313 {
314 wxChar *wxBuffer = new wxChar[256];
315 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
316 if (::WinMessageBox( HWND_DESKTOP
317 ,NULL
318 ,(PSZ)wxBuffer
319 ,(PSZ)WXSTRINGCAST rTitle
320 ,0
321 ,MB_ICONEXCLAMATION | MB_YESNO
322 ) == MBID_YES)
323 delete[] wxBuffer;
324 wxExit();
325 }
326
327 // Fatal error: pop up message box and abort
328 void wxFatalError(
329 const wxString& rMsg
330 , const wxString& rTitle
331 )
332 {
333 unsigned long ulRc;
334
335 ulRc = ::WinMessageBox( HWND_DESKTOP
336 ,NULL
337 ,WXSTRINGCAST rMsg
338 ,WXSTRINGCAST rTitle
339 ,0
340 ,MB_NOICON | MB_OK
341 );
342 DosExit(EXIT_PROCESS, ulRc);
343 }
344
345 #endif // WXWIN_COMPATIBILITY_2_2
346
347 // Emit a beeeeeep
348 void wxBell()
349 {
350 DosBeep(1000,1000); // 1kHz during 1 sec.
351 }
352
353
354 void wxAppTraits::InitializeGui(unsigned long &ulHab)
355 {
356 }
357
358 void wxAppTraits::TerminateGui(unsigned long ulHab)
359 {
360 }
361
362 wxToolkitInfo & wxConsoleAppTraits::GetToolkitInfo()
363 {
364 static wxToolkitInfo vInfo;
365 ULONG ulSysInfo[QSV_MAX] = {0};
366 APIRET ulrc;
367
368 vInfo.name = _T("wxBase");
369 ulrc = ::DosQuerySysInfo( 1L
370 ,QSV_MAX
371 ,(PVOID)ulSysInfo
372 ,sizeof(ULONG) * QSV_MAX
373 );
374 if (ulrc == 0L)
375 {
376 vInfo.versionMajor = ulSysInfo[QSV_VERSION_MAJOR] / 10;
377 vInfo.versionMinor = ulSysInfo[QSV_VERSION_MINOR];
378 }
379 vInfo.os = wxOS2_PM;
380 return vInfo;
381 }
382
383 // ---------------------------------------------------------------------------
384 const wxChar* wxGetHomeDir(
385 wxString* pStr
386 )
387 {
388 wxString& rStrDir = *pStr;
389
390 // OS/2 has no idea about home,
391 // so use the working directory instead.
392 // However, we might have a valid HOME directory,
393 // as is used on many machines that have unix utilities
394 // on them, so we should use that, if available.
395
396 // 256 was taken from os2def.h
397 #ifndef MAX_PATH
398 # define MAX_PATH 256
399 #endif
400
401 const wxChar *szHome = wxGetenv("HOME");
402 if ( szHome == NULL ) {
403 // we're homeless, use current directory.
404 rStrDir = wxT(".");
405 }
406 else
407 rStrDir = szHome;
408
409 return rStrDir.c_str();
410 }
411
412 // Hack for OS/2
413 wxChar* wxGetUserHome (
414 const wxString& rUser
415 )
416 {
417 wxChar* zHome;
418 wxString sUser1(rUser);
419
420 char *wxBuffer = new wxChar[256];
421 #ifndef __EMX__
422 if (sUser1 != _T(""))
423 {
424 wxChar zTmp[64];
425
426 if (wxGetUserId( zTmp
427 ,sizeof(zTmp)/sizeof(char)
428 ))
429 {
430 // Guests belong in the temp dir
431 if (wxStricmp(zTmp, _T("annonymous")) == 0)
432 {
433 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
434 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
435 (zHome = wxGetenv(_T("TEMP"))) != NULL)
436 delete[] wxBuffer;
437 return *zHome ? zHome : (wxChar*)_T("\\");
438 }
439 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
440 sUser1 = _T("");
441 }
442 }
443 #endif
444 if (sUser1 == _T(""))
445 {
446 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
447 {
448 wxStrcpy(wxBuffer, zHome);
449 wxUnix2DosFilename(wxBuffer);
450 wxStrcpy(zHome, wxBuffer);
451 delete[] wxBuffer;
452 return zHome;
453 }
454 }
455 delete[] wxBuffer;
456 return NULL; // No home known!
457 }
458
459 bool wxDirExists(
460 const wxString& rDir
461 )
462 {
463 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
464 }
465
466 wxString WXDLLEXPORT wxPMErrorToStr(
467 ERRORID vError
468 )
469 {
470 wxString sError;
471
472 //
473 // Remove the high order byte -- it is useless
474 //
475 vError &= 0x0000ffff;
476 switch(vError)
477 {
478 case PMERR_INVALID_HWND:
479 sError = wxT("Invalid window handle specified");
480 break;
481
482 case PMERR_INVALID_FLAG:
483 sError = wxT("Invalid flag bit set");
484 break;
485
486 case PMERR_NO_MSG_QUEUE:
487 sError = wxT("No message queue available");
488 break;
489
490 case PMERR_INVALID_PARM:
491 sError = wxT("Parameter contained invalid data");
492 break;
493
494 case PMERR_INVALID_PARAMETERS:
495 sError = wxT("Parameter value is out of range");
496 break;
497
498 case PMERR_PARAMETER_OUT_OF_RANGE:
499 sError = wxT("Parameter value is out of range");
500 break;
501
502 case PMERR_INVALID_INTEGER_ATOM:
503 sError = wxT("Not a valid atom");
504 break;
505
506 case PMERR_INVALID_HATOMTBL:
507 sError = wxT("Atom table handle is invalid");
508 break;
509
510 case PMERR_INVALID_ATOM_NAME:
511 sError = wxT("Not a valid atom name");
512 break;
513
514 case PMERR_ATOM_NAME_NOT_FOUND:
515 sError = wxT("Valid name format, but cannot find name in atom table");
516 break;
517
518 default:
519 sError = wxT("Unknown error");
520 }
521 return(sError);
522 } // end of wxPMErrorToStr
523
524 // replacement for implementation in unix/utilsunx.cpp,
525 // to be used by all X11 based ports.
526 struct wxEndProcessData;
527
528 void wxHandleProcessTermination(wxEndProcessData *proc_data)
529 {
530 // For now, just do nothing. To be filled in as needed.
531 }