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

Last change on this file since 1708 was 1708, checked in by sandervl, 26 years ago

version loading fixes + heap corruption fix

File size: 15.5 KB
Line 
1/* $Id: misc.cpp,v 1.14 1999-11-11 19:10:09 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
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
48extern 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 ---- */
60struct 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 ----- */
68static ULONG ourpid=0; /* our process ID */
69static ULONG servepid=0; /* process IDs of the server */
70static HQUEUE qhandle=0; /* handle for the queue */
71static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
72
73/* ----- Local subroutine ----- */
74static int printf_(struct perthread *);
75
76
77/* ----------------------------------------------------------------- */
78/* The "printf" function. Note this has a variable number of */
79/* arguments. */
80/* ----------------------------------------------------------------- */
81int 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. */
189int 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
241static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
242static BOOL init = FALSE;
243static BOOL fLogging = TRUE;
244
245int SYSTEM EXPORT WriteLog(char *tekst, ...)
246{
247 USHORT sel = RestoreOS2FS();
248 va_list argptr;
249
250 if(!init)
251 {
252 init = TRUE;
253
254#ifdef DEFAULT_LOGGING_OFF
255 if(getenv("WIN32LOG_ENABLED")) {
256#else
257 if(!getenv("NOWIN32LOG")) {
258#endif
259 char logname[CCHMAXPATH];
260
261 sprintf(logname, "odin32_%d.log", loadNr);
262 flog = fopen(logname, "w");
263 if(flog == NULL) {//probably running exe on readonly device
264 sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr);
265 flog = fopen(logname, "w");
266 }
267 }
268 else
269 fLogging = FALSE;
270 }
271
272 if(fLogging && flog)
273 {
274 va_start(argptr, tekst);
275 vfprintf(flog, tekst, argptr);
276 va_end(argptr);
277
278 if(tekst[strlen(tekst)-1] != '\n')
279 fprintf(flog, "\n");
280 }
281
282 SetFS(sel);
283 return 1;
284}
285//******************************************************************************
286//NOTE: No need to save/restore FS, as our FS selectors have already been
287// destroyed and FS == 0x150B.
288//******************************************************************************
289void CloseLogFile()
290{
291 fclose(flog);
292 flog = 0;
293}
294//******************************************************************************
295//******************************************************************************
296int SYSTEM EXPORT WriteLogError(char *tekst, ...)
297{
298 USHORT sel = RestoreOS2FS();
299 va_list argptr;
300
301 va_start(argptr, tekst);
302 printf("ERROR: ");
303 vprintf(tekst, argptr);
304 va_end(argptr);
305 if(tekst[strlen(tekst)-1] != '\n')
306 printf("\n");
307
308 SetFS(sel);
309 return 1;
310}
311//******************************************************************************
312//******************************************************************************
313void SYSTEM CheckVersion(ULONG version, char *modname)
314{
315 dprintf(("CheckVersion of %s, %d\n", modname, version));
316 if(version != PE2LX_VERSION){
317 static char msg[300];
318 int r;
319 dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
320 sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
321 do{
322 r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
323 }while(r == MBID_RETRY); // giggle
324 if( r != MBID_IGNORE )
325 exit(987);
326 }
327}
328//******************************************************************************
329//******************************************************************************
330void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
331{
332 char name[_MAX_PATH];
333
334 // query name of dll.
335 if(!DosQueryModuleName(hModule, sizeof(name), name))
336 CheckVersion(version, name);
337}
338//******************************************************************************
339//******************************************************************************
340#ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
341 extern void interrupt3( void );
342 #pragma aux interrupt3= \
343 "int 3"
344#endif
345void WIN32API DebugBreak()
346{
347 dprintf(("DebugBreak\n"));
348
349 LPSTR lpstrEnv = getenv("WIN32.DEBUGBREAK"); /* query environment */
350 if (lpstrEnv == NULL) /* if environment is not set, don't call debugger ! */
351 return;
352
353#ifdef __WATCOMC__
354 interrupt3();
355#else
356 _interrupt(3);
357#endif
358}
359//******************************************************************************
360//******************************************************************************
361
362
363/*****************************************************************************
364 * Name : DebugErrorBox
365 * Purpose : display an apprioriate error box with detailed information
366 * about the error cause
367 * Parameters: APIRET iErrorCode - OS/2 error code
368 * PSZ pszFormat - printf-format string
369 * ...
370 * Variables :
371 * Result : return code of the message box
372 * Remark :
373 * Status :
374 *
375 * Author : Patrick Haller [Tue, 1999/09/13 19:55]
376 *****************************************************************************/
377
378int SYSTEM EXPORT DebugErrorBox(ULONG iErrorCode,
379 char* pszFormat,
380 ...)
381{
382 char szMessageBuffer[1024]; /* buffer for the text message */
383 char szErrorBuffer[1024]; /* buffer for the operating system text */
384 ULONG bc; /* dummy */
385 APIRET rc2; /* API returncode */
386 int iRC; /* message box return code */
387
388 USHORT sel = RestoreOS2FS();
389 va_list argptr;
390
391 // clear memory
392 memset (szMessageBuffer, 0, sizeof(szMessageBuffer));
393 memset (szErrorBuffer, 0, sizeof(szErrorBuffer));
394
395 // build message string
396 va_start(argptr, pszFormat);
397 vsprintf(szMessageBuffer, pszFormat, argptr);
398 va_end(argptr);
399
400 // query error string
401 rc2 = DosGetMessage(NULL,
402 0,
403 szErrorBuffer,
404 sizeof(szErrorBuffer),
405 iErrorCode,
406 "OSO001.MSG",
407 &bc);
408
409 // display message box
410 iRC = WinMessageBox(HWND_DESKTOP,
411 NULLHANDLE,
412 szMessageBuffer,
413 szErrorBuffer,
414 0,
415 MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
416 SetFS(sel);
417 return iRC;
418}
Note: See TracBrowser for help on using the repository browser.