(doprnt1): Add a cast.
[bpt/emacs.git] / src / doprnt.c
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 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
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 2, or (at your option)
11 any later version.
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
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22
23 #include <config.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include "lisp.h"
27
28 extern long *xmalloc (), *xrealloc ();
29
30 static int doprnt1 ();
31
32 /* Generate output from a format-spec FORMAT,
33 terminated at position FORMAT_END.
34 Output goes in BUFFER, which has room for BUFSIZE chars.
35 If the output does not fit, truncate it to fit.
36 Returns the number of characters stored into BUFFER.
37 ARGS points to the vector of arguments, and NARGS says how many.
38 A double counts as two arguments.
39 String arguments are passed as C strings.
40 Integers are passed as C integers. */
41
42 doprnt (buffer, bufsize, format, format_end, nargs, args)
43 char *buffer;
44 register int bufsize;
45 char *format;
46 char *format_end;
47 int nargs;
48 char **args;
49 {
50 return doprnt1 (0, buffer, bufsize, format, format_end, nargs, args);
51 }
52
53 /* Like doprnt except that strings in ARGS are passed
54 as Lisp_Object. */
55
56 doprnt_lisp (buffer, bufsize, format, format_end, nargs, args)
57 char *buffer;
58 register int bufsize;
59 char *format;
60 char *format_end;
61 int nargs;
62 char **args;
63 {
64 return doprnt1 (1, buffer, bufsize, format, format_end, nargs, args);
65 }
66
67 static int
68 doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args)
69 int lispstrings;
70 char *buffer;
71 register int bufsize;
72 char *format;
73 char *format_end;
74 int nargs;
75 char **args;
76 {
77 int cnt = 0; /* Number of arg to gobble next */
78 register char *fmt = format; /* Pointer into format string */
79 register char *bufptr = buffer; /* Pointer into output buffer.. */
80
81 /* Use this for sprintf unless we need something really big. */
82 char tembuf[100];
83
84 /* Size of sprintf_buffer. */
85 int size_allocated = 100;
86
87 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
88 char *sprintf_buffer = tembuf;
89
90 /* Buffer we have got with malloc. */
91 char *big_buffer = 0;
92
93 register int tem;
94 char *string;
95 char fixed_buffer[20]; /* Default buffer for small formatting. */
96 char *fmtcpy;
97 int minlen;
98 int size; /* Field width factor; e.g., %90d */
99 char charbuf[2]; /* Used for %c. */
100
101 if (format_end == 0)
102 format_end = format + strlen (format);
103
104 if ((format_end - format + 1) < sizeof (fixed_buffer))
105 fmtcpy = fixed_buffer;
106 else
107 fmtcpy = (char *) alloca (format_end - format + 1);
108
109 bufsize--;
110
111 /* Loop until end of format string or buffer full. */
112 while (fmt != format_end && bufsize > 0)
113 {
114 if (*fmt == '%') /* Check for a '%' character */
115 {
116 int size_bound;
117
118 fmt++;
119 /* Copy this one %-spec into fmtcpy. */
120 string = fmtcpy;
121 *string++ = '%';
122 while (1)
123 {
124 *string++ = *fmt;
125 if (! (*fmt >= '0' && *fmt <= '9')
126 && *fmt != '-' && *fmt != ' '&& *fmt != '.')
127 break;
128 fmt++;
129 }
130 *string = 0;
131 /* Get an idea of how much space we might need. */
132 size_bound = atoi (&fmtcpy[1]);
133
134 /* Avoid pitfall of negative "size" parameter ("%-200d"). */
135 if (size_bound < 0)
136 size_bound = -size_bound;
137 size_bound += 50;
138
139 if (size_bound > (unsigned) (1 << (BITS_PER_INT - 1)))
140 error ("Format padding too large");
141
142 /* Make sure we have that much. */
143 if (size_bound > size_allocated)
144 {
145 if (big_buffer)
146 big_buffer = (char *) xrealloc (big_buffer, size_bound);
147 else
148 big_buffer = (char *) xmalloc (size_bound);
149 sprintf_buffer = big_buffer;
150 size_allocated = size_bound;
151 }
152 minlen = 0;
153 switch (*fmt++)
154 {
155 default:
156 error ("Invalid format operation %%%c", fmt[-1]);
157
158 /* case 'b': */
159 case 'd':
160 case 'o':
161 case 'x':
162 if (cnt == nargs)
163 error ("Not enough arguments for format string");
164 if (sizeof (int) == sizeof (EMACS_INT))
165 ;
166 else if (sizeof (long) == sizeof (EMACS_INT))
167 /* Insert an `l' the right place. */
168 string[1] = string[0],
169 string[0] = string[-1],
170 string[-1] = 'l',
171 string++;
172 else
173 abort ();
174 sprintf (sprintf_buffer, fmtcpy, args[cnt++]);
175 /* Now copy into final output, truncating as nec. */
176 string = sprintf_buffer;
177 goto doit;
178
179 case 'f':
180 case 'e':
181 case 'g':
182 {
183 union { double d; char *half[2]; } u;
184 if (cnt + 1 == nargs)
185 error ("not enough arguments for format string");
186 u.half[0] = args[cnt++];
187 u.half[1] = args[cnt++];
188 sprintf (sprintf_buffer, fmtcpy, u.d);
189 /* Now copy into final output, truncating as nec. */
190 string = sprintf_buffer;
191 goto doit;
192 }
193
194 case 'S':
195 string[-1] = 's';
196 case 's':
197 if (cnt == nargs)
198 error ("not enough arguments for format string");
199 if (fmtcpy[1] != 's')
200 minlen = atoi (&fmtcpy[1]);
201 if (lispstrings)
202 {
203 string = (char *) XSTRING (((Lisp_Object *) args)[cnt])->data;
204 tem = XSTRING (((Lisp_Object *) args)[cnt])->size;
205 cnt++;
206 }
207 else
208 {
209 string = args[cnt++];
210 tem = strlen (string);
211 }
212 goto doit1;
213
214 /* Copy string into final output, truncating if no room. */
215 doit:
216 tem = strlen (string);
217 doit1:
218 if (minlen > 0)
219 {
220 while (minlen > tem && bufsize > 0)
221 {
222 *bufptr++ = ' ';
223 bufsize--;
224 minlen--;
225 }
226 minlen = 0;
227 }
228 if (tem > bufsize)
229 tem = bufsize;
230 bcopy (string, bufptr, tem);
231 bufptr += tem;
232 bufsize -= tem;
233 if (minlen < 0)
234 {
235 while (minlen < - tem && bufsize > 0)
236 {
237 *bufptr++ = ' ';
238 bufsize--;
239 minlen++;
240 }
241 minlen = 0;
242 }
243 continue;
244
245 case 'c':
246 if (cnt == nargs)
247 error ("not enough arguments for format string");
248 *charbuf = (EMACS_INT) args[cnt++];
249 string = charbuf;
250 tem = 1;
251 if (fmtcpy[1] != 'c')
252 minlen = atoi (&fmtcpy[1]);
253 goto doit1;
254
255 case '%':
256 fmt--; /* Drop thru and this % will be treated as normal */
257 }
258 }
259 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */
260 bufsize--;
261 };
262
263 /* If we had to malloc something, free it. */
264 if (big_buffer)
265 xfree (big_buffer);
266
267 *bufptr = 0; /* Make sure our string end with a '\0' */
268 return bufptr - buffer;
269 }