aboutsummaryrefslogtreecommitdiff
path: root/read_config_file.c
blob: ddaa74206b738088e00e03d8be8318d5448205b0 (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
#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "ltrace.h"
#include "read_config_file.h"
#include "output.h"
#include "debug.h"

/*
 *	"void"		ARGTYPE_VOID
 *	"int"		ARGTYPE_INT
 *	"uint"		ARGTYPE_UINT
 *	"long"		ARGTYPE_LONG
 *	"ulong"		ARGTYPE_ULONG
 *	"octal"		ARGTYPE_OCTAL
 *	"char"		ARGTYPE_CHAR
 *	"string"	ARGTYPE_STRING
 *	"format"	ARGTYPE_FORMAT
 *	"addr"		ARGTYPE_ADDR
 */

struct function * list_of_functions = NULL;

static struct list_of_pt_t {
	char * name;
	enum arg_type pt;
} list_of_pt[] = {
	{ "void",   ARGTYPE_VOID },
	{ "int",    ARGTYPE_INT },
	{ "uint",   ARGTYPE_UINT },
	{ "long",   ARGTYPE_LONG },
	{ "ulong",  ARGTYPE_ULONG },
	{ "octal",  ARGTYPE_OCTAL },
	{ "char",   ARGTYPE_CHAR },
	{ "addr",   ARGTYPE_ADDR },
	{ "file",   ARGTYPE_FILE },
	{ "format", ARGTYPE_FORMAT },
	{ "string", ARGTYPE_STRING },
	{ NULL,     ARGTYPE_UNKNOWN }		/* Must finish with NULL */
};

static struct complete_arg_type
str2type(char ** str) {
	struct list_of_pt_t * tmp = &list_of_pt[0];
	struct complete_arg_type pt = {0,0};

 	if (!strncmp(*str, "string", 6)
	    && isdigit((unsigned char)(*str)[6]))
	{
		pt.at = ARGTYPE_STRINGN;
		pt.argno = atoi(*str + 6);
		*str += strspn(*str, "string0123456789");
		return pt;
 	}

	while(tmp->name) {
		if (!strncmp(*str, tmp->name, strlen(tmp->name))
			&& index(" ,)#", *(*str+strlen(tmp->name)))) {
				*str += strlen(tmp->name);
				pt.at = tmp->pt;
				return pt;
		}
		tmp++;
	}
	pt.at = ARGTYPE_UNKNOWN;
	return pt;
}

static void
eat_spaces(char ** str) {
	while(**str==' ') {
		(*str)++;
	}
}

/*
  Returns position in string at the left parenthesis which starts the
  function's argument signature. Returns NULL on error.
*/
static char *
start_of_arg_sig(char * str) {
	char * pos;
	int stacked = 0;

	if (!strlen(str)) return NULL;

	pos = &str[strlen(str)];
	do {
		pos--;
		if (pos < str) return NULL;
		while ((pos > str) && (*pos != ')') && (*pos != '(')) pos--;

		if (*pos == ')') stacked++;
		else if (*pos == '(') stacked--;
		else return NULL;

	} while (stacked > 0);

	return (stacked == 0) ? pos : NULL;
}

static int line_no;
static char * filename;

static struct function *
process_line (char * buf) {
	struct function fun;
	struct function * fun_p;
	char * str = buf;
	char * tmp;
	int i;

	line_no++;
	debug(3, "Reading line %d of `%s'", line_no, filename);
	eat_spaces(&str);
	fun.return_type = str2type(&str);
	if (fun.return_type.at==ARGTYPE_UNKNOWN) {
		debug(3, " Skipping line %d", line_no);
		return NULL;
	}
	debug(4, " return_type = %d", fun.return_type);
	eat_spaces(&str);
	tmp = start_of_arg_sig(str);
	if (!tmp) {
		output_line(0, "Syntax error in `%s', line %d", filename, line_no);
		return NULL;
	}
	*tmp = '\0';
	fun.name = strdup(str);
	str = tmp+1;
	debug(3, " name = %s", fun.name);
	fun.params_right = 0;
	for(i=0; i<MAX_ARGS; i++) {
		eat_spaces(&str);
		if (*str == ')') {
			break;
		}
		if (str[0]=='+') {
			fun.params_right++;
			str++;
		} else if (fun.params_right) {
			fun.params_right++;
		}
		fun.arg_types[i] = str2type(&str);
		if (fun.return_type.at==ARGTYPE_UNKNOWN) {
			output_line(0, "Syntax error in `%s', line %d", filename, line_no);
			return NULL;
		}
		eat_spaces(&str);
		if (*str==',') {
			str++;
			continue;
		} else if (*str==')') {
			continue;
		} else {
			output_line(0, "Syntax error in `%s', line %d", filename, line_no);
			return NULL;
		}
	}
	fun.num_params = i;
	fun_p = malloc(sizeof(struct function));
	memcpy(fun_p, &fun, sizeof(struct function));
	return fun_p;
}

void
read_config_file(char * file) {
	FILE * stream;
	char buf[1024];

	filename = file;

	debug(1, "Reading config file `%s'...", filename);

	stream = fopen(filename, "r");
	if (!stream) {
		return;
	}
	line_no=0;
	while (fgets(buf, 1024, stream)) {
		struct function * tmp = process_line(buf);

		if (tmp) {
			debug(2, "New function: `%s'", tmp->name);
			tmp->next = list_of_functions;
			list_of_functions = tmp;
		}
	}
	fclose(stream);
}