diff options
| author | Paul Eggert | 2011-04-10 09:33:22 -0700 |
|---|---|---|
| committer | Paul Eggert | 2011-04-10 09:33:22 -0700 |
| commit | 12020a9e6dcfc2213e8bbb0fec259c1ed1202f30 (patch) | |
| tree | 7e7d37938825e48d316572ce3a85930b44369fdb /src | |
| parent | 69d9a57ddcfb6d9b51ce321bd2557881c6784151 (diff) | |
| download | emacs-12020a9e6dcfc2213e8bbb0fec259c1ed1202f30.tar.gz emacs-12020a9e6dcfc2213e8bbb0fec259c1ed1202f30.zip | |
Keep doprnt.c around for now, as we might revamp and reuse it.
Diffstat (limited to 'src')
| -rw-r--r-- | src/ChangeLog | 5 | ||||
| -rw-r--r-- | src/doprnt.c | 279 |
2 files changed, 282 insertions, 2 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index d3a387d1e63..e9e9abd4876 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -38,8 +38,9 @@ | |||
| 38 | 38 | ||
| 39 | * eval.c (verror): Initial buffer size is 4000 (not 200) bytes. | 39 | * eval.c (verror): Initial buffer size is 4000 (not 200) bytes. |
| 40 | 40 | ||
| 41 | Remove the doprnt implementation, as Emacs now uses vsnprintf. | 41 | Remove invocations of doprnt, as Emacs now uses vsnprintf. |
| 42 | * doprnt.c: Remove. | 42 | But keep the doprint source code for now, as we might revamp it |
| 43 | and use it again (Bug#8435). | ||
| 43 | * lisp.h (doprnt): Remove. | 44 | * lisp.h (doprnt): Remove. |
| 44 | * Makefile.in (base_obj): Remove doprnt.o. | 45 | * Makefile.in (base_obj): Remove doprnt.o. |
| 45 | * deps.mk (doprnt.o): Remove. | 46 | * deps.mk (doprnt.o): Remove. |
diff --git a/src/doprnt.c b/src/doprnt.c new file mode 100644 index 00000000000..36eb272caae --- /dev/null +++ b/src/doprnt.c | |||
| @@ -0,0 +1,279 @@ | |||
| 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, 2001-2011 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 3 of the License, or | ||
| 11 | (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */ | ||
| 20 | |||
| 21 | |||
| 22 | #include <config.h> | ||
| 23 | #include <stdio.h> | ||
| 24 | #include <ctype.h> | ||
| 25 | #include <setjmp.h> | ||
| 26 | |||
| 27 | #ifdef STDC_HEADERS | ||
| 28 | #include <float.h> | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #include <unistd.h> | ||
| 32 | |||
| 33 | #include "lisp.h" | ||
| 34 | |||
| 35 | /* Since we use the macro CHAR_HEAD_P, we have to include this, but | ||
| 36 | don't have to include others because CHAR_HEAD_P does not contains | ||
| 37 | another macro. */ | ||
| 38 | #include "character.h" | ||
| 39 | |||
| 40 | #ifndef DBL_MAX_10_EXP | ||
| 41 | #define DBL_MAX_10_EXP 308 /* IEEE double */ | ||
| 42 | #endif | ||
| 43 | |||
| 44 | /* Generate output from a format-spec FORMAT, | ||
| 45 | terminated at position FORMAT_END. | ||
| 46 | Output goes in BUFFER, which has room for BUFSIZE chars. | ||
| 47 | If the output does not fit, truncate it to fit. | ||
| 48 | Returns the number of bytes stored into BUFFER. | ||
| 49 | ARGS points to the vector of arguments, and NARGS says how many. | ||
| 50 | A double counts as two arguments. | ||
| 51 | String arguments are passed as C strings. | ||
| 52 | Integers are passed as C integers. */ | ||
| 53 | |||
| 54 | EMACS_INT | ||
| 55 | doprnt (char *buffer, register int bufsize, const char *format, | ||
| 56 | const char *format_end, va_list ap) | ||
| 57 | { | ||
| 58 | const char *fmt = format; /* Pointer into format string */ | ||
| 59 | register char *bufptr = buffer; /* Pointer into output buffer.. */ | ||
| 60 | |||
| 61 | /* Use this for sprintf unless we need something really big. */ | ||
| 62 | char tembuf[DBL_MAX_10_EXP + 100]; | ||
| 63 | |||
| 64 | /* Size of sprintf_buffer. */ | ||
| 65 | unsigned size_allocated = sizeof (tembuf); | ||
| 66 | |||
| 67 | /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */ | ||
| 68 | char *sprintf_buffer = tembuf; | ||
| 69 | |||
| 70 | /* Buffer we have got with malloc. */ | ||
| 71 | char *big_buffer = 0; | ||
| 72 | |||
| 73 | register int tem; | ||
| 74 | char *string; | ||
| 75 | char fixed_buffer[20]; /* Default buffer for small formatting. */ | ||
| 76 | char *fmtcpy; | ||
| 77 | int minlen; | ||
| 78 | char charbuf[MAX_MULTIBYTE_LENGTH + 1]; /* Used for %c. */ | ||
| 79 | |||
| 80 | if (format_end == 0) | ||
| 81 | format_end = format + strlen (format); | ||
| 82 | |||
| 83 | if ((format_end - format + 1) < sizeof (fixed_buffer)) | ||
| 84 | fmtcpy = fixed_buffer; | ||
| 85 | else | ||
| 86 | fmtcpy = (char *) alloca (format_end - format + 1); | ||
| 87 | |||
| 88 | bufsize--; | ||
| 89 | |||
| 90 | /* Loop until end of format string or buffer full. */ | ||
| 91 | while (fmt != format_end && bufsize > 0) | ||
| 92 | { | ||
| 93 | if (*fmt == '%') /* Check for a '%' character */ | ||
| 94 | { | ||
| 95 | unsigned size_bound = 0; | ||
| 96 | EMACS_INT width; /* Columns occupied by STRING. */ | ||
| 97 | |||
| 98 | fmt++; | ||
| 99 | /* Copy this one %-spec into fmtcpy. */ | ||
| 100 | string = fmtcpy; | ||
| 101 | *string++ = '%'; | ||
| 102 | while (1) | ||
| 103 | { | ||
| 104 | *string++ = *fmt; | ||
| 105 | if ('0' <= *fmt && *fmt <= '9') | ||
| 106 | { | ||
| 107 | /* Get an idea of how much space we might need. | ||
| 108 | This might be a field width or a precision; e.g. | ||
| 109 | %1.1000f and %1000.1f both might need 1000+ bytes. | ||
| 110 | Parse the width or precision, checking for overflow. */ | ||
| 111 | unsigned n = *fmt - '0'; | ||
| 112 | while ('0' <= fmt[1] && fmt[1] <= '9') | ||
| 113 | { | ||
| 114 | if (n * 10 + fmt[1] - '0' < n) | ||
| 115 | error ("Format width or precision too large"); | ||
| 116 | n = n * 10 + fmt[1] - '0'; | ||
| 117 | *string++ = *++fmt; | ||
| 118 | } | ||
| 119 | |||
| 120 | if (size_bound < n) | ||
| 121 | size_bound = n; | ||
| 122 | } | ||
| 123 | else if (*fmt == '-' || *fmt == ' ' || *fmt == '.' || *fmt == '+') | ||
| 124 | ; | ||
| 125 | else | ||
| 126 | break; | ||
| 127 | fmt++; | ||
| 128 | } | ||
| 129 | *string = 0; | ||
| 130 | |||
| 131 | /* Make the size bound large enough to handle floating point formats | ||
| 132 | with large numbers. */ | ||
| 133 | if (size_bound + DBL_MAX_10_EXP + 50 < size_bound) | ||
| 134 | error ("Format width or precision too large"); | ||
| 135 | size_bound += DBL_MAX_10_EXP + 50; | ||
| 136 | |||
| 137 | /* Make sure we have that much. */ | ||
| 138 | if (size_bound > size_allocated) | ||
| 139 | { | ||
| 140 | if (big_buffer) | ||
| 141 | big_buffer = (char *) xrealloc (big_buffer, size_bound); | ||
| 142 | else | ||
| 143 | big_buffer = (char *) xmalloc (size_bound); | ||
| 144 | sprintf_buffer = big_buffer; | ||
| 145 | size_allocated = size_bound; | ||
| 146 | } | ||
| 147 | minlen = 0; | ||
| 148 | switch (*fmt++) | ||
| 149 | { | ||
| 150 | default: | ||
| 151 | error ("Invalid format operation %%%c", fmt[-1]); | ||
| 152 | |||
| 153 | /* case 'b': */ | ||
| 154 | case 'd': | ||
| 155 | case 'o': | ||
| 156 | case 'x': | ||
| 157 | if (sizeof (int) == sizeof (EMACS_INT)) | ||
| 158 | ; | ||
| 159 | else if (sizeof (long) == sizeof (EMACS_INT)) | ||
| 160 | /* Insert an `l' the right place. */ | ||
| 161 | string[1] = string[0], | ||
| 162 | string[0] = string[-1], | ||
| 163 | string[-1] = 'l', | ||
| 164 | string++; | ||
| 165 | else | ||
| 166 | abort (); | ||
| 167 | sprintf (sprintf_buffer, fmtcpy, va_arg(ap, char *)); | ||
| 168 | /* Now copy into final output, truncating as nec. */ | ||
| 169 | string = sprintf_buffer; | ||
| 170 | goto doit; | ||
| 171 | |||
| 172 | case 'f': | ||
| 173 | case 'e': | ||
| 174 | case 'g': | ||
| 175 | { | ||
| 176 | double d = va_arg(ap, double); | ||
| 177 | sprintf (sprintf_buffer, fmtcpy, d); | ||
| 178 | /* Now copy into final output, truncating as nec. */ | ||
| 179 | string = sprintf_buffer; | ||
| 180 | goto doit; | ||
| 181 | } | ||
| 182 | |||
| 183 | case 'S': | ||
| 184 | string[-1] = 's'; | ||
| 185 | case 's': | ||
| 186 | if (fmtcpy[1] != 's') | ||
| 187 | minlen = atoi (&fmtcpy[1]); | ||
| 188 | string = va_arg (ap, char *); | ||
| 189 | tem = strlen (string); | ||
| 190 | width = strwidth (string, tem); | ||
| 191 | goto doit1; | ||
| 192 | |||
| 193 | /* Copy string into final output, truncating if no room. */ | ||
| 194 | doit: | ||
| 195 | /* Coming here means STRING contains ASCII only. */ | ||
| 196 | width = tem = strlen (string); | ||
| 197 | doit1: | ||
| 198 | /* We have already calculated: | ||
| 199 | TEM -- length of STRING, | ||
| 200 | WIDTH -- columns occupied by STRING when displayed, and | ||
| 201 | MINLEN -- minimum columns of the output. */ | ||
| 202 | if (minlen > 0) | ||
| 203 | { | ||
| 204 | while (minlen > width && bufsize > 0) | ||
| 205 | { | ||
| 206 | *bufptr++ = ' '; | ||
| 207 | bufsize--; | ||
| 208 | minlen--; | ||
| 209 | } | ||
| 210 | minlen = 0; | ||
| 211 | } | ||
| 212 | if (tem > bufsize) | ||
| 213 | { | ||
| 214 | /* Truncate the string at character boundary. */ | ||
| 215 | tem = bufsize; | ||
| 216 | while (!CHAR_HEAD_P (string[tem - 1])) tem--; | ||
| 217 | memcpy (bufptr, string, tem); | ||
| 218 | /* We must calculate WIDTH again. */ | ||
| 219 | width = strwidth (bufptr, tem); | ||
| 220 | } | ||
| 221 | else | ||
| 222 | memcpy (bufptr, string, tem); | ||
| 223 | bufptr += tem; | ||
| 224 | bufsize -= tem; | ||
| 225 | if (minlen < 0) | ||
| 226 | { | ||
| 227 | while (minlen < - width && bufsize > 0) | ||
| 228 | { | ||
| 229 | *bufptr++ = ' '; | ||
| 230 | bufsize--; | ||
| 231 | minlen++; | ||
| 232 | } | ||
| 233 | minlen = 0; | ||
| 234 | } | ||
| 235 | continue; | ||
| 236 | |||
| 237 | case 'c': | ||
| 238 | { | ||
| 239 | /* Sometimes for %c we pass a char, which would widen | ||
| 240 | to int. Sometimes we pass XFASTINT() or XINT() | ||
| 241 | values, which would be EMACS_INT. Let's hope that | ||
| 242 | both are passed the same way, otherwise we'll need | ||
| 243 | to rewrite callers. */ | ||
| 244 | EMACS_INT chr = va_arg(ap, EMACS_INT); | ||
| 245 | tem = CHAR_STRING ((int) chr, (unsigned char *) charbuf); | ||
| 246 | string = charbuf; | ||
| 247 | string[tem] = 0; | ||
| 248 | width = strwidth (string, tem); | ||
| 249 | if (fmtcpy[1] != 'c') | ||
| 250 | minlen = atoi (&fmtcpy[1]); | ||
| 251 | goto doit1; | ||
| 252 | } | ||
| 253 | |||
| 254 | case '%': | ||
| 255 | fmt--; /* Drop thru and this % will be treated as normal */ | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | { | ||
| 260 | /* Just some character; Copy it if the whole multi-byte form | ||
| 261 | fit in the buffer. */ | ||
| 262 | char *save_bufptr = bufptr; | ||
| 263 | |||
| 264 | do { *bufptr++ = *fmt++; } | ||
| 265 | while (--bufsize > 0 && !CHAR_HEAD_P (*fmt)); | ||
| 266 | if (!CHAR_HEAD_P (*fmt)) | ||
| 267 | { | ||
| 268 | bufptr = save_bufptr; | ||
| 269 | break; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | }; | ||
| 273 | |||
| 274 | /* If we had to malloc something, free it. */ | ||
| 275 | xfree (big_buffer); | ||
| 276 | |||
| 277 | *bufptr = 0; /* Make sure our string end with a '\0' */ | ||
| 278 | return bufptr - buffer; | ||
| 279 | } | ||