home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Lib32 / lxdebug.c < prev    next >
C/C++ Source or Header  |  2002-04-26  |  11KB  |  412 lines

  1. /* $Id: lxdebug.c,v 1.2 2002/04/26 23:09:23 smilcke Exp $ */
  2.  
  3. /*
  4.  * sprintf.c
  5.  * Autor:               Stefan Milcke
  6.  * Erstellt am:         05.11.2001
  7.  * Letzte Aenderung am: 11.11.2001
  8.  *
  9. */
  10. #define INCL_NOPMAPI
  11. #define INCL_DOSERRORS           // for ERROR_INVALID_FUNCTION
  12. #include <os2.h>
  13.  
  14.  
  15. #define CR 0x0d
  16. #define LF 0x0a
  17.  
  18.  
  19. #define LEADING_ZEROES          0x8000
  20. #define SIGNIFICANT_FIELD       0x0007
  21.  
  22. BOOL fLineTerminate=TRUE;
  23.  
  24. char hextab[]="0123456789ABCDEF";
  25.  
  26.                                         //-------------------- DecLongToASCII -
  27. char *DecLongToASCII(char *StrPtr, ULONG lDecVal,USHORT Option,USHORT nlz)
  28. {
  29.  BOOL  fNonZero=FALSE;
  30.  BOOL  fDoZero;
  31.  ULONG Digit;
  32.  ULONG Power=1000000000;                      // 1 billion
  33.  ULONG z=10;
  34.  while (Power)
  35.  {
  36.   Digit=0;                                                                        // Digit=lDecVal/Power
  37.   while (lDecVal >=Power)                   // replaced with while loop
  38.   {
  39.    Digit++;
  40.    lDecVal-=Power;
  41.   }
  42.   fDoZero=FALSE;
  43.   if (Digit)
  44.    fNonZero=TRUE;
  45.   if(Digit || fNonZero || ((Power==1) && (fNonZero==FALSE)))
  46.    fDoZero=TRUE;
  47.   if(fDoZero || ((Option & LEADING_ZEROES) && (z<=nlz || nlz==0)))
  48.    fDoZero=TRUE;
  49.   if(fDoZero)
  50.   {
  51.    *StrPtr=(char)('0'+Digit);
  52.    StrPtr++;
  53.   }
  54.   if (Power==1000000000)                    // 1 billion
  55.    Power=100000000;
  56.   else if (Power==100000000)
  57.    Power=10000000;
  58.   else if (Power==10000000)
  59.    Power=1000000;
  60.   else if (Power==1000000)
  61.    Power=100000;
  62.   else if (Power==100000)
  63.    Power=10000;
  64.   else if (Power==10000)
  65.    Power=1000;
  66.   else if (Power==1000)
  67.    Power=100;
  68.   else if (Power==100)
  69.    Power=10;
  70.   else if (Power==10)
  71.    Power=1;
  72.   else
  73.    Power=0;
  74.   if(z)
  75.    z--;
  76.  }
  77.  return (StrPtr);
  78. }
  79.                                         //-------------------- HexWordToASCII -
  80.                                         //-------------------- HexLongToASCII -
  81. char  *HexLongToASCII(char  *StrPtr, ULONG wHexVal, USHORT Option,USHORT nlz)
  82. {
  83.  BOOL  fNonZero=FALSE;
  84.  ULONG Digit;
  85.  ULONG Power=0xF0000000;
  86.  ULONG ShiftVal=28;
  87.  ULONG z=8;
  88.  while (Power)
  89.  {
  90.   Digit=(wHexVal & Power)>>ShiftVal;
  91.   if (Digit)
  92.    fNonZero=TRUE;
  93.   if(Digit || fNonZero || ((Power==0x0F) && (fNonZero==FALSE)))
  94.    *StrPtr++=hextab[Digit];
  95.   else if(((Option & LEADING_ZEROES) && (z<=nlz || nlz==0)))
  96.    *StrPtr++=hextab[0];
  97.   if (Power==0xF0000000)                  // 1 billion
  98.    Power=0xF000000;
  99.   else if (Power==0xF000000)
  100.    Power=0xF00000;
  101.   else if (Power==0xF00000)
  102.    Power=0xF0000;
  103.   else if (Power==0xF0000)
  104.    Power=0xF000;
  105.   else if (Power==0xF000)
  106.    Power=0xF00;
  107.   else if (Power==0xF00)
  108.    Power=0xF0;
  109.   else if (Power==0xF0)
  110.    Power=0xF;
  111.   else
  112.    Power=0;
  113.   ShiftVal-=4;
  114.   if(z)
  115.    z--;
  116.  } // end while
  117.  return (StrPtr);
  118. }
  119.  
  120. #ifdef  DEBUG
  121. char BuildString[1024];
  122. #endif          // DEBUG
  123.  
  124. //------------------------- PrintfOut -
  125. void _cdecl DPD(int level, char *DbgStr, ...)
  126. {
  127. #ifdef DEBUG
  128.    char *BuildPtr=BuildString;
  129.    char *pStr=(char *) DbgStr;
  130.    char *SubStr;
  131.    int numLeadingZeroes;
  132.    union {
  133.          void   *VoidPtr;
  134.          USHORT *WordPtr;
  135.          ULONG  *LongPtr;
  136.          ULONG  *StringPtr;
  137.          } Parm;
  138.    USHORT wBuildOption;
  139.  
  140.    Parm.VoidPtr=(void *) &DbgStr;
  141.    Parm.StringPtr++;                            // skip size of string pointer
  142.  
  143.    while (*pStr)
  144.    {
  145.     numLeadingZeroes=0;
  146.     // don't overflow target
  147.     if (BuildPtr >= (char *) &BuildString[1024-2])
  148.      break;
  149.     switch (*pStr)
  150.     {
  151.      case '%':
  152.       wBuildOption=0;
  153.       pStr++;
  154.       if (*pStr=='0')
  155.       {
  156.        wBuildOption|=LEADING_ZEROES;
  157.        pStr++;
  158.        if(*pStr>='0' && *pStr<='9')
  159.        {
  160.         numLeadingZeroes=((int)(*pStr))-((int)'0');
  161.         pStr++;
  162.        }
  163.       }
  164.       if (*pStr=='u')                                                         // always unsigned
  165.        pStr++;
  166.       if (*pStr=='#')
  167.        pStr++;
  168.       switch(*pStr)
  169.       {
  170.        case 'x':
  171.        case 'X':
  172.        case 'p':
  173.        case 'P':
  174.         BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  175.         pStr++;
  176.         continue;
  177.        case 'd':
  178.         BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  179.         pStr++;
  180.         continue;
  181.        case 's':
  182.         SubStr=(char *)*Parm.StringPtr;
  183.         while (*BuildPtr++ = *SubStr++);
  184.          Parm.StringPtr++;
  185.         BuildPtr--;                      // remove the \0
  186.         pStr++;
  187.         continue;
  188.        case 'l':
  189.         pStr++;
  190.         switch (*pStr)
  191.         {
  192.          case 'x':
  193.          case 'X':
  194.           BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  195.           pStr++;
  196.           continue;
  197.          case 'd':
  198.           BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  199.           pStr++;
  200.           continue;
  201.         } // end switch
  202.         continue;                        // dunno what he wants
  203.        case 0:
  204.         continue;
  205.       } // end switch
  206.       break;
  207.      case '\\':
  208.       pStr++;
  209.       switch (*pStr)
  210.       {
  211.        case 'n':
  212.         *BuildPtr++=LF;
  213.         pStr++;
  214.         continue;
  215.        case 'r':
  216.         *BuildPtr++=CR;
  217.         pStr++;
  218.         continue;
  219.        case 0:
  220.         continue;
  221.         break;
  222.       } // end switch
  223.       break;
  224.     } // end switch
  225.     *BuildPtr++=*pStr++;
  226.    } // end while
  227.  
  228.    *BuildPtr=0;                                 // cauterize the string
  229.    StringOut((char *) BuildString);         // print to comm port
  230. #endif                            //DEBUG
  231. }
  232.  
  233.  
  234. void _cdecl DPE(char *DbgStr, ...)
  235. {
  236. #ifdef DEBUG
  237.    char *BuildPtr=BuildString;
  238.    char *pStr = (char *) DbgStr;
  239.    char *SubStr;
  240.    int numLeadingZeroes;
  241.    union {
  242.          void   *VoidPtr;
  243.          USHORT *WordPtr;
  244.          ULONG  *LongPtr;
  245.          ULONG  *StringPtr;
  246.          } Parm;
  247.    USHORT wBuildOption;
  248.  
  249.    Parm.VoidPtr=(void *) &DbgStr;
  250.    Parm.StringPtr++;                            // skip size of string pointer
  251.  
  252.    while (*pStr)
  253.       {
  254.        numLeadingZeroes=0;
  255.       // don't overflow target
  256.       if (BuildPtr >= (char *) &BuildString[1024-2])
  257.          break;
  258.  
  259.       switch (*pStr)
  260.          {
  261.          case '%':
  262.             wBuildOption=0;
  263.             pStr++;
  264.             if (*pStr=='0')
  265.                {
  266.                wBuildOption|=LEADING_ZEROES;
  267.                pStr++;
  268.        if(*pStr>='0' && *pStr<='9')
  269.        {
  270.         numLeadingZeroes=((int)(*pStr))-((int)'0');
  271.         pStr++;
  272.        }
  273.                }
  274.             if (*pStr=='u')                                                         // always unsigned
  275.                pStr++;
  276.  
  277.             switch(*pStr)
  278.                {
  279.                case 'x':
  280.            case 'X':
  281.                   BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  282.                   pStr++;
  283.                   continue;
  284.  
  285.                case 'd':
  286.                   BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  287.                   pStr++;
  288.                   continue;
  289.  
  290.                case 's':
  291.                   SubStr=(char *)*Parm.StringPtr;
  292.                   while (*BuildPtr++ = *SubStr++);
  293.                   Parm.StringPtr++;
  294.                   BuildPtr--;                      // remove the \0
  295.                   pStr++;
  296.                   continue;
  297.  
  298.                case 'l':
  299.                   pStr++;
  300.                   switch (*pStr)
  301.                   {
  302.                   case 'x':
  303.                   case 'X':
  304.                   BuildPtr=HexLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  305.                   pStr++;
  306.                   continue;
  307.  
  308.                   case 'd':
  309.                      BuildPtr=DecLongToASCII(BuildPtr, *Parm.LongPtr++,wBuildOption,numLeadingZeroes);
  310.                      pStr++;
  311.                      continue;
  312.                   } // end switch
  313.                   continue;                        // dunno what he wants
  314.  
  315.                case 0:
  316.                   continue;
  317.                } // end switch
  318.             break;
  319.  
  320.       case '\\':
  321.          pStr++;
  322.          switch (*pStr)
  323.             {
  324.             case 'n':
  325.             *BuildPtr++=LF;
  326.             pStr++;
  327.             continue;
  328.  
  329.             case 'r':
  330.             *BuildPtr++=CR;
  331.             pStr++;
  332.             continue;
  333.  
  334.             case 0:
  335.             continue;
  336.             break;
  337.             } // end switch
  338.  
  339.          break;
  340.          } // end switch
  341.  
  342.       *BuildPtr++=*pStr++;
  343.       } // end while
  344.  
  345.    *BuildPtr=0;                                 // cauterize the string
  346.    StringOut((char *) BuildString);         // print to comm port
  347. #endif                            //DEBUG
  348. }
  349.  
  350. #ifdef DEBUG                            //------------------------- StringOut -
  351. void StringOut(char *DbgStr)
  352. {
  353.    while (*DbgStr)
  354.       CharOut(*DbgStr++);
  355.  
  356.    if (fLineTerminate)
  357.    {
  358.       CharOut(CR);                              // append carriage return,
  359.       CharOut(LF);                              // linefeed
  360.    }
  361. }
  362. #endif
  363.  
  364. #ifdef DEBUG
  365. //#define       MAGIC_COMM_PORT 0x3f8           // pulled from word ptr 40:0
  366. #define         MAGIC_COMM_PORT 0x2f8           // pulled from word ptr 40:0
  367.  
  368.  
  369. #define UART_DATA               0x00            // UART Data port
  370. #define UART_INT_ENAB           0x01            // UART Interrupt enable
  371. #define UART_INT_ID             0x02            // interrupt ID
  372. #define UART_LINE_CTRL          0x03            // line control registers
  373. #define UART_MODEM_CTRL         0x04            // modem control register
  374. #define UART_LINE_STAT          0x05            // line status register
  375. #define UART_MODEM_STAT         0x06            // modem status regiser
  376. #define UART_DIVISOR_LO         0x00            // divisor latch least sig
  377. #define UART_DIVISOR_HI         0x01h           // divisor latch most sig
  378.  
  379. #define DELAY   nop
  380. #endif
  381.  
  382. #ifdef DEBUG                            //--------------------------- CharOut -
  383. void CharOut(char c)
  384. {
  385.         _asm    {
  386.  
  387.         mov     dx, MAGIC_COMM_PORT     // address of PS/2's first COM port
  388.         add     dx, UART_LINE_STAT
  389.  
  390. ReadyCheck:
  391.         in      al, dx                                                          // wait for comm port ready signal
  392.  
  393.         DELAY
  394.         DELAY
  395.         DELAY
  396.  
  397.         test    al, 020h
  398.         jz      ReadyCheck
  399.  
  400.         // Send the character
  401.  
  402.         add     dx, UART_DATA - UART_LINE_STAT
  403.         mov     al,c
  404.         out     dx, al
  405.  
  406.         DELAY
  407.         DELAY
  408.         DELAY
  409.         }
  410. }
  411. #endif
  412.