home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / datafile / _btriscbsd / booter / c / printf < prev    next >
Encoding:
Text File  |  1996-02-17  |  5.5 KB  |  262 lines

  1. /*    $NetBSD: printf.c,v 1.7 1996/02/08 20:19:36 gwr Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  *
  35.  *    @(#)printf.c    8.1 (Berkeley) 6/11/93
  36.  */
  37.  
  38. /*
  39.  * Scaled down version of printf(3).
  40.  *
  41.  * One additional format:
  42.  *
  43.  * The format %b is supported to decode error registers.
  44.  * Its usage is:
  45.  *
  46.  *    printf("reg=%b\n", regval, "<base><arg>*");
  47.  *
  48.  * where <base> is the output base expressed as a control character, e.g.
  49.  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
  50.  * the first of which gives the bit number to be inspected (origin 1), and
  51.  * the next characters (up to a control character, i.e. a character <= 32),
  52.  * give the name of the register.  Thus:
  53.  *
  54.  *    printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
  55.  *
  56.  * would produce output:
  57.  *
  58.  *    reg=3<BITTWO,BITONE>
  59.  */
  60.  
  61. #ifdef __STDC__
  62. #include <stdarg.h>
  63. #else
  64. #include <varargs.h>
  65. #endif
  66.  
  67. #include "stand.h"
  68.  
  69. static void kprintn (void (*)(), u_long, int);
  70. static void sputchar (int);
  71. static void kprintf (void (*)(), const char *, va_list);
  72.  
  73. static char *sbuf;
  74.  
  75. static void
  76. sputchar(c)
  77.     int c;
  78. {
  79.     *sbuf++ = c;
  80. }
  81.  
  82. void
  83. #ifdef __STDC__
  84. sprintf(char *buf, const char *fmt, ...)
  85. #else
  86. sprintf(buf, fmt, va_alist)
  87.     char *buf, *fmt;
  88. #endif
  89. {
  90.     va_list ap;
  91.  
  92.     sbuf = buf;
  93. #ifdef __STDC__
  94.     va_start(ap, fmt);
  95. #else
  96.     va_start(ap);
  97. #endif
  98.     kprintf(sputchar, fmt, ap);
  99.     va_end(ap);
  100.     *sbuf = '\0';
  101. }
  102.  
  103. void
  104. #ifdef __STDC__
  105. printf(const char *fmt, ...)
  106. #else
  107. printf(fmt, va_alist)
  108.     char *fmt;
  109. #endif
  110. {
  111.     va_list ap;
  112.  
  113. #ifdef __STDC__
  114.     va_start(ap, fmt);
  115. #else
  116.     va_start(ap);
  117. #endif
  118.     kprintf(putchar, fmt, ap);
  119.     va_end(ap);
  120. }
  121.  
  122. void
  123. vprintf(const char *fmt, va_list ap)
  124. {
  125.     kprintf(putchar, fmt, ap);
  126. }
  127.  
  128. void
  129. kprintf(put, fmt, ap)
  130.     void (*put)(int);
  131.     const char *fmt;
  132.     va_list ap;
  133. {
  134.     register char *p;
  135.     register int ch, n;
  136.     unsigned long ul;
  137.     int lflag, set;
  138.     int zflag;
  139.     int width;
  140.  
  141.     for (;;) {
  142.         while ((ch = *fmt++) != '%') {
  143.             if (ch == '\0')
  144.                 return;
  145.             put(ch);
  146.         }
  147.         lflag = 0;
  148.         zflag = 0;
  149.         width = 0;
  150. reswitch:    switch (ch = *fmt++) {
  151.         case 'l':
  152.             lflag = 1;
  153.             goto reswitch;
  154.         case 'b':
  155.             ul = va_arg(ap, int);
  156.             p = va_arg(ap, char *);
  157.             kprintn(put, ul, *p++);
  158.  
  159.             if (!ul)
  160.                 break;
  161.  
  162.             for (set = 0; (n = *p++);) {
  163.                 if (ul & (1 << (n - 1))) {
  164.                     put(set ? ',' : '<');
  165.                     for (; (n = *p) > ' '; ++p)
  166.                         put(n);
  167.                     set = 1;
  168.                 } else
  169.                     for (; *p > ' '; ++p);
  170.             }
  171.             if (set)
  172.                 put('>');
  173.             break;
  174.         case 'c':
  175.             ch = va_arg(ap, int);
  176.                 put(ch & 0x7f);
  177.             break;
  178.         case 's':
  179.             p = va_arg(ap, char *);
  180.             while ((ch = *p++))
  181.                 put(ch);
  182.             break;
  183.         case 'd':
  184.             ul = lflag ?
  185.                 va_arg(ap, long) : va_arg(ap, int);
  186.             if ((long)ul < 0) {
  187.                 put('-');
  188.                 ul = -(long)ul;
  189.             }
  190.             kprintn(put, ul, 10);
  191.             break;
  192.         case 'o':
  193.             ul = lflag ?
  194.                 va_arg(ap, u_long) : va_arg(ap, u_int);
  195.             kprintn(put, ul, 8);
  196.             break;
  197.         case 'u':
  198.             ul = lflag ?
  199.                 va_arg(ap, u_long) : va_arg(ap, u_int);
  200.             kprintn(put, ul, 10);
  201.             break;
  202.         case 'x':
  203.             ul = lflag ?
  204.                 va_arg(ap, u_long) : va_arg(ap, u_int);
  205.             if (width > 0) {
  206.                 unsigned long tmp = ul;
  207.                 do {
  208.                     --width;
  209.                     tmp = tmp >> 4;
  210.                 } while (tmp > 0);
  211.                 while (width > 0) {
  212.                     --width;
  213.                     put(zflag ? '0' : ' ');
  214.                 }
  215.             }
  216.             kprintn(put, ul, 16);
  217.             break;
  218.         default:
  219.             if (isdigit(ch)) {
  220.                 if (ch == '0' && width == 0)
  221.                     zflag = 1;
  222.                 else
  223.                     width = (width * 10) + (ch - '0');
  224.                 goto reswitch;
  225.             } else {
  226.                 put('%');
  227.                 if (lflag)
  228.                     put('l');
  229.                 put(ch);
  230.             }
  231.         }
  232.     }
  233.     va_end(ap);
  234. }
  235.  
  236. static void
  237. kprintn(put, ul, base)
  238.     void (*put)(int);
  239.     unsigned long ul;
  240.     int base;
  241. {
  242.                     /* hold a long in base 8 */
  243.     char *p, buf[(sizeof(long) * NBBY / 3) + 1];
  244.  
  245.     p = buf;
  246.     do {
  247.         *p++ = "0123456789abcdef"[ul % base];
  248.     } while (ul /= base);
  249.     do {
  250.         put(*--p);
  251.     } while (p > buf);
  252. }
  253.  
  254. void
  255. twiddle()
  256. {
  257.     static int pos;
  258.  
  259.     putchar("|/-\\"[pos++ & 3]);
  260.     putchar('\b');
  261. }
  262.