(xmalloc, xrealloc): Declare them here.
[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 1, 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
27 extern long *xmalloc (), *xrealloc ();
28
29 /* Generate output from a format-spec FORMAT,
30 terminated at position FORMAT_END.
31 Output goes in BUFFER, which has room for BUFSIZE chars.
32 If the output does not fit, truncate it to fit.
33 Returns the number of characters stored into BUFFER.
34 ARGS points to the vector of arguments, and NARGS says how many.
35 A double counts as two arguments. */
36
37 doprnt (buffer, bufsize, format, format_end, nargs, args)
38 char *buffer;
39 register int bufsize;
40 char *format;
41 char *format_end;
42 int nargs;
43 char **args;
44 {
45 int cnt = 0; /* Number of arg to gobble next */
46 register char *fmt = format; /* Pointer into format string */
47 register char *bufptr = buffer; /* Pointer into output buffer.. */
48
49 /* Use this for sprintf unless we need something really big. */
50 char tembuf[100];
51
52 /* Size of sprintf_buffer. */
53 int size_allocated = 100;
54
55 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
56 char *sprintf_buffer = tembuf;
57
58 /* Buffer we have got with malloc. */
59 char *big_buffer = 0;
60
61 register int tem;
62 char *string;
63 char fixed_buffer[20]; /* Default buffer for small formatting. */
64 char *fmtcpy;
65 int minlen;
66 int size; /* Field width factor; e.g., %90d */
67 char charbuf[2]; /* Used for %c. */
68
69 if (format_end == 0)
70 format_end = format + strlen (format);
71
72 if ((format_end - format + 1) < sizeof (fixed_buffer))
73 fmtcpy = fixed_buffer;
74 else
75 fmtcpy = (char *) alloca (format_end - format + 1);
76
77 bufsize--;
78
79 /* Loop until end of format string or buffer full. */
80 while (fmt != format_end && bufsize > 0)
81 {
82 if (*fmt == '%') /* Check for a '%' character */
83 {
84 int size_bound;
85
86 fmt++;
87 /* Copy this one %-spec into fmtcpy. */
88 string = fmtcpy;
89 *string++ = '%';
90 while (1)
91 {
92 *string++ = *fmt;
93 if (! (*fmt >= '0' && *fmt <= '9')
94 && *fmt != '-' && *fmt != ' '&& *fmt != '.')
95 break;
96 fmt++;
97 }
98 *string = 0;
99 /* Get an idea of how much space we might need. */
100 size_bound = atoi (&fmtcpy[1]);
101
102 /* Avoid pitfall of negative "size" parameter ("%-200d"). */
103 if (size_bound < 0)
104 size_bound = -size_bound;
105 size_bound += 50;
106
107 /* Make sure we have that much. */
108 if (size_bound > size_allocated)
109 {
110 if (big_buffer)
111 big_buffer = (char *) xrealloc (big_buffer, size_bound);
112 else
113 big_buffer = (char *) xmalloc (size_bound);
114 sprintf_buffer = big_buffer;
115 size_allocated = size_bound;
116 }
117 minlen = 0;
118 switch (*fmt++)
119 {
120 default:
121 error ("Invalid format operation %%%c", fmt[-1]);
122
123 /* case 'b': */
124 case 'd':
125 case 'o':
126 case 'x':
127 if (cnt == nargs)
128 error ("not enough arguments for format string");
129 sprintf (sprintf_buffer, fmtcpy, args[cnt++]);
130 /* Now copy into final output, truncating as nec. */
131 string = sprintf_buffer;
132 goto doit;
133
134 case 'f':
135 case 'e':
136 case 'g':
137 {
138 union { double d; char *half[2]; } u;
139 if (cnt + 1 == nargs)
140 error ("not enough arguments for format string");
141 u.half[0] = args[cnt++];
142 u.half[1] = args[cnt++];
143 sprintf (sprintf_buffer, fmtcpy, u.d);
144 /* Now copy into final output, truncating as nec. */
145 string = sprintf_buffer;
146 goto doit;
147 }
148
149 case 'S':
150 string[-1] = 's';
151 case 's':
152 if (cnt == nargs)
153 error ("not enough arguments for format string");
154 string = args[cnt++];
155 if (fmtcpy[1] != 's')
156 minlen = atoi (&fmtcpy[1]);
157 /* Copy string into final output, truncating if no room. */
158 doit:
159 tem = strlen (string);
160 doit1:
161 if (minlen > 0)
162 {
163 while (minlen > tem && bufsize > 0)
164 {
165 *bufptr++ = ' ';
166 bufsize--;
167 minlen--;
168 }
169 minlen = 0;
170 }
171 if (tem > bufsize)
172 tem = bufsize;
173 strncpy (bufptr, string, tem);
174 bufptr += tem;
175 bufsize -= tem;
176 if (minlen < 0)
177 {
178 while (minlen < - tem && bufsize > 0)
179 {
180 *bufptr++ = ' ';
181 bufsize--;
182 minlen++;
183 }
184 minlen = 0;
185 }
186 continue;
187
188 case 'c':
189 if (cnt == nargs)
190 error ("not enough arguments for format string");
191 *charbuf = (EMACS_INT) args[cnt++];
192 string = charbuf;
193 tem = 1;
194 if (fmtcpy[1] != 'c')
195 minlen = atoi (&fmtcpy[1]);
196 goto doit1;
197
198 case '%':
199 fmt--; /* Drop thru and this % will be treated as normal */
200 }
201 }
202 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */
203 bufsize--;
204 };
205
206 /* If we had to malloc something, free it. */
207 if (big_buffer)
208 xfree (big_buffer);
209
210 *bufptr = 0; /* Make sure our string end with a '\0' */
211 return bufptr - buffer;
212 }