summaryrefslogtreecommitdiffhomepage
path: root/ir/libcore/lc_printf.c
blob: c6458ee4f676c1dd460e078be757033448d62232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 IPD Goos, Universit"at Karlsruhe, Germany
 */


/**
 * A customizable printf clone.
 * @author Sebastian Hack
 * @date 4.12.2005
 */
#include "lc_printf.h"

#include "hashptr.h"
#include "set.h"
#include "util.h"
#include "xmalloc.h"
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* printf implementation */

typedef struct lc_arg_t {
	struct lc_arg_t        *next;
	const char             *name;
	char                    letter;
	int                     lc_arg_type;
	const lc_arg_handler_t *handler;
} lc_arg_t;

struct lc_arg_env_t {
	set      *args;             /**< Map for named arguments. */
	lc_arg_t *lower[26];        /**< Map for lower conversion specifiers. */
	lc_arg_t *upper[26];        /**< Map for upper conversion specifiers. */
};

static int lc_arg_cmp(const void *p1, const void *p2, size_t size)
{
	const lc_arg_t *a1 = (const lc_arg_t*)p1;
	const lc_arg_t *a2 = (const lc_arg_t*)p2;
	(void) size;
	return strcmp(a1->name, a2->name);
}

lc_arg_env_t *lc_arg_new_env(void)
{
	lc_arg_env_t *env = XMALLOCZ(lc_arg_env_t);
	env->args = new_set(lc_arg_cmp, 16);
	return env;
}

void lc_arg_free_env(lc_arg_env_t *env)
{
	del_set(env->args);
	free(env);
}

int lc_arg_register(lc_arg_env_t *env, const char *name, char letter,
                    const lc_arg_handler_t *handler)
{
	lc_arg_t arg;
	arg.name = name;
	arg.letter = letter;
	arg.handler = handler;

	lc_arg_t **map  = NULL;
	int        base = 0;
	if (isupper((unsigned char)letter)) {
		map = env->upper;
		base = 'A';
	} else if (islower((unsigned char)letter)) {
		map = env->lower;
		base = 'a';
	}

	lc_arg_t *ent = set_insert(lc_arg_t, env->args, &arg, sizeof(arg), hash_str(name));

	if (ent && base != 0)
		map[letter - base] = ent;

	return ent != NULL;
}

void lc_arg_unregister(lc_arg_env_t *env, const char *name)
{
	(void) env;
	(void) name;
}

int lc_arg_append(lc_appendable_t *app, const lc_arg_occ_t *occ, const char *str, size_t len)
{
	char pad = ' ';

	/* Set the padding to zero, if the zero is given and we are not left
	 * justified. (A minus overrides the zero). See printf(3). */
	if (!occ->flag_minus && occ->flag_zero)
		pad = '0';

	return lc_appendable_snwadd(app, str, len, MAX(0, occ->width), occ->flag_minus, pad);
}


static int dispatch_snprintf(char *buf, size_t len, const char *fmt,
                             int lc_arg_type, const lc_arg_value_t *val)
{
	int res = 0;

	switch (lc_arg_type) {
#define LC_ARG_TYPE(type,name,va_type) \
		case lc_arg_type_ ## name: res = snprintf(buf, len, fmt, val->v_ ## name); break;
#include "lc_printf_arg_types.def"
#undef LC_ARG_TYPE
	}

	return res;
}

static char *make_fmt(char *buf, size_t len, const lc_arg_occ_t *occ)
{
	char mod[64];
	char prec[16];
	char width[16];

	prec[0] = '\0';
	width[0] = '\0';

	if (occ->precision > 0)
		snprintf(prec, sizeof(prec), ".%d", occ->precision);

	if (occ->width > 0)
		snprintf(width, sizeof(width), "%d", occ->width);

	assert(occ->modifier && "modifier must not be NULL");
	strncpy(mod, occ->modifier, sizeof(mod) - 1);
	mod[MIN(sizeof(mod) - 1, occ->modifier_length)] = '\0';

#ifdef _MSC_VER
	/* work-around for buggy mscrt not supporting z, j,  and t modifier */
	if (occ->modifier_length == 1) {
		if (mod[0] == 'z') {
			if (sizeof(size_t) == sizeof(int))
				mod[0] = '\0';
			if (sizeof(size_t) == sizeof(__int64)) {
				mod[0] = 'I';
				mod[1] = '6';
				mod[2] = '4';
				mod[3] = '\0';
			}
		} else if (mod[0] == 't') {
			if (sizeof(ptrdiff_t) == sizeof(int))
				mod[0] = '\0';
			if (sizeof(ptrdiff_t) == sizeof(__int64)) {
				mod[0] = 'I';
				mod[1] = '6';
				mod[2] = '4';
				mod[3] = '\0';
			}
		} else if (mod[0] == 'j') {
			if (sizeof(intmax_t) == sizeof(int))
				mod[0] = '\0';
			if (sizeof(intmax_t) == sizeof(__int64)) {
				mod[0] = 'I';
				mod[1] = '6';
				mod[2] = '4';
				mod[3] = '\0';
			}
		}
	} else if (occ->modifier_length == 2) {
		if (mod[0] == 'h' && mod[1] == 'h') {
			/* no support for char in mscrt, but we can safely ignore it
			 * because the size is handled by the argument reader code */
			mod[0] = '\0';
		}
	}
#endif
	snprintf(buf, len, "%%%s%s%s%s%s%s%s%s%c",
	         occ->flag_space ? "#" : "",
	         occ->flag_hash ? "#" : "",
	         occ->flag_plus ? "+" : "",
	         occ->flag_minus ? "-" : "",
	         occ->flag_zero ? "0" : "",
	         width, prec,
	         mod, occ->conversion);

	return buf;
}

