doprnt.c 7.97 KB
Newer Older
Joseph Arceneaux's avatar
Joseph Arceneaux committed
1 2 3
/* Output like sprintf to a buffer of specified size.
   Also takes args differently: pass one pointer to an array of strings
   in addition to the format string which is separate.
4
   Copyright (C) 1985, 2001-2011  Free Software Foundation, Inc.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Joseph Arceneaux's avatar
Joseph Arceneaux committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Joseph Arceneaux's avatar
Joseph Arceneaux committed
12 13 14 15 16 17 18

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
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
20 21


Richard M. Stallman's avatar
Richard M. Stallman committed
22
#include <config.h>
Joseph Arceneaux's avatar
Joseph Arceneaux committed
23 24
#include <stdio.h>
#include <ctype.h>
25
#include <setjmp.h>
Joseph Arceneaux's avatar
Joseph Arceneaux committed
26

27
#ifdef STDC_HEADERS
28 29 30
#include <float.h>
#endif

Dave Love's avatar
Dave Love committed
31 32
#include <unistd.h>

33 34 35 36 37
#include <limits.h>
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#endif

38 39
#include "lisp.h"

Karl Heuer's avatar
Karl Heuer committed
40 41 42
/* 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.  */
43
#include "character.h"
Karl Heuer's avatar
Karl Heuer committed
44

45 46 47 48
#ifndef DBL_MAX_10_EXP
#define DBL_MAX_10_EXP 308 /* IEEE double */
#endif

49 50 51 52
/* Generate output from a format-spec FORMAT,
   terminated at position FORMAT_END.
   Output goes in BUFFER, which has room for BUFSIZE chars.
   If the output does not fit, truncate it to fit.
Richard M. Stallman's avatar
Richard M. Stallman committed
53
   Returns the number of bytes stored into BUFFER.
54
   ARGS points to the vector of arguments, and NARGS says how many.
Richard M. Stallman's avatar
Richard M. Stallman committed
55 56 57
   A double counts as two arguments.
   String arguments are passed as C strings.
   Integers are passed as C integers.  */
58

59 60
size_t
doprnt (char *buffer, register size_t bufsize, const char *format,
61
	const char *format_end, va_list ap)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
