File: btsrv-parser.y

package info (click to toggle)
affix 2.1.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,908 kB
  • ctags: 1,247
  • sloc: ansic: 13,807; sh: 7,119; yacc: 232; makefile: 152; lex: 126; python: 56
file content (322 lines) | stat: -rw-r--r-- 6,407 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
%{
/* 
   Affix - Bluetooth Protocol Stack for Linux
   Copyright (C) 2001 Nokia Corporation
   Original Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/* 
   $Id: btsrv-parser.y,v 1.1 2003/03/03 07:52:13 kds Exp $

   parser for btsrv configuration file

   Fixes:	
   		Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
*/

#include <affix/config.h>

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/errno.h>

#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <termios.h>

#include <affix/bluetooth.h>
#include <affix/btcore.h>

#include "btsrv.h"



//#define YYPARSE_PARAM	param
//#define YYLEX_PARAM	param

int yylex(void);
void yyerror(char *s);
void myyyerror(char *format, ...);

extern int yylineno;

static char		*config_file;
static int	 	svcnum;
static int		devnum;
static device_t		*dev;
static service_t	*svc;

int	waitsemi = 0;

%}

/* bison declaration */
//%defines
//%name-prefix="btsrv_"
//%output="btsrv-config.c"

%union {
	int	num;
	char	*str;
}

/* Configuration attribute tokens */
%token	OPT_DEVICE OPT_SERVICE OPT_PROFILE OPT_FLAGS OPT_EXEC OPT_PROVIDER OPT_DESCRIPT
%token	OPT_SECURITY OPT_COD OPT_NAME OPT_ROLE OPT_PKTTYPE OPT_SCANMODE OPT_PORT OPT_ACTIVE
%token	OPT_YES OPT_NO OPT_MANAGEPIN OPT_MANAGEKEY OPT_STARTSVC OPT_INITDEV

%token <num>	NUM
%token <str>	STRING WORDLIST WORD

%type <num>	bool
%type <str>	name
%type <str>	string params

%start config

%%

string:	WORD | STRING ;
params:	WORD | WORDLIST ;
bool:	OPT_YES { $$ = 1; } | OPT_NO { $$ = 0; } ;
	
name:	'*' { $$ = NULL; }
	| string
	;
	
optend:	{ waitsemi = 1; } ';';


config:	/* empty */
	| entry config
	;

entry:	option optend | device | service
	;


/* ------------------------------------------------------------------- */

option:
	OPT_MANAGEPIN bool { managepin = $2; }
	| OPT_MANAGEKEY bool { managekey = $2; }
	| OPT_STARTSVC bool { startsvc = $2; }
	| OPT_INITDEV bool { initdev = $2; }
	;

device:
	'<' OPT_DEVICE name 
			{
				char	*name = $3;
				if (name == NULL) {
					DBPRT("found common device\n");
					if (devnum)
						devices[devnum] = devices[0];
					dev = &devices[0];
					dev->name[0] = '*';
				} else {
					DBPRT("found device: %s", name);
					/* get device num */
					dev = &devices[devnum];
					strcpy(dev->name, name);
				}
			}
	'>'
		devopts
	'<' '/' OPT_DEVICE '>' { devnum++; }
	;


devopts:	/*empty*/
		| devopt optend devopts
		;

devopt:
	OPT_NAME string
			{
				dev->btname = strdup($2);
			}
	| OPT_COD params
			{
				str2cod($2, &dev->cod);
				DBPRT("cod: %#.6x", dev->cod);
			}
	| OPT_ROLE params
			{
				str2mask(role_map, $2, &dev->role);
				;
			}
	| OPT_SECURITY params
			{
				str2mask(sec_mode_map, $2, &dev->security);
				DBPRT("sec_mode: %#x", dev->security);
			}
	| OPT_SCANMODE params
			{
				str2mask(scan_map, $2, &dev->scan);
				DBPRT("scan_mode: %#x", dev->scan);

			}
	| OPT_PKTTYPE params
			{
				str2mask(pkt_type_map, $2, &dev->pkt_type);
				DBPRT("pkt_type: %#x", dev->pkt_type);
			}
	//| error { /*yyclearin; yyerrok;*/ }
	;



/* ------------------------------------------------------------------------------ */
service:
	'<' OPT_SERVICE WORD 
			{ 
				int	i;
				svc = &services[svcnum];
				DBPRT("found service: [%s]", $3);
				for (i = 0; profiles[i].name; i++) {
					if (strcasecmp(profiles[i].name, $3) == 0)
						break;
				}
				if (profiles[i].name == NULL) {
					BTERROR("Invalid profile name %s in config file %s line %d",
						$3, config_file, yylineno);
					return -1;
				}
				svc->info = &profiles[i];
			} 
		'>'
		svcopts
	'<' '/' OPT_SERVICE '>'	{ svcnum++; }
	;

svcopts:	/*empty*/
		| svcopt optend svcopts
		;

svcopt:	
	OPT_PROFILE string
			{ 
				DBPRT("found profile: %s", $2); 
			}
	| OPT_FLAGS params
			{ 
				struct affix_tupla	flags[] = {
						{SRV_FLAG_SOCKET, "socket"},
						{SRV_FLAG_RFCOMM_TTY|SRV_FLAG_SOCKET, "tty"},
						{SRV_FLAG_STD, "std"},
						{0, 0}
						};

				DBPRT("found flag list: [%s]", $2);
				if (!str2mask(flags, $2, &svc->flags)) {
					BTERROR("Invalid flags at line %d", yylineno);
					return -1;
				}
			}
	| OPT_EXEC STRING 
			{ 
				strcpy(svc->cmd, $2);
				DBPRT("found exec: %s", svc->cmd);
			}
	| OPT_NAME string
			{
				svc->name = strdup($2);
			}
	| OPT_PROVIDER string
			{
				svc->prov = strdup($2);
			}
	| OPT_DESCRIPT string
			{
				svc->desc = strdup($2);
			}
	| OPT_COD params
			{
				str2cod_svc($2, &svc->cod);
				DBPRT("cod: %#.6x", svc->cod);
			}
	| OPT_SECURITY params
			{
				str2sec_level($2, &svc->security);
			}
	| OPT_PORT NUM
			{
				svc->port = $2;
			}
					
	| OPT_ACTIVE bool
			{
				DBPRT("Found active: %d\n", $2);
				svc->active = $2;
			}
	//| error { /*yyclearin; yyerrok;*/ }
	;

/* ------------------------------------------------------------------- */

%%

void yyerror(char *s)
{
	BTERROR("reading config file: %s at line %d", s, yylineno);
}

void myyyerror(char *format, ...)
{
	int	n;
	char	str[255];
	va_list	ap;

	va_start(ap, format);
	n = vsnprintf(str, sizeof(str), format, ap);
	va_end(ap);
	yyerror(str);
}
	
int btsrv_read_config(char *file, int *service_num, int *device_num)
{
	int			err;
	extern FILE		*yyin;
	
	config_file = file;
	yyin = fopen(config_file, "r");
	if (!yyin) {
		BTERROR("Unable to open config file %s", config_file);
                return -1;
	}
	svcnum = 0;
	devnum = 0;
	err = yyparse();	/* parse config file */
	*service_num = svcnum;
	*device_num = devnum;
	fclose(yyin);
	yyin = NULL;
	return err;
}