| 1 | /* $Id: misc.cpp,v 1.11 1999-09-13 14:57:07 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 | * 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 |
|
|---|
| 31 | /*****************************************************************************
|
|---|
| 32 | * PMPRINTF Version *
|
|---|
| 33 | *****************************************************************************/
|
|---|
| 34 |
|
|---|
| 35 | #ifdef PMPRINTF
|
|---|
| 36 |
|
|---|
| 37 | /* ----- Customization variables ----- */
|
|---|
| 38 | #define PRINTFID ""
|
|---|
| 39 | #define PRINTFMAXLEN 300
|
|---|
| 40 | #define PRINTFLINELEN 100
|
|---|
| 41 | #define PRINTFTHREADS 54
|
|---|
| 42 | #define PRINTFQNAME "\\QUEUES\\PRINTF32"
|
|---|
| 43 |
|
|---|
| 44 | /* ----- Includes and externals ----- */
|
|---|
| 45 | #include <stddef.h> /* .. */
|
|---|
| 46 | #include <time.h> /* .. */
|
|---|
| 47 |
|
|---|
| 48 | extern ULONG flAllocMem; /*Tue 03.03.1998: knut */
|
|---|
| 49 |
|
|---|
| 50 | /* ----- Local defines ----- */
|
|---|
| 51 | #define PRINTFIDSIZE sizeof(PRINTFID)
|
|---|
| 52 | #define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | /*****************************************************************************
|
|---|
| 56 | * Structures *
|
|---|
| 57 | *****************************************************************************/
|
|---|
| 58 |
|
|---|
| 59 | /* ----- Per-thread output buffer and current indices into line ---- */
|
|---|
| 60 | struct perthread {
|
|---|
| 61 | LONG lineindex; /* where next char */
|
|---|
| 62 | LONG tidemark; /* rightmost char */
|
|---|
| 63 | int bell; /* TRUE if line has bell */
|
|---|
| 64 | UCHAR line[PRINTFMAXBUF]; /* accumulator */
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | /* ----- Local static variables ----- */
|
|---|
| 68 | static ULONG ourpid=0; /* our process ID */
|
|---|
| 69 | static ULONG servepid=0; /* process IDs of the server */
|
|---|
| 70 | static HQUEUE qhandle=0; /* handle for the queue */
|
|---|
| 71 | static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
|
|---|
| 72 |
|
|---|
| 73 | /* ----- Local subroutine ----- */
|
|---|
| 74 | static int printf_(struct perthread *);
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | /* ----------------------------------------------------------------- */
|
|---|
| 78 | /* The "printf" function. Note this has a variable number of */
|
|---|
| 79 | /* arguments. */
|
|---|
| 80 | /* ----------------------------------------------------------------- */
|
|---|
| 81 | int SYSTEM EXPORT WriteLog(char *f, ...)
|
|---|
| 82 | {
|
|---|
| 83 | TIB *ptib; /* process/thread id structures */
|
|---|
| 84 | PIB *ppib; /* .. */
|
|---|
| 85 | TID ourtid; /* thread ID */
|
|---|
| 86 | struct perthread *tp; /* pointer to per-thread data */
|
|---|
| 87 | int rc; /* returncode */
|
|---|
| 88 | ULONG urc; /* returncode */
|
|---|
| 89 |
|
|---|
| 90 | urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME); /* Open the Q */
|
|---|
| 91 | /* Non-0 RC means Q does not exist or cannot be opened */
|
|---|
| 92 | if (urc==343) return 0; /* queue does not exist, so quit */
|
|---|
| 93 | if (urc!=0) return -1; /* report any other error */
|
|---|
| 94 |
|
|---|
| 95 | /* First determine our thread ID (and hence get access to the */
|
|---|
| 96 | /* correct per-thread data. If the per-thread data has not been */
|
|---|
| 97 | /* allocated, then allocate it now. It is never freed, once */
|
|---|
| 98 | /* allocated, as PRINTF is not notified of end-of-thread. */
|
|---|
| 99 | DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
|
|---|
| 100 | ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
|
|---|
| 101 | if (ourtid>PRINTFTHREADS) /* too many threads .. */
|
|---|
| 102 | return 0; /* .. so quit, quietly */
|
|---|
| 103 | tp=tps[ourtid]; /* copy to local pointer */
|
|---|
| 104 | if (tp==NULL) { /* uninitialized (NULL=0) */
|
|---|
| 105 | /* allocate a per-thread structure */
|
|---|
| 106 | tp=(struct perthread *)malloc(sizeof(struct perthread));
|
|---|
| 107 | if (tp==NULL) return -1; /* out of memory -- return error */
|
|---|
| 108 | tps[ourtid]=tp; /* save for future calls */
|
|---|
| 109 | strcpy(tp->line,PRINTFID); /* initialize: line.. */
|
|---|
| 110 | tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
|
|---|
| 111 | tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
|
|---|
| 112 | tp->bell=FALSE; /* ..if line has bell */
|
|---|
| 113 | if (ourpid==0) ourpid=ppib->pib_ulpid; /* save PID for all to use */
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | { /* Block for declarations -- only needed if queue exists, etc. */
|
|---|
| 117 | LONG count; /* count of characters formatted */
|
|---|
| 118 | UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
|
|---|
| 119 | LONG i, newind; /* work */
|
|---|
| 120 | UCHAR ch; /* .. */
|
|---|
| 121 | va_list argptr; /* -> variable argument list */
|
|---|
| 122 |
|
|---|
| 123 | va_start(argptr, f); /* get pointer to argument list */
|
|---|
| 124 | count=vsprintf(buffer, f, argptr);
|
|---|
| 125 | va_end(argptr); /* done with variable arguments */
|
|---|
| 126 |
|
|---|
| 127 | if (count<0) return count-1000;/* bad start */
|
|---|
| 128 |
|
|---|
| 129 | if (count>PRINTFMAXLEN) {
|
|---|
| 130 | /* Disaster -- we are probably "dead", but just in case we */
|
|---|
| 131 | /* are not, carry on with truncated data. */
|
|---|
| 132 | count=PRINTFMAXLEN;
|
|---|
| 133 | }
|
|---|
| 134 | buffer[count]='\0'; /* ensure terminated */
|
|---|
| 135 | /* OK, ready to go with the data now in BUFFER */
|
|---|
| 136 | /* We copy from the formatted string to the output (line) buffer, */
|
|---|
| 137 | /* taking note of certain control characters and sending a line */
|
|---|
| 138 | /* the queue whenever we see a LF control, or when the line */
|
|---|
| 139 | /* fills (causing a forced break). */
|
|---|
| 140 | for (i=0; ; i++) {
|
|---|
| 141 | ch=buffer[i]; if (!ch) break;
|
|---|
| 142 | switch(ch) {
|
|---|
| 143 | case '\r': /* carriage return */
|
|---|
| 144 | tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
|
|---|
| 145 | break;
|
|---|
| 146 | case '\n': /* new line */
|
|---|
| 147 | case '\f': /* form feed */
|
|---|
| 148 | rc=printf_(tp); /* print a line */
|
|---|
| 149 | if (rc!=0) return rc; /* error */
|
|---|
| 150 | break;
|
|---|
| 151 | case '\t': /* tab */
|
|---|
| 152 | newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
|
|---|
| 153 | newind=tp->lineindex+5-newind%5; /* new index requested */
|
|---|
| 154 | if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF; /* clamp */
|
|---|
| 155 | for (; tp->lineindex<newind; tp->lineindex++) {
|
|---|
| 156 | if (tp->lineindex>tp->tidemark) { /* beyond current end */
|
|---|
| 157 | tp->line[tp->lineindex]=' '; /* add space */
|
|---|
| 158 | tp->tidemark=tp->lineindex;
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | break;
|
|---|
| 162 | case '\v': /* vertical tab */
|
|---|
| 163 | /* ignore it */
|
|---|
| 164 | break;
|
|---|
| 165 | case '\b': /* backspace */
|
|---|
| 166 | tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
|
|---|
| 167 | break;
|
|---|
| 168 | case '\a': /* alert (bell) */
|
|---|
| 169 | tp->bell=TRUE;
|
|---|
| 170 | break;
|
|---|
| 171 | default: /* ordinary character */
|
|---|
| 172 | tp->line[tp->lineindex]=ch;
|
|---|
| 173 | if (tp->lineindex>tp->tidemark) /* is rightmost.. */
|
|---|
| 174 | tp->tidemark=tp->lineindex;
|
|---|
| 175 | tp->lineindex++; /* step for next */
|
|---|
| 176 | } /* switch */
|
|---|
| 177 | if (tp->lineindex>=PRINTFMAXBUF) {
|
|---|
| 178 | rc=printf_(tp); /* print a line */
|
|---|
| 179 | if (rc!=0) return rc; /* error */
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | } /* copy loop */
|
|---|
| 183 | return count; /* all formatted data processed */
|
|---|
| 184 | } /* block */
|
|---|
| 185 | } /* printf */
|
|---|
| 186 |
|
|---|
| 187 | /* ----- printf_(tp) -- Local subroutine to send a line ------------ */
|
|---|
| 188 | /* A line has been completed (or overflowed): write it to the queue. */
|
|---|
| 189 | int printf_(struct perthread *tp) /* pointer to per-thread data */
|
|---|
| 190 | {
|
|---|
| 191 | ULONG urc; /* unsigned returncode */
|
|---|
| 192 | PSZ pszTo, pszFrom; /* character pointers */
|
|---|
| 193 | PVOID addr; /* address of output data */
|
|---|
| 194 | long size; /* total size of output data */
|
|---|
| 195 | time_t timenow; /* holds current time */
|
|---|
| 196 |
|
|---|
| 197 | tp->line[tp->tidemark+1]='\0'; /* add terminator */
|
|---|
| 198 | size=tp->tidemark+2; /* total length of data */
|
|---|
| 199 |
|
|---|
| 200 | /* Get some shared memory that can be given away */
|
|---|
| 201 | urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
|
|---|
| 202 | OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
|
|---|
| 203 | /*knut: added flAllocMem */
|
|---|
| 204 |
|
|---|
| 205 | if (urc!=0) return -2; /* error */
|
|---|
| 206 |
|
|---|
| 207 | pszTo=addr; /* copy for clarity */
|
|---|
| 208 | pszFrom=&(tp->line[0]); /* pointer to source */
|
|---|
| 209 | strcpy(pszTo,pszFrom); /* copy the string to shared memory */
|
|---|
| 210 |
|
|---|
| 211 | if (ourpid!=servepid) { /* (no giveaway needed if to self) */
|
|---|
| 212 | urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
|
|---|
| 213 | if (urc!=0) return -3;} /* error */
|
|---|
| 214 |
|
|---|
| 215 | /* Write the selector, size, and timestamp to the queue */
|
|---|
| 216 | if (tp->bell) size=-size; /* BELL passed by negation */
|
|---|
| 217 | time(&timenow); /* optional - else use 0 */
|
|---|
| 218 | urc=DosWriteQueue(qhandle, /* handle */
|
|---|
| 219 | (unsigned)timenow, /* 'request' (timestamp) */
|
|---|
| 220 | (unsigned)size, /* 'length' (length/bell) */
|
|---|
| 221 | addr, /* 'address' (address) */
|
|---|
| 222 | 0); /* priority (FIFO if enabled) */
|
|---|
| 223 | if (urc!=0) return -4; /* error */
|
|---|
| 224 | if (ourpid!=servepid) { /* if given away.. */
|
|---|
| 225 | urc=DosFreeMem(addr); /* .. *we* are done with it */
|
|---|
| 226 | if (urc!=0) return -5;} /* error */
|
|---|
| 227 | /* Reset the line buffer and indices */
|
|---|
| 228 | tp->lineindex=PRINTFIDSIZE-1; /* where next char */
|
|---|
| 229 | tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
|
|---|
| 230 | tp->bell =FALSE; /* true if line has bell */
|
|---|
| 231 | return 0; /* success! */
|
|---|
| 232 | } /* printf_ */
|
|---|
| 233 | #endif
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | /*****************************************************************************
|
|---|
| 238 | * Standard Version *
|
|---|
| 239 | *****************************************************************************/
|
|---|
| 240 |
|
|---|
| 241 | static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
|
|---|
| 242 | static BOOL init = FALSE;
|
|---|
| 243 | static BOOL fLogging = TRUE;
|
|---|
| 244 |
|
|---|
| 245 | int SYSTEM EXPORT WriteLog(char *tekst, ...)
|
|---|
| 246 | {
|
|---|
| 247 | USHORT sel = RestoreOS2FS();
|
|---|
| 248 | va_list argptr;
|
|---|
| 249 |
|
|---|
| 250 | if(!init)
|
|---|
| 251 | {
|
|---|
| 252 | init = TRUE;
|
|---|
| 253 |
|
|---|
| 254 | if(!getenv("NOWIN32LOG")) {
|
|---|
| 255 | char logname[CCHMAXPATH];
|
|---|
| 256 |
|
|---|
| 257 | sprintf(logname, "odin32_%d.log", loadNr);
|
|---|
| 258 | flog = fopen(logname, "w");
|
|---|
| 259 | }
|
|---|
| 260 | else
|
|---|
| 261 | fLogging = FALSE;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | if(fLogging && flog)
|
|---|
| 265 | {
|
|---|
| 266 | va_start(argptr, tekst);
|
|---|
| 267 | vfprintf(flog, tekst, argptr);
|
|---|
| 268 | va_end(argptr);
|
|---|
| 269 |
|
|---|
| 270 | if(tekst[strlen(tekst)-1] != '\n')
|
|---|
| 271 | fprintf(flog, "\n");
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | SetFS(sel);
|
|---|
| 275 | return 1;
|
|---|
| 276 | }
|
|---|
| 277 | //******************************************************************************
|
|---|
| 278 | //NOTE: No need to save/restore FS, as our FS selectors have already been
|
|---|
| 279 | // destroyed and FS == 0x150B.
|
|---|
| 280 | //******************************************************************************
|
|---|
| 281 | void CloseLogFile()
|
|---|
| 282 | {
|
|---|
| 283 | fclose(flog);
|
|---|
| 284 | flog = 0;
|
|---|
| 285 | }
|
|---|
| 286 | //******************************************************************************
|
|---|
| 287 | //******************************************************************************
|
|---|
| 288 | int SYSTEM EXPORT WriteLogError(char *tekst, ...)
|
|---|
| 289 | {
|
|---|
| 290 | USHORT sel = RestoreOS2FS();
|
|---|
| 291 | va_list argptr;
|
|---|
| 292 |
|
|---|
| 293 | va_start(argptr, tekst);
|
|---|
| 294 | printf("ERROR: ");
|
|---|
| 295 | vprintf(tekst, argptr);
|
|---|
| 296 | va_end(argptr);
|
|---|
| 297 | if(tekst[strlen(tekst)-1] != '\n')
|
|---|
| 298 | printf("\n");
|
|---|
| 299 |
|
|---|
| 300 | SetFS(sel);
|
|---|
| 301 | return 1;
|
|---|
| 302 | }
|
|---|
| 303 | //******************************************************************************
|
|---|
| 304 | //******************************************************************************
|
|---|
| 305 | void SYSTEM CheckVersion(ULONG version, char *modname)
|
|---|
| 306 | {
|
|---|
| 307 | dprintf(("CheckVersion of %s, %d\n", modname, version));
|
|---|
| 308 | if(version != PE2LX_VERSION){
|
|---|
| 309 | static char msg[300];
|
|---|
| 310 | int r;
|
|---|
| 311 | dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
|
|---|
| 312 | sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
|
|---|
| 313 | do{
|
|---|
| 314 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
|---|
| 315 | }while(r == MBID_RETRY); // giggle
|
|---|
| 316 | if( r != MBID_IGNORE )
|
|---|
| 317 | exit(987);
|
|---|
| 318 | }
|
|---|
| 319 | }
|
|---|
| 320 | //******************************************************************************
|
|---|
| 321 | //******************************************************************************
|
|---|
| 322 | void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
|
|---|
| 323 | {
|
|---|
| 324 | char name[_MAX_PATH];
|
|---|
| 325 |
|
|---|
| 326 | // query name of dll.
|
|---|
| 327 | if(!DosQueryModuleName(hModule, sizeof(name), name))
|
|---|
| 328 | CheckVersion(version, name);
|
|---|
| 329 | }
|
|---|
| 330 | //******************************************************************************
|
|---|
| 331 | //******************************************************************************
|
|---|
| 332 | #ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
|
|---|
| 333 | extern void interrupt3( void );
|
|---|
| 334 | #pragma aux interrupt3= \
|
|---|
| 335 | "int 3"
|
|---|
| 336 | #endif
|
|---|
| 337 | void WIN32API DebugBreak()
|
|---|
| 338 | {
|
|---|
| 339 | dprintf(("DebugBreak\n"));
|
|---|
| 340 | #ifdef __WATCOMC__
|
|---|
| 341 | interrupt3();
|
|---|
| 342 | #else
|
|---|
| 343 | _interrupt(3);
|
|---|
| 344 | #endif
|
|---|
| 345 | }
|
|---|
| 346 | //******************************************************************************
|
|---|
| 347 | //******************************************************************************
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 | /*****************************************************************************
|
|---|
| 351 | * Name : DebugErrorBox
|
|---|
| 352 | * Purpose : display an apprioriate error box with detailed information
|
|---|
| 353 | * about the error cause
|
|---|
| 354 | * Parameters: APIRET iErrorCode - OS/2 error code
|
|---|
| 355 | * PSZ pszFormat - printf-format string
|
|---|
| 356 | * ...
|
|---|
| 357 | * Variables :
|
|---|
| 358 | * Result : return code of the message box
|
|---|
| 359 | * Remark :
|
|---|
| 360 | * Status :
|
|---|
| 361 | *
|
|---|
| 362 | * Author : Patrick Haller [Tue, 1999/09/13 19:55]
|
|---|
| 363 | *****************************************************************************/
|
|---|
| 364 |
|
|---|
| 365 | int SYSTEM EXPORT DebugErrorBox(ULONG iErrorCode,
|
|---|
| 366 | char* pszFormat,
|
|---|
| 367 | ...)
|
|---|
| 368 | {
|
|---|
| 369 | char szMessageBuffer[1024]; /* buffer for the text message */
|
|---|
| 370 | char szErrorBuffer[1024]; /* buffer for the operating system text */
|
|---|
| 371 | ULONG bc; /* dummy */
|
|---|
| 372 | APIRET rc2; /* API returncode */
|
|---|
| 373 | int iRC; /* message box return code */
|
|---|
| 374 |
|
|---|
| 375 | USHORT sel = RestoreOS2FS();
|
|---|
| 376 | va_list argptr;
|
|---|
| 377 |
|
|---|
| 378 | // clear memory
|
|---|
| 379 | memset (szMessageBuffer, 0, sizeof(szMessageBuffer));
|
|---|
| 380 | memset (szErrorBuffer, 0, sizeof(szErrorBuffer));
|
|---|
| 381 |
|
|---|
| 382 | // build message string
|
|---|
| 383 | va_start(argptr, pszFormat);
|
|---|
| 384 | vsprintf(szMessageBuffer, pszFormat, argptr);
|
|---|
| 385 | va_end(argptr);
|
|---|
| 386 |
|
|---|
| 387 | // query error string
|
|---|
| 388 | rc2 = DosGetMessage(NULL,
|
|---|
| 389 | 0,
|
|---|
| 390 | szErrorBuffer,
|
|---|
| 391 | sizeof(szErrorBuffer),
|
|---|
| 392 | iErrorCode,
|
|---|
| 393 | "OSO001.MSG",
|
|---|
| 394 | &bc);
|
|---|
| 395 |
|
|---|
| 396 | // display message box
|
|---|
| 397 | iRC = WinMessageBox(HWND_DESKTOP,
|
|---|
| 398 | NULLHANDLE,
|
|---|
| 399 | szMessageBuffer,
|
|---|
| 400 | szErrorBuffer,
|
|---|
| 401 | 0,
|
|---|
| 402 | MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
|---|
| 403 | SetFS(sel);
|
|---|
| 404 | return iRC;
|
|---|
| 405 | }
|
|---|