~ubuntu-branches/ubuntu/trusty/keepalived/trusty

« back to all changes in this revision

Viewing changes to lib/parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2005-04-29 23:22:40 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050429232240-a8m3jtpi3cvuyyy2
Tags: 1.1.11-3
Added a warning about sarge kernels to README.Debian and 
the package description 

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 *              data structure representation the conf file representing
8
8
 *              the loadbalanced server pool.
9
9
 *  
10
 
 * Version:     $Id: parser.c,v 1.1.7 2004/04/04 23:28:05 acassen Exp $
 
10
 * Version:     $Id: parser.c,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
11
11
 * 
12
12
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
13
13
 *              
21
21
 *              as published by the Free Software Foundation; either version
22
22
 *              2 of the License, or (at your option) any later version.
23
23
 *
24
 
 * Copyright (C) 2001-2004 Alexandre Cassen, <acassen@linux-vs.org>
 
24
 * Copyright (C) 2001-2005 Alexandre Cassen, <acassen@linux-vs.org>
25
25
 */
26
26
 
27
27
#include "parser.h"
28
28
#include "memory.h"
29
29
 
 
30
/* global vars */
 
31
vector keywords;
 
32
FILE *stream;
 
33
int reload = 0;
 
34
 
30
35
/* local vars */
31
36
static int sublevel = 0;
32
37
 
33
38
void
34
 
keyword_alloc(vector keywords, char *string, void (*handler) (vector))
 
39
keyword_alloc(vector keywords_vec, char *string, void (*handler) (vector))
35
40
{
36
41
        struct keyword *keyword;
37
42
 
38
 
        vector_alloc_slot(keywords);
 
43
        vector_alloc_slot(keywords_vec);
39
44
 
40
45
        keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
41
46
        keyword->string = string;
42
47
        keyword->handler = handler;
43
48
 
44
 
        vector_set_slot(keywords, keyword);
45
 
}
46
 
 
47
 
void
48
 
install_keyword_root(char *string, void (*handler) (vector))
49
 
{
50
 
        keyword_alloc(keywords, string, handler);
51
 
}
52
 
 
53
 
void
54
 
install_sublevel(void)
55
 
{
56
 
        sublevel++;
57
 
}
58
 
 
59
 
void
60
 
install_sublevel_end(void)
61
 
{
62
 
        sublevel--;
63
 
}
64
 
 
65
 
void
66
 
install_keyword(char *string, void (*handler) (vector))
 
49
        vector_set_slot(keywords_vec, keyword);
 
50
}
 
51
 
 
52
void
 
53
keyword_alloc_sub(vector keywords_vec, char *string, void (*handler) (vector))
67
54
{
68
55
        int i = 0;
69
56
        struct keyword *keyword;
70
57
 
71
58
        /* fetch last keyword */
72
 
        keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
 
59
        keyword = VECTOR_SLOT(keywords_vec, VECTOR_SIZE(keywords_vec) - 1);
73
60
 
74
61
        /* position to last sub level */
75
62
        for (i = 0; i < sublevel; i++)
84
71
        keyword_alloc(keyword->sub, string, handler);
85
72
}
86
73
 
 
74
/* Exported helpers */
 
75
void
 
76
install_sublevel(void)
 
77
{
 
78
        sublevel++;
 
79
}
 
80
 
 
81
void
 
82
install_sublevel_end(void)
 
83
{
 
84
        sublevel--;
 
85
}
 
86
 
 
87
void
 
88
install_keyword_root(char *string, void (*handler) (vector))
 
89
{
 
90
        keyword_alloc(keywords, string, handler);
 
91
}
 
92
 
 
93
void
 
94
install_keyword(char *string, void (*handler) (vector))
 
95
{
 
96
        keyword_alloc_sub(keywords, string, handler);
 
97
}
 
98
 
87
99
void
88
100
dump_keywords(vector keydump, int level)
89
101
{
90
102
        int i, j;
91
 
        struct keyword *keyword;
 
103
        struct keyword *keyword_vec;
92
104
 
93
105
        for (i = 0; i < VECTOR_SIZE(keydump); i++) {
94
 
                keyword = VECTOR_SLOT(keydump, i);
 
106
                keyword_vec = VECTOR_SLOT(keydump, i);
95
107
                for (j = 0; j < level; j++)
96
108
                        printf("  ");
97
 
                printf("Keyword : %s\n", keyword->string);
98
 
                if (keyword->sub)
99
 
                        dump_keywords(keyword->sub, level + 1);
 
109
                printf("Keyword : %s\n", keyword_vec->string);
 
110
                if (keyword_vec->sub)
 
111
                        dump_keywords(keyword_vec->sub, level + 1);
100
112
        }
101
113
}
102
114
 
103
115
void
104
 
free_keywords(vector keywords)
 
