Commit | Line | Data |
---|---|---|
24f98398 | 1 | /* Output like sprintf to a buffer of specified size. |
762b15be EZ |
2 | Also takes args differently: pass one pointer to the end |
3 | of the format string in addition to the format string itself. | |
73b0cd50 | 4 | Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc. |
24f98398 JA |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
24f98398 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
24f98398 JA |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
24f98398 | 20 | |
762b15be EZ |
21 | /* If you think about replacing this with some similar standard C function of |
22 | the printf family (such as vsnprintf), please note that this function | |
23 | supports the following Emacs-specific features: | |
24 | ||
25 | . For %c conversions, it produces a string with the multibyte representation | |
26 | of the (`int') argument, suitable for display in an Emacs buffer. | |
27 | ||
28 | . For %s and %c, when field width is specified (e.g., %25s), it accounts for | |
29 | the diplay width of each character, according to char-width-table. That | |
30 | is, it does not assume that each character takes one column on display. | |
31 | ||
32 | . If the size of the buffer is not enough to produce the formatted string in | |
33 | its entirety, it makes sure that truncation does not chop the last | |
34 | character in the middle of its multibyte sequence, producing an invalid | |
35 | sequence. | |
36 | ||
37 | . It accepts a pointer to the end of the format string, so the format string | |
38 | could include embedded null characters. | |
39 | ||
40 | . It signals an error if the length of the formatted string is about to | |
41 | overflow MOST_POSITIVE_FIXNUM, to avoid producing strings longer than what | |
42 | Emacs can handle. | |
43 | ||
44 | OTOH, this function supports only a small subset of the standard C formatted | |
45 | output facilities. E.g., %u and %ll are not supported, and precision is | |
825cd63c EZ |
46 | ignored %s and %c conversions. (See below for the detailed documentation of |
47 | what is supported.) However, this is okay, as this function is supposed to | |
48 | be called from `error' and similar functions, and thus does not need to | |
49 | support features beyond those in `Fformat', which is used by `error' on the | |
50 | Lisp level. */ | |
51 | ||
52 | /* This function supports the following %-sequences in the `format' | |
53 | argument: | |
54 | ||
55 | %s means print a string argument. | |
56 | %S is silently treated as %s, for loose compatibility with `Fformat'. | |
57 | %d means print a `signed int' argument in decimal. | |
825cd63c EZ |
58 | %o means print an `unsigned int' argument in octal. |
59 | %x means print an `unsigned int' argument in hex. | |
60 | %e means print a `double' argument in exponential notation. | |
61 | %f means print a `double' argument in decimal-point notation. | |
62 | %g means print a `double' argument in exponential notation | |
63 | or in decimal-point notation, whichever uses fewer characters. | |
64 | %c means print a `signed int' argument as a single character. | |
65 | %% means produce a literal % character. | |
66 | ||
94dcfacf EZ |
67 | A %-sequence may contain optional flag, width, and precision specifiers, and |
68 | a length modifier, as follows: | |
825cd63c | 69 | |
94dcfacf | 70 | %<flags><width><precision><length>character |
825cd63c | 71 | |
b71a1728 | 72 | where flags is [+ -0], width is [0-9]+, precision is .[0-9]+, and length |
ad5f9eea PE |
73 | is empty or l or the value of the pI macro. Also, %% in a format |
74 | stands for a single % in the output. A % that does not introduce a | |
75 | valid %-sequence causes undefined behavior. | |
825cd63c EZ |
76 | |
77 | The + flag character inserts a + before any positive number, while a space | |
94dcfacf EZ |
78 | inserts a space before any positive number; these flags only affect %d, %o, |
79 | %x, %e, %f, and %g sequences. The - and 0 flags affect the width specifier, | |
80 | as described below. For signed numerical arguments only, the ` ' (space) | |
81 | flag causes the result to be prefixed with a space character if it does not | |
82 | start with a sign (+ or -). | |
83 | ||
84 | The l (lower-case letter ell) length modifier is a `long' data type | |
85 | modifier: it is supported for %d, %o, and %x conversions of integral | |
54b8e3f7 | 86 | arguments, must immediately precede the conversion specifier, and means that |
94dcfacf | 87 | the respective argument is to be treated as `long int' or `unsigned long |
ad5f9eea PE |
88 | int'. Similarly, the value of the pI macro means to use EMACS_INT or |
89 | EMACS_UINT and the empty length modifier means `int' or `unsigned int'. | |
825cd63c EZ |
90 | |
91 | The width specifier supplies a lower limit for the length of the printed | |
92 | representation. The padding, if any, normally goes on the left, but it goes | |
93 | on the right if the - flag is present. The padding character is normally a | |
94 | space, but (for numerical arguments only) it is 0 if the 0 flag is present. | |
95 | The - flag takes precedence over the 0 flag. | |
96 | ||
97 | For %e, %f, and %g sequences, the number after the "." in the precision | |
98 | specifier says how many decimal places to show; if zero, the decimal point | |
99 | itself is omitted. For %s and %S, the precision specifier is ignored. */ | |
24f98398 | 100 | |
6d291527 | 101 | #include <config.h> |
24f98398 JA |
102 | #include <stdio.h> |
103 | #include <ctype.h> | |
d7306fe6 | 104 | #include <setjmp.h> |
24f98398 | 105 | |
cf09633a | 106 | #ifdef STDC_HEADERS |
be65c2f4 PE |
107 | #include <float.h> |
108 | #endif | |
109 | ||
48236137 | 110 | #include <unistd.h> |
48236137 | 111 | |
e6c3da20 | 112 | #include <limits.h> |
e6c3da20 | 113 | |
523e9291 RS |
114 | #include "lisp.h" |
115 | ||
a0ca925c KH |
116 | /* Since we use the macro CHAR_HEAD_P, we have to include this, but |
117 | don't have to include others because CHAR_HEAD_P does not contains | |
118 | another macro. */ | |
83be827a | 119 | #include "character.h" |
a0ca925c | 120 | |
92bc9a36 DN |
121 | #ifndef DBL_MAX_10_EXP |
122 | #define DBL_MAX_10_EXP 308 /* IEEE double */ | |
123 | #endif | |
124 | ||
f4c730d3 RS |
125 | /* Generate output from a format-spec FORMAT, |
126 | terminated at position FORMAT_END. | |
f40b429d | 127 | (*FORMAT_END is not part of the format, but must exist and be readable.) |
f4c730d3 | 128 | Output goes in BUFFER, which has room for BUFSIZE chars. |
f40b429d PE |
129 | BUFSIZE must be positive. If the output does not fit, truncate it |
130 | to fit and return BUFSIZE - 1; if this truncates a multibyte | |
131 | sequence, store '\0' into the sequence's first byte. | |
825cd63c EZ |
132 | Returns the number of bytes stored into BUFFER, excluding |
133 | the terminating null byte. Output is always null-terminated. | |
1513af9e RS |
134 | String arguments are passed as C strings. |
135 | Integers are passed as C integers. */ | |
f4c730d3 | 136 | |
e6c3da20 EZ |
137 | size_t |
138 | doprnt (char *buffer, register size_t bufsize, const char *format, | |
a8fe7202 | 139 | const char *format_end, va_list ap) |
24f98398 | 140 | { |
a8fe7202 | 141 | const char *fmt = format; /* Pointer into format string */ |
24f98398 | 142 | register char *bufptr = buffer; /* Pointer into output buffer.. */ |
03383aaf | 143 | |
f4c730d3 | 144 | /* Use this for sprintf unless we need something really big. */ |
be65c2f4 | 145 | char tembuf[DBL_MAX_10_EXP + 100]; |
03383aaf | 146 | |
f4c730d3 | 147 | /* Size of sprintf_buffer. */ |
e6c3da20 | 148 | size_t size_allocated = sizeof (tembuf); |
03383aaf | 149 | |
f4c730d3 RS |
150 | /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */ |
151 | char *sprintf_buffer = tembuf; | |
03383aaf | 152 | |
f4c730d3 | 153 | /* Buffer we have got with malloc. */ |
e6c3da20 | 154 | char *big_buffer = NULL; |
03383aaf | 155 | |
e6c3da20 | 156 | register size_t tem; |
7469ef5d | 157 | char *string; |
03383aaf BF |
158 | char fixed_buffer[20]; /* Default buffer for small formatting. */ |
159 | char *fmtcpy; | |
24f98398 | 160 | int minlen; |
7469ef5d | 161 | char charbuf[MAX_MULTIBYTE_LENGTH + 1]; /* Used for %c. */ |
825cd63c | 162 | USE_SAFE_ALLOCA; |
24f98398 JA |
163 | |
164 | if (format_end == 0) | |
165 | format_end = format + strlen (format); | |
166 | ||
03383aaf BF |
167 | if ((format_end - format + 1) < sizeof (fixed_buffer)) |
168 | fmtcpy = fixed_buffer; | |
169 | else | |
825cd63c | 170 | SAFE_ALLOCA (fmtcpy, char *, format_end - format + 1); |
03383aaf | 171 | |
24f98398 | 172 | bufsize--; |
03383aaf BF |
173 | |
174 | /* Loop until end of format string or buffer full. */ | |
94dcfacf | 175 | while (fmt < format_end && bufsize > 0) |
24f98398 JA |
176 | { |
177 | if (*fmt == '%') /* Check for a '%' character */ | |
178 | { | |
e6c3da20 EZ |
179 | size_t size_bound = 0; |
180 | EMACS_INT width; /* Columns occupied by STRING on display. */ | |
181 | int long_flag = 0; | |
ad5f9eea | 182 | int pIlen = sizeof pI - 1; |
f4c730d3 | 183 | |
24f98398 | 184 | fmt++; |
d427b66a | 185 | /* Copy this one %-spec into fmtcpy. */ |
7469ef5d | 186 | string = fmtcpy; |
24f98398 | 187 | *string++ = '%'; |
94dcfacf | 188 | while (fmt < format_end) |
24f98398 JA |
189 | { |
190 | *string++ = *fmt; | |
be65c2f4 PE |
191 | if ('0' <= *fmt && *fmt <= '9') |
192 | { | |
193 | /* Get an idea of how much space we might need. | |
194 | This might be a field width or a precision; e.g. | |
195 | %1.1000f and %1000.1f both might need 1000+ bytes. | |
196 | Parse the width or precision, checking for overflow. */ | |
e6c3da20 | 197 | size_t n = *fmt - '0'; |
ad5f9eea | 198 | while (fmt + 1 < format_end |
94dcfacf | 199 | && '0' <= fmt[1] && fmt[1] <= '9') |
be65c2f4 | 200 | { |
ca2d6274 PE |
201 | /* Avoid size_t overflow. Avoid int overflow too, as |
202 | many sprintfs mishandle widths greater than INT_MAX. | |
203 | This test is simple but slightly conservative: e.g., | |
204 | (INT_MAX - INT_MAX % 10) is reported as an overflow | |
205 | even when it's not. */ | |
f76dee0c | 206 | if (n >= min (INT_MAX, SIZE_MAX) / 10) |
be65c2f4 | 207 | error ("Format width or precision too large"); |
26898943 | 208 | n = n * 10 + fmt[1] - '0'; |
be65c2f4 PE |
209 | *string++ = *++fmt; |
210 | } | |
211 | ||
212 | if (size_bound < n) | |
213 | size_bound = n; | |
214 | } | |
ad5f9eea PE |
215 | else if (! (*fmt == '-' || *fmt == ' ' || *fmt == '.' |
216 | || *fmt == '+')) | |
24f98398 JA |
217 | break; |
218 | fmt++; | |
219 | } | |
ad5f9eea PE |
220 | |
221 | if (0 < pIlen && pIlen <= format_end - fmt | |
222 | && memcmp (fmt, pI, pIlen) == 0) | |
223 | { | |
224 | long_flag = 2; | |
225 | memcpy (string, fmt + 1, pIlen); | |
226 | string += pIlen; | |
227 | fmt += pIlen; | |
228 | } | |
229 | else if (fmt < format_end && *fmt == 'l') | |
230 | { | |
231 | long_flag = 1; | |
232 | *string++ = *++fmt; | |
233 | } | |
24f98398 | 234 | *string = 0; |
03383aaf | 235 | |
be65c2f4 PE |
236 | /* Make the size bound large enough to handle floating point formats |
237 | with large numbers. */ | |
e6c3da20 | 238 | if (size_bound > SIZE_MAX - DBL_MAX_10_EXP - 50) |
be65c2f4 | 239 | error ("Format width or precision too large"); |
01769a73 | 240 | size_bound += DBL_MAX_10_EXP + 50; |
6e951728 | 241 | |
f4c730d3 RS |
242 | /* Make sure we have that much. */ |
243 | if (size_bound > size_allocated) | |
244 | { | |
245 | if (big_buffer) | |
94dcfacf EZ |
246 | xfree (big_buffer); |
247 | big_buffer = (char *) xmalloc (size_bound); | |
f4c730d3 RS |
248 | sprintf_buffer = big_buffer; |
249 | size_allocated = size_bound; | |
250 | } | |
24f98398 JA |
251 | minlen = 0; |
252 | switch (*fmt++) | |
253 | { | |
254 | default: | |
ad5f9eea | 255 | error ("Invalid format operation %s", fmtcpy); |
24f98398 JA |
256 | |
257 | /* case 'b': */ | |
e6c3da20 | 258 | case 'l': |
24f98398 | 259 | case 'd': |
e6c3da20 EZ |
260 | { |
261 | int i; | |
262 | long l; | |
263 | ||
e810457d PE |
264 | if (1 < long_flag) |
265 | { | |
ad5f9eea | 266 | EMACS_INT ll = va_arg (ap, EMACS_INT); |
e810457d | 267 | sprintf (sprintf_buffer, fmtcpy, ll); |
e810457d PE |
268 | } |
269 | else if (long_flag) | |
e6c3da20 EZ |
270 | { |
271 | l = va_arg(ap, long); | |
272 | sprintf (sprintf_buffer, fmtcpy, l); | |
273 | } | |
274 | else | |
275 | { | |
276 | i = va_arg(ap, int); | |
277 | sprintf (sprintf_buffer, fmtcpy, i); | |
278 | } | |
279 | /* Now copy into final output, truncating as necessary. */ | |
280 | string = sprintf_buffer; | |
281 | goto doit; | |
282 | } | |
283 | ||
24f98398 JA |
284 | case 'o': |
285 | case 'x': | |
e6c3da20 EZ |
286 | { |
287 | unsigned u; | |
288 | unsigned long ul; | |
289 | ||
e810457d PE |
290 | if (1 < long_flag) |
291 | { | |
ad5f9eea | 292 | EMACS_UINT ull = va_arg (ap, EMACS_UINT); |
e810457d | 293 | sprintf (sprintf_buffer, fmtcpy, ull); |
e810457d PE |
294 | } |
295 | else if (long_flag) | |
e6c3da20 EZ |
296 | { |
297 | ul = va_arg(ap, unsigned long); | |
298 | sprintf (sprintf_buffer, fmtcpy, ul); | |
299 | } | |
300 | else | |
301 | { | |
302 | u = va_arg(ap, unsigned); | |
303 | sprintf (sprintf_buffer, fmtcpy, u); | |
304 | } | |
305 | /* Now copy into final output, truncating as necessary. */ | |
306 | string = sprintf_buffer; | |
307 | goto doit; | |
308 | } | |
24f98398 | 309 | |
f4c730d3 RS |
310 | case 'f': |
311 | case 'e': | |
312 | case 'g': | |
313 | { | |
6a8033e1 KR |
314 | double d = va_arg(ap, double); |
315 | sprintf (sprintf_buffer, fmtcpy, d); | |
e6c3da20 | 316 | /* Now copy into final output, truncating as necessary. */ |
7469ef5d | 317 | string = sprintf_buffer; |
f4c730d3 RS |
318 | goto doit; |
319 | } | |
320 | ||
24f98398 JA |
321 | case 'S': |
322 | string[-1] = 's'; | |
323 | case 's': | |
24f98398 JA |
324 | if (fmtcpy[1] != 's') |
325 | minlen = atoi (&fmtcpy[1]); | |
7469ef5d | 326 | string = va_arg (ap, char *); |
e267324c | 327 | tem = strlen (string); |
c9d624c6 | 328 | if (STRING_BYTES_BOUND < tem) |
e6c3da20 | 329 | error ("String for %%s or %%S format is too long"); |
a0ca925c | 330 | width = strwidth (string, tem); |
1513af9e RS |
331 | goto doit1; |
332 | ||
24f98398 JA |
333 | /* Copy string into final output, truncating if no room. */ |
334 | doit: | |
a0ca925c | 335 | /* Coming here means STRING contains ASCII only. */ |
e6c3da20 | 336 | tem = strlen (string); |
c9d624c6 | 337 | if (STRING_BYTES_BOUND < tem) |
e6c3da20 EZ |
338 | error ("Format width or precision too large"); |
339 | width = tem; | |
35a65fce | 340 | doit1: |
a0ca925c KH |
341 | /* We have already calculated: |
342 | TEM -- length of STRING, | |
343 | WIDTH -- columns occupied by STRING when displayed, and | |
344 | MINLEN -- minimum columns of the output. */ | |
24f98398 JA |
345 | if (minlen > 0) |
346 | { | |
a0ca925c | 347 | while (minlen > width && bufsize > 0) |
24f98398 JA |
348 | { |
349 | *bufptr++ = ' '; | |
350 | bufsize--; | |
351 | minlen--; | |
352 | } | |
353 | minlen = 0; | |
354 | } | |
355 | if (tem > bufsize) | |
a0ca925c KH |
356 | { |
357 | /* Truncate the string at character boundary. */ | |
358 | tem = bufsize; | |
a50545d9 | 359 | while (!CHAR_HEAD_P (string[tem - 1])) tem--; |
afda1437 EZ |
360 | /* If the multibyte sequence of this character is |
361 | too long for the space we have left in the | |
362 | buffer, truncate before it. */ | |
363 | if (tem > 0 | |
364 | && BYTES_BY_CHAR_HEAD (string[tem - 1]) > bufsize) | |
365 | tem--; | |
366 | if (tem > 0) | |
367 | memcpy (bufptr, string, tem); | |
368 | bufptr[tem] = 0; | |
369 | /* Trigger exit from the loop, but make sure we | |
370 | return to the caller a value which will indicate | |
371 | that the buffer was too small. */ | |
372 | bufptr += bufsize; | |
373 | bufsize = 0; | |
374 | continue; | |
a0ca925c KH |
375 | } |
376 | else | |
72af86bd | 377 | memcpy (bufptr, string, tem); |
24f98398 JA |
378 | bufptr += tem; |
379 | bufsize -= tem; | |
380 | if (minlen < 0) | |
381 | { | |
a0ca925c | 382 | while (minlen < - width && bufsize > 0) |
24f98398 JA |
383 | { |
384 | *bufptr++ = ' '; | |
385 | bufsize--; | |
386 | minlen++; | |
387 | } | |
388 | minlen = 0; | |
389 | } | |
390 | continue; | |
391 | ||
392 | case 'c': | |
6a8033e1 | 393 | { |
e6c3da20 EZ |
394 | int chr = va_arg(ap, int); |
395 | tem = CHAR_STRING (chr, (unsigned char *) charbuf); | |
6a8033e1 KR |
396 | string = charbuf; |
397 | string[tem] = 0; | |
398 | width = strwidth (string, tem); | |
399 | if (fmtcpy[1] != 'c') | |
400 | minlen = atoi (&fmtcpy[1]); | |
401 | goto doit1; | |
402 | } | |
24f98398 JA |
403 | |
404 | case '%': | |
405 | fmt--; /* Drop thru and this % will be treated as normal */ | |
406 | } | |
407 | } | |
a0ca925c KH |
408 | |
409 | { | |
410 | /* Just some character; Copy it if the whole multi-byte form | |
411 | fit in the buffer. */ | |
412 | char *save_bufptr = bufptr; | |
413 | ||
414 | do { *bufptr++ = *fmt++; } | |
94dcfacf | 415 | while (fmt < format_end && --bufsize > 0 && !CHAR_HEAD_P (*fmt)); |
a50545d9 | 416 | if (!CHAR_HEAD_P (*fmt)) |
a0ca925c | 417 | { |
d178f871 EZ |
418 | /* Truncate, but return value that will signal to caller |
419 | that the buffer was too small. */ | |
420 | *save_bufptr = 0; | |
a0ca925c KH |
421 | break; |
422 | } | |
423 | } | |
24f98398 JA |
424 | }; |
425 | ||
f4c730d3 | 426 | /* If we had to malloc something, free it. */ |
70fdbb46 | 427 | xfree (big_buffer); |
f4c730d3 | 428 | |
e6c3da20 | 429 | *bufptr = 0; /* Make sure our string ends with a '\0' */ |
825cd63c EZ |
430 | |
431 | SAFE_FREE (); | |
24f98398 JA |
432 | return bufptr - buffer; |
433 | } |