Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, 2021 Intel Corporation. |
Evgeniy Paltsev | ab13033 | 2021-07-09 00:52:31 +0300 | [diff] [blame] | 3 | * Copyright (c) 2021 Synopsys. |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
Gerard Marull-Paretas | cbd31d7 | 2022-05-06 11:23:05 +0200 | [diff] [blame] | 9 | #include <zephyr/sys/libc-hooks.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 10 | #include <zephyr/internal/syscall_handler.h> |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 11 | #include <string.h> |
Gerard Marull-Paretas | cbd31d7 | 2022-05-06 11:23:05 +0200 | [diff] [blame] | 12 | #include <zephyr/sys/errno_private.h> |
Evgeniy Paltsev | b84a0fe | 2021-08-05 19:41:37 +0300 | [diff] [blame] | 13 | #include <unistd.h> |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | |
Nikolay Agishev | 6e940c5 | 2023-06-07 15:02:39 +0400 | [diff] [blame] | 16 | #ifndef STDIN_FILENO |
| 17 | #define STDIN_FILENO 0 |
| 18 | #endif |
| 19 | |
| 20 | #ifndef STDOUT_FILENO |
| 21 | #define STDOUT_FILENO 1 |
| 22 | #endif |
| 23 | |
| 24 | #ifndef STDERR_FILENO |
| 25 | #define STDERR_FILENO 2 |
| 26 | #endif |
| 27 | |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 28 | static int _stdout_hook_default(int c) |
| 29 | { |
| 30 | ARG_UNUSED(c); |
| 31 | |
| 32 | return EOF; |
| 33 | } |
| 34 | |
| 35 | static int (*_stdout_hook)(int) = _stdout_hook_default; |
| 36 | |
| 37 | void __stdout_hook_install(int (*hook)(int)) |
| 38 | { |
| 39 | _stdout_hook = hook; |
| 40 | } |
| 41 | |
Stephanos Ioannidis | 92f0f70 | 2021-09-10 16:33:11 +0900 | [diff] [blame] | 42 | int z_impl_zephyr_write_stdout(const void *buffer, int nbytes) |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 43 | { |
| 44 | const char *buf = buffer; |
| 45 | int i; |
| 46 | |
| 47 | for (i = 0; i < nbytes; i++) { |
| 48 | if (*(buf + i) == '\n') { |
| 49 | _stdout_hook('\r'); |
| 50 | } |
| 51 | _stdout_hook(*(buf + i)); |
| 52 | } |
| 53 | return nbytes; |
| 54 | } |
| 55 | |
| 56 | #ifdef CONFIG_USERSPACE |
Stephanos Ioannidis | 92f0f70 | 2021-09-10 16:33:11 +0900 | [diff] [blame] | 57 | static inline int z_vrfy_zephyr_write_stdout(const void *buf, int nbytes) |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 58 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 59 | K_OOPS(K_SYSCALL_MEMORY_READ(buf, nbytes)); |
Stephanos Ioannidis | 92f0f70 | 2021-09-10 16:33:11 +0900 | [diff] [blame] | 60 | return z_impl_zephyr_write_stdout(buf, nbytes); |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 61 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 62 | #include <zephyr/syscalls/zephyr_write_stdout_mrsh.c> |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | #ifndef CONFIG_POSIX_API |
| 66 | int _write(int fd, const char *buf, unsigned int nbytes) |
| 67 | { |
| 68 | ARG_UNUSED(fd); |
| 69 | |
Stephanos Ioannidis | 92f0f70 | 2021-09-10 16:33:11 +0900 | [diff] [blame] | 70 | return zephyr_write_stdout(buf, nbytes); |
Evgeniy Paltsev | a5788ff | 2021-06-04 00:03:23 +0300 | [diff] [blame] | 71 | } |
| 72 | #endif |
Evgeniy Paltsev | ab13033 | 2021-07-09 00:52:31 +0300 | [diff] [blame] | 73 | |
Evgeniy Paltsev | b84a0fe | 2021-08-05 19:41:37 +0300 | [diff] [blame] | 74 | /* |
| 75 | * It's require to implement _isatty to have STDIN/STDOUT/STDERR buffered |
| 76 | * properly. |
| 77 | */ |
| 78 | int _isatty(int file) |
| 79 | { |
| 80 | if (file == STDIN_FILENO || file == STDOUT_FILENO || file == STDERR_FILENO) { |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Evgeniy Paltsev | ab13033 | 2021-07-09 00:52:31 +0300 | [diff] [blame] | 87 | int *___errno(void) |
| 88 | { |
| 89 | return z_errno(); |
| 90 | } |
Evgeniy Paltsev | 9f948a8 | 2021-08-03 20:24:56 +0300 | [diff] [blame] | 91 | |
| 92 | __weak void _exit(int status) |
| 93 | { |
| 94 | while (1) { |
| 95 | } |
| 96 | } |