/**
 * Standard argument handler.
 */
static int std_get_lc_arg_type(const lc_arg_occ_t *occ)
{
	size_t modlen = occ->modifier_length;

	/* check, if the type can be derived from the modifier */
	if (modlen > 0) {
		const char *mod = occ->modifier;
		switch (mod[0]) {
			case 'h':
				return modlen > 1 && mod[1] == 'h' ? lc_arg_type_char : lc_arg_type_short;
			case 'l':
				return modlen > 1 && mod[1] == 'l' ? lc_arg_type_long_long : lc_arg_type_long;
#define TYPE_CASE(letter,type) case letter: return lc_arg_type_ ## type
			TYPE_CASE('j', intmax_t);
			TYPE_CASE('z', size_t);
			TYPE_CASE('t', ptrdiff_t);
			TYPE_CASE('L', long_double);
#undef TYPE_CASE
		}
	}

	/* The type is given by the conversion specifier and cannot be
	 * determined from the modifier. */
	switch (occ->conversion) {
		case 'e':
		case 'E':
		case 'f':
		case 'F':
		case 'g':
		case 'G':
			return lc_arg_type_double;
		case 's':
		case 'n':
		case 'p':
			return lc_arg_type_ptr;
		default:
			return lc_arg_type_int;
	}
}

static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *val)
{
	char fmt[32];
	int res = 0;

	make_fmt(fmt, sizeof(fmt), occ);

	switch (occ->conversion) {
		/* Store the number of written characters in the given
		 * int pointer location */
		case 'n': {
			int *num = (int*)val->v_ptr;
			*num = (int)app->written;
			break;
		}

		/* strings are dumped directly, since they can get really big. A
		 * buffer of 512 letters for all other types should be enough. */
		case 's': {
			const char *str = (const char*)val->v_ptr;
			size_t size = strlen(str);
			lc_arg_append(app, occ, str, size);
			res = size;
			break;
		}

		default: {
			int len = MAX(512, occ->width + 1);
			char *buf = XMALLOCN(char, len);
			res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
			assert(res < len);
			lc_appendable_snadd(app, buf, res);
			free(buf);
		}
	}

	return res;
}

static const lc_arg_handler_t std_handler = {
	std_get_lc_arg_type,
	std_emit
};

lc_arg_env_t *lc_arg_add_std(lc_arg_env_t *env)
{
	lc_arg_register(env, "std:c", 'c', &std_handler);
	lc_arg_register(env, "std:i", 'i', &std_handler);
	lc_arg_register(env, "std:d", 'd', &std_handler);
	lc_arg_register(env, "std:o", 'o', &std_handler);
	lc_arg_register(env, "std:u", 'u', &std_handler);
	lc_arg_register(env, "std:x", 'x', &std_handler);
	lc_arg_register(env, "std:X", 'X', &std_handler);

	lc_arg_register(env, "std:e", 'e', &std_handler);
	lc_arg_register(env, "std:E", 'E', &std_handler);
	lc_arg_register(env, "std:f", 'f', &std_handler);
	lc_arg_register(env, "std:F", 'F', &std_handler);
	lc_arg_register(env, "std:g", 'g', &std_handler);
	lc_arg_register(env, "std:G", 'G', &std_handler);

	lc_arg_register(env, "std:s", 's', &std_handler);
	lc_arg_register(env, "std:p", 'p', &std_handler);
	lc_arg_register(env, "std:n", 'n', &std_handler);

	return env;
}

static char *read_int(const char *s, int *value)
{
	char *endptr;
	int res = (int) strtol(s, &endptr, 10);
	*value = endptr == s ? -1 : res;
	return endptr;
}

/* Generic printf() function. */

