read_config_file.c revision e5ea57e57d265973e60ef83bbf44715f9c72e3d7
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 1998,1999,2003,2007,2008,2009 Juan Cespedes
5 * Copyright (C) 2006 Ian Wienand
6 * Copyright (C) 2006 Steve Fink
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/* getline is POSIX.1-2008.  It was originally a GNU extension, and
25 * chances are uClibc still needs _GNU_SOURCE, but for now try it this
26 * way.  */
27#define _POSIX_C_SOURCE 200809L
28
29#include "config.h"
30
31#include <string.h>
32#include <stdlib.h>
33#include <ctype.h>
34#include <errno.h>
35#include <assert.h>
36
37#include "common.h"
38#include "output.h"
39#include "expr.h"
40#include "param.h"
41#include "printf.h"
42#include "prototype.h"
43#include "zero.h"
44#include "type.h"
45#include "lens.h"
46#include "lens_default.h"
47#include "lens_enum.h"
48
49/* Lifted from GCC: The ctype functions are often implemented as
50 * macros which do lookups in arrays using the parameter as the
51 * offset.  If the ctype function parameter is a char, then gcc will
52 * (appropriately) warn that a "subscript has type char".  Using a
53 * (signed) char as a subscript is bad because you may get negative
54 * offsets and thus it is not 8-bit safe.  The CTYPE_CONV macro
55 * ensures that the parameter is cast to an unsigned char when a char
56 * is passed in.  When an int is passed in, the parameter is left
57 * alone so we don't lose EOF.  */
58
59#define CTYPE_CONV(CH) \
60  (sizeof(CH) == sizeof(unsigned char) ? (int)(unsigned char)(CH) : (int)(CH))
61
62struct locus
63{
64	const char *filename;
65	int line_no;
66};
67
68static struct arg_type_info *parse_nonpointer_type(struct protolib *plib,
69						   struct locus *loc,
70						   char **str,
71						   struct param **extra_param,
72						   size_t param_num,
73						   int *ownp, int *forwardp);
74static struct arg_type_info *parse_type(struct protolib *plib,
75					struct locus *loc,
76					char **str, struct param **extra_param,
77					size_t param_num, int *ownp,
78					int *forwardp);
79static struct arg_type_info *parse_lens(struct protolib *plib,
80					struct locus *loc,
81					char **str, struct param **extra_param,
82					size_t param_num, int *ownp,
83					int *forwardp);
84static int parse_enum(struct protolib *plib, struct locus *loc,
85		      char **str, struct arg_type_info **retp, int *ownp);
86
87struct prototype *list_of_functions = NULL;
88
89static int
90parse_arg_type(char **name, enum arg_type *ret)
91{
92	char *rest = NULL;
93	enum arg_type candidate;
94
95#define KEYWORD(KWD, TYPE)						\
96	do {								\
97		if (strncmp(*name, KWD, sizeof(KWD) - 1) == 0) {	\
98			rest = *name + sizeof(KWD) - 1;			\
99			candidate = TYPE;				\
100			goto ok;					\
101		}							\
102	} while (0)
103
104	KEYWORD("void", ARGTYPE_VOID);
105	KEYWORD("int", ARGTYPE_INT);
106	KEYWORD("uint", ARGTYPE_UINT);
107	KEYWORD("long", ARGTYPE_LONG);
108	KEYWORD("ulong", ARGTYPE_ULONG);
109	KEYWORD("char", ARGTYPE_CHAR);
110	KEYWORD("short", ARGTYPE_SHORT);
111	KEYWORD("ushort", ARGTYPE_USHORT);
112	KEYWORD("float", ARGTYPE_FLOAT);
113	KEYWORD("double", ARGTYPE_DOUBLE);
114	KEYWORD("array", ARGTYPE_ARRAY);
115	KEYWORD("struct", ARGTYPE_STRUCT);
116
117	/* Misspelling of int used in ltrace.conf that we used to
118	 * ship.  */
119	KEYWORD("itn", ARGTYPE_INT);
120
121	assert(rest == NULL);
122	return -1;
123
124#undef KEYWORD
125
126ok:
127	if (isalnum(CTYPE_CONV(*rest)))
128		return -1;
129
130	*name = rest;
131	*ret = candidate;
132	return 0;
133}
134
135static void
136eat_spaces(char **str) {
137	while (**str == ' ') {
138		(*str)++;
139	}
140}
141
142static char *
143xstrndup(char *str, size_t len) {
144	char *ret = (char *) malloc(len + 1);
145	if (ret == NULL) {
146		report_global_error("malloc: %s", strerror(errno));
147		return NULL;
148	}
149	strncpy(ret, str, len);
150	ret[len] = 0;
151	return ret;
152}
153
154static char *
155parse_ident(struct locus *loc, char **str)
156{
157	char *ident = *str;
158
159	if (!isalpha(CTYPE_CONV(**str)) && **str != '_') {
160		report_error(loc->filename, loc->line_no, "bad identifier");
161		return NULL;
162	}
163
164	while (**str && (isalnum(CTYPE_CONV(**str)) || **str == '_')) {
165		++(*str);
166	}
167
168	return xstrndup(ident, *str - ident);
169}
170
171/*
172  Returns position in string at the left parenthesis which starts the
173  function's argument signature. Returns NULL on error.
174*/
175static char *
176start_of_arg_sig(char *str) {
177	char *pos;
178	int stacked = 0;
179
180	if (!strlen(str))
181		return NULL;
182
183	pos = &str[strlen(str)];
184	do {
185		pos--;
186		if (pos < str)
187			return NULL;
188		while ((pos > str) && (*pos != ')') && (*pos != '('))
189			pos--;
190
191		if (*pos == ')')
192			stacked++;
193		else if (*pos == '(')
194			stacked--;
195		else
196			return NULL;
197
198	} while (stacked > 0);
199
200	return (stacked == 0) ? pos : NULL;
201}
202
203static int
204parse_int(struct locus *loc, char **str, long *ret)
205{
206	char *end;
207	long n = strtol(*str, &end, 0);
208	if (end == *str) {
209		report_error(loc->filename, loc->line_no, "bad number");
210		return -1;
211	}
212
213	*str = end;
214	if (ret != NULL)
215		*ret = n;
216	return 0;
217}
218
219static int
220check_nonnegative(struct locus *loc, long l)
221{
222	if (l < 0) {
223		report_error(loc->filename, loc->line_no,
224			     "expected non-negative value, got %ld", l);
225		return -1;
226	}
227	return 0;
228}
229
230static int
231check_int(struct locus *loc, long l)
232{
233	int i = l;
234	if ((long)i != l) {
235		report_error(loc->filename, loc->line_no,
236			     "Number too large: %ld", l);
237		return -1;
238	}
239	return 0;
240}
241
242static int
243parse_char(struct locus *loc, char **str, char expected)
244{
245	if (**str != expected) {
246		report_error(loc->filename, loc->line_no,
247			     "expected '%c', got '%c'", expected, **str);
248		return -1;
249	}
250
251	++*str;
252	return 0;
253}
254
255static struct expr_node *parse_argnum(struct locus *loc,
256				      char **str, int *ownp, int zero);
257
258static struct expr_node *
259parse_zero(struct locus *loc, char **str, struct expr_node *ret, int *ownp)
260{
261	eat_spaces(str);
262	if (**str == '(') {
263		++*str;
264		int own;
265		struct expr_node *arg = parse_argnum(loc, str, &own, 0);
266		if (arg == NULL)
267			return NULL;
268		if (parse_char(loc, str, ')') < 0) {
269		fail:
270			expr_destroy(arg);
271			free(arg);
272			return NULL;
273		}
274
275		struct expr_node *ret = build_zero_w_arg(arg, own);
276		if (ret == NULL)
277			goto fail;
278		*ownp = 1;
279		return ret;
280
281	} else {
282		free(ret);
283		*ownp = 0;
284		return expr_node_zero();
285	}
286}
287
288static int
289wrap_in_zero(struct expr_node **nodep)
290{
291	struct expr_node *n = build_zero_w_arg(*nodep, 1);
292	if (n == NULL)
293		return -1;
294	*nodep = n;
295	return 0;
296}
297
298/*
299 * Input:
300 *  argN   : The value of argument #N, counting from 1
301 *  eltN   : The value of element #N of the containing structure
302 *  retval : The return value
303 *  N      : The numeric value N
304 */
305static struct expr_node *
306parse_argnum(struct locus *loc, char **str, int *ownp, int zero)
307{
308	struct expr_node *expr = malloc(sizeof(*expr));
309	if (expr == NULL)
310		return NULL;
311
312	if (isdigit(CTYPE_CONV(**str))) {
313		long l;
314		if (parse_int(loc, str, &l) < 0
315		    || check_nonnegative(loc, l) < 0
316		    || check_int(loc, l) < 0)
317			goto fail;
318
319		expr_init_const_word(expr, l, type_get_simple(ARGTYPE_LONG), 0);
320
321		if (zero && wrap_in_zero(&expr) < 0)
322			goto fail;
323
324		*ownp = 1;
325		return expr;
326
327	} else {
328		char *const name = parse_ident(loc, str);
329		if (name == NULL) {
330		fail_ident:
331			free(name);
332			goto fail;
333		}
334
335		int is_arg = strncmp(name, "arg", 3) == 0;
336		if (is_arg || strncmp(name, "elt", 3) == 0) {
337			long l;
338			char *num = name + 3;
339			if (parse_int(loc, &num, &l) < 0
340			    || check_int(loc, l) < 0)
341				goto fail_ident;
342
343			if (is_arg) {
344				if (l == 0)
345					expr_init_named(expr, "retval", 0);
346				else
347					expr_init_argno(expr, l - 1);
348			} else {
349				struct expr_node *e_up = malloc(sizeof(*e_up));
350				struct expr_node *e_ix = malloc(sizeof(*e_ix));
351				if (e_up == NULL || e_ix == NULL) {
352					free(e_up);
353					free(e_ix);
354					goto fail_ident;
355				}
356
357				expr_init_up(e_up, expr_self(), 0);
358				struct arg_type_info *ti
359					= type_get_simple(ARGTYPE_LONG);
360				expr_init_const_word(e_ix, l - 1, ti, 0);
361				expr_init_index(expr, e_up, 1, e_ix, 1);
362			}
363
364		} else if (strcmp(name, "retval") == 0) {
365			expr_init_named(expr, "retval", 0);
366
367		} else if (strcmp(name, "zero") == 0) {
368			struct expr_node *ret
369				= parse_zero(loc, str, expr, ownp);
370			if (ret == NULL)
371				goto fail_ident;
372			free(name);
373			return ret;
374
375		} else {
376			report_error(loc->filename, loc->line_no,
377				     "Unknown length specifier: '%s'", name);
378			goto fail_ident;
379		}
380
381		if (zero && wrap_in_zero(&expr) < 0)
382			goto fail_ident;
383
384		free(name);
385		*ownp = 1;
386		return expr;
387	}
388
389fail:
390	free(expr);
391	return NULL;
392}
393
394static struct arg_type_info *
395parse_typedef_name(struct protolib *plib, char **str)
396{
397	char *end = *str;
398	while (*end && (isalnum(CTYPE_CONV(*end)) || *end == '_'))
399		++end;
400	if (end == *str)
401		return NULL;
402
403	size_t len = end - *str;
404	char buf[len + 1];
405	memcpy(buf, *str, len);
406	*str += len;
407	buf[len] = 0;
408
409	struct named_type *nt = protolib_lookup_type(plib, buf);
410	if (nt == NULL)
411		return NULL;
412	return nt->info;
413}
414
415static int
416parse_typedef(struct protolib *plib, struct locus *loc, char **str)
417{
418	(*str) += strlen("typedef");
419	eat_spaces(str);
420	char *name = parse_ident(loc, str);
421
422	/* Look through the typedef list whether we already have a
423	 * forward of this type.  If we do, it must be forward
424	 * structure.  */
425	struct named_type *forward = protolib_lookup_type(&g_prototypes, name);
426	if (forward != NULL
427	    && (forward->info->type != ARGTYPE_STRUCT
428		|| !forward->forward)) {
429		report_error(loc->filename, loc->line_no,
430			     "Redefinition of typedef '%s'", name);
431	err:
432		free(name);
433		return -1;
434	}
435
436	// Skip = sign
437	eat_spaces(str);
438	if (parse_char(loc, str, '=') < 0)
439		goto err;
440	eat_spaces(str);
441
442	int fwd = 0;
443	int own = 0;
444	struct arg_type_info *info
445		= parse_lens(plib, loc, str, NULL, 0, &own, &fwd);
446	if (info == NULL)
447		goto err;
448
449	struct named_type this_nt;
450	named_type_init(&this_nt, info, own);
451	this_nt.forward = fwd;
452
453	if (forward == NULL) {
454		if (protolib_add_named_type(&g_prototypes, name,
455					    1, &this_nt) < 0) {
456			named_type_destroy(&this_nt);
457			goto err;
458		}
459		return 0;
460	}
461
462	/* If we are defining a forward, make sure the definition is a
463	 * structure as well.  */
464	if (this_nt.info->type != ARGTYPE_STRUCT) {
465		report_error(loc->filename, loc->line_no,
466			     "Definition of forward '%s' must be a structure.",
467			     name);
468		named_type_destroy(&this_nt);
469		goto err;
470	}
471
472	/* Now move guts of the actual type over to the forward type.
473	 * We can't just move pointers around, because references to
474	 * forward must stay intact.  */
475	assert(this_nt.own_type);
476	type_destroy(forward->info);
477	*forward->info = *this_nt.info;
478	forward->forward = 0;
479	free(this_nt.info);
480	free(name);
481	return 0;
482}
483
484/* Syntax: struct ( type,type,type,... ) */
485static int
486parse_struct(struct protolib *plib, struct locus *loc,
487	     char **str, struct arg_type_info *info,
488	     int *forwardp)
489{
490	eat_spaces(str);
491
492	if (**str == ';') {
493		if (forwardp == NULL) {
494			report_error(loc->filename, loc->line_no,
495				     "Forward struct can be declared only "
496				     "directly after a typedef.");
497			return -1;
498		}
499
500		/* Forward declaration is currently handled as an
501		 * empty struct.  */
502		type_init_struct(info);
503		*forwardp = 1;
504		return 0;
505	}
506
507	if (parse_char(loc, str, '(') < 0)
508		return -1;
509
510	eat_spaces(str); // Empty arg list with whitespace inside
511
512	type_init_struct(info);
513
514	while (1) {
515		eat_spaces(str);
516		if (**str == 0 || **str == ')') {
517			parse_char(loc, str, ')');
518			return 0;
519		}
520
521		/* Field delimiter.  */
522		if (type_struct_size(info) > 0)
523			parse_char(loc, str, ',');
524
525		eat_spaces(str);
526		int own;
527		struct arg_type_info *field
528			= parse_lens(plib, loc, str, NULL, 0, &own, NULL);
529		if (field == NULL || type_struct_add(info, field, own)) {
530			type_destroy(info);
531			return -1;
532		}
533	}
534}
535
536static int
537parse_string(struct protolib *plib, struct locus *loc,
538	     char **str, struct arg_type_info **retp, int *ownp)
539{
540	struct arg_type_info *info = malloc(sizeof(*info) * 2);
541	if (info == NULL) {
542	fail:
543		free(info);
544		return -1;
545	}
546
547	struct expr_node *length;
548	int own_length;
549	int with_arg = 0;
550
551	if (isdigit(CTYPE_CONV(**str))) {
552		/* string0 is string[retval], length is zero(retval)
553		 * stringN is string[argN], length is zero(argN) */
554		long l;
555		if (parse_int(loc, str, &l) < 0
556		    || check_int(loc, l) < 0)
557			goto fail;
558
559		struct expr_node *length_arg = malloc(sizeof(*length_arg));
560		if (length_arg == NULL)
561			goto fail;
562
563		if (l == 0)
564			expr_init_named(length_arg, "retval", 0);
565		else
566			expr_init_argno(length_arg, l - 1);
567
568		length = build_zero_w_arg(length_arg, 1);
569		if (length == NULL) {
570			expr_destroy(length_arg);
571			free(length_arg);
572			goto fail;
573		}
574		own_length = 1;
575
576	} else {
577		eat_spaces(str);
578		if (**str == '[') {
579			(*str)++;
580			eat_spaces(str);
581
582			length = parse_argnum(loc, str, &own_length, 1);
583			if (length == NULL)
584				goto fail;
585
586			eat_spaces(str);
587			parse_char(loc, str, ']');
588
589		} else if (**str == '(') {
590			/* Usage of "string" as lens.  */
591			++*str;
592
593			free(info);
594
595			eat_spaces(str);
596			info = parse_type(plib, loc, str, NULL, 0, ownp, NULL);
597			if (info == NULL)
598				goto fail;
599
600			eat_spaces(str);
601			parse_char(loc, str, ')');
602
603			with_arg = 1;
604
605		} else {
606			/* It was just a simple string after all.  */
607			length = expr_node_zero();
608			own_length = 0;
609		}
610	}
611
612	/* String is a pointer to array of chars.  */
613	if (!with_arg) {
614		type_init_array(&info[1], type_get_simple(ARGTYPE_CHAR), 0,
615				length, own_length);
616
617		type_init_pointer(&info[0], &info[1], 0);
618		*ownp = 1;
619	}
620
621	info->lens = &string_lens;
622	info->own_lens = 0;
623
624	*retp = info;
625	return 0;
626}
627
628static int
629build_printf_pack(struct locus *loc, struct param **packp, size_t param_num)
630{
631	if (packp == NULL) {
632		report_error(loc->filename, loc->line_no,
633			     "'format' type in unexpected context");
634		return -1;
635	}
636	if (*packp != NULL) {
637		report_error(loc->filename, loc->line_no,
638			     "only one 'format' type per function supported");
639		return -1;
640	}
641
642	*packp = malloc(sizeof(**packp));
643	if (*packp == NULL)
644		return -1;
645
646	struct expr_node *node = malloc(sizeof(*node));
647	if (node == NULL) {
648		free(*packp);
649		return -1;
650	}
651
652	expr_init_argno(node, param_num);
653
654	param_pack_init_printf(*packp, node, 1);
655
656	return 0;
657}
658
659/* Match and consume KWD if it's next in stream, and return 0.
660 * Otherwise return negative number.  */
661static int
662try_parse_kwd(char **str, const char *kwd)
663{
664	size_t len = strlen(kwd);
665	if (strncmp(*str, kwd, len) == 0
666	    && !isalnum(CTYPE_CONV((*str)[len]))) {
667		(*str) += len;
668		return 0;
669	}
670	return -1;
671}
672
673/* Make a copy of INFO and set the *OWN bit if it's not already
674 * owned.  */
675static int
676unshare_type_info(struct locus *loc, struct arg_type_info **infop, int *ownp)
677{
678	if (*ownp)
679		return 0;
680
681	struct arg_type_info *ninfo = malloc(sizeof(*ninfo));
682	if (ninfo == NULL) {
683		report_error(loc->filename, loc->line_no,
684			     "malloc: %s", strerror(errno));
685		return -1;
686	}
687	*ninfo = **infop;
688	*infop = ninfo;
689	*ownp = 1;
690	return 0;
691}
692
693/* XXX extra_param and param_num are a kludge to get in
694 * backward-compatible support for "format" parameter type.  The
695 * latter is only valid if the former is non-NULL, which is only in
696 * top-level context.  */
697static int
698parse_alias(struct protolib *plib, struct locus *loc,
699	    char **str, struct arg_type_info **retp, int *ownp,
700	    struct param **extra_param, size_t param_num)
701{
702	/* For backward compatibility, we need to support things like
703	 * stringN (which is like string[argN], string[N], and also
704	 * bare string.  We might, in theory, replace this by
705	 * preprocessing configure file sources with M4, but for now,
706	 * "string" is syntax.  */
707	if (strncmp(*str, "string", 6) == 0) {
708		(*str) += 6;
709		return parse_string(plib, loc, str, retp, ownp);
710
711	} else if (try_parse_kwd(str, "format") >= 0
712		   && extra_param != NULL) {
713		/* For backward compatibility, format is parsed as
714		 * "string", but it smuggles to the parameter list of
715		 * a function a "printf" argument pack with this
716		 * parameter as argument.  */
717		if (parse_string(plib, loc, str, retp, ownp) < 0)
718			return -1;
719
720		return build_printf_pack(loc, extra_param, param_num);
721
722	} else if (try_parse_kwd(str, "enum") >=0) {
723
724		return parse_enum(plib, loc, str, retp, ownp);
725
726	} else {
727		*retp = NULL;
728		return 0;
729	}
730}
731
732/* Syntax: array ( type, N|argN ) */
733static int
734parse_array(struct protolib *plib, struct locus *loc,
735	    char **str, struct arg_type_info *info)
736{
737	eat_spaces(str);
738	if (parse_char(loc, str, '(') < 0)
739		return -1;
740
741	eat_spaces(str);
742	int own;
743	struct arg_type_info *elt_info
744		= parse_lens(plib, loc, str, NULL, 0, &own, NULL);
745	if (elt_info == NULL)
746		return -1;
747
748	eat_spaces(str);
749	parse_char(loc, str, ',');
750
751	eat_spaces(str);
752	int own_length;
753	struct expr_node *length = parse_argnum(loc, str, &own_length, 0);
754	if (length == NULL) {
755		if (own) {
756			type_destroy(elt_info);
757			free(elt_info);
758		}
759		return -1;
760	}
761
762	type_init_array(info, elt_info, own, length, own_length);
763
764	eat_spaces(str);
765	parse_char(loc, str, ')');
766	return 0;
767}
768
769/* Syntax:
770 *   enum (keyname[=value],keyname[=value],... )
771 *   enum<type> (keyname[=value],keyname[=value],... )
772 */
773static int
774parse_enum(struct protolib *plib, struct locus *loc, char **str,
775	   struct arg_type_info **retp, int *ownp)
776{
777	/* Optional type argument.  */
778	eat_spaces(str);
779	if (**str == '[') {
780		parse_char(loc, str, '[');
781		eat_spaces(str);
782		*retp = parse_nonpointer_type(plib, loc, str, NULL, 0, ownp, 0);
783		if (*retp == NULL)
784			return -1;
785
786		if (!type_is_integral((*retp)->type)) {
787			report_error(loc->filename, loc->line_no,
788				     "integral type required as enum argument");
789		fail:
790			if (*ownp) {
791				/* This also releases associated lens
792				 * if any was set so far.  */
793				type_destroy(*retp);
794				free(*retp);
795			}
796			return -1;
797		}
798
799		eat_spaces(str);
800		if (parse_char(loc, str, ']') < 0)
801			goto fail;
802
803	} else {
804		*retp = type_get_simple(ARGTYPE_INT);
805		*ownp = 0;
806	}
807
808	/* We'll need to set the lens, so unshare.  */
809	if (unshare_type_info(loc, retp, ownp) < 0)
810		goto fail;
811
812	eat_spaces(str);
813	if (parse_char(loc, str, '(') < 0)
814		goto fail;
815
816	struct enum_lens *lens = malloc(sizeof(*lens));
817	if (lens == NULL) {
818		report_error(loc->filename, loc->line_no,
819			     "malloc enum lens: %s", strerror(errno));
820		return -1;
821	}
822
823	lens_init_enum(lens);
824	(*retp)->lens = &lens->super;
825	(*retp)->own_lens = 1;
826
827	long last_val = 0;
828	while (1) {
829		eat_spaces(str);
830		if (**str == 0 || **str == ')') {
831			parse_char(loc, str, ')');
832			return 0;
833		}
834
835		/* Field delimiter.  XXX should we support the C
836		 * syntax, where the enumeration can end in pending
837		 * comma?  */
838		if (lens_enum_size(lens) > 0)
839			parse_char(loc, str, ',');
840
841		eat_spaces(str);
842		char *key = parse_ident(loc, str);
843		if (key == NULL) {
844		err:
845			free(key);
846			goto fail;
847		}
848
849		if (**str == '=') {
850			++*str;
851			eat_spaces(str);
852			if (parse_int(loc, str, &last_val) < 0)
853				goto err;
854		}
855
856		struct value *value = malloc(sizeof(*value));
857		if (value == NULL)
858			goto err;
859		value_init_detached(value, NULL, *retp, 0);
860		value_set_word(value, last_val);
861
862		if (lens_enum_add(lens, key, 1, value, 1) < 0)
863			goto err;
864
865		last_val++;
866	}
867
868	return 0;
869}
870
871static struct arg_type_info *
872parse_nonpointer_type(struct protolib *plib, struct locus *loc,
873		      char **str, struct param **extra_param, size_t param_num,
874		      int *ownp, int *forwardp)
875{
876	enum arg_type type;
877	if (parse_arg_type(str, &type) < 0) {
878		struct arg_type_info *simple;
879		if (parse_alias(plib, loc, str, &simple,
880				ownp, extra_param, param_num) < 0)
881			return NULL;
882		if (simple == NULL)
883			simple = parse_typedef_name(plib, str);
884		if (simple != NULL) {
885			*ownp = 0;
886			return simple;
887		}
888
889		report_error(loc->filename, loc->line_no,
890			     "unknown type around '%s'", *str);
891		return NULL;
892	}
893
894	/* For some types that's all we need.  */
895	switch (type) {
896	case ARGTYPE_VOID:
897	case ARGTYPE_INT:
898	case ARGTYPE_UINT:
899	case ARGTYPE_LONG:
900	case ARGTYPE_ULONG:
901	case ARGTYPE_CHAR:
902	case ARGTYPE_SHORT:
903	case ARGTYPE_USHORT:
904	case ARGTYPE_FLOAT:
905	case ARGTYPE_DOUBLE:
906		*ownp = 0;
907		return type_get_simple(type);
908
909	case ARGTYPE_ARRAY:
910	case ARGTYPE_STRUCT:
911		break;
912
913	case ARGTYPE_POINTER:
914		/* Pointer syntax is not based on keyword, so we
915		 * should never get this type.  */
916		assert(type != ARGTYPE_POINTER);
917		abort();
918	}
919
920	struct arg_type_info *info = malloc(sizeof(*info));
921	if (info == NULL) {
922		report_error(loc->filename, loc->line_no,
923			     "malloc: %s", strerror(errno));
924		return NULL;
925	}
926	*ownp = 1;
927
928	if (type == ARGTYPE_ARRAY) {
929		if (parse_array(plib, loc, str, info) < 0) {
930		fail:
931			free(info);
932			return NULL;
933		}
934	} else {
935		assert(type == ARGTYPE_STRUCT);
936		if (parse_struct(plib, loc, str, info, forwardp) < 0)
937			goto fail;
938	}
939
940	return info;
941}
942
943static struct named_lens {
944	const char *name;
945	struct lens *lens;
946} lenses[] = {
947	{ "hide", &blind_lens },
948	{ "octal", &octal_lens },
949	{ "oct", &octal_lens },
950	{ "bitvec", &bitvect_lens },
951	{ "hex", &hex_lens },
952	{ "bool", &bool_lens },
953	{ "guess", &guess_lens },
954};
955
956static struct lens *
957name2lens(char **str, int *own_lensp)
958{
959	size_t i;
960	for (i = 0; i < sizeof(lenses)/sizeof(*lenses); ++i)
961		if (try_parse_kwd(str, lenses[i].name) == 0) {
962			*own_lensp = 0;
963			return lenses[i].lens;
964		}
965
966	return NULL;
967}
968
969static struct arg_type_info *
970parse_type(struct protolib *plib, struct locus *loc, char **str,
971	   struct param **extra_param, size_t param_num,
972	   int *ownp, int *forwardp)
973{
974	struct arg_type_info *info
975		= parse_nonpointer_type(plib, loc, str, extra_param,
976					param_num, ownp, forwardp);
977	if (info == NULL)
978		return NULL;
979
980	while (1) {
981		eat_spaces(str);
982		if (**str == '*') {
983			struct arg_type_info *outer = malloc(sizeof(*outer));
984			if (outer == NULL) {
985				if (*ownp) {
986					type_destroy(info);
987					free(info);
988				}
989				report_error(loc->filename, loc->line_no,
990					     "malloc: %s", strerror(errno));
991				return NULL;
992			}
993			type_init_pointer(outer, info, *ownp);
994			*ownp = 1;
995			(*str)++;
996			info = outer;
997		} else
998			break;
999	}
1000	return info;
1001}
1002
1003static struct arg_type_info *
1004parse_lens(struct protolib *plib, struct locus *loc,
1005	   char **str, struct param **extra_param,
1006	   size_t param_num, int *ownp, int *forwardp)
1007{
1008	int own_lens;
1009	struct lens *lens = name2lens(str, &own_lens);
1010	int has_args = 1;
1011	struct arg_type_info *info;
1012	if (lens != NULL) {
1013		eat_spaces(str);
1014
1015		/* Octal lens gets special treatment, because of
1016		 * backward compatibility.  */
1017		if (lens == &octal_lens && **str != '(') {
1018			has_args = 0;
1019			info = type_get_simple(ARGTYPE_INT);
1020			*ownp = 0;
1021		} else if (parse_char(loc, str, '(') < 0) {
1022			report_error(loc->filename, loc->line_no,
1023				     "expected type argument after the lens");
1024			return NULL;
1025		}
1026	}
1027
1028	if (has_args) {
1029		eat_spaces(str);
1030		info = parse_type(plib, loc, str, extra_param, param_num,
1031				  ownp, forwardp);
1032		if (info == NULL) {
1033		fail:
1034			if (own_lens && lens != NULL)
1035				lens_destroy(lens);
1036			return NULL;
1037		}
1038	}
1039
1040	if (lens != NULL && has_args) {
1041		eat_spaces(str);
1042		parse_char(loc, str, ')');
1043	}
1044
1045	/* We can't modify shared types.  Make a copy if we have a
1046	 * lens.  */
1047	if (lens != NULL && unshare_type_info(loc, &info, ownp) < 0)
1048		goto fail;
1049
1050	if (lens != NULL) {
1051		info->lens = lens;
1052		info->own_lens = own_lens;
1053	}
1054
1055	return info;
1056}
1057
1058static int
1059param_is_void(struct param *param)
1060{
1061	return param->flavor == PARAM_FLAVOR_TYPE
1062		&& param->u.type.type->type == ARGTYPE_VOID;
1063}
1064
1065static struct arg_type_info *
1066get_hidden_int(void)
1067{
1068	static struct arg_type_info *info = NULL;
1069	if (info != NULL)
1070		return info;
1071
1072	char *str = strdup("hide(int)");
1073	char *ptr = str;
1074	assert(str != NULL);
1075	int own;
1076	struct locus loc = { NULL, 0 };
1077
1078	struct protolib fake_plib;
1079	protolib_init(&fake_plib);
1080
1081	info = parse_lens(&fake_plib, &loc, &ptr, NULL, 0, &own, NULL);
1082
1083	protolib_destroy(&fake_plib);
1084	assert(info != NULL);
1085	free(str);
1086
1087	return info;
1088}
1089
1090static enum callback_status
1091void_to_hidden_int(struct prototype *proto, struct param *param, void *data)
1092{
1093	struct locus *loc = data;
1094	if (param_is_void(param)) {
1095		report_warning(loc->filename, loc->line_no,
1096			       "void parameter assumed to be 'hide(int)'");
1097
1098		static struct arg_type_info *type = NULL;
1099		if (type == NULL)
1100			type = get_hidden_int();
1101		param_destroy(param);
1102		param_init_type(param, type, 0);
1103	}
1104	return CBS_CONT;
1105}
1106
1107static int
1108process_line(struct protolib *plib, struct locus *loc, char *buf)
1109{
1110	char *str = buf;
1111	char *tmp;
1112
1113	debug(3, "Reading line %d of `%s'", loc->line_no, loc->filename);
1114	eat_spaces(&str);
1115
1116	/* A comment or empty line.  */
1117	if (*str == ';' || *str == 0 || *str == '\n')
1118		return 0;
1119
1120	if (strncmp(str, "typedef", 7) == 0) {
1121		parse_typedef(plib, loc, &str);
1122		return 0;
1123	}
1124
1125	struct prototype fun;
1126	prototype_init(&fun);
1127
1128	char *proto_name = NULL;
1129	int own;
1130	fun.return_info = parse_lens(plib, loc, &str, NULL, 0, &own, NULL);
1131	if (fun.return_info == NULL) {
1132	err:
1133		debug(3, " Skipping line %d", loc->line_no);
1134		prototype_destroy(&fun);
1135		free(proto_name);
1136		return -1;
1137	}
1138	fun.own_return_info = own;
1139	debug(4, " return_type = %d", fun.return_info->type);
1140
1141	eat_spaces(&str);
1142	tmp = start_of_arg_sig(str);
1143	if (tmp == NULL) {
1144		report_error(loc->filename, loc->line_no, "syntax error");
1145		goto err;
1146	}
1147	*tmp = '\0';
1148
1149	proto_name = strdup(str);
1150	if (proto_name == NULL) {
1151	oom:
1152		report_error(loc->filename, loc->line_no,
1153			     "%s", strerror(errno));
1154		goto err;
1155	}
1156
1157	str = tmp + 1;
1158	debug(3, " name = %s", proto_name);
1159
1160	struct param *extra_param = NULL;
1161	int have_stop = 0;
1162
1163	while (1) {
1164		eat_spaces(&str);
1165		if (*str == ')')
1166			break;
1167
1168		if (str[0] == '+') {
1169			if (have_stop == 0) {
1170				struct param param;
1171				param_init_stop(&param);
1172				if (prototype_push_param(&fun, &param) < 0)
1173					goto oom;
1174				have_stop = 1;
1175			}
1176			str++;
1177		}
1178
1179		int own;
1180		size_t param_num = prototype_num_params(&fun) - have_stop;
1181		struct arg_type_info *type
1182			= parse_lens(plib, loc, &str, &extra_param,
1183				     param_num, &own, NULL);
1184		if (type == NULL) {
1185			report_error(loc->filename, loc->line_no,
1186				     "unknown argument type");
1187			goto err;
1188		}
1189
1190		struct param param;
1191		param_init_type(&param, type, own);
1192		if (prototype_push_param(&fun, &param) < 0)
1193			goto oom;
1194
1195		eat_spaces(&str);
1196		if (*str == ',') {
1197			str++;
1198			continue;
1199		} else if (*str == ')') {
1200			continue;
1201		} else {
1202			if (str[strlen(str) - 1] == '\n')
1203				str[strlen(str) - 1] = '\0';
1204			report_error(loc->filename, loc->line_no,
1205				     "syntax error around \"%s\"", str);
1206			goto err;
1207		}
1208	}
1209
1210	/* We used to allow void parameter as a synonym to an argument
1211	 * that shouldn't be displayed.  But backends really need to
1212	 * know the exact type that they are dealing with.  The proper
1213	 * way to do this these days is to use the hide lens.
1214	 *
1215	 * So if there are any voids in the parameter list, show a
1216	 * warning and assume that they are ints.  If there's a sole
1217	 * void, assume the function doesn't take any arguments.  The
1218	 * latter is conservative, we can drop the argument
1219	 * altogether, instead of fetching and then not showing it,
1220	 * without breaking any observable behavior.  */
1221	if (prototype_num_params(&fun) == 1
1222	    && param_is_void(prototype_get_nth_param(&fun, 0))) {
1223		if (0)
1224			/* Don't show this warning.  Pre-0.7.0
1225			 * ltrace.conf often used this idiom.  This
1226			 * should be postponed until much later, when
1227			 * extant uses are likely gone.  */
1228			report_warning(loc->filename, loc->line_no,
1229				       "sole void parameter ignored");
1230		prototype_destroy_nth_param(&fun, 0);
1231	} else {
1232		prototype_each_param(&fun, NULL, void_to_hidden_int, loc);
1233	}
1234
1235	if (extra_param != NULL) {
1236		prototype_push_param(&fun, extra_param);
1237		free(extra_param);
1238	}
1239
1240	if (protolib_add_prototype(plib, proto_name, 1, &fun) < 0) {
1241		report_error(loc->filename, loc->line_no,
1242			     "couldn't add prototype: %s",
1243			     strerror(errno));
1244		goto err;
1245	}
1246
1247	return 0;
1248}
1249
1250int
1251read_config_file(struct protolib *plib, const char *path)
1252{
1253	FILE *stream = fopen(path, "r");
1254	if (stream == NULL)
1255		return -1;
1256
1257	debug(DEBUG_FUNCTION, "Reading config file `%s'...", path);
1258
1259	struct locus loc = { path, 0 };
1260	char *line = NULL;
1261	size_t len = 0;
1262	while (getline(&line, &len, stream) >= 0) {
1263		loc.line_no++;
1264		process_line(&g_prototypes, &loc, line);
1265	}
1266
1267	free(line);
1268	fclose(stream);
1269	return 0;
1270}
1271
1272void
1273init_global_config(void)
1274{
1275	protolib_init(&g_prototypes);
1276
1277	struct arg_type_info *void_info = type_get_simple(ARGTYPE_VOID);
1278	static struct arg_type_info ptr_info;
1279	type_init_pointer(&ptr_info, void_info, 0);
1280
1281	static struct named_type voidptr_type;
1282	named_type_init(&voidptr_type, &ptr_info, 0);
1283
1284	if (protolib_add_named_type(&g_prototypes, "addr", 0,
1285				    &voidptr_type) < 0
1286	    || protolib_add_named_type(&g_prototypes, "file", 0,
1287				       &voidptr_type) < 0) {
1288		fprintf(stderr,
1289			"Couldn't initialize aliases `addr' and `file'.\n");
1290		exit(1);
1291	}
1292}
1293
1294void
1295destroy_global_config(void)
1296{
1297	protolib_destroy(&g_prototypes);
1298}
1299