~ubuntu-branches/debian/sid/network-manager/sid

« back to all changes in this revision

Viewing changes to .pc/51-normalized-keys.patch/system-settings/plugins/ifupdown/interface_parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-03-06 20:59:46 UTC
  • Revision ID: james.westby@ubuntu.com-20110306205946-fpdcb3ordico3ylg
Tags: 0.8.2-6
* debian/patches/83-dnsmasq-send-no-config-file-instead-of-a-bogus-one.patch
  - Newer versions of dnsmasq validate the option parameters more strictly.
    Instead of passing a bogus file name simply use --conf-file without
    additional parameters. (Closes: #615082)
* debian/control
  - Remove old Conflicts/Replaces which were required for upgrades to
    squeeze.
  - Bump Breaks against network-manager-gnome to (<< 0.8.2) to avoid
    partial upgrades which can lead to problems with user settings for
    ethernet connections. (Closes: #612291)
* debian/ifblacklist_migrate.sh
  - Only comment out iface lines if we have an exact match for the network
    interface. (Closes: #612247)
* debian/patches/51-normalized-keys.patch
  - Normalize keys in ifupdown parser, so we accept options with either
    hyphens or underscores, like e.g. bridge_ports and bridge-ports.
    (Closes: #609831)

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 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.
 
19
 *
 
20
 * (C) Copyright 2004 Tom Parker
 
21
 */
 
22
 
 
23
 
 
24
#include "interface_parser.h"
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include "nm-utils.h"
 
29
 
 
30
if_block* first;
 
31
if_block* last;
 
32
 
 
33
if_data* last_data;
 
34
 
 
35
void add_block(const char *type, const char* name)
 
36
{
 
37
        if_block *ret = (if_block*)calloc(1,sizeof(struct _if_block));
 
38
        ret->name = g_strdup(name);
 
39
        ret->type = g_strdup(type);
 
40
        if (first == NULL)
 
41
                first = last = ret;
 
42
        else
 
43
        {
 
44
                last->next = ret;
 
45
                last = ret;
 
46
        }
 
47
        last_data = NULL;
 
48
        //printf("added block '%s' with type '%s'\n",name,type);
 
49
}
 
50
 
 
51
void add_data(const char *key,const char *data)
 
52
{
 
53
        if_data *ret;
 
54
 
 
55
        // Check if there is a block where we can attach our data
 
56
        if (first == NULL)
 
57
                return;
 
58
 
 
59
        ret = (if_data*) calloc(1,sizeof(struct _if_data));
 
60
        ret->key = g_strdup(key);
 
61
        ret->data = g_strdup(data);
 
62
 
 
63
        if (last->info == NULL)
 
64
        {
 
65
                last->info = ret;
 
66
                last_data = ret;
 
67
        }
 
68
        else
 
69
        {
 
70
                last_data->next = ret;
 
71
                last_data = last_data->next;
 
72
        }
 
73
        //printf("added data '%s' with key '%s'\n",data,key);
 
74
}
 
75
 
 
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)
 
78
{
 
79
        if (dst != NULL) {
 
80
                *dst = '\0';
 
81
                if (src != NULL && *src != NULL) {
 
82
                        strcat(dst, *src);
 
83
 
 
84
                        for (src++; *src != NULL; src++) {
 
85
                                strcat(dst, " ");
 
86
                                strcat(dst, *src);
 
87
                        }
 
88
                }
 
89
        }
 
90
        return(dst);
 
91
}
 
92
 
 
93
void ifparser_init (const char *eni_file, int quiet)
 
94
{
 
95
        FILE *inp = fopen (eni_file, "r");
 
96
        char line[255];
 
97
        int skip_to_block = 1;
 
98
        int skip_long_line = 0;
 
99
        int offs = 0;
 
100
 
 
101
        if (inp == NULL) {
 
102
                if (!quiet)
 
103
                        g_warning ("Error: Can't open %s\n", eni_file);
 
104
                return;
 
105
        }
 
106
 
 
107
        first = last = NULL;
 
108
        while (!feof(inp))
 
109
        {
 
110
                char *token[128];       // 255 chars can only be split into 127 tokens
 
111
                char value[255];        // large enough to join previously split tokens
 
112
                char *safeptr;
 
113
                int toknum;
 
114
                int len = 0;
 
115
 
 
116
                char *ptr = fgets(line+offs, 255-offs, inp);
 
117
                if (ptr == NULL)
 
118
                        break;
 
119
 
 
120
                len = strlen(line);
 
121
                // skip over-long lines
 
122
                if (!feof(inp) && len > 0 &&  line[len-1] != '\n') {
 
123
                        if (!skip_long_line) {
 
124
                                if (!quiet)
 
125
                                        g_message ("Error: Skipping over-long-line '%s...'\n", line);
 
126
                        }
 
127
                        skip_long_line = 1;
 
128
                        continue;
 
129
                }
 
130
 
 
131
                // trailing '\n' found: remove it & reset offset to 0
 
132
                if (len > 0 && line[len-1] == '\n') {
 
133
                        line[--len] = '\0';
 
134
                        offs = 0;
 
135
                }
 
136
 
 
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] != '\\')
 
140
                                skip_long_line = 0;
 
141
                        continue;
 
142
                }
 
143
 
 
144
                // unwrap wrapped lines
 
145
                if (len > 0 && line[len-1] == '\\') {
 
146
                        offs = len - 1;
 
147
                        continue;
 
148
                }
 
149
 
 
150
                //printf(">>%s<<\n", line);
 
151
 
 
152
#define SPACES  " \t"
 
153
                // tokenize input;
 
154
                for (toknum = 0, token[toknum] = strtok_r(line, SPACES, &safeptr);
 
155
                     token[toknum] != NULL;
 
156
                     toknum++, token[toknum] = strtok_r(NULL, SPACES, &safeptr))
 
157
                        ;
 
158
 
 
159
                // ignore comments and empty lines
 
160
                if (toknum == 0 || *token[0]=='#')
 
161
                        continue;
 
162
 
 
163
                if (toknum < 2) {
 
164
                        if (!quiet) {
 
165
                                g_message ("Error: Can't parse interface line '%s'\n",
 
166
                                                join_values_with_spaces(value, token));
 
167
                        }
 
168
                        skip_to_block = 1;
 
169
                        continue;
 
170
                }
 
171
 
 
172
                // There are four different stanzas:
 
173
                // iface, mapping, auto and allow-*. Create a block for each of them.
 
174
 
 
175
                // iface stanza takes at least 3 parameters
 
176
                if (strcmp(token[0], "iface") == 0) {
 
177
                        if (toknum < 4) {
 
178
                                if (!quiet) {
 
179
                                        g_message ("Error: Can't parse iface line '%s'\n",
 
180
                                                        join_values_with_spaces(value, token));
 
181
                                }
 
182
                                continue;
 
183
                        }
 
184
                        add_block(token[0], token[1]);
 
185
                        skip_to_block = 0;
 
186
                        add_data(token[2], join_values_with_spaces(value, token + 3));
 
187
                }
 
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) {
 
192
                        int i;
 
193
                        for (i = 1; i < toknum; i++)
 
194
                                add_block("auto", token[i]);
 
195
                        skip_to_block = 0;
 
196
                }
 
197
                else if (strcmp(token[0], "mapping") == 0) {
 
198
                        add_block(token[0], join_values_with_spaces(value, token + 1));
 
199
                        skip_to_block = 0;
 
200
                }
 
201
                // allow-* can take multiple interfaces as parameters: add one block for each
 
202
                else if (strncmp(token[0],"allow-",6) == 0) {
 
203
                        int i;
 
204
                        for (i = 1; i < toknum; i++)
 
205
                                add_block(token[0], token[i]);
 
206
                        skip_to_block = 0;
 
207
                }
 
208
                else {
 
209
                        if (skip_to_block) {
 
210
                                if (!quiet) {
 
211
                                        g_message ("Error: ignoring out-of-block data '%s'\n",
 
212
                                                        join_values_with_spaces(value, token));
 
213
                                }
 
214
                        } else
 
215
                                add_data(token[0], join_values_with_spaces(value, token + 1));
 
216
                }
 
217
        }
 
