1 /*
2  * Copyright © 2021, Keith Packard <keithp@keithp.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "picolibc-hooks.h"
8 
9 static LIBC_DATA int (*_stdout_hook)(int);
10 
z_impl_zephyr_fputc(int a,FILE * out)11 int z_impl_zephyr_fputc(int a, FILE *out)
12 {
13 	(*_stdout_hook)(a);
14 	return 0;
15 }
16 
17 #ifdef CONFIG_USERSPACE
z_vrfy_zephyr_fputc(int c,FILE * stream)18 static inline int z_vrfy_zephyr_fputc(int c, FILE *stream)
19 {
20 	return z_impl_zephyr_fputc(c, stream);
21 }
22 #include <zephyr/syscalls/zephyr_fputc_mrsh.c>
23 #endif
24 
picolibc_put(char a,FILE * f)25 static int picolibc_put(char a, FILE *f)
26 {
27 	zephyr_fputc(a, f);
28 	return 0;
29 }
30 
31 static LIBC_DATA FILE __stdout = FDEV_SETUP_STREAM(picolibc_put, NULL, NULL, 0);
32 static LIBC_DATA FILE __stdin = FDEV_SETUP_STREAM(NULL, NULL, NULL, 0);
33 
34 #ifdef __strong_reference
35 #define STDIO_ALIAS(x) __strong_reference(stdout, x);
36 #else
37 #define STDIO_ALIAS(x) FILE *const x = &__stdout;
38 #endif
39 
40 FILE *const stdin = &__stdin;
41 FILE *const stdout = &__stdout;
42 STDIO_ALIAS(stderr);
43 
__stdout_hook_install(int (* hook)(int))44 void __stdout_hook_install(int (*hook)(int))
45 {
46 	_stdout_hook = hook;
47 	__stdout.flags |= _FDEV_SETUP_WRITE;
48 }
49 
__stdin_hook_install(unsigned char (* hook)(void))50 void __stdin_hook_install(unsigned char (*hook)(void))
51 {
52 	__stdin.get = (int (*)(FILE *)) hook;
53 	__stdin.flags |= _FDEV_SETUP_READ;
54 }
55