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