1153486Sphk/*-
2153486Sphk * Copyright (c) 2005 Poul-Henning Kamp
3153486Sphk * All rights reserved.
4153486Sphk *
5153486Sphk * Redistribution and use in source and binary forms, with or without
6153486Sphk * modification, are permitted provided that the following conditions
7153486Sphk * are met:
8153486Sphk * 1. Redistributions of source code must retain the above copyright
9153486Sphk *    notice, this list of conditions and the following disclaimer.
10153486Sphk * 2. Redistributions in binary form must reproduce the above copyright
11153486Sphk *    notice, this list of conditions and the following disclaimer in the
12153486Sphk *    documentation and/or other materials provided with the distribution.
13153486Sphk *
14153486Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15153486Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16153486Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17153486Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18153486Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19153486Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20153486Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21153486Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22153486Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23153486Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24153486Sphk * SUCH DAMAGE.
25153486Sphk *
26153486Sphk * $FreeBSD$
27153486Sphk */
28153486Sphk
29153486Sphk#ifndef _PRINTF_H_
30153486Sphk#define _PRINTF_H_
31153486Sphk
32219343Spjd#include <stdio.h>
33156208Sphk#include <wchar.h>
34156208Sphk
35153486Sphk/*
36153486Sphk * The API defined by glibc allows a renderer to take multiple arguments
37153486Sphk * This is obviously usable for things like (ptr+len) pairs etc.
38153486Sphk * But the do not actually provide support for it at the end of the day,
39153486Sphk * they offer only one argument to the arginfo function, but do accept
40153486Sphk * >1 returns, although the do not check the types of those arguments
41153486Sphk * argument
42153486Sphk * Be compatible for now.
43153486Sphk */
44153486Sphk#define __PRINTFMAXARG		2
45153486Sphk
46153486Sphkstruct printf_info {
47153486Sphk	/* GLIBC compatible */
48153486Sphk	int		prec;
49153486Sphk	int		width;
50153486Sphk	wchar_t		spec;
51153486Sphk	unsigned 	is_long_double;
52153486Sphk	unsigned 	is_char;
53153486Sphk	unsigned	is_short;
54153486Sphk	unsigned	is_long;
55153486Sphk	unsigned	alt;
56153486Sphk	unsigned	space;
57153486Sphk	unsigned	left;
58153486Sphk	unsigned	showsign;
59153486Sphk	unsigned	group;
60153486Sphk	unsigned	extra;
61153486Sphk	unsigned	wide;
62153486Sphk	wchar_t		pad;
63153486Sphk
64153486Sphk	/* FreeBSD extensions */
65153486Sphk
66153486Sphk	unsigned	is_quad;
67153486Sphk	unsigned	is_intmax;
68153486Sphk	unsigned	is_ptrdiff;
69153486Sphk	unsigned	is_size;
70153486Sphk
71153486Sphk	/* private */
72153486Sphk	int		sofar;
73153486Sphk	unsigned	get_width;
74153486Sphk	unsigned	get_prec;
75153486Sphk	const char	*begin;
76153486Sphk	const char	*end;
77153486Sphk	void 		*arg[__PRINTFMAXARG];
78153486Sphk};
79153486Sphk
80153486Sphkenum {
81153486Sphk	PA_INT		= (1 << 0),	/* int */
82153486Sphk	PA_CHAR		= (1 << 1),	/* int, cast to char */
83153486Sphk	PA_WCHAR	= (1 << 2),	/* wide char */
84153486Sphk	PA_STRING	= (1 << 3),	/* const char * (with '\0') */
85153486Sphk	PA_WSTRING	= (1 << 4),	/* const wchar_t * */
86153486Sphk	PA_POINTER	= (1 << 5),	/* void * */
87153486Sphk	PA_FLOAT	= (1 << 6),	/* float */
88153486Sphk	PA_DOUBLE	= (1 << 7) 	/* double */
89153486Sphk};
90153486Sphk
91153486Sphk#define	PA_FLAG_MASK		0xff0000
92153486Sphk#define	PA_FLAG_LONG_LONG	(1 << 16)
93153486Sphk#define	PA_FLAG_LONG		(1 << 17)
94153486Sphk#define	PA_FLAG_SHORT		(1 << 18)
95153486Sphk#define	PA_FLAG_PTR		(1 << 19)
96153486Sphk#define	PA_FLAG_QUAD		(1 << 20)
97153486Sphk#define	PA_FLAG_INTMAX		(1 << 21)
98153486Sphk#define	PA_FLAG_SIZE		(1 << 22)
99153486Sphk#define	PA_FLAG_PTRDIFF		(1 << 23)
100153486Sphk#define	PA_FLAG_LONG_DOUBLE	PA_FLAG_LONG_LONG
101153486Sphk
102153486Sphktypedef int printf_arginfo_function(const struct printf_info *, size_t, int *);
103153486Sphktypedef int printf_function(FILE *, const struct printf_info *, const void *const *);
104153486Sphk
105153486Sphk/* FreeBSD extension */
106153486Sphkstruct __printf_io;
107153486Sphktypedef int printf_render(struct __printf_io *, const struct printf_info *, const void *const *);
108153486Sphk
109153486Sphk/* vprintf.c */
110153486Sphkextern const char __lowercase_hex[17];
111153486Sphkextern const char __uppercase_hex[17];
112153486Sphk
113153486Sphkvoid __printf_flush(struct __printf_io *io);
114153486Sphkint __printf_puts(struct __printf_io *io, const void *ptr, int len);
115153486Sphkint __printf_pad(struct __printf_io *io, int n, int zero);
116153486Sphkint __printf_out(struct __printf_io *io, const struct printf_info *pi, const void *ptr, int len);
117153486Sphk
118153486Sphkint __xvprintf(FILE *fp, const char *fmt0, va_list ap);
119153486Sphkextern int __use_xprintf;
120153486Sphk
121153486Sphk/* GLIBC compat */
122153486Sphkint register_printf_function(int spec, printf_function *render, printf_arginfo_function *arginfo);
123153486Sphk
124153486Sphk/* FreeBSD */
125153486Sphkint register_printf_render(int spec, printf_render *render, printf_arginfo_function *arginfo);
126238111Spjdint register_printf_render_std(const char *specs);
127153486Sphk
128154815Sphk/* vprintf_errno.c */
129154815Sphkprintf_arginfo_function		__printf_arginfo_errno;
130154815Sphkprintf_render			__printf_render_errno;
131154815Sphk
132153486Sphk/* vprintf_float.c */
133153486Sphkprintf_arginfo_function		__printf_arginfo_float;
134153486Sphkprintf_render			__printf_render_float;
135153486Sphk
136154815Sphk/* vprintf_hexdump.c */
137154815Sphkprintf_arginfo_function		__printf_arginfo_hexdump;
138154815Sphkprintf_render 			__printf_render_hexdump;
139154815Sphk
140153486Sphk/* vprintf_int.c */
141153486Sphkprintf_arginfo_function		__printf_arginfo_ptr;
142153486Sphkprintf_arginfo_function		__printf_arginfo_int;
143153486Sphkprintf_render			__printf_render_ptr;
144153486Sphkprintf_render			__printf_render_int;
145153486Sphk
146154815Sphk/* vprintf_quoute.c */
147154815Sphkprintf_arginfo_function		__printf_arginfo_quote;
148154815Sphkprintf_render 			__printf_render_quote;
149154815Sphk
150153486Sphk/* vprintf_str.c */
151153486Sphkprintf_arginfo_function		__printf_arginfo_chr;
152153486Sphkprintf_render			__printf_render_chr;
153153486Sphkprintf_arginfo_function		__printf_arginfo_str;
154153486Sphkprintf_render			__printf_render_str;
155153486Sphk
156153486Sphk/* vprintf_time.c */
157153486Sphkprintf_arginfo_function		__printf_arginfo_time;
158153486Sphkprintf_render			__printf_render_time;
159153486Sphk
160153486Sphk/* vprintf_vis.c */
161153486Sphkprintf_arginfo_function		__printf_arginfo_vis;
162153486Sphkprintf_render 			__printf_render_vis;
163153486Sphk
164153486Sphk#endif /* !_PRINTF_H */
165