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. | |
58 | %l means print a `long int' argument in decimal. | |
59 | %o means print an `unsigned int' argument in octal. | |
60 | %x means print an `unsigned int' argument in hex. | |
61 | %e means print a `double' argument in exponential notation. | |
62 | %f means print a `double' argument in decimal-point notation. | |
63 | %g means print a `double' argument in exponential notation | |
64 | or in decimal-point notation, whichever uses fewer characters. | |
65 | %c means print a `signed int' argument as a single character. | |
66 | %% means produce a literal % character. | |
67 | ||
68 | A %-sequence may contain optional flag, width, and precision specifiers, as | |
69 | follows: | |
70 | ||
71 | %<flags><width><precision>character | |
72 | ||
73 | where flags is [+ -0l], width is [0-9]+, and precision is .[0-9]+ | |
74 | ||
75 | The + flag character inserts a + before any positive number, while a space | |
76 | inserts a space before any positive number; these flags only affect %d, %l, | |
77 | %o, %x, %e, %f, and %g sequences. The - and 0 flags affect the width | |
78 | specifier, as described below. | |
79 | ||
80 | The l (lower-case letter ell) flag is a `long' data type modifier: it is | |
81 | supported for %d, %o, and %x conversions of integral arguments, and means | |
82 | that the respective argument is to be treated as `long int' or `unsigned | |
83 | long int'. The EMACS_INT data type should use this modifier. | |
84 | ||
85 | The width specifier supplies a lower limit for the length of the printed | |
86 | representation. The padding, if any, normally goes on the left, but it goes | |
87 | on the right if the - flag is present. The padding character is normally a | |
88 | space, but (for numerical arguments only) it is 0 if the 0 flag is present. | |
89 | The - flag takes precedence over the 0 flag. | |
90 | ||
91 | For %e, %f, and %g sequences, the number after the "." in the precision | |
92 | specifier says how many decimal places to show; if zero, the decimal point | |
93 | itself is omitted. For %s and %S, the precision specifier is ignored. */ | |
24f98398 | 94 | |
6d291527 | 95 | #include <config.h> |
24f98398 JA |
96 | #include <stdio.h> |
97 | #include <ctype.h> | |
d7306fe6 | 98 | #include <setjmp.h> |
24f98398 | 99 | |
cf09633a | 100 | #ifdef STDC_HEADERS |
be65c2f4 PE |
101 | #include <float.h> |
102 | #endif | |
103 | ||
48236137 | 104 | #include <unistd.h> |
48236137 | 105 | |
e6c3da20 EZ |
106 | #include <limits.h> |
107 | #ifndef SIZE_MAX | |
108 | # define SIZE_MAX ((size_t) -1) | |
109 | #endif | |
110 | ||
523e9291 RS |
111 | #include "lisp.h" |
112 | ||
a0ca925c KH |
113 | /* Since we use the macro CHAR_HEAD_P, we have to include this, but |
114 | don't have to include others because CHAR_HEAD_P does not contains | |
115 | another macro. */ | |
83be827a | 116 | #include "character.h" |
a0ca925c | 117 | |
92bc9a36 DN |
118 | #ifndef DBL_MAX_10_EXP |
119 | #define DBL_MAX_10_EXP 308 /* IEEE double */ | |
120 | #endif | |
121 | ||
f4c730d3 RS |
122 | /* Generate output from a format-spec FORMAT, |
123 | terminated at position FORMAT_END. | |
124 | Output goes in BUFFER, which has room for BUFSIZE chars. | |
125 | If the output does not fit, truncate it to fit. | |
825cd63c EZ |
126 | Returns the number of bytes stored into BUFFER, excluding |
127 | the terminating null byte. Output is always null-terminated. | |
1513af9e RS |
128 | String arguments are passed as C strings. |
129 | Integers are passed as C integers. */ | |
f4c730d3 | 130 | |
e6c3da20 EZ |
131 | size_t |
132 | doprnt (char *buffer, register size_t bufsize, const char *format, | |
a8fe7202 | 133 | const char *format_end, va_list ap) |
24f98398 | 134 | { |
a8fe7202 | 135 | const char *fmt = format; /* Pointer into format string */ |
24f98398 | 136 | register char *bufptr = buffer; /* Pointer into output buffer.. */ |
03383aaf | 137 | |
f4c730d3 | 138 | /* Use this for sprintf unless we need something really big. */ |
be65c2f4 | 139 | char tembuf[DBL_MAX_10_EXP + 100]; |
03383aaf | 140 | |
f4c730d3 | 141 | /* Size of sprintf_buffer. */ |
e6c3da20 | 142 | size_t size_allocated = sizeof (tembuf); |
03383aaf | 143 | |
f4c730d3 RS |
144 | /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */ |
145 | char *sprintf_buffer = tembuf; | |
03383aaf | 146 | |
f4c730d3 | 147 | /* Buffer we have got with malloc. */ |
e6c3da20 | 148 | char *big_buffer = NULL; |
03383aaf | 149 | |
e6c3da20 | 150 | register size_t tem; |
7469ef5d | 151 | char *string; |
03383aaf BF |
152 | char fixed_buffer[20]; /* Default buffer for small formatting. */ |
153 | char *fmtcpy; | |
24f98398 | 154 | int minlen; |
7469ef5d | 155 | char charbuf[MAX_MULTIBYTE_LENGTH + 1]; /* Used for %c. */ |
825cd63c | 156 | USE_SAFE_ALLOCA; |
24f98398 JA |
157 | |
158 | if (format_end == 0) | |
159 | format_end = format + strlen (format); | |
160 | ||
03383aaf BF |
161 | if ((format_end - format + 1) < sizeof (fixed_buffer)) |
162 | fmtcpy = fixed_buffer; | |
163 | else | |
825cd63c | 164 | SAFE_ALLOCA (fmtcpy, char *, format_end - format + 1); |
03383aaf | 165 | |
24f98398 | 166 | bufsize--; |
03383aaf BF |
167 | |
168 | /* Loop until end of format string or buffer full. */ | |
169 | while (fmt != format_end && bufsize > 0) | |
24f98398 JA |
170 | { |
171 | if (*fmt == '%') /* Check for a '%' character */ | |
172 | { | |
e6c3da20 EZ |
173 | size_t size_bound = 0; |
174 | EMACS_INT width; /* Columns occupied by STRING on display. */ | |
175 | int long_flag = 0; | |
f4c730d3 | 176 | |
24f98398 | 177 | fmt++; |
d427b66a | 178 | /* Copy this one %-spec into fmtcpy. */ |
7469ef5d | 179 | string = fmtcpy; |
24f98398 | 180 | *string++ = '%'; |
03383aaf | 181 | while (1) |
24f98398 JA |
182 | { |
183 | *string++ = *fmt; | |
be65c2f4 PE |
184 | if ('0' <= *fmt && *fmt <= '9') |
185 | { | |
186 | /* Get an idea of how much space we might need. | |
187 | This might be a field width or a precision; e.g. | |
188 | %1.1000f and %1000.1f both might need 1000+ bytes. | |
189 | Parse the width or precision, checking for overflow. */ | |
e6c3da20 | 190 | size_t n = *fmt - '0'; |
be65c2f4 PE |
191 | while ('0' <= fmt[1] && fmt[1] <= '9') |
192 | { | |
e6c3da20 EZ |
193 | if (n >= SIZE_MAX / 10 |
194 | || n * 10 > SIZE_MAX - (fmt[1] - '0')) | |
be65c2f4 | 195 | error ("Format width or precision too large"); |
26898943 | 196 | n = n * 10 + fmt[1] - '0'; |
be65c2f4 PE |
197 | *string++ = *++fmt; |
198 | } | |
199 | ||
200 | if (size_bound < n) | |
201 | size_bound = n; | |
202 | } | |
01769a73 | 203 | else if (*fmt == '-' || *fmt == ' ' || *fmt == '.' || *fmt == '+') |
be65c2f4 | 204 | ; |
e6c3da20 EZ |
205 | else if (*fmt == 'l') |
206 | { | |
207 | long_flag = 1; | |
208 | if (!strchr ("dox", fmt[1])) | |
209 | /* %l as conversion specifier, not as modifier. */ | |
210 | break; | |
211 | } | |
be65c2f4 | 212 | else |
24f98398 JA |
213 | break; |
214 | fmt++; | |
215 | } | |
216 | *string = 0; | |
03383aaf | 217 | |
be65c2f4 PE |
218 | /* Make the size bound large enough to handle floating point formats |
219 | with large numbers. */ | |
e6c3da20 | 220 | if (size_bound > SIZE_MAX - DBL_MAX_10_EXP - 50) |
be65c2f4 | 221 | error ("Format width or precision too large"); |
01769a73 | 222 | size_bound += DBL_MAX_10_EXP + 50; |
6e951728 | 223 | |
f4c730d3 RS |
224 | /* Make sure we have that much. */ |
225 | if (size_bound > size_allocated) | |
226 | { | |
227 | if (big_buffer) | |
228 | big_buffer = (char *) xrealloc (big_buffer, size_bound); | |
229 | else | |
230 | big_buffer = (char *) xmalloc (size_bound); | |
231 | sprintf_buffer = big_buffer; | |
232 | size_allocated = size_bound; | |
233 | } | |
24f98398 JA |
234 | minlen = 0; |
235 | switch (*fmt++) | |
236 | { | |
237 | default: | |
238 | error ("Invalid format operation %%%c", fmt[-1]); | |
239 | ||
240 | /* case 'b': */ | |
e6c3da20 | 241 | case 'l': |
24f98398 | 242 | case 'd': |
e6c3da20 EZ |
243 | { |
244 | int i; | |
245 | long l; | |
246 | ||
247 | if (long_flag) | |
248 | { | |
249 | l = va_arg(ap, long); | |
250 | sprintf (sprintf_buffer, fmtcpy, l); | |
251 | } | |
252 | else | |
253 | { | |
254 | i = va_arg(ap, int); | |
255 | sprintf (sprintf_buffer, fmtcpy, i); | |
256 | } | |
257 | /* Now copy into final output, truncating as necessary. */ | |
258 | string = sprintf_buffer; | |
259 | goto doit; | |
260 | } | |
261 | ||
24f98398 JA |
262 | case 'o': |
263 | case 'x': | |
e6c3da20 EZ |
264 | { |
265 | unsigned u; | |
266 | unsigned long ul; | |
267 | ||
268 | if (long_flag) | |
269 | { | |
270 | ul = va_arg(ap, unsigned long); | |
271 | sprintf (sprintf_buffer, fmtcpy, ul); | |
272 | } | |
273 | else | |
274 | { | |
275 | u = va_arg(ap, unsigned); | |
276 | sprintf (sprintf_buffer, fmtcpy, u); | |
277 | } | |
278 | /* Now copy into final output, truncating as necessary. */ | |
279 | string = sprintf_buffer; | |
280 | goto doit; | |
281 | } | |
24f98398 | 282 | |
f4c730d3 RS |
283 | case 'f': |
284 | case 'e': | |
285 | case 'g': | |
286 | { | |
6a8033e1 KR |
287 | double d = va_arg(ap, double); |
288 | sprintf (sprintf_buffer, fmtcpy, d); | |
e6c3da20 | 289 | /* Now copy into final output, truncating as necessary. */ |
7469ef5d | 290 | string = sprintf_buffer; |
f4c730d3 RS |
291 | goto doit; |
292 | } | |
293 | ||
24f98398 JA |
294 | case 'S': |
295 | string[-1] = 's'; | |
296 | case 's': | |
24f98398 JA |
297 | if (fmtcpy[1] != 's') |
298 | minlen = atoi (&fmtcpy[1]); | |
7469ef5d | 299 | string = va_arg (ap, char *); |
e267324c | 300 | tem = strlen (string); |
e6c3da20 EZ |
301 | if (tem > MOST_POSITIVE_FIXNUM) |
302 | error ("String for %%s or %%S format is too long"); | |
a0ca925c | 303 | width = strwidth (string, tem); |
1513af9e RS |
304 | goto doit1; |
305 | ||
24f98398 JA |
306 | /* Copy string into final output, truncating if no room. */ |
307 | doit: | |
a0ca925c | 308 | /* Coming here means STRING contains ASCII only. */ |
e6c3da20 EZ |
309 | tem = strlen (string); |
310 | if (tem > MOST_POSITIVE_FIXNUM) | |
311 | error ("Format width or precision too large"); | |
312 | width = tem; | |
35a65fce | 313 | doit1: |
a0ca925c KH |
314 | /* We have already calculated: |
315 | TEM -- length of STRING, | |
316 | WIDTH -- columns occupied by STRING when displayed, and | |
317 | MINLEN -- minimum columns of the output. */ | |
24f98398 JA |
318 | if (minlen > 0) |
319 | { | |
a0ca925c | 320 | while (minlen > width && bufsize > 0) |
24f98398 JA |
321 | { |
322 | *bufptr++ = ' '; | |
323 | bufsize--; | |
324 | minlen--; | |
325 | } | |
326 | minlen = 0; | |
327 | } | |
328 | if (tem > bufsize) | |
a0ca925c KH |
329 | { |
330 | /* Truncate the string at character boundary. */ | |
331 | tem = bufsize; | |
a50545d9 | 332 | while (!CHAR_HEAD_P (string[tem - 1])) tem--; |
72af86bd | 333 | memcpy (bufptr, string, tem); |
a0ca925c KH |
334 | /* We must calculate WIDTH again. */ |
335 | width = strwidth (bufptr, tem); | |
336 | } | |
337 | else | |
72af86bd | 338 | memcpy (bufptr, string, tem); |
24f98398 JA |
339 | bufptr += tem; |
340 | bufsize -= tem; | |
341 | if (minlen < 0) | |
342 | { | |
a0ca925c | 343 | while (minlen < - width && bufsize > 0) |
24f98398 JA |
344 | { |
345 | *bufptr++ = ' '; | |
346 | bufsize--; | |
347 | minlen++; | |
348 | } | |
349 | minlen = 0; | |
350 | } | |
351 | continue; | |
352 | ||
353 | case 'c': | |
6a8033e1 | 354 | { |
e6c3da20 EZ |
355 | int chr = va_arg(ap, int); |
356 | tem = CHAR_STRING (chr, (unsigned char *) charbuf); | |
6a8033e1 KR |
357 | string = charbuf; |
358 | string[tem] = 0; | |
359 | width = strwidth (string, tem); | |
360 | if (fmtcpy[1] != 'c') | |
361 | minlen = atoi (&fmtcpy[1]); | |
362 | goto doit1; | |
363 | } | |
24f98398 JA |
364 | |
365 | case '%': | |
366 | fmt--; /* Drop thru and this % will be treated as normal */ | |
367 | } | |
368 | } | |
a0ca925c KH |
369 | |
370 | { | |
371 | /* Just some character; Copy it if the whole multi-byte form | |
372 | fit in the buffer. */ | |
373 | char *save_bufptr = bufptr; | |
374 | ||
375 | do { *bufptr++ = *fmt++; } | |
a50545d9 RS |
376 | while (--bufsize > 0 && !CHAR_HEAD_P (*fmt)); |
377 | if (!CHAR_HEAD_P (*fmt)) | |
a0ca925c KH |
378 | { |
379 | bufptr = save_bufptr; | |
380 | break; | |
381 | } | |
382 | } | |
24f98398 JA |
383 | }; |
384 | ||
f4c730d3 | 385 | /* If we had to malloc something, free it. */ |
70fdbb46 | 386 | xfree (big_buffer); |
f4c730d3 | 387 | |
e6c3da20 | 388 | *bufptr = 0; /* Make sure our string ends with a '\0' */ |
825cd63c EZ |
389 | |
390 | SAFE_FREE (); | |
24f98398 JA |
391 | return bufptr - buffer; |
392 | } |