annotate src/doprnt.c @ 446:1ccc32a20af4 r21-2-38

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