1
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
2
/* NetworkManager -- Network link manager
4
* Tom Parker <palfrey@tevp.net>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License along
17
* with this program; if not, write to the Free Software Foundation, Inc.,
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
* (C) Copyright 2004 Tom Parker
24
#include "interface_parser.h"
35
void add_block(const char *type, const char* name)
37
if_block *ret = (if_block*)calloc(1,sizeof(struct _if_block));
38
ret->name = g_strdup(name);
39
ret->type = g_strdup(type);
48
//printf("added block '%s' with type '%s'\n",name,type);
51
void add_data(const char *key,const char *data)
55
// Check if there is a block where we can attach our data
59
ret = (if_data*) calloc(1,sizeof(struct _if_data));
60
ret->key = g_strdup(key);
61
ret->data = g_strdup(data);
63
if (last->info == NULL)
70
last_data->next = ret;
71
last_data = last_data->next;
73
//printf("added data '%s' with key '%s'\n",data,key);
76
// join values in src with spaces into dst; dst needs to be large enough
77
static char *join_values_with_spaces(char *dst, char **src)
81
if (src != NULL && *src != NULL) {
84
for (src++; *src != NULL; src++) {
93
void ifparser_init (const char *eni_file, int quiet)
95
FILE *inp = fopen (eni_file, "r");
97
int skip_to_block = 1;
98
int skip_long_line = 0;
103
g_warning ("Error: Can't open %s\n", eni_file);
110
char *token[128]; // 255 chars can only be split into 127 tokens
111
char value[255]; // large enough to join previously split tokens
116
char *ptr = fgets(line+offs, 255-offs, inp);
121
// skip over-long lines
122
if (!feof(inp) && len > 0 && line[len-1] != '\n') {
123
if (!skip_long_line) {
125
g_message ("Error: Skipping over-long-line '%s...'\n", line);
131
// trailing '\n' found: remove it & reset offset to 0
132
if (len > 0 && line[len-1] == '\n') {
137
// if we're in long_line_skip mode, terminate it for real next line
138
if (skip_long_line) {
139
if (len == 0 || line[len-1] != '\\')
144
// unwrap wrapped lines
145
if (len > 0 && line[len-1] == '\\') {
150
//printf(">>%s<<\n", line);
154
for (toknum = 0, token[toknum] = strtok_r(line, SPACES, &safeptr);
155
token[toknum] != NULL;
156
toknum++, token[toknum] = strtok_r(NULL, SPACES, &safeptr))
159
// ignore comments and empty lines
160
if (toknum == 0 || *token[0]=='#')
165
g_message ("Error: Can't parse interface line '%s'\n",
166
join_values_with_spaces(value, token));
172
// There are four different stanzas:
173
// iface, mapping, auto and allow-*. Create a block for each of them.
175
// iface stanza takes at least 3 parameters
176
if (strcmp(token[0], "iface") == 0) {
179
g_message ("Error: Can't parse iface line '%s'\n",
180
join_values_with_spaces(value, token));
184
add_block(token[0], token[1]);
186
add_data(token[2], join_values_with_spaces(value, token + 3));
188
// auto and allow-auto stanzas are equivalent,
189
// both can take multiple interfaces as parameters: add one block for each
190
else if (strcmp(token[0], "auto") == 0 ||
191
strcmp(token[0], "allow-auto") == 0) {
193
for (i = 1; i < toknum; i++)
194
add_block("auto", token[i]);
197
else if (strcmp(token[0], "mapping") == 0) {
198
add_block(token[0], join_values_with_spaces(value, token + 1));
201
// allow-* can take multiple interfaces as parameters: add one block for each
202
else if (strncmp(token[0],"allow-",6) == 0) {
204
for (i = 1; i < toknum; i++)
205
add_block(token[0], token[i]);
211
g_message ("Error: ignoring out-of-block data '%s'\n",
212
join_values_with_spaces(value, token));
215
add_data(token[0], join_values_with_spaces(value, token + 1));
221
void _destroy_data(if_data *ifd)
225
_destroy_data(ifd->next);
232
void _destroy_block(if_block* ifb)
236
_destroy_block(ifb->next);
237
_destroy_data(ifb->info);
244
void ifparser_destroy(void)
246
_destroy_block(first);
250
if_block *ifparser_getfirst(void)
255
int ifparser_get_num_blocks(void)
258
if_block *iter = first;
267
if_block *ifparser_getif(const char* iface)
269
if_block *curr = first;
272
if (strcmp(curr->type,"iface")==0 && strcmp(curr->name,iface)==0)
279
const char *ifparser_getkey(if_block* iface, const char *key)
281
if_data *curr = iface->info;
284
if (strcmp(curr->key,key)==0)
291
int ifparser_get_num_info(if_block* iface)
294
if_data *iter = iface->info;