| 1 | /* $Id: misc.cpp,v 1.42 2001-09-26 15:31:05 phaller 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 | *   Internal Functions                                                         * | 
|---|
| 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 <win32api.h> | 
|---|
| 29 | #include <misc.h> | 
|---|
| 30 | #include "initterm.h" | 
|---|
| 31 | #include "logging.h" | 
|---|
| 32 | #include "exceptutil.h" | 
|---|
| 33 | #include <wprocess.h> | 
|---|
| 34 | #include <versionos2.h> | 
|---|
| 35 | #include "odinbuild.h" | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | /***************************************************************************** | 
|---|
| 39 | * PMPRINTF Version                                                          * | 
|---|
| 40 | *****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifdef PMPRINTF | 
|---|
| 43 |  | 
|---|
| 44 | /* ----- Customization variables ----- */ | 
|---|
| 45 | #define PRINTFID      "" | 
|---|
| 46 | #define PRINTFMAXLEN  300 | 
|---|
| 47 | #define PRINTFLINELEN 100 | 
|---|
| 48 | #define PRINTFTHREADS  54 | 
|---|
| 49 | #define PRINTFQNAME   "\\QUEUES\\PRINTF32" | 
|---|
| 50 |  | 
|---|
| 51 | /* ----- Includes and externals ----- */ | 
|---|
| 52 | #include <stddef.h>                /* .. */ | 
|---|
| 53 | #include <time.h>                  /* .. */ | 
|---|
| 54 |  | 
|---|
| 55 | extern ULONG flAllocMem;    /*Tue 03.03.1998: knut */ | 
|---|
| 56 |  | 
|---|
| 57 | /* ----- Local defines ----- */ | 
|---|
| 58 | #define PRINTFIDSIZE sizeof(PRINTFID) | 
|---|
| 59 | #define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | /***************************************************************************** | 
|---|
| 63 | * Structures                                                                * | 
|---|
| 64 | *****************************************************************************/ | 
|---|
| 65 |  | 
|---|
| 66 | /* ----- Per-thread output buffer and current indices into line ---- */ | 
|---|
| 67 | struct perthread { | 
|---|
| 68 | LONG   lineindex;                /* where next char */ | 
|---|
| 69 | LONG   tidemark;                 /* rightmost char */ | 
|---|
| 70 | int    bell;                     /* TRUE if line has bell */ | 
|---|
| 71 | UCHAR  line[PRINTFMAXBUF];       /* accumulator */ | 
|---|
| 72 | }; | 
|---|
| 73 |  | 
|---|
| 74 | /* ----- Local static variables ----- */ | 
|---|
| 75 | static ULONG  ourpid=0;            /* our process ID */ | 
|---|
| 76 | static ULONG  servepid=0;          /* process IDs of the server */ | 
|---|
| 77 | static HQUEUE qhandle=0;           /* handle for the queue */ | 
|---|
| 78 | static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */ | 
|---|
| 79 |  | 
|---|
| 80 | /* ----- Local subroutine ----- */ | 
|---|
| 81 | static int printf_(struct perthread *); | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | /* ----------------------------------------------------------------- */ | 
|---|
| 85 | /* The "printf" function.  Note this has a variable number of        */ | 
|---|
| 86 | /* arguments.                                                        */ | 
|---|
| 87 | /* ----------------------------------------------------------------- */ | 
|---|
| 88 | int SYSTEM WriteLog(char *f, ...) | 
|---|
| 89 | { | 
|---|
| 90 | TIB    *ptib;                    /* process/thread id structures */ | 
|---|
| 91 | PIB    *ppib;                    /* .. */ | 
|---|
| 92 | TID    ourtid;                   /* thread ID */ | 
|---|
| 93 | struct perthread *tp;            /* pointer to per-thread data */ | 
|---|
| 94 | int    rc;                       /* returncode */ | 
|---|
| 95 | ULONG  urc;                      /* returncode */ | 
|---|
| 96 |  | 
|---|
| 97 | urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME);  /* Open the Q */ | 
|---|
| 98 | /* Non-0 RC means Q does not exist or cannot be opened */ | 
|---|
| 99 | if (urc==343) return 0;          /* queue does not exist, so quit */ | 
|---|
| 100 | if (urc!=0)   return -1;         /* report any other error */ | 
|---|
| 101 |  | 
|---|
| 102 | /* First determine our thread ID (and hence get access to the      */ | 
|---|
| 103 | /* correct per-thread data.  If the per-thread data has not been   */ | 
|---|
| 104 | /* allocated, then allocate it now.  It is never freed, once       */ | 
|---|
| 105 | /* allocated, as PRINTF is not notified of end-of-thread.          */ | 
|---|
| 106 | DosGetInfoBlocks(&ptib,&ppib);   /* get process/thread info */ | 
|---|
| 107 | ourtid=ptib->tib_ptib2->tib2_ultid;   /* .. and copy TID */ | 
|---|
| 108 | if (ourtid>PRINTFTHREADS)        /* too many threads .. */ | 
|---|
| 109 | return 0;                      /* .. so quit, quietly */ | 
|---|
| 110 | tp=tps[ourtid];                  /* copy to local pointer */ | 
|---|
| 111 | if (tp==NULL) {                  /* uninitialized (NULL=0) */ | 
|---|
| 112 | /* allocate a per-thread structure */ | 
|---|
| 113 | tp=(struct perthread *)malloc(sizeof(struct perthread)); | 
|---|
| 114 | if (tp==NULL) return -1;       /* out of memory -- return error */ | 
|---|
| 115 | tps[ourtid]=tp;                /* save for future calls */ | 
|---|
| 116 | strcpy(tp->line,PRINTFID);     /* initialize: line.. */ | 
|---|
| 117 | tp->lineindex=PRINTFIDSIZE-1;  /* ..where next char */ | 
|---|
| 118 | tp->tidemark =PRINTFIDSIZE-2;  /* ..rightmost char */ | 
|---|
| 119 | tp->bell=FALSE;                /* ..if line has bell */ | 
|---|
| 120 | if (ourpid==0) ourpid=ppib->pib_ulpid;   /* save PID for all to use */ | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | { /* Block for declarations -- only needed if queue exists, etc. */ | 
|---|
| 124 | LONG  count;                   /* count of characters formatted */ | 
|---|
| 125 | UCHAR buffer[PRINTFMAXLEN+1];  /* formatting area */ | 
|---|
| 126 | LONG  i, newind;               /* work */ | 
|---|
| 127 | UCHAR ch;                      /* .. */ | 
|---|
| 128 | va_list argptr;                /* -> variable argument list */ | 
|---|
| 129 |  | 
|---|
| 130 | va_start(argptr, f);           /* get pointer to argument list */ | 
|---|
| 131 | count=vsprintf(buffer, f, argptr); | 
|---|
| 132 | va_end(argptr);                /* done with variable arguments */ | 
|---|
| 133 |  | 
|---|
| 134 | if (count<0) return count-1000;/* bad start */ | 
|---|
| 135 |  | 
|---|
| 136 | if (count>PRINTFMAXLEN) { | 
|---|
| 137 | /* Disaster -- we are probably "dead", but just in case we */ | 
|---|
| 138 | /* are not, carry on with truncated data. */ | 
|---|
| 139 | count=PRINTFMAXLEN; | 
|---|
| 140 | } | 
|---|
| 141 | buffer[count]='\0';            /* ensure terminated */ | 
|---|
| 142 | /* OK, ready to go with the data now in BUFFER                    */ | 
|---|
| 143 | /* We copy from the formatted string to the output (line) buffer, */ | 
|---|
| 144 | /* taking note of certain control characters and sending a line   */ | 
|---|
| 145 | /* the queue whenever we see a LF control, or when the line       */ | 
|---|
| 146 | /* fills (causing a forced break).                                */ | 
|---|
| 147 | for (i=0; ; i++) { | 
|---|
| 148 | ch=buffer[i]; if (!ch) break; | 
|---|
| 149 | switch(ch) { | 
|---|
| 150 | case '\r':                 /* carriage return */ | 
|---|
| 151 | tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */ | 
|---|
| 152 | break; | 
|---|
| 153 | case '\n':                 /* new line */ | 
|---|
| 154 | case '\f':                 /* form feed */ | 
|---|
| 155 | rc=printf_(tp);          /* print a line */ | 
|---|
| 156 | if (rc!=0) return rc;    /* error */ | 
|---|
| 157 | break; | 
|---|
| 158 | case '\t':                 /* tab */ | 
|---|
| 159 | newind=tp->lineindex-PRINTFIDSIZE+1;   /* offset into data */ | 
|---|
| 160 | newind=tp->lineindex+5-newind%5;    /* new index requested */ | 
|---|
| 161 | if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF;    /* clamp */ | 
|---|
| 162 | for (; tp->lineindex<newind; tp->lineindex++) { | 
|---|
| 163 | if (tp->lineindex>tp->tidemark) {  /* beyond current end */ | 
|---|
| 164 | tp->line[tp->lineindex]=' ';              /* add space */ | 
|---|
| 165 | tp->tidemark=tp->lineindex; | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 | break; | 
|---|
| 169 | case '\v':                 /* vertical tab */ | 
|---|
| 170 | /* ignore it */ | 
|---|
| 171 | break; | 
|---|
| 172 | case '\b':                 /* backspace */ | 
|---|
| 173 | tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE); | 
|---|
| 174 | break; | 
|---|
| 175 | case '\a':                 /* alert (bell) */ | 
|---|
| 176 | tp->bell=TRUE; | 
|---|
| 177 | break; | 
|---|
| 178 | default:                   /* ordinary character */ | 
|---|
| 179 | tp->line[tp->lineindex]=ch; | 
|---|
| 180 | if (tp->lineindex>tp->tidemark)  /* is rightmost.. */ | 
|---|
| 181 | tp->tidemark=tp->lineindex; | 
|---|
| 182 | tp->lineindex++;                 /* step for next */ | 
|---|
| 183 | } /* switch */ | 
|---|
| 184 | if (tp->lineindex>=PRINTFMAXBUF) { | 
|---|
| 185 | rc=printf_(tp);            /* print a line */ | 
|---|
| 186 | if (rc!=0) return rc;      /* error */ | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | } /* copy loop */ | 
|---|
| 190 | return count;                  /* all formatted data processed */ | 
|---|
| 191 | } /* block */ | 
|---|
| 192 | } /* printf */ | 
|---|
| 193 |  | 
|---|
| 194 | /* ----- printf_(tp) -- Local subroutine to send a line ------------ */ | 
|---|
| 195 | /* A line has been completed (or overflowed): write it to the queue. */ | 
|---|
| 196 | int printf_(struct perthread *tp)  /* pointer to per-thread data */ | 
|---|
| 197 | { | 
|---|
| 198 | ULONG   urc;                     /* unsigned returncode */ | 
|---|
| 199 | PSZ     pszTo, pszFrom;          /* character pointers */ | 
|---|
| 200 | PVOID   addr;                    /* address of output data */ | 
|---|
| 201 | long    size;                    /* total size of output data */ | 
|---|
| 202 | time_t  timenow;                 /* holds current time */ | 
|---|
| 203 |  | 
|---|
| 204 | tp->line[tp->tidemark+1]='\0';   /* add terminator */ | 
|---|
| 205 | size=tp->tidemark+2;             /* total length of data */ | 
|---|
| 206 |  | 
|---|
| 207 | /* Get some shared memory that can be given away */ | 
|---|
| 208 | urc=DosAllocSharedMem(&addr, NULL, (unsigned)size, | 
|---|
| 209 | OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem); | 
|---|
| 210 | /*knut: added flAllocMem */ | 
|---|
| 211 |  | 
|---|
| 212 | if (urc!=0) return -2;           /* error */ | 
|---|
| 213 |  | 
|---|
| 214 | pszTo=addr;                      /* copy for clarity */ | 
|---|
| 215 | pszFrom=&(tp->line[0]);          /* pointer to source */ | 
|---|
| 216 | strcpy(pszTo,pszFrom);           /* copy the string to shared memory */ | 
|---|
| 217 |  | 
|---|
| 218 | if (ourpid!=servepid) {          /* (no giveaway needed if to self) */ | 
|---|
| 219 | urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */ | 
|---|
| 220 | if (urc!=0) return -3;}        /* error */ | 
|---|
| 221 |  | 
|---|
| 222 | /* Write the selector, size, and timestamp to the queue */ | 
|---|
| 223 | if (tp->bell) size=-size;        /* BELL passed by negation */ | 
|---|
| 224 | time(&timenow);                  /* optional - else use 0 */ | 
|---|
| 225 | urc=DosWriteQueue(qhandle,       /* handle */ | 
|---|
| 226 | (unsigned)timenow,        /* 'request' (timestamp) */ | 
|---|
| 227 | (unsigned)size,           /* 'length'  (length/bell) */ | 
|---|
| 228 | addr,           /* 'address' (address) */ | 
|---|
| 229 | 0);             /* priority (FIFO if enabled) */ | 
|---|
| 230 | if (urc!=0) return -4;           /* error */ | 
|---|
| 231 | if (ourpid!=servepid) {          /* if given away.. */ | 
|---|
| 232 | urc=DosFreeMem(addr);          /* .. *we* are done with it */ | 
|---|
| 233 | if (urc!=0) return -5;}        /* error */ | 
|---|
| 234 | /* Reset the line buffer and indices */ | 
|---|
| 235 | tp->lineindex=PRINTFIDSIZE-1;    /* where next char */ | 
|---|
| 236 | tp->tidemark =PRINTFIDSIZE-2;    /* rightmost char */ | 
|---|
| 237 | tp->bell     =FALSE;             /* true if line has bell */ | 
|---|
| 238 | return 0;                        /* success! */ | 
|---|
| 239 | } /* printf_ */ | 
|---|
| 240 | #endif | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 |  | 
|---|
| 244 | /***************************************************************************** | 
|---|
| 245 | * Standard Version                                                          * | 
|---|
| 246 | *****************************************************************************/ | 
|---|
| 247 |  | 
|---|
| 248 | static FILE *flog = NULL;   /*PLF Mon  97-09-08 20:00:15*/ | 
|---|
| 249 | static BOOL init = FALSE; | 
|---|
| 250 | static BOOL fLogging = TRUE; | 
|---|
| 251 | static int  dwEnableLogging = 1; | 
|---|
| 252 | static int  oldcrtmsghandle = 0; | 
|---|
| 253 |  | 
|---|
| 254 | static BOOL fDisableThread[5] = {0}; | 
|---|
| 255 | static BOOL fFlushLines = FALSE; | 
|---|
| 256 |  | 
|---|
| 257 | //#define CHECK_ODINHEAP | 
|---|
| 258 | #if defined(DEBUG) && defined(CHECK_ODINHEAP) | 
|---|
| 259 | int checkOdinHeap = 1; | 
|---|
| 260 | int checkingheap = 0; | 
|---|
| 261 | #define ODIN_HEAPCHECK() \ | 
|---|
| 262 | if(checkingheap) checkOdinHeap = 0; \ | 
|---|
| 263 | checkingheap++; \ | 
|---|
| 264 | if(checkOdinHeap)   _heap_check(); \ | 
|---|
| 265 | checkingheap--; | 
|---|
| 266 | #else | 
|---|
| 267 | #define ODIN_HEAPCHECK() | 
|---|
| 268 | #endif | 
|---|
| 269 |  | 
|---|
| 270 | //#define LOG_TIME | 
|---|
| 271 |  | 
|---|
| 272 | int SYSTEM WriteLog(char *tekst, ...) | 
|---|
| 273 | { | 
|---|
| 274 | USHORT  sel = RestoreOS2FS(); | 
|---|
| 275 | va_list argptr; | 
|---|
| 276 | TEB *teb = GetThreadTEB(); | 
|---|
| 277 |  | 
|---|
| 278 | ODIN_HEAPCHECK(); | 
|---|
| 279 |  | 
|---|
| 280 | if(!init) | 
|---|
| 281 | { | 
|---|
| 282 | init = TRUE; | 
|---|
| 283 |  | 
|---|
| 284 | if(getenv("WIN32LOG_FLUSHLINES")) { | 
|---|
| 285 | fFlushLines = TRUE; | 
|---|
| 286 | } | 
|---|
| 287 | #ifdef DEFAULT_LOGGING_OFF | 
|---|
| 288 | if(getenv("WIN32LOG_ENABLED")) { | 
|---|
| 289 | #else | 
|---|
| 290 | if(!getenv("NOWIN32LOG")) { | 
|---|
| 291 | #endif | 
|---|
| 292 | char logname[CCHMAXPATH]; | 
|---|
| 293 |  | 
|---|
| 294 | sprintf(logname, "odin32_%d.log", loadNr); | 
|---|
| 295 | flog = fopen(logname, "w"); | 
|---|
| 296 | if(flog == NULL) {//probably running exe on readonly device | 
|---|
| 297 | sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr); | 
|---|
| 298 | flog = fopen(logname, "w"); | 
|---|
| 299 | } | 
|---|
| 300 | oldcrtmsghandle = _set_crt_msg_handle(fileno(flog)); | 
|---|
| 301 | } | 
|---|
| 302 | else | 
|---|
| 303 | fLogging = FALSE; | 
|---|
| 304 |  | 
|---|
| 305 | if(getenv("DISABLE_THREAD1")) { | 
|---|
| 306 | fDisableThread[0] = TRUE; | 
|---|
| 307 | } | 
|---|
| 308 | if(getenv("DISABLE_THREAD2")) { | 
|---|
| 309 | fDisableThread[1] = TRUE; | 
|---|
| 310 | } | 
|---|
| 311 | if(getenv("DISABLE_THREAD3")) { | 
|---|
| 312 | fDisableThread[2] = TRUE; | 
|---|
| 313 | } | 
|---|
| 314 | if(getenv("DISABLE_THREAD4")) { | 
|---|
| 315 | fDisableThread[3] = TRUE; | 
|---|
| 316 | } | 
|---|
| 317 | if(getenv("DISABLE_THREAD5")) { | 
|---|
| 318 | fDisableThread[4] = TRUE; | 
|---|
| 319 | } | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | if(teb) { | 
|---|
| 323 | if(teb->o.odin.threadId < 5 && fDisableThread[teb->o.odin.threadId-1] == 1) { | 
|---|
| 324 | SetFS(sel); | 
|---|
| 325 | return 1; | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | if(!tekst) { | 
|---|
| 330 | if(flog) fflush( flog); | 
|---|
| 331 | SetFS(sel); | 
|---|
| 332 | return 1; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | if(fLogging && flog && (dwEnableLogging > 0)) | 
|---|
| 336 | { | 
|---|
| 337 | va_start(argptr, tekst); | 
|---|
| 338 | if(teb) | 
|---|
| 339 | { | 
|---|
| 340 | ULONG ulCallDepth; | 
|---|
| 341 | #ifdef DEBUG | 
|---|
| 342 | ulCallDepth = teb->o.odin.dbgCallDepth; | 
|---|
| 343 | #else | 
|---|
| 344 | ulCallDepth = 0; | 
|---|
| 345 | #endif | 
|---|
| 346 |  | 
|---|
| 347 | teb->o.odin.logfile = (DWORD)flog; | 
|---|
| 348 |  | 
|---|
| 349 | #ifdef LOG_TIME | 
|---|
| 350 | if(sel == 0x150b && !fIsOS2Image) | 
|---|
| 351 | fprintf(flog, | 
|---|
| 352 | "t%02d (%3d): (%x) (FS=150B) ", | 
|---|
| 353 | teb->o.odin.threadId, | 
|---|
| 354 | ulCallDepth, | 
|---|
| 355 | GetTickCount()); | 
|---|
| 356 | else | 
|---|
| 357 | fprintf(flog, | 
|---|
| 358 | "t%02d (%3d): (%x) ", | 
|---|
| 359 | teb->o.odin.threadId, | 
|---|
| 360 | ulCallDepth, | 
|---|
| 361 | GetTickCount()); | 
|---|
| 362 | #else | 
|---|
| 363 | if(sel == 0x150b && !fIsOS2Image) | 
|---|
| 364 | fprintf(flog, | 
|---|
| 365 | "t%02d (%3d): (FS=150B) ", | 
|---|
| 366 | teb->o.odin.threadId, | 
|---|
| 367 | ulCallDepth); | 
|---|
| 368 | else | 
|---|
| 369 | fprintf(flog, | 
|---|
| 370 | "t%02d (%3d): ", | 
|---|
| 371 | teb->o.odin.threadId, | 
|---|
| 372 | ulCallDepth); | 
|---|
| 373 | #endif | 
|---|
| 374 | } | 
|---|
| 375 | #ifdef LOG_TIME | 
|---|
| 376 | else { | 
|---|
| 377 | fprintf(flog, "tX: (%x) ", GetTickCount()); | 
|---|
| 378 | } | 
|---|
| 379 | #endif | 
|---|
| 380 | vfprintf(flog, tekst, argptr); | 
|---|
| 381 | if(teb) teb->o.odin.logfile = 0; | 
|---|
| 382 | va_end(argptr); | 
|---|
| 383 |  | 
|---|
| 384 | if(tekst[strlen(tekst)-1] != '\n') | 
|---|
| 385 | fprintf(flog, "\n"); | 
|---|
| 386 |  | 
|---|
| 387 | if(fFlushLines) | 
|---|
| 388 | fflush(flog); | 
|---|
| 389 | } | 
|---|
| 390 | SetFS(sel); | 
|---|
| 391 | return 1; | 
|---|
| 392 | } | 
|---|
| 393 | //****************************************************************************** | 
|---|
| 394 | //****************************************************************************** | 
|---|
| 395 | int SYSTEM WriteLogNoEOL(char *tekst, ...) | 
|---|
| 396 | { | 
|---|
| 397 | USHORT  sel = RestoreOS2FS(); | 
|---|
| 398 | va_list argptr; | 
|---|
| 399 |  | 
|---|
| 400 | ODIN_HEAPCHECK(); | 
|---|
| 401 |  | 
|---|
| 402 | if(!init) | 
|---|
| 403 | { | 
|---|
| 404 | init = TRUE; | 
|---|
| 405 |  | 
|---|
| 406 | #ifdef DEFAULT_LOGGING_OFF | 
|---|
| 407 | if(getenv("WIN32LOG_ENABLED")) { | 
|---|
| 408 | #else | 
|---|
| 409 | if(!getenv("NOWIN32LOG")) { | 
|---|
| 410 | #endif | 
|---|
| 411 | char logname[CCHMAXPATH]; | 
|---|
| 412 |  | 
|---|
| 413 | sprintf(logname, "odin32_%d.log", loadNr); | 
|---|
| 414 | flog = fopen(logname, "w"); | 
|---|
| 415 | if(flog == NULL) {//probably running exe on readonly device | 
|---|
| 416 | sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr); | 
|---|
| 417 | flog = fopen(logname, "w"); | 
|---|
| 418 | } | 
|---|
| 419 | } | 
|---|
| 420 | else | 
|---|
| 421 | fLogging = FALSE; | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 | if(fLogging && flog && (dwEnableLogging > 0)) | 
|---|
| 425 | { | 
|---|
| 426 | TEB *teb = GetThreadTEB(); | 
|---|
| 427 |  | 
|---|
| 428 | va_start(argptr, tekst); | 
|---|
| 429 | if(teb) { | 
|---|
| 430 | teb->o.odin.logfile = (DWORD)flog; | 
|---|
| 431 | } | 
|---|
| 432 | vfprintf(flog, tekst, argptr); | 
|---|
| 433 | if(teb) teb->o.odin.logfile = 0; | 
|---|
| 434 | va_end(argptr); | 
|---|
| 435 | } | 
|---|
| 436 | SetFS(sel); | 
|---|
| 437 | return 1; | 
|---|
| 438 | } | 
|---|
| 439 | //****************************************************************************** | 
|---|
| 440 | //****************************************************************************** | 
|---|
| 441 | void SYSTEM DecreaseLogCount() | 
|---|
| 442 | { | 
|---|
| 443 | dwEnableLogging--; | 
|---|
| 444 | } | 
|---|
| 445 | //****************************************************************************** | 
|---|
| 446 | //****************************************************************************** | 
|---|
| 447 | void SYSTEM IncreaseLogCount() | 
|---|
| 448 | { | 
|---|
| 449 | dwEnableLogging++; | 
|---|
| 450 | } | 
|---|
| 451 | //****************************************************************************** | 
|---|
| 452 | //****************************************************************************** | 
|---|
| 453 | int SYSTEM WritePrivateLog(void *logfile, char *tekst, ...) | 
|---|
| 454 | { | 
|---|
| 455 | USHORT  sel = RestoreOS2FS(); | 
|---|
| 456 | va_list argptr; | 
|---|
| 457 |  | 
|---|
| 458 | if(fLogging && logfile) | 
|---|
| 459 | { | 
|---|
| 460 | TEB *teb = GetThreadTEB(); | 
|---|
| 461 |  | 
|---|
| 462 | va_start(argptr, tekst); | 
|---|
| 463 | if(teb) { | 
|---|
| 464 | teb->o.odin.logfile = (DWORD)flog; | 
|---|
| 465 | } | 
|---|
| 466 | vfprintf((FILE *)logfile, tekst, argptr); | 
|---|
| 467 | if(teb) teb->o.odin.logfile = 0; | 
|---|
| 468 | va_end(argptr); | 
|---|
| 469 |  | 
|---|
| 470 | if(tekst[strlen(tekst)-1] != '\n') | 
|---|
| 471 | fprintf((FILE *)logfile, "\n"); | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | SetFS(sel); | 
|---|
| 475 | return 1; | 
|---|
| 476 | } | 
|---|
| 477 | //****************************************************************************** | 
|---|
| 478 | //WriteLog has to take special care to handle dprintfs inside our os/2 exception | 
|---|
| 479 | //handler; if an exception occurs inside a dprintf, using dprintf in the exception | 
|---|
| 480 | //handler will hang the process | 
|---|
| 481 | //****************************************************************************** | 
|---|
| 482 | void LogException(int state) | 
|---|
| 483 | { | 
|---|
| 484 | TEB *teb = GetThreadTEB(); | 
|---|
| 485 |  | 
|---|
| 486 | if (!teb) return; | 
|---|
| 487 |  | 
|---|
| 488 | #if !defined(__EMX__) | 
|---|
| 489 | if (teb->o.odin.logfile) | 
|---|
| 490 | { | 
|---|
| 491 | #if (__IBMCPP__ == 300) || (__IBMC__ == 300) | 
|---|
| 492 | PUSHORT lock = (USHORT *)(teb->o.odin.logfile+0x1C); | 
|---|
| 493 | #else | 
|---|
| 494 | #if __IBMC__ >= 360 || __IBMCPP__ >= 360 | 
|---|
| 495 | //TODO: test this!!!!!!! | 
|---|
| 496 | PUSHORT lock = (USHORT *)(teb->o.odin.logfile+0x1C); | 
|---|
| 497 | #else | 
|---|
| 498 | #error Check the offset of the lock count word in the file stream structure for this compiler revision!!!!! | 
|---|
| 499 | #endif | 
|---|
| 500 | #endif | 
|---|
| 501 | if (state == ENTER_EXCEPTION) | 
|---|
| 502 | { | 
|---|
| 503 | (*lock)--; | 
|---|
| 504 | } | 
|---|
| 505 | else | 
|---|
| 506 | { //LEAVE_EXCEPTION | 
|---|
| 507 | (*lock)++; | 
|---|
| 508 | } | 
|---|
| 509 | } | 
|---|
| 510 | #else | 
|---|
| 511 | //kso 2001-01-29: EMX/GCC | 
|---|
| 512 | // we maybe should do something with the _more->rsem (_rmutex) structure but | 
|---|
| 513 | // I wanna have this compile, so we'll address problems later. | 
|---|
| 514 | #endif | 
|---|
| 515 | } | 
|---|
| 516 | //****************************************************************************** | 
|---|
| 517 | //Check if the exception occurred inside a fprintf (logging THDB member set) | 
|---|
| 518 | //If true, decrease the lock count for that file stream | 
|---|
| 519 | //NOTE: HACK: DEPENDS ON COMPILER VERSION!!!! | 
|---|
| 520 | //****************************************************************************** | 
|---|
| 521 | void CheckLogException() | 
|---|
| 522 | { | 
|---|
| 523 | TEB *teb = GetThreadTEB(); | 
|---|
| 524 | PUSHORT lock; | 
|---|
| 525 |  | 
|---|
| 526 | if(!teb) return; | 
|---|
| 527 |  | 
|---|
| 528 | #if !defined(__EMX__) | 
|---|
| 529 | if(teb->o.odin.logfile) { | 
|---|
| 530 | //oops, exception in vfprintf; let's clear the lock count | 
|---|
| 531 | #if (__IBMCPP__ == 300) || (__IBMC__ == 300) | 
|---|
| 532 | lock = (PUSHORT)(teb->o.odin.logfile+0x1C); | 
|---|
| 533 | #else | 
|---|
| 534 | #if __IBMC__ >= 360 || __IBMCPP__ >= 360 | 
|---|
| 535 | //TODO: test this!!!!!!! | 
|---|
| 536 | PUSHORT lock = (USHORT *)(teb->o.odin.logfile+0x1C); | 
|---|
| 537 | #else | 
|---|
| 538 | #error Check the offset of the lock count word in the file stream structure for this compiler revision!!!!! | 
|---|
| 539 | #endif | 
|---|
| 540 | #endif | 
|---|
| 541 | (*lock)--; | 
|---|
| 542 | } | 
|---|
| 543 | #else | 
|---|
| 544 | //kso 2001-01-29: EMX/GCC | 
|---|
| 545 | // we maybe should do something with the _more->rsem (_rmutex) structure but | 
|---|
| 546 | // I wanna have this compile, so we'll address problems later. | 
|---|
| 547 | #endif | 
|---|
| 548 | } | 
|---|
| 549 | //****************************************************************************** | 
|---|
| 550 | //NOTE: No need to save/restore FS, as our FS selectors have already been | 
|---|
| 551 | //      destroyed and FS == 0x150B. | 
|---|
| 552 | //****************************************************************************** | 
|---|
| 553 | void CloseLogFile() | 
|---|
| 554 | { | 
|---|
| 555 | if(oldcrtmsghandle) | 
|---|
| 556 | _set_crt_msg_handle(oldcrtmsghandle); | 
|---|
| 557 |  | 
|---|
| 558 | fclose(flog); | 
|---|
| 559 | flog = 0; | 
|---|
| 560 | } | 
|---|
| 561 | //****************************************************************************** | 
|---|
| 562 | //Used to open any private logfiles used in kernel32 (for now only in winimagepeldr.cpp) | 
|---|
| 563 | //****************************************************************************** | 
|---|
| 564 | void OpenPrivateLogFiles() | 
|---|
| 565 | { | 
|---|
| 566 | #ifdef DEFAULT_LOGGING_OFF | 
|---|
| 567 | if(getenv("WIN32LOG_ENABLED")) { | 
|---|
| 568 | #else | 
|---|
| 569 | if(!getenv("NOWIN32LOG")) { | 
|---|
| 570 | #endif | 
|---|
| 571 | OpenPrivateLogFilePE(); | 
|---|
| 572 | } | 
|---|
| 573 | } | 
|---|
| 574 | //****************************************************************************** | 
|---|
| 575 | //Used to close all private logfiles used in kernel32 (for now only in winimagepeldr.cpp) | 
|---|
| 576 | //****************************************************************************** | 
|---|
| 577 | void ClosePrivateLogFiles() | 
|---|
| 578 | { | 
|---|
| 579 | #ifdef DEFAULT_LOGGING_OFF | 
|---|
| 580 | if(getenv("WIN32LOG_ENABLED")) { | 
|---|
| 581 | #else | 
|---|
| 582 | if(!getenv("NOWIN32LOG")) { | 
|---|
| 583 | #endif | 
|---|
| 584 | ClosePrivateLogFilePE(); | 
|---|
| 585 | } | 
|---|
| 586 | } | 
|---|
| 587 | //****************************************************************************** | 
|---|
| 588 | //****************************************************************************** | 
|---|
| 589 | int SYSTEM WriteLogError(char *tekst, ...) | 
|---|
| 590 | { | 
|---|
| 591 | USHORT  sel = RestoreOS2FS(); | 
|---|
| 592 | va_list argptr; | 
|---|
| 593 |  | 
|---|
| 594 | va_start(argptr, tekst); | 
|---|
| 595 | printf("ERROR: "); | 
|---|
| 596 | vprintf(tekst, argptr); | 
|---|
| 597 | va_end(argptr); | 
|---|
| 598 | if(tekst[strlen(tekst)-1] != '\n') | 
|---|
| 599 | printf("\n"); | 
|---|
| 600 |  | 
|---|
| 601 | SetFS(sel); | 
|---|
| 602 | return 1; | 
|---|
| 603 | } | 
|---|
| 604 | //****************************************************************************** | 
|---|
| 605 | //****************************************************************************** | 
|---|
| 606 | void SYSTEM CheckVersion(ULONG version, char *modname) | 
|---|
| 607 | { | 
|---|
| 608 | dprintf(("CheckVersion of %s, %d\n", modname, version)); | 
|---|
| 609 | if(version != PE2LX_VERSION){ | 
|---|
| 610 | static char msg[300]; | 
|---|
| 611 | int r; | 
|---|
| 612 | dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname)); | 
|---|
| 613 | sprintf(msg, "%s is intended for use with a different release of Odin.\n", modname); | 
|---|
| 614 | do{ | 
|---|
| 615 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE); | 
|---|
| 616 | }while(r == MBID_RETRY);   // giggle | 
|---|
| 617 | if( r != MBID_IGNORE ) | 
|---|
| 618 | exit(987); | 
|---|
| 619 | } | 
|---|
| 620 | } | 
|---|
| 621 | //****************************************************************************** | 
|---|
| 622 | //****************************************************************************** | 
|---|
| 623 | void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule) | 
|---|
| 624 | { | 
|---|
| 625 | char name[_MAX_PATH]; | 
|---|
| 626 |  | 
|---|
| 627 | // query name of dll. | 
|---|
| 628 | if(!DosQueryModuleName(hModule, sizeof(name), name)) | 
|---|
| 629 | CheckVersion(version, name); | 
|---|
| 630 | } | 
|---|
| 631 | //****************************************************************************** | 
|---|
| 632 | //****************************************************************************** | 
|---|
| 633 | #ifdef __WATCOMC__  /*PLF Sat  97-06-21 17:12:36*/ | 
|---|
| 634 | extern void interrupt3( void ); | 
|---|
| 635 | #pragma aux interrupt3= \ | 
|---|
| 636 | "int 3" | 
|---|
| 637 | #endif | 
|---|
| 638 | void WIN32API DebugBreak() | 
|---|
| 639 | { | 
|---|
| 640 | dprintf(("DebugBreak\n")); | 
|---|
| 641 |  | 
|---|
| 642 | LPSTR lpstrEnv = getenv("WIN32.DEBUGBREAK");              /* query environment */ | 
|---|
| 643 | if (lpstrEnv == NULL)      /* if environment is not set, don't call debugger ! */ | 
|---|
| 644 | return; | 
|---|
| 645 |  | 
|---|
| 646 | #ifdef __WATCOMC__ | 
|---|
| 647 | interrupt3(); | 
|---|
| 648 | #else | 
|---|
| 649 | _interrupt(3); | 
|---|
| 650 | #endif | 
|---|
| 651 | } | 
|---|
| 652 | //****************************************************************************** | 
|---|
| 653 | //****************************************************************************** | 
|---|
| 654 |  | 
|---|
| 655 |  | 
|---|
| 656 | /***************************************************************************** | 
|---|
| 657 | * Name      : DebugErrorBox | 
|---|
| 658 | * Purpose   : display an apprioriate error box with detailed information | 
|---|
| 659 | *             about the error cause | 
|---|
| 660 | * Parameters: APIRET iErrorCode - OS/2 error code | 
|---|
| 661 | *             PSZ    pszFormat  - printf-format string | 
|---|
| 662 | *             ... | 
|---|
| 663 | * Variables : | 
|---|
| 664 | * Result    : return code of the message box | 
|---|
| 665 | * Remark    : | 
|---|
| 666 | * Status    : | 
|---|
| 667 | * | 
|---|
| 668 | * Author    : Patrick Haller [Tue, 1999/09/13 19:55] | 
|---|
| 669 | *****************************************************************************/ | 
|---|
| 670 |  | 
|---|
| 671 | int SYSTEM DebugErrorBox(ULONG  iErrorCode, | 
|---|
| 672 | char*  pszFormat, | 
|---|
| 673 | ...) | 
|---|
| 674 | { | 
|---|
| 675 | char   szMessageBuffer[1024];              /* buffer for the text message */ | 
|---|
| 676 | char   szErrorBuffer[1024];       /* buffer for the operating system text */ | 
|---|
| 677 | ULONG  bc;                                                       /* dummy */ | 
|---|
| 678 | APIRET rc2;                                             /* API returncode */ | 
|---|
| 679 | int    iRC;                                    /* message box return code */ | 
|---|
| 680 |  | 
|---|
| 681 | USHORT  sel = RestoreOS2FS(); | 
|---|
| 682 | va_list argptr; | 
|---|
| 683 |  | 
|---|
| 684 | // clear memory | 
|---|
| 685 | memset (szMessageBuffer, 0, sizeof(szMessageBuffer)); | 
|---|
| 686 | memset (szErrorBuffer, 0, sizeof(szErrorBuffer)); | 
|---|
| 687 |  | 
|---|
| 688 | // build message string | 
|---|
| 689 | va_start(argptr, pszFormat); | 
|---|
| 690 | vsprintf(szMessageBuffer, pszFormat, argptr); | 
|---|
| 691 | va_end(argptr); | 
|---|
| 692 |  | 
|---|
| 693 | // query error string | 
|---|
| 694 | rc2 = DosGetMessage(NULL, | 
|---|
| 695 | 0, | 
|---|
| 696 | szErrorBuffer, | 
|---|
| 697 | sizeof(szErrorBuffer), | 
|---|
| 698 | iErrorCode, | 
|---|
| 699 | "OSO001.MSG", | 
|---|
| 700 | &bc); | 
|---|
| 701 |  | 
|---|
| 702 | // display message box | 
|---|
| 703 | iRC = WinMessageBox(HWND_DESKTOP, | 
|---|
| 704 | NULLHANDLE, | 
|---|
| 705 | szMessageBuffer, | 
|---|
| 706 | szErrorBuffer, | 
|---|
| 707 | 0, | 
|---|
| 708 | MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE); | 
|---|
| 709 | SetFS(sel); | 
|---|
| 710 | return iRC; | 
|---|
| 711 | } | 
|---|
| 712 |  | 
|---|
| 713 |  | 
|---|
| 714 | /** | 
|---|
| 715 | * Query Odin32 build number. | 
|---|
| 716 | * @returns Build number. | 
|---|
| 717 | * @status  Completely implemented. | 
|---|
| 718 | * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 719 | */ | 
|---|
| 720 | int     WIN32API Odin32GetBuildNumber(void) | 
|---|
| 721 | { | 
|---|
| 722 | dprintf(("Odin32GetBuildNumber returned %d\n", ODIN32_BUILD_NR)); | 
|---|
| 723 | return ODIN32_BUILD_NR; | 
|---|
| 724 | } | 
|---|
| 725 |  | 
|---|