1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <types.h>
8 #include <rtl.h>
9 #include <util.h>
10 #include <sprintf.h>
11 #include <console.h>
12 
13 static void
charout(size_t cmd,const char * s_arg,uint32_t sz_arg,struct snprint_param * param)14 charout(size_t cmd, const char *s_arg, uint32_t sz_arg, struct snprint_param *param)
15 {
16 	const char *s = s_arg;
17 	uint32_t sz = sz_arg;
18 	/* pointer to an integer to store the number of characters */
19 	size_t nchars = param->wrtn;
20 	/* working pointer */
21 	const char *p = s;
22 	size_t len;
23 
24 	/* copy mode ? */
25 	if (cmd == PRINT_CMD_COPY) {
26 		if (sz > 0U) { /* copy 'sz' characters */
27 			len = console_write(s, sz);
28 			s += len;
29 		}
30 
31 		nchars += (s - p);
32 	} else {
33 		/* fill mode */
34 		nchars += sz;
35 		while (sz != 0U) {
36 			console_putc(s);
37 			sz--;
38 		}
39 	}
40 	param->wrtn = nchars;
41 }
42 
vprintf(const char * fmt,va_list args)43 void vprintf(const char *fmt, va_list args)
44 {
45 	/* struct to store all necessary parameters */
46 	struct print_param param;
47 	struct snprint_param snparam;
48 
49 	/* initialize parameters */
50 	(void)memset(&snparam, 0U, sizeof(snparam));
51 	(void)memset(&param, 0U, sizeof(param));
52 	param.emit = charout;
53 	param.data = &snparam;
54 
55 	/* execute the printf() */
56 	do_print(fmt, &param, args);
57 }
58 
printf(const char * fmt,...)59 void printf(const char *fmt, ...)
60 {
61 	/* variable argument list needed for do_print() */
62 	va_list args;
63 
64 	va_start(args, fmt);
65 
66 	/* execute the printf() */
67 	vprintf(fmt, args);
68 
69 	/* destroy parameter list */
70 	va_end(args);
71 }
72