218
        fclose(inp);
 
219
}
 
220
 
 
221
void _destroy_data(if_data *ifd)
 
222
{
 
223
        if (ifd == NULL)
 
224
                return;
 
225
        _destroy_data(ifd->next);
 
226
        free(ifd->key);
 
227
        free(ifd->data);
 
228
        free(ifd);
 
229
        return;
 
230
}
 
231
 
 
232
void _destroy_block(if_block* ifb)
 
233
{
 
234
        if (ifb == NULL)
 
235
                return;
 
236
        _destroy_block(ifb->next);
 
237
        _destroy_data(ifb->info);
 
238
        free(ifb->name);
 
239
        free(ifb->type);
 
240
        free(ifb);
 
241
        return;
 
242
}
 
243
 
 
244
void ifparser_destroy(void)
 
245
{
 
246
        _destroy_block(first);
 
247
        first = last = NULL;
 
248
}
 
249
 
 
250
if_block *ifparser_getfirst(void)
 
251
{
 
252
        return first;
 
253
}
 
254
 
 
255
int ifparser_get_num_blocks(void)
 
256
{
 
257
        int i = 0;
 
258
        if_block *iter = first;
 
259
 
 
260
        while (iter) {
 
261
                i++;
 
262
                iter = iter->next;
 
263
        }
 
264
        return i;
 
265
}
 
266
 
 
267
if_block *ifparser_getif(const char* iface)
 
268
{
 
269
        if_block *curr = first;
 
270
        while(curr!=NULL)
 
271
        {
 
272
                if (strcmp(curr->type,"iface")==0 && strcmp(curr->name,iface)==0)
 
273
                        return curr;
 
274
                curr = curr->next;
 
275
        }
 
276
        return NULL;
 
277
}
 
278
 
 
279
const char *ifparser_getkey(if_block* iface, const char *key)
 
280
{
 
281
        if_data *curr = iface->info;
 
282
        while(curr!=NULL)
 
283
        {
 
284
                if (strcmp(curr->key,key)==0)
 
285
                        return curr->data;
 
286
                curr = curr->next;
 
287
        }
 
288
        return NULL;
 
289
}
 
290
 
 
291
int ifparser_get_num_info(if_block* iface)
 
292
{
 
293
        int i = 0;
 
294
        if_data *iter = iface->info;
 
295
 
 
296
        while (iter) {
 
297
                i++;
 
298
                iter = iter->next;
 
299
        }
 
300
        return i;
 
301
}