* doprnt.c (SIZE_MAX): Move defn after all includes, as they might #define it.
[bpt/emacs.git] / src / doprnt.c
CommitLineData
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
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
24f98398 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
24f98398
JA
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along 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
PE
72 where flags is [+ -0], width is [0-9]+, precision is .[0-9]+, and length
73 modifier is empty or l or ll.
825cd63c
EZ
74
75 The + flag character inserts a + before any positive number, while a space
94dcfacf
EZ
76 inserts a space before any positive number; these flags only affect %d, %o,
77 %x, %e, %f, and %g sequences. The - and 0 flags affect the width specifier,
78 as described below. For signed numerical arguments only, the ` ' (space)
79 flag causes the result to be prefixed with a space character if it does not
80 start with a sign (+ or -).
81
82 The l (lower-case letter ell) length modifier is a `long' data type
83 modifier: it is supported for %d, %o, and %x conversions of integral
54b8e3f7 84 arguments, must immediately precede the conversion specifier, and means that
94dcfacf 85 the respective argument is to be treated as `long int' or `unsigned long
54b8e3f7 86 int'. Similarly, ll (two letter ells) means to use `long long int' or
b71a1728
PE
87 `unsigned long long int'; this can be used only on hosts that have
88 these two types. The empty length modifier means to use `int' or
54b8e3f7
PE
89 `unsigned int'. EMACS_INT arguments should use the pI macro, which
90 expands to whatever length modifier is needed for the target host.
825cd63c
EZ
91
92 The width specifier supplies a lower limit for the length of the printed
93 representation. The padding, if any, normally goes on the left, but it goes
94 on the right if the - flag is present. The padding character is normally a
95 space, but (for numerical arguments only) it is 0 if the 0 flag is present.
96 The - flag takes precedence over the 0 flag.
97
98 For %e, %f, and %g sequences, the number after the "." in the precision
99 specifier says how many decimal places to show; if zero, the decimal point
100 itself is omitted. For %s and %S, the precision specifier is ignored. */
24f98398 101
6d291527 102#include <config.h>
24f98398
JA
103#include <stdio.h>
104#include <ctype.h>
d7306fe6 105#include <setjmp.h>
24f98398 106
cf09633a 107#ifdef STDC_HEADERS
be65c2f4
PE
108#include <float.h>
109#endif
110
48236137 111#include <unistd.h>
48236137 112
e6c3da20 113#include <limits.h>
e6c3da20 114
523e9291
RS
115#include "lisp.h"
116
a0ca925c
KH
117/* Since we use the macro CHAR_HEAD_P, we have to include this, but
118 don't have to include others because CHAR_HEAD_P does not contains
119 another macro. */
83be827a 120#include "character.h"
a0ca925c 121
283cdbef
PE
122#ifndef SIZE_MAX
123# define SIZE_MAX ((size_t) -1)
124#endif
125
92bc9a36
DN
126#ifndef DBL_MAX_10_EXP
127#define DBL_MAX_10_EXP 308 /* IEEE double */
128#endif
129
f4c730d3
RS
130/* Generate output from a format-spec FORMAT,
131 terminated at position FORMAT_END.
132 Output goes in BUFFER, which has room for BUFSIZE chars.
133 If the output does not fit, truncate it to fit.
825cd63c
EZ
134 Returns the number of bytes stored into BUFFER, excluding
135 the terminating null byte. Output is always null-terminated.
1513af9e
RS
136 String arguments are passed as C strings.
137 Integers are passed as C integers. */
f4c730d3 138
e6c3da20
EZ
139size_t
140doprnt (char *buffer, register size_t bufsize, const char *format,
a8fe7202 141 const char *format_end, va_list ap)
24f98398 142{
a8fe7202 143 const char *fmt = format; /* Pointer into format string */
24f98398 144 register char *bufptr = buffer; /* Pointer into output buffer.. */
03383aaf 145
f4c730d3 146 /* Use this for sprintf unless we need something really big. */
be65c2f4 147 char tembuf[DBL_MAX_10_EXP + 100];
03383aaf 148
f4c730d3 149 /* Size of sprintf_buffer. */
e6c3da20 150 size_t size_allocated = sizeof (tembuf);
03383aaf 151
f4c730d3
RS
152 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
153 char *sprintf_buffer = tembuf;
03383aaf 154
f4c730d3 155 /* Buffer we have got with malloc. */
e6c3da20 156 char *big_buffer = NULL;
03383aaf 157
e6c3da20 158 register size_t tem;
7469ef5d 159 char *string;
03383aaf
BF
160 char fixed_buffer[20]; /* Default buffer for small formatting. */
161 char *fmtcpy;
24f98398 162 int minlen;
7469ef5d 163 char charbuf[MAX_MULTIBYTE_LENGTH + 1]; /* Used for %c. */
825cd63c 164 USE_SAFE_ALLOCA;
24f98398
JA
165
166 if (format_end == 0)
167 format_end = format + strlen (format);
168
03383aaf
BF
169 if ((format_end - format + 1) < sizeof (fixed_buffer))
170 fmtcpy = fixed_buffer;
171 else
825cd63c 172 SAFE_ALLOCA (fmtcpy, char *, format_end - format + 1);
03383aaf 173
24f98398 174 bufsize--;
03383aaf
BF
175
176 /* Loop until end of format string or buffer full. */
94dcfacf 177 while (fmt < format_end && bufsize > 0)
24f98398
JA
178 {
179 if (*fmt == '%') /* Check for a '%' character */
180 {
e6c3da20
EZ
181 size_t size_bound = 0;
182 EMACS_INT width; /* Columns occupied by STRING on display. */
183 int long_flag = 0;
f4c730d3 184
24f98398 185 fmt++;
d427b66a 186 /* Copy this one %-spec into fmtcpy. */
7469ef5d 187 string = fmtcpy;
24f98398 188 *string++ = '%';
94dcfacf 189 while (fmt < format_end)
24f98398
JA
190 {
191 *string++ = *fmt;
be65c2f4
PE
192 if ('0' <= *fmt && *fmt <= '9')
193 {
194 /* Get an idea of how much space we might need.
195 This might be a field width or a precision; e.g.
196 %1.1000f and %1000.1f both might need 1000+ bytes.
197 Parse the width or precision, checking for overflow. */
e6c3da20 198 size_t n = *fmt - '0';
94dcfacf
EZ
199 while (fmt < format_end
200 && '0' <= fmt[1] && fmt[1] <= '9')
be65c2f4 201 {
f76dee0c
PE
202 /* Avoid int overflow, because many sprintfs seriously
203 mess up with widths or precisions greater than
204 INT_MAX. Avoid size_t overflow, since our counters
205 use size_t. This test is slightly conservative, for
206 speed and simplicity. */
207 if (n >= min (INT_MAX, SIZE_MAX) / 10)
be65c2f4 208 error ("Format width or precision too large");
26898943 209 n = n * 10 + fmt[1] - '0';
be65c2f4
PE
210 *string++ = *++fmt;
211 }
212
213 if (size_bound < n)
214 size_bound = n;
215 }
01769a73 216 else if (*fmt == '-' || *fmt == ' ' || *fmt == '.' || *fmt == '+')
be65c2f4 217 ;
e6c3da20
EZ
218 else if (*fmt == 'l')
219 {
226be1b0 220 long_flag = 1 + (fmt + 1 < format_end && fmt[1] == 'l');
54b8e3f7 221 fmt += long_flag;
94dcfacf 222 break;
e6c3da20 223 }
be65c2f4 224 else
24f98398
JA
225 break;
226 fmt++;
227 }
94dcfacf
EZ
228 if (fmt > format_end)
229 fmt = format_end;
24f98398 230 *string = 0;
03383aaf 231
be65c2f4
PE
232 /* Make the size bound large enough to handle floating point formats
233 with large numbers. */
e6c3da20 234 if (size_bound > SIZE_MAX - DBL_MAX_10_EXP - 50)
be65c2f4 235 error ("Format width or precision too large");
01769a73 236 size_bound += DBL_MAX_10_EXP + 50;
6e951728 237
f4c730d3
RS
238 /* Make sure we have that much. */
239 if (size_bound > size_allocated)
240 {
241 if (big_buffer)
94dcfacf
EZ
242 xfree (big_buffer);
243 big_buffer = (char *) xmalloc (size_bound);
f4c730d3
RS
244 sprintf_buffer = big_buffer;
245 size_allocated = size_bound;
246 }
24f98398
JA
247 minlen = 0;
248 switch (*fmt++)
249 {
250 default:
94dcfacf 251 error ("Invalid format operation %%%s%c",
226be1b0 252 "ll" + 2 - long_flag, fmt[-1]);
24f98398
JA
253
254/* case 'b': */
e6c3da20 255 case 'l':
24f98398 256 case 'd':
e6c3da20
EZ
257 {
258 int i;
259 long l;
260
e810457d
PE
261 if (1 < long_flag)
262 {
263#ifdef HAVE_LONG_LONG_INT
264 long long ll = va_arg (ap, long long);
265 sprintf (sprintf_buffer, fmtcpy, ll);
266#else
267 abort ();
268#endif
269 }
270 else if (long_flag)
e6c3da20
EZ
271 {
272 l = va_arg(ap, long);
273 sprintf (sprintf_buffer, fmtcpy, l);
274 }
275 else
276 {
277 i = va_arg(ap, int);
278 sprintf (sprintf_buffer, fmtcpy, i);
279 }
280 /* Now copy into final output, truncating as necessary. */
281 string = sprintf_buffer;
282 goto doit;
283 }
284
24f98398
JA
285 case 'o':
286 case 'x':
e6c3da20
EZ
287 {
288 unsigned u;
289 unsigned long ul;
290
e810457d
PE
291 if (1 < long_flag)
292 {
293#ifdef HAVE_UNSIGNED_LONG_LONG_INT
294 unsigned long long ull = va_arg (ap, unsigned long long);
295 sprintf (sprintf_buffer, fmtcpy, ull);
296#else
297 abort ();
298#endif
299 }
300 else if (long_flag)
e6c3da20
EZ
301 {
302 ul = va_arg(ap, unsigned long);
303 sprintf (sprintf_buffer, fmtcpy, ul);
304 }
305 else
306 {
307 u = va_arg(ap, unsigned);
308 sprintf (sprintf_buffer, fmtcpy, u);
309 }
310 /* Now copy into final output, truncating as necessary. */
311 string = sprintf_buffer;
312 goto doit;
313 }
24f98398 314
f4c730d3
RS
315 case 'f':
316 case 'e':
317 case 'g':
318 {
6a8033e1
KR
319 double d = va_arg(ap, double);
320 sprintf (sprintf_buffer, fmtcpy, d);
e6c3da20 321 /* Now copy into final output, truncating as necessary. */
7469ef5d 322 string = sprintf_buffer;
f4c730d3
RS
323 goto doit;
324 }
325
24f98398
JA
326 case 'S':
327 string[-1] = 's';
328 case 's':
24f98398
JA
329 if (fmtcpy[1] != 's')
330 minlen = atoi (&fmtcpy[1]);
7469ef5d 331 string = va_arg (ap, char *);
e267324c 332 tem = strlen (string);
e6c3da20
EZ
333 if (tem > MOST_POSITIVE_FIXNUM)
334 error ("String for %%s or %%S format is too long");
a0ca925c 335 width = strwidth (string, tem);
1513af9e
RS
336 goto doit1;
337
24f98398
JA
338 /* Copy string into final output, truncating if no room. */
339 doit:
a0ca925c 340 /* Coming here means STRING contains ASCII only. */
e6c3da20
EZ
341 tem = strlen (string);
342 if (tem > MOST_POSITIVE_FIXNUM)
343 error ("Format width or precision too large");
344 width = tem;
35a65fce 345 doit1:
a0ca925c
KH
346 /* We have already calculated:
347 TEM -- length of STRING,
348 WIDTH -- columns occupied by STRING when displayed, and
349 MINLEN -- minimum columns of the output. */
24f98398
JA
350 if (minlen > 0)
351 {
a0ca925c 352 while (minlen > width && bufsize > 0)
24f98398
JA
353 {
354 *bufptr++ = ' ';
355 bufsize--;
356 minlen--;
357 }
358 minlen = 0;
359 }
360 if (tem > bufsize)
a0ca925c
KH
361 {
362 /* Truncate the string at character boundary. */
363 tem = bufsize;
a50545d9 364 while (!CHAR_HEAD_P (string[tem - 1])) tem--;
72af86bd 365 memcpy (bufptr, string, tem);
a0ca925c
KH
366 /* We must calculate WIDTH again. */
367 width = strwidth (bufptr, tem);
368 }
369 else
72af86bd 370 memcpy (bufptr, string, tem);
24f98398
JA
371 bufptr += tem;
372 bufsize -= tem;
373 if (minlen < 0)
374 {
a0ca925c 375 while (minlen < - width && bufsize > 0)
24f98398
JA
376 {
377 *bufptr++ = ' ';
378 bufsize--;
379 minlen++;
380 }
381 minlen = 0;
382 }
383 continue;
384
385 case 'c':
6a8033e1 386 {
e6c3da20
EZ
387 int chr = va_arg(ap, int);
388 tem = CHAR_STRING (chr, (unsigned char *) charbuf);
6a8033e1
KR
389 string = charbuf;
390 string[tem] = 0;
391 width = strwidth (string, tem);
392 if (fmtcpy[1] != 'c')
393 minlen = atoi (&fmtcpy[1]);
394 goto doit1;
395 }
24f98398
JA
396
397 case '%':
398 fmt--; /* Drop thru and this % will be treated as normal */
399 }
400 }
a0ca925c
KH
401
402 {
403 /* Just some character; Copy it if the whole multi-byte form
404 fit in the buffer. */
405 char *save_bufptr = bufptr;
406
407 do { *bufptr++ = *fmt++; }
94dcfacf 408 while (fmt < format_end && --bufsize > 0 && !CHAR_HEAD_P (*fmt));
a50545d9 409 if (!CHAR_HEAD_P (*fmt))
a0ca925c 410 {
d178f871
EZ
411 /* Truncate, but return value that will signal to caller
412 that the buffer was too small. */
413 *save_bufptr = 0;
a0ca925c
KH
414 break;
415 }
416 }
24f98398
JA
417 };
418
f4c730d3 419 /* If we had to malloc something, free it. */
70fdbb46 420 xfree (big_buffer);
f4c730d3 421
e6c3da20 422 *bufptr = 0; /* Make sure our string ends with a '\0' */
825cd63c
EZ
423
424 SAFE_FREE ();
24f98398
JA
425 return bufptr - buffer;
426}