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