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