home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / aros / kprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  4.1 KB  |  286 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: kprintf.c,v 1.12 1997/01/28 13:41:32 digulla Exp $
  4.  
  5.     Desc: Formats a message and makes sure the user will see it.
  6.     Lang: english
  7. */
  8. #include <aros/arosbase.h>
  9. #include <stdarg.h>
  10. #include <stdlib.h>
  11. #include <aros/system.h>
  12. #include <proto/dos.h>
  13. #include <proto/aros.h>
  14. #undef kprintf
  15. #include <unistd.h>
  16.  
  17. #define AROSBase    ((struct AROSBase *)(SysBase->DebugData))
  18.  
  19. #define isdigit(x)      ((x) >= '0' && (x) <= '9')
  20.  
  21. /*****************************************************************************
  22.  
  23.     NAME */
  24. #include <proto/aros.h>
  25.  
  26.     int kprintf (
  27.  
  28. /*  SYNOPSIS */
  29.     const UBYTE * fmt,
  30.     ...)
  31.  
  32. /*  FUNCTION
  33.     Formats fmt with the specified arguments like printf() (and *not*
  34.     like RawDoFmt()) and uses a secure way to deliver the message to
  35.     the user; ie. the user *will* see this message no matter what.
  36.  
  37.     INPUTS
  38.     fmt - printf()-style format string
  39.  
  40.     RESULT
  41.     The number of characters output.
  42.  
  43.     NOTES
  44.     This function is not part of a library and may thus be called
  45.     any time.
  46.  
  47.     EXAMPLE
  48.  
  49.     BUGS
  50.  
  51.     SEE ALSO
  52.  
  53.     INTERNALS
  54.  
  55.     HISTORY
  56.     24-12-95    digulla created
  57.  
  58. ******************************************************************************/
  59. {
  60.     va_list     args;
  61.     int      ret;
  62.     static const char uhex[] = "0123456789ABCDEF";
  63.     static const char lhex[] = "0123456789abcdef";
  64.     char       * fill;
  65.     ULONG     val;
  66.     LONG     lval;
  67.  
  68.     if (!fmt)
  69.     return write (2, "(null)", 6);
  70.  
  71.     va_start (args, fmt);
  72.  
  73.     ret = 0;
  74.  
  75.     while (*fmt)
  76.     {
  77.     if (*fmt == '%')
  78.     {
  79.         int width = 0;
  80.  
  81.         fmt ++;
  82.  
  83.         if (*fmt == '0')
  84.         {
  85.         fill = "00000000";
  86.         fmt ++;
  87.         }
  88.         else
  89.         {
  90.         fill = "        ";
  91.         }
  92.  
  93.         if (isdigit (*fmt))
  94.         width = atoi (fmt);
  95.  
  96.         while (isdigit(*fmt) || *fmt=='.' || *fmt=='-' || *fmt=='+')
  97.         fmt ++;
  98.  
  99.         switch (*fmt)
  100.         {
  101.         case '%': break;
  102.         write (2, fmt, 1);
  103.         ret ++;
  104.         break;
  105.  
  106.         case 's':
  107.         case 'S': {
  108.         char * str = va_arg (args, char *);
  109.         int len;
  110.  
  111.         if (!str)
  112.             str = "(null)";
  113.  
  114.         if (*fmt == 'S')
  115.         {
  116.             write (2, "\"", 1);
  117.             ret ++;
  118.         }
  119.  
  120.         len = strlen (str);
  121.  
  122.         write (2, str, len);
  123.         ret += len;
  124.  
  125.         if (*fmt == 'S')
  126.         {
  127.             write (2, "\"", 1);
  128.             ret ++;
  129.         }
  130.  
  131.         break; }
  132.  
  133.         case 'p': {
  134.         int t;
  135.         char puffer[sizeof (void *)*2];
  136.         ULONG val;
  137.  
  138.         t = sizeof (void *)*2;
  139.         val = va_arg (args, ULONG);
  140.  
  141.         while (t)
  142.         {
  143.             puffer[--t] = lhex[val & 0x0F];
  144.             val >>= 4;
  145.         }
  146.  
  147.         write (2, puffer, sizeof (void *)*2);
  148.  
  149.         break; }
  150.  
  151.         case 'c': {
  152.         char c;
  153.  
  154.         c = va_arg (args, char);
  155.  
  156.         write (2, &c, 1);
  157.  
  158.         break; }
  159.  
  160.         case 'l': {
  161.         int t;
  162.         char puffer[32];
  163.  
  164.         if (fmt[1] == 'u' || fmt[1] == 'd' || fmt[1] == 'x' || fmt[1] == 'X')
  165.             fmt ++;
  166.  
  167.         if (*fmt == 'd')
  168.         {
  169.             lval = va_arg (args, LONG);
  170.  
  171.             val = (lval < 0) ? -lval : lval;
  172.         }
  173.         else
  174.         {
  175.             val = va_arg (args, ULONG);
  176.         }
  177.  
  178. print_int:
  179.         if (val==0)
  180.         {
  181.             if (width == 0)
  182.             width = 1;
  183.  
  184.             if (*fill == ' ')
  185.             width --;
  186.  
  187.             while (width > 0)
  188.             {
  189.             write (2, fill, (width < 8) ? width : 8);
  190.             width -= 8;
  191.             }
  192.  
  193.             if (*fill == ' ')
  194.             write (2, "0", 1);
  195.  
  196.             ret ++;
  197.             break;
  198.         }
  199.  
  200.         t = 32;
  201.  
  202.         if (*fmt == 'd' || *fmt == 'u')
  203.         {
  204.             if (*fmt == 'u')
  205.             {
  206.             if (lval < 0)
  207.             {
  208.                 write (2, "-", 1);
  209.                 ret ++;
  210.                 val = -lval;
  211.             }
  212.             else
  213.                 val = lval;
  214.             }
  215.  
  216.             while (val && t)
  217.             {
  218.             puffer[--t] = lhex[val % 10];
  219.  
  220.             val /= 10;
  221.             }
  222.         }
  223.         else if (*fmt == 'x')
  224.         {
  225.             while (val && t)
  226.             {
  227.             puffer[--t] = lhex[val & 0x0F];
  228.  
  229.             val >>= 4;
  230.             }
  231.         }
  232.         else
  233.         {
  234.             while (val && t)
  235.             {
  236.             puffer[--t] = uhex[val & 0x0F];
  237.  
  238.             val >>= 4;
  239.             }
  240.         }
  241.  
  242.         width -= 32-t;
  243.  
  244.         while (width > 0)
  245.         {
  246.             write (2, fill, (width < 8) ? width : 8);
  247.             width -= 8;
  248.         }
  249.  
  250.         write (2, &puffer[t], 32-t);
  251.         ret += 32-t;
  252.  
  253.         break; }
  254.  
  255.         default: {
  256.         if (*fmt == 'd')
  257.         {
  258.             lval = va_arg (args, int);
  259.  
  260.             val = (lval < 0) ? -lval : lval;
  261.         }
  262.         else
  263.         {
  264.             val = va_arg (args, unsigned int);
  265.         }
  266.  
  267.         goto print_int;
  268.  
  269.         break; }
  270.         } /* switch */
  271.     }
  272.     else
  273.     {
  274.         write (2, fmt, 1);
  275.         ret ++;
  276.     }
  277.  
  278.     fmt ++; /* Next char */
  279.     } /* while (*fmt); */
  280.  
  281.     va_end (args);
  282.  
  283.     return ret;
  284. } /* kprintf */
  285.  
  286.