lwlib/Makefile.in misc cleanup.
[emacs.git] / src / doprnt.c
blob36eb272caaee215f2ad58b750dbc43399272164d
1 /* Output like sprintf to a buffer of specified size.
2 Also takes args differently: pass one pointer to an array of strings
3 in addition to the format string which is separate.
4 Copyright (C) 1985, 2001-2011 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
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.
18 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/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <setjmp.h>
27 #ifdef STDC_HEADERS
28 #include <float.h>
29 #endif
31 #include <unistd.h>
33 #include "lisp.h"
35 /* Since we use the macro CHAR_HEAD_P, we have to include this, but
36 don't have to include others because CHAR_HEAD_P does not contains
37 another macro. */
38 #include "character.h"
40 #ifndef DBL_MAX_10_EXP
41 #define DBL_MAX_10_EXP 308 /* IEEE double */
42 #endif
44 /* Generate output from a format-spec FORMAT,
45 terminated at position FORMAT_END.
46 Output goes in BUFFER, which has room for BUFSIZE chars.
47 If the output does not fit, truncate it to fit.
48 Returns the number of bytes stored into BUFFER.
49 ARGS points to the vector of arguments, and NARGS says how many.
50 A double counts as two arguments.
51 String arguments are passed as C strings.
52 Integers are passed as C integers. */
54 EMACS_INT
55 doprnt (char *buffer, register int bufsize, const char *format,
56 const char *format_end, va_list ap)
58 const char *fmt = format; /* Pointer into format string */
59 register char *bufptr = buffer; /* Pointer into output buffer.. */
61 /* Use this for sprintf unless we need something really big. */
62 char tembuf[DBL_MAX_10_EXP + 100];
64 /* Size of sprintf_buffer. */
65 unsigned size_allocated = sizeof (tembuf);
67 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
68 char *sprintf_buffer = tembuf;
70 /* Buffer we have got with malloc. */
71 char *big_buffer = 0;
73 register int tem;
74 char *string;
75 char fixed_buffer[20]; /* Default buffer for small formatting. */
76 char *fmtcpy;
77 int minlen;
78 char charbuf[MAX_MULTIBYTE_LENGTH + 1]; /* Used for %c. */
80 if (format_end == 0)
81 format_end = format + strlen (format);
83 if ((format_end - format + 1) < sizeof (fixed_buffer))
84 fmtcpy = fixed_buffer;
85 else
86 fmtcpy = (char *) alloca (format_end - format + 1);
88 bufsize--;
90 /* Loop until end of format string or buffer full. */
91 while (fmt != format_end && bufsize > 0)
93 if (*fmt == '%') /* Check for a '%' character */
95 unsigned size_bound = 0;
96 EMACS_INT width; /* Columns occupied by STRING. */
98 fmt++;
99 /* Copy this one %-spec into fmtcpy. */
100 string = fmtcpy;
101 *string++ = '%';
102 while (1)
104 *string++ = *fmt;
105 if ('0' <= *fmt && *fmt <= '9')
107 /* Get an idea of how much space we might need.
108 This might be a field width or a precision; e.g.
109 %1.1000f and %1000.1f both might need 1000+ bytes.
110 Parse the width or precision, checking for overflow. */
111 unsigned n = *fmt - '0';
112 while ('0' <= fmt[1] && fmt[1] <= '9')
114 if (n * 10 + fmt[1] - '0' < n)
115 error ("Format width or precision too large");
116 n = n * 10 + fmt[1] - '0';
117 *string++ = *++fmt;
120 if (size_bound < n)
121 size_bound = n;
123 else if (*fmt == '-' || *fmt == ' ' || *fmt == '.' || *fmt == '+')
125 else
126 break;
127 fmt++;
129 *string = 0;
131 /* Make the size bound large enough to handle floating point formats
132 with large numbers. */
133 if (size_bound + DBL_MAX_10_EXP + 50 < size_bound)
134 error ("Format width or precision too large");
135 size_bound += DBL_MAX_10_EXP + 50;
137 /* Make sure we have that much. */
138 if (size_bound > size_allocated)
140 if (big_buffer)
141 big_buffer = (char *) xrealloc (big_buffer, size_bound);
142 else
143 big_buffer = (char *) xmalloc (size_bound);
144 sprintf_buffer = big_buffer;
145 size_allocated = size_bound;
147 minlen = 0;
148 switch (*fmt++)
150 default:
151 error ("Invalid format operation %%%c", fmt[-1]);
153 /* case 'b': */
154 case 'd':
155 case 'o':
156 case 'x':
157 if (sizeof (int) == sizeof (EMACS_INT))
159 else if (sizeof (long) == sizeof (EMACS_INT))
160 /* Insert an `l' the right place. */
161 string[1] = string[0],
162 string[0] = string[-1],
163 string[-1] = 'l',
164 string++;
165 else
166 abort ();
167 sprintf (sprintf_buffer, fmtcpy, va_arg(ap, char *));
168 /* Now copy into final output, truncating as nec. */
169 string = sprintf_buffer;
170 goto doit;
172 case 'f':
173 case 'e':
174 case 'g':
176 double d = va_arg(ap, double);
177 sprintf (sprintf_buffer, fmtcpy, d);
178 /* Now copy into final output, truncating as nec. */
179 string = sprintf_buffer;
180 goto doit;
183 case 'S':
184 string[-1] = 's';
185 case 's':
186 if (fmtcpy[1] != 's')
187 minlen = atoi (&fmtcpy[1]);
188 string = va_arg (ap, char *);
189 tem = strlen (string);
190 width = strwidth (string, tem);
191 goto doit1;
193 /* Copy string into final output, truncating if no room. */
194 doit:
195 /* Coming here means STRING contains ASCII only. */
196 width = tem = strlen (string);
197 doit1:
198 /* We have already calculated:
199 TEM -- length of STRING,
200 WIDTH -- columns occupied by STRING when displayed, and
201 MINLEN -- minimum columns of the output. */
202 if (minlen > 0)
204 while (minlen > width && bufsize > 0)
206 *bufptr++ = ' ';
207 bufsize--;
208 minlen--;
210 minlen = 0;
212 if (tem > bufsize)
214 /* Truncate the string at character boundary. */
215 tem = bufsize;
216 while (!CHAR_HEAD_P (string[tem - 1])) tem--;
217 memcpy (bufptr, string, tem);
218 /* We must calculate WIDTH again. */
219 width = strwidth (bufptr, tem);
221 else
222 memcpy (bufptr, string, tem);
223 bufptr += tem;
224 bufsize -= tem;
225 if (minlen < 0)
227 while (minlen < - width && bufsize > 0)
229 *bufptr++ = ' ';
230 bufsize--;
231 minlen++;
233 minlen = 0;
235 continue;
237 case 'c':
239 /* Sometimes for %c we pass a char, which would widen
240 to int. Sometimes we pass XFASTINT() or XINT()
241 values, which would be EMACS_INT. Let's hope that
242 both are passed the same way, otherwise we'll need
243 to rewrite callers. */
244 EMACS_INT chr = va_arg(ap, EMACS_INT);
245 tem = CHAR_STRING ((int) chr, (unsigned char *) charbuf);
246 string = charbuf;
247 string[tem] = 0;
248 width = strwidth (string, tem);
249 if (fmtcpy[1] != 'c')
250 minlen = atoi (&fmtcpy[1]);
251 goto doit1;
254 case '%':
255 fmt--; /* Drop thru and this % will be treated as normal */
260 /* Just some character; Copy it if the whole multi-byte form
261 fit in the buffer. */
262 char *save_bufptr = bufptr;
264 do { *bufptr++ = *fmt++; }
265 while (--bufsize > 0 && !CHAR_HEAD_P (*fmt));
266 if (!CHAR_HEAD_P (*fmt))
268 bufptr = save_bufptr;
269 break;
274 /* If we had to malloc something, free it. */
275 xfree (big_buffer);
277 *bufptr = 0; /* Make sure our string end with a '\0' */
278 return bufptr - buffer;