Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/printf-parse.c @ 9928:9a02133ad731
Add tentative support for Linux libc5.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 17 Apr 2008 02:01:23 +0200 |
parents | 82384b8e3c31 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Formatted output to strings. |
9588
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
2 Copyright (C) 1999-2000, 2002-2003, 2006-2008 Free Software Foundation, Inc. |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
4 This program is free software; you can redistribute it and/or modify |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
6 the Free Software Foundation; either version 2, or (at your option) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 any later version. |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
12 GNU General Public License for more details. |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
14 You should have received a copy of the GNU General Public License along |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
15 with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
4886
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
18 /* This file can be parametrized with the following macros: |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
19 CHAR_T The element type of the format string. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
20 CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
21 in the format string are ASCII. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
22 DIRECTIVE Structure denoting a format directive. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
23 Depends on CHAR_T. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
24 DIRECTIVES Structure denoting the set of format directives of a |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
25 format string. Depends on CHAR_T. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
26 PRINTF_PARSE Function that parses a format string. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
27 Depends on CHAR_T. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
28 STATIC Set to 'static' to declare the function static. |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
29 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */ |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
30 |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
31 #ifndef PRINTF_PARSE |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
32 # include <config.h> |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
33 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 /* Specification. */ |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
36 #ifndef PRINTF_PARSE |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
37 # include "printf-parse.h" |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
38 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
40 /* Default parameters. */ |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
41 #ifndef PRINTF_PARSE |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
42 # define PRINTF_PARSE printf_parse |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
43 # define CHAR_T char |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
44 # define DIRECTIVE char_directive |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
45 # define DIRECTIVES char_directives |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
46 #endif |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
47 |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 /* Get size_t, NULL. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 #include <stddef.h> |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 /* Get intmax_t. */ |
9384
e5e1a09aa484
Tweak to make it usable in gettext's libasprintf.
Bruno Haible <bruno@clisp.org>
parents:
8973
diff
changeset
|
52 #if defined IN_LIBINTL || defined IN_LIBASPRINTF |
8551
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
53 # if HAVE_STDINT_H_WITH_UINTMAX |
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
54 # include <stdint.h> |
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
55 # endif |
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
56 # if HAVE_INTTYPES_H_WITH_UINTMAX |
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
57 # include <inttypes.h> |
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
58 # endif |
a2ed636e1b10
Support 'j' size specifier on all platforms.
Bruno Haible <bruno@clisp.org>
parents:
8051
diff
changeset
|
59 #else |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 # include <stdint.h> |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 /* malloc(), realloc(), free(). */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 #include <stdlib.h> |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
66 /* errno. */ |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
67 #include <errno.h> |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
68 |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
69 /* Checked size_t computations. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
70 #include "xsize.h" |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
71 |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
72 #if CHAR_T_ONLY_ASCII |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
73 /* c_isascii(). */ |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
74 # include "c-ctype.h" |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
75 #endif |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
76 |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 #ifdef STATIC |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 STATIC |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
80 int |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
81 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
82 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
83 const CHAR_T *cp = format; /* pointer into format */ |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
84 size_t arg_posn = 0; /* number of regular arguments consumed */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
85 size_t d_allocated; /* allocated elements of d->dir */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
86 size_t a_allocated; /* allocated elements of a->arg */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
87 size_t max_width_length = 0; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
88 size_t max_precision_length = 0; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
89 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 d->count = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 d_allocated = 1; |
7596
49c1c5e086f4
Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
92 d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 if (d->dir == NULL) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
94 /* Out of memory. */ |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
95 goto out_of_memory_1; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
96 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
97 a->count = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
98 a_allocated = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 a->arg = NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 #define REGISTER_ARG(_index_,_type_) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 { \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
103 size_t n = (_index_); \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
104 if (n >= a_allocated) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
105 { \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
106 size_t memory_size; \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
107 argument *memory; \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
108 \ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
109 a_allocated = xtimes (a_allocated, 2); \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
110 if (a_allocated <= n) \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
111 a_allocated = xsum (n, 1); \ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
112 memory_size = xtimes (a_allocated, sizeof (argument)); \ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
113 if (size_overflow_p (memory_size)) \ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
114 /* Overflow, would lead to out of memory. */ \ |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
115 goto out_of_memory; \ |
7596
49c1c5e086f4
Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
116 memory = (argument *) (a->arg \ |
49c1c5e086f4
Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
117 ? realloc (a->arg, memory_size) \ |
49c1c5e086f4
Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
118 : malloc (memory_size)); \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
119 if (memory == NULL) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
120 /* Out of memory. */ \ |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
121 goto out_of_memory; \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
122 a->arg = memory; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
123 } \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
124 while (a->count <= n) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
125 a->arg[a->count++].type = TYPE_NONE; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
126 if (a->arg[n].type == TYPE_NONE) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
127 a->arg[n].type = (_type_); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
128 else if (a->arg[n].type != (_type_)) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
129 /* Ambiguous type for positional argument. */ \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
130 goto error; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
131 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
132 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
133 while (*cp != '\0') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
134 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
135 CHAR_T c = *cp++; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
136 if (c == '%') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
137 { |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
138 size_t arg_index = ARG_NONE; |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
139 DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
140 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
141 /* Initialize the next directive. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
142 dp->dir_start = cp - 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
143 dp->flags = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
144 dp->width_start = NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
145 dp->width_end = NULL; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
146 dp->width_arg_index = ARG_NONE; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
147 dp->precision_start = NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
148 dp->precision_end = NULL; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
149 dp->precision_arg_index = ARG_NONE; |
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
150 dp->arg_index = ARG_NONE; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
151 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
152 /* Test for positional argument. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
153 if (*cp >= '0' && *cp <= '9') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
154 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
155 const CHAR_T *np; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
156 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
157 for (np = cp; *np >= '0' && *np <= '9'; np++) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
158 ; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
159 if (*np == '$') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
160 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
161 size_t n = 0; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
162 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
163 for (np = cp; *np >= '0' && *np <= '9'; np++) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
164 n = xsum (xtimes (n, 10), *np - '0'); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
165 if (n == 0) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
166 /* Positional argument 0. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
167 goto error; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
168 if (size_overflow_p (n)) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
169 /* n too large, would lead to out of memory later. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
170 goto error; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
171 arg_index = n - 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
172 cp = np + 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
173 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
174 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
175 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
176 /* Read the flags. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
177 for (;;) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
178 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
179 if (*cp == '\'') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
180 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
181 dp->flags |= FLAG_GROUP; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
182 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
183 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
184 else if (*cp == '-') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
185 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
186 dp->flags |= FLAG_LEFT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
187 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
188 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
189 else if (*cp == '+') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
190 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
191 dp->flags |= FLAG_SHOWSIGN; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
192 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
193 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
194 else if (*cp == ' ') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
195 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
196 dp->flags |= FLAG_SPACE; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
197 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
198 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
199 else if (*cp == '#') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
200 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
201 dp->flags |= FLAG_ALT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
202 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
203 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
204 else if (*cp == '0') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
205 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
206 dp->flags |= FLAG_ZERO; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
207 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
208 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
209 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
210 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
211 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
212 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
213 /* Parse the field width. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
214 if (*cp == '*') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
215 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
216 dp->width_start = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
217 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
218 dp->width_end = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
219 if (max_width_length < 1) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
220 max_width_length = 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
221 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
222 /* Test for positional argument. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
223 if (*cp >= '0' && *cp <= '9') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
224 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
225 const CHAR_T *np; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
226 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
227 for (np = cp; *np >= '0' && *np <= '9'; np++) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
228 ; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
229 if (*np == '$') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
230 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
231 size_t n = 0; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
232 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
233 for (np = cp; *np >= '0' && *np <= '9'; np++) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
234 n = xsum (xtimes (n, 10), *np - '0'); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
235 if (n == 0) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
236 /* Positional argument 0. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
237 goto error; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
238 if (size_overflow_p (n)) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
239 /* n too large, would lead to out of memory later. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
240 goto error; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
241 dp->width_arg_index = n - 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
242 cp = np + 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
243 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
244 } |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
245 if (dp->width_arg_index == ARG_NONE) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
246 { |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
247 dp->width_arg_index = arg_posn++; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
248 if (dp->width_arg_index == ARG_NONE) |
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
249 /* arg_posn wrapped around. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
250 goto error; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
251 } |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
252 REGISTER_ARG (dp->width_arg_index, TYPE_INT); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
253 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
254 else if (*cp >= '0' && *cp <= '9') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
255 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
256 size_t width_length; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
257 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
258 dp->width_start = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
259 for (; *cp >= '0' && *cp <= '9'; cp++) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
260 ; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
261 dp->width_end = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
262 width_length = dp->width_end - dp->width_start; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
263 if (max_width_length < width_length) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
264 max_width_length = width_length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
265 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
266 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
267 /* Parse the precision. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
268 if (*cp == '.') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
269 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
270 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
271 if (*cp == '*') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
272 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
273 dp->precision_start = cp - 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
274 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
275 dp->precision_end = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
276 if (max_precision_length < 2) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
277 max_precision_length = 2; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
278 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
279 /* Test for positional argument. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
280 if (*cp >= '0' && *cp <= '9') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
281 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
282 const CHAR_T *np; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
283 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
284 for (np = cp; *np >= '0' && *np <= '9'; np++) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
285 ; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
286 if (*np == '$') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
287 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
288 size_t n = 0; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
289 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
290 for (np = cp; *np >= '0' && *np <= '9'; np++) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
291 n = xsum (xtimes (n, 10), *np - '0'); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
292 if (n == 0) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
293 /* Positional argument 0. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
294 goto error; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
295 if (size_overflow_p (n)) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
296 /* n too large, would lead to out of memory |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
297 later. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
298 goto error; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
299 dp->precision_arg_index = n - 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
300 cp = np + 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
301 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
302 } |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
303 if (dp->precision_arg_index == ARG_NONE) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
304 { |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
305 dp->precision_arg_index = arg_posn++; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
306 if (dp->precision_arg_index == ARG_NONE) |
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
307 /* arg_posn wrapped around. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
308 goto error; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
309 } |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
310 REGISTER_ARG (dp->precision_arg_index, TYPE_INT); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
311 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
312 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
313 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
314 size_t precision_length; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
315 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
316 dp->precision_start = cp - 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
317 for (; *cp >= '0' && *cp <= '9'; cp++) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
318 ; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
319 dp->precision_end = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
320 precision_length = dp->precision_end - dp->precision_start; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
321 if (max_precision_length < precision_length) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
322 max_precision_length = precision_length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
323 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
324 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
325 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
326 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
327 arg_type type; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
328 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
329 /* Parse argument type/size specifiers. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
330 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
331 int flags = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
332 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
333 for (;;) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
334 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
335 if (*cp == 'h') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
336 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
337 flags |= (1 << (flags & 1)); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
338 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
339 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
340 else if (*cp == 'L') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
341 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
342 flags |= 4; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
343 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
344 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
345 else if (*cp == 'l') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
346 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
347 flags += 8; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
348 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
349 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
350 else if (*cp == 'j') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
351 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
352 if (sizeof (intmax_t) > sizeof (long)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
353 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
354 /* intmax_t = long long */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
355 flags += 16; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
356 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
357 else if (sizeof (intmax_t) > sizeof (int)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
358 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
359 /* intmax_t = long */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
360 flags += 8; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
361 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
362 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
363 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
364 else if (*cp == 'z' || *cp == 'Z') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
365 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
366 /* 'z' is standardized in ISO C 99, but glibc uses 'Z' |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
367 because the warning facility in gcc-2.95.2 understands |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
368 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
369 if (sizeof (size_t) > sizeof (long)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
370 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
371 /* size_t = long long */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
372 flags += 16; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
373 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
374 else if (sizeof (size_t) > sizeof (int)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
375 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
376 /* size_t = long */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
377 flags += 8; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
378 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
379 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
380 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
381 else if (*cp == 't') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
382 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
383 if (sizeof (ptrdiff_t) > sizeof (long)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
384 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
385 /* ptrdiff_t = long long */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
386 flags += 16; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
387 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
388 else if (sizeof (ptrdiff_t) > sizeof (int)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
389 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
390 /* ptrdiff_t = long */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
391 flags += 8; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
392 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
393 cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
394 } |
9588
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
395 #if defined __APPLE__ && defined __MACH__ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
396 /* On MacOS X 10.3, PRIdMAX is defined as "qd". |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
397 We cannot change it to "lld" because PRIdMAX must also |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
398 be understood by the system's printf routines. */ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
399 else if (*cp == 'q') |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
400 { |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
401 if (64 / 8 > sizeof (long)) |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
402 { |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
403 /* int64_t = long long */ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
404 flags += 16; |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
405 } |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
406 else |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
407 { |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
408 /* int64_t = long */ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
409 flags += 8; |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
410 } |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
411 cp++; |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
412 } |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
413 #endif |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
414 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
415 /* On native Win32, PRIdMAX is defined as "I64d". |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
416 We cannot change it to "lld" because PRIdMAX must also |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
417 be understood by the system's printf routines. */ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
418 else if (*cp == 'I' && cp[1] == '6' && cp[2] == '4') |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
419 { |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
420 if (64 / 8 > sizeof (long)) |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
421 { |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
422 /* __int64 = long long */ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
423 flags += 16; |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
424 } |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
425 else |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
426 { |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
427 /* __int64 = long */ |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
428 flags += 8; |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
429 } |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
430 cp += 3; |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
431 } |
82384b8e3c31
Handle the particular PRIdMAX values on MacOS X and mingw.
Bruno Haible <bruno@clisp.org>
parents:
9438
diff
changeset
|
432 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
433 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
434 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
435 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
436 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
437 /* Read the conversion character. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
438 c = *cp++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
439 switch (c) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
440 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
441 case 'd': case 'i': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
442 #if HAVE_LONG_LONG_INT |
7475
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
443 /* If 'long long' exists and is larger than 'long': */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
444 if (flags >= 16 || (flags & 4)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
445 type = TYPE_LONGLONGINT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
446 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
447 #endif |
7475
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
448 /* If 'long long' exists and is the same as 'long', we parse |
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
449 "lld" into TYPE_LONGINT. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
450 if (flags >= 8) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
451 type = TYPE_LONGINT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
452 else if (flags & 2) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
453 type = TYPE_SCHAR; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
454 else if (flags & 1) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
455 type = TYPE_SHORT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
456 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
457 type = TYPE_INT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
458 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
459 case 'o': case 'u': case 'x': case 'X': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
460 #if HAVE_LONG_LONG_INT |
7475
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
461 /* If 'long long' exists and is larger than 'long': */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
462 if (flags >= 16 || (flags & 4)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
463 type = TYPE_ULONGLONGINT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
464 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
465 #endif |
7475
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
466 /* If 'unsigned long long' exists and is the same as |
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
467 'unsigned long', we parse "llu" into TYPE_ULONGINT. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
468 if (flags >= 8) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
469 type = TYPE_ULONGINT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
470 else if (flags & 2) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
471 type = TYPE_UCHAR; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
472 else if (flags & 1) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
473 type = TYPE_USHORT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
474 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
475 type = TYPE_UINT; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
476 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
477 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
478 case 'a': case 'A': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
479 if (flags >= 16 || (flags & 4)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
480 type = TYPE_LONGDOUBLE; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
481 else |
8648
359d135f748c
Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents:
8551
diff
changeset
|
482 type = TYPE_DOUBLE; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
483 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
484 case 'c': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
485 if (flags >= 8) |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
486 #if HAVE_WINT_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
487 type = TYPE_WIDE_CHAR; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
488 #else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
489 goto error; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
490 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
491 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
492 type = TYPE_CHAR; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
493 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
494 #if HAVE_WINT_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
495 case 'C': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
496 type = TYPE_WIDE_CHAR; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
497 c = 'c'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
498 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
499 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
500 case 's': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
501 if (flags >= 8) |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
502 #if HAVE_WCHAR_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
503 type = TYPE_WIDE_STRING; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
504 #else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
505 goto error; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
506 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
507 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
508 type = TYPE_STRING; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
509 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
510 #if HAVE_WCHAR_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
511 case 'S': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
512 type = TYPE_WIDE_STRING; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
513 c = 's'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
514 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
515 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
516 case 'p': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
517 type = TYPE_POINTER; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
518 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
519 case 'n': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7596
diff
changeset
|
520 #if HAVE_LONG_LONG_INT |
7475
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
521 /* If 'long long' exists and is larger than 'long': */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
522 if (flags >= 16 || (flags & 4)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
523 type = TYPE_COUNT_LONGLONGINT_POINTER; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
524 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
525 #endif |
7475
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
526 /* If 'long long' exists and is the same as 'long', we parse |
99b9b91e548c
Use HAVE_LONG_LONG_INT instead of HAVE_LONG_LONG.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
527 "lln" into TYPE_COUNT_LONGINT_POINTER. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
528 if (flags >= 8) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
529 type = TYPE_COUNT_LONGINT_POINTER; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
530 else if (flags & 2) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
531 type = TYPE_COUNT_SCHAR_POINTER; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
532 else if (flags & 1) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
533 type = TYPE_COUNT_SHORT_POINTER; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
534 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
535 type = TYPE_COUNT_INT_POINTER; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
536 break; |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
537 #if ENABLE_UNISTDIO |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
538 /* The unistdio extensions. */ |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
539 case 'U': |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
540 if (flags >= 16) |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
541 type = TYPE_U32_STRING; |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
542 else if (flags >= 8) |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
543 type = TYPE_U16_STRING; |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
544 else |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
545 type = TYPE_U8_STRING; |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
546 break; |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
547 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
548 case '%': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
549 type = TYPE_NONE; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
550 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
551 default: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
552 /* Unknown conversion character. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
553 goto error; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
554 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
555 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
556 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
557 if (type != TYPE_NONE) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
558 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
559 dp->arg_index = arg_index; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
560 if (dp->arg_index == ARG_NONE) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
561 { |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
562 dp->arg_index = arg_posn++; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
563 if (dp->arg_index == ARG_NONE) |
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
564 /* arg_posn wrapped around. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
565 goto error; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
566 } |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
567 REGISTER_ARG (dp->arg_index, type); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
568 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
569 dp->conversion = c; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
570 dp->dir_end = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
571 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
572 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
573 d->count++; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
574 if (d->count >= d_allocated) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
575 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
576 size_t memory_size; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
577 DIRECTIVE *memory; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
578 |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
579 d_allocated = xtimes (d_allocated, 2); |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
580 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE)); |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
581 if (size_overflow_p (memory_size)) |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
582 /* Overflow, would lead to out of memory. */ |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
583 goto out_of_memory; |
7596
49c1c5e086f4
Make it compile in C++ mode.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
584 memory = (DIRECTIVE *) realloc (d->dir, memory_size); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
585 if (memory == NULL) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
586 /* Out of memory. */ |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
587 goto out_of_memory; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
588 d->dir = memory; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
589 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
590 } |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
591 #if CHAR_T_ONLY_ASCII |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
592 else if (!c_isascii (c)) |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
593 { |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
594 /* Non-ASCII character. Not supported. */ |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
595 goto error; |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
596 } |
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
597 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
598 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
599 d->dir[d->count].dir_start = cp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
600 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
601 d->max_width_length = max_width_length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
602 d->max_precision_length = max_precision_length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
603 return 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
604 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
605 error: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
606 if (a->arg) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
607 free (a->arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
608 if (d->dir) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
609 free (d->dir); |
9438
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
610 errno = EINVAL; |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
611 return -1; |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
612 |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
613 out_of_memory: |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
614 if (a->arg) |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
615 free (a->arg); |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
616 if (d->dir) |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
617 free (d->dir); |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
618 out_of_memory_1: |
290aa46b4526
Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents:
9384
diff
changeset
|
619 errno = ENOMEM; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
620 return -1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
621 } |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
622 |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
623 #undef PRINTF_PARSE |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
624 #undef DIRECTIVES |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
625 #undef DIRECTIVE |
8973
7df86a1985e4
Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents:
8648
diff
changeset
|
626 #undef CHAR_T_ONLY_ASCII |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
627 #undef CHAR_T |