Harald Welte | 361ed20 | 2019-02-24 21:15:39 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief STDIO redirection |
| 5 | * |
| 6 | * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <stdio_io.h> |
| 35 | #include <stdio.h> |
| 36 | |
| 37 | #ifndef _UNIT_TEST_ |
| 38 | #include <yfuns.h> |
| 39 | #else |
| 40 | #define _STD_BEGIN |
| 41 | #define _STD_END |
| 42 | #define _LLIO_ERROR ((size_t)-1) /* For __read and __write. */ |
| 43 | #define _LLIO_STDIN 0 |
| 44 | #define _LLIO_STDOUT 1 |
| 45 | #define _LLIO_STDERR 2 |
| 46 | #endif |
| 47 | |
| 48 | #if (__VER__ < 8010000) |
| 49 | /* Refer http://ftp.iar.se/WWWfiles/arm/webic/doc/EWARM_MigrationGuide.ENU.pdf */ |
| 50 | _STD_BEGIN |
| 51 | #endif |
| 52 | |
| 53 | #pragma module_name = "?__read" |
| 54 | |
| 55 | /*! \brief Reads a number of bytes, at most \a size, into the memory area |
| 56 | * pointed to by \a buffer. |
| 57 | * |
| 58 | * \param handle File handle to read from. |
| 59 | * \param buffer Pointer to buffer to write read bytes to. |
| 60 | * \param size Number of bytes to read. |
| 61 | * |
| 62 | * \return The number of bytes read, \c 0 at the end of the file, or |
| 63 | * \c _LLIO_ERROR on failure. |
| 64 | */ |
| 65 | size_t __read(int handle, unsigned char *buffer, size_t size) |
| 66 | { |
| 67 | int n = 0; |
| 68 | /* This implementation only reads from stdin. |
| 69 | * For all other file handles, it returns failure. */ |
| 70 | if (handle != _LLIO_STDIN) { |
| 71 | return _LLIO_ERROR; |
| 72 | } |
| 73 | |
| 74 | n = stdio_io_read((uint8_t *)buffer, size); |
| 75 | if (n < 0) { |
| 76 | return _LLIO_ERROR; |
| 77 | } |
| 78 | return n; |
| 79 | } |
| 80 | |
| 81 | /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10 |
| 82 | * the implementation is empty to be compatible with old IAR version. |
| 83 | */ |
| 84 | int __close(int handle) |
| 85 | { |
| 86 | (void)(handle); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | #ifndef __GNUC__ |
| 91 | /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10 |
| 92 | * the implementation is empty to be compatible with old IAR version. |
| 93 | */ |
| 94 | int remove(const char *val) |
| 95 | { |
| 96 | (void)(val); |
| 97 | return 0; |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | /*! \brief This routine is required by IAR DLIB library since EWAVR V6.10 |
| 102 | * the implementation is empty to be compatible with old IAR version. |
| 103 | */ |
| 104 | long __lseek(int handle, long val, int val2) |
| 105 | { |
| 106 | (void)(handle); |
| 107 | (void)(val2); |
| 108 | return val; |
| 109 | } |
| 110 | |
| 111 | #if (__VER__ < 8010000) |
| 112 | /* Refer http://ftp.iar.se/WWWfiles/arm/webic/doc/EWARM_MigrationGuide.ENU.pdf */ |
| 113 | _STD_END |
| 114 | #endif |