vsprintf.c File Reference

#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <console.h>
#include <errno.h>
#include <gpxe/vsprintf.h>

Go to the source code of this file.

Data Structures

struct  sputc_context
 Context used by vsnprintf() and friends. More...

Defines

#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
#define LCASE   0x20
 Use lower-case for hexadecimal digits.
#define ALT_FORM   0x02
 Use "alternate form".

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static char * format_hex (char *end, unsigned long long num, int width, int flags)
 Format a hexadecimal number.
static char * format_decimal (char *end, signed long num, int width)
 Format a decimal number.
static void cputchar (struct printf_context *ctx, unsigned int c)
 Print character via a printf context.
size_t vcprintf (struct printf_context *ctx, const char *fmt, va_list args)
 Write a formatted string to a printf context.
static void printf_sputc (struct printf_context *ctx, unsigned int c)
 Write character to buffer.
int vsnprintf (char *buf, size_t size, const char *fmt, va_list args)
 Write a formatted string to a buffer.
int snprintf (char *buf, size_t size, const char *fmt,...)
 Write a formatted string to a buffer.
int vssnprintf (char *buf, ssize_t ssize, const char *fmt, va_list args)
 Version of vsnprintf() that accepts a signed buffer size.
int ssnprintf (char *buf, ssize_t ssize, const char *fmt,...)
 Version of vsnprintf() that accepts a signed buffer size.
static void printf_putchar (struct printf_context *ctx __unused, unsigned int c)
 Write character to console.
int vprintf (const char *fmt, va_list args)
 Write a formatted string to the console.
int printf (const char *fmt,...)
 Write a formatted string to the console.

Variables

static uint8_t type_sizes []


Detailed Description

Definition in file vsprintf.c.


Define Documentation

#define CHAR_LEN   0

"hh" length modifier

Definition at line 30 of file vsprintf.c.

#define SHORT_LEN   1

"h" length modifier

Definition at line 31 of file vsprintf.c.

#define INT_LEN   2

no length modifier

Definition at line 32 of file vsprintf.c.

Referenced by vcprintf().

#define LONG_LEN   3

"l" length modifier

Definition at line 33 of file vsprintf.c.

#define LONGLONG_LEN   4

"ll" length modifier

Definition at line 34 of file vsprintf.c.

#define SIZE_T_LEN   5

"z" length modifier

Definition at line 35 of file vsprintf.c.

Referenced by vcprintf().

#define LCASE   0x20

Use lower-case for hexadecimal digits.

Note that this value is set to 0x20 since that makes for very efficient calculations. (Bitwise-ORing with LCASE converts to a lower-case character, for example.)

Definition at line 53 of file vsprintf.c.

Referenced by format_hex(), and vcprintf().

#define ALT_FORM   0x02

Use "alternate form".

For hexadecimal numbers, this means to add a "0x" or "0X" prefix to the number.

Definition at line 61 of file vsprintf.c.

Referenced by format_hex(), and vcprintf().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static char* format_hex ( char *  end,
unsigned long long  num,
int  width,
int  flags 
) [static]

Format a hexadecimal number.

Parameters:
end End of buffer to contain number
num Number to format
width Minimum field width
Return values:
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.

Definition at line 79 of file vsprintf.c.

References ALT_FORM, and LCASE.

Referenced by vcprintf().

00080                                        {
00081         char *ptr = end;
00082         int case_mod;
00083 
00084         /* Generate the number */
00085         case_mod = flags & LCASE;
00086         do {
00087                 *(--ptr) = "0123456789ABCDEF"[ num & 0xf ] | case_mod;
00088                 num >>= 4;
00089         } while ( num );
00090 
00091         /* Zero-pad to width */
00092         while ( ( end - ptr ) < width )
00093                 *(--ptr) = '0';
00094 
00095         /* Add "0x" or "0X" if alternate form specified */
00096         if ( flags & ALT_FORM ) {
00097                 *(--ptr) = 'X' | case_mod;
00098                 *(--ptr) = '0';
00099         }
00100 
00101         return ptr;
00102 }

static char* format_decimal ( char *  end,
signed long  num,
int  width 
) [static]

Format a decimal number.

Parameters:
end End of buffer to contain number
num Number to format
width Minimum field width
Return values:
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.

Definition at line 118 of file vsprintf.c.

Referenced by vcprintf().

