blob: 30187c6b53541cfdb51a3167a360c875040445ba [file] [log] [blame]
Petr Machata940ec062012-01-06 22:43:51 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 1998,2004,2007,2008,2009 Juan Cespedes
5 * Copyright (C) 2006 Steve Fink
6 * Copyright (C) 2006 Ian Wienand
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24#include <assert.h>
25#include <stdlib.h>
26
27#include "printf.h"
28#include "type.h"
29#include "value.h"
30#include "expr.h"
31#include "zero.h"
32#include "param.h"
Petr Machatae3f4a982012-01-09 04:27:26 +010033#include "lens_default.h"
Petr Machata940ec062012-01-06 22:43:51 +010034
35struct param_enum {
36 struct value array;
37 int percent;
38 size_t *future_length;
39 char *format;
40 char const *ptr;
41 char const *end;
42};
43
44static struct param_enum *
45param_printf_init(struct value *cb_args, size_t nargs,
46 struct value_dict *arguments)
47{
48 assert(nargs == 1);
49
50 /* We expect a char array pointer. */
Petr Machatae3f4a982012-01-09 04:27:26 +010051 if (cb_args->type->type != ARGTYPE_POINTER
52 || cb_args->type->u.ptr_info.info->type != ARGTYPE_ARRAY
53 || (cb_args->type->u.ptr_info.info->u.array_info.elt_type->type
54 != ARGTYPE_CHAR))
Petr Machata940ec062012-01-06 22:43:51 +010055 return NULL;
56
57 struct param_enum *self = malloc(sizeof(*self));
58 if (self == NULL) {
59 fail:
60 free(self);
61 return NULL;
62 }
63
Petr Machatae3f4a982012-01-09 04:27:26 +010064 if (value_init_deref(&self->array, cb_args) < 0)
Petr Machata940ec062012-01-06 22:43:51 +010065 goto fail;
66
67 assert(self->array.type->type == ARGTYPE_ARRAY);
68
69 self->format = (char *)value_get_data(&self->array, arguments);
Petr Machata43417842012-10-29 17:59:17 +010070 if (self->format == NULL) {
71 value_destroy(&self->array);
Petr Machata940ec062012-01-06 22:43:51 +010072 goto fail;
Petr Machata43417842012-10-29 17:59:17 +010073 }
Petr Machata940ec062012-01-06 22:43:51 +010074
75 size_t size = value_size(&self->array, arguments);
Petr Machata43417842012-10-29 17:59:17 +010076 if (size == (size_t)-1) {
77 value_destroy(&self->array);
Petr Machata940ec062012-01-06 22:43:51 +010078 goto fail;
Petr Machata43417842012-10-29 17:59:17 +010079 }
Petr Machata940ec062012-01-06 22:43:51 +010080
81 self->percent = 0;
82 self->ptr = self->format;
83 self->end = self->format + size;
84 self->future_length = NULL;
85 return self;
86}
87
88static void
89drop_future_length(struct param_enum *self)
90{
91 if (self->future_length != NULL) {
92 free(self->future_length);
93 self->future_length = NULL;
94 }
95}
96
97static int
98form_next_param(struct param_enum *self,
99 enum arg_type format_type, enum arg_type elt_type,
100 unsigned hlf, unsigned lng, char *len_buf, size_t len_buf_len,
101 struct arg_type_info *infop)
102{
103 /* XXX note: Some types are wrong because we lack
104 ARGTYPE_LONGLONG, ARGTYPE_UCHAR and ARGTYPE_SCHAR. */
105 assert(lng <= 2);
106 assert(hlf <= 2);
107 static enum arg_type ints[] =
108 { ARGTYPE_CHAR, ARGTYPE_SHORT, ARGTYPE_INT,
109 ARGTYPE_LONG, ARGTYPE_ULONG };
110 static enum arg_type uints[] =
111 { ARGTYPE_CHAR, ARGTYPE_USHORT, ARGTYPE_UINT,
112 ARGTYPE_ULONG, ARGTYPE_ULONG };
113
114 struct arg_type_info *elt_info = NULL;
Petr Machatab7819162012-01-09 04:26:15 +0100115 if (format_type == ARGTYPE_ARRAY || format_type == ARGTYPE_POINTER)
Petr Machata940ec062012-01-06 22:43:51 +0100116 elt_info = type_get_simple(elt_type);
Petr Machatab7819162012-01-09 04:26:15 +0100117 else if (format_type == ARGTYPE_INT)
Petr Machata940ec062012-01-06 22:43:51 +0100118 format_type = ints[2 + lng - hlf];
119 else if (format_type == ARGTYPE_UINT)
120 format_type = uints[2 + lng - hlf];
121
122
123 if (format_type == ARGTYPE_ARRAY) {
124 struct expr_node *node = NULL;
Petr Machata58e75f52012-10-30 00:22:46 +0100125 int own_node;
Petr Machata940ec062012-01-06 22:43:51 +0100126 if (len_buf_len != 0
127 || self->future_length != NULL) {
128 struct tmp {
129 struct expr_node node;
130 struct arg_type_info type;
131 };
132 struct tmp *len = malloc(sizeof(*len));
133 if (len == NULL) {
134 fail:
135 free(len);
136 return -1;
137 }
138
Petr Machata16804482012-10-30 00:19:36 +0100139 len->type = *type_get_simple(ARGTYPE_LONG);
Petr Machata940ec062012-01-06 22:43:51 +0100140
141 long l;
142 if (self->future_length != NULL) {
143 l = *self->future_length;
144 drop_future_length(self);
145 } else {
146 l = atol(len_buf);
147 }
148
149 expr_init_const_word(&len->node, l, &len->type, 0);
150
151 node = build_zero_w_arg(&len->node, 1);
152 if (node == NULL)
153 goto fail;
Petr Machata58e75f52012-10-30 00:22:46 +0100154 own_node = 1;
Petr Machata940ec062012-01-06 22:43:51 +0100155
156 } else {
157 node = expr_node_zero();
Petr Machata58e75f52012-10-30 00:22:46 +0100158 own_node = 0;
Petr Machata940ec062012-01-06 22:43:51 +0100159 }
160
161 assert(node != NULL);
Petr Machata940ec062012-01-06 22:43:51 +0100162
Petr Machata58e75f52012-10-30 00:22:46 +0100163 type_init_array(infop, elt_info, 0, node, own_node);
Petr Machata940ec062012-01-06 22:43:51 +0100164 } else if (format_type == ARGTYPE_POINTER) {
165 type_init_pointer(infop, elt_info, 1);
166
167 } else {
168 *infop = *type_get_simple(format_type);
169 }
170
171 return 0;
172}
173
174static int
175param_printf_next(struct param_enum *self, struct arg_type_info *infop,
176 int *insert_stop)
177{
178 unsigned hlf = 0;
179 unsigned lng = 0;
Petr Machatab7819162012-01-09 04:26:15 +0100180 enum arg_type format_type = ARGTYPE_VOID;
181 enum arg_type elt_type = ARGTYPE_VOID;
Petr Machata940ec062012-01-06 22:43:51 +0100182 char len_buf[25] = {};
183 size_t len_buf_len = 0;
Petr Machata31af32c2012-01-08 02:36:50 +0100184 struct lens *lens = NULL;
Petr Machata940ec062012-01-06 22:43:51 +0100185
186 for (; self->ptr < self->end; ++self->ptr) {
187 if (!self->percent) {
188 if (*self->ptr == '%')
189 self->percent = 1;
190 continue;
191 }
192
193 switch (*self->ptr) {
194 case '#': case ' ': case '-':
195 case '+': case 'I': case '\'':
196 /* These are only important for formatting,
197 * not for interpreting the type. */
198 continue;
199
200 case '*':
201 /* Length parameter given in the next
202 * argument. */
203 if (self->future_length == NULL)
204 /* This should really be an assert,
205 * but we can't just fail on invalid
206 * format string. */
207 self->future_length
208 = malloc(sizeof(*self->future_length));
209
210 if (self->future_length != NULL) {
211 ++self->ptr;
212 format_type = ARGTYPE_INT;
213 break;
214 }
215
216 case '0':
217 case '1': case '2': case '3':
218 case '4': case '5': case '6':
219 case '7': case '8': case '9':
220 /* Field length likewise, but we need to parse
221 * this to attach the appropriate string
222 * length expression. */
223 if (len_buf_len < sizeof(len_buf) - 1)
224 len_buf[len_buf_len++] = *self->ptr;
225 continue;
226
227 case 'h':
228 if (hlf < 2)
229 hlf++;
230 continue;
231
232 case 'l':
233 if (lng < 2)
234 lng++;
235 continue;
236
237 case 'q':
238 lng = 2;
239 continue;
240
241 case 'L': /* long double */
242 lng = 1;
243 continue;
244
245 case 'j': /* intmax_t */
246 /* XXX ABI should know */
247 lng = 2;
248 continue;
249
250 case 't': /* ptrdiff_t */
251 case 'Z': case 'z': /* size_t */
252 lng = 1; /* XXX ABI should tell */
253 continue;
254
255 case 'd':
256 case 'i':
257 format_type = ARGTYPE_INT;
258 self->percent = 0;
259 break;
260
Petr Machata940ec062012-01-06 22:43:51 +0100261 case 'o':
Petr Machatac70b1952012-04-27 21:00:26 +0200262 lens = &octal_lens;
263 goto uint;
264
Petr Machata940ec062012-01-06 22:43:51 +0100265 case 'x': case 'X':
Petr Machatac70b1952012-04-27 21:00:26 +0200266 lens = &hex_lens;
267 case 'u':
268 uint:
Petr Machata940ec062012-01-06 22:43:51 +0100269 format_type = ARGTYPE_UINT;
270 self->percent = 0;
271 break;
272
273 case 'e': case 'E':
274 case 'f': case 'F':
275 case 'g': case 'G':
276 case 'a': case 'A':
277 format_type = ARGTYPE_DOUBLE;
278 self->percent = 0;
279 break;
280
281 case 'C': /* like "lc" */
282 if (lng == 0)
283 lng++;
284 case 'c':
285 /* XXX "lc" means wchar_t string. */
286 format_type = ARGTYPE_CHAR;
287 self->percent = 0;
288 break;
289
290 case 'S': /* like "ls" */
291 if (lng == 0)
292 lng++;
293 case 's':
294 format_type = ARGTYPE_ARRAY;
295 /* XXX "ls" means wchar_t string. */
296 elt_type = ARGTYPE_CHAR;
297 self->percent = 0;
Petr Machatae3f4a982012-01-09 04:27:26 +0100298 lens = &string_lens;
Petr Machata940ec062012-01-06 22:43:51 +0100299 break;
300
301 case 'p':
302 case 'n': /* int* where to store no. of printed chars. */
303 format_type = ARGTYPE_POINTER;
304 elt_type = ARGTYPE_VOID;
305 self->percent = 0;
306 break;
307
308 case 'm': /* (glibc) print argument of errno */
309 case '%':
310 lng = 0;
311 hlf = 0;
312 self->percent = 0;
313 continue;
314
315 default:
316 continue;
317 }
318
319 /* If we got here, the type must have been set. */
Petr Machatab7819162012-01-09 04:26:15 +0100320 assert(format_type != ARGTYPE_VOID);
Petr Machata940ec062012-01-06 22:43:51 +0100321
322 if (form_next_param(self, format_type, elt_type, hlf, lng,
323 len_buf, len_buf_len, infop) < 0)
324 return -1;
325
Petr Machata31af32c2012-01-08 02:36:50 +0100326 infop->lens = lens;
327 infop->own_lens = 0;
328
Petr Machata940ec062012-01-06 22:43:51 +0100329 return 0;
330 }
331
Petr Machata16804482012-10-30 00:19:36 +0100332 *infop = *type_get_simple(ARGTYPE_VOID);
Petr Machata940ec062012-01-06 22:43:51 +0100333 return 0;
334}
335
336static enum param_status
337param_printf_stop(struct param_enum *self, struct value *value)
338{
339 if (self->future_length != NULL
340 && value_extract_word(value, (long *)self->future_length, NULL) < 0)
341 drop_future_length(self);
342
343 return PPCB_CONT;
344}
345
346static void
347param_printf_done(struct param_enum *context)
348{
Petr Machata43417842012-10-29 17:59:17 +0100349 value_destroy(&context->array);
Petr Machata940ec062012-01-06 22:43:51 +0100350 free(context);
351}
352
353void
354param_pack_init_printf(struct param *param, struct expr_node *arg, int own_arg)
355{
Petr Machatae36298a2012-09-13 17:12:41 +0200356 param_init_pack(param, PARAM_PACK_VARARGS, arg, 1, own_arg,
Petr Machata940ec062012-01-06 22:43:51 +0100357 &param_printf_init, &param_printf_next,
358 &param_printf_stop, &param_printf_done);
359}