Mercurial > emacs
annotate src/doprnt.c @ 5163:220ec2f456e0
Comment change.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 25 Nov 1993 02:37:21 +0000 |
parents | b98e742f0b05 |
children | d9f096200099 |
rev | line source |
---|---|
49 | 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 | |
5036 | 23 #include <config.h> |
49 | 24 #include <stdio.h> |
25 #include <ctype.h> | |
26 | |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
27 /* Generate output from a format-spec FORMAT, |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
28 terminated at position FORMAT_END. |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
29 Output goes in BUFFER, which has room for BUFSIZE chars. |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
30 If the output does not fit, truncate it to fit. |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
31 Returns the number of characters stored into BUFFER. |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
32 ARGS points to the vector of arguments, and NARGS says how many. |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
33 A double counts as two arguments. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
34 |
49 | 35 doprnt (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.. */ | |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
46 |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
47 /* Use this for sprintf unless we need something really big. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
48 char tembuf[100]; |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
49 |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
50 /* Size of sprintf_buffer. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
51 int size_allocated = 100; |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
52 |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
53 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
54 char *sprintf_buffer = tembuf; |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
55 |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
56 /* Buffer we have got with malloc. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
57 char *big_buffer = 0; |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
58 |
49 | 59 register int tem; |
60 char *string; | |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
61 char fixed_buffer[20]; /* Default buffer for small formatting. */ |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
62 char *fmtcpy; |
49 | 63 int minlen; |
64 int size; /* Field width factor; e.g., %90d */ | |
65 | |
66 if (format_end == 0) | |
67 format_end = format + strlen (format); | |
68 | |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
69 if ((format_end - format + 1) < sizeof (fixed_buffer)) |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
70 fmtcpy = fixed_buffer; |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
71 else |
5053
b98e742f0b05
(doprnt): Cast the value alloca returns.
Richard M. Stallman <rms@gnu.org>
parents:
5036
diff
changeset
|
72 fmtcpy = (char *) alloca (format_end - format + 1); |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
73 |
49 | 74 bufsize--; |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
75 |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
76 /* Loop until end of format string or buffer full. */ |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
77 while (fmt != format_end && bufsize > 0) |
49 | 78 { |
79 if (*fmt == '%') /* Check for a '%' character */ | |
80 { | |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
81 int size_bound; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
82 |
49 | 83 fmt++; |
484 | 84 /* Copy this one %-spec into fmtcpy. */ |
49 | 85 string = fmtcpy; |
86 *string++ = '%'; | |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
87 while (1) |
49 | 88 { |
89 *string++ = *fmt; | |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
90 if (! (*fmt >= '0' && *fmt <= '9') |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
91 && *fmt != '-' && *fmt != ' '&& *fmt != '.') |
49 | 92 break; |
93 fmt++; | |
94 } | |
95 *string = 0; | |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
96 /* Get an idea of how much space we might need. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
97 size_bound = atoi (&fmtcpy[1]) + 50; |
4774
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
98 |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
99 /* Avoid pitfall of negative "size" parameter ("%-200d"). */ |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
100 if (size_bound < 0) |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
101 size_bound = -size_bound; |
8e36034f65e2
(doprnt): Use a fixed buffer to store the format
Brian Fox <bfox@gnu.org>
parents:
2439
diff
changeset
|
102 |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
103 /* Make sure we have that much. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
104 if (size_bound > size_allocated) |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
105 { |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
106 if (big_buffer) |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
107 big_buffer = (char *) xrealloc (big_buffer, size_bound); |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
108 else |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
109 big_buffer = (char *) xmalloc (size_bound); |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
110 sprintf_buffer = big_buffer; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
111 size_allocated = size_bound; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
112 } |
49 | 113 minlen = 0; |
114 switch (*fmt++) | |
115 { | |
116 default: | |
117 error ("Invalid format operation %%%c", fmt[-1]); | |
118 | |
119 /* case 'b': */ | |
120 case 'd': | |
121 case 'o': | |
122 case 'x': | |
123 if (cnt == nargs) | |
124 error ("Format string wants too many arguments"); | |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
125 sprintf (sprintf_buffer, fmtcpy, args[cnt++]); |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
126 /* Now copy into final output, truncating as nec. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
127 string = sprintf_buffer; |
49 | 128 goto doit; |
129 | |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
130 case 'f': |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
131 case 'e': |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
132 case 'g': |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
133 { |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
134 union { double d; char *half[2]; } u; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
135 if (cnt + 1 == nargs) |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
136 error ("Format string wants too many arguments"); |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
137 u.half[0] = args[cnt++]; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
138 u.half[1] = args[cnt++]; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
139 sprintf (sprintf_buffer, fmtcpy, u.d); |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
140 /* Now copy into final output, truncating as nec. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
141 string = sprintf_buffer; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
142 goto doit; |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
143 } |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
144 |
49 | 145 case 'S': |
146 string[-1] = 's'; | |
147 case 's': | |
148 if (cnt == nargs) | |
149 error ("Format string wants too many arguments"); | |
150 string = args[cnt++]; | |
151 if (fmtcpy[1] != 's') | |
152 minlen = atoi (&fmtcpy[1]); | |
153 /* Copy string into final output, truncating if no room. */ | |
154 doit: | |
155 tem = strlen (string); | |
156 if (minlen > 0) | |
157 { | |
158 while (minlen > tem && bufsize > 0) | |
159 { | |
160 *bufptr++ = ' '; | |
161 bufsize--; | |
162 minlen--; | |
163 } | |
164 minlen = 0; | |
165 } | |
166 if (tem > bufsize) | |
167 tem = bufsize; | |
168 strncpy (bufptr, string, tem); | |
169 bufptr += tem; | |
170 bufsize -= tem; | |
171 if (minlen < 0) | |
172 { | |
173 while (minlen < - tem && bufsize > 0) | |
174 { | |
175 *bufptr++ = ' '; | |
176 bufsize--; | |
177 minlen++; | |
178 } | |
179 minlen = 0; | |
180 } | |
181 continue; | |
182 | |
183 case 'c': | |
184 if (cnt == nargs) | |
185 error ("Format string wants too many arguments"); | |
186 *bufptr++ = (int) args[cnt++]; | |
187 bufsize--; | |
188 continue; | |
189 | |
190 case '%': | |
191 fmt--; /* Drop thru and this % will be treated as normal */ | |
192 } | |
193 } | |
194 *bufptr++ = *fmt++; /* Just some characters; Copy 'em */ | |
195 bufsize--; | |
196 }; | |
197 | |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
198 /* If we had to malloc something, free it. */ |
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
199 if (big_buffer) |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
484
diff
changeset
|
200 xfree (big_buffer); |
147
0f50f1badd75
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
116
diff
changeset
|
201 |
49 | 202 *bufptr = 0; /* Make sure our string end with a '\0' */ |
203 return bufptr - buffer; | |
204 } |