62
{
63
  const char *fmt = format;	/* Pointer into format string */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
64
  register char *bufptr = buffer; /* Pointer into output buffer.. */
65

66
  /* Use this for sprintf unless we need something really big.  */
67
  char tembuf[DBL_MAX_10_EXP + 100];
68

69
  /* Size of sprintf_buffer.  */
70
  size_t size_allocated = sizeof (tembuf);
71

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

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

78
  register size_t tem;
79
  char *string;
80 81
  char fixed_buffer[20];	/* Default buffer for small formatting. */
  char *fmtcpy;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
82
  int minlen;
83
  char charbuf[MAX_MULTIBYTE_LENGTH + 1];	/* Used for %c.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
84 85 86 87

  if (format_end == 0)
    format_end = format + strlen (format);

88 89 90
  if ((format_end - format + 1) < sizeof (fixed_buffer))
    fmtcpy = fixed_buffer;
  else
91
    fmtcpy = (char *) alloca (format_end - format + 1);
92

Joseph Arceneaux's avatar
Joseph Arceneaux committed
93
  bufsize--;
94 95 96

  /* Loop until end of format string or buffer full. */
  while (fmt != format_end && bufsize > 0)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
97 98 99
    {
      if (*fmt == '%')	/* Check for a '%' character */
	{
100 101 102
	  size_t size_bound = 0;
	  EMACS_INT width;  /* Columns occupied by STRING on display.  */
	  int long_flag = 0;
103

Joseph Arceneaux's avatar
Joseph Arceneaux committed
104
	  fmt++;
Jim Blandy's avatar
Jim Blandy committed
105
	  /* Copy this one %-spec into fmtcpy.  */
106
	  string = fmtcpy;
Joseph Arceneaux's avatar
Joseph Arceneaux committed
107
	  *string++ = '%';
108
	  while (1)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
109 110
	    {
	      *string++ = *fmt;
111 112 113 114 115 116
	      if ('0' <= *fmt && *fmt <= '9')
		{
		  /* Get 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.
		     Parse the width or precision, checking for overflow.  */
117
		  size_t n = *fmt - '0';
118 119
		  while ('0' <= fmt[1] && fmt[1] <= '9')
		    {
120 121
		      if (n >= SIZE_MAX / 10
			  || n * 10 > SIZE_MAX - (fmt[1] - '0'))
122
			error ("Format width or precision too large");
Andreas Schwab's avatar
Andreas Schwab committed
123
		      n = n * 10 + fmt[1] - '0';
124 125 126 127 128 129
		      *string++ = *++fmt;
		    }

		  if (size_bound < n)
		    size_bound = n;
		}
130
	      else if (*fmt == '-' || *fmt == ' ' || *fmt == '.' || *fmt == '+')
131
		;
132 133 134 135 136 137 138
	      else if (*fmt == 'l')
		{
		  long_flag = 1;
		  if (!strchr ("dox", fmt[1]))
		    /* %l as conversion specifier, not as modifier.  */
		    break;
		}
139
	      else
Joseph Arceneaux's avatar
Joseph Arceneaux committed
140 141 142 143
		break;
	      fmt++;
	    }
	  *string = 0;
144

145 146
	  /* Make the size bound large enough to handle floating point formats
	     with large numbers.  */
147
	  if (size_bound > SIZE_MAX - DBL_MAX_10_EXP - 50)
148
	    error ("Format width or precision too large");
149
	  size_bound += DBL_MAX_10_EXP + 50;
150

151 152 153 154 155 156 157 158 159 160
	  /* Make sure we have that much.  */
	  if (size_bound > size_allocated)
	    {
	      if (big_buffer)
		big_buffer = (char *) xrealloc (big_buffer, size_bound);
	      else
		big_buffer = (char *) xmalloc (size_bound);
	      sprintf_buffer = big_buffer;
	      size_allocated = size_bound;
	    }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
161 162 163 164 165 166 167
	  minlen = 0;
	  switch (*fmt++)
	    {
	    default:
	      error ("Invalid format operation %%%c", fmt[-1]);

/*	    case 'b': */
168
	    case 'l':
Joseph Arceneaux's avatar
Joseph Arceneaux committed
169
	    case 'd':
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	      {
		int i;
		long l;

		if (long_flag)
		  {
		    l = va_arg(ap, long);
		    sprintf (sprintf_buffer, fmtcpy, l);
		  }
		else
		  {
		    i = va_arg(ap, int);
		    sprintf (sprintf_buffer, fmtcpy, i);
		  }
		/* Now copy into final output, truncating as necessary.  */
		string = sprintf_buffer;
		goto doit;
	      }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
189 190
	    case 'o':
	    case 'x':
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	      {
		unsigned u;
		unsigned long ul;

		if (long_flag)
		  {
		    ul = va_arg(ap, unsigned long);
		    sprintf (sprintf_buffer, fmtcpy, ul);
		  }
		else
		  {
		    u = va_arg(ap, unsigned);
		    sprintf (sprintf_buffer, fmtcpy, u);
		  }
		/* Now copy into final output, truncating as necessary.  */
		string = sprintf_buffer;
		goto doit;
	      }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
209

210 211 212 213
	    case 'f':
	    case 'e':
	    case 'g':
	      {
214 215
		double d = va_arg(ap, double);
		sprintf (sprintf_buffer, fmtcpy, d);
216
		/* Now copy into final output, truncating as necessary.  */
217
		string = sprintf_buffer;
218 219 220
		goto doit;
	      }

Joseph Arceneaux's avatar
Joseph Arceneaux committed
221 222 223 224 225
	    case 'S':
	      string[-1] = 's';
	    case 's':
	      if (fmtcpy[1] != 's')
		minlen = atoi (&fmtcpy[1]);
226
	      string = va_arg (ap, char *);
227
	      tem = strlen (string);
228 229
	      if (tem > MOST_POSITIVE_FIXNUM)
		error ("String for %%s or %%S format is too long");
Karl Heuer's avatar
Karl Heuer committed
230
	      width = strwidth (string, tem);
Richard M. Stallman's avatar
Richard M. Stallman committed
231 232
	      goto doit1;

Joseph Arceneaux's avatar
Joseph Arceneaux committed
233 234
	      /* Copy string into final output, truncating if no room.  */
	    doit:
Karl Heuer's avatar
Karl Heuer committed
235
	      /* Coming here means STRING contains ASCII only.  */
236 237 238 239
	      tem = strlen (string);
	      if (tem > MOST_POSITIVE_FIXNUM)
		error ("Format width or precision too large");
	      width = tem;
240
	    doit1:
Karl Heuer's avatar
Karl Heuer committed
241 242 243 244
	      /* We have already calculated:
		 TEM -- length of STRING,
		 WIDTH -- columns occupied by STRING when displayed, and
		 MINLEN -- minimum columns of the output.  */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
245 246
	      if (minlen > 0)
		{
Karl Heuer's avatar
Karl Heuer committed
247
		  while (minlen > width && bufsize > 0)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
248 249 250 251 252 253 254 255
		    {
		      *bufptr++ = ' ';
		      bufsize--;
		      minlen--;
		    }
		  minlen = 0;
		}
	      if (tem > bufsize)
Karl Heuer's avatar
Karl Heuer committed
256 257 258
		{
		  /* Truncate the string at character boundary.  */
		  tem = bufsize;
259
		  while (!CHAR_HEAD_P (string[tem - 1])) tem--;
260
		  memcpy (bufptr, string, tem);
Karl Heuer's avatar
Karl Heuer committed
261 262 263 264
		  /* We must calculate WIDTH again.  */
		  width = strwidth (bufptr, tem);
		}
	      else
265
		memcpy (bufptr, string, tem);
Joseph Arceneaux's avatar
Joseph Arceneaux committed
266 267 268 269
	      bufptr += tem;
	      bufsize -= tem;
	      if (minlen < 0)
		{
Karl Heuer's avatar
Karl Heuer committed
270
		  while (minlen < - width && bufsize > 0)
Joseph Arceneaux's avatar
Joseph Arceneaux committed
271 272 273 274 275 276 277 278 279 280
		    {
		      *bufptr++ = ' ';
		      bufsize--;
		      minlen++;
		    }
		  minlen = 0;
		}
	      continue;

	    case 'c':
281
	      {
282 283
		int chr = va_arg(ap, int);
		tem = CHAR_STRING (chr, (unsigned char *) charbuf);
284 285 286 287 288 289 290
		string = charbuf;
		string[tem] = 0;
		width = strwidth (string, tem);
		if (fmtcpy[1] != 'c')
		  minlen = atoi (&fmtcpy[1]);
		goto doit1;
	      }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
291 292 293 294 295

	    case '%':
	      fmt--;    /* Drop thru and this % will be treated as normal */
	    }
	}
Karl Heuer's avatar
Karl Heuer committed
296 297 298 299 300 301 302

      {
	/* Just some character; Copy it if the whole multi-byte form
	   fit in the buffer.  */
	char *save_bufptr = bufptr;

	do { *bufptr++ = *fmt++; }
303 304
	while (--bufsize > 0 && !CHAR_HEAD_P (*fmt));
	if (!CHAR_HEAD_P (*fmt))
Karl Heuer's avatar
Karl Heuer committed
305 306 307 308 309
	  {
	    bufptr = save_bufptr;
	    break;
	  }
      }
Joseph Arceneaux's avatar
Joseph Arceneaux committed
310 311
    };

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

315
  *bufptr = 0;		/* Make sure our string ends with a '\0' */
Joseph Arceneaux's avatar
Joseph Arceneaux committed
316 317
  return bufptr - buffer;
}