428
|
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) 1995 Free Software Foundation, Inc.
|
|
5 Rewritten by mly to use varargs.h.
|
|
6 Rewritten from scratch by Ben Wing (February 1995) for Mule; expanded
|
|
7 to full printf spec.
|
|
8
|
|
9 This file is part of XEmacs.
|
|
10
|
|
11 XEmacs is free software; you can redistribute it and/or modify it
|
|
12 under the terms of the GNU General Public License as published by the
|
|
13 Free Software Foundation; either version 2, or (at your option) any
|
|
14 later version.
|
|
15
|
|
16 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
19 for more details.
|
|
20
|
|
21 You should have received a copy of the GNU General Public License
|
|
22 along with XEmacs; see the file COPYING. If not, write to
|
|
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 Boston, MA 02111-1307, USA. */
|
|
25
|
|
26 /* Synched up with: Rewritten. Not in FSF. */
|
|
27
|
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
|
31 #include "buffer.h"
|
|
32 #include "lstream.h"
|
|
33
|
446
|
34 static const char * const valid_flags = "-+ #0";
|
|
35 static const char * const valid_converters = "dic" "ouxX" "feEgG" "sS";
|
|
36 static const char * const int_converters = "dic";
|
|
37 static const char * const unsigned_int_converters = "ouxX";
|
|
38 static const char * const double_converters = "feEgG";
|
|
39 static const char * const string_converters = "sS";
|
428
|
40
|
|
41 typedef struct printf_spec printf_spec;
|
|
42 struct printf_spec
|
|
43 {
|
|
44 int argnum; /* which argument does this spec want? This is one-based:
|
|
45 The first argument given is numbered 1, the second
|
|
46 is 2, etc. This is to handle %##$x-type specs. */
|
|
47 int minwidth;
|
|
48 int precision;
|
|
49 unsigned int minus_flag:1;
|
|
50 unsigned int plus_flag:1;
|
|
51 unsigned int space_flag:1;
|
|
52 unsigned int number_flag:1;
|
|
53 unsigned int zero_flag:1;
|
|
54 unsigned int h_flag:1;
|
|
55 unsigned int l_flag:1;
|
|
56 unsigned int forwarding_precision:1;
|
|
57 char converter; /* converter character or 0 for dummy marker
|
|
58 indicating literal text at the end of the
|
|
59 specification */
|
|
60 Bytecount text_before; /* position of the first character of the
|
|
61 block of literal text before this spec */
|
|
62 Bytecount text_before_len; /* length of that text */
|
|
63 };
|
|
64
|
|
65 typedef union printf_arg printf_arg;
|
|
66 union printf_arg
|
|
67 {
|
|
68 long l;
|
|
69 unsigned long ul;
|
|
70 double d;
|
|
71 Bufbyte *bp;
|
|
72 };
|
|
73
|
|
74 /* We maintain a list of all the % specs in the specification,
|
|
75 along with the offset and length of the block of literal text
|
|
76 before each spec. In addition, we have a "dummy" spec that
|
|
77 represents all the literal text at the end of the specification.
|
|
78 Its converter is 0. */
|
|
79
|
|
80 typedef struct
|
|
81 {
|
|
82 Dynarr_declare (struct printf_spec);
|
|
83 } printf_spec_dynarr;
|
|
84
|
|
85 typedef struct
|
|
86 {
|
|
87 Dynarr_declare (union printf_arg);
|
|
88 } printf_arg_dynarr;
|
|
89
|
448
|
90 /* Append STRING (of length LEN bytes) to STREAM.
|
|
91 MINLEN is the minimum field width.
|
|
92 If MINUS_FLAG is set, left-justify the string in its field;
|
|
93 otherwise, right-justify.
|
|
94 If ZERO_FLAG is set, pad with 0's; otherwise pad with spaces.
|
|
95 If MAXLEN is non-negative, the string is first truncated on the
|
|
96 right to that many characters.
|
428
|
97
|
|
98 Note that MINLEN and MAXLEN are Charcounts but LEN is a Bytecount. */
|
|
99
|
|
100 static void
|
442
|
101 doprnt_1 (Lisp_Object stream, const Bufbyte *string, Bytecount len,
|
428
|
102 Charcount minlen, Charcount maxlen, int minus_flag, int zero_flag)
|
|
103 {
|
|
104 Lstream *lstr = XLSTREAM (stream);
|
448
|
105 Charcount cclen = bytecount_to_charcount (string, len);
|
|
106 int to_add = minlen - cclen;
|
428
|
107
|
|
108 /* Padding at beginning to right-justify ... */
|
448
|
109 if (!minus_flag)
|
|
110 while (to_add-- > 0)
|
|
111 Lstream_putc (lstr, zero_flag ? '0' : ' ');
|
428
|
112
|
448
|
113 if (0 <= maxlen && maxlen < cclen)
|
|
114 len = charcount_to_bytecount (string, maxlen);
|
428
|
115 Lstream_write (lstr, string, len);
|
|
116
|
|
117 /* Padding at end to left-justify ... */
|
448
|
118 if (minus_flag)
|
|
119 while (to_add-- > 0)
|
|
120 Lstream_putc (lstr, zero_flag ? '0' : ' ');
|
428
|
121 }
|
|
122
|
442
|
123 static const Bufbyte *
|
|
124 parse_off_posnum (const Bufbyte *start, const Bufbyte *end, int *returned_num)
|
428
|
125 {
|
|
126 Bufbyte arg_convert[100];
|
|
127 REGISTER Bufbyte *arg_ptr = arg_convert;
|
|
128
|
|
129 *returned_num = -1;
|
|
130 while (start != end && isdigit (*start))
|
|
131 {
|
647
|
132 if (arg_ptr - arg_convert >= (int) sizeof (arg_convert) - 1)
|
|
133 syntax_error ("Format converter number too large", Qunbound);
|
428
|
134 *arg_ptr++ = *start++;
|
|
135 }
|
|
136 *arg_ptr = '\0';
|
|
137 if (arg_convert != arg_ptr)
|
|
138 *returned_num = atoi ((char *) arg_convert);
|
|
139 return start;
|
|
140 }
|
|
141
|
|
142 #define NEXT_ASCII_BYTE(ch) \
|
|
143 do { \
|
|
144 if (fmt == fmt_end) \
|
563
|
145 syntax_error ("Premature end of format string", Qunbound); \
|
428
|
146 ch = *fmt; \
|
|
147 if (ch >= 0200) \
|
563
|
148 syntax_error ("Non-ASCII character in format converter spec", Qunbound); \
|
428
|
149 fmt++; \
|
|
150 } while (0)
|
|
151
|
|
152 #define RESOLVE_FLAG_CONFLICTS(spec) \
|
|
153 do { \
|
|
154 if (spec.space_flag && spec.plus_flag) \
|
|
155 spec.space_flag = 0; \
|
|
156 if (spec.zero_flag && spec.space_flag) \
|
|
157 spec.zero_flag = 0; \
|
|
158 } while (0)
|
|
159
|
|
160 static printf_spec_dynarr *
|
442
|
161 parse_doprnt_spec (const Bufbyte *format, Bytecount format_length)
|
428
|
162 {
|
442
|
163 const Bufbyte *fmt = format;
|
|
164 const Bufbyte *fmt_end = format + format_length;
|
428
|
165 printf_spec_dynarr *specs = Dynarr_new (printf_spec);
|
|
166 int prev_argnum = 0;
|
|
167
|
|
168 while (1)
|
|
169 {
|
|
170 struct printf_spec spec;
|
442
|
171 const Bufbyte *text_end;
|
428
|
172 Bufbyte ch;
|
|
173
|
|
174 xzero (spec);
|
|
175 if (fmt == fmt_end)
|
|
176 return specs;
|
|
177 text_end = (Bufbyte *) memchr (fmt, '%', fmt_end - fmt);
|
|
178 if (!text_end)
|
|
179 text_end = fmt_end;
|
|
180 spec.text_before = fmt - format;
|
|
181 spec.text_before_len = text_end - fmt;
|
|
182 fmt = text_end;
|
|
183 if (fmt != fmt_end)
|
|
184 {
|
|
185 fmt++; /* skip over % */
|
|
186
|
|
187 /* A % is special -- no arg number. According to ANSI specs,
|
|
188 field width does not apply to %% conversion. */
|
|
189 if (fmt != fmt_end && *fmt == '%')
|
|
190 {
|
|
191 spec.converter = '%';
|
|
192 Dynarr_add (specs, spec);
|
|
193 fmt++;
|
|
194 continue;
|
|
195 }
|
|
196
|
|
197 /* Is there a field number specifier? */
|
|
198 {
|
442
|
199 const Bufbyte *ptr;
|
428
|
200 int fieldspec;
|
|
201
|
|
202 ptr = parse_off_posnum (fmt, fmt_end, &fieldspec);
|
|
203 if (fieldspec > 0 && ptr != fmt_end && *ptr == '$')
|
|
204 {
|
|
205 /* There is a format specifier */
|
|
206 prev_argnum = fieldspec;
|
|
207 fmt = ptr + 1;
|
|
208 }
|
|
209 else
|
|
210 prev_argnum++;
|
|
211 spec.argnum = prev_argnum;
|
|
212 }
|
|
213
|
|
214 /* Parse off any flags */
|
|
215 NEXT_ASCII_BYTE (ch);
|
|
216 while (strchr (valid_flags, ch))
|
|
217 {
|
|
218 switch (ch)
|
|
219 {
|
446
|
220 case '-': spec.minus_flag = 1; break;
|
|
221 case '+': spec.plus_flag = 1; break;
|
|
222 case ' ': spec.space_flag = 1; break;
|
428
|
223 case '#': spec.number_flag = 1; break;
|
446
|
224 case '0': spec.zero_flag = 1; break;
|
428
|
225 default: abort ();
|
|
226 }
|
|
227 NEXT_ASCII_BYTE (ch);
|
|
228 }
|
|
229
|
|
230 /* Parse off the minimum field width */
|
|
231 fmt--; /* back up */
|
|
232
|
|
233 /*
|
|
234 * * means the field width was passed as an argument.
|
|
235 * Mark the current spec as one that forwards its
|
|
236 * field width and flags to the next spec in the array.
|
|
237 * Then create a new spec and continue with the parsing.
|
|
238 */
|
|
239 if (fmt != fmt_end && *fmt == '*')
|
|
240 {
|
|
241 spec.converter = '*';
|
|
242 RESOLVE_FLAG_CONFLICTS(spec);
|
|
243 Dynarr_add (specs, spec);
|
|
244 xzero (spec);
|
|
245 spec.argnum = ++prev_argnum;
|
|
246 fmt++;
|
|
247 }
|
|
248 else
|
|
249 {
|
|
250 fmt = parse_off_posnum (fmt, fmt_end, &spec.minwidth);
|
|
251 if (spec.minwidth == -1)
|
|
252 spec.minwidth = 0;
|
|
253 }
|
|
254
|
|
255 /* Parse off any precision specified */
|
|
256 NEXT_ASCII_BYTE (ch);
|
|
257 if (ch == '.')
|
|
258 {
|
|
259 /*
|
|
260 * * means the precision was passed as an argument.
|
|
261 * Mark the current spec as one that forwards its
|
|
262 * fieldwidth, flags and precision to the next spec in
|
|
263 * the array. Then create a new spec and continue
|
|
264 * with the parse.
|
|
265 */
|
|
266 if (fmt != fmt_end && *fmt == '*')
|
|
267 {
|
|
268 spec.converter = '*';
|
|
269 spec.forwarding_precision = 1;
|
|
270 RESOLVE_FLAG_CONFLICTS(spec);
|
|
271 Dynarr_add (specs, spec);
|
|
272 xzero (spec);
|
|
273 spec.argnum = ++prev_argnum;
|
|
274 fmt++;
|
|
275 }
|
|
276 else
|
|
277 {
|
|
278 fmt = parse_off_posnum (fmt, fmt_end, &spec.precision);
|
|
279 if (spec.precision == -1)
|
|
280 spec.precision = 0;
|
|
281 }
|
|
282 NEXT_ASCII_BYTE (ch);
|
|
283 }
|
|
284 else
|
|
285 /* No precision specified */
|
|
286 spec.precision = -1;
|
|
287
|
|
288 /* Parse off h or l flag */
|
|
289 if (ch == 'h' || ch == 'l')
|
|
290 {
|
|
291 if (ch == 'h')
|
|
292 spec.h_flag = 1;
|
|
293 else
|
|
294 spec.l_flag = 1;
|
|
295 NEXT_ASCII_BYTE (ch);
|
|
296 }
|
|
297
|
|
298 if (!strchr (valid_converters, ch))
|
563
|
299 syntax_error ("Invalid converter character", make_char (ch));
|
428
|
300 spec.converter = ch;
|
|
301 }
|
|
302
|
|
303 RESOLVE_FLAG_CONFLICTS(spec);
|
|
304 Dynarr_add (specs, spec);
|
|
305 }
|
|
306
|
|
307 RETURN_NOT_REACHED(specs) /* suppress compiler warning */
|
|
308 }
|
|
309
|
|
310 static int
|
|
311 get_args_needed (printf_spec_dynarr *specs)
|
|
312 {
|
|
313 int args_needed = 0;
|
|
314 REGISTER int i;
|
|
315
|
|
316 /* Figure out how many args are needed. This may be less than
|
|
317 the number of specs because a spec could be %% or could be
|
|
318 missing (literal text at end of format string) or there
|
|
319 could be specs where the field number is explicitly given.
|
|
320 We just look for the maximum argument number that's referenced. */
|
|
321
|
|
322 for (i = 0; i < Dynarr_length (specs); i++)
|
|
323 {
|
|
324 char ch = Dynarr_at (specs, i).converter;
|
|
325 if (ch && ch != '%')
|
|
326 {
|
|
327 int argnum = Dynarr_at (specs, i).argnum;
|
|
328 if (argnum > args_needed)
|
|
329 args_needed = argnum;
|
|
330 }
|
|
331 }
|
|
332
|
|
333 return args_needed;
|
|
334 }
|
|
335
|
|
336 static printf_arg_dynarr *
|
|
337 get_doprnt_args (printf_spec_dynarr *specs, va_list vargs)
|
|
338 {
|
|
339 printf_arg_dynarr *args = Dynarr_new (printf_arg);
|
|
340 union printf_arg arg;
|
|
341 REGISTER int i;
|
|
342 int args_needed = get_args_needed (specs);
|
|
343
|
|
344 xzero (arg);
|
|
345 for (i = 1; i <= args_needed; i++)
|
|
346 {
|
|
347 int j;
|
|
348 char ch;
|
|
349 struct printf_spec *spec = 0;
|
|
350
|
|
351 for (j = 0; j < Dynarr_length (specs); j++)
|
|
352 {
|
|
353 spec = Dynarr_atp (specs, j);
|
|
354 if (spec->argnum == i)
|
|
355 break;
|
|
356 }
|
|
357
|
|
358 if (j == Dynarr_length (specs))
|
563
|
359 syntax_error ("No conversion spec for argument", make_int (i));
|
428
|
360
|
|
361 ch = spec->converter;
|
|
362
|
|
363 if (strchr (int_converters, ch))
|
|
364 {
|
446
|
365 if (spec->l_flag)
|
428
|
366 arg.l = va_arg (vargs, long);
|
|
367 else
|
446
|
368 /* int even if ch == 'c' or spec->h_flag:
|
|
369 "the type used in va_arg is supposed to match the
|
|
370 actual type **after default promotions**."
|
|
371 Hence we read an int, not a short, if spec->h_flag. */
|
|
372 arg.l = va_arg (vargs, int);
|
428
|
373 }
|
|
374 else if (strchr (unsigned_int_converters, ch))
|
|
375 {
|
446
|
376 if (spec->l_flag)
|
428
|
377 arg.ul = va_arg (vargs, unsigned long);
|
|
378 else
|
446
|
379 /* unsigned int even if ch == 'c' or spec->h_flag */
|
|
380 arg.ul = (unsigned long) va_arg (vargs, unsigned int);
|
428
|
381 }
|
|
382 else if (strchr (double_converters, ch))
|
|
383 arg.d = va_arg (vargs, double);
|
|
384 else if (strchr (string_converters, ch))
|
|
385 arg.bp = va_arg (vargs, Bufbyte *);
|
|
386 else abort ();
|
|
387
|
|
388 Dynarr_add (args, arg);
|
|
389 }
|
|
390
|
|
391 return args;
|
|
392 }
|
|
393
|
|
394 /* Generate output from a format-spec FORMAT, of length FORMAT_LENGTH.
|
|
395 Output goes in BUFFER, which has room for BUFSIZE bytes.
|
|
396 If the output does not fit, truncate it to fit.
|
|
397 Returns the number of bytes stored into BUFFER.
|
|
398 LARGS or VARGS points to the arguments, and NARGS says how many.
|
|
399 if LARGS is non-zero, it should be a pointer to NARGS worth of
|
|
400 Lisp arguments. Otherwise, VARGS should be a va_list referring
|
|
401 to the arguments. */
|
|
402
|
|
403 static Bytecount
|
442
|
404 emacs_doprnt_1 (Lisp_Object stream, const Bufbyte *format_nonreloc,
|
428
|
405 Lisp_Object format_reloc, Bytecount format_length,
|
|
406 int nargs,
|
|
407 /* #### Gag me, gag me, gag me */
|
442
|
408 const Lisp_Object *largs, va_list vargs)
|
428
|
409 {
|
|
410 printf_spec_dynarr *specs = 0;
|
|
411 printf_arg_dynarr *args = 0;
|
|
412 REGISTER int i;
|
|
413 int init_byte_count = Lstream_byte_count (XLSTREAM (stream));
|
|
414
|
|
415 if (!NILP (format_reloc))
|
|
416 {
|
|
417 format_nonreloc = XSTRING_DATA (format_reloc);
|
|
418 format_length = XSTRING_LENGTH (format_reloc);
|
|
419 }
|
|
420 if (format_length < 0)
|
442
|
421 format_length = (Bytecount) strlen ((const char *) format_nonreloc);
|
428
|
422
|
|
423 specs = parse_doprnt_spec (format_nonreloc, format_length);
|
|
424 if (largs)
|
|
425 {
|
446
|
426 /* allow too many args for string, but not too few */
|
428
|
427 if (nargs < get_args_needed (specs))
|
563
|
428 signal_error_1 (Qwrong_number_of_arguments,
|
428
|
429 list3 (Qformat,
|
|
430 make_int (nargs),
|
|
431 !NILP (format_reloc) ? format_reloc :
|
|
432 make_string (format_nonreloc, format_length)));
|
|
433 }
|
|
434 else
|
|
435 {
|
|
436 args = get_doprnt_args (specs, vargs);
|
|
437 }
|
|
438
|
|
439 for (i = 0; i < Dynarr_length (specs); i++)
|
|
440 {
|
|
441 struct printf_spec *spec = Dynarr_atp (specs, i);
|
|
442 char ch;
|
|
443
|
|
444 /* Copy the text before */
|
|
445 if (!NILP (format_reloc)) /* refetch in case of GC below */
|
|
446 format_nonreloc = XSTRING_DATA (format_reloc);
|
446
|
447
|
|
448 doprnt_1 (stream, format_nonreloc + spec->text_before,
|
|
449 spec->text_before_len, 0, -1, 0, 0);
|
428
|
450
|
|
451 ch = spec->converter;
|
|
452
|
|
453 if (!ch)
|
|
454 continue;
|
|
455
|
|
456 if (ch == '%')
|
|
457 {
|
|
458 doprnt_1 (stream, (Bufbyte *) &ch, 1, 0, -1, 0, 0);
|
|
459 continue;
|
|
460 }
|
|
461
|
|
462 /* The char '*' as converter means the field width, precision
|
|
463 was specified as an argument. Extract the data and forward
|
|
464 it to the next spec, to which it will apply. */
|
|
465 if (ch == '*')
|
|
466 {
|
|
467 struct printf_spec *nextspec = Dynarr_atp (specs, i + 1);
|
|
468 Lisp_Object obj = largs[spec->argnum - 1];
|
|
469
|
|
470 if (INTP (obj))
|
|
471 {
|
|
472 if (spec->forwarding_precision)
|
|
473 {
|
|
474 nextspec->precision = XINT (obj);
|
|
475 nextspec->minwidth = spec->minwidth;
|
|
476 }
|
|
477 else
|
|
478 {
|
|
479 nextspec->minwidth = XINT (obj);
|
446
|
480 if (XINT (obj) < 0)
|
428
|
481 {
|
|
482 spec->minus_flag = 1;
|
|
483 nextspec->minwidth = - nextspec->minwidth;
|
|
484 }
|
|
485 }
|
446
|
486 nextspec->minus_flag = spec->minus_flag;
|
|
487 nextspec->plus_flag = spec->plus_flag;
|
|
488 nextspec->space_flag = spec->space_flag;
|
428
|
489 nextspec->number_flag = spec->number_flag;
|
446
|
490 nextspec->zero_flag = spec->zero_flag;
|
428
|
491 }
|
|
492 continue;
|
|
493 }
|
|
494
|
|
495 if (largs && (spec->argnum < 1 || spec->argnum > nargs))
|
563
|
496 syntax_error ("Invalid repositioning argument", make_int (spec->argnum));
|
428
|
497
|
|
498 else if (ch == 'S' || ch == 's')
|
|
499 {
|
|
500 Bufbyte *string;
|
|
501 Bytecount string_len;
|
|
502
|
|
503 if (!largs)
|
|
504 {
|
|
505 string = Dynarr_at (args, spec->argnum - 1).bp;
|
|
506 /* error() can be called with null string arguments.
|
|
507 E.g., in fileio.c, the return value of strerror()
|
|
508 is never checked. We'll print (null), like some
|
|
509 printf implementations do. Would it be better (and safe)
|
|
510 to signal an error instead? Or should we just use the
|
|
511 empty string? -dkindred@cs.cmu.edu 8/1997
|
|
512 */
|
|
513 if (!string)
|
|
514 string = (Bufbyte *) "(null)";
|
|
515 string_len = strlen ((char *) string);
|
|
516 }
|
|
517 else
|
|
518 {
|
|
519 Lisp_Object obj = largs[spec->argnum - 1];
|
440
|
520 Lisp_String *ls;
|
428
|
521
|
|
522 if (ch == 'S')
|
|
523 {
|
|
524 /* For `S', prin1 the argument and then treat like
|
|
525 a string. */
|
|
526 ls = XSTRING (Fprin1_to_string (obj, Qnil));
|
|
527 }
|
|
528 else if (STRINGP (obj))
|
|
529 ls = XSTRING (obj);
|
|
530 else if (SYMBOLP (obj))
|
|
531 ls = XSYMBOL (obj)->name;
|
|
532 else
|
|
533 {
|
|
534 /* convert to string using princ. */
|
|
535 ls = XSTRING (Fprin1_to_string (obj, Qt));
|
|
536 }
|
|
537 string = string_data (ls);
|
|
538 string_len = string_length (ls);
|
|
539 }
|
|
540
|
|
541 doprnt_1 (stream, string, string_len, spec->minwidth,
|
|
542 spec->precision, spec->minus_flag, spec->zero_flag);
|
|
543 }
|
|
544
|
|
545 else
|
|
546 {
|
|
547 /* Must be a number. */
|
|
548 union printf_arg arg;
|
|
549
|
|
550 if (!largs)
|
|
551 {
|
|
552 arg = Dynarr_at (args, spec->argnum - 1);
|
|
553 }
|
|
554 else
|
|
555 {
|
|
556 Lisp_Object obj = largs[spec->argnum - 1];
|
|
557 if (CHARP (obj))
|
|
558 obj = make_int (XCHAR (obj));
|
|
559 if (!INT_OR_FLOATP (obj))
|
|
560 {
|
563
|
561 syntax_error ("format specifier %%%c doesn't match argument type",
|
568
|
562 make_char (ch));
|
428
|
563 }
|
|
564 else if (strchr (double_converters, ch))
|
|
565 arg.d = XFLOATINT (obj);
|
|
566 else
|
|
567 {
|
446
|
568 if (FLOATP (obj))
|
|
569 obj = Ftruncate (obj);
|
428
|
570
|
|
571 if (strchr (unsigned_int_converters, ch))
|
446
|
572 arg.ul = (unsigned long) XINT (obj);
|
428
|
573 else
|
446
|
574 arg.l = XINT (obj);
|
428
|
575 }
|
|
576 }
|
|
577
|
|
578
|
|
579 if (ch == 'c')
|
|
580 {
|
|
581 Emchar a;
|
|
582 Bytecount charlen;
|
|
583 Bufbyte charbuf[MAX_EMCHAR_LEN];
|
|
584
|
446
|
585 a = (Emchar) arg.l;
|
428
|
586
|
|
587 if (!valid_char_p (a))
|
568
|
588 syntax_error ("invalid character value %d to %%c spec", make_char (a));
|
428
|
589
|
|
590 charlen = set_charptr_emchar (charbuf, a);
|
|
591 doprnt_1 (stream, charbuf, charlen, spec->minwidth,
|
|
592 -1, spec->minus_flag, spec->zero_flag);
|
|
593 }
|
|
594 else
|
|
595 {
|
448
|
596 /* ASCII Decimal representation uses 2.4 times as many
|
|
597 bits as machine binary. */
|
|
598 char *text_to_print =
|
|
599 alloca_array (char, 32 +
|
|
600 max (spec->minwidth,
|
647
|
601 (int) max (sizeof (double),
|
|
602 sizeof (long)) * 3 +
|
448
|
603 max (spec->precision, 0)));
|
428
|
604 char constructed_spec[100];
|
446
|
605 char *p = constructed_spec;
|
428
|
606
|
448
|
607 /* Mostly reconstruct the spec and use sprintf() to
|
428
|
608 format the string. */
|
|
609
|
446
|
610 *p++ = '%';
|
|
611 if (spec->plus_flag) *p++ = '+';
|
|
612 if (spec->space_flag) *p++ = ' ';
|
|
613 if (spec->number_flag) *p++ = '#';
|
448
|
614 if (spec->minus_flag) *p++ = '-';
|
|
615 if (spec->zero_flag) *p++ = '0';
|
446
|
616
|
448
|
617 if (spec->minwidth >= 0)
|
577
|
618 {
|
|
619 long_to_string (p, spec->minwidth);
|
|
620 p += strlen (p);
|
|
621 }
|
448
|
622 if (spec->precision >= 0)
|
428
|
623 {
|
446
|
624 *p++ = '.';
|
577
|
625 long_to_string (p, spec->precision);
|
|
626 p += strlen (p);
|
428
|
627 }
|
448
|
628
|
428
|
629 if (strchr (double_converters, ch))
|
442
|
630 {
|
446
|
631 *p++ = ch;
|
|
632 *p++ = '\0';
|
442
|
633 sprintf (text_to_print, constructed_spec, arg.d);
|
|
634 }
|
428
|
635 else
|
|
636 {
|
448
|
637 *p++ = 'l'; /* Always use longs with sprintf() */
|
|
638 *p++ = ch;
|
|
639 *p++ = '\0';
|
446
|
640
|
|
641 if (strchr (unsigned_int_converters, ch))
|
|
642 sprintf (text_to_print, constructed_spec, arg.ul);
|
|
643 else
|
428
|
644 sprintf (text_to_print, constructed_spec, arg.l);
|
|
645 }
|
|
646
|
|
647 doprnt_1 (stream, (Bufbyte *) text_to_print,
|
448
|
648 strlen (text_to_print), 0, -1, 0, 0);
|
428
|
649 }
|
|
650 }
|
|
651 }
|
|
652
|
|
653 /* #### will not get freed if error */
|
|
654 if (specs)
|
|
655 Dynarr_free (specs);
|
|
656 if (args)
|
|
657 Dynarr_free (args);
|
|
658 return Lstream_byte_count (XLSTREAM (stream)) - init_byte_count;
|
|
659 }
|
|
660
|
|
661 /* You really don't want to know why this is necessary... */
|
|
662 static Bytecount
|
442
|
663 emacs_doprnt_2 (Lisp_Object stream, const Bufbyte *format_nonreloc,
|
428
|
664 Lisp_Object format_reloc, Bytecount format_length, int nargs,
|
442
|
665 const Lisp_Object *largs, ...)
|
428
|
666 {
|
|
667 va_list vargs;
|
|
668 Bytecount val;
|
|
669 va_start (vargs, largs);
|
|
670 val = emacs_doprnt_1 (stream, format_nonreloc, format_reloc,
|
|
671 format_length, nargs, largs, vargs);
|
|
672 va_end (vargs);
|
|
673 return val;
|
|
674 }
|
|
675
|
|
676 /*********************** external entry points ***********************/
|
|
677
|
|
678 #ifdef I18N3
|
|
679 /* A note about I18N3 translating: the format string should get
|
|
680 translated, but not under all circumstances. When the format
|
|
681 string is a Lisp string, what should happen is that Fformat()
|
|
682 should format the untranslated args[0] and return that, and also
|
|
683 call Fgettext() on args[0] and, if that is different, format it
|
|
684 and store it in the `string-translatable' property of
|
|
685 the returned string. See Fgettext(). */
|
|
686 #endif
|
|
687
|
|
688 /* Send formatted output to STREAM. The format string comes from
|
|
689 either FORMAT_NONRELOC (of length FORMAT_LENGTH; -1 means use
|
|
690 strlen() to determine the length) or from FORMAT_RELOC, which
|
|
691 should be a Lisp string. Return the number of bytes written
|
|
692 to the stream.
|
|
693
|
|
694 DO NOT pass the data from a Lisp string as the FORMAT_NONRELOC
|
|
695 parameter, because this function can cause GC. */
|
|
696
|
|
697 Bytecount
|
442
|
698 emacs_doprnt_c (Lisp_Object stream, const Bufbyte *format_nonreloc,
|
428
|
699 Lisp_Object format_reloc, Bytecount format_length,
|
|
700 ...)
|
|
701 {
|
|
702 int val;
|
|
703 va_list vargs;
|
|
704
|
|
705 va_start (vargs, format_length);
|
|
706 val = emacs_doprnt_1 (stream, format_nonreloc, format_reloc,
|
|
707 format_length, 0, 0, vargs);
|
|
708 va_end (vargs);
|
|
709 return val;
|
|
710 }
|
|
711
|
|
712 /* Like emacs_doprnt_c but the args come in va_list format. */
|
|
713
|
|
714 Bytecount
|
442
|
715 emacs_doprnt_va (Lisp_Object stream, const Bufbyte *format_nonreloc,
|
428
|
716 Lisp_Object format_reloc, Bytecount format_length,
|
|
717 va_list vargs)
|
|
718 {
|
|
719 return emacs_doprnt_1 (stream, format_nonreloc, format_reloc,
|
|
720 format_length, 0, 0, vargs);
|
|
721 }
|
|
722
|
|
723 /* Like emacs_doprnt_c but the args are Lisp objects instead of
|
|
724 C arguments. This causes somewhat different behavior from
|
|
725 the above two functions (which should act like printf).
|
|
726 See `format' for a description of this behavior. */
|
|
727
|
|
728 Bytecount
|
442
|
729 emacs_doprnt_lisp (Lisp_Object stream, const Bufbyte *format_nonreloc,
|
428
|
730 Lisp_Object format_reloc, Bytecount format_length,
|
442
|
731 int nargs, const Lisp_Object *largs)
|
428
|
732 {
|
|
733 return emacs_doprnt_2 (stream, format_nonreloc, format_reloc,
|
|
734 format_length, nargs, largs);
|
|
735 }
|
|
736
|
|
737 /* Like the previous function but takes a variable number of arguments. */
|
|
738
|
|
739 Bytecount
|
442
|
740 emacs_doprnt_lisp_2 (Lisp_Object stream, const Bufbyte *format_nonreloc,
|
428
|
741 Lisp_Object format_reloc, Bytecount format_length,
|
|
742 int nargs, ...)
|
|
743 {
|
|
744 va_list vargs;
|
|
745 int i;
|
|
746 Lisp_Object *foo = alloca_array (Lisp_Object, nargs);
|
|
747
|
|
748 va_start (vargs, nargs);
|
|
749 for (i = 0; i < nargs; i++)
|
|
750 foo[i] = va_arg (vargs, Lisp_Object);
|
|
751 va_end (vargs);
|
|
752
|
|
753 return emacs_doprnt_2 (stream, format_nonreloc, format_reloc,
|
|
754 format_length, nargs, foo);
|
|
755 }
|
|
756
|
|
757 /* The following four functions work like the above three but
|
|
758 return their output as a Lisp string instead of sending it
|
|
759 to a stream. */
|
|
760
|
|
761 Lisp_Object
|
442
|
762 emacs_doprnt_string_c (const Bufbyte *format_nonreloc,
|
428
|
763 Lisp_Object format_reloc, Bytecount format_length,
|
|
764 ...)
|
|
765 {
|
|
766 va_list vargs;
|
|
767 Lisp_Object obj;
|
|
768 Lisp_Object stream = make_resizing_buffer_output_stream ();
|
|
769 struct gcpro gcpro1;
|
|
770
|
|
771 GCPRO1 (stream);
|
|
772 va_start (vargs, format_length);
|
|
773 emacs_doprnt_1 (stream, format_nonreloc, format_reloc,
|
|
774 format_length, 0, 0, vargs);
|
|
775 va_end (vargs);
|
|
776 Lstream_flush (XLSTREAM (stream));
|
|
777 obj = make_string (resizing_buffer_stream_ptr (XLSTREAM (stream)),
|
|
778 Lstream_byte_count (XLSTREAM (stream)));
|
|
779 UNGCPRO;
|
|
780 Lstream_delete (XLSTREAM (stream));
|
|
781 return obj;
|
|
782 }
|
|
783
|
|
784 Lisp_Object
|
442
|
785 emacs_doprnt_string_va (const Bufbyte *format_nonreloc,
|
428
|
786 Lisp_Object format_reloc, Bytecount format_length,
|
|
787 va_list vargs)
|
|
788 {
|
|
789 /* I'm fairly sure that this function cannot actually GC.
|
|
790 That can only happen when the arguments to emacs_doprnt_1() are
|
|
791 Lisp objects rather than C args. */
|
|
792 Lisp_Object obj;
|
|
793 Lisp_Object stream = make_resizing_buffer_output_stream ();
|
|
794 struct gcpro gcpro1;
|
|
795
|
|
796 GCPRO1 (stream);
|
|
797 emacs_doprnt_1 (stream, format_nonreloc, format_reloc,
|
|
798 format_length, 0, 0, vargs);
|
|
799 Lstream_flush (XLSTREAM (stream));
|
|
800 obj = make_string (resizing_buffer_stream_ptr (XLSTREAM (stream)),
|
|
801 Lstream_byte_count (XLSTREAM (stream)));
|
|
802 UNGCPRO;
|
|
803 Lstream_delete (XLSTREAM (stream));
|
|
804 return obj;
|
|
805 }
|
|
806
|
|
807 Lisp_Object
|
442
|
808 emacs_doprnt_string_lisp (const Bufbyte *format_nonreloc,
|
428
|
809 Lisp_Object format_reloc, Bytecount format_length,
|
442
|
810 int nargs, const Lisp_Object *largs)
|
428
|
811 {
|
|
812 Lisp_Object obj;
|
|
813 Lisp_Object stream = make_resizing_buffer_output_stream ();
|
|
814 struct gcpro gcpro1;
|
|
815
|
|
816 GCPRO1 (stream);
|
|
817 emacs_doprnt_2 (stream, format_nonreloc, format_reloc,
|
|
818 format_length, nargs, largs);
|
|
819 Lstream_flush (XLSTREAM (stream));
|
|
820 obj = make_string (resizing_buffer_stream_ptr (XLSTREAM (stream)),
|
|
821 Lstream_byte_count (XLSTREAM (stream)));
|
|
822 UNGCPRO;
|
|
823 Lstream_delete (XLSTREAM (stream));
|
|
824 return obj;
|
|
825 }
|
|
826
|
|
827 Lisp_Object
|
442
|
828 emacs_doprnt_string_lisp_2 (const Bufbyte *format_nonreloc,
|
428
|
829 Lisp_Object format_reloc, Bytecount format_length,
|
|
830 int nargs, ...)
|
|
831 {
|
|
832 Lisp_Object obj;
|
|
833 Lisp_Object stream = make_resizing_buffer_output_stream ();
|
|
834 struct gcpro gcpro1;
|
|
835 va_list vargs;
|
|
836 int i;
|
|
837 Lisp_Object *foo = alloca_array (Lisp_Object, nargs);
|
|
838
|
|
839 va_start (vargs, nargs);
|
|
840 for (i = 0; i < nargs; i++)
|
|
841 foo[i] = va_arg (vargs, Lisp_Object);
|
|
842 va_end (vargs);
|
|
843
|
|
844 GCPRO1 (stream);
|
|
845 emacs_doprnt_2 (stream, format_nonreloc, format_reloc,
|
|
846 format_length, nargs, foo);
|
|
847 Lstream_flush (XLSTREAM (stream));
|
|
848 obj = make_string (resizing_buffer_stream_ptr (XLSTREAM (stream)),
|
|
849 Lstream_byte_count (XLSTREAM (stream)));
|
|
850 UNGCPRO;
|
|
851 Lstream_delete (XLSTREAM (stream));
|
|
852 return obj;
|
|
853 }
|