(Freplace_match): New arg SUBEXP.
[bpt/emacs.git] / src / doprnt.c
CommitLineData
24f98398
JA
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
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
7c938215 10the Free Software Foundation; either version 2, or (at your option)
24f98398
JA
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
20the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22
6d291527 23#include <config.h>
24f98398
JA
24#include <stdio.h>
25#include <ctype.h>
26
1077c789
RS
27extern long *xmalloc (), *xrealloc ();
28
f4c730d3
RS
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
24f98398
JA
37doprnt (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.. */
03383aaf 48
f4c730d3
RS
49 /* Use this for sprintf unless we need something really big. */
50 char tembuf[100];
03383aaf 51
f4c730d3
RS
52 /* Size of sprintf_buffer. */
53 int size_allocated = 100;
03383aaf 54
f4c730d3
RS
55 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
56 char *sprintf_buffer = tembuf;
03383aaf 57
f4c730d3
RS
58 /* Buffer we have got with malloc. */
59 char *big_buffer = 0;
03383aaf 60
24f98398
JA
61 register int tem;
62 char *string;
03383aaf
BF
63 char fixed_buffer[20]; /* Default buffer for small formatting. */
64 char *fmtcpy;
24f98398
JA
65 int minlen;
66 int size; /* Field width factor; e.g., %90d */
35a65fce 67 char charbuf[2]; /* Used for %c. */
24f98398
JA
68
69 if (format_end == 0)
70 format_end = format + strlen (format);
71
03383aaf
BF
72 if ((format_end - format + 1) < sizeof (fixed_buffer))
73 fmtcpy = fixed_buffer;
74 else
91098d38 75 fmtcpy = (char *) alloca (format_end - format + 1);
03383aaf 76
24f98398 77 bufsize--;
03383aaf
BF
78
79 /* Loop until end of format string or buffer full. */
80 while (fmt != format_end && bufsize > 0)
24f98398
JA
81 {
82 if (*fmt == '%') /* Check for a '%' character */
83 {
f4c730d3
RS
84 int size_bound;
85
24f98398 86 fmt++;
d427b66a 87 /* Copy this one %-spec into fmtcpy. */
24f98398
JA
88 string = fmtcpy;
89 *string++ = '%';
03383aaf 90 while (1)
24f98398
JA
91 {
92 *string++ = *fmt;
03383aaf
BF
93 if (! (*fmt >= '0' && *fmt <= '9')
94 && *fmt != '-' && *fmt != ' '&& *fmt != '.')
24f98398
JA
95 break;
96 fmt++;
97 }
98 *string = 0;
f4c730d3 99 /* Get an idea of how much space we might need. */
dfd351ef 100 size_bound = atoi (&fmtcpy[1]);
03383aaf
BF
101
102 /* Avoid pitfall of negative "size" parameter ("%-200d"). */
103 if (size_bound < 0)
104 size_bound = -size_bound;
dfd351ef 105 size_bound += 50;
03383aaf 106
6e951728
RS
107 if (size_bound > bufsize)
108 /* This is certainly enough that it doesn't matter
109 to lose the rest. */
110 size_bound = bufsize + 100;
111
f4c730d3
RS
112 /* Make sure we have that much. */
113 if (size_bound > size_allocated)
114 {
115 if (big_buffer)
116 big_buffer = (char *) xrealloc (big_buffer, size_bound);
117 else
118 big_buffer = (char *) xmalloc (size_bound);
119 sprintf_buffer = big_buffer;
120 size_allocated = size_bound;
121 }
24f98398
JA
122 minlen = 0;
123 switch (*fmt++)
124 {
125 default:
126 error ("Invalid format operation %%%c", fmt[-1]);
127
128/* case 'b': */
129 case 'd':
130 case 'o':
131 case 'x':
132 if (cnt == nargs)
6e951728 133 error ("Not enough arguments for format string");
f9fa352f
RS
134 if (sizeof (int) == sizeof (EMACS_INT))
135 ;
136 else if (sizeof (long) == sizeof (EMACS_INT))
137 /* Insert an `l' the right place. */
138 string[1] = string[0],
139 string[0] = string[-1],
140 string[-1] = 'l',
141 string++;
142 else
143 abort ();
f4c730d3
RS
144 sprintf (sprintf_buffer, fmtcpy, args[cnt++]);
145 /* Now copy into final output, truncating as nec. */
146 string = sprintf_buffer;
24f98398
JA
147 goto doit;
148
f4c730d3
RS
149 case 'f':
150 case 'e':
151 case 'g':
152 {
153 union { double d; char *half[2]; } u;
154 if (cnt + 1 == nargs)
ee0c28e3 155 error ("not enough arguments for format string");
f4c730d3
RS
156 u.half[0] = args[cnt++];
157 u.half[1] = args[cnt++];
158 sprintf (sprintf_buffer, fmtcpy, u.d);
159 /* Now copy into final output, truncating as nec. */
160 string = sprintf_buffer;
161 goto doit;
162 }
163
24f98398
JA
164 case 'S':
165 string[-1] = 's';
166 case 's':
167 if (cnt == nargs)
ee0c28e3 168 error ("not enough arguments for format string");
24f98398
JA
169 string = args[cnt++];
170 if (fmtcpy[1] != 's')
171 minlen = atoi (&fmtcpy[1]);
172 /* Copy string into final output, truncating if no room. */
173 doit:
174 tem = strlen (string);
35a65fce 175 doit1:
24f98398
JA
176 if (minlen > 0)
177 {
178 while (minlen > tem && bufsize > 0)
179 {
180 *bufptr++ = ' ';
181 bufsize--;
182 minlen--;
183 }
184 minlen = 0;
185 }
186 if (tem > bufsize)
187 tem = bufsize;
188 strncpy (bufptr, string, tem);
189 bufptr += tem;
190 bufsize -= tem;
191 if (minlen < 0)
192 {
193 while (minlen < - tem && bufsize > 0)
194 {
195 *bufptr++ = ' ';
196 bufsize--;
197 minlen++;
198 }
199 minlen = 0;
200 }
201 continue;
202
203 case 'c':
204 if (cnt == nargs)
ee0c28e3 205 error ("not enough arguments for format string");
1077c789 206 *charbuf = (EMACS_INT) args[cnt++];
35a65fce
RS
207 string = charbuf;
208 tem = 1;
209 if (fmtcpy[1] != 'c')
210 minlen = atoi (&fmtcpy[1]);
211 goto doit1;
24f98398
JA
212
213 case '%':
214 fmt--; /* Drop thru and this % will be treated as normal */
215 }
216 }
217 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */
218 bufsize--;
219 };
220
f4c730d3
RS
221 /* If we had to malloc something, free it. */
222 if (big_buffer)
9ac0d9e0 223 xfree (big_buffer);
f4c730d3 224
24f98398
JA
225 *bufptr = 0; /* Make sure our string end with a '\0' */
226 return bufptr - buffer;
227}