00118                                                                        {
00119         char *ptr = end;
00120         int negative = 0;
00121 
00122         /* Generate the number */
00123         if ( num < 0 ) {
00124                 negative = 1;
00125                 num = -num;
00126         }
00127         do {
00128                 *(--ptr) = '0' + ( num % 10 );
00129                 num /= 10;
00130         } while ( num );
00131 
00132         /* Add "-" if necessary */
00133         if ( negative )
00134                 *(--ptr) = '-';
00135 
00136         /* Space-pad to width */
00137         while ( ( end - ptr ) < width )
00138                 *(--ptr) = ' ';
00139 
00140         return ptr;
00141 }

static void cputchar ( struct printf_context ctx,
unsigned int  c 
) [inline, static]

Print character via a printf context.

Parameters:
ctx Context
c Character
Call's the printf_context::handler() method and increments printf_context::len.

Definition at line 152 of file vsprintf.c.

References printf_context::handler, and printf_context::len.

Referenced by vcprintf().

00152                                                                            {
00153         ctx->handler ( ctx, c );
00154         ++ctx->len;
00155 }

size_t vcprintf ( struct printf_context ctx,
const char *  fmt,
va_list  args 
)

Write a formatted string to a printf context.

Parameters:
ctx Context
fmt Format string
args Arguments corresponding to the format string
Return values:
len Length of formatted string

Definition at line 165 of file vsprintf.c.

References ALT_FORM, cputchar(), format_decimal(), format_hex(), INT_LEN, LCASE, printf_context::len, SIZE_T_LEN, type_sizes, and va_arg.

Referenced by vprintf(), vsnprintf(), and vw_printw().

00165                                                                               {
00166         int flags;
00167         int width;
00168         uint8_t *length;
00169         char *ptr;
00170         char tmp_buf[32]; /* 32 is enough for all numerical formats.
00171                            * Insane width fields could overflow this buffer. */
00172 
00173         /* Initialise context */
00174         ctx->len = 0;
00175 
00176         for ( ; *fmt ; fmt++ ) {
00177                 /* Pass through ordinary characters */
00178                 if ( *fmt != '%' ) {
00179                         cputchar ( ctx, *fmt );
00180                         continue;
00181                 }
00182                 fmt++;
00183                 /* Process flag characters */
00184                 flags = 0;
00185                 for ( ; ; fmt++ ) {
00186                         if ( *fmt == '#' ) {
00187                                 flags |= ALT_FORM;
00188                         } else if ( *fmt == '0' ) {
00189                                 /* We always 0-pad hex and space-pad decimal */
00190                         } else {
00191                                 /* End of flag characters */
00192                                 break;
00193                         }
00194                 }
00195                 /* Process field width */
00196                 width = 0;
00197                 for ( ; ; fmt++ ) {
00198                         if ( ( ( unsigned ) ( *fmt - '0' ) ) < 10 ) {
00199                                 width = ( width * 10 ) + ( *fmt - '0' );
00200                         } else {
00201                                 break;
00202                         }
00203                 }
00204                 /* We don't do floating point */
00205                 /* Process length modifier */
00206                 length = &type_sizes[INT_LEN];
00207                 for ( ; ; fmt++ ) {
00208                         if ( *fmt == 'h' ) {
00209                                 length--;
00210                         } else if ( *fmt == 'l' ) {
00211                                 length++;
00212                         } else if ( *fmt == 'z' ) {
00213                                 length = &type_sizes[SIZE_T_LEN];
00214                         } else {
00215                                 break;
00216                         }
00217                 }
00218                 /* Process conversion specifier */
00219                 ptr = tmp_buf + sizeof ( tmp_buf ) - 1;
00220                 *ptr = '\0';
00221                 if ( *fmt == 'c' ) {
00222                         cputchar ( ctx, va_arg ( args, unsigned int ) );
00223                 } else if ( *fmt == 's' ) {
00224                         ptr = va_arg ( args, char * );
00225                         if ( ! ptr )
00226                                 ptr = "<NULL>";
00227                 } else if ( *fmt == 'p' ) {
00228                         intptr_t ptrval;
00229 
00230                         ptrval = ( intptr_t ) va_arg ( args, void * );
00231                         ptr = format_hex ( ptr, ptrval, width, 
00232                                            ( ALT_FORM | LCASE ) );
00233                 } else if ( ( *fmt & ~0x20 ) == 'X' ) {
00234                         unsigned long long hex;
00235 
00236                         flags |= ( *fmt & 0x20 ); /* LCASE */
00237                         if ( *length >= sizeof ( unsigned long long ) ) {
00238                                 hex = va_arg ( args, unsigned long long );
00239                         } else if ( *length >= sizeof ( unsigned long ) ) {
00240                                 hex = va_arg ( args, unsigned long );
00241                         } else {
00242                                 hex = va_arg ( args, unsigned int );
00243                         }
00244                         ptr = format_hex ( ptr, hex, width, flags );
00245                 } else if ( ( *fmt == 'd' ) || ( *fmt == 'i' ) ){
00246                         signed long decimal;
00247 
00248                         if ( *length >= sizeof ( signed long ) ) {
00249                                 decimal = va_arg ( args, signed long );
00250                         } else {
00251                                 decimal = va_arg ( args, signed int );
00252                         }
00253                         ptr = format_decimal ( ptr, decimal, width );
00254                 } else {
00255                         *(--ptr) = *fmt;
00256                 }
00257                 /* Write out conversion result */
00258                 for ( ; *ptr ; ptr++ ) {
00259                         cputchar ( ctx, *ptr );
00260                 }
00261         }
00262 
00263         return ctx->len;
00264 }

