read_config_file.c revision d44c6b8b090b8b7aa9d971d9e0bfd848732a3071
1#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <string.h>
6#include <stdlib.h>
7#include <ctype.h>
8
9#include "ltrace.h"
10#include "read_config_file.h"
11#include "options.h"
12#include "output.h"
13
14/*
15 *	"void"		LT_PT_VOID
16 *	"int"		LT_PT_INT
17 *	"uint"		LT_PT_UINT
18 *	"octal"		LT_PT_OCTAL
19 *	"char"		LT_PT_CHAR
20 *	"string"	LT_PT_STRING
21 *	"format"	LT_PT_FORMAT
22 *	"addr"		LT_PT_ADDR
23 */
24
25struct function * list_of_functions = NULL;
26
27static struct list_of_pt_t {
28	char * name;
29	enum param_type pt;
30} list_of_pt[] = {
31	{ "void",	LT_PT_VOID },
32	{ "int",	LT_PT_INT },
33	{ "uint",	LT_PT_UINT },
34	{ "octal",	LT_PT_OCTAL },
35	{ "char",	LT_PT_CHAR },
36	{ "addr",	LT_PT_ADDR },
37	{ "file",	LT_PT_FILE },
38	{ "format",	LT_PT_FORMAT },
39	{ "string",	LT_PT_STRING },
40	{ "string0",	LT_PT_STRING0 },
41	{ "string1",	LT_PT_STRING1 },
42	{ "string2",	LT_PT_STRING2 },
43	{ "string3",	LT_PT_STRING3 },
44	{ NULL,		LT_PT_UNKNOWN }		/* Must finish with NULL */
45};
46
47static enum param_type str2type(char ** str)
48{
49	struct list_of_pt_t * tmp = &list_of_pt[0];
50
51	while(tmp->name) {
52		if (!strncmp(*str, tmp->name, strlen(tmp->name))
53			&& index(" ,)#", *(*str+strlen(tmp->name)))) {
54				*str += strlen(tmp->name);
55				return tmp->pt;
56		}
57		tmp++;
58	}
59	return LT_PT_UNKNOWN;
60}
61
62static void eat_spaces(char ** str)
63{
64	while(**str==' ') {
65		(*str)++;
66	}
67}
68
69static int line_no;
70static char * filename;
71
72struct function * process_line (char * buf) {
73	struct function fun;
74	struct function * fun_p;
75	char * str = buf;
76	char * tmp;
77	int i;
78
79	line_no++;
80	if (opt_d>2) {
81		output_line(0, "Reading line %d of `%s'", line_no, filename);
82	}
83	eat_spaces(&str);
84	fun.return_type = str2type(&str);
85	if (fun.return_type==LT_PT_UNKNOWN) {
86		if (opt_d>2) {
87			output_line(0, " Skipping line %d", line_no);
88		}
89		return NULL;
90	}
91	if (opt_d>3) {
92		output_line(0, " return_type = %d", fun.return_type);
93	}
94	eat_spaces(&str);
95	tmp = strpbrk(str, " (");
96	if (!tmp) {
97		output_line(0, "Syntax error in `%s', line %d", filename, line_no);
98		return NULL;
99	}
100	*tmp = '\0';
101	fun.name = strdup(str);
102	str = tmp+1;
103	if (opt_d>2) {
104		output_line(0, " name = %s", fun.name);
105	}
106	fun.params_right = 0;
107	for(i=0; i<MAX_ARGS; i++) {
108		eat_spaces(&str);
109		if (*str == ')') {
110			break;
111		}
112		if (str[0]=='+') {
113			fun.params_right++;
114			str++;
115		} else if (fun.params_right) {
116			fun.params_right++;
117		}
118		fun.param_types[i] = str2type(&str);
119		if (fun.return_type==LT_PT_UNKNOWN) {
120			output_line(0, "Syntax error in `%s', line %d", filename, line_no);
121			return NULL;
122		}
123		eat_spaces(&str);
124		if (*str==',') {
125			str++;
126			continue;
127		} else if (*str==')') {
128			continue;
129		} else {
130			output_line(0, "Syntax error in `%s', line %d", filename, line_no);
131			return NULL;
132		}
133	}
134	fun.num_params = i;
135	fun_p = malloc(sizeof(struct function));
136	memcpy(fun_p, &fun, sizeof(struct function));
137	return fun_p;
138}
139
140void read_config_file(char * file)
141{
142	FILE * stream;
143	char buf[1024];
144
145	filename = file;
146
147	if (opt_d) {
148		output_line(0, "Reading config file `%s'...", filename);
149	}
150
151	stream = fopen(filename, "r");
152	if (!stream) {
153		return;
154	}
155	line_no=0;
156	while (fgets(buf, 1024, stream)) {
157		struct function * tmp = process_line(buf);
158
159		if (tmp) {
160			if (opt_d > 1) {
161				output_line(0, "New function: `%s'", tmp->name);
162			}
163			tmp->next = list_of_functions;
164			list_of_functions = tmp;
165		}
166	}
167}
168