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