1 | /* $Id: misc.cpp,v 1.13 1999-10-25 21:38:50 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 | if(flog == NULL) {//probably running exe on readonly device
|
---|
260 | sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr);
|
---|
261 | flog = fopen(logname, "w");
|
---|
262 | }
|
---|
263 | }
|
---|
264 | else
|
---|
265 | fLogging = FALSE;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if(fLogging && flog)
|
---|
269 | {
|
---|
270 | va_start(argptr, tekst);
|
---|
271 | vfprintf(flog, tekst, argptr);
|
---|
272 | va_end(argptr);
|
---|
273 |
|
---|
274 | if(tekst[strlen(tekst)-1] != '\n')
|
---|
275 | fprintf(flog, "\n");
|
---|
276 | }
|
---|
277 |
|
---|
278 | SetFS(sel);
|
---|
279 | return 1;
|
---|
280 | }
|
---|
281 | //******************************************************************************
|
---|
282 | //NOTE: No need to save/restore FS, as our FS selectors have already been
|
---|
283 | // destroyed and FS == 0x150B.
|
---|
284 | //******************************************************************************
|
---|
285 | void CloseLogFile()
|
---|
286 | {
|
---|
287 | fclose(flog);
|
---|
288 | flog = 0;
|
---|
289 | }
|
---|
290 | //******************************************************************************
|
---|
291 | //******************************************************************************
|
---|
292 | int SYSTEM EXPORT WriteLogError(char *tekst, ...)
|
---|
293 | {
|
---|
294 | USHORT sel = RestoreOS2FS();
|
---|
295 | va_list argptr;
|
---|
296 |
|
---|
297 | va_start(argptr, tekst);
|
---|
298 | printf("ERROR: ");
|
---|
299 | vprintf(tekst, argptr);
|
---|
300 | va_end(argptr);
|
---|
301 | if(tekst[strlen(tekst)-1] != '\n')
|
---|
302 | printf("\n");
|
---|
303 |
|
---|
304 | SetFS(sel);
|
---|
305 | return 1;
|
---|
306 | }
|
---|
307 | //******************************************************************************
|
---|
308 | //******************************************************************************
|
---|
309 | void SYSTEM CheckVersion(ULONG version, char *modname)
|
---|
310 | {
|
---|
311 | dprintf(("CheckVersion of %s, %d\n", modname, version));
|
---|
312 | if(version != PE2LX_VERSION){
|
---|
313 | static char msg[300];
|
---|
314 | int r;
|
---|
315 | dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
|
---|
316 | sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
|
---|
317 | do{
|
---|
318 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
---|
319 | }while(r == MBID_RETRY); // giggle
|
---|
320 | if( r != MBID_IGNORE )
|
---|
321 | exit(987);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | //******************************************************************************
|
---|
325 | //******************************************************************************
|
---|
326 | void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
|
---|
327 | {
|
---|
328 | char name[_MAX_PATH];
|
---|
329 |
|
---|
330 | // query name of dll.
|
---|
331 | if(!DosQueryModuleName(hModule, sizeof(name), name))
|
---|
332 | CheckVersion(version, name);
|
---|
333 | }
|
---|
334 | //******************************************************************************
|
---|
335 | //******************************************************************************
|
---|
336 | #ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
|
---|
337 | extern void interrupt3( void );
|
---|
338 | #pragma aux interrupt3= \
|
---|
339 | "int 3"
|
---|
340 | #endif
|
---|
341 | void WIN32API DebugBreak()
|
---|
342 | {
|
---|
343 | dprintf(("DebugBreak\n"));
|
---|
344 |
|
---|
345 | LPSTR lpstrEnv = getenv("WIN32.DEBUGBREAK"); /* query environment */
|
---|
346 | if (lpstrEnv == NULL) /* if environment is not set, don't call debugger ! */
|
---|
347 | return;
|
---|
348 |
|
---|
349 | #ifdef __WATCOMC__
|
---|
350 | interrupt3();
|
---|
351 | #else
|
---|
352 | _interrupt(3);
|
---|
353 | #endif
|
---|
354 | }
|
---|
355 | //******************************************************************************
|
---|
356 | //******************************************************************************
|
---|
357 |
|
---|
358 |
|
---|
359 | /*****************************************************************************
|
---|
360 | * Name : DebugErrorBox
|
---|
361 | * Purpose : display an apprioriate error box with detailed information
|
---|
362 | * about the error cause
|
---|
363 | * Parameters: APIRET iErrorCode - OS/2 error code
|
---|
364 | * PSZ pszFormat - printf-format string
|
---|
365 | * ...
|
---|
366 | * Variables :
|
---|
367 | * Result : return code of the message box
|
---|
368 | * Remark :
|
---|
369 | * Status :
|
---|
370 | *
|
---|
371 | * Author : Patrick Haller [Tue, 1999/09/13 19:55]
|
---|
372 | *****************************************************************************/
|
---|
373 |
|
---|
374 | int SYSTEM EXPORT DebugErrorBox(ULONG iErrorCode,
|
---|
375 | char* pszFormat,
|
---|
376 | ...)
|
---|
377 | {
|
---|
378 | char szMessageBuffer[1024]; /* buffer for the text message */
|
---|
379 | char szErrorBuffer[1024]; /* buffer for the operating system text */
|
---|
380 | ULONG bc; /* dummy */
|
---|
381 | APIRET rc2; /* API returncode */
|
---|
382 | int iRC; /* message box return code */
|
---|
383 |
|
---|
384 | USHORT sel = RestoreOS2FS();
|
---|
385 | va_list argptr;
|
---|
386 |
|
---|
387 | // clear memory
|
---|
388 | memset (szMessageBuffer, 0, sizeof(szMessageBuffer));
|
---|
389 | memset (szErrorBuffer, 0, sizeof(szErrorBuffer));
|
---|
390 |
|
---|
391 | // build message string
|
---|
392 | va_start(argptr, pszFormat);
|
---|
393 | vsprintf(szMessageBuffer, pszFormat, argptr);
|
---|
394 | va_end(argptr);
|
---|
395 |
|
---|
396 | // query error string
|
---|
397 | rc2 = DosGetMessage(NULL,
|
---|
398 | 0,
|
---|
399 | szErrorBuffer,
|
---|
400 | sizeof(szErrorBuffer),
|
---|
401 | iErrorCode,
|
---|
402 | "OSO001.MSG",
|
---|
403 | &bc);
|
---|
404 |
|
---|
405 | // display message box
|
---|
406 | iRC = WinMessageBox(HWND_DESKTOP,
|
---|
407 | NULLHANDLE,
|
---|
408 | szMessageBuffer,
|
---|
409 | szErrorBuffer,
|
---|
410 | 0,
|
---|
411 | MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
---|
412 | SetFS(sel);
|
---|
413 | return iRC;
|
---|
414 | }
|
---|