Home | History | Annotate | Line # | Download | only in wsmoused
config_yacc.y revision 1.1
      1 /* $NetBSD: config_yacc.y,v 1.1 2003/03/04 14:33:55 jmmv Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julio Merino.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. The name authors may not be used to endorse or promote products
     16  *    derived from this software without specific prior written
     17  *    permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     25  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 %{
     33 
     34 #include <sys/cdefs.h>
     35 
     36 #ifndef lint
     37 __RCSID("$NetBSD: config_yacc.y,v 1.1 2003/03/04 14:33:55 jmmv Exp $");
     38 #endif /* not lint */
     39 
     40 #include <sys/time.h>
     41 #include <dev/wscons/wsconsio.h>
     42 #include <err.h>
     43 #include <errno.h>
     44 #include <stdio.h>
     45 #include <stdarg.h>
     46 #include <string.h>
     47 
     48 #include "wsmoused.h"
     49 
     50 int yylex(void);
     51 int yyparse(void);
     52 int yyerror(const char *, ...);
     53 void yyrestart(FILE *);
     54 struct block *config_parse(FILE *);
     55 
     56 int yyline;
     57 static struct block *Conf;
     58 
     59 %}
     60 
     61 %token TK_EOL
     62 %token TK_EQUAL TK_LBRACE TK_RBRACE
     63 %token TK_STRING TK_EVENT TK_EVENTPROP TK_MODE TK_MODEPROP
     64 %type <string> TK_STRING TK_EVENTPROP TK_MODEPROP
     65 %type <prop> eventprop modeprop
     66 %type <block> main outermode mode outerevent event
     67 %union {
     68 	char *string;
     69 	struct prop *prop;
     70 	struct block *block;
     71 }
     72 
     73 %%
     74 
     75 Config :
     76 	  main			{ Conf = $1;
     77 				  Conf->b_name = strdup("global"); }
     78 ;
     79 
     80 /* Matches the whole configuration file and constructs a block defining it.
     81    Can contain mode properties (common to all modes) and mode definitions. */
     82 main :
     83 	  modeprop		{ struct block *b = block_new(BLOCK_GLOBAL);
     84 				  block_add_prop(b, $1);
     85 				  $$ = b; }
     86 	| main modeprop		{ block_add_prop($1, $2); }
     87 	| outermode		{ struct block *b = block_new(BLOCK_GLOBAL);
     88 				  block_add_child(b, $1);
     89 				  $$ = b; }
     90 	| main outermode	{ block_add_child($1, $2); }
     91 	| error TK_EOL		{ yyerrok; }
     92 ;
     93 
     94 /* Defines the aspect of a mode definition. Returns the block given by the
     95    mode definition itself. */
     96 outermode :
     97 	  TK_MODE TK_STRING TK_LBRACE mode TK_RBRACE
     98 				{ $4->b_name = strdup($2);
     99 				  $$ = $4; }
    100 	| TK_MODE TK_STRING TK_LBRACE TK_RBRACE
    101 				{ $$ = block_new(BLOCK_MODE);
    102 				  $$->b_name = strdup($2); }
    103 ;
    104 
    105 /* Matches a mode and returns a block defining it. Can contain properties
    106    and event definitions. */
    107 mode :
    108 	  modeprop		{ struct block *b = block_new(BLOCK_MODE);
    109 				  block_add_prop(b, $1);
    110 				  $$ = b; }
    111 	| mode modeprop		{ block_add_prop($1, $2); }
    112 	| outerevent		{ struct block *b = block_new(BLOCK_MODE);
    113 				  block_add_child(b, $1);
    114 				  $$ = b; }
    115 	| mode outerevent	{ block_add_child($1, $2); }
    116 	| error TK_EOL		{ yyerrok; }
    117 ;
    118 
    119 /* Defines the aspect of an event definition. Returns the block given by the
    120    event definition itself. */
    121 outerevent :
    122 	  TK_EVENT TK_LBRACE event TK_RBRACE
    123 				{ $$ = $3; }
    124 	  TK_EVENT TK_LBRACE TK_RBRACE
    125 				{ $$ = block_new(BLOCK_EVENT); }
    126 ;
    127 
    128 /* Matches an event and returns a block defining it. Contains properties. */
    129 event :
    130 	  eventprop		{ struct block *b = block_new(BLOCK_EVENT);
    131 				  block_add_prop(b, $1);
    132 				  $$ = b; }
    133 	| event eventprop	{ block_add_prop($1, $2); }
    134 	| error TK_EOL		{ yyerrok; }
    135 	| error			{ yyerrok; }
    136 ;
    137 
    138 /* Matches a mode property and returns a prop defining it. */
    139 modeprop :
    140 	TK_MODEPROP TK_EQUAL TK_STRING TK_EOL
    141 				{ struct prop *p = prop_new();
    142 				  p->p_name = strdup($1);
    143 				  p->p_value = strdup($3);
    144 				  $$ = p; }
    145 ;
    146 
    147 /* Matches an event property and returns a prop defining it. */
    148 eventprop :
    149 	TK_EVENTPROP TK_EQUAL TK_STRING TK_EOL
    150 				{ struct prop *p = prop_new();
    151 				  p->p_name = strdup($1);
    152 				  p->p_value = strdup($3);
    153 				  $$ = p; }
    154 ;
    155 
    156 %%
    157 
    158 int
    159 yyerror(const char *fmt, ...)
    160 {
    161 	va_list ap;
    162 
    163 	va_start(ap, fmt);
    164 	fprintf(stderr, "%s: ", getprogname());
    165 	vfprintf(stderr, fmt, ap);
    166 	fprintf(stderr, " in line %d\n", yyline);
    167 	va_end(ap);
    168 	return (0);
    169 }
    170 
    171 struct block *
    172 config_parse(FILE *f)
    173 {
    174 	Conf = NULL;
    175 	yyrestart(f);
    176 	yyline = 1;
    177 	yyparse();
    178 
    179 	return Conf;
    180 }
    181