source: GPL/trunk/lib32/debug.c@ 479

Last change on this file since 479 was 479, checked in by David Azarewicz, 15 years ago

Cleanup compiler warnings

File size: 17.3 KB
Line 
1/* $Id: debug.c,v 1.1.1.1 2003/07/02 13:57:02 eleph Exp $ */
2/*
3 * COM port debugging functions
4 *
5 * (C) 2000-2002 InnoTek Systemberatung GmbH
6 * (C) 2000-2001 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
21 * USA.
22 *
23 */
24#define INCL_NOPMAPI
25#define INCL_DOSERRORS // for ERROR_INVALID_FUNCTION
26#include <os2.h>
27
28#include <string.h>
29
30#define CR 0x0d
31#define LF 0x0a
32
33#define LEADING_ZEROES 0x8000
34#define SIGNIFICANT_FIELD 0x0007
35
36//#define COMM_DEBUG
37
38BOOL fLineTerminate=TRUE;
39int DebugLevel = 1;
40
41extern int wrOffset;
42extern char *szprintBuf;
43extern int max_buf_size;
44
45char hextab[]="0123456789ABCDEF";
46
47 //-------------------- DecLongToASCII -
48char * DecLongToASCII(char *StrPtr, ULONG lDecVal,USHORT Option)
49{
50 BOOL fNonZero=FALSE;
51 ULONG Digit;
52 ULONG Power=1000000000; // 1 billion
53 LONG lVal = (LONG)lDecVal;
54
55 if(lVal < 0) {
56 *StrPtr = '-';
57 StrPtr++;
58 lDecVal = -lDecVal;
59 }
60
61 while (Power)
62 {
63 Digit=0; // Digit=lDecVal/Power
64 while (lDecVal >=Power) // replaced with while loop
65 {
66 Digit++;
67 lDecVal-=Power;
68 }
69
70 if (Digit)
71 fNonZero=TRUE;
72
73 if (Digit ||
74 fNonZero ||
75 (Option & LEADING_ZEROES) ||
76 ((Power==1) && (fNonZero==FALSE)))
77 {
78 *StrPtr=(char)('0'+Digit);
79 StrPtr++;
80 }
81
82 if (Power==1000000000) // 1 billion
83 Power=100000000;
84 else if (Power==100000000)
85 Power=10000000;
86 else if (Power==10000000)
87 Power=1000000;
88 else if (Power==1000000)
89 Power=100000;
90 else if (Power==100000)
91 Power=10000;
92 else if (Power==10000)
93 Power=1000;
94 else if (Power==1000)
95 Power=100;
96 else if (Power==100)
97 Power=10;
98 else if (Power==10)
99 Power=1;
100 else
101 Power=0;
102 }
103 return (StrPtr);
104}
105 //-------------------- HexWordToASCII -
106 //-------------------- HexLongToASCII -
107char * HexLongToASCII(char *StrPtr, ULONG wHexVal, USHORT Option)
108{
109 BOOL fNonZero=FALSE;
110 ULONG Digit;
111 ULONG Power=0xF0000000;
112 ULONG ShiftVal=28;
113
114 while (Power)
115 {
116 Digit=(wHexVal & Power)>>ShiftVal;
117 if (Digit)
118 fNonZero=TRUE;
119
120 if (Digit ||
121 fNonZero ||
122 (Option & LEADING_ZEROES) ||
123 ((Power==0x0F) && (fNonZero==FALSE)))
124 *StrPtr++=hextab[Digit];
125
126 if (Power==0xF0000000) // 1 billion
127 Power=0xF000000;
128 else if (Power==0xF000000)
129 Power=0xF00000;
130 else if (Power==0xF00000)
131 Power=0xF0000;
132 else if (Power==0xF0000)
133 Power=0xF000;
134 else if (Power==0xF000)
135 Power=0xF00;
136 else if (Power==0xF00)
137 Power=0xF0;
138 else if (Power==0xF0)
139 Power=0xF;
140 else Power=0;
141
142 ShiftVal-=4;
143 } // end while
144
145 return (StrPtr);
146}
147
148#ifdef DEBUG
149//------------------------- StringOut --------------------------//
150
151#define VMDHA_FIXED 0x0002
152
153extern APIRET VMAlloc(ULONG size, ULONG flags, char NEAR* *pAddr);
154
155/**
156 * Finds the length of a string up to cchMax.
157 * @returns Length.
158 * @param psz Pointer to string.
159 * @param cchMax Max length.
160 */
161static unsigned _strnlen(const char *psz, unsigned cchMax)
162{
163 const char *pszC = psz;
164
165 while (cchMax-- > 0 && *psz != '\0')
166 psz++;
167
168 return psz - pszC;
169}
170
171#ifdef COMM_DEBUG
172short int MAGIC_COMM_PORT = 0; // pulled from word ptr 40:0
173
174
175#define UART_DATA 0x00 // UART Data port
176#define UART_INT_ENAB 0x01 // UART Interrupt enable
177#define UART_INT_ID 0x02 // interrupt ID
178#define UART_LINE_CTRL 0x03 // line control registers
179#define UART_MODEM_CTRL 0x04 // modem control register
180#define UART_LINE_STAT 0x05 // line status register
181#define UART_MODEM_STAT 0x06 // modem status regiser
182#define UART_DIVISOR_LO 0x00 // divisor latch least sig
183#define UART_DIVISOR_HI 0x01h // divisor latch most sig
184
185#define DELAY nop
186
187void CharOut(char c)
188{
189 if( MAGIC_COMM_PORT )
190 {
191
192 _asm {
193
194 mov dx, MAGIC_COMM_PORT // address of PS/2's first COM port
195 add dx, UART_LINE_STAT
196
197ReadyCheck:
198 in al, dx // wait for comm port ready signal
199
200 DELAY
201 DELAY
202 DELAY
203
204 test al, 020h
205 jz ReadyCheck
206
207 // Send the character
208
209 add dx, UART_DATA - UART_LINE_STAT
210 mov al,c
211 out dx, al
212
213 DELAY
214 DELAY
215 DELAY
216 }
217 }
218}
219#endif
220
221void StringOut(char *DbgStr)
222{
223 int len;
224#ifdef COMM_DEBUG
225 int i;
226#endif /* DEBUG */
227
228 len= _strnlen( DbgStr, 1024 );
229/*
230 while (*DbgStr)
231 CharOut(*DbgStr++);
232 */
233#ifdef COMM_DEBUG
234 if (MAGIC_COMM_PORT) //PS+++ If have comport - out to it
235 {
236 for( i= 0; i < len; i++ )
237 CharOut( DbgStr[i] );
238
239 if (fLineTerminate)
240 {
241 CharOut(CR); // append carriage return,
242 CharOut(LF); // linefeed
243 }
244 }
245#endif
246 if( szprintBuf == 0 )
247 {
248 VMAlloc( max_buf_size, VMDHA_FIXED, &szprintBuf );
249 if( szprintBuf )
250 memset( szprintBuf, 0, max_buf_size );
251 wrOffset= 0;
252 }
253 if( szprintBuf )
254 {
255 if( (len + wrOffset) > max_buf_size )
256 {
257 int cntr;
258 cntr= max_buf_size - wrOffset;
259 memcpy( szprintBuf + wrOffset, DbgStr, cntr );
260 DbgStr+= cntr;
261 len= len - cntr;
262 wrOffset= 0;
263 }
264 if( len )
265 {
266 memcpy( szprintBuf + wrOffset, DbgStr, len );
267 wrOffset= wrOffset + len;
268 if( wrOffset >= max_buf_size )
269 wrOffset= 0;
270 }
271 if (fLineTerminate)
272 {
273// if( (wrOffset+1) >= max_buf_size )
274// wrOffset= 0;
275 szprintBuf[wrOffset]= CR;
276 if( ++wrOffset >= max_buf_size )
277 wrOffset= 0;
278 szprintBuf[wrOffset]= LF;
279 if( ++wrOffset >= max_buf_size )
280 wrOffset= 0;
281 }
282 }
283}
284#endif
285
286#ifdef DEBUG
287char BuildString[1024];
288#endif // DEBUG
289
290//------------------------- PrintfOut -
291void _cdecl DPD(int level, char *DbgStr, ...)
292{
293#ifdef DEBUG
294 char *BuildPtr=BuildString;
295 char *pStr=(char *) DbgStr;
296 char *SubStr;
297 union {
298 void *VoidPtr;
299 USHORT *WordPtr;
300 ULONG *LongPtr;
301 ULONG *StringPtr;
302 } Parm;
303 USHORT wBuildOption;
304
305 Parm.VoidPtr=(void *) &DbgStr;
306 Parm.StringPtr++; // skip size of string pointer
307
308 while (*pStr)
309 {
310 // don't overflow target
311 if (BuildPtr >= (char *) &BuildString[1024-2])
312 break;
313
314 switch (*pStr)
315 {
316 case '%':
317 wBuildOption=0;
318 pStr++;
319 if (*pStr=='0')
320 {
321 wBuildOption|=LEADING_ZEROES;
322 pStr++;
323 }
324 if (*pStr=='u') // always unsigned
325 pStr++;
326 if (*pStr=='#')
327 pStr++;
328
329 switch(*pStr)
330 {
331 case 'x':
332 case 'X':
333 case 'p':
334 case 'P':
335 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
336 pStr++;
337 continue;
338
339 case 'd':
340 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
341 pStr++;
342 continue;
343
344 case 's':
345 SubStr=(char *)*Parm.StringPtr;
346 while (*BuildPtr++ = *SubStr++);
347 Parm.StringPtr++;
348 BuildPtr--; // remove the \0
349 pStr++;
350 continue;
351
352 case 'l':
353 pStr++;
354 switch (*pStr)
355 {
356 case 'x':
357 case 'X':
358 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
359 pStr++;
360 continue;
361
362 case 'd':
363 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
364 pStr++;
365 continue;
366 } // end switch
367 continue; // dunno what he wants
368
369 case 0:
370 continue;
371 } // end switch
372 break;
373
374 case '\\':
375 pStr++;
376 switch (*pStr)
377 {
378 case 'n':
379 *BuildPtr++=LF;
380 pStr++;
381 continue;
382
383 case 'r':
384 *BuildPtr++=CR;
385 pStr++;
386 continue;
387
388 case 0:
389 continue;
390 break;
391 } // end switch
392
393 break;
394 } // end switch
395
396 *BuildPtr++=*pStr++;
397 } // end while
398
399 *BuildPtr=0; // cauterize the string
400 StringOut((char *) BuildString);
401#endif //DEBUG
402}
403
404
405void _cdecl DPE(char *DbgStr, ...)
406{
407#ifdef DEBUG
408 char *BuildPtr=BuildString;
409 char *pStr = (char *) DbgStr;
410 char *SubStr;
411 union {
412 void *VoidPtr;
413 USHORT *WordPtr;
414 ULONG *LongPtr;
415 ULONG *StringPtr;
416 } Parm;
417 USHORT wBuildOption;
418
419 Parm.VoidPtr=(void *) &DbgStr;
420 Parm.StringPtr++; // skip size of string pointer
421
422 while (*pStr)
423 {
424 // don't overflow target
425 if (BuildPtr >= (char *) &BuildString[1024-2])
426 break;
427
428 switch (*pStr)
429 {
430 case '%':
431 wBuildOption=0;
432 pStr++;
433 if (*pStr=='0')
434 {
435 wBuildOption|=LEADING_ZEROES;
436 pStr++;
437 }
438// if (*pStr=='u') // always unsigned
439// pStr++;
440
441 switch(*pStr)
442 {
443 case 'x':
444 case 'X':
445 case 'p':
446 case 'P':
447 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
448 pStr++;
449 continue;
450
451 case 'd':
452 case 'u':
453 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
454 pStr++;
455 continue;
456
457 case 's':
458 SubStr=(char *)*Parm.StringPtr;
459 while (*BuildPtr++ = *SubStr++);
460 Parm.StringPtr++;
461 BuildPtr--; // remove the \0
462 pStr++;
463 continue;
464
465 case 'c':
466 *BuildPtr++ = (char)*Parm.LongPtr;
467 Parm.LongPtr++;
468 pStr++;
469 continue;
470
471 case 'l':
472 pStr++;
473 switch (*pStr)
474 {
475 case 'x':
476 case 'X':
477 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
478 pStr++;
479 continue;
480
481 case 'd':
482 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
483 pStr++;
484 continue;
485 } // end switch
486 continue; // dunno what he wants
487
488 case 0:
489 continue;
490 } // end switch
491 break;
492
493 case '\\':
494 pStr++;
495 switch (*pStr)
496 {
497 case 'n':
498 *BuildPtr++=LF;
499 pStr++;
500 continue;
501
502 case 'r':
503 *BuildPtr++=CR;
504 pStr++;
505 continue;
506
507 case 0:
508 continue;
509 break;
510 } // end switch
511
512 break;
513 } // end switch
514
515 *BuildPtr++=*pStr++;
516 } // end while
517
518 *BuildPtr=0; // cauterize the string
519 StringOut((char *) BuildString);
520#endif //DEBUG
521}
522
523struct snd_info_buffer {
524 char *buffer; /* pointer to begin of buffer */
525 char *curr; /* current position in buffer */
526 unsigned long size; /* current size */
527 unsigned long len; /* total length of buffer */
528 int stop; /* stop flag */
529 int error; /* error code */
530};
531
532typedef struct snd_info_buffer snd_info_buffer_t;
533
534int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
535{
536 char *BuildPtr=buffer->curr;
537 char *pStr=(char *) fmt;
538 char *SubStr;
539 int res;
540 union {
541 void *VoidPtr;
542 USHORT *WordPtr;
543 ULONG *LongPtr;
544 ULONG *StringPtr;
545 } Parm;
546 USHORT wBuildOption;
547
548 Parm.VoidPtr=(void *) &fmt;
549 Parm.StringPtr++; // skip size of string pointer
550
551 if (buffer->stop || buffer->error)
552 return 0;
553
554 while (*pStr)
555 {
556 // don't overflow target
557 if (BuildPtr >= (char *) &buffer->curr[buffer->len - 4])
558 break;
559
560 switch (*pStr)
561 {
562 case '%':
563 wBuildOption=0;
564 pStr++;
565 if (*pStr=='0')
566 {
567 wBuildOption|=LEADING_ZEROES;
568 pStr++;
569 }
570// if (*pStr=='u') // always unsigned
571// pStr++;
572 if (*pStr=='#')
573 pStr++;
574
575 switch(*pStr)
576 {
577 case 'x':
578 case 'X':
579 case 'p':
580 case 'P':
581 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
582 pStr++;
583 continue;
584
585 case 'd':
586 case 'u':
587 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
588 pStr++;
589 continue;
590
591 case 's':
592 SubStr=(char *)*Parm.StringPtr;
593 while (*BuildPtr++ = *SubStr++);
594 Parm.StringPtr++;
595 BuildPtr--; // remove the \0
596 pStr++;
597 continue;
598
599 case 'c':
600 *BuildPtr++ = (char)*Parm.LongPtr;
601 Parm.LongPtr++;
602 pStr++;
603 continue;
604
605 case 'l':
606 pStr++;
607 switch (*pStr)
608 {
609 case 'x':
610 case 'X':
611 BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
612 pStr++;
613 continue;
614
615 case 'd':
616 BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption);
617 pStr++;
618 continue;
619 } // end switch
620 continue; // dunno what he wants
621
622 case 0:
623 continue;
624 } // end switch
625 break;
626
627 case '\\':
628 pStr++;
629 switch (*pStr)
630 {
631 case 'n':
632 *BuildPtr++=LF;
633 pStr++;
634 continue;
635
636 case 'r':
637 *BuildPtr++=CR;
638 pStr++;
639 continue;
640
641 case 0:
642 continue;
643 break;
644 } // end switch
645
646 break;
647 } // end switch
648
649 *BuildPtr++=*pStr++;
650 } // end while
651
652 *BuildPtr=0; // cauterize the string
653
654 res = strlen(buffer->curr);
655 if (buffer->size + res >= buffer->len) {
656 buffer->stop = 1;
657 return 0;
658 }
659 buffer->curr += res;
660 buffer->size += res;
661 return res;
662}
663
Note: See TracBrowser for help on using the repository browser.