116
free_keywords(vector keywords_vec)
105
117
{
106
 
        struct keyword *keyword;
 
118
        struct keyword *keyword_vec;
107
119
        int i;
108
120
 
109
 
        for (i = 0; i < VECTOR_SIZE(keywords); i++) {
110
 
                keyword = VECTOR_SLOT(keywords, i);
111
 
                if (keyword->sub)
112
 
                        free_keywords(keyword->sub);
113
 
                FREE(keyword);
 
121
        for (i = 0; i < VECTOR_SIZE(keywords_vec); i++) {
 
122
                keyword_vec = VECTOR_SLOT(keywords_vec, i);
 
123
                if (keyword_vec->sub)
 
124
                        free_keywords(keyword_vec->sub);
 
125
                FREE(keyword_vec);
114
126
        }
115
 
        vector_free(keywords);
 
127
        vector_free(keywords_vec);
116
128
}
117
129
 
118
130
vector
119
131
alloc_strvec(char *string)
120
132
{
121
133
        char *cp, *start, *token;
122
 
        int strlen;
 
134
        int str_len;
123
135
        vector strvec;
124
136
 
125
137
        if (!string)
152
164
                } else {
153
165
                        while (!isspace((int) *cp) && *cp != '\0' && *cp != '"')
154
166
                                cp++;
155
 
                        strlen = cp - start;
156
 
                        token = MALLOC(strlen + 1);
157
 
                        memcpy(token, start, strlen);
158
 
                        *(token + strlen) = '\0';
 
167
                        str_len = cp - start;
 
168
                        token = MALLOC(str_len + 1);
 
169
                        memcpy(token, start, str_len);
 
170
                        *(token + str_len) = '\0';
159
171
                }
160
172
 
161
173
                /* Alloc & set the slot */
290
302
/* recursive configuration stream handler */
291
303
static int kw_level = 0;
292
304
void
293
 
process_stream(vector keywords)
 
305
process_stream(vector keywords_vec)
294
306
{
295
307
        int i;
296
 
        struct keyword *keyword;
 
308
        struct keyword *keyword_vec;
297
309
        char *str;
298
310
        char *buf;
299
311
        vector strvec;
300
312
 
301
 
        buf = MALLOC(MAXBUF);
302
 
        if (!read_line(buf, MAXBUF)) {
303
 
                FREE(buf);
304
 
                return;
305
 
        }
306
 
 
307
 
        strvec = alloc_strvec(buf);
308
 
        FREE(buf);
309
 
 
310
 
        if (!strvec) {
311
 
                process_stream(keywords);
312
 
                return;
313
 
        }
314
 
 
315
 
        str = VECTOR_SLOT(strvec, 0);
316
 
 
317
 
        if (!strcmp(str, EOB) && kw_level > 0) {
 
313
        buf = zalloc(MAXBUF);
 
314
        while (read_line(buf, MAXBUF)) {
 
315
                strvec = alloc_strvec(buf);
 
316
                memset(buf,0, MAXBUF);
 
317
 
 
318
                if (!strvec)
 
319
                        continue;
 
320
 
 
321
                str = VECTOR_SLOT(strvec, 0);
 
322
 
 
323
                if (!strcmp(str, EOB) && kw_level > 0) {
 
324
                        free_strvec(strvec);
 
325
                        break;
 
326
                }
 
327
 
 
328
                for (i = 0; i < VECTOR_SIZE(keywords_vec); i++) {
 
329
                        keyword_vec = VECTOR_SLOT(keywords_vec, i);
 
330
 
 
331
                        if (!strcmp(keyword_vec->string, str)) {
 
332
                                if (keyword_vec->handler)
 
333
                                        (*keyword_vec->handler) (strvec);
 
334
 
 
335
                                if (keyword_vec->sub) {
 
336
                                        kw_level++;
 
337
                                        process_stream(keyword_vec->sub);
 
338
                                        kw_level--;
 
339
                                }
 
340
                                break;
 
341
                        }
 
342
                }
 
343
 
318
344
                free_strvec(strvec);
319
 
                return;
320
 
        }
321
 
 
322
 
        for (i = 0; i < VECTOR_SIZE(keywords); i++) {
323
 
                keyword = VECTOR_SLOT(keywords, i);
324
 
 
325
 
                if (!strcmp(keyword->string, str)) {
326
 
                        if (keyword->handler)
327
 
                                (*keyword->handler) (strvec);
328
 
 
329
 
                        if (keyword->sub) {
330
 
                                kw_level++;
331
 
                                process_stream(keyword->sub);
332
 
                                kw_level--;
333
 
                        }
334
 
                        break;
335
 
                }
336
 
        }
337
 
 
338
 
        free_strvec(strvec);
339
 
        process_stream(keywords);
 
345
        }
 
346
 
 
347
        free(buf);
 
348
        return;
340
349
}
341
350
 
342
351
/* Data initialization */
350
359
        }
351
360
 
352
361
        /* Init Keywords structure */
 
362
        keywords = vector_alloc();
353
363
        (*init_keywords) ();
354
364
 
355
365
/* Dump configuration *
356
 
  vector_dump(keywords);
357
 
  dump_keywords(keywords, 0);
 
366
vector_dump(keywords);
 
367
dump_keywords(keywords, 0);
358
368
*/
359
369
 
360
370
        /* Stream handling */