update changelog
[profile/ivi/genivi/genivi-audio-manager.git] / domain-manager / ini.y
1 /*
2 Copyright (c) 2014, Intel Corporation
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10
11     * Redistributions in binary form must reproduce the above copyright
12       notice, this list of conditions and the following disclaimer in
13       the documentation and/or other materials provided with the
14       distribution.
15
16     * Neither the name of Intel Corporation nor the names of its
17       contributors may be used to endorse or promote products derived
18       from this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33
34 %{
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdbool.h>
39
40 #include <murphy/common/macros.h>
41 #include <murphy/common/log.h>
42
43 #include "ini-parser.h"
44
45 static void yyerror(ini_parser_context_t *ctx, void *yyscanner, const char *err);
46
47 /* this has always to be of type yyscan_t */
48 #define YYLEX_PARAM scanner
49 %}
50
51 %error-verbose
52
53 %pure-parser
54
55 %parse-param { ini_parser_context_t *ctx }
56 %parse-param { void *scanner }
57
58 %token OBRACKET
59 %token EBRACKET
60 %token EQUAL
61
62 %union {
63     int number;
64     char *string;
65 }
66
67 %token <string> DATA
68 %token <string> WORD
69 %token <string> STRING
70 %token <string> BOOLEAN
71 /* %token <number> NUMBER */
72 %token <string> NUMBER
73
74 %%
75
76 conf            : sections
77                 ;
78
79 sections        : section sections
80                 | section
81                 ;
82
83 section         : sectionname sectionitems
84                 | sectionname
85                 ;
86
87 sectionname     : OBRACKET WORD EBRACKET  { add_section(ctx->r, $2); }
88                 ;
89
90 sectionitems    : sectionitem sectionitems
91                 | sectionitem
92                 ;
93
94 sectionitem     : keyvaluepair
95                 ;
96
97 keyvaluepair    : key EQUAL value
98                 ;
99
100 key             : keybase OBRACKET modifier EBRACKET
101                 | keybase
102                 ;
103
104 keybase         : WORD { add_key(ctx->r, $1); }
105                 ;
106
107 modifier        : WORD  { add_modifier(ctx->r, $1); }
108                 ;
109
110 value           : STRING  { add_value(ctx->r, $1); }
111                 | BOOLEAN { add_value(ctx->r, $1); }
112                 | NUMBER  { add_value(ctx->r, $1); }
113                 ;
114
115 %%
116
117 static void yyerror(ini_parser_context_t *ctx, void *yyscanner, const char *err)
118 {
119     MRP_UNUSED(yyscanner);
120     mrp_log_error("ini file parser error: '%s'", err);
121     ctx->error = 1;
122     return;
123 }
124
125 bool mrp_parse_ini_file(const char *filename, ini_parser_result_t *result) {
126
127     /* contains the buffers required for saving the string data */
128     ini_parser_context_t ctx;
129     FILE *input;
130
131     /* contains the flex-initialized parser data */
132     void *scanner;
133
134     input = fopen(filename, "r");
135
136     if (!input)
137         return false;
138
139     initialize_parser_buffer(&ctx);
140
141     ctx.r = result;
142
143     ini_parser_lex_init(&scanner);
144
145     /* set the input file */
146     ini_parser_set_in(input, scanner);
147
148     /* set the parameter to be passed to flex (yyextra) */
149     ini_parser_set_extra(&ctx, scanner);
150
151     ini_parser_parse(&ctx, scanner);
152
153     if (ctx.error == 1)
154         return false;
155
156     ini_parser_lex_destroy(scanner);
157     delete_parser_buffer(&ctx);
158
159     return true;
160 };
161