1 | /* |
---|
2 | * Zoo main configuration file parser |
---|
3 | * |
---|
4 | * Author : Gérald FENOY |
---|
5 | * |
---|
6 | * Copyright (c) 209-2015 GeoLabs SARL |
---|
7 | * |
---|
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
9 | * of this software and associated documentation files (the "Software"), to deal |
---|
10 | * in the Software without restriction, including without limitation the rights |
---|
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
12 | * copies of the Software, and to permit persons to whom the Software is |
---|
13 | * furnished to do so, subject to the following conditions: |
---|
14 | * |
---|
15 | * The above copyright notice and this permission notice shall be included in |
---|
16 | * all copies or substantial portions of the Software. |
---|
17 | * |
---|
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
24 | * THE SOFTWARE. |
---|
25 | */ |
---|
26 | %{ |
---|
27 | #include <service.h> |
---|
28 | |
---|
29 | static maps* my_maps=NULL; |
---|
30 | static maps* current_maps=NULL; |
---|
31 | static map* current_content=NULL; |
---|
32 | static char* curr_key; |
---|
33 | static int debug=0; |
---|
34 | |
---|
35 | extern void crerror(const char *s); |
---|
36 | |
---|
37 | void usage(void) ; |
---|
38 | |
---|
39 | extern int crdebug; |
---|
40 | |
---|
41 | extern char crtext[]; |
---|
42 | |
---|
43 | extern int crlineno; |
---|
44 | |
---|
45 | extern FILE* crin; |
---|
46 | |
---|
47 | extern int crlex(void); |
---|
48 | extern int crlex_destroy(void); |
---|
49 | |
---|
50 | %} |
---|
51 | |
---|
52 | %union { char* s;char* chaine; char* key;char* val;} |
---|
53 | |
---|
54 | %token <s> ID |
---|
55 | %token <s> CHAINE |
---|
56 | |
---|
57 | %token PAIR SPAIR EPAIR EPAIRS ANID |
---|
58 | %type <chaine> PAIR |
---|
59 | %type <chaine> EPAIRS |
---|
60 | %type <chaine> EPAIR |
---|
61 | %type <chaine> SPAIR |
---|
62 | |
---|
63 | %token WHITESPACE NEWLINE |
---|
64 | |
---|
65 | %type <s> ANID |
---|
66 | |
---|
67 | %% |
---|
68 | |
---|
69 | document |
---|
70 | : miscetoile miscetoile {} |
---|
71 | | contentetoile processid contentetoile document {} |
---|
72 | ; |
---|
73 | |
---|
74 | miscetoile |
---|
75 | : {} |
---|
76 | ; |
---|
77 | |
---|
78 | Attributeetoile |
---|
79 | : Attributeetoile {} |
---|
80 | | {/* Epsilon */} |
---|
81 | ; |
---|
82 | |
---|
83 | contentetoile |
---|
84 | : contentetoile NEWLINE {} |
---|
85 | | contentetoile pair {} |
---|
86 | | {/* Epsilon */} |
---|
87 | ; |
---|
88 | |
---|
89 | pair: PAIR {if(curr_key!=NULL) free(curr_key);curr_key=zStrdup($1);} |
---|
90 | | EPAIR { |
---|
91 | if(current_content==NULL) |
---|
92 | current_content=createMap(curr_key,$1); |
---|
93 | else{ |
---|
94 | addToMap(current_content,curr_key,$1); |
---|
95 | } |
---|
96 | if(debug){ |
---|
97 | printf("EPAIR FOUND !! \n"); |
---|
98 | printf("[%s=>%s]\n",curr_key,$1); |
---|
99 | } |
---|
100 | free(curr_key); |
---|
101 | curr_key=NULL; |
---|
102 | } |
---|
103 | | SPAIR {if(curr_key!=NULL) free(curr_key);curr_key=zStrdup($1);if(debug) printf("SPAIR FOUND !!\n"); } |
---|
104 | ; |
---|
105 | |
---|
106 | |
---|
107 | processid |
---|
108 | : ANID { |
---|
109 | if(current_maps->name!=NULL){ |
---|
110 | addMapToMap(¤t_maps->content,current_content); |
---|
111 | freeMap(¤t_content); |
---|
112 | free(current_content); |
---|
113 | current_maps->next=NULL; |
---|
114 | current_maps->next=createMaps($1); |
---|
115 | current_maps=current_maps->next; |
---|
116 | current_content=current_maps->content; |
---|
117 | } |
---|
118 | else{ |
---|
119 | current_maps->name=(char*)malloc((strlen($1)+1)*sizeof(char)); |
---|
120 | snprintf(current_maps->name,(strlen($1)+1),"%s",$1); |
---|
121 | current_maps->content=NULL; |
---|
122 | current_maps->next=NULL; |
---|
123 | current_content=NULL; |
---|
124 | } |
---|
125 | } |
---|
126 | ; |
---|
127 | |
---|
128 | %% |
---|
129 | |
---|
130 | /** |
---|
131 | * Print on stderr the message and the line number of the error which occurred. |
---|
132 | * |
---|
133 | * @param s the error message |
---|
134 | */ |
---|
135 | void crerror(const char *s) |
---|
136 | { |
---|
137 | if(debug) |
---|
138 | printf("\nligne %d : %s\n",crlineno,s); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Parse the main.cfg file and fill the maps structure. |
---|
143 | * |
---|
144 | * @param file the filename to parse |
---|
145 | * @param my_map the maps structure to fill |
---|
146 | */ |
---|
147 | int conf_read(const char* file,maps* my_map){ |
---|
148 | |
---|
149 | crin = fopen(file,"r"); |
---|
150 | if (crin==NULL){ |
---|
151 | return 2 ; |
---|
152 | } |
---|
153 | |
---|
154 | my_maps=my_map; |
---|
155 | my_maps->name=NULL; |
---|
156 | current_maps=my_maps; |
---|
157 | |
---|
158 | int resultatYYParse = crparse() ; |
---|
159 | if(current_content!=NULL){ |
---|
160 | addMapToMap(¤t_maps->content,current_content); |
---|
161 | current_maps->next=NULL; |
---|
162 | freeMap(¤t_content); |
---|
163 | free(current_content); |
---|
164 | }else{ |
---|
165 | current_maps->next=NULL; |
---|
166 | } |
---|
167 | if(curr_key!=NULL){ |
---|
168 | free(curr_key); |
---|
169 | } |
---|
170 | |
---|
171 | fclose(crin); |
---|
172 | #ifndef WIN32 |
---|
173 | crlex_destroy(); |
---|
174 | #endif |
---|
175 | |
---|
176 | return resultatYYParse; |
---|
177 | } |
---|
178 | |
---|