summaryrefslogtreecommitdiff
path: root/src/doprnt.c
blob: fe484b8e766e9cd108aefa58ee13dc5c72e13911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* Output like sprintf to a buffer of specified size.    -*- coding: utf-8 -*-
   Also takes args differently: pass one pointer to the end
   of the format string in addition to the format string itself.
   Copyright (C) 1985, 2001-2021 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

/* If you think about replacing this with some similar standard C function of
   the printf family (such as vsnprintf), please note that this function
   supports the following Emacs-specific features:

   . For %c conversions, it produces a string with the multibyte representation
     of the (`int') argument, suitable for display in an Emacs buffer.

   . For %s and %c, when field width is specified (e.g., %25s), it accounts for
     the display width of each character, according to char-width-table.  That
     is, it does not assume that each character takes one column on display.
     Nor does it assume that each character is a single byte.

   . If the size of the buffer is not enough to produce the formatted string in
     its entirety, it makes sure that truncation does not chop the last
     character in the middle of its multibyte sequence, producing an invalid
     sequence.

   . It accepts a pointer to the end of the format string, so the format string
     could include embedded null characters.

   . It signals an error if the length of the formatted string is about to
     overflow ptrdiff_t or size_t, to avoid producing strings longer than what
     Emacs can handle.

   OTOH, this function supports only a small subset of the standard C formatted
   output facilities.  E.g., %u is not supported, precision is ignored
   in %s and %c conversions, and %lld does not necessarily work and
   code should use something like %"pM"d with intmax_t instead.
   (See below for the detailed documentation of what is supported.)
   However, this is okay, as this function is supposed to be called
   from 'error' and similar C functions, and thus does not need to
   support all the features of 'Fformat_message', which is used by the
   Lisp 'error' function.  */

/* In the FORMAT argument this function supports ` and ' as directives
   that output left and right quotes as per ‘text-quoting style’.  It
   also supports the following %-sequences:

   %s means print a string argument.
   %S is treated as %s, for loose compatibility with `Fformat_message'.
   %d means print a `signed int' argument in decimal.
   %o means print an `unsigned int' argument in octal.
   %x means print an `unsigned int' argument in hex.
   %e means print a `double' argument in exponential notation.
   %f means print a `double' argument in decimal-point notation.
   %g means print a `double' argument in exponential notation
      or in decimal-point notation, depending on the value;
      this is often (though not always) the shorter of the two notations.
   %c means print a `signed int' argument as a single character.
   %% means produce a literal % character.

   A %-sequence other than %% may contain optional flags, width, precision,
   and length, as follows:

     %<flags><width><precision><length>character

   where flags is [+ -0], width is [0-9]+, precision is .[0-9]+, and length
   is empty or l or the value of the pD or pI or PRIdMAX (sans "d") macros.
   A % that does not introduce a valid %-sequence causes undefined behavior.
   Bytes in FORMAT other than % are copied through as-is.

   The + flag character inserts a + before any positive number, while a space
   inserts a space before any positive number; these flags only affect %d, %o,
   %x, %e, %f, and %g sequences.  The - and 0 flags affect the width specifier,
   as described below.  For signed numerical arguments only, the ` ' (space)
   flag causes the result to be prefixed with a space character if it does not
   start with a sign (+ or -).

   The l (lower-case letter ell) length modifier is a `long' data type
   modifier: it is supported for %d, %o, and %x conversions of integral
   arguments, must immediately precede the conversion specifier, and means that
   the respective argument is to be treated as `long int' or `unsigned long
   int'.  Similarly, the value of the pD macro means to use ptrdiff_t,
   the value of the pI macro means to use EMACS_INT or EMACS_UINT, the
   value of the PRIdMAX etc. macros means to use intmax_t or uintmax_t,
   and the empty length modifier means `int' or `unsigned int'.

   The width specifier supplies a lower limit for the length of the printed
   representation.  The padding, if any, normally goes on the left, but it goes
   on the right if the - flag is present.  The padding character is normally a
   space, but (for numerical arguments only) it is 0 if the 0 flag is present.
   The - flag takes precedence over the 0 flag.

   For %e, %f, and %g sequences, the number after the "." in the precision
   specifier says how many decimal places to show; if zero, the decimal point
   itself is omitted.  For %d, %o, and %x sequences, the precision specifies
   the minimum number of digits to appear.  Precision specifiers are
   not supported for other %-sequences.  */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <unistd.h>
