[3c1dec0] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include "printf.h"
|
---|
| 30 | #include "ofw.h"
|
---|
| 31 |
|
---|
| 32 | static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | /** Print hexadecimal digits
|
---|
| 36 | *
|
---|
| 37 | * Print fixed count of hexadecimal digits from
|
---|
| 38 | * the number num. The digits are printed in
|
---|
| 39 | * natural left-to-right order starting with
|
---|
| 40 | * the width-th digit.
|
---|
| 41 | *
|
---|
| 42 | * @param num Number containing digits.
|
---|
| 43 | * @param width Count of digits to print.
|
---|
| 44 | *
|
---|
| 45 | */
|
---|
| 46 | static void print_fixed_hex(const __u64 num, const int width)
|
---|
| 47 | {
|
---|
| 48 | int i;
|
---|
| 49 |
|
---|
| 50 | for (i = width * 8 - 4; i >= 0; i -= 4)
|
---|
| 51 | ofw_write(digits + ((num >> i) & 0xf), 1);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | /** Print number in given base
|
---|
| 56 | *
|
---|
| 57 | * Print significant digits of a number in given
|
---|
| 58 | * base.
|
---|
| 59 | *
|
---|
| 60 | * @param num Number to print.
|
---|
| 61 | * @param base Base to print the number in (should
|
---|
| 62 | * be in range 2 .. 16).
|
---|
| 63 | *
|
---|
| 64 | */
|
---|
| 65 | static void print_number(const __native num, const unsigned int base)
|
---|
| 66 | {
|
---|
| 67 | int val = num;
|
---|
| 68 | char d[sizeof(__native) * 8 + 1]; /* this is good enough even for base == 2 */
|
---|
| 69 | int i = sizeof(__native) * 8 - 1;
|
---|
| 70 |
|
---|
| 71 | do {
|
---|
| 72 | d[i--] = digits[val % base];
|
---|
| 73 | } while (val /= base);
|
---|
| 74 |
|
---|
| 75 | d[sizeof(__native) * 8] = 0;
|
---|
| 76 | ofw_puts(&d[i + 1]);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | /** General formatted text print
|
---|
| 81 | *
|
---|
| 82 | * Print text formatted according the fmt parameter
|
---|
| 83 | * and variant arguments. Each formatting directive
|
---|
| 84 | * begins with \% (percentage) character and one of the
|
---|
| 85 | * following character:
|
---|
| 86 | *
|
---|
| 87 | * \% Prints the percentage character.
|
---|
| 88 | *
|
---|
| 89 | * s The next variant argument is treated as char*
|
---|
| 90 | * and printed as a NULL terminated string.
|
---|
| 91 | *
|
---|
| 92 | * c The next variant argument is treated as a single char.
|
---|
| 93 | *
|
---|
| 94 | * p The next variant argument is treated as a maximum
|
---|
| 95 | * bit-width integer with respect to architecture
|
---|
| 96 | * and printed in full hexadecimal width.
|
---|
| 97 | *
|
---|
| 98 | * P As with 'p', but '0x' is prefixed.
|
---|
| 99 | *
|
---|
| 100 | * q The next variant argument is treated as a 64b integer
|
---|
| 101 | * and printed in full hexadecimal width.
|
---|
| 102 | *
|
---|
| 103 | * Q As with 'q', but '0x' is prefixed.
|
---|
| 104 | *
|
---|
| 105 | * l The next variant argument is treated as a 32b integer
|
---|
| 106 | * and printed in full hexadecimal width.
|
---|
| 107 | *
|
---|
| 108 | * L As with 'l', but '0x' is prefixed.
|
---|
| 109 | *
|
---|
| 110 | * w The next variant argument is treated as a 16b integer
|
---|
| 111 | * and printed in full hexadecimal width.
|
---|
| 112 | *
|
---|
| 113 | * W As with 'w', but '0x' is prefixed.
|
---|
| 114 | *
|
---|
| 115 | * b The next variant argument is treated as a 8b integer
|
---|
| 116 | * and printed in full hexadecimal width.
|
---|
| 117 | *
|
---|
| 118 | * B As with 'b', but '0x' is prefixed.
|
---|
| 119 | *
|
---|
| 120 | * d The next variant argument is treated as integer
|
---|
| 121 | * and printed in standard decimal format (only significant
|
---|
| 122 | * digits).
|
---|
| 123 | *
|
---|
| 124 | * x The next variant argument is treated as integer
|
---|
| 125 | * and printed in standard hexadecimal format (only significant
|
---|
| 126 | * digits).
|
---|
| 127 | *
|
---|
| 128 | * X As with 'x', but '0x' is prefixed.
|
---|
| 129 | *
|
---|
| 130 | * All other characters from fmt except the formatting directives
|
---|
| 131 | * are printed in verbatim.
|
---|
| 132 | *
|
---|
| 133 | * @param fmt Formatting NULL terminated string.
|
---|
| 134 | */
|
---|
| 135 | void printf(const char *fmt, ...)
|
---|
| 136 | {
|
---|
| 137 | int i = 0;
|
---|
| 138 | va_list ap;
|
---|
| 139 | char c;
|
---|
| 140 |
|
---|
| 141 | va_start(ap, fmt);
|
---|
| 142 |
|
---|
| 143 | while ((c = fmt[i++])) {
|
---|
| 144 | switch (c) {
|
---|
| 145 |
|
---|
| 146 | /* control character */
|
---|
| 147 | case '%':
|
---|
| 148 |
|
---|
| 149 | switch (c = fmt[i++]) {
|
---|
| 150 |
|
---|
| 151 | /* percentile itself */
|
---|
| 152 | case '%':
|
---|
| 153 | break;
|
---|
| 154 |
|
---|
| 155 | /*
|
---|
| 156 | * String and character conversions.
|
---|
| 157 | */
|
---|
| 158 | case 's':
|
---|
| 159 | ofw_puts(va_arg(ap, char_ptr));
|
---|
| 160 | goto loop;
|
---|
| 161 |
|
---|
| 162 | case 'c':
|
---|
| 163 | c = (char) va_arg(ap, int);
|
---|
| 164 | break;
|
---|
| 165 |
|
---|
| 166 | /*
|
---|
| 167 | * Hexadecimal conversions with fixed width.
|
---|
| 168 | */
|
---|
| 169 | case 'P':
|
---|
| 170 | ofw_puts("0x");
|
---|
| 171 | case 'p':
|
---|
| 172 | print_fixed_hex(va_arg(ap, __native), sizeof(__native));
|
---|
| 173 | goto loop;
|
---|
| 174 |
|
---|
| 175 | case 'Q':
|
---|
| 176 | ofw_puts("0x");
|
---|
| 177 | case 'q':
|
---|
| 178 | print_fixed_hex(va_arg(ap, __u64), INT64);
|
---|
| 179 | goto loop;
|
---|
| 180 |
|
---|
| 181 | case 'L':
|
---|
| 182 | ofw_puts("0x");
|
---|
| 183 | case 'l':
|
---|
| 184 | print_fixed_hex(va_arg(ap, __native), INT32);
|
---|
| 185 | goto loop;
|
---|
| 186 |
|
---|
| 187 | case 'W':
|
---|
| 188 | ofw_puts("0x");
|
---|
| 189 | case 'w':
|
---|
| 190 | print_fixed_hex(va_arg(ap, __native), INT16);
|
---|
| 191 | goto loop;
|
---|
| 192 |
|
---|
| 193 | case 'B':
|
---|
| 194 | ofw_puts("0x");
|
---|
| 195 | case 'b':
|
---|
| 196 | print_fixed_hex(va_arg(ap, __native), INT8);
|
---|
| 197 | goto loop;
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
| 200 | * Decimal and hexadecimal conversions.
|
---|
| 201 | */
|
---|
| 202 | case 'd':
|
---|
| 203 | print_number(va_arg(ap, __native), 10);
|
---|
| 204 | goto loop;
|
---|
| 205 |
|
---|
| 206 | case 'X':
|
---|
| 207 | ofw_puts("0x");
|
---|
| 208 | case 'x':
|
---|
| 209 | print_number(va_arg(ap, __native), 16);
|
---|
| 210 | goto loop;
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * Bad formatting.
|
---|
| 214 | */
|
---|
| 215 | default:
|
---|
| 216 | goto out;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | default:
|
---|
| 220 | ofw_write(&c, 1);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | loop:
|
---|
| 224 | ;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | out:
|
---|
| 228 |
|
---|
| 229 | va_end(ap);
|
---|
| 230 | }
|
---|