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