aboutsummaryrefslogtreecommitdiff
path: root/arch/all/core/src/stdio/printf/vfprintf.c
blob: 94e2f4c506f1e3853d4fc8432068c5550b9e4c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* ****************************************************************************
 * stdio/printf/vfprintf.c -- The core of all.
 *
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of the 'all/core' module in libcarrot, an experimental
 * modular libc project.
 *
 * This file is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Lesser Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this file. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include "printf.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* ************************************************************************* */
/*  Utilities                                                                */
/* ************************************************************************* */
/**
 *	get_num:
 *	Get a number from the string.
 *
 *	@arg	ps		double pointer on the format.
 *	@return			the number.
 */

static int get_num(const char **ps)
{
	int i = 0; const char *s = *ps;

	while (isdigit(*s))
		i = i * 10 + *s++ - '0';
	*ps = s;
	return (i);
}

/**
 *	get_format:
 *	Get the format.
 *
 *	@arg	p		double pointer on the format.
 *	@arg	info	the information.
 *	@arg	ap		the arguments pointer.
 *	@return			number of read characters.
 */

static int get_format(const char **ps, printf_t *info, va_list ap)
{
	const char *s; int i;
	int still_info;
	int n;

	/* initialize info */
	info->flags = 0;
	info->prec = -1;
	info->width = 0;
	info->pad = ' ';

	s = *ps; still_info = 1;
	for (; still_info; s++) switch (*s) {
		/* flags */
		case '-': info->flags |= printf_flag_left;  break;
		case '+': info->flags |= printf_flag_sign;  break;
		case ' ': info->flags |= printf_flag_space; break;
		case '#': info->flags |= printf_flag_alt;   break;
		case '0': info->pad = '0'; break;

		/* width */
		case '*':
			i = va_arg(ap, int);
			if (i < 0) {
				info->flags |= printf_flag_left;
				i = -i;
			}
			info->width = i;
			break;
		case '1': case '2': case '3': case '4': case '5':
		case '6': case '7': case '8': case '9':
			info->width = get_num(&s);
			s--; /* compensate */
			break;

		/* precision */
		case '.': s++; switch (*s) {
			case '*': info->prec = va_arg(ap, int); break;
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				i = get_num(&s);
				info->prec = abs(i);
				s--; /* compensate */
				break;
			default: return (-1);
		} break;

		/* length */
		case 'h':
			info->flags |= printf_flag_short
				| ((info->flags & printf_flag_short) << 1);
			break;
		case 'l':
			info->flags |= printf_flag_long
				| ((info->flags & printf_flag_long) >> 1);
			break;
		case 'L': case 'q':
			info->flags |= printf_flag_llong; break;
		case 'z': info->flags |= printf_flag_ptr; break;
		case 'j': info->flags |= printf_flag_imax; break;
		case 't': info->flags |= printf_flag_ptr; break;

		default: /* TODO: check if is a letter? */
			still_info = 0; break;
	}

	/* and get the type */
	info->spec = *s++;

	/* everything went well :) */
	n = (int)(s - *ps);
	*ps = s;
	return (n);
}
/* ************************************************************************* */
/*  Main function                                                            */
/* ************************************************************************* */
/**
 *	vfprintf:
 *	Main print formatted function.
 *
 *	@arg	stream		the stream to print to.
 *	@arg	fmt			the format string.
 *	@arg	ap			the variable argument list (argument pointer).
 *	@return				if everything is okay.
 */

int vfprintf(FILE *stream, const char *fmt, va_list ap)
{
	int n = 0, ntemp;
	size_t l; const char *f;
	printf_t info;
	printf_callback_t *cb;

	while (*fmt) {
		/* pass litteral text */
		f = strchr(fmt, '%');
		if (!f) {
			l = strlen(fmt);
			if (!fwrite(fmt, l, 1, stream)) return (-1);
			n += l;
			break;
		}

		/* don't forget to copy it */
		l = (size_t)(f - fmt);
		if (l && !fwrite(fmt, l, 1, stream)) return (-1);
		n += l;

		/* get the format */
		fmt++; ntemp = get_format(&fmt, &info, ap);
		if (ntemp < 0) return (-1);
		n += ntemp;

		/* lookup for a callback using that specifier */
		if (find_printf_function(info.spec, &cb))
			return (-1);

		/* call this callback */
		ntemp = (*cb)(stream, &info, ap);
		if (ntemp < 0) return (-1);
		n += ntemp;
	}

	/* well, looks like everything went well */
	return (n);
}