xref: /freebsd/include/printf.h (revision 84b3c4547afa496b37e86c5e649e99237c8afc6e)
1  /*-
2   * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3   *
4   * Copyright (c) 2005 Poul-Henning Kamp
5   * 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   *
16   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26   * SUCH DAMAGE.
27   *
28   * $FreeBSD$
29   */
30  
31  #ifndef _PRINTF_H_
32  #define _PRINTF_H_
33  
34  #include <stdio.h>
35  #include <wchar.h>
36  
37  /*
38   * The API defined by glibc allows a renderer to take multiple arguments
39   * This is obviously usable for things like (ptr+len) pairs etc.
40   * But the do not actually provide support for it at the end of the day,
41   * they offer only one argument to the arginfo function, but do accept
42   * >1 returns, although the do not check the types of those arguments
43   * argument
44   * Be compatible for now.
45   */
46  #define __PRINTFMAXARG		2
47  
48  struct printf_info {
49  	/* GLIBC compatible */
50  	int		prec;
51  	int		width;
52  	wchar_t		spec;
53  	unsigned 	is_long_double;
54  	unsigned 	is_char;
55  	unsigned	is_short;
56  	unsigned	is_long;
57  	unsigned	alt;
58  	unsigned	space;
59  	unsigned	left;
60  	unsigned	showsign;
61  	unsigned	group;
62  	unsigned	extra;
63  	unsigned	wide;
64  	wchar_t		pad;
65  
66  	/* FreeBSD extensions */
67  
68  	unsigned	is_quad;
69  	unsigned	is_intmax;
70  	unsigned	is_ptrdiff;
71  	unsigned	is_size;
72  
73  	/* private */
74  	int		sofar;
75  	unsigned	get_width;
76  	unsigned	get_prec;
77  	const char	*begin;
78  	const char	*end;
79  	void 		*arg[__PRINTFMAXARG];
80  };
81  
82  enum {
83  	PA_INT		= (1 << 0),	/* int */
84  	PA_CHAR		= (1 << 1),	/* int, cast to char */
85  	PA_WCHAR	= (1 << 2),	/* wide char */
86  	PA_STRING	= (1 << 3),	/* const char * (with '\0') */
87  	PA_WSTRING	= (1 << 4),	/* const wchar_t * */
88  	PA_POINTER	= (1 << 5),	/* void * */
89  	PA_FLOAT	= (1 << 6),	/* float */
90  	PA_DOUBLE	= (1 << 7) 	/* double */
91  };
92  
93  #define	PA_FLAG_MASK		0xff0000
94  #define	PA_FLAG_LONG_LONG	(1 << 16)
95  #define	PA_FLAG_LONG		(1 << 17)
96  #define	PA_FLAG_SHORT		(1 << 18)
97  #define	PA_FLAG_PTR		(1 << 19)
98  #define	PA_FLAG_QUAD		(1 << 20)
99  #define	PA_FLAG_INTMAX		(1 << 21)
100  #define	PA_FLAG_SIZE		(1 << 22)
101  #define	PA_FLAG_PTRDIFF		(1 << 23)
102  #define	PA_FLAG_LONG_DOUBLE	PA_FLAG_LONG_LONG
103  
104  typedef int printf_arginfo_function(const struct printf_info *, size_t, int *);
105  typedef int printf_function(FILE *, const struct printf_info *, const void *const *);
106  
107  /* FreeBSD extension */
108  struct __printf_io;
109  typedef int printf_render(struct __printf_io *, const struct printf_info *, const void *const *);
110  
111  /* vprintf.c */
112  extern const char __lowercase_hex[17];
113  extern const char __uppercase_hex[17];
114  
115  void __printf_flush(struct __printf_io *io);
116  int __printf_puts(struct __printf_io *io, const void *ptr, int len);
117  int __printf_pad(struct __printf_io *io, int n, int zero);
118  int __printf_out(struct __printf_io *io, const struct printf_info *pi, const void *ptr, int len);
119  
120  int __xvprintf(FILE *fp, const char *fmt0, va_list ap);
121  extern int __use_xprintf;
122  
123  /* GLIBC compat */
124  int register_printf_function(int spec, printf_function *render, printf_arginfo_function *arginfo);
125  
126  /* FreeBSD */
127  int register_printf_render(int spec, printf_render *render, printf_arginfo_function *arginfo);
128  int register_printf_render_std(const char *specs);
129  
130  /* vprintf_errno.c */
131  printf_arginfo_function		__printf_arginfo_errno;
132  printf_render			__printf_render_errno;
133  
134  /* vprintf_float.c */
135  printf_arginfo_function		__printf_arginfo_float;
136  printf_render			__printf_render_float;
137  
138  /* vprintf_hexdump.c */
139  printf_arginfo_function		__printf_arginfo_hexdump;
140  printf_render 			__printf_render_hexdump;
141  
142  /* vprintf_int.c */
143  printf_arginfo_function		__printf_arginfo_ptr;
144  printf_arginfo_function		__printf_arginfo_int;
145  printf_render			__printf_render_ptr;
146  printf_render			__printf_render_int;
147  
148  /* vprintf_quoute.c */
149  printf_arginfo_function		__printf_arginfo_quote;
150  printf_render 			__printf_render_quote;
151  
152  /* vprintf_str.c */
153  printf_arginfo_function		__printf_arginfo_chr;
154  printf_render			__printf_render_chr;
155  printf_arginfo_function		__printf_arginfo_str;
156  printf_render			__printf_render_str;
157  
158  /* vprintf_time.c */
159  printf_arginfo_function		__printf_arginfo_time;
160  printf_render			__printf_render_time;
161  
162  /* vprintf_vis.c */
163  printf_arginfo_function		__printf_arginfo_vis;
164  printf_render 			__printf_render_vis;
165  
166  #endif /* !_PRINTF_H */
167