static void printf_sputc ( struct printf_context ctx,
unsigned int  c 
) [static]

Write character to buffer.

Parameters:
ctx Context
c Character

Definition at line 281 of file vsprintf.c.

References sputc_context::buf, container_of, printf_context::len, and sputc_context::max_len.

Referenced by vsnprintf().

00281                                                                         {
00282         struct sputc_context * sctx =
00283                 container_of ( ctx, struct sputc_context, ctx );
00284 
00285         if ( ctx->len < sctx->max_len )
00286                 sctx->buf[ctx->len] = c;
00287 }

int vsnprintf ( char *  buf,
size_t  size,
const char *  fmt,
va_list  args 
)

Write a formatted string to a buffer.

Parameters:
buf Buffer into which to write the string
size Size of buffer
fmt Format string
args Arguments corresponding to the format string
Return values:
len Length of formatted string
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.

Definition at line 302 of file vsprintf.c.

References sputc_context::buf, sputc_context::ctx, printf_context::handler, sputc_context::max_len, printf_sputc(), and vcprintf().

Referenced by snprintf(), vasprintf(), vmsg(), vsprintf(), vssnprintf(), and xfer_vprintf().

00302                                                                         {
00303         struct sputc_context sctx;
00304         size_t len;
00305         size_t end;
00306 
00307         /* Hand off to vcprintf */
00308         sctx.ctx.handler = printf_sputc;
00309         sctx.buf = buf;
00310         sctx.max_len = size;
00311         len = vcprintf ( &sctx.ctx, fmt, args );
00312 
00313         /* Add trailing NUL */
00314         if ( size ) {
00315                 end = size - 1;
00316                 if ( len < end )
00317                         end = len;
00318                 buf[end] = '\0';
00319         }
00320 
00321         return len;
00322 }

int snprintf ( char *  buf,
size_t  size,
const char *  fmt,
  ... 
)

Write a formatted string to a buffer.

Parameters:
buf Buffer into which to write the string
size Size of buffer
fmt Format string
... Arguments corresponding to the format string
Return values:
len Length of formatted string

Definition at line 333 of file vsprintf.c.

References va_end, va_start, and vsnprintf().

Referenced by __vxge_hw_vpath_fw_ver_get(), boot_next_server_and_filename(), cpio_set_field(), dhcp_tag_name(), efi_strerror(), eisabus_probe(), fetchf_int(), fetchf_ipv4(), fetchf_uint(), fetchf_uuid(), http_step(), ib_sma_node_desc(), ipoib_ntoa(), isabus_probe(), isapnpbus_probe(), iwlist(), mcabus_probe(), multiboot_add_cmdline(), pcibus_probe(), pxe_menu_draw_item(), pxe_tftp_open(), pxenv_undi_get_iface_info(), register_image(), register_netdev(), settings_name(), skge_board_name(), strerror(), t509bus_probe(), tftp_apply_settings(), tftp_send_rrq(), undipci_probe(), and uri_decode().

