annotate src/doprnt.c @ 161:28f395d8dc7a r20-3b7

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