| 1 | %{ |
|---|
| 2 | //====================================================== |
|---|
| 3 | /** |
|---|
| 4 | Zoo main configuration file parser |
|---|
| 5 | **/ |
|---|
| 6 | //====================================================== |
|---|
| 7 | |
|---|
| 8 | #include <string> |
|---|
| 9 | #include <stdio.h> |
|---|
| 10 | #include <ctype.h> |
|---|
| 11 | #include <service.h> |
|---|
| 12 | #include <vector> |
|---|
| 13 | |
|---|
| 14 | static int defaultsc=0; |
|---|
| 15 | static maps* my_maps=NULL; |
|---|
| 16 | static maps* current_maps=NULL; |
|---|
| 17 | static map* previous_content=NULL; |
|---|
| 18 | static map* current_content=NULL; |
|---|
| 19 | static elements* current_element=NULL; |
|---|
| 20 | static map* scontent=NULL; |
|---|
| 21 | static char* curr_key; |
|---|
| 22 | static int debug=0; |
|---|
| 23 | static int previous_data=0; |
|---|
| 24 | static int current_data=0; |
|---|
| 25 | using namespace std; |
|---|
| 26 | |
|---|
| 27 | extern void crerror(const char *s); |
|---|
| 28 | |
|---|
| 29 | void usage(void) ; |
|---|
| 30 | |
|---|
| 31 | extern int crdebug; |
|---|
| 32 | |
|---|
| 33 | extern char crtext[]; |
|---|
| 34 | |
|---|
| 35 | extern int crlineno; |
|---|
| 36 | |
|---|
| 37 | extern FILE* crin; |
|---|
| 38 | |
|---|
| 39 | extern int crlex(void); |
|---|
| 40 | extern int crlex_destroy(void); |
|---|
| 41 | |
|---|
| 42 | %} |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | //====================================================== |
|---|
| 47 | /* le type des lval des jetons et des elements non terminaux bison */ |
|---|
| 48 | //====================================================== |
|---|
| 49 | %union { char* s;char* chaine; char* key;char* val;} |
|---|
| 50 | //====================================================== |
|---|
| 51 | |
|---|
| 52 | // jetons // |
|---|
| 53 | //====================================================== |
|---|
| 54 | /* les jetons que l on retrouve dans FLEX */ |
|---|
| 55 | //====================================================== |
|---|
| 56 | /* texte on a besoin de récupérer une valeur char* pour la comparer */ |
|---|
| 57 | %token <s> ID |
|---|
| 58 | %token <s> CHAINE |
|---|
| 59 | /* STARTXMLDECL et ENDXMLDECL qui sont <?xml et ?>*/ |
|---|
| 60 | %token STARTXMLDECL ENDXMLDECL |
|---|
| 61 | //====================================================== |
|---|
| 62 | /* version="xxx" et encoding="xxx" */ |
|---|
| 63 | %token VERSIONDECL ENCODINGDECL SDDECL |
|---|
| 64 | //====================================================== |
|---|
| 65 | /* < et > */ |
|---|
| 66 | %token INFCAR SUPCAR |
|---|
| 67 | //====================================================== |
|---|
| 68 | /* / = a1 texte "texte" */ |
|---|
| 69 | %token SLASH Eq CHARDATA ATTVALUE PAIR SPAIR EPAIR EPAIRS ANID |
|---|
| 70 | %type <chaine> PAIR |
|---|
| 71 | %type <chaine> EPAIRS |
|---|
| 72 | %type <chaine> EPAIR |
|---|
| 73 | %type <chaine> SPAIR |
|---|
| 74 | |
|---|
| 75 | //====================================================== |
|---|
| 76 | /* <!-- xxx -> <? xxx yyy ?> */ |
|---|
| 77 | %token PI PIERROR /** COMMENT **/ |
|---|
| 78 | //====================================================== |
|---|
| 79 | /* <!-- xxx -> <? xxx yyy ?> */ |
|---|
| 80 | %token ERREURGENERALE CDATA WHITESPACE NEWLINE |
|---|
| 81 | //====================================================== |
|---|
| 82 | // non terminaux typés |
|---|
| 83 | //====================================================== |
|---|
| 84 | /* elements non terminaux de type char * */ |
|---|
| 85 | /* uniquement ceux qui devrons etre comparés */ |
|---|
| 86 | //====================================================== |
|---|
| 87 | %type <s> STag |
|---|
| 88 | %type <s> ETag |
|---|
| 89 | %type <s> ANID |
|---|
| 90 | //====================================================== |
|---|
| 91 | // %start |
|---|
| 92 | //====================================================== |
|---|
| 93 | |
|---|
| 94 | %% |
|---|
| 95 | // document <//=== |
|---|
| 96 | //====================================================== |
|---|
| 97 | // regle 1 |
|---|
| 98 | // on est a la racine du fichier xml |
|---|
| 99 | //====================================================== |
|---|
| 100 | document |
|---|
| 101 | : miscetoile element miscetoile {} |
|---|
| 102 | | contentetoile processid contentetoile document {} |
|---|
| 103 | ; |
|---|
| 104 | |
|---|
| 105 | miscetoile |
|---|
| 106 | : miscetoile PIERROR {crerror("processing instruction begining with <?xml ?> impossible\n");} |
|---|
| 107 | | miscetoile PI {} |
|---|
| 108 | | {} |
|---|
| 109 | ; |
|---|
| 110 | // element |
|---|
| 111 | //====================================================== |
|---|
| 112 | // regle 39 |
|---|
| 113 | // OUVRANTE CONTENU FERMANTE obligatoirement |
|---|
| 114 | // ou neutre |
|---|
| 115 | // on ne peut pas avoir Epsilon |
|---|
| 116 | // un fichier xml ne peut pas etre vide ou seulement avec un prolog |
|---|
| 117 | //====================================================== |
|---|
| 118 | element |
|---|
| 119 | : STag contentetoile ETag |
|---|
| 120 | { |
|---|
| 121 | /* les non terminaux rendent les valeurs de leur identifiants de balise */ |
|---|
| 122 | /* en char*, donc on peut comparer ces valeurs avec la fonction C++ strcmp(const char*;const char*) */ |
|---|
| 123 | /* de string */ |
|---|
| 124 | if (strcmp($1,$3) != 0) |
|---|
| 125 | { |
|---|
| 126 | crerror("Opening and ending tag mismatch"); |
|---|
| 127 | printf("\n ::details : tag '%s' et '%s' \n",$1,$3); |
|---|
| 128 | return 1; |
|---|
| 129 | // on retourne different de 0 |
|---|
| 130 | // sinon yyparse rendra 0 |
|---|
| 131 | // et dans le main on croira a le fichier xml est valide ! |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | // pour neutre |
|---|
| 135 | // on a rien a faire, meme pas renvoyer l identificateur de balise |
|---|
| 136 | // vu qu'il n y a pas de comparaison d'identificateurs avec un balise jumelle . |
|---|
| 137 | | EmptyElemTag {} |
|---|
| 138 | ; |
|---|
| 139 | //====================================================== |
|---|
| 140 | // STag |
|---|
| 141 | //====================================================== |
|---|
| 142 | // regle 40 |
|---|
| 143 | // BALISE OUVRANTE |
|---|
| 144 | // on est obligé de faire appel a infcar et supcar |
|---|
| 145 | // pour acceder aux start conditions DANSBALISE et INITIAL |
|---|
| 146 | //====================================================== |
|---|
| 147 | STag |
|---|
| 148 | : INFCAR ID Attributeetoile SUPCAR |
|---|
| 149 | { |
|---|
| 150 | |
|---|
| 151 | #ifdef DEBUG |
|---|
| 152 | printf("* Identifiant : %s\n",$2); |
|---|
| 153 | #endif |
|---|
| 154 | |
|---|
| 155 | $$ = $2 ; |
|---|
| 156 | } |
|---|
| 157 | ; |
|---|
| 158 | //====================================================== |
|---|
| 159 | // Attributeetoile |
|---|
| 160 | //====================================================== |
|---|
| 161 | // regle 41 |
|---|
| 162 | // une liste qui peut etre vide d'attributs |
|---|
| 163 | // utiliser la récursivité a gauche |
|---|
| 164 | //====================================================== |
|---|
| 165 | Attributeetoile |
|---|
| 166 | : Attributeetoile attribute {} |
|---|
| 167 | | {/* Epsilon */} |
|---|
| 168 | ; |
|---|
| 169 | //====================================================== |
|---|
| 170 | // attribute |
|---|
| 171 | //====================================================== |
|---|
| 172 | // regle 41 |
|---|
| 173 | // un attribut est compose d'un identifiant |
|---|
| 174 | // d'un "=" |
|---|
| 175 | // et d'une définition de chaine de caractere |
|---|
| 176 | // ( "xxx" ou 'xxx' ) |
|---|
| 177 | //====================================================== |
|---|
| 178 | attribute |
|---|
| 179 | : ID Eq ATTVALUE |
|---|
| 180 | { |
|---|
| 181 | // on verifie que les attributst ne sont pas en double |
|---|
| 182 | // sinon on ajoute au vector |
|---|
| 183 | } |
|---|
| 184 | ; |
|---|
| 185 | //====================================================== |
|---|
| 186 | // EmptyElemTag |
|---|
| 187 | //====================================================== |
|---|
| 188 | // regle 44 |
|---|
| 189 | // ICI ON DEFINIT NEUTRE |
|---|
| 190 | // on ne renvoie pas de char* |
|---|
| 191 | // parce qu'il n'y a pas de comparaisons a faire |
|---|
| 192 | // avec un identifiant d'une balise jumelle |
|---|
| 193 | //====================================================== |
|---|
| 194 | EmptyElemTag |
|---|
| 195 | : INFCAR ID Attributeetoile SLASH SUPCAR {} |
|---|
| 196 | ; |
|---|
| 197 | //====================================================== |
|---|
| 198 | // ETag |
|---|
| 199 | //====================================================== |
|---|
| 200 | // regle 42 |
|---|
| 201 | // BALISE FERMANTE |
|---|
| 202 | // les separateurs après ID sont filtrés |
|---|
| 203 | //====================================================== |
|---|
| 204 | ETag |
|---|
| 205 | : INFCAR SLASH ID SUPCAR |
|---|
| 206 | { |
|---|
| 207 | /* on renvoie l'identifiant de la balise pour pouvoir comparer les 2 */ |
|---|
| 208 | /* /!\ une balise fermante n'a pas d'attributs (c.f. : W3C) */ |
|---|
| 209 | $$ = $3; |
|---|
| 210 | } |
|---|
| 211 | ; |
|---|
| 212 | //====================================================== |
|---|
| 213 | // contentetoile |
|---|
| 214 | //====================================================== |
|---|
| 215 | // regle 43 |
|---|
| 216 | // ENTRE 2 BALISES |
|---|
| 217 | // entre 2 balises, on peut avoir : |
|---|
| 218 | // --- OUVRANTE CONTENU FERMANTE (recursivement !) |
|---|
| 219 | // --- DU TEXTE quelconque |
|---|
| 220 | // --- COMMENTS |
|---|
| 221 | // --- DES PROCESSES INSTRUCTIONS |
|---|
| 222 | // --- /!\ il peut y avoir une processing instruction invalide ! <?xml |
|---|
| 223 | // --- EPSILON |
|---|
| 224 | // ### et/ou tout ca a la suite en nombre indeterminé |
|---|
| 225 | // ### donc c'est un operateur etoile (*) |
|---|
| 226 | //====================================================== |
|---|
| 227 | contentetoile |
|---|
| 228 | : contentetoile element {} |
|---|
| 229 | | contentetoile PIERROR {crerror("processing instruction <?xml ?> impossible\n");} |
|---|
| 230 | | contentetoile PI {} |
|---|
| 231 | ///// on filtre les commentaires | contentetoile comment {} |
|---|
| 232 | | contentetoile NEWLINE {/*printf("NEWLINE FOUND !!");*/} |
|---|
| 233 | | contentetoile pair {} |
|---|
| 234 | | contentetoile processid {} |
|---|
| 235 | | contentetoile texteinterbalise {} |
|---|
| 236 | | contentetoile CDATA {} |
|---|
| 237 | | {/* Epsilon */} |
|---|
| 238 | ; |
|---|
| 239 | //====================================================== |
|---|
| 240 | // texteinterbalise |
|---|
| 241 | //====================================================== |
|---|
| 242 | // regle 14 |
|---|
| 243 | // DU TEXTE quelconque |
|---|
| 244 | // c'est du CHARDATA |
|---|
| 245 | // il y a eut un probleme avec ID, |
|---|
| 246 | // on a mis des starts conditions, |
|---|
| 247 | // maintenant on croise les ID dans les dbalises |
|---|
| 248 | // et des CHARDATA hors des balises |
|---|
| 249 | //====================================================== |
|---|
| 250 | texteinterbalise |
|---|
| 251 | : CHARDATA {} |
|---|
| 252 | ; |
|---|
| 253 | //====================================================== |
|---|
| 254 | |
|---|
| 255 | pair: PAIR {curr_key=strdup($1);/*printf("START 0 PAIR FOUND !! \n [%s]\n",$1);*/} |
|---|
| 256 | | EPAIR { |
|---|
| 257 | if(current_content==NULL) |
|---|
| 258 | current_content=createMap(curr_key,$1); |
|---|
| 259 | else{ |
|---|
| 260 | addToMap(current_content,curr_key,$1); |
|---|
| 261 | } |
|---|
| 262 | if(debug){ |
|---|
| 263 | printf("EPAIR FOUND !! \n"); |
|---|
| 264 | printf("[%s=>%s]\n",curr_key,$1); |
|---|
| 265 | } |
|---|
| 266 | free(curr_key); |
|---|
| 267 | } |
|---|
| 268 | | SPAIR {curr_key=strdup($1);if(debug) printf("SPAIR FOUND !!\n"); } |
|---|
| 269 | ; |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | processid |
|---|
| 273 | : ANID { |
|---|
| 274 | if(current_maps->name!=NULL){ |
|---|
| 275 | addMapToMap(¤t_maps->content,current_content); |
|---|
| 276 | freeMap(¤t_content); |
|---|
| 277 | free(current_content); |
|---|
| 278 | current_maps->next=NULL; |
|---|
| 279 | current_maps->next=(maps*)malloc(MAPS_SIZE); |
|---|
| 280 | current_maps->next->name=strdup($1); |
|---|
| 281 | current_maps->next->content=NULL; |
|---|
| 282 | current_maps->next->next=NULL; |
|---|
| 283 | current_maps=current_maps->next; |
|---|
| 284 | current_content=current_maps->content; |
|---|
| 285 | } |
|---|
| 286 | else{ |
|---|
| 287 | current_maps->name=(char*)malloc((strlen($1)+1)*sizeof(char)); |
|---|
| 288 | snprintf(current_maps->name,(strlen($1)+1),"%s",$1); |
|---|
| 289 | current_maps->content=NULL; |
|---|
| 290 | current_maps->next=NULL; |
|---|
| 291 | current_content=NULL; |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | ; |
|---|
| 295 | |
|---|
| 296 | %% |
|---|
| 297 | |
|---|
| 298 | // crerror |
|---|
| 299 | //====================================================== |
|---|
| 300 | /* fonction qui affiche l erreur si il y en a une */ |
|---|
| 301 | //====================================================== |
|---|
| 302 | void crerror(const char *s) |
|---|
| 303 | { |
|---|
| 304 | if(debug) |
|---|
| 305 | printf("\nligne %d : %s\n",crlineno,s); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | // main |
|---|
| 309 | //====================================================== |
|---|
| 310 | /* fonction principale : entrée dans le programme */ |
|---|
| 311 | //====================================================== |
|---|
| 312 | int conf_read(const char* file,maps* my_map){ |
|---|
| 313 | |
|---|
| 314 | crin = fopen(file,"r"); |
|---|
| 315 | if (crin==NULL){ |
|---|
| 316 | printf("error : le fichier specifie n'existe pas ou n'est pas accessible en lecture\n") ; |
|---|
| 317 | return 2 ; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | my_maps=my_map; |
|---|
| 321 | my_maps->name=NULL; |
|---|
| 322 | current_maps=my_maps; |
|---|
| 323 | |
|---|
| 324 | int resultatYYParse = crparse() ; |
|---|
| 325 | if(current_content!=NULL){ |
|---|
| 326 | addMapToMap(¤t_maps->content,current_content); |
|---|
| 327 | current_maps->next=NULL; |
|---|
| 328 | freeMap(¤t_content); |
|---|
| 329 | free(current_content); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | fclose(crin); |
|---|
| 333 | #ifndef WIN32 |
|---|
| 334 | crlex_destroy(); |
|---|
| 335 | #endif |
|---|
| 336 | |
|---|
| 337 | return resultatYYParse; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | //====================================================== |
|---|
| 342 | // FIN // |
|---|
| 343 | //====================================================== |
|---|