int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                 va_list args)
{
	(void)args;

	int         res  = 0;
	const char *last = fmt + strlen(fmt);

	/* Find the first % */
	const char *s = strchr(fmt, '%');

	/* Emit the text before the first % was found */
	size_t addlen = (s ? s : last) - fmt;
	lc_appendable_snadd(app, fmt, addlen);
	res += addlen;

	while (s != NULL) {
		lc_arg_value_t val;

		/* We must be at a '%' */
		assert(*s == '%');

		/* Reset the occurrence structure */
		lc_arg_occ_t occ;
		memset(&occ, 0, sizeof(occ));

		/* Eat all flags and set the corresponding flags in the occ struct */
		for (++s; strchr("#0-+", *s); ++s) {
			switch (*s) {
				case '#':
					occ.flag_hash = 1;
					break;
				case '0':
					occ.flag_zero = 1;
					break;
				case '-':
					occ.flag_minus = 1;
					break;
				case '+':
					occ.flag_plus = 1;
					break;
				case ' ':
					occ.flag_space = 1;
					break;
			}
		}

		/* Read the width if given */
		s = read_int(s, &occ.width);

		occ.precision = -1;

		/* read the precision if given */
		if (*s == '.') {
			int precision;
			s = read_int(s + 1, &precision);

			/* Negative or lacking precision after a '.' is treated as
			 * precision 0. */
			occ.precision = MAX(0, precision);
		}

		/*
		 * Now, we can either have:
		 * - a named argument like {node}
		 * - some modifiers followed by a conversion specifier
		 * - or some other character, which ends this format invalidly
		 */
		char            ch  = *s;
		const lc_arg_t *arg = NULL;
		switch (ch) {
			case '%':
				s++;
				lc_appendable_chadd(app, '%');
				++res;
				break;
			case '{': {
				const char *named = ++s;

				/* Read until the closing brace or end of the string. */
				for (ch = *s; ch != '}' && ch != '\0'; ch = *++s) {
				}

				if (s - named) {
					size_t n = s - named;
					char *name;
					lc_arg_t tmp;

					name = (char*) malloc(sizeof(char) * (n + 1));
					MEMCPY(name, named, n);
					name[n] = '\0';
					tmp.name = name;

					arg = set_find(lc_arg_t, env->args, &tmp, sizeof(tmp), hash_str(named));
					occ.modifier = "";
					occ.modifier_length = 0;

					/* Set the conversion specifier of the occurrence to the
					 * letter specified in the argument description. */
					if (arg)
						occ.conversion = arg->letter;

					free(name);

					/* If we ended with a closing brace, move the current
					 * pointer after it, since it is not to be dumped. */
					if (ch == '}')
						s++;
				}
				break;
			}

			default: {
				const char *mod = s;

				/* Read, as long there are letters */
				while (isalpha((unsigned char)ch) && !arg) {
					int              base = 'a';
					lc_arg_t *const *map  = env->lower;

					/* If uppercase, select the uppercase map from the
					 * environment */
					if (isupper((unsigned char)ch)) {
						base = 'A';
						map = env->upper;
					}

					if (map[ch - base] != NULL) {
						occ.modifier = mod;
						occ.modifier_length = s - mod;
						occ.conversion = ch;
						arg = map[ch - base];
					}

					ch = *++s;
				}
			}
		}

		/* Call the handler if an argument was determined */
		if (arg != NULL && arg->handler != NULL) {
			const lc_arg_handler_t *handler = arg->handler;

			/* Let the handler determine the type of the argument based on the
			 * information gathered. */
			occ.lc_arg_type = handler->get_lc_arg_type(&occ);

			/* Store the value according to argument information */
			switch (occ.lc_arg_type) {
#define LC_ARG_TYPE(type,name,va_type) \
			case lc_arg_type_ ## name: val.v_ ## name = va_arg(args, va_type); break;
#include "lc_printf_arg_types.def"
#undef LC_ARG_TYPE
			}

			/* Finally, call the handler. */
			res += handler->emit(app, &occ, &val);
		}

		const char *old = s;
		s = strchr(s, '%');
		size_t addlen = (s ? s : last) - old;
		lc_appendable_snadd(app, old, addlen);
		res += addlen;
	}

	return res;
}

/* Convenience implementations */

int lc_eoprintf(const lc_arg_env_t *env, struct obstack *obst, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	int res = lc_evoprintf(env, obst, fmt, args);
	va_end(args);
	return res;
}

int lc_evprintf(const lc_arg_env_t *env, const char *fmt, va_list args)
{
	return lc_evfprintf(env, stdout, fmt, args);
}

int lc_evsnprintf(const lc_arg_env_t *env, char *buf, size_t len, const char *fmt, va_list args)
{
	lc_appendable_t app;

	lc_appendable_init(&app, lc_appendable_string, buf, len);
	int res = lc_evpprintf(env, &app, fmt, args);
	lc_appendable_finish(&app);
	return res;
}

int lc_evfprintf(const lc_arg_env_t *env, FILE *f, const char *fmt, va_list args)
{
	lc_appendable_t app;

	lc_appendable_init(&app, lc_appendable_file, f, 0);
	int res = lc_evpprintf(env, &app, fmt, args);
	lc_appendable_finish(&app);
	return res;
}

int lc_evoprintf(const lc_arg_env_t *env, struct obstack *obst, const char *fmt, va_list args)
{
	lc_appendable_t app;

	lc_appendable_init(&app, lc_appendable_obstack, obst, 0);
	int res = lc_evpprintf(env, &app, fmt, args);
	lc_appendable_finish(&app);
	return res;
}