Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 1986, 1988, 1991, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * (c) UNIX System Laboratories, Inc. |
| 5 | * All or some portions of this file are derived from material licensed |
| 6 | * to the University of California by American Telephone and Telegraph |
| 7 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 8 | * the permission of UNIX System Laboratories, Inc. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 4. Neither the name of the University nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this software |
| 20 | * without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | * SUCH DAMAGE. |
| 33 | * |
| 34 | * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 |
| 35 | */ |
| 36 | |
| 37 | /* |
Dan Handley | ab2d31e | 2013-12-02 19:25:12 +0000 | [diff] [blame^] | 38 | * Portions copyright (c) 2009-2013, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | /* |
| 42 | #include "types.h" |
| 43 | #include "varargs.h" |
| 44 | #include "ctype.h" |
| 45 | #include "string.h" |
| 46 | */ |
| 47 | #include <stddef.h> |
| 48 | #include <sys/types.h> /* For ssize_t */ |
| 49 | #include <stdint.h> |
| 50 | #include <string.h> |
| 51 | |
| 52 | #include "ctype.h" |
| 53 | |
| 54 | typedef uint64_t uintmax_t; |
| 55 | typedef int64_t intmax_t; |
| 56 | typedef unsigned char u_char; |
| 57 | typedef unsigned int u_int; |
| 58 | typedef int64_t quad_t; |
| 59 | typedef uint64_t u_quad_t; |
| 60 | typedef unsigned long u_long; |
| 61 | typedef unsigned short u_short; |
| 62 | |
| 63 | static inline int imax(int a, int b) { return (a > b ? a : b); } |
| 64 | |
| 65 | /* |
| 66 | * Note that stdarg.h and the ANSI style va_start macro is used for both |
| 67 | * ANSI and traditional C compilers. |
| 68 | */ |
| 69 | |
| 70 | #define TOCONS 0x01 |
| 71 | #define TOTTY 0x02 |
| 72 | #define TOLOG 0x04 |
| 73 | |
| 74 | /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ |
| 75 | #define MAXNBUF (sizeof(intmax_t) * 8 + 1) |
| 76 | |
| 77 | struct putchar_arg { |
| 78 | int flags; |
| 79 | int pri; |
| 80 | struct tty *tty; |
| 81 | char *p_bufr; |
| 82 | size_t n_bufr; |
| 83 | char *p_next; |
| 84 | size_t remain; |
| 85 | }; |
| 86 | |
| 87 | struct snprintf_arg { |
| 88 | char *str; |
| 89 | size_t remain; |
| 90 | }; |
| 91 | |
| 92 | extern int log_open; |
| 93 | |
| 94 | static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper); |
| 95 | static void snprintf_func(int ch, void *arg); |
| 96 | static int kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap); |
| 97 | |
| 98 | int vsnprintf(char *str, size_t size, const char *format, va_list ap); |
| 99 | |
| 100 | static char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 101 | #define hex2ascii(hex) (hex2ascii_data[hex]) |
| 102 | |
| 103 | /* |
| 104 | * Scaled down version of sprintf(3). |
| 105 | */ |
| 106 | int |
| 107 | sprintf(char *buf, const char *cfmt, ...) |
| 108 | { |
| 109 | int retval; |
| 110 | va_list ap; |
| 111 | |
| 112 | va_start(ap, cfmt); |
| 113 | retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); |
| 114 | buf[retval] = '\0'; |
| 115 | va_end(ap); |
| 116 | return (retval); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Scaled down version of vsprintf(3). |
| 121 | */ |
| 122 | int |
| 123 | vsprintf(char *buf, const char *cfmt, va_list ap) |
| 124 | { |
| 125 | int retval; |
| 126 | |
| 127 | retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); |
| 128 | buf[retval] = '\0'; |
| 129 | return (retval); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Scaled down version of snprintf(3). |
| 134 | */ |
| 135 | int |
| 136 | snprintf(char *str, size_t size, const char *format, ...) |
| 137 | { |
| 138 | int retval; |
| 139 | va_list ap; |
| 140 | |
| 141 | va_start(ap, format); |
| 142 | retval = vsnprintf(str, size, format, ap); |
| 143 | va_end(ap); |
| 144 | return(retval); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Scaled down version of vsnprintf(3). |
| 149 | */ |
| 150 | int |
| 151 | vsnprintf(char *str, size_t size, const char *format, va_list ap) |
| 152 | { |
| 153 | struct snprintf_arg info; |
| 154 | int retval; |
| 155 | |
| 156 | info.str = str; |
| 157 | info.remain = size; |
| 158 | retval = kvprintf(format, snprintf_func, &info, 10, ap); |
| 159 | if (info.remain >= 1) |
| 160 | *info.str++ = '\0'; |
| 161 | return (retval); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | snprintf_func(int ch, void *arg) |
| 166 | { |
| 167 | struct snprintf_arg *const info = arg; |
| 168 | |
| 169 | if (info->remain >= 2) { |
| 170 | *info->str++ = ch; |
| 171 | info->remain--; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * Kernel version which takes radix argument vsnprintf(3). |
| 178 | */ |
| 179 | int |
| 180 | vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap) |
| 181 | { |
| 182 | struct snprintf_arg info; |
| 183 | int retval; |
| 184 | |
| 185 | info.str = str; |
| 186 | info.remain = size; |
| 187 | retval = kvprintf(format, snprintf_func, &info, radix, ap); |
| 188 | if (info.remain >= 1) |
| 189 | *info.str++ = '\0'; |
| 190 | return (retval); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /* |
| 195 | * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse |
| 196 | * order; return an optional length and a pointer to the last character |
| 197 | * written in the buffer (i.e., the first character of the string). |
| 198 | * The buffer pointed to by `nbuf' must have length >= MAXNBUF. |
| 199 | */ |
| 200 | static char * |
| 201 | ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) |
| 202 | { |
| 203 | char *p, c; |
| 204 | |
| 205 | p = nbuf; |
| 206 | *p = '\0'; |
| 207 | do { |
| 208 | c = hex2ascii(num % base); |
| 209 | *++p = upper ? toupper(c) : c; |
| 210 | } while (num /= base); |
| 211 | if (lenp) |
| 212 | *lenp = p - nbuf; |
| 213 | return (p); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Scaled down version of printf(3). |
| 218 | * |
| 219 | * Two additional formats: |
| 220 | * |
| 221 | * The format %b is supported to decode error registers. |
| 222 | * Its usage is: |
| 223 | * |
| 224 | * printf("reg=%b\n", regval, "<base><arg>*"); |
| 225 | * |
| 226 | * where <base> is the output base expressed as a control character, e.g. |
| 227 | * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, |
| 228 | * the first of which gives the bit number to be inspected (origin 1), and |
| 229 | * the next characters (up to a control character, i.e. a character <= 32), |
| 230 | * give the name of the register. Thus: |
| 231 | * |
| 232 | * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); |
| 233 | * |
| 234 | * would produce output: |
| 235 | * |
| 236 | * reg=3<BITTWO,BITONE> |
| 237 | * |
| 238 | * XXX: %D -- Hexdump, takes pointer and separator string: |
| 239 | * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX |
| 240 | * ("%*D", len, ptr, " " -> XX XX XX XX ... |
| 241 | */ |
| 242 | int |
| 243 | kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) |
| 244 | { |
| 245 | #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } |
| 246 | char nbuf[MAXNBUF]; |
| 247 | char *d; |
| 248 | const char *p, *percent, *q; |
| 249 | u_char *up; |
| 250 | int ch, n; |
| 251 | uintmax_t num; |
| 252 | int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; |
| 253 | int cflag, hflag, jflag, tflag, zflag; |
| 254 | int dwidth, upper; |
| 255 | char padc; |
| 256 | int stop = 0, retval = 0; |
| 257 | |
| 258 | num = 0; |
| 259 | if (!func) |
| 260 | d = (char *) arg; |
| 261 | else |
| 262 | d = NULL; |
| 263 | |
| 264 | if (fmt == NULL) |
| 265 | fmt = "(fmt null)\n"; |
| 266 | |
| 267 | if (radix < 2 || radix > 36) |
| 268 | radix = 10; |
| 269 | |
| 270 | for (;;) { |
| 271 | padc = ' '; |
| 272 | width = 0; |
| 273 | while ((ch = (u_char)*fmt++) != '%' || stop) { |
| 274 | if (ch == '\0') |
| 275 | return (retval); |
| 276 | PCHAR(ch); |
| 277 | } |
| 278 | percent = fmt - 1; |
| 279 | qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; |
| 280 | sign = 0; dot = 0; dwidth = 0; upper = 0; |
| 281 | cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0; |
| 282 | reswitch: switch (ch = (u_char)*fmt++) { |
| 283 | case '.': |
| 284 | dot = 1; |
| 285 | goto reswitch; |
| 286 | case '#': |
| 287 | sharpflag = 1; |
| 288 | goto reswitch; |
| 289 | case '+': |
| 290 | sign = 1; |
| 291 | goto reswitch; |
| 292 | case '-': |
| 293 | ladjust = 1; |
| 294 | goto reswitch; |
| 295 | case '%': |
| 296 | PCHAR(ch); |
| 297 | break; |
| 298 | case '*': |
| 299 | if (!dot) { |
| 300 | width = va_arg(ap, int); |
| 301 | if (width < 0) { |
| 302 | ladjust = !ladjust; |
| 303 | width = -width; |
| 304 | } |
| 305 | } else { |
| 306 | dwidth = va_arg(ap, int); |
| 307 | } |
| 308 | goto reswitch; |
| 309 | case '0': |
| 310 | if (!dot) { |
| 311 | padc = '0'; |
| 312 | goto reswitch; |
| 313 | } |
| 314 | case '1': case '2': case '3': case '4': |
| 315 | case '5': case '6': case '7': case '8': case '9': |
| 316 | for (n = 0;; ++fmt) { |
| 317 | n = n * 10 + ch - '0'; |
| 318 | ch = *fmt; |
| 319 | if (ch < '0' || ch > '9') |
| 320 | break; |
| 321 | } |
| 322 | if (dot) |
| 323 | dwidth = n; |
| 324 | else |
| 325 | width = n; |
| 326 | goto reswitch; |
| 327 | case 'b': |
| 328 | num = (u_int)va_arg(ap, int); |
| 329 | p = va_arg(ap, char *); |
| 330 | for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) |
| 331 | PCHAR(*q--); |
| 332 | |
| 333 | if (num == 0) |
| 334 | break; |
| 335 | |
| 336 | for (tmp = 0; *p;) { |
| 337 | n = *p++; |
| 338 | if (num & (1 << (n - 1))) { |
| 339 | PCHAR(tmp ? ',' : '<'); |
| 340 | for (; (n = *p) > ' '; ++p) |
| 341 | PCHAR(n); |
| 342 | tmp = 1; |
| 343 | } else |
| 344 | for (; *p > ' '; ++p) |
| 345 | continue; |
| 346 | } |
| 347 | if (tmp) |
| 348 | PCHAR('>'); |
| 349 | break; |
| 350 | case 'c': |
| 351 | PCHAR(va_arg(ap, int)); |
| 352 | break; |
| 353 | case 'D': |
| 354 | up = va_arg(ap, u_char *); |
| 355 | p = va_arg(ap, char *); |
| 356 | if (!width) |
| 357 | width = 16; |
| 358 | while(width--) { |
| 359 | PCHAR(hex2ascii(*up >> 4)); |
| 360 | PCHAR(hex2ascii(*up & 0x0f)); |
| 361 | up++; |
| 362 | if (width) |
| 363 | for (q=p;*q;q++) |
| 364 | PCHAR(*q); |
| 365 | } |
| 366 | break; |
| 367 | case 'd': |
| 368 | case 'i': |
| 369 | base = 10; |
| 370 | sign = 1; |
| 371 | goto handle_sign; |
| 372 | case 'h': |
| 373 | if (hflag) { |
| 374 | hflag = 0; |
| 375 | cflag = 1; |
| 376 | } else |
| 377 | hflag = 1; |
| 378 | goto reswitch; |
| 379 | case 'j': |
| 380 | jflag = 1; |
| 381 | goto reswitch; |
| 382 | case 'l': |
| 383 | if (lflag) { |
| 384 | lflag = 0; |
| 385 | qflag = 1; |
| 386 | } else |
| 387 | lflag = 1; |
| 388 | goto reswitch; |
| 389 | case 'n': |
| 390 | if (jflag) |
| 391 | *(va_arg(ap, intmax_t *)) = retval; |
| 392 | else if (qflag) |
| 393 | *(va_arg(ap, quad_t *)) = retval; |
| 394 | else if (lflag) |
| 395 | *(va_arg(ap, long *)) = retval; |
| 396 | else if (zflag) |
| 397 | *(va_arg(ap, size_t *)) = retval; |
| 398 | else if (hflag) |
| 399 | *(va_arg(ap, short *)) = retval; |
| 400 | else if (cflag) |
| 401 | *(va_arg(ap, char *)) = retval; |
| 402 | else |
| 403 | *(va_arg(ap, int *)) = retval; |
| 404 | break; |
| 405 | case 'o': |
| 406 | base = 8; |
| 407 | goto handle_nosign; |
| 408 | case 'p': |
| 409 | base = 16; |
| 410 | sharpflag = (width == 0); |
| 411 | sign = 0; |
| 412 | num = (uintptr_t)va_arg(ap, void *); |
| 413 | goto number; |
| 414 | case 'q': |
| 415 | qflag = 1; |
| 416 | goto reswitch; |
| 417 | case 'r': |
| 418 | base = radix; |
| 419 | if (sign) |
| 420 | goto handle_sign; |
| 421 | goto handle_nosign; |
| 422 | case 's': |
| 423 | p = va_arg(ap, char *); |
| 424 | if (p == NULL) |
| 425 | p = "(null)"; |
| 426 | if (!dot) |
| 427 | n = strlen (p); |
| 428 | else |
| 429 | for (n = 0; n < dwidth && p[n]; n++) |
| 430 | continue; |
| 431 | |
| 432 | width -= n; |
| 433 | |
| 434 | if (!ladjust && width > 0) |
| 435 | while (width--) |
| 436 | PCHAR(padc); |
| 437 | while (n--) |
| 438 | PCHAR(*p++); |
| 439 | if (ladjust && width > 0) |
| 440 | while (width--) |
| 441 | PCHAR(padc); |
| 442 | break; |
| 443 | case 't': |
| 444 | tflag = 1; |
| 445 | goto reswitch; |
| 446 | case 'u': |
| 447 | base = 10; |
| 448 | goto handle_nosign; |
| 449 | case 'X': |
| 450 | upper = 1; |
| 451 | case 'x': |
| 452 | base = 16; |
| 453 | goto handle_nosign; |
| 454 | case 'y': |
| 455 | base = 16; |
| 456 | sign = 1; |
| 457 | goto handle_sign; |
| 458 | case 'z': |
| 459 | zflag = 1; |
| 460 | goto reswitch; |
| 461 | handle_nosign: |
| 462 | sign = 0; |
| 463 | if (jflag) |
| 464 | num = va_arg(ap, uintmax_t); |
| 465 | else if (qflag) |
| 466 | num = va_arg(ap, u_quad_t); |
| 467 | else if (tflag) |
| 468 | num = va_arg(ap, ptrdiff_t); |
| 469 | else if (lflag) |
| 470 | num = va_arg(ap, u_long); |
| 471 | else if (zflag) |
| 472 | num = va_arg(ap, size_t); |
| 473 | else if (hflag) |
| 474 | num = (u_short)va_arg(ap, int); |
| 475 | else if (cflag) |
| 476 | num = (u_char)va_arg(ap, int); |
| 477 | else |
| 478 | num = va_arg(ap, u_int); |
| 479 | goto number; |
| 480 | handle_sign: |
| 481 | if (jflag) |
| 482 | num = va_arg(ap, intmax_t); |
| 483 | else if (qflag) |
| 484 | num = va_arg(ap, quad_t); |
| 485 | else if (tflag) |
| 486 | num = va_arg(ap, ptrdiff_t); |
| 487 | else if (lflag) |
| 488 | num = va_arg(ap, long); |
| 489 | else if (zflag) |
| 490 | num = va_arg(ap, ssize_t); |
| 491 | else if (hflag) |
| 492 | num = (short)va_arg(ap, int); |
| 493 | else if (cflag) |
| 494 | num = (char)va_arg(ap, int); |
| 495 | else |
| 496 | num = va_arg(ap, int); |
| 497 | number: |
| 498 | if (sign && (intmax_t)num < 0) { |
| 499 | neg = 1; |
| 500 | num = -(intmax_t)num; |
| 501 | } |
| 502 | p = ksprintn(nbuf, num, base, &n, upper); |
| 503 | tmp = 0; |
| 504 | if (sharpflag && num != 0) { |
| 505 | if (base == 8) |
| 506 | tmp++; |
| 507 | else if (base == 16) |
| 508 | tmp += 2; |
| 509 | } |
| 510 | if (neg) |
| 511 | tmp++; |
| 512 | |
| 513 | if (!ladjust && padc == '0') |
| 514 | dwidth = width - tmp; |
| 515 | width -= tmp + imax(dwidth, n); |
| 516 | dwidth -= n; |
| 517 | if (!ladjust) |
| 518 | while (width-- > 0) |
| 519 | PCHAR(' '); |
| 520 | if (neg) |
| 521 | PCHAR('-'); |
| 522 | if (sharpflag && num != 0) { |
| 523 | if (base == 8) { |
| 524 | PCHAR('0'); |
| 525 | } else if (base == 16) { |
| 526 | PCHAR('0'); |
| 527 | PCHAR('x'); |
| 528 | } |
| 529 | } |
| 530 | while (dwidth-- > 0) |
| 531 | PCHAR('0'); |
| 532 | |
| 533 | while (*p) |
| 534 | PCHAR(*p--); |
| 535 | |
| 536 | if (ladjust) |
| 537 | while (width-- > 0) |
| 538 | PCHAR(' '); |
| 539 | |
| 540 | break; |
| 541 | default: |
| 542 | while (percent < fmt) |
| 543 | PCHAR(*percent++); |
| 544 | /* |
| 545 | * Since we ignore an formatting argument it is no |
| 546 | * longer safe to obey the remaining formatting |
| 547 | * arguments as the arguments will no longer match |
| 548 | * the format specs. |
| 549 | */ |
| 550 | stop = 1; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | #undef PCHAR |
| 555 | } |