1 /* 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 2012-2014 ARM Ltd 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. The name of the company may not be used to endorse or promote 46 * products derived from this software without specific prior written 47 * permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED 50 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 51 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 54 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 #ifndef VFPRINTF_LOCAL 62 #define VFPRINTF_LOCAL 63 64 #ifndef NO_FLOATING_POINT 65 # define FLOATING_POINT 66 #endif 67 68 #define _NO_POS_ARGS 69 #undef _WANT_IO_C99_FORMATS 70 71 /* Currently a test is made to see if long double processing is warranted. 72 This could be changed in the future should the _ldtoa_r code be 73 preferred over _dtoa_r. */ 74 #define _NO_LONGDBL 75 76 #define _NO_LONGLONG 77 78 #define _PRINTF_FLOAT_TYPE double 79 80 #if defined (FLOATING_POINT) 81 # include <locale.h> 82 #endif 83 #ifdef FLOATING_POINT 84 # include <math.h> 85 86 /* For %La, an exponent of 15 bits occupies the exponent character, 87 a sign, and up to 5 digits. */ 88 # define MAXEXPLEN 7 89 # define DEFPREC 6 90 91 extern char *_dtoa_r _PARAMS((struct _reent *, double, int, 92 int, int *, int *, char **)); 93 94 # define _DTOA_R _dtoa_r 95 # define FREXP frexp 96 97 #endif /* FLOATING_POINT. */ 98 99 /* BUF must be big enough for the maximum %#llo (assuming long long is 100 at most 64 bits, this would be 23 characters), the maximum 101 multibyte character %C, and the maximum default precision of %La 102 (assuming long double is at most 128 bits with 113 bits of 103 mantissa, this would be 29 characters). %e, %f, and %g use 104 reentrant storage shared with mprec. All other formats that use 105 buf get by with fewer characters. Making BUF slightly bigger 106 reduces the need for malloc in %.*a and %S, when large precision or 107 long strings are processed. 108 The bigger size of 100 bytes is used on systems which allow number 109 strings using the locale's grouping character. Since that's a multibyte 110 value, we should use a conservative value. */ 111 #define BUF 40 112 113 #define quad_t long 114 #define u_quad_t unsigned long 115 116 typedef quad_t * quad_ptr_t; 117 typedef _PTR void_ptr_t; 118 typedef char * char_ptr_t; 119 typedef long * long_ptr_t; 120 typedef int * int_ptr_t; 121 typedef short * short_ptr_t; 122 123 /* Macros for converting digits to letters and vice versa. */ 124 #define to_digit(c) ((c) - '0') 125 #define is_digit(c) ((unsigned)to_digit (c) <= 9) 126 #define to_char(n) ((n) + '0') 127 128 /* Flags used during conversion. */ 129 #define ALT 0x001 /* Alternate form. */ 130 #define LADJUST 0x002 /* Left adjustment. */ 131 #define ZEROPAD 0x004 /* Zero (as opposed to blank) pad. */ 132 #define PLUSSGN 0x008 /* Plus sign flag. */ 133 #define SPACESGN 0x010 /* Space flag. */ 134 #define HEXPREFIX 0x020 /* Add 0x or 0X prefix. */ 135 #define SHORTINT 0x040 /* Short integer. */ 136 #define LONGINT 0x080 /* Long integer. */ 137 #define LONGDBL 0x100 /* Long double. */ 138 /* ifdef _NO_LONGLONG, make QUADINT equivalent to LONGINT, so 139 that %lld behaves the same as %ld, not as %d, as expected if: 140 sizeof (long long) = sizeof long > sizeof int. */ 141 #define QUADINT LONGINT 142 #define FPT 0x400 /* Floating point number. */ 143 /* Define as 0, to make SARG and UARG occupy fewer instructions. */ 144 # define CHARINT 0 145 146 /* Macros to support positional arguments. */ 147 #define GET_ARG(n, ap, type) (va_arg ((ap), type)) 148 149 /* To extend shorts properly, we need both signed and unsigned 150 argument extraction methods. Also they should be used in nano-vfprintf_i.c 151 and nano-vfprintf_float.c only, since ap is a pointer to va_list. */ 152 #define SARG(flags) \ 153 (flags&LONGINT ? GET_ARG (N, (*ap), long) : \ 154 flags&SHORTINT ? (long)(short)GET_ARG (N, (*ap), int) : \ 155 flags&CHARINT ? (long)(signed char)GET_ARG (N, (*ap), int) : \ 156 (long)GET_ARG (N, (*ap), int)) 157 #define UARG(flags) \ 158 (flags&LONGINT ? GET_ARG (N, (*ap), u_long) : \ 159 flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, (*ap), int) : \ 160 flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, (*ap), int) : \ 161 (u_long)GET_ARG (N, (*ap), u_int)) 162 163 /* BEWARE, these `goto error' on error. And they are used 164 in more than one functions. 165 166 Following macros are each referred about twice in printf for integer, 167 so it is not worth to rewrite them into functions. This situation may 168 change in the future. */ 169 #define PRINT(ptr, len) { \ 170 if (pfunc (data, fp, (ptr), (len)) == EOF) \ 171 goto error; \ 172 } 173 #define PAD(howmany, ch) { \ 174 int temp_i = 0; \ 175 while (temp_i < (howmany)) \ 176 { \ 177 if (pfunc (data, fp, &(ch), 1) == EOF) \ 178 goto error; \ 179 temp_i++; \ 180 } \ 181 } 182 #define PRINTANDPAD(p, ep, len, ch) { \ 183 int temp_n = (ep) - (p); \ 184 if (temp_n > (len)) \ 185 temp_n = (len); \ 186 if (temp_n > 0) \ 187 PRINT((p), temp_n); \ 188 PAD((len) - (temp_n > 0 ? temp_n : 0), (ch)); \ 189 } 190 191 /* All data needed to decode format string are kept in below struct. */ 192 struct _prt_data_t 193 { 194 int flags; /* Flags. */ 195 int prec; /* Precision. */ 196 int dprec; /* Decimal precision. */ 197 int width; /* Width. */ 198 int size; /* Size of converted field or string. */ 199 int ret; /* Return value accumulator. */ 200 char code; /* Current conversion specifier. */ 201 char blank; /* Blank character. */ 202 char zero; /* Zero character. */ 203 char buf[BUF]; /* Output buffer for non-floating point number. */ 204 char l_buf[3]; /* Sign&hex_prefix, "+/-" and "0x/X". */ 205 #ifdef FLOATING_POINT 206 _PRINTF_FLOAT_TYPE _double_; /* Double value. */ 207 char expstr[MAXEXPLEN]; /* Buffer for exponent string. */ 208 int lead; /* The sig figs before decimal or group sep. */ 209 #endif 210 }; 211 212 extern int 213 _printf_common (struct _reent *data, 214 struct _prt_data_t *pdata, 215 int *realsz, 216 FILE *fp, 217 int (*pfunc)(struct _reent *, FILE *, 218 _CONST char *, size_t len)); 219 220 extern int 221 _printf_i (struct _reent *data, struct _prt_data_t *pdata, FILE *fp, 222 int (*pfunc)(struct _reent *, FILE *, _CONST char *, size_t len), 223 va_list *ap); 224 225 /* Make _printf_float weak symbol, so it won't be linked in if target program 226 does not need it. */ 227 extern int 228 _printf_float (struct _reent *data, 229 struct _prt_data_t *pdata, 230 FILE *fp, 231 int (*pfunc)(struct _reent *, FILE *, 232 _CONST char *, size_t len), 233 va_list *ap) _ATTRIBUTE((__weak__)); 234 #endif 235