#include <limits.h>

#include "lisp.h"

/* Since we use the macro CHAR_HEAD_P, we have to include this, but
   don't have to include others because CHAR_HEAD_P does not contains
   another macro.  */
#include "character.h"

/* Enough to handle floating point formats with large numbers.  */
enum { SIZE_BOUND_EXTRA = DBL_MAX_10_EXP + 50 };

/* Parse FMT as an unsigned decimal integer, putting its value into *VALUE.
   Return the address of the first byte after the integer.
   If FMT is not an integer, return FMT and store zero into *VALUE.  */
static char const *
parse_format_integer (char const *fmt, int *value)
{
  int n = 0;
  bool overflow = false;
  for (; '0' <= *fmt && *fmt <= '9'; fmt++)
    {
      overflow |= INT_MULTIPLY_WRAPV (n, 10, &n);
      overflow |= INT_ADD_WRAPV (n, *fmt - '0', &n);
    }
  if (overflow || min (PTRDIFF_MAX, SIZE_MAX) - SIZE_BOUND_EXTRA < n)
    error ("Format width or precision too large");
  *value = n;
  return fmt;
}

/* Like doprnt, except FORMAT_END must be non-null.
   Although this function is never exercised in current Emacs,
   it is retained in case some future Emacs version
   contains doprnt callers that need such formats.
   Having a separate function helps GCC optimize doprnt better.  */
static ptrdiff_t
doprnt_non_null_end (char *buffer, ptrdiff_t bufsize, char const *format,
		     char const *format_end, va_list ap)
{
  USE_SAFE_ALLOCA;
  ptrdiff_t fmtlen = format_end - format;
  char *fmt = SAFE_ALLOCA (fmtlen + 1);
  memcpy (fmt, format, fmtlen);
  fmt[fmtlen] = 0;
  ptrdiff_t nbytes = doprnt (buffer, bufsize, fmt, NULL, ap);
  SAFE_FREE ();
  return nbytes;
}

/* Generate output from a format-spec FORMAT,
   terminated at either the first NUL or (if FORMAT_END is non-null
   and there are no NUL bytes between FORMAT and FORMAT_END)
   terminated at position FORMAT_END.
   (*FORMAT_END is not part of the format, but must exist and be readable.)
   Output goes in BUFFER, which has room for BUFSIZE chars.
   BUFSIZE must be positive.  If the output does not fit, truncate it
   to fit and return BUFSIZE - 1; if this truncates a multibyte
   sequence, store '\0' into the sequence's first byte.
   Returns the number of bytes stored into BUFFER, excluding
   the terminating null byte.  Output is always null-terminated.
   String arguments are passed as C strings.
   Integers are passed as C integers.

   FIXME: If FORMAT_END is not at a character boundary
   doprnt_non_null_end will cut the string in the middle of the
   character and the returned string will have an incomplete character
   sequence at the end.  We may prefer to cut at a character
   boundary.  */

