Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
glibc
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security
Insights
Files
c06b716
abilist
aout
argp
assert
bits
catgets
conf
conform
crypt
csu
ctype
debug
dirent
dlfcn
elf
gmon
gnulib
grp
hesiod
hurd
iconv
iconvdata
include
inet
intl
io
libidn
libio
locale
localedata
login
mach
malloc
manual
math
misc
nis
nptl
nptl_db
nscd
nss
po
posix
pwd
resolv
resource
rt
scripts
setjmp
shadow
signal
socket
soft-fp
stdio-common
bits
.cvsignore
Depend
Makefile
Versions
_i18n_number.h
_itoa.c
_itoa.h
_itowa.c
_itowa.h
asprintf.c
bug1.c
bug1.input
bug10.c
bug11.c
bug12.c
bug13.c
bug14.c
bug15.c
bug16.c
bug17.c
bug18.c
bug18a.c
bug19.c
bug19a.c
bug2.c
bug3.c
bug4.c
bug5.c
bug6.c
bug6.input
bug7.c
bug8.c
bug9.c
ctermid.c
cuserid.c
dprintf.c
errlist.c
errnobug.c
flockfile.c
fprintf.c
fscanf.c
ftrylockfile.c
funlockfile.c
fxprintf.c
getline.c
getw.c
isoc99_fscanf.c
isoc99_scanf.c
isoc99_sscanf.c
isoc99_vfscanf.c
isoc99_vscanf.c
isoc99_vsscanf.c
itoa-digits.c
itoa-udigits.c
itowa-digits.c
perror.c
printf-parse.h
printf-parsemb.c
printf-parsewc.c
printf-prs.c
printf.c
printf.h
printf_fp.c
printf_fphex.c
printf_size.c
psignal.c
putw.c
reg-printf.c
remove.c
rename.c
renameat.c
scanf.c
scanf1.c
scanf10.c
scanf11.c
scanf12.c
scanf12.input
scanf13.c
scanf14.c
scanf15.c
scanf2.c
scanf3.c
scanf4.c
scanf5.c
scanf7.c
scanf8.c
scanf9.c
siglist.c
snprintf.c
sprintf.c
sscanf.c
stdio_ext.h
stdio_lim.h.in
tempnam.c
tempname.c
temptest.c
test-fseek.c
test-fwrite.c
test-popen.c
test-vfprintf.c
test_rdwr.c
tfformat.c
tiformat.c
tllformat.c
tmpfile.c
tmpfile64.c
tmpnam.c
tmpnam_r.c
tst-cookie.c
tst-fdopen.c
tst-ferror.c
tst-ferror.input
tst-fgets.c
tst-fileno.c
tst-fmemopen.c
tst-fmemopen2.c
tst-fphex.c
tst-fseek.c
tst-fwrite.c
tst-gets.c
tst-gets.input
tst-obprintf.c
tst-perror.c
tst-popen.c
tst-popen2.c
tst-printf.c
tst-printf.sh
tst-printfsz.c
tst-put-error.c
tst-rndseek.c
tst-sprintf.c
tst-sprintf2.c
tst-sscanf.c
tst-swprintf.c
tst-swscanf.c
tst-tmpnam.c
tst-unbputc.c
tst-unbputc.sh
tst-ungetc.c
tst-unlockedio.c
tst-wc-printf.c
tstdiomisc.c
tstgetln.c
tstgetln.input
tstscanf.c
tstscanf.input
vfprintf.c
vfscanf.c
vfwprintf.c
vfwscanf.c
vprintf.c
xbug.c
stdlib
streams
string
sunrpc
sysdeps
sysvipc
termios
time
timezone
wcsmbs
wctype
.cvsignore
BUGS
CANCEL-FCT-WAIVE
CANCEL-FILE-WAIVE
CONFORMANCE
COPYING
COPYING.LIB
ChangeLog
ChangeLog.1
ChangeLog.10
ChangeLog.11
ChangeLog.12
ChangeLog.13
ChangeLog.14
ChangeLog.15
ChangeLog.16
ChangeLog.2
ChangeLog.3
ChangeLog.4
ChangeLog.5
ChangeLog.6
ChangeLog.7
ChangeLog.8
ChangeLog.9
FAQ
FAQ.in
INSTALL
LICENSES
Makeconfig
Makefile
Makefile.in
Makerules
NAMESPACE
NEWS
NOTES
PROJECTS
README
README.libm
README.template
Rules
Versions.def
WUR-REPORT
abi-tags
aclocal.m4
config-name.in
config.h.in
config.make.in
configure
configure.in
cppflags-iterator.mk
extra-lib.mk
extra-modules.mk
o-iterator.mk
shlib-versions
test-skeleton.c
tls.make.c
version.h
Breadcrumbs
glibc
/
stdio-common
/
printf-parsemb.c
Blame
Blame
Latest commit
History
History
381 lines (348 loc) · 9.51 KB
Breadcrumbs
glibc
/
stdio-common
/
printf-parsemb.c
Top
File metadata and controls
Code
Blame
381 lines (348 loc) · 9.51 KB
Raw
/* Helper functions for parsing printf format strings. Copyright (C) 1995-2000,2002,2003,2004,2006 Free Software Foundation, Inc. This file is part of th GNU C Library. The GNU C Library 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 2.1 of the License, or (at your option) any later version. The GNU C Library 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 Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include <ctype.h> #include <limits.h> #include <stdlib.h> #include <string.h> #include <sys/param.h> #include <wchar.h> #include <wctype.h> #ifndef COMPILE_WPRINTF # define CHAR_T char # define UCHAR_T unsigned char # define INT_T int # define L_(Str) Str # define ISDIGIT(Ch) isdigit (Ch) #else # define CHAR_T wchar_t # define UCHAR_T unsigned int # define INT_T wint_t # define L_(Str) L##Str # define ISDIGIT(Ch) iswdigit (Ch) #endif #include "printf-parse.h" #define NDEBUG 1 #include <assert.h> /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC with the parsed details. POSN is the number of arguments already consumed. At most MAXTYPES - POSN types are filled in TYPES. Return the number of args consumed by this spec; *MAX_REF_ARG is updated so it remains the highest argument index used. */ size_t attribute_hidden #ifdef COMPILE_WPRINTF __parse_one_specwc (const UCHAR_T *format, size_t posn, struct printf_spec *spec, size_t *max_ref_arg) #else __parse_one_specmb (const UCHAR_T *format, size_t posn, struct printf_spec *spec, size_t *max_ref_arg) #endif { unsigned int n; size_t nargs = 0; /* Skip the '%'. */ ++format; /* Clear information structure. */ spec->data_arg = -1; spec->info.alt = 0; spec->info.space = 0; spec->info.left = 0; spec->info.showsign = 0; spec->info.group = 0; spec->info.i18n = 0; spec->info.extra = 0; spec->info.pad = ' '; spec->info.wide = sizeof (UCHAR_T) > 1; /* Test for positional argument. */ if (ISDIGIT (*format)) { const UCHAR_T *begin = format; n = read_int (&format); if (n > 0 && *format == L_('$')) /* Is positional parameter. */ { ++format; /* Skip the '$'. */ spec->data_arg = n - 1; *max_ref_arg = MAX (*max_ref_arg, n); } else /* Oops; that was actually the width and/or 0 padding flag. Step back and read it again. */ format = begin; } /* Check for spec modifiers. */ do { switch (*format) { case L_(' '): /* Output a space in place of a sign, when there is no sign. */ spec->info.space = 1; continue; case L_('+'): /* Always output + or - for numbers. */ spec->info.showsign = 1; continue; case L_('-'): /* Left-justify things. */ spec->info.left = 1; continue; case L_('#'): /* Use the "alternate form": Hex has 0x or 0X, FP always has a decimal point. */ spec->info.alt = 1; continue; case L_('0'): /* Pad with 0s. */ spec->info.pad = '0'; continue; case L_('\''): /* Show grouping in numbers if the locale information indicates any. */ spec->info.group = 1; continue; case L_('I'): /* Use the internationalized form of the output. Currently means to use the `outdigits' of the current locale. */ spec->info.i18n = 1; continue; default: break; } break; } while (*++format); if (spec->info.left) spec->info.pad = ' '; /* Get the field width. */ spec->width_arg = -1; spec->info.width = 0; if (*format == L_('*')) { /* The field width is given in an argument. A negative field width indicates left justification. */ const UCHAR_T *begin = ++format; if (ISDIGIT (*format)) { /* The width argument might be found in a positional parameter. */ n = read_int (&format); if (n > 0 && *format == L_('$')) { spec->width_arg = n - 1; *max_ref_arg = MAX (*max_ref_arg, n); ++format; /* Skip '$'. */ } } if (spec->width_arg < 0) { /* Not in a positional parameter. Consume one argument. */ spec->width_arg = posn++; ++nargs; format = begin; /* Step back and reread. */ } } else if (ISDIGIT (*format)) /* Constant width specification. */ spec->info.width = read_int (&format); /* Get the precision. */ spec->prec_arg = -1; /* -1 means none given; 0 means explicit 0. */ spec->info.prec = -1; if (*format == L_('.')) { ++format; if (*format == L_('*')) { /* The precision is given in an argument. */ const UCHAR_T *begin = ++format; if (ISDIGIT (*format)) { n = read_int (&format); if (n > 0 && *format == L_('$')) { spec->prec_arg = n - 1; *max_ref_arg = MAX (*max_ref_arg, n); ++format; } } if (spec->prec_arg < 0) { /* Not in a positional parameter. */ spec->prec_arg = posn++; ++nargs; format = begin; } } else if (ISDIGIT (*format)) spec->info.prec = read_int (&format); else /* "%.?" is treated like "%.0?". */ spec->info.prec = 0; } /* Check for type modifiers. */ spec->info.is_long_double = 0; spec->info.is_short = 0; spec->info.is_long = 0; spec->info.is_char = 0; switch (*format++) { case L_('h'): /* ints are short ints or chars. */ if (*format != L_('h')) spec->info.is_short = 1; else { ++format; spec->info.is_char = 1; } break; case L_('l'): /* ints are long ints. */ spec->info.is_long = 1; if (*format != L_('l')) break; ++format; /* FALLTHROUGH */ case L_('L'): /* doubles are long doubles, and ints are long long ints. */ case L_('q'): /* 4.4 uses this for long long. */ spec->info.is_long_double = 1; break; case L_('z'): case L_('Z'): /* ints are size_ts. */ assert (sizeof (size_t) <= sizeof (unsigned long long int)); #if LONG_MAX != LONG_LONG_MAX spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int); #endif spec->info.is_long = sizeof (size_t) > sizeof (unsigned int); break; case L_('t'): assert (sizeof (ptrdiff_t) <= sizeof (long long int)); #if LONG_MAX != LONG_LONG_MAX spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int)); #endif spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int); break; case L_('j'): assert (sizeof (uintmax_t) <= sizeof (unsigned long long int)); #if LONG_MAX != LONG_LONG_MAX spec->info.is_long_double = (sizeof (uintmax_t) > sizeof (unsigned long int)); #endif spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int); break; default: /* Not a recognized modifier. Backup. */ --format; break; } /* Get the format specification. */ spec->info.spec = (wchar_t) *format++; if (__builtin_expect (__printf_function_table != NULL, 0) && spec->info.spec <= UCHAR_MAX && __printf_arginfo_table[spec->info.spec] != NULL) /* We don't try to get the types for all arguments if the format uses more than one. The normal case is covered though. */ spec->ndata_args = (*__printf_arginfo_table[spec->info.spec]) (&spec->info, 1, &spec->data_arg_type); else { /* Find the data argument types of a built-in spec. */ spec->ndata_args = 1; switch (spec->info.spec) { case L'i': case L'd': case L'u': case L'o': case L'X': case L'x': #if LONG_MAX != LONG_LONG_MAX if (spec->info.is_long_double) spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG; else #endif if (spec->info.is_long) spec->data_arg_type = PA_INT|PA_FLAG_LONG; else if (spec->info.is_short) spec->data_arg_type = PA_INT|PA_FLAG_SHORT; else if (spec->info.is_char) spec->data_arg_type = PA_CHAR; else spec->data_arg_type = PA_INT; break; case L'e': case L'E': case L'f': case L'F': case L'g': case L'G': case L'a': case L'A': if (spec->info.is_long_double) spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE; else spec->data_arg_type = PA_DOUBLE; break; case L'c': spec->data_arg_type = PA_CHAR; break; case L'C': spec->data_arg_type = PA_WCHAR; break; case L's': spec->data_arg_type = PA_STRING; break; case L'S': spec->data_arg_type = PA_WSTRING; break; case L'p': spec->data_arg_type = PA_POINTER; break; case L'n': spec->data_arg_type = PA_INT|PA_FLAG_PTR; break; case L'm': default: /* An unknown spec will consume no args. */ spec->ndata_args = 0; break; } } if (spec->data_arg == -1 && spec->ndata_args > 0) { /* There are args consumed, but no positional spec. Use the next sequential arg position. */ spec->data_arg = posn; nargs += spec->ndata_args; } if (spec->info.spec == L'\0') /* Format ended before this spec was complete. */ spec->end_of_fmt = spec->next_fmt = format - 1; else { /* Find the next format spec. */ spec->end_of_fmt = format; #ifdef COMPILE_WPRINTF spec->next_fmt = __find_specwc (format); #else spec->next_fmt = __find_specmb (format); #endif } return nargs; }
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
You can’t perform that action at this time.