00333                                                               {
00334         va_list args;
00335         int i;
00336 
00337         va_start ( args, fmt );
00338         i = vsnprintf ( buf, size, fmt, args );
00339         va_end ( args );
00340         return i;
00341 }

int vssnprintf ( char *  buf,
ssize_t  ssize,
const char *  fmt,
va_list  args 
)

Version of vsnprintf() that accepts a signed buffer size.

Parameters:
buf Buffer into which to write the string
size Size of buffer
fmt Format string
args Arguments corresponding to the format string
Return values:
len Length of formatted string

Definition at line 352 of file vsprintf.c.

References vsnprintf().

Referenced by ssnprintf().

00352                                                                            {
00353 
00354         /* Treat negative buffer size as zero buffer size */
00355         if ( ssize < 0 )
00356                 ssize = 0;
00357 
00358         /* Hand off to vsnprintf */
00359         return vsnprintf ( buf, ssize, fmt, args );
00360 }

int ssnprintf ( char *  buf,
ssize_t  ssize,
const char *  fmt,
  ... 
)

Version of vsnprintf() that accepts a signed buffer size.

Parameters:
buf Buffer into which to write the string
size Size of buffer
fmt Format string
... Arguments corresponding to the format string
Return values:
len Length of formatted string

Definition at line 371 of file vsprintf.c.

References va_end, va_start, and vssnprintf().

Referenced by fetchf_hex(), iscsi_build_login_request_strings(), unparse_uri(), and uri_encode().

00371                                                                  {
00372         va_list args;
00373         int len;
00374 
00375         /* Hand off to vssnprintf */
00376         va_start ( args, fmt );
00377         len = vssnprintf ( buf, ssize, fmt, args );
00378         va_end ( args );
00379         return len;
00380 }

static void printf_putchar ( struct printf_context *ctx  __unused,
unsigned int  c 
) [static]

Write character to console.

Parameters:
ctx Context
c Character

Definition at line 388 of file vsprintf.c.

References putchar().

Referenced by vprintf().

00389                                               {
00390         putchar ( c );
00391 }

int vprintf ( const char *  fmt,
va_list  args 
)

Write a formatted string to the console.

Parameters:
fmt Format string
args Arguments corresponding to the format string
Return values:
len Length of formatted string

Definition at line 400 of file vsprintf.c.

References printf_context::handler, printf_putchar(), and vcprintf().

Referenced by printf().

00400                                               {
00401         struct printf_context ctx;
00402 
00403         /* Hand off to vcprintf */
00404         ctx.handler = printf_putchar;   
00405         return vcprintf ( &ctx, fmt, args );    
00406 }

int printf ( const char *  fmt,
  ... 
)

Write a formatted string to the console.

Parameters:
fmt Format string
... Arguments corresponding to the format string
Return values:
len Length of formatted string

Definition at line 415 of file vsprintf.c.

References va_end, va_start, and vprintf().

