source: trunk/src/kernel32/misc.cpp@ 3993

Last change on this file since 3993 was 3993, checked in by sandervl, 25 years ago

Updates for fake system dll headers

File size: 19.8 KB
Line 
1/* $Id: misc.cpp,v 1.25 2000-08-11 10:56:16 sandervl Exp $ */
2
3/*
4 * Project Odin Software License can be found in LICENSE.TXT
5 * Logging procedures
6 *
7 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
8 * Copyright 1998 Joel Troster
9 * Copyright 1998 Peter FitzSimmons
10 *
11 */
12
13
14/*****************************************************************************
15 * Includes *
16 *****************************************************************************/
17
18#define INCL_BASE
19#define INCL_WIN
20#define INCL_WINERRORS
21#define INCL_DOSFILEMGR
22#include <os2wrap.h> //Odin32 OS/2 api wrappers
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27#include <win32type.h>
28#include <misc.h>
29#include "initterm.h"
30#include "logging.h"
31#include <wprocess.h>
32#include <versionos2.h>
33
34/*****************************************************************************
35 * PMPRINTF Version *
36 *****************************************************************************/
37
38#ifdef PMPRINTF
39
40/* ----- Customization variables ----- */
41#define PRINTFID ""
42#define PRINTFMAXLEN 300
43#define PRINTFLINELEN 100
44#define PRINTFTHREADS 54
45#define PRINTFQNAME "\\QUEUES\\PRINTF32"
46
47/* ----- Includes and externals ----- */
48#include <stddef.h> /* .. */
49#include <time.h> /* .. */
50
51extern ULONG flAllocMem; /*Tue 03.03.1998: knut */
52
53/* ----- Local defines ----- */
54#define PRINTFIDSIZE sizeof(PRINTFID)
55#define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
56
57
58/*****************************************************************************
59 * Structures *
60 *****************************************************************************/
61
62/* ----- Per-thread output buffer and current indices into line ---- */
63struct perthread {
64 LONG lineindex; /* where next char */
65 LONG tidemark; /* rightmost char */
66 int bell; /* TRUE if line has bell */
67 UCHAR line[PRINTFMAXBUF]; /* accumulator */
68 };
69
70/* ----- Local static variables ----- */
71static ULONG ourpid=0; /* our process ID */
72static ULONG servepid=0; /* process IDs of the server */
73static HQUEUE qhandle=0; /* handle for the queue */
74static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
75
76/* ----- Local subroutine ----- */
77static int printf_(struct perthread *);
78
79
80/* ----------------------------------------------------------------- */
81/* The "printf" function. Note this has a variable number of */
82/* arguments. */
83/* ----------------------------------------------------------------- */
84int SYSTEM EXPORT WriteLog(char *f, ...)
85 {
86 TIB *ptib; /* process/thread id structures */
87 PIB *ppib; /* .. */
88 TID ourtid; /* thread ID */
89 struct perthread *tp; /* pointer to per-thread data */
90 int rc; /* returncode */
91 ULONG urc; /* returncode */
92
93 urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME); /* Open the Q */
94 /* Non-0 RC means Q does not exist or cannot be opened */
95 if (urc==343) return 0; /* queue does not exist, so quit */
96 if (urc!=0) return -1; /* report any other error */
97
98 /* First determine our thread ID (and hence get access to the */
99 /* correct per-thread data. If the per-thread data has not been */
100 /* allocated, then allocate it now. It is never freed, once */
101 /* allocated, as PRINTF is not notified of end-of-thread. */
102 DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
103 ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
104 if (ourtid>PRINTFTHREADS) /* too many threads .. */
105 return 0; /* .. so quit, quietly */
106 tp=tps[ourtid]; /* copy to local pointer */
107 if (tp==NULL) { /* uninitialized (NULL=0) */
108 /* allocate a per-thread structure */
109 tp=(struct perthread *)malloc(sizeof(struct perthread));
110 if (tp==NULL) return -1; /* out of memory -- return error */
111 tps[ourtid]=tp; /* save for future calls */
112 strcpy(tp->line,PRINTFID); /* initialize: line.. */
113 tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
114 tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
115 tp->bell=FALSE; /* ..if line has bell */
116 if (ourpid==0) ourpid=ppib->pib_ulpid; /* save PID for all to use */
117 }
118
119 { /* Block for declarations -- only needed if queue exists, etc. */
120 LONG count; /* count of characters formatted */
121 UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
122 LONG i, newind; /* work */
123 UCHAR ch; /* .. */
124 va_list argptr; /* -> variable argument list */
125
126 va_start(argptr, f); /* get pointer to argument list */
127 count=vsprintf(buffer, f, argptr);
128 va_end(argptr); /* done with variable arguments */
129
130 if (count<0) return count-1000;/* bad start */
131
132 if (count>PRINTFMAXLEN) {
133 /* Disaster -- we are probably "dead", but just in case we */
134 /* are not, carry on with truncated data. */
135 count=PRINTFMAXLEN;
136 }
137 buffer[count]='\0'; /* ensure terminated */
138 /* OK, ready to go with the data now in BUFFER */
139 /* We copy from the formatted string to the output (line) buffer, */
140 /* taking note of certain control characters and sending a line */
141 /* the queue whenever we see a LF control, or when the line */
142 /* fills (causing a forced break). */
143 for (i=0; ; i++) {
144 ch=buffer[i]; if (!ch) break;
145 switch(ch) {
146 case '\r': /* carriage return */
147 tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
148 break;
149 case '\n': /* new line */
150 case '\f': /* form feed */
151 rc=printf_(tp); /* print a line */
152 if (rc!=0) return rc; /* error */
153 break;
154 case '\t': /* tab */
155 newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
156 newind=tp->lineindex+5-newind%5; /* new index requested */
157 if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF; /* clamp */
158 for (; tp->lineindex<newind; tp->lineindex++) {
159 if (tp->lineindex>tp->tidemark) { /* beyond current end */
160 tp->line[tp->lineindex]=' '; /* add space */
161 tp->tidemark=tp->lineindex;
162 }
163 }
164 break;
165 case '\v': /* vertical tab */
166 /* ignore it */
167 break;
168 case '\b': /* backspace */
169 tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
170 break;
171 case '\a': /* alert (bell) */
172 tp->bell=TRUE;
173 break;
174 default: /* ordinary character */
175 tp->line[tp->lineindex]=ch;
176 if (tp->lineindex>tp->tidemark) /* is rightmost.. */
177 tp->tidemark=tp->lineindex;
178 tp->lineindex++; /* step for next */
179 } /* switch */
180 if (tp->lineindex>=PRINTFMAXBUF) {
181 rc=printf_(tp); /* print a line */
182 if (rc!=0) return rc; /* error */
183 }
184
185 } /* copy loop */
186 return count; /* all formatted data processed */
187 } /* block */
188 } /* printf */
189
190/* ----- printf_(tp) -- Local subroutine to send a line ------------ */
191/* A line has been completed (or overflowed): write it to the queue. */
192int printf_(struct perthread *tp) /* pointer to per-thread data */
193 {
194 ULONG urc; /* unsigned returncode */
195 PSZ pszTo, pszFrom; /* character pointers */
196 PVOID addr; /* address of output data */
197 long size; /* total size of output data */
198 time_t timenow; /* holds current time */
199
200 tp->line[tp->tidemark+1]='\0'; /* add terminator */
201 size=tp->tidemark+2; /* total length of data */
202
203 /* Get some shared memory that can be given away */
204 urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
205 OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
206 /*knut: added flAllocMem */
207
208 if (urc!=0) return -2; /* error */
209
210 pszTo=addr; /* copy for clarity */
211 pszFrom=&(tp->line[0]); /* pointer to source */
212 strcpy(pszTo,pszFrom); /* copy the string to shared memory */
213
214 if (ourpid!=servepid) { /* (no giveaway needed if to self) */
215 urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
216 if (urc!=0) return -3;} /* error */
217
218 /* Write the selector, size, and timestamp to the queue */
219 if (tp->bell) size=-size; /* BELL passed by negation */
220 time(&timenow); /* optional - else use 0 */
221 urc=DosWriteQueue(qhandle, /* handle */
222 (unsigned)timenow, /* 'request' (timestamp) */
223 (unsigned)size, /* 'length' (length/bell) */
224 addr, /* 'address' (address) */
225 0); /* priority (FIFO if enabled) */
226 if (urc!=0) return -4; /* error */
227 if (ourpid!=servepid) { /* if given away.. */
228 urc=DosFreeMem(addr); /* .. *we* are done with it */
229 if (urc!=0) return -5;} /* error */
230 /* Reset the line buffer and indices */
231 tp->lineindex=PRINTFIDSIZE-1; /* where next char */
232 tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
233 tp->bell =FALSE; /* true if line has bell */
234 return 0; /* success! */
235 } /* printf_ */
236#endif
237
238
239
240/*****************************************************************************
241 * Standard Version *
242 *****************************************************************************/
243
244static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
245static BOOL init = FALSE;
246static BOOL fLogging = TRUE;
247static int dwEnableLogging = 1;
248static int oldcrtmsghandle = 0;
249
250//#define CHECK_ODINHEAP
251#if defined(DEBUG) && defined(CHECK_ODINHEAP)
252int checkOdinHeap = 1;
253#define ODIN_HEAPCHECK() if(checkOdinHeap) _heap_check();
254#else
255#define ODIN_HEAPCHECK()
256#endif
257
258int SYSTEM EXPORT WriteLog(char *tekst, ...)
259{
260 USHORT sel = RestoreOS2FS();
261 va_list argptr;
262
263 ODIN_HEAPCHECK();
264
265 if(!init)
266 {
267 init = TRUE;
268
269#ifdef DEFAULT_LOGGING_OFF
270 if(getenv("WIN32LOG_ENABLED")) {
271#else
272 if(!getenv("NOWIN32LOG")) {
273#endif
274 char logname[CCHMAXPATH];
275
276 sprintf(logname, "odin32_%d.log", loadNr);
277 flog = fopen(logname, "w");
278 if(flog == NULL) {//probably running exe on readonly device
279 sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr);
280 flog = fopen(logname, "w");
281 }
282 oldcrtmsghandle = _set_crt_msg_handle(fileno(flog));
283 }
284 else
285 fLogging = FALSE;
286 }
287
288 if(fLogging && flog && (dwEnableLogging > 0))
289 {
290 THDB *thdb = GetThreadTHDB();
291
292 va_start(argptr, tekst);
293 if(thdb) {
294 thdb->logfile = (DWORD)flog;
295 if(sel == 0x150b) {
296 fprintf(flog, "t%d: (FS=150B) ", thdb->threadId);
297 }
298 else fprintf(flog, "t%d: ", thdb->threadId);
299 }
300 vfprintf(flog, tekst, argptr);
301 if(thdb) thdb->logfile = 0;
302 va_end(argptr);
303
304 if(tekst[strlen(tekst)-1] != '\n')
305 fprintf(flog, "\n");
306 }
307 SetFS(sel);
308 return 1;
309}
310//******************************************************************************
311//******************************************************************************
312int SYSTEM EXPORT WriteLogNoEOL(char *tekst, ...)
313{
314 USHORT sel = RestoreOS2FS();
315 va_list argptr;
316
317 ODIN_HEAPCHECK();
318
319 if(!init)
320 {
321 init = TRUE;
322
323#ifdef DEFAULT_LOGGING_OFF
324 if(getenv("WIN32LOG_ENABLED")) {
325#else
326 if(!getenv("NOWIN32LOG")) {
327#endif
328 char logname[CCHMAXPATH];
329
330 sprintf(logname, "odin32_%d.log", loadNr);
331 flog = fopen(logname, "w");
332 if(flog == NULL) {//probably running exe on readonly device
333 sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr);
334 flog = fopen(logname, "w");
335 }
336 }
337 else
338 fLogging = FALSE;
339 }
340
341 if(fLogging && flog && (dwEnableLogging > 0))
342 {
343 THDB *thdb = GetThreadTHDB();
344
345 va_start(argptr, tekst);
346 if(thdb) {
347 thdb->logfile = (DWORD)flog;
348 }
349 vfprintf(flog, tekst, argptr);
350 if(thdb) thdb->logfile = 0;
351 va_end(argptr);
352 }
353 SetFS(sel);
354 return 1;
355}
356//******************************************************************************
357//******************************************************************************
358void SYSTEM DecreaseLogCount()
359{
360 dwEnableLogging--;
361}
362//******************************************************************************
363//******************************************************************************
364void SYSTEM IncreaseLogCount()
365{
366 dwEnableLogging++;
367}
368//******************************************************************************
369//******************************************************************************
370int SYSTEM EXPORT WritePrivateLog(void *logfile, char *tekst, ...)
371{
372 USHORT sel = RestoreOS2FS();
373 va_list argptr;
374
375 if(fLogging && logfile)
376 {
377 THDB *thdb = GetThreadTHDB();
378
379 va_start(argptr, tekst);
380 vfprintf((FILE *)logfile, tekst, argptr);
381 if(thdb) thdb->logfile = 0;
382 va_end(argptr);
383
384 if(tekst[strlen(tekst)-1] != '\n')
385 fprintf((FILE *)logfile, "\n");
386 }
387
388 SetFS(sel);
389 return 1;
390}
391//******************************************************************************
392//Check if the exception occurred inside a fprintf (logging THDB member set)
393//If true, decrease the lock count for that file stream
394//NOTE: HACK: DEPENDS ON COMPILER VERSION!!!!
395//******************************************************************************
396void CheckLogException()
397{
398 THDB *thdb = GetThreadTHDB();
399 USHORT *lock;
400
401 if(!thdb) return;
402
403 if(thdb->logfile) {
404 //oops, exception in vfprintf; let's clear the lock count
405#if (__IBMCPP__ == 300) || (__IBMC__ == 300)
406 lock = (USHORT *)(thdb->logfile+0x1C);
407#else
408#error Check the offset of the lock count word in the file stream structure for this compiler revision!!!!!
409#endif
410 (*lock)--;
411 }
412}
413//******************************************************************************
414//NOTE: No need to save/restore FS, as our FS selectors have already been
415// destroyed and FS == 0x150B.
416//******************************************************************************
417void CloseLogFile()
418{
419 if(oldcrtmsghandle)
420 _set_crt_msg_handle(oldcrtmsghandle);
421
422 fclose(flog);
423 flog = 0;
424}
425//******************************************************************************
426//Used to open any private logfiles used in kernel32 (for now only in winimagepeldr.cpp)
427//******************************************************************************
428void OpenPrivateLogFiles()
429{
430#ifdef DEFAULT_LOGGING_OFF
431 if(getenv("WIN32LOG_ENABLED")) {
432#else
433 if(!getenv("NOWIN32LOG")) {
434#endif
435 OpenPrivateLogFilePE();
436 }
437}
438//******************************************************************************
439//Used to close all private logfiles used in kernel32 (for now only in winimagepeldr.cpp)
440//******************************************************************************
441void ClosePrivateLogFiles()
442{
443#ifdef DEFAULT_LOGGING_OFF
444 if(getenv("WIN32LOG_ENABLED")) {
445#else
446 if(!getenv("NOWIN32LOG")) {
447#endif
448 ClosePrivateLogFilePE();
449 }
450}
451//******************************************************************************
452//******************************************************************************
453int SYSTEM EXPORT WriteLogError(char *tekst, ...)
454{
455 USHORT sel = RestoreOS2FS();
456 va_list argptr;
457
458 va_start(argptr, tekst);
459 printf("ERROR: ");
460 vprintf(tekst, argptr);
461 va_end(argptr);
462 if(tekst[strlen(tekst)-1] != '\n')
463 printf("\n");
464
465 SetFS(sel);
466 return 1;
467}
468//******************************************************************************
469//******************************************************************************
470void SYSTEM CheckVersion(ULONG version, char *modname)
471{
472 dprintf(("CheckVersion of %s, %d\n", modname, version));
473 if(version != PE2LX_VERSION){
474 static char msg[300];
475 int r;
476 dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
477 sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
478 do{
479 r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
480 }while(r == MBID_RETRY); // giggle
481 if( r != MBID_IGNORE )
482 exit(987);
483 }
484}
485//******************************************************************************
486//******************************************************************************
487void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
488{
489 char name[_MAX_PATH];
490
491 // query name of dll.
492 if(!DosQueryModuleName(hModule, sizeof(name), name))
493 CheckVersion(version, name);
494}
495//******************************************************************************
496//******************************************************************************
497#ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
498 extern void interrupt3( void );
499 #pragma aux interrupt3= \
500 "int 3"
501#endif
502void WIN32API DebugBreak()
503{
504 dprintf(("DebugBreak\n"));
505
506 LPSTR lpstrEnv = getenv("WIN32.DEBUGBREAK"); /* query environment */
507 if (lpstrEnv == NULL) /* if environment is not set, don't call debugger ! */
508 return;
509
510#ifdef __WATCOMC__
511 interrupt3();
512#else
513 _interrupt(3);
514#endif
515}
516//******************************************************************************
517//******************************************************************************
518
519
520/*****************************************************************************
521 * Name : DebugErrorBox
522 * Purpose : display an apprioriate error box with detailed information
523 * about the error cause
524 * Parameters: APIRET iErrorCode - OS/2 error code
525 * PSZ pszFormat - printf-format string
526 * ...
527 * Variables :
528 * Result : return code of the message box
529 * Remark :
530 * Status :
531 *
532 * Author : Patrick Haller [Tue, 1999/09/13 19:55]
533 *****************************************************************************/
534
535int SYSTEM EXPORT DebugErrorBox(ULONG iErrorCode,
536 char* pszFormat,
537 ...)
538{
539 char szMessageBuffer[1024]; /* buffer for the text message */
540 char szErrorBuffer[1024]; /* buffer for the operating system text */
541 ULONG bc; /* dummy */
542 APIRET rc2; /* API returncode */
543 int iRC; /* message box return code */
544
545 USHORT sel = RestoreOS2FS();
546 va_list argptr;
547
548 // clear memory
549 memset (szMessageBuffer, 0, sizeof(szMessageBuffer));
550 memset (szErrorBuffer, 0, sizeof(szErrorBuffer));
551
552 // build message string
553 va_start(argptr, pszFormat);
554 vsprintf(szMessageBuffer, pszFormat, argptr);
555 va_end(argptr);
556
557 // query error string
558 rc2 = DosGetMessage(NULL,
559 0,
560 szErrorBuffer,
561 sizeof(szErrorBuffer),
562 iErrorCode,
563 "OSO001.MSG",
564 &bc);
565
566 // display message box
567 iRC = WinMessageBox(HWND_DESKTOP,
568 NULLHANDLE,
569 szMessageBuffer,
570 szErrorBuffer,
571 0,
572 MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
573 SetFS(sel);
574 return iRC;
575}
Note: See TracBrowser for help on using the repository browser.