Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2008, Atmel Corporation |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame] | 5 | * Copyright (c) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 6 | * |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * |
| 12 | * - Redistributions of source code must retain the above copyright notice, |
| 13 | * this list of conditions and the disclaimer below. |
| 14 | * |
| 15 | * Atmel's name may not be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 24 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 27 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * ---------------------------------------------------------------------------- |
| 29 | */ |
| 30 | |
| 31 | //------------------------------------------------------------------------------ |
| 32 | /// \unit |
| 33 | /// |
| 34 | /// !Purpose |
| 35 | /// |
| 36 | /// Implementation of several stdio.h methods, such as printf(), sprintf() and |
| 37 | /// so on. This reduces the memory footprint of the binary when using those |
| 38 | /// methods, compared to the libc implementation. |
| 39 | /// |
| 40 | /// !Usage |
| 41 | /// |
| 42 | /// Adds stdio.c to the list of file to compile for the project. This will |
| 43 | /// automatically replace libc methods by the custom ones. |
| 44 | //------------------------------------------------------------------------------ |
| 45 | |
| 46 | //------------------------------------------------------------------------------ |
| 47 | // Headers |
| 48 | //------------------------------------------------------------------------------ |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <stdarg.h> |
| 52 | |
| 53 | //------------------------------------------------------------------------------ |
| 54 | // Local Definitions |
| 55 | //------------------------------------------------------------------------------ |
| 56 | |
| 57 | // Maximum string size allowed (in bytes). |
Harald Welte | ee9ebb3 | 2017-03-02 16:52:08 +0100 | [diff] [blame] | 58 | #define MAX_STRING_SIZE 512 |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 59 | |
| 60 | //------------------------------------------------------------------------------ |
| 61 | // Global Variables |
| 62 | //------------------------------------------------------------------------------ |
Harald Welte | c430ac1 | 2017-02-28 01:25:12 +0100 | [diff] [blame] | 63 | // |
| 64 | FILE* const stdin = NULL; |
Harald Welte | c394109 | 2018-08-26 09:53:13 +0200 | [diff] [blame] | 65 | /* If we use NULL here, we get compiler warnings of calling stdio functions with |
| 66 | * NULL values. Our fputs() implementation ignores the value of those pointers anyway */ |
| 67 | FILE* const stdout = (FILE *) 0x1; |
| 68 | FILE* const stderr = (FILE *) 0x2; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 69 | |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 70 | |
| 71 | //------------------------------------------------------------------------------ |
| 72 | // Local Functions |
| 73 | //------------------------------------------------------------------------------ |
| 74 | |
| 75 | //------------------------------------------------------------------------------ |
| 76 | // Writes a character inside the given string. Returns 1. |
| 77 | // \param pStr Storage string. |
| 78 | // \param c Character to write. |
| 79 | //------------------------------------------------------------------------------ |
| 80 | signed int PutChar(char *pStr, char c) |
| 81 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 82 | *pStr = c; |
| 83 | return 1; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | //------------------------------------------------------------------------------ |
| 87 | // Writes a string inside the given string. |
| 88 | // Returns the size of the written |
| 89 | // string. |
| 90 | // \param pStr Storage string. |
| 91 | // \param pSource Source string. |
| 92 | //------------------------------------------------------------------------------ |
| 93 | signed int PutString(char *pStr, const char *pSource) |
| 94 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 95 | signed int num = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 96 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 97 | while (*pSource != 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 98 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 99 | *pStr++ = *pSource++; |
| 100 | num++; |
| 101 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 102 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 103 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | //------------------------------------------------------------------------------ |
| 107 | // Writes an unsigned int inside the given string, using the provided fill & |
| 108 | // width parameters. |
| 109 | // Returns the size in characters of the written integer. |
| 110 | // \param pStr Storage string. |
| 111 | // \param fill Fill character. |
| 112 | // \param width Minimum integer width. |
| 113 | // \param value Integer value. |
| 114 | //------------------------------------------------------------------------------ |
| 115 | signed int PutUnsignedInt( |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 116 | char *pStr, |
| 117 | char fill, |
| 118 | signed int width, |
| 119 | unsigned int value) |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 120 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 121 | signed int num = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 122 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 123 | // Take current digit into account when calculating width |
| 124 | width--; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 125 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 126 | // Recursively write upper digits |
| 127 | if ((value / 10) > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 128 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 129 | num = PutUnsignedInt(pStr, fill, width, value / 10); |
| 130 | pStr += num; |
| 131 | } |
| 132 | // Write filler characters |
| 133 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 134 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 135 | while (width > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 136 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 137 | PutChar(pStr, fill); |
| 138 | pStr++; |
| 139 | num++; |
| 140 | width--; |
| 141 | } |
| 142 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 143 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 144 | // Write lower digit |
| 145 | num += PutChar(pStr, (value % 10) + '0'); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 146 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 147 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | //------------------------------------------------------------------------------ |
| 151 | // Writes a signed int inside the given string, using the provided fill & width |
| 152 | // parameters. |
| 153 | // Returns the size of the written integer. |
| 154 | // \param pStr Storage string. |
| 155 | // \param fill Fill character. |
| 156 | // \param width Minimum integer width. |
| 157 | // \param value Signed integer value. |
| 158 | //------------------------------------------------------------------------------ |
| 159 | signed int PutSignedInt( |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 160 | char *pStr, |
| 161 | char fill, |
| 162 | signed int width, |
| 163 | signed int value) |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 164 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 165 | signed int num = 0; |
| 166 | unsigned int absolute; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 167 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 168 | // Compute absolute value |
| 169 | if (value < 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 170 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 171 | absolute = -value; |
| 172 | } |
| 173 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 174 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 175 | absolute = value; |
| 176 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 177 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 178 | // Take current digit into account when calculating width |
| 179 | width--; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 180 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 181 | // Recursively write upper digits |
| 182 | if ((absolute / 10) > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 183 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 184 | if (value < 0) { |
| 185 | |
| 186 | num = PutSignedInt(pStr, fill, width, -(absolute / 10)); |
| 187 | } |
| 188 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 189 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 190 | num = PutSignedInt(pStr, fill, width, absolute / 10); |
| 191 | } |
| 192 | pStr += num; |
| 193 | } |
| 194 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 195 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 196 | // Reserve space for sign |
| 197 | if (value < 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 198 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 199 | width--; |
| 200 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 201 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 202 | // Write filler characters |
| 203 | while (width > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 204 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 205 | PutChar(pStr, fill); |
| 206 | pStr++; |
| 207 | num++; |
| 208 | width--; |
| 209 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 210 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 211 | // Write sign |
| 212 | if (value < 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 213 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 214 | num += PutChar(pStr, '-'); |
| 215 | pStr++; |
| 216 | } |
| 217 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 218 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 219 | // Write lower digit |
| 220 | num += PutChar(pStr, (absolute % 10) + '0'); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 221 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 222 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | //------------------------------------------------------------------------------ |
| 226 | // Writes an hexadecimal value into a string, using the given fill, width & |
| 227 | // capital parameters. |
| 228 | // Returns the number of char written. |
| 229 | // \param pStr Storage string. |
| 230 | // \param fill Fill character. |
| 231 | // \param width Minimum integer width. |
| 232 | // \param maj Indicates if the letters must be printed in lower- or upper-case. |
| 233 | // \param value Hexadecimal value. |
| 234 | //------------------------------------------------------------------------------ |
| 235 | signed int PutHexa( |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 236 | char *pStr, |
| 237 | char fill, |
| 238 | signed int width, |
| 239 | unsigned char maj, |
| 240 | unsigned int value) |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 241 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 242 | signed int num = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 243 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 244 | // Decrement width |
| 245 | width--; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 246 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 247 | // Recursively output upper digits |
| 248 | if ((value >> 4) > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 249 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 250 | num += PutHexa(pStr, fill, width, maj, value >> 4); |
| 251 | pStr += num; |
| 252 | } |
| 253 | // Write filler chars |
| 254 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 255 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 256 | while (width > 0) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 257 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 258 | PutChar(pStr, fill); |
| 259 | pStr++; |
| 260 | num++; |
| 261 | width--; |
| 262 | } |
| 263 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 264 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 265 | // Write current digit |
| 266 | if ((value & 0xF) < 10) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 267 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 268 | PutChar(pStr, (value & 0xF) + '0'); |
| 269 | } |
| 270 | else if (maj) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 271 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 272 | PutChar(pStr, (value & 0xF) - 10 + 'A'); |
| 273 | } |
| 274 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 275 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 276 | PutChar(pStr, (value & 0xF) - 10 + 'a'); |
| 277 | } |
| 278 | num++; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 279 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 280 | return num; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | //------------------------------------------------------------------------------ |
| 284 | // Global Functions |
| 285 | //------------------------------------------------------------------------------ |
| 286 | |
| 287 | //------------------------------------------------------------------------------ |
| 288 | /// Stores the result of a formatted string into another string. Format |
| 289 | /// arguments are given in a va_list instance. |
| 290 | /// Return the number of characters written. |
| 291 | /// \param pStr Destination string. |
| 292 | /// \param length Length of Destination string. |
| 293 | /// \param pFormat Format string. |
| 294 | /// \param ap Argument list. |
| 295 | //------------------------------------------------------------------------------ |
| 296 | signed int vsnprintf(char *pStr, size_t length, const char *pFormat, va_list ap) |
| 297 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 298 | char fill; |
| 299 | unsigned char width; |
| 300 | signed int num = 0; |
| 301 | size_t size = 0; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 302 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 303 | // Clear the string |
| 304 | if (pStr) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 305 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 306 | *pStr = 0; |
| 307 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 308 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 309 | // Phase string |
| 310 | while (*pFormat != 0 && size < length) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 311 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 312 | // Normal character |
| 313 | if (*pFormat != '%') { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 314 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 315 | *pStr++ = *pFormat++; |
| 316 | size++; |
| 317 | } |
| 318 | // Escaped '%' |
| 319 | else if (*(pFormat+1) == '%') { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 320 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 321 | *pStr++ = '%'; |
| 322 | pFormat += 2; |
| 323 | size++; |
| 324 | } |
| 325 | // Token delimiter |
| 326 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 327 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 328 | fill = ' '; |
| 329 | width = 0; |
| 330 | pFormat++; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 331 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 332 | // Parse filler |
| 333 | if (*pFormat == '0') { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 334 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 335 | fill = '0'; |
| 336 | pFormat++; |
| 337 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 338 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 339 | // Parse width |
| 340 | while ((*pFormat >= '0') && (*pFormat <= '9')) { |
| 341 | |
| 342 | width = (width*10) + *pFormat-'0'; |
| 343 | pFormat++; |
| 344 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 345 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 346 | // Check if there is enough space |
| 347 | if (size + width > length) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 348 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 349 | width = length - size; |
| 350 | } |
| 351 | |
| 352 | // Parse type |
Kévin Redon | 29200c6 | 2018-08-28 19:36:58 +0200 | [diff] [blame] | 353 | do { |
| 354 | num = 0; |
| 355 | switch (*pFormat) { |
| 356 | case 'l': num = -1; break; // ignore long qualifier since int == long (and long long is not supported) |
| 357 | case 'd': |
| 358 | case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break; |
| 359 | case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break; |
| 360 | case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break; |
| 361 | case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break; |
| 362 | case 's': num = PutString(pStr, va_arg(ap, char *)); break; |
| 363 | case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break; |
| 364 | default: |
| 365 | return EOF; |
| 366 | } |
| 367 | pFormat++; |
| 368 | } while (num < 0); |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 369 | pStr += num; |
| 370 | size += num; |
| 371 | } |
| 372 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 373 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 374 | // NULL-terminated (final \0 is not counted) |
| 375 | if (size < length) { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 376 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 377 | *pStr = 0; |
| 378 | } |
| 379 | else { |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 380 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 381 | *(--pStr) = 0; |
| 382 | size--; |
| 383 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 384 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 385 | return size; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | //------------------------------------------------------------------------------ |
| 389 | /// Stores the result of a formatted string into another string. Format |
| 390 | /// arguments are given in a va_list instance. |
| 391 | /// Return the number of characters written. |
| 392 | /// \param pString Destination string. |
| 393 | /// \param length Length of Destination string. |
| 394 | /// \param pFormat Format string. |
| 395 | /// \param ... Other arguments |
| 396 | //------------------------------------------------------------------------------ |
| 397 | signed int snprintf(char *pString, size_t length, const char *pFormat, ...) |
| 398 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 399 | va_list ap; |
| 400 | signed int rc; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 401 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 402 | va_start(ap, pFormat); |
| 403 | rc = vsnprintf(pString, length, pFormat, ap); |
| 404 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 405 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 406 | return rc; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | //------------------------------------------------------------------------------ |
| 410 | /// Stores the result of a formatted string into another string. Format |
| 411 | /// arguments are given in a va_list instance. |
| 412 | /// Return the number of characters written. |
| 413 | /// \param pString Destination string. |
| 414 | /// \param pFormat Format string. |
| 415 | /// \param ap Argument list. |
| 416 | //------------------------------------------------------------------------------ |
| 417 | signed int vsprintf(char *pString, const char *pFormat, va_list ap) |
| 418 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 419 | return vsnprintf(pString, MAX_STRING_SIZE, pFormat, ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | //------------------------------------------------------------------------------ |
| 423 | /// Outputs a formatted string on the given stream. Format arguments are given |
| 424 | /// in a va_list instance. |
| 425 | /// \param pStream Output stream. |
| 426 | /// \param pFormat Format string |
| 427 | /// \param ap Argument list. |
| 428 | //------------------------------------------------------------------------------ |
| 429 | signed int vfprintf(FILE *pStream, const char *pFormat, va_list ap) |
| 430 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 431 | char pStr[MAX_STRING_SIZE]; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 432 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 433 | // Write formatted string in buffer |
Kévin Redon | c171112 | 2018-08-28 19:27:34 +0200 | [diff] [blame] | 434 | int rc = vsprintf(pStr, pFormat, ap); |
| 435 | if (rc < 0) { |
| 436 | fputs("format string error in ", stderr); |
| 437 | fputs(pFormat, stderr); |
| 438 | return rc; |
| 439 | } |
| 440 | if (rc >= MAX_STRING_SIZE) { |
| 441 | fputs("stdio.c: increase MAX_STRING_SIZE\n\r", stderr); |
| 442 | return rc; |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 443 | } |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 444 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 445 | // Display string |
| 446 | return fputs(pStr, pStream); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | //------------------------------------------------------------------------------ |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame] | 450 | /// Outputs a formatted string on the given stream. Format arguments are given |
| 451 | /// in a va_list instance. |
| 452 | /// \note This function is synchronous (i.e. blocks until the print completes) |
| 453 | /// \param pStream Output stream. |
| 454 | /// \param pFormat Format string |
| 455 | /// \param ap Argument list. |
| 456 | //------------------------------------------------------------------------------ |
| 457 | #pragma GCC diagnostic push |
| 458 | #pragma GCC diagnostic ignored "-Wsuggest-attribute=format" |
| 459 | signed int vfprintf_sync(FILE *pStream, const char *pFormat, va_list ap) |
| 460 | { |
| 461 | char pStr[MAX_STRING_SIZE]; |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame] | 462 | |
| 463 | // Write formatted string in buffer |
Kévin Redon | c171112 | 2018-08-28 19:27:34 +0200 | [diff] [blame] | 464 | int rc = vsprintf(pStr, pFormat, ap); |
| 465 | if (rc < 0) { |
| 466 | fputs_sync("format string error in ", stderr); |
| 467 | fputs_sync(pFormat, stderr); |
| 468 | return rc; |
| 469 | } |
| 470 | if (rc >= MAX_STRING_SIZE) { |
| 471 | fputs_sync("stdio.c: increase MAX_STRING_SIZE\n\r", stderr); |
| 472 | return rc; |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // Display string |
| 476 | return fputs_sync(pStr, pStream); |
| 477 | } |
| 478 | #pragma GCC diagnostic pop |
| 479 | |
| 480 | //------------------------------------------------------------------------------ |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 481 | /// Outputs a formatted string on the DBGU stream. Format arguments are given |
| 482 | /// in a va_list instance. |
| 483 | /// \param pFormat Format string |
| 484 | /// \param ap Argument list. |
| 485 | //------------------------------------------------------------------------------ |
| 486 | signed int vprintf(const char *pFormat, va_list ap) |
| 487 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 488 | return vfprintf(stdout, pFormat, ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | //------------------------------------------------------------------------------ |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame] | 492 | /// Outputs a formatted string on the DBGU stream. Format arguments are given |
| 493 | /// in a va_list instance. |
| 494 | /// \note This function is synchronous (i.e. blocks until the print completes) |
| 495 | /// \param pFormat Format string |
| 496 | /// \param ap Argument list. |
| 497 | //------------------------------------------------------------------------------ |
| 498 | signed int vprintf_sync(const char *pFormat, va_list ap) |
| 499 | { |
| 500 | return vfprintf_sync(stdout, pFormat, ap); |
| 501 | } |
| 502 | |
| 503 | //------------------------------------------------------------------------------ |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 504 | /// Outputs a formatted string on the given stream, using a variable number of |
| 505 | /// arguments. |
| 506 | /// \param pStream Output stream. |
| 507 | /// \param pFormat Format string. |
| 508 | //------------------------------------------------------------------------------ |
| 509 | signed int fprintf(FILE *pStream, const char *pFormat, ...) |
| 510 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 511 | va_list ap; |
| 512 | signed int result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 513 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 514 | // Forward call to vfprintf |
| 515 | va_start(ap, pFormat); |
| 516 | result = vfprintf(pStream, pFormat, ap); |
| 517 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 518 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 519 | return result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | //------------------------------------------------------------------------------ |
| 523 | /// Outputs a formatted string on the DBGU stream, using a variable number of |
| 524 | /// arguments. |
| 525 | /// \param pFormat Format string. |
| 526 | //------------------------------------------------------------------------------ |
| 527 | signed int printf(const char *pFormat, ...) |
| 528 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 529 | va_list ap; |
| 530 | signed int result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 531 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 532 | // Forward call to vprintf |
| 533 | va_start(ap, pFormat); |
| 534 | result = vprintf(pFormat, ap); |
| 535 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 536 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 537 | return result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | //------------------------------------------------------------------------------ |
Kévin Redon | 1836ac0 | 2018-08-02 15:02:05 +0200 | [diff] [blame] | 541 | /// Outputs a formatted string on the DBGU stream, using a variable number of |
| 542 | /// arguments. |
| 543 | /// \note This function is synchronous (i.e. blocks until the print completes) |
| 544 | /// \param pFormat Format string. |
| 545 | //------------------------------------------------------------------------------ |
| 546 | signed int printf_sync(const char *pFormat, ...) |
| 547 | { |
| 548 | va_list ap; |
| 549 | signed int result; |
| 550 | |
| 551 | // Forward call to vprintf |
| 552 | va_start(ap, pFormat); |
| 553 | result = vprintf_sync(pFormat, ap); |
| 554 | va_end(ap); |
| 555 | |
| 556 | return result; |
| 557 | } |
| 558 | |
| 559 | //------------------------------------------------------------------------------ |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 560 | /// Writes a formatted string inside another string. |
| 561 | /// \param pStr Storage string. |
| 562 | /// \param pFormat Format string. |
| 563 | //------------------------------------------------------------------------------ |
| 564 | signed int sprintf(char *pStr, const char *pFormat, ...) |
| 565 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 566 | va_list ap; |
| 567 | signed int result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 568 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 569 | // Forward call to vsprintf |
| 570 | va_start(ap, pFormat); |
| 571 | result = vsprintf(pStr, pFormat, ap); |
| 572 | va_end(ap); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 573 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 574 | return result; |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | //------------------------------------------------------------------------------ |
| 578 | /// Outputs a string on stdout. |
| 579 | /// \param pStr String to output. |
| 580 | //------------------------------------------------------------------------------ |
| 581 | signed int puts(const char *pStr) |
| 582 | { |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 583 | return fputs(pStr, stdout); |
Harald Welte | d09829d | 2017-02-27 22:58:59 +0100 | [diff] [blame] | 584 | } |
| 585 | |