Referenced by __attribute__(), _dump_regs(), alloc(), amd79c901_read_mode(), amd8111e_poll_link(), amd8111e_probe_ext_phy(), amd8111e_restart(), amd8111e_transmit(), amd8111e_wait_tx_ring(), ansiscr_movetoyx(), ansiscr_putc(), ansiscr_reset(), aoeboot(), aout_download(), aout_probe(), autoboot(), autoboot_exec(), beep(), bi_depermanent(), bi_free(), bi_permanent(), bi_terminate(), bnx2_fw_sync(), bnx2_init_board(), bnx2_init_nvram(), bnx2_probe(), bnx2_report_link(), bnx2_reset_chip(), bnx2_transmit(), ce_loader(), check_duplex(), check_region(), clear_exec(), clrline(), config_exec(), corkscrew_probe1(), cs89x0_probe(), cs89x0_transmit(), davicom_reset(), davicom_transmit(), dbg_autocolourise(), dbg_decolourise(), dbg_hex_dump_da_row(), detect_aui(), detect_bnc(), detect_tp(), dhcp(), dhcp_exec(), dhcp_syntax(), digest_exec(), digest_syntax(), dm9132_id_table(), dmfe_poll(), dmfe_probe(), echo_exec(), eepro_poll(), eepro_probe(), eepro_transmit(), eeprom_rdy(), enable_multicast(), epic100_open(), epic100_poll(), epic100_probe(), epic100_transmit(), eth_pio_write(), eth_probe(), execv(), exit_exec(), fnrec_dump(), forcedeth_probe(), forcedeth_reset(), gateA20_set(), gdbstub_exec(), gdbstub_syntax(), gdbudp_init(), get_eeprom_data(), get_x_header(), getopt_long(), help_exec(), hfa384x_copy_from_bap(), hfa384x_copy_to_bap(), hfa384x_docmd_wait(), hfa384x_drvr_getconfig(), hfa384x_drvr_setconfig(), hfa384x_wait_for_event(), ib_srpboot(), ics1893_read_mode(), ifcommon_do_list(), ifcommon_syntax(), iflinkwait(), ifopen(), ifstat(), ifstat_errors(), imgargs_exec(), imgargs_syntax(), imgexec_exec(), imgexec_syntax(), imgfetch_core_exec(), imgfetch_core_syntax(), imgfree_exec(), imgfree_syntax(), imgload_exec(), imgload_syntax(), imgstat(), imgstat_syntax(), init_media(), iscsiboot(), iwlist(), iwstat(), keep_san(), legacy_probe(), linebuf_test(), login_exec(), main(), match_long_option(), match_short_option(), monojob_wait(), more(), mtd_poll(), mtd_probe(), mtd_transmit(), netboot(), ns83820_probe(), ns83820_run_bist(), ns8390_poll(), nway_start(), parse_eeprom(), pause(), pci_resource_flags(), pcnet32_probe(), pcnet32_transmit(), phy_init(), phy_intr(), pnic_api_check(), pnic_command(), pnic_command_quiet(), pnic_do_nway(), pnic_probe(), prism2_find_plx(), prism2_pci_probe(), prism2_probe(), prism2_transmit(), pxe_menu_prompt_and_select(), pxe_menu_select(), pxebs(), pxebs_exec(), pxebs_syntax(), readline(), reg_delay(), rhine_disable(), rhine_poll(), rhine_probe1(), route(), route_syntax(), rtl8201_read_mode(), sanboot_exec(), sanboot_syntax(), select_media(), send_test_pkt(), set_exec(), set_rx_mode(), shell_banner(), show_exec(), sis900_get_mac_addr(), sis900_init_rxd(), sis900_init_rxfilter(), sis900_init_txd(), sis900_poll(), sis900_probe(), sis900_read_mode(), sis900_transmit(), sis96x_get_mac_addr(), sleep_exec(), smc9000_poll(), smc9000_probe(), smc9000_transmit(), smc_detect_phy(), smc_read_phy_register(), smc_write_phy_register(), start_link(), sundance_poll(), sundance_probe(), sundance_transmit(), t509_poll(), t509_transmit(), t515_poll(), t515_reset(), t529_probe(), t595_poll(), t595_transmit(), t5x9_probe(), test_parse_unparse(), test_resolve(), tg3_abort_hw(), tg3_get_invariants(), tg3_link_report(), tg3_probe(), tg3_restart_fw(), tg3_setup_hw(), tg3_stop_block(), tg3_transmit(), time_exec(), TLan_PhyDetect(), TLan_PhyFinishAutoNeg(), tlan_probe(), tlan_transmit(), tulip_check_duplex(), tulip_probe(), tulip_reset(), tulip_transmit(), umalloc_test(), update_linkspeed(), uri_test(), velocity_get_pci_info(), velocity_init_info(), velocity_init_registers(), velocity_open(), velocity_print_link_status(), velocity_probe(), velocity_set_bool_opt(), velocity_set_int_opt(), velocity_set_media_mode(), velocity_transmit(), virtnet_probe(), vp_find_vq(), vt6103_read_mode(), vxge_probe(), vxgetlink(), vxsetlink(), w89c840_poll(), w89c840_probe(), w89c840_reset(), w89c840_transmit(), whereami(), and wince_probe().

00415                                     {
00416         va_list args;
00417         int i;
00418 
00419         va_start ( args, fmt );
00420         i = vprintf ( fmt, args );
00421         va_end ( args );
00422         return i;
00423 }


Variable Documentation

uint8_t type_sizes[] [static]

Initial value:

 {
        [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 ),
}

Definition at line 37 of file vsprintf.c.

Referenced by vcprintf().


Generated on Tue Apr 6 20:01:15 2010 for gPXE by  doxygen 1.5.7.1