2 * Copyright (c) 2005 Poul-Henning Kamp
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "namespace.h"
38 #include <sys/types.h>
46 #include <namespace.h>
49 #include "un-namespace.h"
55 int __use_xprintf = -1;
57 /* private stuff -----------------------------------------------------*/
63 #ifndef NO_FLOATING_POINT
65 long double longdoublearg;
74 * Macros for converting digits to letters and vice versa
76 #define to_digit(c) ((c) - '0')
77 #define is_digit(c) (((unsigned)to_digit(c)) <= 9)
79 /* various globals ---------------------------------------------------*/
81 const char __lowercase_hex[17] = "0123456789abcdef?"; /*lint !e784 */
82 const char __uppercase_hex[17] = "0123456789ABCDEF?"; /*lint !e784 */
85 static char blanks[PADSIZE] =
86 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
87 static char zeroes[PADSIZE] =
88 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
90 /* printing and padding functions ------------------------------------*/
97 struct __siov iov[NIOV];
102 __printf_init(struct __printf_io *io)
105 io->uio.uio_iov = io->iovp = &io->iov[0];
106 io->uio.uio_resid = 0;
107 io->uio.uio_iovcnt = 0;
111 __printf_flush(struct __printf_io *io)
114 __sfvwrite(io->fp, &io->uio);
119 __printf_puts(struct __printf_io *io, const void *ptr, int len)
123 if (io->fp->_flags & __SERR)
127 io->iovp->iov_base = __DECONST(void *, ptr);
128 io->iovp->iov_len = len;
129 io->uio.uio_resid += len;
131 io->uio.uio_iovcnt++;
132 if (io->uio.uio_iovcnt >= NIOV)
138 __printf_pad(struct __printf_io *io, int howmany, int zero)
149 if ((n = (howmany)) > 0) {
150 while (n > PADSIZE) {
151 ret += __printf_puts(io, with, PADSIZE);
154 ret += __printf_puts(io, with, n);
160 __printf_out(struct __printf_io *io, const struct printf_info *pi, const void *ptr, int len)
164 if ((!pi->left) && pi->width > len)
165 ret += __printf_pad(io, pi->width - len, pi->pad == '0');
166 ret += __printf_puts(io, ptr, len);
167 if (pi->left && pi->width > len)
168 ret += __printf_pad(io, pi->width - len, pi->pad == '0');
173 /* percent handling -------------------------------------------------*/
176 __printf_arginfo_pct(const struct printf_info *pi __unused, size_t n __unused, int *argt __unused)
183 __printf_render_pct(struct __printf_io *io, const struct printf_info *pi __unused, const void *const *arg __unused)
186 return (__printf_puts(io, "%", 1));
189 /* 'n' ---------------------------------------------------------------*/
192 __printf_arginfo_n(const struct printf_info *pi, size_t n, int *argt)
196 argt[0] = PA_POINTER;
201 * This is a printf_render so that all output has been flushed before it
206 __printf_render_n(FILE *io __unused, const struct printf_info *pi, const void *const *arg)
210 **((signed char **)arg[0]) = (signed char)pi->sofar;
211 else if (pi->is_short)
212 **((short **)arg[0]) = (short)pi->sofar;
213 else if (pi->is_long)
214 **((long **)arg[0]) = pi->sofar;
215 else if (pi->is_long_double)
216 **((long long **)arg[0]) = pi->sofar;
217 else if (pi->is_intmax)
218 **((intmax_t **)arg[0]) = pi->sofar;
219 else if (pi->is_ptrdiff)
220 **((ptrdiff_t **)arg[0]) = pi->sofar;
221 else if (pi->is_quad)
222 **((quad_t **)arg[0]) = pi->sofar;
223 else if (pi->is_size)
224 **((size_t **)arg[0]) = pi->sofar;
226 **((int **)arg[0]) = pi->sofar;
231 /* table -------------------------------------------------------------*/
233 /*lint -esym(785, printf_tbl) */
235 printf_arginfo_function *arginfo;
236 printf_function *gnurender;
237 printf_render *render;
238 } printf_tbl[256] = {
239 ['%'] = { __printf_arginfo_pct, NULL, __printf_render_pct },
240 ['A'] = { __printf_arginfo_float, NULL, __printf_render_float },
241 ['C'] = { __printf_arginfo_chr, NULL, __printf_render_chr },
242 ['E'] = { __printf_arginfo_float, NULL, __printf_render_float },
243 ['F'] = { __printf_arginfo_float, NULL, __printf_render_float },
244 ['G'] = { __printf_arginfo_float, NULL, __printf_render_float },
245 ['S'] = { __printf_arginfo_str, NULL, __printf_render_str },
246 ['X'] = { __printf_arginfo_int, NULL, __printf_render_int },
247 ['a'] = { __printf_arginfo_float, NULL, __printf_render_float },
248 ['c'] = { __printf_arginfo_chr, NULL, __printf_render_chr },
249 ['d'] = { __printf_arginfo_int, NULL, __printf_render_int },
250 ['e'] = { __printf_arginfo_float, NULL, __printf_render_float },
251 ['f'] = { __printf_arginfo_float, NULL, __printf_render_float },
252 ['g'] = { __printf_arginfo_float, NULL, __printf_render_float },
253 ['i'] = { __printf_arginfo_int, NULL, __printf_render_int },
254 ['n'] = { __printf_arginfo_n, __printf_render_n, NULL },
255 ['o'] = { __printf_arginfo_int, NULL, __printf_render_int },
256 ['p'] = { __printf_arginfo_ptr, NULL, __printf_render_ptr },
257 ['q'] = { __printf_arginfo_int, NULL, __printf_render_int },
258 ['s'] = { __printf_arginfo_str, NULL, __printf_render_str },
259 ['u'] = { __printf_arginfo_int, NULL, __printf_render_int },
260 ['x'] = { __printf_arginfo_int, NULL, __printf_render_int },
265 __v2printf(FILE *fp, const char *fmt0, unsigned pct, va_list ap)
267 struct printf_info *pi, *pil;
270 struct printf_info pia[pct + 10];
272 union arg args[pct + 10];
277 struct __printf_io io;
285 memset(argt, 0, sizeof argt);
286 for (pi = pia; ; pi++) {
287 memset(pi, 0, sizeof *pi);
294 pi->begin = pi->end = fmt;
295 while (*fmt != '\0' && *fmt != '%')
305 * ``If the space and + flags both appear, the space
306 * flag will be ignored.''
309 if (pi->showsign == 0)
322 pi->get_prec = nextarg;
323 argt[nextarg++] = PA_INT;
326 while (*fmt != '\0' && is_digit(*fmt)) {
328 pi->prec += to_digit(*fmt);
342 pi->get_width = nextarg;
343 argt[nextarg++] = PA_INT;
354 * ``Note that 0 is taken as a flag, not as the
355 * beginning of a field width.''
361 case '1': case '2': case '3':
362 case '4': case '5': case '6':
363 case '7': case '8': case '9':
365 while (*fmt != '\0' && is_digit(*fmt)) {
371 if (nextarg > maxarg)
381 pi->spec += ('a' - 'A');
383 if (pi->is_long_double || pi->is_quad) {
385 pi->is_long_double = 1;
388 pi->is_long_double = 0;
402 pi->is_long_double = 1;
418 pi->is_long_double = 1;
437 if (printf_tbl[pi->spec].arginfo == NULL)
438 errx(1, "arginfo[%c] = NULL", pi->spec);
439 ch = printf_tbl[pi->spec].arginfo(
440 pi, __PRINTFMAXARG, &argt[nextarg]);
442 pi->arg[0] = &args[nextarg];
444 pi->arg[1] = &args[nextarg + 1];
449 if (nextarg > maxarg)
452 fprintf(stderr, "fmt0 <%s>\n", fmt0);
453 fprintf(stderr, "pil %p\n", pil);
455 for (ch = 1; ch < maxarg; ch++) {
457 fprintf(stderr, "arg %d %x\n", ch, argt[ch]);
461 args[ch].intarg = (char)va_arg (ap, int);
464 args[ch].intarg = va_arg (ap, int);
466 case PA_INT | PA_FLAG_SHORT:
467 args[ch].intarg = (short)va_arg (ap, int);
469 case PA_INT | PA_FLAG_LONG:
470 args[ch].longarg = va_arg (ap, long);
472 case PA_INT | PA_FLAG_INTMAX:
473 args[ch].intmaxarg = va_arg (ap, intmax_t);
475 case PA_INT | PA_FLAG_QUAD:
476 args[ch].intmaxarg = va_arg (ap, quad_t);
478 case PA_INT | PA_FLAG_LONG_LONG:
479 args[ch].intmaxarg = va_arg (ap, long long);
481 case PA_INT | PA_FLAG_SIZE:
482 args[ch].intmaxarg = va_arg (ap, size_t);
484 case PA_INT | PA_FLAG_PTRDIFF:
485 args[ch].intmaxarg = va_arg (ap, ptrdiff_t);
488 args[ch].wintarg = va_arg (ap, wint_t);
491 args[ch].pvoidarg = va_arg (ap, void *);
494 args[ch].pchararg = va_arg (ap, char *);
497 args[ch].pwchararg = va_arg (ap, wchar_t *);
500 #ifndef NO_FLOATING_POINT
501 args[ch].doublearg = va_arg (ap, double);
504 case PA_DOUBLE | PA_FLAG_LONG_DOUBLE:
505 #ifndef NO_FLOATING_POINT
506 args[ch].longdoublearg = va_arg (ap, long double);
510 errx(1, "argtype = %x (fmt = \"%s\")\n",
514 for (pi = pia; pi < pil; pi++) {
516 fprintf(stderr, "pi %p", pi);
517 fprintf(stderr, " spec '%c'", pi->spec);
518 fprintf(stderr, " args %d",
519 ((uintptr_t)pi->arg[0] - (uintptr_t)args) / sizeof args[0]);
520 if (pi->width) fprintf(stderr, " width %d", pi->width);
521 if (pi->pad) fprintf(stderr, " pad 0x%x", pi->pad);
522 if (pi->left) fprintf(stderr, " left");
523 if (pi->showsign) fprintf(stderr, " showsign");
524 if (pi->prec != -1) fprintf(stderr, " prec %d", pi->prec);
525 if (pi->is_char) fprintf(stderr, " char");
526 if (pi->is_short) fprintf(stderr, " short");
527 if (pi->is_long) fprintf(stderr, " long");
528 if (pi->is_long_double) fprintf(stderr, " long_double");
529 fprintf(stderr, "\n");
530 fprintf(stderr, "\t\"%.*s\"\n", pi->end - pi->begin, pi->begin);
533 pi->width = args[pi->get_width].intarg;
535 * ``A negative field width argument is taken as a
536 * - flag followed by a positive field width.''
538 * They don't exclude field widths read from args.
542 pi->width = -pi->width;
546 pi->prec = args[pi->get_prec].intarg;
547 ret += __printf_puts(&io, pi->begin, pi->end - pi->begin);
548 if (printf_tbl[pi->spec].gnurender != NULL) {
551 ret += printf_tbl[pi->spec].gnurender(
552 fp, pi, (const void *)pi->arg);
553 } else if (printf_tbl[pi->spec].render != NULL) {
555 n = printf_tbl[pi->spec].render(
556 &io, pi, (const void *)pi->arg);
558 io.fp->_flags |= __SERR;
561 } else if (pi->begin == pi->end)
562 errx(1, "render[%c] = NULL", *fmt);
568 extern int __fflush(FILE *fp);
571 * Helper function for `fprintf to unbuffered unix file': creates a
572 * temporary buffer. We only work on write-only files; this avoids
573 * worries about ungetc buffers and so forth.
576 __v3printf(FILE *fp, const char *fmt, int pct, va_list ap)
579 FILE fake = FAKE_FILE;
580 unsigned char buf[BUFSIZ];
582 /* copy the important variables */
583 fake._flags = fp->_flags & ~__SNBF;
584 fake._file = fp->_file;
585 fake._cookie = fp->_cookie;
586 fake._write = fp->_write;
587 fake._orientation = fp->_orientation;
588 fake._mbstate = fp->_mbstate;
590 /* set up the buffer */
591 fake._bf._base = fake._p = buf;
592 fake._bf._size = fake._w = sizeof(buf);
593 fake._lbfsize = 0; /* not actually used, but Just In Case */
595 /* do the work, then copy any error status */
596 ret = __v2printf(&fake, fmt, pct, ap);
597 if (ret >= 0 && __fflush(&fake))
599 if (fake._flags & __SERR)
600 fp->_flags |= __SERR;
605 __xvprintf(FILE *fp, const char *fmt0, va_list ap)
610 /* Count number of '%' signs handling double '%' signs */
611 for (p = fmt0, u = 0; *p; p++) {
619 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
620 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
622 return (__v3printf(fp, fmt0, u, ap));
624 return (__v2printf(fp, fmt0, u, ap));
627 /* extending ---------------------------------------------------------*/
630 register_printf_function(int spec, printf_function *render, printf_arginfo_function *arginfo)
633 if (spec > 255 || spec < 0)
635 printf_tbl[spec].gnurender = render;
636 printf_tbl[spec].arginfo = arginfo;
642 register_printf_render(int spec, printf_render *render, printf_arginfo_function *arginfo)
645 if (spec > 255 || spec < 0)
647 printf_tbl[spec].render = render;
648 printf_tbl[spec].arginfo = arginfo;
654 register_printf_render_std(const char *specs)
657 for (; *specs != '\0'; specs++) {
660 register_printf_render(*specs,
661 __printf_render_hexdump,
662 __printf_arginfo_hexdump);
665 register_printf_render(*specs,
666 __printf_render_errno,
667 __printf_arginfo_errno);
670 register_printf_render(*specs,
671 __printf_render_quote,
672 __printf_arginfo_quote);
675 register_printf_render(*specs,
676 __printf_render_time,
677 __printf_arginfo_time);
680 register_printf_render(*specs,
682 __printf_arginfo_vis);