2006-05-14 03:31:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2006-05-14 02:51:55 +00:00
|
|
|
#include <stddef.h>
|
2005-03-08 18:53:11 +00:00
|
|
|
#include <stdarg.h>
|
2006-05-14 02:51:55 +00:00
|
|
|
#include <console.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <vsprintf.h>
|
|
|
|
|
2006-05-14 03:31:20 +00:00
|
|
|
/** @file */
|
|
|
|
|
|
|
|
#define CHAR_LEN 0 /**< "hh" length modifier */
|
|
|
|
#define SHORT_LEN 1 /**< "h" length modifier */
|
|
|
|
#define INT_LEN 2 /**< no length modifier */
|
|
|
|
#define LONG_LEN 3 /**< "l" length modifier */
|
|
|
|
#define LONGLONG_LEN 4 /**< "ll" length modifier */
|
|
|
|
#define SIZE_T_LEN 5 /**< "z" length modifier */
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
static uint8_t type_sizes[] = {
|
|
|
|
[CHAR_LEN] = sizeof ( char ),
|
|
|
|
[SHORT_LEN] = sizeof ( short ),
|
|
|
|
[INT_LEN] = sizeof ( int ),
|
|
|
|
[LONG_LEN] = sizeof ( long ),
|
|
|
|
[LONGLONG_LEN] = sizeof ( long long ),
|
|
|
|
[SIZE_T_LEN] = sizeof ( size_t ),
|
|
|
|
};
|
2005-03-08 18:53:11 +00:00
|
|
|
|
2006-05-14 03:31:20 +00:00
|
|
|
/**
|
|
|
|
* Use lower-case for hexadecimal digits
|
|
|
|
*
|
|
|
|
* Note that this value is set to 0x20 since that makes for very
|
|
|
|
* efficient calculations. (Bitwise-ORing with @c LCASE converts to a
|
|
|
|
* lower-case character, for example.)
|
|
|
|
*/
|
2006-05-14 02:51:55 +00:00
|
|
|
#define LCASE 0x20
|
2006-05-14 03:31:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use "alternate form"
|
|
|
|
*
|
|
|
|
* For hexadecimal numbers, this means to add a "0x" or "0X" prefix to
|
|
|
|
* the number.
|
|
|
|
*/
|
2006-05-14 02:51:55 +00:00
|
|
|
#define ALT_FORM 0x02
|
|
|
|
|
2006-05-14 03:31:20 +00:00
|
|
|
/**
|
|
|
|
* Format a hexadecimal number
|
|
|
|
*
|
|
|
|
* @v end End of buffer to contain number
|
|
|
|
* @v num Number to format
|
|
|
|
* @v width Minimum field width
|
|
|
|
* @ret ptr End of buffer
|
|
|
|
*
|
|
|
|
* Fills a buffer in reverse order with a formatted hexadecimal
|
|
|
|
* number. The number will be zero-padded to the specified width.
|
|
|
|
* Lower-case and "alternate form" (i.e. "0x" prefix) flags may be
|
|
|
|
* set.
|
|
|
|
*
|
|
|
|
* There must be enough space in the buffer to contain the largest
|
|
|
|
* number that this function can format.
|
|
|
|
*/
|
|
|
|
static char * format_hex ( char *end, unsigned long long num, int width,
|
2006-05-14 02:51:55 +00:00
|
|
|
int flags ) {
|
2006-05-14 03:31:20 +00:00
|
|
|
char *ptr = end;
|
2006-05-14 02:51:55 +00:00
|
|
|
int case_mod;
|
|
|
|
|
|
|
|
/* Generate the number */
|
|
|
|
case_mod = flags & LCASE;
|
|
|
|
do {
|
2006-05-14 03:31:20 +00:00
|
|
|
*(--ptr) = "0123456789ABCDEF"[ num & 0xf ] | case_mod;
|
2006-05-14 02:51:55 +00:00
|
|
|
num >>= 4;
|
|
|
|
} while ( num );
|
|
|
|
|
|
|
|
/* Zero-pad to width */
|
2006-05-14 03:31:20 +00:00
|
|
|
while ( ( end - ptr ) < width )
|
|
|
|
*(--ptr) = '0';
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
/* Add "0x" or "0X" if alternate form specified */
|
|
|
|
if ( flags & ALT_FORM ) {
|
2006-05-14 03:31:20 +00:00
|
|
|
*(--ptr) = 'X' | case_mod;
|
|
|
|
*(--ptr) = '0';
|
2006-05-14 02:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2006-05-14 03:31:20 +00:00
|
|
|
/**
|
|
|
|
* Format a decimal number
|
|
|
|
*
|
|
|
|
* @v end End of buffer to contain number
|
|
|
|
* @v num Number to format
|
|
|
|
* @v width Minimum field width
|
|
|
|
* @ret ptr End of buffer
|
|
|
|
*
|
|
|
|
* Fills a buffer in reverse order with a formatted decimal number.
|
|
|
|
* The number will be space-padded to the specified width.
|
|
|
|
*
|
|
|
|
* There must be enough space in the buffer to contain the largest
|
|
|
|
* number that this function can format.
|
|
|
|
*/
|
|
|
|
static char * format_decimal ( char *end, signed long num, int width ) {
|
|
|
|
char *ptr = end;
|
2006-05-14 02:51:55 +00:00
|
|
|
int negative = 0;
|
|
|
|
|
|
|
|
/* Generate the number */
|
|
|
|
if ( num < 0 ) {
|
|
|
|
negative = 1;
|
|
|
|
num = -num;
|
|
|
|
}
|
|
|
|
do {
|
2006-05-14 03:31:20 +00:00
|
|
|
*(--ptr) = '0' + ( num % 10 );
|
2006-05-14 02:51:55 +00:00
|
|
|
num /= 10;
|
|
|
|
} while ( num );
|
|
|
|
|
|
|
|
/* Add "-" if necessary */
|
|
|
|
if ( negative )
|
2006-05-14 03:31:20 +00:00
|
|
|
*(--ptr) = '-';
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
/* Space-pad to width */
|
2006-05-14 03:31:20 +00:00
|
|
|
while ( ( end - ptr ) < width )
|
|
|
|
*(--ptr) = ' ';
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2006-05-22 15:41:01 +00:00
|
|
|
/**
|
|
|
|
* Print character via a printf context
|
|
|
|
*
|
|
|
|
* @v ctx Context
|
|
|
|
* @v c Character
|
|
|
|
*
|
|
|
|
* Call's the printf_context::handler() method and increments
|
|
|
|
* printf_context::len.
|
|
|
|
*/
|
|
|
|
static inline void cputchar ( struct printf_context *ctx, unsigned int c ) {
|
|
|
|
ctx->handler ( ctx, c );
|
|
|
|
++ctx->len;
|
|
|
|
}
|
|
|
|
|
2006-05-14 02:51:55 +00:00
|
|
|
/**
|
|
|
|
* Write a formatted string to a printf context
|
|
|
|
*
|
|
|
|
* @v ctx Context
|
2005-05-19 14:51:37 +00:00
|
|
|
* @v fmt Format string
|
|
|
|
* @v args Arguments corresponding to the format string
|
2006-05-14 02:51:55 +00:00
|
|
|
* @ret len Length of formatted string
|
2005-05-19 14:51:37 +00:00
|
|
|
*/
|
2006-05-14 11:18:42 +00:00
|
|
|
size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
|
2006-05-14 02:51:55 +00:00
|
|
|
int flags;
|
|
|
|
int width;
|
|
|
|
uint8_t *length;
|
|
|
|
char *ptr;
|
2006-05-14 03:31:20 +00:00
|
|
|
char tmp_buf[32]; /* 32 is enough for all numerical formats.
|
|
|
|
* Insane width fields could overflow this buffer. */
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
/* Initialise context */
|
|
|
|
ctx->len = 0;
|
|
|
|
|
|
|
|
for ( ; *fmt ; fmt++ ) {
|
|
|
|
/* Pass through ordinary characters */
|
|
|
|
if ( *fmt != '%' ) {
|
2006-05-22 15:41:01 +00:00
|
|
|
cputchar ( ctx, *fmt );
|
2005-03-08 18:53:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fmt++;
|
2006-05-14 02:51:55 +00:00
|
|
|
/* Process flag characters */
|
|
|
|
flags = 0;
|
|
|
|
for ( ; ; fmt++ ) {
|
|
|
|
if ( *fmt == '#' ) {
|
|
|
|
flags |= ALT_FORM;
|
|
|
|
} else if ( *fmt == '0' ) {
|
|
|
|
/* We always 0-pad hex and space-pad decimal */
|
|
|
|
} else {
|
|
|
|
/* End of flag characters */
|
|
|
|
break;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
2006-05-14 02:51:55 +00:00
|
|
|
}
|
|
|
|
/* Process field width */
|
|
|
|
width = 0;
|
|
|
|
for ( ; ; fmt++ ) {
|
|
|
|
if ( ( ( unsigned ) ( *fmt - '0' ) ) < 10 ) {
|
|
|
|
width = ( width * 10 ) + ( *fmt - '0' );
|
|
|
|
} else {
|
|
|
|
break;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
2006-05-14 02:51:55 +00:00
|
|
|
}
|
|
|
|
/* We don't do floating point */
|
|
|
|
/* Process length modifier */
|
|
|
|
length = &type_sizes[INT_LEN];
|
|
|
|
for ( ; ; fmt++ ) {
|
|
|
|
if ( *fmt == 'h' ) {
|
|
|
|
length--;
|
|
|
|
} else if ( *fmt == 'l' ) {
|
|
|
|
length++;
|
|
|
|
} else if ( *fmt == 'z' ) {
|
|
|
|
length = &type_sizes[SIZE_T_LEN];
|
|
|
|
} else {
|
|
|
|
break;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
2006-05-14 02:51:55 +00:00
|
|
|
}
|
|
|
|
/* Process conversion specifier */
|
2006-05-14 03:31:20 +00:00
|
|
|
ptr = tmp_buf + sizeof ( tmp_buf ) - 1;
|
|
|
|
*ptr = '\0';
|
2006-05-14 02:51:55 +00:00
|
|
|
if ( *fmt == 'c' ) {
|
2006-05-22 15:41:01 +00:00
|
|
|
cputchar ( ctx, va_arg ( args, unsigned int ) );
|
2006-05-14 02:51:55 +00:00
|
|
|
} else if ( *fmt == 's' ) {
|
|
|
|
ptr = va_arg ( args, char * );
|
|
|
|
} else if ( *fmt == 'p' ) {
|
2006-05-14 03:31:20 +00:00
|
|
|
intptr_t ptrval;
|
|
|
|
|
|
|
|
ptrval = ( intptr_t ) va_arg ( args, void * );
|
|
|
|
ptr = format_hex ( ptr, ptrval, width,
|
2006-05-14 02:51:55 +00:00
|
|
|
( ALT_FORM | LCASE ) );
|
|
|
|
} else if ( ( *fmt & ~0x20 ) == 'X' ) {
|
2006-05-14 03:31:20 +00:00
|
|
|
unsigned long long hex;
|
|
|
|
|
2006-05-14 02:51:55 +00:00
|
|
|
flags |= ( *fmt & 0x20 ); /* LCASE */
|
|
|
|
if ( *length >= sizeof ( unsigned long long ) ) {
|
|
|
|
hex = va_arg ( args, unsigned long long );
|
|
|
|
} else if ( *length >= sizeof ( unsigned long ) ) {
|
|
|
|
hex = va_arg ( args, unsigned long );
|
|
|
|
} else {
|
|
|
|
hex = va_arg ( args, unsigned int );
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
2006-05-14 03:31:20 +00:00
|
|
|
ptr = format_hex ( ptr, hex, width, flags );
|
2006-07-25 14:33:20 +00:00
|
|
|
} else if ( ( *fmt == 'd' ) || ( *fmt == 'i' ) ){
|
2006-05-14 03:31:20 +00:00
|
|
|
signed long decimal;
|
|
|
|
|
2006-05-14 02:51:55 +00:00
|
|
|
if ( *length >= sizeof ( signed long ) ) {
|
|
|
|
decimal = va_arg ( args, signed long );
|
|
|
|
} else {
|
|
|
|
decimal = va_arg ( args, signed int );
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
2006-05-14 03:31:20 +00:00
|
|
|
ptr = format_decimal ( ptr, decimal, width );
|
2006-05-14 02:51:55 +00:00
|
|
|
} else {
|
2006-05-14 03:31:20 +00:00
|
|
|
*(--ptr) = *fmt;
|
|
|
|
}
|
|
|
|
/* Write out conversion result */
|
|
|
|
for ( ; *ptr ; ptr++ ) {
|
2006-05-22 15:41:01 +00:00
|
|
|
cputchar ( ctx, *ptr );
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
return ctx->len;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
2006-05-22 15:41:01 +00:00
|
|
|
/** Context used by vsnprintf() and friends */
|
|
|
|
struct sputc_context {
|
|
|
|
struct printf_context ctx;
|
|
|
|
/** Buffer for formatted string (used by printf_sputc()) */
|
|
|
|
char *buf;
|
|
|
|
/** Buffer length (used by printf_sputc()) */
|
|
|
|
size_t max_len;
|
|
|
|
};
|
|
|
|
|
2005-05-19 14:51:37 +00:00
|
|
|
/**
|
2006-05-14 02:51:55 +00:00
|
|
|
* Write character to buffer
|
2005-05-19 14:51:37 +00:00
|
|
|
*
|
2006-05-14 02:51:55 +00:00
|
|
|
* @v ctx Context
|
|
|
|
* @v c Character
|
|
|
|
*/
|
|
|
|
static void printf_sputc ( struct printf_context *ctx, unsigned int c ) {
|
2006-05-22 15:41:01 +00:00
|
|
|
struct sputc_context * sctx =
|
|
|
|
container_of ( ctx, struct sputc_context, ctx );
|
|
|
|
|
|
|
|
if ( ctx->len <= sctx->max_len )
|
|
|
|
sctx->buf[ctx->len] = c;
|
2006-05-14 02:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a formatted string to a buffer
|
2005-05-19 14:51:37 +00:00
|
|
|
*
|
2006-05-14 02:51:55 +00:00
|
|
|
* @v buf Buffer into which to write the string
|
|
|
|
* @v size Size of buffer
|
|
|
|
* @v fmt Format string
|
|
|
|
* @v args Arguments corresponding to the format string
|
|
|
|
* @ret len Length of formatted string
|
2005-05-19 14:51:37 +00:00
|
|
|
*
|
2006-05-14 02:51:55 +00:00
|
|
|
* If the buffer is too small to contain the string, the returned
|
|
|
|
* length is the length that would have been written had enough space
|
|
|
|
* been available.
|
2005-05-19 14:51:37 +00:00
|
|
|
*/
|
2006-05-14 02:51:55 +00:00
|
|
|
int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args ) {
|
2006-05-22 15:41:01 +00:00
|
|
|
struct sputc_context sctx;
|
2006-05-14 11:18:42 +00:00
|
|
|
size_t len;
|
|
|
|
size_t end;
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
/* Hand off to vcprintf */
|
2006-05-22 15:41:01 +00:00
|
|
|
sctx.ctx.handler = printf_sputc;
|
|
|
|
sctx.buf = buf;
|
|
|
|
sctx.max_len = size;
|
|
|
|
len = vcprintf ( &sctx.ctx, fmt, args );
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
/* Add trailing NUL */
|
2006-05-14 11:18:42 +00:00
|
|
|
if ( size ) {
|
|
|
|
end = size - 1;
|
|
|
|
if ( len < end )
|
|
|
|
end = len;
|
|
|
|
buf[end] = '\0';
|
|
|
|
}
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
return len;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|
|
|
|
|
2006-05-14 02:51:55 +00:00
|
|
|
/**
|
|
|
|
* Write a formatted string to a buffer
|
|
|
|
*
|
|
|
|
* @v buf Buffer into which to write the string
|
|
|
|
* @v size Size of buffer
|
|
|
|
* @v fmt Format string
|
|
|
|
* @v ... Arguments corresponding to the format string
|
|
|
|
* @ret len Length of formatted string
|
|
|
|
*/
|
2006-04-30 12:01:31 +00:00
|
|
|
int snprintf ( char *buf, size_t size, const char *fmt, ... ) {
|
|
|
|
va_list args;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
va_start ( args, fmt );
|
2006-05-14 02:51:55 +00:00
|
|
|
i = vsnprintf ( buf, size, fmt, args );
|
2006-04-30 12:01:31 +00:00
|
|
|
va_end ( args );
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2006-05-14 02:51:55 +00:00
|
|
|
/**
|
|
|
|
* Write character to console
|
|
|
|
*
|
|
|
|
* @v ctx Context
|
|
|
|
* @v c Character
|
|
|
|
*/
|
2006-05-22 15:41:01 +00:00
|
|
|
static void printf_putchar ( struct printf_context *ctx __unused,
|
|
|
|
unsigned int c ) {
|
2006-05-14 02:51:55 +00:00
|
|
|
putchar ( c );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a formatted string to the console
|
|
|
|
*
|
|
|
|
* @v fmt Format string
|
|
|
|
* @v args Arguments corresponding to the format string
|
|
|
|
* @ret len Length of formatted string
|
|
|
|
*/
|
|
|
|
int vprintf ( const char *fmt, va_list args ) {
|
|
|
|
struct printf_context ctx;
|
|
|
|
|
|
|
|
/* Hand off to vcprintf */
|
|
|
|
ctx.handler = printf_putchar;
|
|
|
|
return vcprintf ( &ctx, fmt, args );
|
|
|
|
}
|
|
|
|
|
2005-05-19 14:51:37 +00:00
|
|
|
/**
|
|
|
|
* Write a formatted string to the console.
|
|
|
|
*
|
|
|
|
* @v fmt Format string
|
|
|
|
* @v ... Arguments corresponding to the format string
|
2006-05-14 02:51:55 +00:00
|
|
|
* @ret len Length of formatted string
|
2005-05-19 14:51:37 +00:00
|
|
|
*/
|
2006-05-14 02:51:55 +00:00
|
|
|
int printf ( const char *fmt, ... ) {
|
2005-03-08 18:53:11 +00:00
|
|
|
va_list args;
|
|
|
|
int i;
|
2006-05-14 02:51:55 +00:00
|
|
|
|
|
|
|
va_start ( args, fmt );
|
|
|
|
i = vprintf ( fmt, args );
|
|
|
|
va_end ( args );
|
2006-03-16 17:40:55 +00:00
|
|
|
return i;
|
2005-03-08 18:53:11 +00:00
|
|
|
}
|