summaryrefslogtreecommitdiff
path: root/tests/ddb/ddb-txt.y
blob: 1596827e9745e379a43b95b4c945dc8e03c206d9 (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/** \file ddb-txt.y
 * \author Copyright (C) 2006 Hans Ulrich Niedermann
 *
 * \note
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * \note
 * This library 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
 * Lesser General Public License for more details. 
 *
 * \note
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define _GPHOTO2_INTERNAL_CODE
#include <gphoto2/gphoto2-abilities-list.h>

#include "ddb-common.h"
#include "ddb-txt.tab.h"


extern char *yytext;


char *current_driver = NULL;
char *current_device_name = NULL;
CameraAbilities ca;
unsigned int speed_index = 0;

char *alloc_parse_string(char *s);

char *error_string = NULL;
     
static void reset_ca(void);


#ifdef __GNUC__
#define __unused__ __attribute__((unused))
#else
#define __unused__
#endif


static int
compare_camera_abilities(const CameraAbilities *a, 
			 const CameraAbilities *b);


%}

%defines

/* This is supposed to track linenumbers and stuff, but I don't know how to
 * make it work. */
%locations

/* Pure yylex.  */
//%pure-parser

/* More than "syntax error" */
%error-verbose

/* parameters to yyparse() and yyerror() */
%parse-param { char const *file_name };
%parse-param { CameraAbilitiesList *al };

/* parameters to yylex() */
/* %lex-param { CameraAbilitiesList *al }; */

%initial-action
{
  lexer_reset(file_name);
  yyloc.begin.filename = yyloc.end.filename = file_name;
  yyloc.begin.column = yyloc.end.column = -1;
  yyloc.begin.lineno = yyloc.end.lineno = -1;
  @$.begin.filename = @$.end.filename = file_name;
  error_string = NULL;
};

%token TOK_SEP TOK_COMMA
%token TOK_WHITESPACE TOK_NEWLINE

%token <ui_val> TOK_NUMBER
%token <str_val> TOK_STRING

%token TOK_INTERFACE
%token TOK_DRIVER
%token TOK_DEVICE
%token TOK_BEGIN
%token TOK_END

%token TOK_SERIAL
%token TOK_SPEEDS

%token TOK_USB
%token TOK_PRODUCT
%token TOK_VENDOR
%token TOK_CLASS
%token TOK_SUBCLASS
%token TOK_PROTOCOL

%token TOK_DISK
%token TOK_PTPIP

%token TOK_DEVICE_TYPE
%token TOK_DRIVER_STATUS
%token TOK_OPERATIONS
%token TOK_FILE_OPERATIONS
%token TOK_FOLDER_OPERATIONS

%token TOK_DRIVER_OPTIONS
%token TOK_OPTION

%token <str_val> VAL_FLAG

 /* %type <ui_val> interface_usb_vendor */
 /* %type <ui_val> interface_usb_product */

%start devices

%%

devices:
	/* empty */
	| devices device
	;

device:
	TOK_DEVICE TOK_STRING TOK_BEGIN
	{
	  current_device_name = alloc_parse_string($<str_val>2);
	  /* printf("BEGIN_DEVICE <<%s>>\n", current_device_name); */
	  reset_ca();
	  strncpy(ca.model, current_device_name, sizeof(ca.model));
	  ca.model[sizeof(ca.model)-1]='\0';
	}
	device_type
	driver_definition
	driver_status
	operations
	file_operations
	folder_operations
	interfaces
	driver_options
	TOK_END
	{
	  //printf("END_DEVICE <<%s>>\n", current_device_name);
	  free(current_device_name);  current_device_name = NULL;
	  if (current_driver != NULL) {
	    free(current_driver);  current_driver = NULL;
	  }
	  gp_abilities_list_append(al, ca);
	}
	TOK_SEP
	;

driver_options:
	/* empty */
	| TOK_DRIVER_OPTIONS TOK_BEGIN 
	driver_option_list
	TOK_END TOK_SEP
	;

driver_option_list:
	/* empty */
	| driver_option_list driver_option_item
	;

driver_option_item:
	TOK_OPTION TOK_STRING TOK_SEP
	| TOK_OPTION TOK_STRING TOK_STRING TOK_SEP
	;

device_type:
	/* empty */
	| TOK_DEVICE_TYPE VAL_FLAG TOK_SEP
	{
	  if (gpi_string_to_enum($<str_val>2, &ca.device_type,
				 gpi_gphoto_device_type_map)) {
	    error_string = $<str_val>2;
	    yyerror(file_name, al,
		    "illegal device_type value");
	  }
	}
	;

driver_definition:
        TOK_DRIVER TOK_STRING TOK_SEP
	{
	  const char *camlib_env = getenv(CAMLIBDIR_ENV);
	  const char *camlibs = (camlib_env != NULL)?camlib_env:CAMLIBS;
	  current_driver = alloc_parse_string($<str_val>2);
	  strncpy(ca.library, camlibs, sizeof(ca.library));
	  strncat(ca.library, "/", sizeof(ca.library)-strlen(ca.library)-1);
	  strncat(ca.library, current_driver, sizeof(ca.library)-strlen(ca.library)-1);
	  ca.library[sizeof(ca.library)-1] = '\0';
	  //printf("   DRIVER <<%s>>\n", current_driver);
	}
	;

driver_status:
	/* empty */
        | TOK_DRIVER_STATUS VAL_FLAG TOK_SEP
	{
	  if (gpi_string_to_enum($<str_val>2, &ca.status,
				 gpi_camera_driver_status_map)) {
	    error_string = $<str_val>2;
	    yyerror(file_name, al,
		    "illegal driver_status value");
	  }
	}
	;

folder_operations:
	/* empty */
	| TOK_FOLDER_OPERATIONS folder_operations_list TOK_SEP
	;

file_operations:
	/* empty */
	| TOK_FILE_OPERATIONS file_operations_list TOK_SEP
	;

operations:
	/* empty */
	| TOK_OPERATIONS operations_list TOK_SEP
	;

folder_operations_list:
        folder_operations_list TOK_COMMA folder_operation
	| folder_operation
	;

file_operations_list:
        file_operations_list TOK_COMMA file_operation
	| file_operation
	;

operations_list:
        operations_list TOK_COMMA operation
	| operation
	;

folder_operation:
	VAL_FLAG
	{
	  if (gpi_string_or_to_flags($<str_val>1, &ca.folder_operations,
				     gpi_folder_operation_map)) {
	    error_string = $<str_val>1;
	    yyerror(file_name, al, 
		    "illegal folder_operation flag");
	  }
	}
	;

file_operation:
	VAL_FLAG
	{
	  if (gpi_string_or_to_flags($<str_val>1, &ca.file_operations,
				     gpi_file_operation_map)) {
	    error_string = $<str_val>1;
	    yyerror(file_name, al, 
		    "illegal file_operation flag");
	  }
	}
	;

operation:
	VAL_FLAG
	{
	  if (gpi_string_or_to_flags($<str_val>1,
				     &ca.operations,
				     gpi_camera_operation_map)) {
	    error_string = $<str_val>1;
	    yyerror(file_name, al,
		    "illegal operation flag");
	  }
	}
	;

interfaces:
	/* empty */
	| interfaces interface TOK_SEP
	;

interface:
	interface_serial
	| interface_usb
	| TOK_INTERFACE TOK_DISK {
	  ca.port |= GP_PORT_DISK;
	}
	| TOK_INTERFACE TOK_PTPIP {
	  ca.port |= GP_PORT_PTPIP;
	}
	;

interface_serial:
	TOK_INTERFACE TOK_SERIAL
	{
	  ca.port |= GP_PORT_SERIAL;
	  /* Ideally: Prepare serial entry. */
	}
	TOK_BEGIN
	interface_serial_internal
	TOK_END
        {
	  /* Ideally: Add serial entry to database. */
        }
	;

interface_serial_internal:
	/* empty */
	| TOK_SPEEDS speed_number_list TOK_SEP
	{
	  if (speed_index < (sizeof(ca.speed)/sizeof(ca.speed[0]))) {
	    ca.speed[speed_index++] = 0;
	  } else {
	    ca.speed[(sizeof(ca.speed)/sizeof(ca.speed[0]))-1] = 0;
	  }
	}
	;

speed_number_list:
	speed_number_list TOK_COMMA speed_list_number
	| speed_list_number
	;

speed_list_number:
	TOK_NUMBER
	{
	  if (speed_index < (sizeof(ca.speed)/sizeof(ca.speed[0]))) {
	    ca.speed[speed_index++] = $<ui_val>1;
	  }
	}
	;

/* FIXME: Make sure that the CameraAbilities usb_* fields are not
 *  overwritten by multiple definitions 
 */

interface_usb:
	TOK_INTERFACE TOK_USB
	TOK_BEGIN 
	{
	  ca.port |= GP_PORT_USB;
	  /* Ideally: Prepare USB entry for this camera */
	}
	interface_usb_internal
	TOK_END
	{
	  /* Ideally: Commit prepared USB entry to database */
	}
	;

interface_usb_internal:
	interface_usb_vendprod_optional
	interface_usb_class_optional
	;

interface_usb_vendprod_optional:
	/* empty */
	| interface_usb_vendor
	  interface_usb_product
	;

interface_usb_vendor:
	TOK_VENDOR TOK_NUMBER TOK_SEP
	{
	  ca.usb_vendor = $<ui_val>2;
	}
	;

interface_usb_product:
	TOK_PRODUCT TOK_NUMBER TOK_SEP
	{
	  ca.usb_product = $<ui_val>2;
	}
	;

interface_usb_class_optional:
	/* empty */
	| TOK_CLASS TOK_NUMBER TOK_SEP
	{
	  ca.usb_class = $<ui_val>2;
	}
	interface_usb_subclass_optional
	;

interface_usb_subclass_optional:
	/* empty */
	| TOK_SUBCLASS TOK_NUMBER TOK_SEP
	{
	  ca.usb_subclass = $<ui_val>2;
	}
	interface_usb_protocol_optional
	;

interface_usb_protocol_optional:
	/* empty */
	| TOK_PROTOCOL TOK_NUMBER TOK_SEP
	{
	  ca.usb_protocol = $<ui_val>2;
	}
	;

%%


char *alloc_parse_string(char *s)
  {
    if (s[0] == '"') {
      int len = strlen(s);
      if (s[len-1] == '"') {
	char *ret = malloc(len-2+1);
	strncpy(ret, &s[1], len-2);
	ret[len-2] = '\0';
	free(s);
	return ret;
      } else {
	fprintf(stderr, "Error: string does not end with quote: <<%s>>\n", s);
	exit(2);
      }
    } else {
      fprintf(stderr, "Error: string does not start with quote: <<%s>>\n", s);
      exit(2);
    }
  }


void yyerror(const char *filename, 
	     CameraAbilitiesList __unused__ *al, 
	     const char *str)
{
  fprintf(stderr, "%s:%d: parse error: %s\n", 
	  filename, yylineno, str);
  fprintf(stderr, "%s:%d- near: `%s'\n", 
	  filename, yylineno, yytext);
  if (NULL != error_string) {
    fprintf(stderr, "%s:%d-              <<%s>>\n",
	    filename, yylineno, error_string);
    error_string = NULL;
  }
  exit(4);
}


static void 
reset_ca(void)
{
  memset(&ca, 0, sizeof(ca));
  speed_index = 0;
  /* FIXME: These defaults are of dubious quality. */
  ca.usb_subclass = -1;
  ca.usb_protocol = -1;
}