ptrdiff_t
doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
	const char *format_end, va_list ap)
{
  if (format_end)
    return doprnt_non_null_end (buffer, bufsize, format, format_end, ap);

  const char *fmt = format;	/* Pointer into format string.  */
  char *bufptr = buffer;	/* Pointer into output buffer.  */

  /* Use this for sprintf unless we need something really big.  */
  char tembuf[SIZE_BOUND_EXTRA + 50];

  /* Size of sprintf_buffer.  */
  ptrdiff_t size_allocated = sizeof (tembuf);

  /* Buffer to use for sprintf.  Either tembuf or same as BIG_BUFFER.  */
  char *sprintf_buffer = tembuf;

  /* Buffer we have got with malloc.  */
  char *big_buffer = NULL;

  Lisp_Object quoting_style = Ftext_quoting_style ();

  bufsize--;

  /* Loop until end of format string or buffer full. */
  while (*fmt && bufsize > 0)
    {
      char const *fmt0 = fmt;
      char fmtchar = *fmt++;
      if (fmtchar == '%')
	{
	  ptrdiff_t width;  /* Columns occupied by STRING on display.  */
	  enum {
	    pDlen = sizeof pD - 1,
	    pIlen = sizeof pI - 1,
	    pMlen = sizeof PRIdMAX - 2,
	    maxmlen = max (max (1, pDlen), max (pIlen, pMlen))
	  };
	  enum {
	    no_modifier, long_modifier, pD_modifier, pI_modifier, pM_modifier
	  } length_modifier = no_modifier;
	  static char const modifier_len[] = { 0, 1, pDlen, pIlen, pMlen };
	  int mlen;
	  char charbuf[MAX_MULTIBYTE_LENGTH + 1];	/* Used for %c.  */

	  /* Width and precision specified by this %-sequence.  */
	  int wid = 0, prec = -1;

	  /* FMTSTAR will be a "%*.*X"-like version of this %-sequence.
	     Start by putting '%' into FMTSTAR.  */
	  char fmtstar[sizeof "%-+ 0*.*d" + maxmlen];
	  char *string = fmtstar;
	  *string++ = '%';

	  /* Copy at most one instance of each flag into FMTSTAR.  */
	  bool minusflag = false, plusflag = false, zeroflag = false,
	    spaceflag = false;
	  for (;; fmt++)
	    {
	      *string = *fmt;
	      switch (*fmt)
		{
		case '-': string += !minusflag; minusflag = true; continue;
		case '+': string += !plusflag; plusflag = true; continue;
		case ' ': string += !spaceflag; spaceflag = true; continue;
		case '0': string += !zeroflag; zeroflag = true; continue;
		}
	      break;
	    }

	  /* Parse width and precision, putting "*.*" into FMTSTAR.  */
	  if ('1' <= *fmt && *fmt <= '9')
	    fmt = parse_format_integer (fmt, &wid);
	  if (*fmt == '.')
	    fmt = parse_format_integer (fmt + 1, &prec);
	  *string++ = '*';
	  *string++ = '.';
	  *string++ = '*';

	  /* Check for the length modifiers in textual length order, so
	     that longer modifiers override shorter ones.  */
	  for (mlen = 1; mlen <= maxmlen; mlen++)
	    {
	      if (mlen == 1 && *fmt == 'l')
		length_modifier = long_modifier;
	      if (mlen == pDlen && strncmp (fmt, pD, pDlen) == 0)
		length_modifier = pD_modifier;
	      if (mlen == pIlen && strncmp (fmt, pI, pIlen) == 0)
		length_modifier = pI_modifier;
	      if (mlen == pMlen && strncmp (fmt, PRIdMAX, pMlen) == 0)
		length_modifier = pM_modifier;
	    }

	  /* Copy optional length modifier and conversion specifier
	     character into FMTSTAR, and append a NUL.  */
	  mlen = modifier_len[length_modifier];
	  string = mempcpy (string, fmt, mlen + 1);
	  fmt += mlen;
	  *string = 0;

	  /* An idea of how much space we might need.
	     This might be a field width or a precision; e.g.
	     %1.1000f and %1000.1f both might need 1000+ bytes.
	     Make it large enough to handle floating point formats
	     with large numbers.  */
	  ptrdiff_t size_bound = max (wid, prec) + SIZE_BOUND_EXTRA;

	  /* Make sure we have that much.  */
	  if (size_bound > size_allocated)
	    {
	      if (big_buffer)
		xfree (big_buffer);
	      big_buffer = xmalloc (size_bound);
	      sprintf_buffer = big_buffer;
	      size_allocated = size_bound;
	    }
	  int minlen = 0;
	  ptrdiff_t tem;
	  switch (*fmt++)
	    {
	    default:
	      error ("Invalid format operation %s", fmt0);

	    case 'd':
	      switch (length_modifier)
		{
		case no_modifier:
		  {
		    int v = va_arg (ap, int);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case long_modifier:
		  {
		    long v = va_arg (ap, long);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case pD_modifier:
		signed_pD_modifier:
		  {
		    ptrdiff_t v = va_arg (ap, ptrdiff_t);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case pI_modifier:
		  {
		    EMACS_INT v = va_arg (ap, EMACS_INT);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case pM_modifier:
		  {
		    intmax_t v = va_arg (ap, intmax_t);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		default:
		  eassume (false);
		}
	      /* Now copy into final output, truncating as necessary.  */
	      string = sprintf_buffer;
	      goto doit;

	    case 'o':
	    case 'x':
	      switch (length_modifier)
		{
		case no_modifier:
		  {
		    unsigned v = va_arg (ap, unsigned);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case long_modifier:
		  {
		    unsigned long v = va_arg (ap, unsigned long);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case pD_modifier:
		  goto signed_pD_modifier;
		case pI_modifier:
		  {
		    EMACS_UINT v = va_arg (ap, EMACS_UINT);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		case pM_modifier:
		  {
		    uintmax_t v = va_arg (ap, uintmax_t);
		    tem = sprintf (sprintf_buffer, fmtstar, wid, prec, v);
		  }
		  break;
		default:
		  eassume (false);
		}
	      /* Now copy into final output, truncating as necessary.  */
	      string = sprintf_buffer;
	      goto doit;

	    case 'f':
	    case 'e':
	    case 'g':
	      {
		double d = va_arg (ap, double);
		tem = sprintf (sprintf_buffer, fmtstar, wid, prec, d);
		/* Now copy into final output, truncating as necessary.  */
		string = sprintf_buffer;
		goto doit;
	      }

	    case 'S':
	    case 's':
	      minlen = minusflag ? -wid : wid;
	      string = va_arg (ap, char *);
	      tem = strnlen (string, STRING_BYTES_BOUND + 1);
	      if (tem == STRING_BYTES_BOUND + 1)
		error ("String for %%s or %%S format is too long");
	      width = strwidth (string, tem);
	      goto doit1;

	      /* Copy string into final output, truncating if no room.  */
	    doit:
	      eassert (0 <= tem);
	      /* Coming here means STRING contains ASCII only.  */
	      if (STRING_BYTES_BOUND < tem)
		error ("Format width or precision too large");
	      width = tem;
	    doit1:
	      /* We have already calculated:
		 TEM -- length of STRING,
		 WIDTH -- columns occupied by STRING when displayed, and
		 MINLEN -- minimum columns of the output.  */
	      if (minlen > 0)
		{
		  while (minlen > width && bufsize > 0)
		    {
		      *bufptr++ = ' ';
		      bufsize--;
		      minlen--;
		    }
		  minlen = 0;
		}
	      if (tem > bufsize)
		{
		  /* Truncate the string at character boundary.  */
		  tem = bufsize;
		  do
		    {
		      tem--;
		      if (CHAR_HEAD_P (string[tem]))
			{
			  if (BYTES_BY_CHAR_HEAD (string[tem]) <= bufsize - tem)
			    tem = bufsize;
			  break;
			}
		    }
		  while (tem != 0);

		  memcpy (bufptr, string, tem);
		  bufptr[tem] = 0;
		  /* Trigger exit from the loop, but make sure we
		     return to the caller a value which will indicate
		     that the buffer was too small.  */
		  bufptr += bufsize;
		  bufsize = 0;
		  continue;
		}
	      memcpy (bufptr, string, tem);
	      bufptr += tem;
	      bufsize -= tem;
	      if (minlen < 0)
		{
		  while (minlen < - width && bufsize > 0)
		    {
		      *bufptr++ = ' ';
		      bufsize--;
		      minlen++;
		    }
		  minlen = 0;
		}
	      continue;

	    case 'c':
	      {
		int chr = va_arg (ap, int);
		tem = CHAR_STRING (chr, (unsigned char *) charbuf);
		string = charbuf;
		string[tem] = 0;
		width = strwidth (string, tem);
		minlen = minusflag ? -wid : wid;
		goto doit1;
	      }

	    case '%':
	      /* Treat this '%' as normal.  */
	      break;
	    }
	}

      char const *src;
      ptrdiff_t srclen;
      if (EQ (quoting_style, Qcurve) && fmtchar == '`')
	src = uLSQM, srclen = sizeof uLSQM - 1;
      else if (EQ (quoting_style, Qcurve) && fmtchar == '\'')
	src = uRSQM, srclen = sizeof uRSQM - 1;
      else if (! LEADING_CODE_P (fmtchar))
	{
	  if (EQ (quoting_style, Qstraight) && fmtchar == '`')
	    fmtchar = '\'';

	  *bufptr++ = fmtchar;
	  continue;
	}
      else
        {
          int charlen = BYTES_BY_CHAR_HEAD (fmtchar);
          src = fmt0;

          /* If the format string ends in the middle of a multibyte
             character we don't want to skip over the NUL byte.  */
          for (srclen = 1 ; *(src + srclen) != 0 && srclen < charlen ; srclen++);

          fmt = src + srclen;
        }

      if (bufsize < srclen)
	{
	  /* Truncate, but return value that will signal to caller
	     that the buffer was too small.  */
	  do
	    *bufptr++ = '\0';
	  while (--bufsize != 0);
	}
      else
	{
	  do
	    *bufptr++ = *src++;
	  while (--srclen != 0);
	}
    }

  /* If we had to malloc something, free it.  */
  xfree (big_buffer);

  *bufptr = 0;		/* Make sure our string ends with a '\0' */
  return bufptr - buffer;
}

/* Format to an unbounded buffer BUF.  This is like sprintf, except it
   is not limited to returning an 'int' so it doesn't have a silly 2
   GiB limit on typical 64-bit hosts.  However, it is limited to the
   Emacs-style formats that doprnt supports, and it requotes ` and '
   as per ‘text-quoting-style’.

   Return the number of bytes put into BUF, excluding the terminating
   '\0'.  */
ptrdiff_t
esprintf (char *buf, char const *format, ...)
{
  ptrdiff_t nbytes;
  va_list ap;
  va_start (ap, format);
  nbytes = doprnt (buf, TYPE_MAXIMUM (ptrdiff_t), format, 0, ap);
  va_end (ap);
  return nbytes;
}

#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT

/* Format to buffer *BUF of positive size *BUFSIZE, reallocating *BUF
   and updating *BUFSIZE if the buffer is too small, and otherwise
   behaving line esprintf.  When reallocating, free *BUF unless it is
   equal to NONHEAPBUF, and if BUFSIZE_MAX is nonnegative then signal
   memory exhaustion instead of growing the buffer size past
   BUFSIZE_MAX.  */
ptrdiff_t
exprintf (char **buf, ptrdiff_t *bufsize,
	  char *nonheapbuf, ptrdiff_t bufsize_max,
	  char const *format, ...)
{
  ptrdiff_t nbytes;
  va_list ap;
  va_start (ap, format);
  nbytes = evxprintf (buf, bufsize, nonheapbuf, bufsize_max, format, ap);
  va_end (ap);
  return nbytes;
}

#endif

/* Act like exprintf, except take a va_list.  */
ptrdiff_t
evxprintf (char **buf, ptrdiff_t *bufsize,
	   char *nonheapbuf, ptrdiff_t bufsize_max,
	   char const *format, va_list ap)
{
  for (;;)
    {
      ptrdiff_t nbytes;
      va_list ap_copy;
      va_copy (ap_copy, ap);
      nbytes = doprnt (*buf, *bufsize, format, 0, ap_copy);
      va_end (ap_copy);
      if (nbytes < *bufsize - 1)
	return nbytes;
      if (*buf != nonheapbuf)
	{
	  xfree (*buf);
	  *buf = NULL;
	}
      *buf = xpalloc (NULL, bufsize, 1, bufsize_max, 1);
    }
}