2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/printf.c
19 * @brief UTF-8 *printf workalike (core)
22 #define NO_MEMORY_ALLOCATION
23 /* because byte_snprintf used from log.c */
36 /** @brief Flags from a converstion specification
51 /** @brief Possible lengths of a conversion specification */
65 /** @brief Formatter state */
67 /** @brief Output stream */
70 /** @brief Number of bytes written */
73 /** @brief Argument list */
77 /** @brief Definition of a conversion specifier */
79 /** @brief Defining character ('d', 's' etc) */
82 /** @brief Consistency check
83 * @param c Conversion being processed
84 * @return 0 if OK, -1 on error
86 int (*check
)(const struct conversion
*c
);
88 /** @brief Generate output
89 * @param s Formatter state
90 * @param c Conversion being processed
91 * @return 0 on success, -1 on error
93 int (*output
)(struct state
*s
, struct conversion
*c
);
95 /** @brief Number base */
98 /** @brief Digit set */
101 /** @brief Alternative-form prefix */
105 /** @brief One conversion specified as it's handled */
107 /** @brief Flags in this conversion */
110 /** @brief Field width (if @ref f_width) */
113 /** @brief Precision (if @ref f_precision) */
116 /** @brief Length modifier or 0 */
119 /** @brief Specifier used */
120 const struct specifier
*specifier
;
123 /** @brief Flag characters (order significant!) */
124 static const char flags
[] = "'-+ #0";
126 /* write @nbytes@ to the output. Return -1 on error, 0 on success.
127 * Keeps track of the number of bytes written. */
128 static int do_write(struct state
*s
,
131 if(s
->bytes
> INT_MAX
- nbytes
) {
137 if(s
->output
->write(s
->output
, buffer
, nbytes
) < 0)
143 /* write character @ch@ @n@ times, reasonably efficiently */
144 static int do_pad(struct state
*s
, int ch
, unsigned n
) {
149 case ' ': padding
= " "; break;
150 case '0': padding
= "00000000000000000000000000000000"; break;
156 if(do_write(s
, padding
, 32) < 0)
159 if(do_write(s
, padding
, n
) < 0)
164 /* pick up the integer at @ptr@, returning it via @intp@. Return the
165 * number of characters consumed. Return 0 if there is no integer
166 * there and -1 if an error occurred (e.g. too big) */
167 static int get_integer(int *intp
, const char *ptr
) {
172 n
= strtol(ptr
, &e
, 10);
173 if(errno
|| n
> INT_MAX
|| n
< INT_MIN
|| e
== ptr
)
179 /* consistency checks for various conversion specifications */
181 static int check_integer(const struct conversion
*c
) {
198 static int check_string(const struct conversion
*c
) {
201 /* XXX don't support %ls, %lc */
208 static int check_pointer(const struct conversion
*c
) {
214 static int check_percent(const struct conversion
*c
) {
215 if(c
->flags
|| c
->width
|| c
->precision
|| c
->length
)
220 /* output functions for various conversion specifications */
222 static int output_percent(struct state
*s
,
223 struct conversion
attribute((unused
)) *c
) {
224 return do_write(s
, "%", 1);
227 static int output_integer(struct state
*s
, struct conversion
*c
) {
231 int base
, dp
, iszero
, ndigits
, prec
, xform
, sign_bytes
, pad
;
232 char digits
[CHAR_BIT
* sizeof (uintmax_t)]; /* overestimate */
234 switch(c
->specifier
->ch
) {
236 if(c
->specifier
->base
< 0) {
238 case 0: l
= va_arg(s
->ap
, int); break;
239 case l_char
: l
= (signed char)va_arg(s
->ap
, int); break;
240 case l_short
: l
= (short)va_arg(s
->ap
, int); break;
241 case l_long
: l
= va_arg(s
->ap
, long); break;
242 case l_longlong
: l
= va_arg(s
->ap
, long_long
); break;
243 case l_intmax_t
: l
= va_arg(s
->ap
, intmax_t); break;
244 case l_size_t
: l
= va_arg(s
->ap
, ssize_t
); break;
245 case l_ptrdiff_t
: l
= va_arg(s
->ap
, ptrdiff_t); break;
248 base
= -c
->specifier
->base
;
258 case 0: u
= va_arg(s
->ap
, unsigned int); break;
259 case l_char
: u
= (unsigned char)va_arg(s
->ap
, unsigned int); break;
260 case l_short
: u
= (unsigned short)va_arg(s
->ap
, unsigned int); break;
261 case l_long
: u
= va_arg(s
->ap
, unsigned long); break;
262 case l_longlong
: u
= va_arg(s
->ap
, u_long_long
); break;
263 case l_intmax_t
: u
= va_arg(s
->ap
, uintmax_t); break;
264 case l_size_t
: u
= va_arg(s
->ap
, size_t); break;
265 case l_ptrdiff_t
: u
= va_arg(s
->ap
, ptrdiff_t); break;
268 base
= c
->specifier
->base
;
273 u
= (uintptr_t)va_arg(s
->ap
, void *);
275 base
= c
->specifier
->base
;
279 /* default precision */
280 if(!(c
->flags
& f_precision
))
283 if((c
->flags
& f_sign
) && !sign
)
285 /* compute the digits */
289 digits
[--dp
] = c
->specifier
->digits
[u
% base
];
292 ndigits
= sizeof digits
- dp
;
293 /* alternative form */
294 if(c
->flags
& f_hash
) {
297 if((dp
== sizeof digits
|| digits
[dp
] != '0')
298 && c
->precision
<= ndigits
)
299 c
->precision
= ndigits
+ 1;
302 if(!iszero
&& c
->specifier
->xform
)
303 xform
= strlen(c
->specifier
->xform
);
308 /* calculate number of 0s to add for precision */
309 if(ndigits
< c
->precision
)
310 prec
= c
->precision
- ndigits
;
313 /* bytes occupied by the sign */
318 /* XXX implement the ' ' flag */
319 /* calculate number of bytes of padding */
320 if(c
->flags
& f_width
) {
321 if((pad
= c
->width
- (ndigits
+ prec
+ xform
+ sign_bytes
)) < 0)
325 /* now we are ready to output. Possibilities are:
326 * [space pad][sign][xform][0 prec]digits
327 * [sign][xform][0 pad][0 prec]digits
328 * [sign][xform][0 prec]digits[space pad]
332 if(c
->flags
& f_left
) {
333 if(sign
&& do_write(s
, &sign
, 1))
335 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
337 if(prec
&& do_pad(s
, '0', prec
) < 0)
339 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
341 if(pad
&& do_pad(s
, ' ', pad
) < 0)
343 } else if(c
->flags
& f_zero
) {
344 if(sign
&& do_write(s
, &sign
, 1))
346 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
348 if(pad
&& do_pad(s
, '0', pad
) < 0)
350 if(prec
&& do_pad(s
, '0', prec
) < 0)
352 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
355 if(pad
&& do_pad(s
, ' ', pad
) < 0)
357 if(sign
&& do_write(s
, &sign
, 1))
359 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
361 if(prec
&& do_pad(s
, '0', prec
) < 0)
363 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
369 static int output_string(struct state
*s
, struct conversion
*c
) {
373 str
= va_arg(s
->ap
, const char *);
374 if(c
->flags
& f_precision
) {
375 if((n
= memchr(str
, 0, c
->precision
)))
381 if(c
->flags
& f_width
) {
382 if((pad
= c
->width
- len
) < 0)
386 if(c
->flags
& f_left
) {
387 if(do_write(s
, str
, len
) < 0)
389 if(pad
&& do_pad(s
, ' ', pad
) < 0)
392 if(pad
&& do_pad(s
, ' ', pad
) < 0)
394 if(do_write(s
, str
, len
) < 0)
401 static int output_char(struct state
*s
, struct conversion
*c
) {
405 ch
= va_arg(s
->ap
, int);
406 if(c
->flags
& f_width
) {
407 if((pad
= c
->width
- 1) < 0)
411 if(c
->flags
& f_left
) {
412 if(do_write(s
, &ch
, 1) < 0)
414 if(pad
&& do_pad(s
, ' ', pad
) < 0)
417 if(pad
&& do_pad(s
, ' ', pad
) < 0)
419 if(do_write(s
, &ch
, 1) < 0)
425 static int output_count(struct state
*s
, struct conversion
*c
) {
427 case 0: *va_arg(s
->ap
, int *) = s
->bytes
; break;
428 case l_char
: *va_arg(s
->ap
, signed char *) = s
->bytes
; break;
429 case l_short
: *va_arg(s
->ap
, short *) = s
->bytes
; break;
430 case l_long
: *va_arg(s
->ap
, long *) = s
->bytes
; break;
431 case l_longlong
: *va_arg(s
->ap
, long_long
*) = s
->bytes
; break;
432 case l_intmax_t
: *va_arg(s
->ap
, intmax_t *) = s
->bytes
; break;
433 case l_size_t
: *va_arg(s
->ap
, ssize_t
*) = s
->bytes
; break;
434 case l_ptrdiff_t
: *va_arg(s
->ap
, ptrdiff_t *) = s
->bytes
; break;
440 /* table of conversion specifiers */
441 static const struct specifier specifiers
[] = {
442 /* XXX don't support floating point conversions */
443 { '%', check_percent
, output_percent
, 0, 0, 0 },
444 { 'X', check_integer
, output_integer
, 16, "0123456789ABCDEF", "0X" },
445 { 'c', check_string
, output_char
, 0, 0, 0 },
446 { 'd', check_integer
, output_integer
, -10, "0123456789", 0 },
447 { 'i', check_integer
, output_integer
, -10, "0123456789", 0 },
448 { 'n', check_integer
, output_count
, 0, 0, 0 },
449 { 'o', check_integer
, output_integer
, 8, "01234567", 0 },
450 { 'p', check_pointer
, output_integer
, 16, "0123456789abcdef", "0x" },
451 { 's', check_string
, output_string
, 0, 0, 0 },
452 { 'u', check_integer
, output_integer
, 10, "0123456789", 0 },
453 { 'x', check_integer
, output_integer
, 16, "0123456789abcdef", "0x" },
456 /* collect and check information about a conversion specification */
457 static int parse_conversion(struct conversion
*c
, const char *ptr
) {
459 const char *q
, *start
= ptr
;
461 memset(c
, 0, sizeof *c
);
463 while(*ptr
&& (q
= strchr(flags
, *ptr
))) {
464 c
->flags
|= (1 << (q
- flags
));
467 /* minimum field width */
468 if(*ptr
>= '0' && *ptr
<= '9') {
469 if((n
= get_integer(&c
->width
, ptr
)) < 0)
473 } else if(*ptr
== '*') {
481 if(*ptr
>= '0' && *ptr
<= '9') {
482 if((n
= get_integer(&c
->precision
, ptr
)) < 0)
485 } else if(*ptr
== '*') {
490 c
->flags
|= f_precision
;
492 /* length modifier */
493 switch(ch
= *ptr
++) {
495 if((ch
= *ptr
++) == 'h') {
503 if((ch
= *ptr
++) == 'l') {
504 c
->length
= l_longlong
;
510 case 'q': c
->length
= l_longlong
; ch
= *ptr
++; break;
511 case 'j': c
->length
= l_intmax_t
; ch
= *ptr
++; break;
512 case 'z': c
->length
= l_size_t
; ch
= *ptr
++; break;
513 case 't': c
->length
= l_ptrdiff_t
; ch
= *ptr
++; break;
514 case 'L': c
->length
= l_longdouble
; ch
= *ptr
++; break;
516 /* conversion specifier */
518 r
= sizeof specifiers
/ sizeof *specifiers
;
519 while(l
<= r
&& (specifiers
[m
= (l
+ r
) / 2].ch
!= ch
))
520 if(ch
< specifiers
[m
].ch
)
524 if(specifiers
[m
].ch
!= ch
)
526 if(specifiers
[m
].check(c
))
528 c
->specifier
= &specifiers
[m
];
532 /* ISO/IEC 9899:1999 7.19.6.1 */
533 /* http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html */
535 int byte_vsinkprintf(struct sink
*output
,
543 memset(&s
, 0, sizeof s
);
547 /* output text up to next conversion specification */
548 for(ptr
= fmt
; *fmt
&& *fmt
!= '%'; ++fmt
)
551 if(do_write(&s
, ptr
, n
) < 0)
556 /* parse conversion */
557 if((n
= parse_conversion(&c
, fmt
)) < 0)
560 /* fill in width and precision */
561 if((c
.flags
& f_width
) && c
.width
== -1)
562 if((c
.width
= va_arg(s
.ap
, int)) < 0) {
566 if((c
.flags
& f_precision
) && c
.precision
== -1)
567 if((c
.precision
= va_arg(s
.ap
, int)) < 0)
568 c
.flags
^= f_precision
;
569 /* generate the output */
570 if(c
.specifier
->output(&s
, &c
) < 0)
580 int byte_sinkprintf(struct sink
*output
, const char *fmt
, ...) {
585 n
= byte_vsinkprintf(output
, fmt
, ap
);