Home | History | Annotate | Line # | Download | only in wsmoused
config_yacc.y revision 1.3
      1 /* $NetBSD: config_yacc.y,v 1.3 2003/08/06 22:11:49 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 M. Merino Vidal.
      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.3 2003/08/06 22:11:49 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_MODE TK_MODEPROP
     64 %type <string> TK_STRING TK_MODEPROP
     65 %type <prop> modeprop
     66 %type <block> main outermode mode
     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.  Contains properties */
    106 mode :
    107 	  modeprop		{ struct block *b = block_new(BLOCK_MODE);
    108 				  block_add_prop(b, $1);
    109 				  $$ = b; }
    110 	| mode modeprop		{ block_add_prop($1, $2); }
    111 	| error TK_EOL		{ yyerrok; }
    112 ;
    113 
    114 /* Matches a mode property and returns a prop defining it. */
    115 modeprop :
    116 	TK_MODEPROP TK_EQUAL TK_STRING TK_EOL
    117 				{ struct prop *p = prop_new();
    118 				  p->p_name = strdup($1);
    119 				  p->p_value = strdup($3);
    120 				  $$ = p; }
    121 ;
    122 
    123 %%
    124 
    125 int
    126 yyerror(const char *fmt, ...)
    127 {
    128 	va_list ap;
    129 
    130 	va_start(ap, fmt);
    131 	fprintf(stderr, "%s: ", getprogname());
    132 	vfprintf(stderr, fmt, ap);
    133 	fprintf(stderr, " in line %d\n", yyline);
    134 	va_end(ap);
    135 	return (0);
    136 }
    137 
    138 struct block *
    139 config_parse(FILE *f)
    140 {
    141 	Conf = NULL;
    142 	yyrestart(f);
    143 	yyline = 1;
    144 	yyparse();
    145 
    146 	return Conf;
    147 }
    148