2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007 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 */
69 int (*check
)(const struct conversion
*c
);
70 int (*output
)(struct state
*s
, struct conversion
*c
);
81 const struct specifier
*specifier
;
84 static const char flags
[] = "'-+ #0";
86 /* write @nbytes@ to the output. Return -1 on error, 0 on success.
87 * Keeps track of the number of bytes written. */
88 static int do_write(struct state
*s
,
91 if(s
->bytes
> INT_MAX
- nbytes
) {
97 if(s
->output
->write(s
->output
, buffer
, nbytes
) < 0) return -1;
102 /* write character @ch@ @n@ times, reasonably efficiently */
103 static int do_pad(struct state
*s
, int ch
, unsigned n
) {
108 case ' ': padding
= " "; break;
109 case '0': padding
= "00000000000000000000000000000000"; break;
115 if(do_write(s
, padding
, 32) < 0) return -1;
117 if(do_write(s
, padding
, n
) < 0) return -1;
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
) return -1;
135 /* consistency checks for various conversion specifications */
137 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
) {
164 if(c
->length
) return -1;
168 static int check_percent(const struct conversion
*c
) {
169 if(c
->flags
|| c
->width
|| c
->precision
|| c
->length
) return -1;
173 /* output functions for various conversion specifications */
175 static int output_percent(struct state
*s
,
176 struct conversion
attribute((unused
)) *c
) {
177 return do_write(s
, "%", 1);
180 static int output_integer(struct state
*s
, struct conversion
*c
) {
184 int base
, dp
, iszero
, ndigits
, prec
, xform
, sign_bytes
, pad
;
185 char digits
[CHAR_BIT
* sizeof (uintmax_t)]; /* overestimate */
187 switch(c
->specifier
->ch
) {
189 if(c
->specifier
->base
< 0) {
191 case 0: l
= va_arg(s
->ap
, int); break;
192 case l_char
: l
= (signed char)va_arg(s
->ap
, int); break;
193 case l_short
: l
= (short)va_arg(s
->ap
, int); break;
194 case l_long
: l
= va_arg(s
->ap
, long); break;
195 case l_longlong
: l
= va_arg(s
->ap
, long_long
); break;
196 case l_intmax_t
: l
= va_arg(s
->ap
, intmax_t); break;
197 case l_size_t
: l
= va_arg(s
->ap
, ssize_t
); break;
198 case l_ptrdiff_t
: l
= va_arg(s
->ap
, ptrdiff_t); break;
201 base
= -c
->specifier
->base
;
211 case 0: u
= va_arg(s
->ap
, unsigned int); break;
212 case l_char
: u
= (unsigned char)va_arg(s
->ap
, unsigned int); break;
213 case l_short
: u
= (unsigned short)va_arg(s
->ap
, unsigned int); break;
214 case l_long
: u
= va_arg(s
->ap
, unsigned long); break;
215 case l_longlong
: u
= va_arg(s
->ap
, u_long_long
); break;
216 case l_intmax_t
: u
= va_arg(s
->ap
, uintmax_t); break;
217 case l_size_t
: u
= va_arg(s
->ap
, size_t); break;
218 case l_ptrdiff_t
: u
= va_arg(s
->ap
, ptrdiff_t); break;
221 base
= c
->specifier
->base
;
226 u
= (uintptr_t)va_arg(s
->ap
, void *);
228 base
= c
->specifier
->base
;
232 /* default precision */
233 if(!(c
->flags
& f_precision
))
236 if((c
->flags
& f_sign
) && !sign
) sign
= '+';
237 /* compute the digits */
241 digits
[--dp
] = c
->specifier
->digits
[u
% base
];
244 ndigits
= sizeof digits
- dp
;
245 /* alternative form */
246 if(c
->flags
& f_hash
) {
249 if((dp
== sizeof digits
|| digits
[dp
] != '0')
250 && c
->precision
<= ndigits
)
251 c
->precision
= ndigits
+ 1;
254 if(!iszero
&& c
->specifier
->xform
)
255 xform
= strlen(c
->specifier
->xform
);
260 /* calculate number of 0s to add for precision */
261 if(ndigits
< c
->precision
)
262 prec
= c
->precision
- ndigits
;
265 /* bytes occupied by the sign */
270 /* XXX implement the ' ' flag */
271 /* calculate number of bytes of padding */
272 if(c
->flags
& f_width
) {
273 if((pad
= c
->width
- (ndigits
+ prec
+ xform
+ sign_bytes
)) < 0)
277 /* now we are ready to output. Possibilities are:
278 * [space pad][sign][xform][0 prec]digits
279 * [sign][xform][0 pad][0 prec]digits
280 * [sign][xform][0 prec]digits[space pad]
284 if(c
->flags
& f_left
) {
285 if(pad
&& do_pad(s
, ' ', pad
) < 0) return -1;
286 if(sign
&& do_write(s
, &sign
, 1)) return -1;
287 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
)) return -1;
288 if(prec
&& do_pad(s
, '0', prec
) < 0) return -1;
289 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
)) return -1;
290 } else if(c
->flags
& f_zero
) {
291 if(sign
&& do_write(s
, &sign
, 1)) return -1;
292 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
)) return -1;
293 if(pad
&& do_pad(s
, '0', pad
) < 0) return -1;
294 if(prec
&& do_pad(s
, '0', prec
) < 0) return -1;
295 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
)) return -1;
297 if(sign
&& do_write(s
, &sign
, 1)) return -1;
298 if(xform
&& do_write(s
, c
->specifier
->xform
, xform
)) return -1;
299 if(prec
&& do_pad(s
, '0', prec
) < 0) return -1;
300 if(ndigits
&& do_write(s
, digits
+ dp
, ndigits
)) return -1;
301 if(pad
&& do_pad(s
, ' ', pad
) < 0) return -1;
306 static int output_string(struct state
*s
, struct conversion
*c
) {
310 str
= va_arg(s
->ap
, const char *);
311 if(c
->flags
& f_precision
) {
312 if((n
= memchr(str
, 0, c
->precision
)))
318 if(c
->flags
& f_width
) {
319 if((pad
= c
->width
- len
) < 0)
323 if(c
->flags
& f_left
) {
324 if(pad
&& do_pad(s
, ' ', pad
) < 0) return -1;
325 if(do_write(s
, str
, len
) < 0) return -1;
327 if(do_write(s
, str
, len
) < 0) return -1;
328 if(pad
&& do_pad(s
, ' ', pad
) < 0) return -1;
334 static int output_char(struct state
*s
, struct conversion
*c
) {
338 ch
= va_arg(s
->ap
, int);
339 if(c
->flags
& f_width
) {
340 if((pad
= c
->width
- 1) < 0)
344 if(c
->flags
& f_left
) {
345 if(pad
&& do_pad(s
, ' ', pad
) < 0) return -1;
346 if(do_write(s
, &ch
, 1) < 0) return -1;
348 if(do_write(s
, &ch
, 1) < 0) return -1;
349 if(pad
&& do_pad(s
, ' ', pad
) < 0) return -1;
354 static int output_count(struct state
*s
, struct conversion
*c
) {
356 case 0: *va_arg(s
->ap
, int *) = s
->bytes
; break;
357 case l_char
: *va_arg(s
->ap
, signed char *) = s
->bytes
; break;
358 case l_short
: *va_arg(s
->ap
, short *) = s
->bytes
; break;
359 case l_long
: *va_arg(s
->ap
, long *) = s
->bytes
; break;
360 case l_longlong
: *va_arg(s
->ap
, long_long
*) = s
->bytes
; break;
361 case l_intmax_t
: *va_arg(s
->ap
, intmax_t *) = s
->bytes
; break;
362 case l_size_t
: *va_arg(s
->ap
, ssize_t
*) = s
->bytes
; break;
363 case l_ptrdiff_t
: *va_arg(s
->ap
, ptrdiff_t *) = s
->bytes
; break;
369 /* table of conversion specifiers */
370 static const struct specifier specifiers
[] = {
371 /* XXX don't support floating point conversions */
372 { '%', check_percent
, output_percent
, 0, 0, 0 },
373 { 'X', check_integer
, output_integer
, 16, "0123456789ABCDEF", "0X" },
374 { 'c', check_string
, output_char
, 0, 0, 0 },
375 { 'd', check_integer
, output_integer
, -10, "0123456789", 0 },
376 { 'i', check_integer
, output_integer
, -10, "0123456789", 0 },
377 { 'n', check_integer
, output_count
, 0, 0, 0 },
378 { 'o', check_integer
, output_integer
, 8, "01234567", 0 },
379 { 'p', check_pointer
, output_integer
, 16, "0123456789abcdef", "0x" },
380 { 's', check_string
, output_string
, 0, 0, 0 },
381 { 'u', check_integer
, output_integer
, 10, "0123456789", 0 },
382 { 'x', check_integer
, output_integer
, 16, "0123456789abcdef", "0x" },
385 /* collect and check information about a conversion specification */
386 static int parse_conversion(struct conversion
*c
, const char *ptr
) {
388 const char *q
, *start
= ptr
;
390 memset(c
, 0, sizeof *c
);
392 while(*ptr
&& (q
= strchr(flags
, *ptr
))) {
393 c
->flags
|= (1 << (q
- flags
));
396 /* minimum field width */
397 if(*ptr
>= '0' && *ptr
<= '9') {
398 if((n
= get_integer(&c
->width
, ptr
)) < 0) return -1;
401 } else if(*ptr
== '*') {
409 if(*ptr
>= '0' && *ptr
<= '9') {
410 if((n
= get_integer(&c
->precision
, ptr
)) < 0) return -1;
412 } else if(*ptr
== '*') {
417 c
->flags
|= f_precision
;
419 /* length modifier */
420 switch(ch
= *ptr
++) {
422 if((ch
= *ptr
++) == 'h') { c
->length
= l_char
; ch
= *ptr
++; }
423 else c
->length
= l_short
;
426 if((ch
= *ptr
++) == 'l') { c
->length
= l_longlong
; ch
= *ptr
++; }
427 else c
->length
= l_long
;
429 case 'q': c
->length
= l_longlong
; ch
= *ptr
++; break;
430 case 'j': c
->length
= l_intmax_t
; ch
= *ptr
++; break;
431 case 'z': c
->length
= l_size_t
; ch
= *ptr
++; break;
432 case 't': c
->length
= l_ptrdiff_t
; ch
= *ptr
++; break;
433 case 'L': c
->length
= l_longdouble
; ch
= *ptr
++; break;
435 /* conversion specifier */
437 r
= sizeof specifiers
/ sizeof *specifiers
;
438 while(l
<= r
&& (specifiers
[m
= (l
+ r
) / 2].ch
!= ch
))
439 if(ch
< specifiers
[m
].ch
) r
= m
- 1;
441 if(specifiers
[m
].ch
!= ch
) return -1;
442 if(specifiers
[m
].check(c
)) return -1;
443 c
->specifier
= &specifiers
[m
];
447 /* ISO/IEC 9899:1999 7.19.6.1 */
448 /* http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html */
450 int byte_vsinkprintf(struct sink
*output
,
458 memset(&s
, 0, sizeof s
);
462 /* output text up to next conversion specification */
463 for(ptr
= fmt
; *fmt
&& *fmt
!= '%'; ++fmt
)
466 if(do_write(&s
, ptr
, n
) < 0) goto error
;
470 /* parse conversion */
471 if((n
= parse_conversion(&c
, fmt
)) < 0) goto error
;
473 /* fill in width and precision */
474 if((c
.flags
& f_width
) && c
.width
== -1)
475 if((c
.width
= va_arg(s
.ap
, int)) < 0) {
479 if((c
.flags
& f_precision
) && c
.precision
== -1)
480 if((c
.precision
= va_arg(s
.ap
, int)) < 0)
481 c
.flags
^= f_precision
;
482 /* generate the output */
483 if(c
.specifier
->output(&s
, &c
) < 0) goto error
;