~network-manager/network-manager/ubuntu.hardy.07

« back to all changes in this revision

Viewing changes to system-settings/plugins/ifupdown/interface_parser.c

(merge) RELEASE 0.7~~svn20080928t225540+eni0-0ubuntu1 to ubuntu/intrepid

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
 
2
/* NetworkManager -- Network link manager
 
3
 *
 
4
 * Tom Parker <palfrey@tevp.net>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * (C) Copyright 2004 Tom Parker
 
21
 */
 
22
 
 
23
 
 
24
#include "interface_parser.h"
 
25
#include "NetworkManagerUtils.h"
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
#include "nm-utils.h"
 
30
 
 
31
if_block* first;
 
32
if_block* last;
 
33
 
 
34
if_data* last_data;
 
35
 
 
36
void add_block(const char *type, const char* name)
 
37
{
 
38
        if_block *ret = (if_block*)calloc(1,sizeof(struct _if_block));
 
39
        ret->name = g_strdup(name);
 
40
        ret->type = g_strdup(type);
 
41
        if (first == NULL)
 
42
                first = last = ret;
 
43
        else
 
44
        {
 
45
                last->next = ret;
 
46
                last = ret;
 
47
        }
 
48
        last_data = NULL;
 
49
        //printf("added block '%s' with type '%s'\n",name,type);
 
50
}
 
51
 
 
52
void add_data(const char *key,const char *data)
 
53
{
 
54
        if_data *ret;
 
55
 
 
56
        // Check if there is a block where we can attach our data
 
57
        if (first == NULL)
 
58
                return;
 
59
 
 
60
        ret = (if_data*) calloc(1,sizeof(struct _if_data));
 
61
        ret->key = g_strdup(key);
 
62
        ret->data = g_strdup(data);
 
63
 
 
64
        if (last->info == NULL)
 
65
        {
 
66
                last->info = ret;
 
67
                last_data = ret;
 
68
        }
 
69
        else
 
70
        {
 
71
                last_data->next = ret;
 
72
                last_data = last_data->next;
 
73
        }
 
74
        //printf("added data '%s' with key '%s'\n",data,key);
 
75
}
 
76
 
 
77
#define SPACE_OR_TAB(string,ret) {ret = strchr(string,' ');ret=(ret == NULL?strchr(string,'\t'):ret);}
 
78
 
 
79
void ifparser_init(void)
 
80
{
 
81
        FILE *inp = fopen(ENI_INTERFACES_FILE, "r");
 
82
        int ret = 0;
 
83
        char *line;
 
84
        char *space;
 
85
        char rline[255];
 
86
 
 
87
        if (inp == NULL)
 
88
        {
 
89
                nm_warning ("Error: Can't open %s\n", ENI_INTERFACES_FILE);
 
90
                return;
 
91
        }
 
92
        first = last = NULL;
 
93
        while(1)
 
94
        {
 
95
                line = space = NULL;
 
96
                ret = fscanf(inp,"%255[^\n]\n",rline);
 
97
                if (ret == EOF)
 
98
                        break;
 
99
                // If the line did not match, skip it
 
100
                if (ret == 0) {
 
101
                        fgets(rline, 255, inp);
 
102
                        continue;
 
103
                }
 
104
 
 
105
                line = rline;
 
106
                while(line[0] == ' ')
 
107
                        line++;
 
108
                if (line[0]=='#' || line[0]=='\0')
 
109
                        continue;
 
110
 
 
111
                SPACE_OR_TAB(line,space)
 
112
                        if (space == NULL)
 
113
                        {
 
114
                                nm_warning ("Error: Can't parse interface line '%s'\n",line);
 
115
                                continue;
 
116
                        }
 
117
                space[0] = '\0';
 
118
 
 
119
                // There are four different stanzas:
 
120
                // iface, mapping, auto and allow-*. Create a block for each of them.
 
121
                if (strcmp(line,"iface")==0)
 
122
                {
 
123
                        char *space2 = strchr(space+1,' ');
 
124
                        if (space2 == NULL)
 
125
                        {
 
126
                                nm_warning ("Error: Can't parse iface line '%s'\n",space+1);
 
127
                                continue;
 
128
                        }
 
129
                        space2[0]='\0';
 
130
                        add_block(line,space+1);
 
131
 
 
132
                        if (space2[1]!='\0')
 
133
                        {
 
134
                                space = strchr(space2+1,' ');
 
135
                                if (space == NULL)
 
136
                                {
 
137
                                        nm_warning ("Error: Can't parse data '%s'\n",space2+1);
 
138
                                        continue;
 
139
                                }
 
140
                                space[0] = '\0';
 
141
                                add_data(space2+1,space+1);
 
142
                        }
 
143
                }
 
144
                else if (strcmp(line,"auto")==0)
 
145
                        add_block(line,space+1);
 
146
                else if (strcmp(line,"mapping")==0)
 
147
                        add_block(line,space+1);
 
148
                else if (strncmp(line,"allow-",6)==0)
 
149
                        add_block(line,space+1);
 
150
                else
 
151
                        add_data(line,space+1);
 
152
 
 
153
                //printf("line: '%s' ret=%d\n",rline,ret);
 
154
        }
 
155
        fclose(inp);
 
156
}
 
157
 
 
158
void _destroy_data(if_data *ifd)
 
159
{
 
160
        if (ifd == NULL)
 
161
                return;
 
162
        _destroy_data(ifd->next);
 
163
        free(ifd->key);
 
164
        free(ifd->data);
 
165
        free(ifd);
 
166
        return;
 
167
}
 
168
 
 
169
void _destroy_block(if_block* ifb)
 
170
{
 
171
        if (ifb == NULL)
 
172
                return;
 
173
        _destroy_block(ifb->next);
 
174
        _destroy_data(ifb->info);
 
175
        free(ifb->name);
 
176
        free(ifb->type);
 
177
        free(ifb);
 
178
        return;
 
179
}
 
180
 
 
181
void ifparser_destroy(void)
 
182
{
 
183
        _destroy_block(first);
 
184
        first = last = NULL;
 
185
}
 
186
 
 
187
if_block *ifparser_getfirst(void)
 
188
{
 
189
        return first;
 
190
}
 
191
 
 
192
if_block *ifparser_getif(const char* iface)
 
193
{
 
194
        if_block *curr = first;
 
195
        while(curr!=NULL)
 
196
        {
 
197
                if (strcmp(curr->type,"iface")==0 && strcmp(curr->name,iface)==0)
 
198
                        return curr;
 
199
                curr = curr->next;
 
200
        }
 
201
        return NULL;
 
202
}
 
203
 
 
204
const char *ifparser_getkey(if_block* iface, const char *key)
 
205
{
 
206
        if_data *curr = iface->info;
 
207
        while(curr!=NULL)
 
208
        {
 
209
                if (strcmp(curr->key,key)==0)
 
210
                        return curr->data;
 
211
                curr = curr->next;
 
212
        }
 
213
        return NULL;
 
214
}