Fix build break for rpm
[framework/connectivity/bluez.git] / tools / parser.y
1 %{
2 /*
3  *
4  *  BlueZ - Bluetooth protocol stack for Linux
5  *
6  *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36
37 #include <bluetooth/bluetooth.h>
38 #include <bluetooth/rfcomm.h>
39
40 #include "kword.h"
41
42 int yylex(void);
43 int yyerror(char *s); 
44
45 struct rfcomm_opts *opts;
46
47 %}
48
49 %union {
50         int number;
51         char *string;
52         bdaddr_t *bdaddr;
53 }
54
55 %token K_BIND K_DEVICE K_CHANNEL K_COMMENT
56 %token K_YES K_NO
57
58 %token <number> NUMBER RFCOMM
59 %token <string> STRING WORD
60 %token <bdaddr> BDADDR
61
62 %type <number> bool
63
64 %%
65
66 config          :
67                 | statement
68                 | config statement
69                 ;
70
71 statement       : section '{' rfcomm_options '}'
72                 | rfcomm  '{' rfcomm_options '}'
73                 | WORD
74                         {
75                         }
76                 | error
77                         {
78                                 yyclearin;
79                                 yyerrok;
80                         }
81                 ;
82
83 section         : WORD
84                         {
85                                 opts = NULL;
86                         }
87                 ;
88
89 rfcomm          : RFCOMM
90                         {
91                                 if (($1 >= 0) && ($1 < RFCOMM_MAX_DEV))
92                                         opts = &rfcomm_opts[$1];
93                                 else
94                                         opts = NULL;
95                         }
96                 ;
97
98 rfcomm_options  : rfcomm_option ';'
99                 | error ';'
100                 | rfcomm_options rfcomm_option ';'
101                 ;
102
103 rfcomm_option   : K_BIND bool
104                         {
105                                 if (opts)
106                                         opts->bind = $2;
107                         }
108                 | K_DEVICE BDADDR
109                         {
110                                 if (opts)
111                                         bacpy(&opts->bdaddr, $2);
112                         }
113                 | K_CHANNEL NUMBER
114                         {
115                                 if (opts)
116                                         opts->channel = $2;
117                         }
118                 | K_COMMENT STRING
119                         {
120                                 if (opts)
121                                         snprintf(opts->comment, MAXCOMMENTLEN, "%s", $2);
122                         }
123                 | WORD
124                         {
125                                 // Unknown option
126                         }
127                 ;
128
129 bool            : K_YES { $$ = 1; }
130                 | K_NO  { $$ = 0; }
131                 ;
132
133 %%
134
135 int yyerror(char *s) 
136 {
137         fprintf(stderr, "%s line %d\n", s, lineno);
138         return 0;
139 }
140
141 int rfcomm_read_config(char *filename)
142 {
143         extern FILE *yyin;
144         char file[MAXPATHLEN + 1];
145         int i;
146
147         for (i = 0; i < RFCOMM_MAX_DEV; i++) {
148                 rfcomm_opts[i].bind = 0;
149                 bacpy(&rfcomm_opts[i].bdaddr, BDADDR_ANY);
150                 rfcomm_opts[i].channel = 1;
151         }
152
153         if (filename) {
154                 snprintf(file, MAXPATHLEN,  "%s", filename);
155         } else {
156                 snprintf(file, MAXPATHLEN, "%s/.bluetooth/rfcomm.conf", getenv("HOME"));
157
158                 if ((getuid() == 0) || (access(file, R_OK) < 0))
159                         snprintf(file, MAXPATHLEN, "%s/rfcomm.conf", CONFIGDIR);
160         }
161
162         if (!(yyin = fopen(file, "r")))
163                 return -1;
164
165         lineno = 1;
166         yyparse();
167
168         fclose(yyin);
169
170         return 0;
171 }