xref: /netbsd-src/usr.sbin/wsmoused/config_yacc.y (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: config_yacc.y,v 1.4 2008/07/17 16:24:56 drochner 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.4 2008/07/17 16:24:56 drochner 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 #include <stdlib.h>
48 
49 #include "wsmoused.h"
50 
51 int yylex(void);
52 int yyparse(void);
53 int yyerror(const char *, ...);
54 void yyrestart(FILE *);
55 struct block *config_parse(FILE *);
56 
57 int yyline;
58 static struct block *Conf;
59 
60 %}
61 
62 %token TK_EOL
63 %token TK_EQUAL TK_LBRACE TK_RBRACE
64 %token TK_STRING TK_MODE TK_MODEPROP
65 %type <string> TK_STRING TK_MODEPROP
66 %type <prop> modeprop
67 %type <block> main outermode mode
68 %union {
69 	char *string;
70 	struct prop *prop;
71 	struct block *block;
72 }
73 
74 %%
75 
76 Config :
77 	  main			{ Conf = $1;
78 				  Conf->b_name = strdup("global"); }
79 ;
80 
81 /* Matches the whole configuration file and constructs a block defining it.
82    Can contain mode properties (common to all modes) and mode definitions. */
83 main :
84 	  modeprop		{ struct block *b = block_new(BLOCK_GLOBAL);
85 				  block_add_prop(b, $1);
86 				  $$ = b; }
87 	| main modeprop		{ block_add_prop($1, $2); }
88 	| outermode		{ struct block *b = block_new(BLOCK_GLOBAL);
89 				  block_add_child(b, $1);
90 				  $$ = b; }
91 	| main outermode	{ block_add_child($1, $2); }
92 	| error TK_EOL		{ yyerrok; }
93 ;
94 
95 /* Defines the aspect of a mode definition.  Returns the block given by the
96    mode definition itself. */
97 outermode :
98 	  TK_MODE TK_STRING TK_LBRACE mode TK_RBRACE
99 				{ $4->b_name = strdup($2);
100 				  $$ = $4; }
101 	| TK_MODE TK_STRING TK_LBRACE TK_RBRACE
102 				{ $$ = block_new(BLOCK_MODE);
103 				  $$->b_name = strdup($2); }
104 ;
105 
106 /* Matches a mode and returns a block defining it.  Contains properties */
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 	| error TK_EOL		{ yyerrok; }
113 ;
114 
115 /* Matches a mode property and returns a prop defining it. */
116 modeprop :
117 	TK_MODEPROP TK_EQUAL TK_STRING TK_EOL
118 				{ struct prop *p = prop_new();
119 				  p->p_name = strdup($1);
120 				  p->p_value = strdup($3);
121 				  $$ = p; }
122 ;
123 
124 %%
125 
126 int
127 yyerror(const char *fmt, ...)
128 {
129 	va_list ap;
130 
131 	va_start(ap, fmt);
132 	fprintf(stderr, "%s: ", getprogname());
133 	vfprintf(stderr, fmt, ap);
134 	fprintf(stderr, " in line %d\n", yyline);
135 	va_end(ap);
136 	return (0);
137 }
138 
139 struct block *
140 config_parse(FILE *f)
141 {
142 	Conf = NULL;
143 	yyrestart(f);
144 	yyline = 1;
145 	yyparse();
146 
147 	return Conf;
148 }
149