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/>.
19 #define NO_MEMORY_ALLOCATION
20 /* because byte_snprintf used from log.c */
64 int (*check
)(const struct conversion
*c
);
65 int (*output
)(struct state
*s
, struct conversion
*c
);
76 const struct specifier
*specifier
;
79 static const char flags
[] = "'-+ #0";
81 /* write @nbytes@ to the output. Return -1 on error, 0 on success.
82 * Keeps track of the number of bytes written. */
83 static int do_write(struct state
*s
,
86 if(s
->bytes
> INT_MAX
- nbytes
) {
92 if(s
->output
->write(s
->output
, buffer
, nbytes
) < 0)
98 /* write character @ch@ @n@ times, reasonably efficiently */
99 static int do_pad(struct state
*s
, int ch
, unsigned n
) {
104 case ' ': padding
= " "; break;
105 case '0': padding
= "00000000000000000000000000000000"; break;
111 if(do_write(s
, padding
, 32) < 0)
114 if(do_write(s
, padding
, n
) < 0)
119 /* pick up the integer at @ptr@, returning it via @intp@. Return the
120 * number of characters consumed. Return 0 if there is no integer
121 * there and -1 if an error occurred (e.g. too big) */
122 static int get_integer(int *intp
, const char *ptr
) {
127 n
= strtol(ptr
, &e
, 10);
128 if(errno
|| n
> INT_MAX
|| n
< INT_MIN
|| e
== ptr
)
134 /* consistency checks for various conversion specifications */
136 static int check_integer(const struct conversion
*c
) {
153 static int check_string(const struct conversion
*c
) {
156 /* XXX don't support %ls, %lc */
163 static int check_pointer(const struct conversion
*c
) {
169 static int check_percent(const struct conversion
*c
) {
170 if(c
->flags
|| c
->width
|| c
->precision
|| c
->length
)
175 /* output functions for various conversion specifications */
177 static int output_percent(struct state
*s
,
178 struct conversion
attribute((unused
)) *c
) {
179 return do_write(s
, "%", 1);
182 static int output_integer(struct state
*s
, struct conversion
*c
) {
186 int base
, dp
, iszero
, ndigits
, prec
, xform
, sign_bytes
, pad
;
187 char digits
[CHAR_BIT
* sizeof (uintmax_t)]; /* overestimate */
189 switch(c
->specifier
->ch
) {
191 if(c
->specifier
->base
< 0) {
193 case 0: l
= va_arg(s
->ap
, int); break;
194 case l_char
: l
= (signed char)va_arg(s
->ap
, int); break;
195 case l_short
: l
= (short)va_arg(s
->ap
, int); break;
196 case l_long
: l
= va_arg(s
->ap
, long); break;
197 case l_longlong
: l
= va_arg(s
->ap
, long_long
); break;
198 case l_intmax_t
: l
= va_arg(s
->ap
, intmax_t); break;
199 case l_size_t
: l
= va_arg(s
->ap
, ssize_t
); break;
200 case l_ptrdiff_t
: l
= va_arg(s
->ap
, ptrdiff_t); break;
203 base
= -c
->specifier
->base
;
213 case 0: u
= va_arg(s
->ap
, unsigned int); break;
214 case l_char
: u
= (unsigned char)va_arg(s
->ap
, unsigned int); break;
215 case l_short
: u
= (unsigned short)va_arg(s
->ap
, unsigned int); break;
216 case l_long
: u
= va_arg(s
->ap
, unsigned long); break;
217 case l_longlong
: u
= va_arg(s
->ap
, u_long_long
); break;
218 case l_intmax_t
: u
= va_arg(s
->ap
, uintmax_t); break;
219 case l_size_t
: u
= va_arg(s
->ap
, size_t); break;
220 case l_ptrdiff_t
: u
= va_arg(s
->ap
, ptrdiff_t); break;
223 base
= c
->specifier
->base
;
228 u
= (uintptr_t)va_arg(s
->ap
, void *);
230 base
= c
->specifier
->base
;
234 /* default precision */
235 if(!(c
->flags
& f_precision
))
238 if((c
->flags
& f_sign
) && !sign
)
240 /* compute the digits */
244 digits
[--dp
] = c
->specifier
->digits
[u
% base
];
247 ndigits
= sizeof digits
- dp
;
248 /* alternative form */
249 if(c
->flags
& f_hash
) {
252 if((dp
== sizeof digits
|| digits
[dp
] != '0')
253 && c
->precision
<= ndigits
)
254 c
->precision
= ndigits
+ 1;
257 if(!iszero
&& c
->specifier
->xform
)
258 xform
= strlen(c
->specifier
->xform
);
263 /* calculate number of 0s to add for precision */
264 if(ndigits
< c
->precision
)
265 prec
= c
->precision
- ndigits
;
268 /* bytes occupied by the sign */
273 /* XXX implement the ' ' flag */
274 /* calculate number of bytes of padding */
275 if(c
->flags
& f_width
) {
276 if((pad
= c
->width
- (ndigits
+ prec
+ xform
+ sign_bytes
)) < 0)
280 /* now we are ready to output. Possibilities are:
281 * [space pad][sign][xform][0 prec]digits
282 * [sign][xform][0 pad][0 prec]digits
283 * [sign][xform][0 prec]digits[space pad]
287 if(c
->flags
& f_left
) {
288 if(sign
&& do_write(s
, &sign
, 1))
290 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
292 if(prec
&& do_pad(s
, '0', prec
) < 0)
294 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
296 if(pad
&& do_pad(s
, ' ', pad
) < 0)
298 } else if(c
->flags
& f_zero
) {
299 if(sign
&& do_write(s
, &sign
, 1))
301 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
303 if(pad
&& do_pad(s
, '0', pad
) < 0)
305 if(prec
&& do_pad(s
, '0', prec
) < 0)
307 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
310 if(pad
&& do_pad(s
, ' ', pad
) < 0)
312 if(sign
&& do_write(s
, &sign
, 1))
314 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
))
316 if(prec
&& do_pad(s
, '0', prec
) < 0)
318 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
))
324 static int output_string(struct state
*s
, struct conversion
*c
) {
328 str
= va_arg(s
->ap
, const char *);
329 if(c
->flags
& f_precision
) {
330 if((n
= memchr(str
, 0, c
->precision
)))
336 if(c
->flags
& f_width
) {
337 if((pad
= c
->width
- len
) < 0)
341 if(c
->flags
& f_left
) {
342 if(do_write(s
, str
, len
) < 0)
344 if(pad
&& do_pad(s
, ' ', pad
) < 0)
347 if(pad
&& do_pad(s
, ' ', pad
) < 0)
349 if(do_write(s
, str
, len
) < 0)
356 static int output_char(struct state
*s
, struct conversion
*c
) {
360 ch
= va_arg(s
->ap
, int);
361 if(c
->flags
& f_width
) {
362 if((pad
= c
->width
- 1) < 0)
366 if(c
->flags
& f_left
) {
367 if(do_write(s
, &ch
, 1) < 0)
369 if(pad
&& do_pad(s
, ' ', pad
) < 0)
372 if(pad
&& do_pad(s
, ' ', pad
) < 0)
374 if(do_write(s
, &ch
, 1) < 0)
380 static int output_count(struct state
*s
, struct conversion
*c
) {
382 case 0: *va_arg(s
->ap
, int *) = s
->bytes
; break;
383 case l_char
: *va_arg(s
->ap
, signed char *) = s
->bytes
; break;
384 case l_short
: *va_arg(s
->ap
, short *) = s
->bytes
; break;
385 case l_long
: *va_arg(s
->ap
, long *) = s
->bytes
; break;
386 case l_longlong
: *va_arg(s
->ap
, long_long
*) = s
->bytes
; break;
387 case l_intmax_t
: *va_arg(s
->ap
, intmax_t *) = s
->bytes
; break;
388 case l_size_t
: *va_arg(s
->ap
, ssize_t
*) = s
->bytes
; break;
389 case l_ptrdiff_t
: *va_arg(s
->ap
, ptrdiff_t *) = s
->bytes
; break;
395 /* table of conversion specifiers */
396 static const struct specifier specifiers
[] = {
397 /* XXX don't support floating point conversions */
398 { '%', check_percent
, output_percent
, 0, 0, 0 },
399 { 'X', check_integer
, output_integer
, 16, "0123456789ABCDEF", "0X" },
400 { 'c', check_string
, output_char
, 0, 0, 0 },
401 { 'd', check_integer
, output_integer
, -10, "0123456789", 0 },
402 { 'i', check_integer
, output_integer
, -10, "0123456789", 0 },
403 { 'n', check_integer
, output_count
, 0, 0, 0 },
404 { 'o', check_integer
, output_integer
, 8, "01234567", 0 },
405 { 'p', check_pointer
, output_integer
, 16, "0123456789abcdef", "0x" },
406 { 's', check_string
, output_string
, 0, 0, 0 },
407 { 'u', check_integer
, output_integer
, 10, "0123456789", 0 },
408 { 'x', check_integer
, output_integer
, 16, "0123456789abcdef", "0x" },
411 /* collect and check information about a conversion specification */
412 static int parse_conversion(struct conversion
*c
, const char *ptr
) {
414 const char *q
, *start
= ptr
;
416 memset(c
, 0, sizeof *c
);
418 while(*ptr
&& (q
= strchr(flags
, *ptr
))) {
419 c
->flags
|= (1 << (q
- flags
));
422 /* minimum field width */
423 if(*ptr
>= '0' && *ptr
<= '9') {
424 if((n
= get_integer(&c
->width
, ptr
)) < 0)
428 } else if(*ptr
== '*') {
436 if(*ptr
>= '0' && *ptr
<= '9') {
437 if((n
= get_integer(&c
->precision
, ptr
)) < 0)
440 } else if(*ptr
== '*') {
445 c
->flags
|= f_precision
;
447 /* length modifier */
448 switch(ch
= *ptr
++) {
450 if((ch
= *ptr
++) == 'h') {
458 if((ch
= *ptr
++) == 'l') {
459 c
->length
= l_longlong
;
465 case 'q': c
->length
= l_longlong
; ch
= *ptr
++; break;
466 case 'j': c
->length
= l_intmax_t
; ch
= *ptr
++; break;
467 case 'z': c
->length
= l_size_t
; ch
= *ptr
++; break;
468 case 't': c
->length
= l_ptrdiff_t
; ch
= *ptr
++; break;
469 case 'L': c
->length
= l_longdouble
; ch
= *ptr
++; break;
471 /* conversion specifier */
473 r
= sizeof specifiers
/ sizeof *specifiers
;
474 while(l
<= r
&& (specifiers
[m
= (l
+ r
) / 2].ch
!= ch
))
475 if(ch
< specifiers
[m
].ch
)
479 if(specifiers
[m
].ch
!= ch
)
481 if(specifiers
[m
].check(c
))
483 c
->specifier
= &specifiers
[m
];
487 /* ISO/IEC 9899:1999 7.19.6.1 */
488 /* http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html */
490 int byte_vsinkprintf(struct sink
*output
,
498 memset(&s
, 0, sizeof s
);
502 /* output text up to next conversion specification */
503 for(ptr
= fmt
; *fmt
&& *fmt
!= '%'; ++fmt
)
506 if(do_write(&s
, ptr
, n
) < 0)
511 /* parse conversion */
512 if((n
= parse_conversion(&c
, fmt
)) < 0)
515 /* fill in width and precision */
516 if((c
.flags
& f_width
) && c
.width
== -1)
517 if((c
.width
= va_arg(s
.ap
, int)) < 0) {
521 if((c
.flags
& f_precision
) && c
.precision
== -1)
522 if((c
.precision
= va_arg(s
.ap
, int)) < 0)
523 c
.flags
^= f_precision
;
524 /* generate the output */
525 if(c
.specifier
->output(&s
, &c
) < 0)
535 int byte_sinkprintf(struct sink
*output
, const char *fmt
, ...) {
540 n
= byte_vsinkprintf(output
, fmt
, ap
);