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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 #define NO_MEMORY_ALLOCATION
22 /* because byte_snprintf used from log.c */
66 int (*check
)(const struct conversion
*c
);
67 int (*output
)(struct state
*s
, struct conversion
*c
);
78 const struct specifier
*specifier
;
81 static const char flags
[] = "'-+ #0";
83 /* write @nbytes@ to the output. Return -1 on error, 0 on success.
84 * Keeps track of the number of bytes written. */
85 static int do_write(struct state
*s
,
88 if(s
->bytes
> INT_MAX
- nbytes
) {
94 if(s
->output
->write(s
->output
, buffer
, nbytes
) < 0)
100 /* write character @ch@ @n@ times, reasonably efficiently */
101 static int do_pad(struct state
*s
, int ch
, unsigned n
) {
106 case ' ': padding
= " "; break;
107 case '0': padding
= "00000000000000000000000000000000"; break;
113 if(do_write(s
, padding
, 32) < 0)
116 if(do_write(s
, padding
, n
) < 0)
121 /* pick up the integer at @ptr@, returning it via @intp@. Return the
122 * number of characters consumed. Return 0 if there is no integer
123 * there and -1 if an error occurred (e.g. too big) */
124 static int get_integer(int *intp
, const char *ptr
) {
129 n
= strtol(ptr
, &e
, 10);
130 if(errno
|| n
> INT_MAX
|| n
< INT_MIN
|| e
== ptr
)
136 /* consistency checks for various conversion specifications */
138 static int check_integer(const struct conversion
*c
) {
155 static int check_string(const struct conversion
*c
) {
158 /* XXX don't support %ls, %lc */
165 static int check_pointer(const struct conversion
*c
) {
171 static int check_percent(const struct conversion
*c
) {
172 if(c
->flags
|| c
->width
|| c
->precision
|| c
->length
)
177 /* output functions for various conversion specifications */
179 static int output_percent(struct state
*s
,
180 struct conversion
attribute((unused
)) *c
) {
181 return do_write(s
, "%", 1);
184 static int output_integer(struct state
*s
, struct conversion
*c
) {
188 int base
, dp
, iszero
, ndigits
, prec
, xform
, sign_bytes
, pad
;
189 char digits
[CHAR_BIT
* sizeof (uintmax_t)]; /* overestimate */
191 switch(c
->specifier
->ch
) {
193 if(c
->specifier
->base
< 0) {
195 case 0: l
= va_arg(s
->ap
, int); break;
196 case l_char
: l
= (signed char)va_arg(s
->ap
, int); break;
197 case l_short
: l
= (short)va_arg(s
->ap
, int); break;
198 case l_long
: l
= va_arg(s
->ap
, long); break;
199 case l_longlong
: l
= va_arg(s
->ap
, long_long
); break;
200 case l_intmax_t
: l
= va_arg(s
->ap
, intmax_t); break;
201 case l_size_t
: l
= va_arg(s
->ap
, ssize_t
); break;
202 case l_ptrdiff_t
: l
= va_arg(s
->ap
, ptrdiff_t); break;
205 base
= -c
->specifier
->base
;
215 case 0: u
= va_arg(s
->ap
, unsigned int); break;
216 case l_char
: u
= (unsigned char)va_arg(s
->ap
, unsigned int); break;
217 case l_short
: u
= (unsigned short)va_arg(s
->ap
, unsigned int); break;
218 case l_long
: u
= va_arg(s
->ap
, unsigned long); break;
219 case l_longlong
: u
= va_arg(s
->ap
, u_long_long
); break;
220 case l_intmax_t
: u
= va_arg(s
->ap
, uintmax_t); break;
221 case l_size_t
: u
= va_arg(s
->ap
, size_t); break;
222 case l_ptrdiff_t
: u
= va_arg(s
->ap
, ptrdiff_t); break;
225 base
= c
->specifier
->base
;
230 u
= (uintptr_t)va_arg(s
->ap
, void *);
232 base
= c
->specifier
->base
;
236 /* default precision */
237 if(!(c
->flags
& f_precision
))
240 if((c
->flags
& f_sign
) && !sign
)
242 /* compute the digits */
246 digits
[--dp
] = c
->specifier
->digits
[u
% base
];
249 ndigits
= sizeof digits
- dp
;
250 /* alternative form */
251 if(c
->flags
& f_hash
) {
254 if((dp
== sizeof digits
|| digits
[dp
] != '0')
255 && c
->precision
<= ndigits
)
256 c
->precision
= ndigits
+ 1;
259 if(!iszero
&& c
->specifier
->xform
)
260 xform
= strlen(c
->specifier
->xform
);
265 /* calculate number of 0s to add for precision */
266 if(ndigits
< c
->precision
)
267 prec
= c
->precision
- ndigits
;
270 /* bytes occupied by the sign */
275 /* XXX implement the ' ' flag */
276 /* calculate number of bytes of padding */
277 if(c
->flags
& f_width
) {
278 if((pad
= c
->width
- (ndigits
+ prec
+ xform
+ sign_bytes
)) < 0)
282 /* now we are ready to output. Possibilities are:
283 * [space pad][sign][xform][0 prec]digits
284 * [sign][xform][0 pad][0 prec]digits
285 * [sign][xform][0 prec]digits[space pad]
289 if(c
->flags
& f_left
) {
290 if(sign
&& do_write(s
, &sign
, 1))
292 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
294 if(prec
&& do_pad(s
, '0', prec
) < 0)
296 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
298 if(pad
&& do_pad(s
, ' ', pad
) < 0)
300 } else if(c
->flags
& f_zero
) {
301 if(sign
&& do_write(s
, &sign
, 1))
303 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
305 if(pad
&& do_pad(s
, '0', pad
) < 0)
307 if(prec
&& do_pad(s
, '0', prec
) < 0)
309 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
312 if(pad
&& do_pad(s
, ' ', pad
) < 0)
314 if(sign
&& do_write(s
, &sign
, 1))
316 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
318 if(prec
&& do_pad(s
, '0', prec
) < 0)
320 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
326 static int output_string(struct state
*s
, struct conversion
*c
) {
330 str
= va_arg(s
->ap
, const char *);
331 if(c
->flags
& f_precision
) {
332 if((n
= memchr(str
, 0, c
->precision
)))
338 if(c
->flags
& f_width
) {
339 if((pad
= c
->width
- len
) < 0)
343 if(c
->flags
& f_left
) {
344 if(do_write(s
, str
, len
) < 0)
346 if(pad
&& do_pad(s
, ' ', pad
) < 0)
349 if(pad
&& do_pad(s
, ' ', pad
) < 0)
351 if(do_write(s
, str
, len
) < 0)
358 static int output_char(struct state
*s
, struct conversion
*c
) {
362 ch
= va_arg(s
->ap
, int);
363 if(c
->flags
& f_width
) {
364 if((pad
= c
->width
- 1) < 0)
368 if(c
->flags
& f_left
) {
369 if(do_write(s
, &ch
, 1) < 0)
371 if(pad
&& do_pad(s
, ' ', pad
) < 0)
374 if(pad
&& do_pad(s
, ' ', pad
) < 0)
376 if(do_write(s
, &ch
, 1) < 0)
382 static int output_count(struct state
*s
, struct conversion
*c
) {
384 case 0: *va_arg(s
->ap
, int *) = s
->bytes
; break;
385 case l_char
: *va_arg(s
->ap
, signed char *) = s
->bytes
; break;
386 case l_short
: *va_arg(s
->ap
, short *) = s
->bytes
; break;
387 case l_long
: *va_arg(s
->ap
, long *) = s
->bytes
; break;
388 case l_longlong
: *va_arg(s
->ap
, long_long
*) = s
->bytes
; break;
389 case l_intmax_t
: *va_arg(s
->ap
, intmax_t *) = s
->bytes
; break;
390 case l_size_t
: *va_arg(s
->ap
, ssize_t
*) = s
->bytes
; break;
391 case l_ptrdiff_t
: *va_arg(s
->ap
, ptrdiff_t *) = s
->bytes
; break;
397 /* table of conversion specifiers */
398 static const struct specifier specifiers
[] = {
399 /* XXX don't support floating point conversions */
400 { '%', check_percent
, output_percent
, 0, 0, 0 },
401 { 'X', check_integer
, output_integer
, 16, "0123456789ABCDEF", "0X" },
402 { 'c', check_string
, output_char
, 0, 0, 0 },
403 { 'd', check_integer
, output_integer
, -10, "0123456789", 0 },
404 { 'i', check_integer
, output_integer
, -10, "0123456789", 0 },
405 { 'n', check_integer
, output_count
, 0, 0, 0 },
406 { 'o', check_integer
, output_integer
, 8, "01234567", 0 },
407 { 'p', check_pointer
, output_integer
, 16, "0123456789abcdef", "0x" },
408 { 's', check_string
, output_string
, 0, 0, 0 },
409 { 'u', check_integer
, output_integer
, 10, "0123456789", 0 },
410 { 'x', check_integer
, output_integer
, 16, "0123456789abcdef", "0x" },
413 /* collect and check information about a conversion specification */
414 static int parse_conversion(struct conversion
*c
, const char *ptr
) {
416 const char *q
, *start
= ptr
;
418 memset(c
, 0, sizeof *c
);
420 while(*ptr
&& (q
= strchr(flags
, *ptr
))) {
421 c
->flags
|= (1 << (q
- flags
));
424 /* minimum field width */
425 if(*ptr
>= '0' && *ptr
<= '9') {
426 if((n
= get_integer(&c
->width
, ptr
)) < 0)
430 } else if(*ptr
== '*') {
438 if(*ptr
>= '0' && *ptr
<= '9') {
439 if((n
= get_integer(&c
->precision
, ptr
)) < 0)
442 } else if(*ptr
== '*') {
447 c
->flags
|= f_precision
;
449 /* length modifier */
450 switch(ch
= *ptr
++) {
452 if((ch
= *ptr
++) == 'h') {
460 if((ch
= *ptr
++) == 'l') {
461 c
->length
= l_longlong
;
467 case 'q': c
->length
= l_longlong
; ch
= *ptr
++; break;
468 case 'j': c
->length
= l_intmax_t
; ch
= *ptr
++; break;
469 case 'z': c
->length
= l_size_t
; ch
= *ptr
++; break;
470 case 't': c
->length
= l_ptrdiff_t
; ch
= *ptr
++; break;
471 case 'L': c
->length
= l_longdouble
; ch
= *ptr
++; break;
473 /* conversion specifier */
475 r
= sizeof specifiers
/ sizeof *specifiers
;
476 while(l
<= r
&& (specifiers
[m
= (l
+ r
) / 2].ch
!= ch
))
477 if(ch
< specifiers
[m
].ch
)
481 if(specifiers
[m
].ch
!= ch
)
483 if(specifiers
[m
].check(c
))
485 c
->specifier
= &specifiers
[m
];
489 /* ISO/IEC 9899:1999 7.19.6.1 */
490 /* http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html */
492 int byte_vsinkprintf(struct sink
*output
,
500 memset(&s
, 0, sizeof s
);
504 /* output text up to next conversion specification */
505 for(ptr
= fmt
; *fmt
&& *fmt
!= '%'; ++fmt
)
508 if(do_write(&s
, ptr
, n
) < 0)
513 /* parse conversion */
514 if((n
= parse_conversion(&c
, fmt
)) < 0)
517 /* fill in width and precision */
518 if((c
.flags
& f_width
) && c
.width
== -1)
519 if((c
.width
= va_arg(s
.ap
, int)) < 0) {
523 if((c
.flags
& f_precision
) && c
.precision
== -1)
524 if((c
.precision
= va_arg(s
.ap
, int)) < 0)
525 c
.flags
^= f_precision
;
526 /* generate the output */
527 if(c
.specifier
->output(&s
, &c
) < 0)
537 int byte_sinkprintf(struct sink
*output
, const char *fmt
, ...) {
542 n
= byte_vsinkprintf(output
, fmt
, ap
);