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 struct cb_bits {
10 	FILE f;
11 	cbprintf_cb out;
12 	void	*ctx;
13 };
14 
cbputc(char c,FILE * _s)15 static int cbputc(char c, FILE *_s)
16 {
17 	struct cb_bits *s = (struct cb_bits *) _s;
18 
19 	(*s->out) (c, s->ctx);
20 	return 0;
21 }
22 
cbvprintf(cbprintf_cb out,void * ctx,const char * fp,va_list ap)23 int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap)
24 {
25 	struct cb_bits	s = {
26 		.f = FDEV_SETUP_STREAM(cbputc, NULL, NULL, _FDEV_SETUP_WRITE),
27 		.out = out,
28 		.ctx = ctx,
29 	};
30 	return vfprintf(&s.f, fp, ap);
31 }
32