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