2 This file is part of systemd.
4 Copyright 2014 Emil Renner Berthing <systemd@esmil.dk>
6 With parts from the musl C library
7 Copyright 2005-2014 Rich Felker, et al.
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "parse-printf-format.h"
30 static const char *consume_nonarg(const char *fmt)
35 } while (*fmt++ != '%');
39 static const char *consume_num(const char *fmt)
41 for (;*fmt >= '0' && *fmt <= '9'; fmt++)
46 static const char *consume_argn(const char *fmt, size_t *arg)
51 if (*p < '1' || *p > '9')
54 val = 10*val + (*p++ - '0');
55 } while (*p >= '0' && *p <= '9');
63 static const char *consume_flags(const char *fmt)
116 static const short pa_types[MAXTYPE] = {
121 [ULLONG] = PA_INT | PA_FLAG_LONG_LONG,
122 [LONG] = PA_INT | PA_FLAG_LONG,
123 [ULONG] = PA_INT | PA_FLAG_LONG,
124 [SHORT] = PA_INT | PA_FLAG_SHORT,
125 [USHORT] = PA_INT | PA_FLAG_SHORT,
128 [LLONG] = PA_INT | PA_FLAG_LONG_LONG,
129 [SIZET] = PA_INT | PA_FLAG_LONG,
130 [IMAX] = PA_INT | PA_FLAG_LONG_LONG,
131 [UMAX] = PA_INT | PA_FLAG_LONG_LONG,
132 [PDIFF] = PA_INT | PA_FLAG_LONG_LONG,
133 [UIPTR] = PA_INT | PA_FLAG_LONG,
135 [LDBL] = PA_DOUBLE | PA_FLAG_LONG_DOUBLE
138 #define S(x) [(x)-'A']
139 #define E(x) (STOP + (x))
141 static const unsigned char states[]['z'-'A'+1] = {
142 { /* 0: bare types */
143 S('d') = E(INT), S('i') = E(INT),
144 S('o') = E(UINT),S('u') = E(UINT),S('x') = E(UINT), S('X') = E(UINT),
145 S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL),
146 S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL),
147 S('c') = E(CHAR),S('C') = E(INT),
148 S('s') = E(PTR), S('S') = E(PTR), S('p') = E(UIPTR),S('n') = E(PTR),
150 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
151 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE
152 }, { /* 1: l-prefixed */
153 S('d') = E(LONG), S('i') = E(LONG),
154 S('o') = E(ULONG),S('u') = E(ULONG),S('x') = E(ULONG),S('X') = E(ULONG),
155 S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL),
156 S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL),
157 S('c') = E(INT), S('s') = E(PTR), S('n') = E(PTR),
159 }, { /* 2: ll-prefixed */
160 S('d') = E(LLONG), S('i') = E(LLONG),
161 S('o') = E(ULLONG),S('u') = E(ULLONG),
162 S('x') = E(ULLONG),S('X') = E(ULLONG),
164 }, { /* 3: h-prefixed */
165 S('d') = E(SHORT), S('i') = E(SHORT),
166 S('o') = E(USHORT),S('u') = E(USHORT),
167 S('x') = E(USHORT),S('X') = E(USHORT),
170 }, { /* 4: hh-prefixed */
171 S('d') = E(CHAR), S('i') = E(CHAR),
172 S('o') = E(UCHAR),S('u') = E(UCHAR),
173 S('x') = E(UCHAR),S('X') = E(UCHAR),
175 }, { /* 5: L-prefixed */
176 S('e') = E(LDBL),S('f') = E(LDBL),S('g') = E(LDBL), S('a') = E(LDBL),
177 S('E') = E(LDBL),S('F') = E(LDBL),S('G') = E(LDBL), S('A') = E(LDBL),
179 }, { /* 6: z- or t-prefixed (assumed to be same size) */
180 S('d') = E(PDIFF),S('i') = E(PDIFF),
181 S('o') = E(SIZET),S('u') = E(SIZET),
182 S('x') = E(SIZET),S('X') = E(SIZET),
184 }, { /* 7: j-prefixed */
185 S('d') = E(IMAX), S('i') = E(IMAX),
186 S('o') = E(UMAX), S('u') = E(UMAX),
187 S('x') = E(UMAX), S('X') = E(UMAX),
192 size_t parse_printf_format(const char *fmt, size_t n, int *types)
203 fmt = consume_nonarg(fmt);
211 fmt = consume_argn(fmt, &arg);
213 fmt = consume_flags(fmt);
217 fmt = consume_argn(fmt+1, &warg);
222 if (warg <= n && types[warg-1] == NONE)
225 fmt = consume_num(fmt);
231 fmt = consume_argn(fmt+1, &parg);
236 if (parg <= n && types[parg-1] == NONE)
241 fmt = consume_num(fmt);
244 /* length modifier and conversion specifier */
247 unsigned char c = *fmt++;
249 if (c < 'A' || c > 'z')
251 state = states[state]S(c);
254 } while (state < STOP);
256 if (state == E(NONE))
264 types[arg-1] = state - STOP;
269 for (i = 0; i < last; i++)
270 types[i] = pa_types[types[i]];
275 #endif // HAVE_PRINTF_H