xref: /freebsd/usr.sbin/apmd/apmdparse.y (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1 %{
2 /*-
3  * APM (Advanced Power Management) Event Dispatcher
4  *
5  * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6  * Copyright (c) 1999 KOIE Hidetaka <koie@suri.co.jp>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <stdio.h>
34 #include <bitstring.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "apmd.h"
38 
39 #ifdef DEBUG
40 #define YYDEBUG 1
41 #endif
42 
43 extern int first_time;
44 
45 %}
46 
47 %union {
48 	char *str;
49 	bitstr_t bit_decl(evlist, EVENT_MAX);
50 	int ev;
51 	struct event_cmd * evcmd;
52 	int i;
53 }
54 
55 %token BEGINBLOCK ENDBLOCK
56 %token COMMA SEMICOLON
57 %token APMEVENT
58 %token APMBATT
59 %token BATTCHARGE BATTDISCHARGE
60 %token <i> BATTTIME BATTPERCENT
61 %token EXECCMD REJECTCMD
62 %token <ev> EVENT
63 %token <str> STRING UNKNOWN
64 
65 %type <i> apm_battery_level
66 %type <i> apm_battery_direction
67 %type <str> string
68 %type <str> unknown
69 %type <evlist> event_list
70 %type <evcmd> cmd_list
71 %type <evcmd> cmd
72 %type <evcmd> exec_cmd reject_cmd
73 
74 %%
75 
76 config_file
77 	: { first_time = 1; } config_list
78 	;
79 
80 config_list
81 	: config
82 	| config_list config
83 	;
84 
85 config
86 	: apm_event_statement
87 	| apm_battery_statement
88 	;
89 
90 apm_event_statement
91 	: APMEVENT event_list BEGINBLOCK cmd_list ENDBLOCK
92 		{
93 			if (register_apm_event_handlers($2, $4) < 0)
94 				abort(); /* XXX */
95 			free_event_cmd_list($4);
96 		}
97 	;
98 
99 apm_battery_level
100 	: BATTPERCENT
101 		{
102 			$$ = $1;
103 		}
104 	| BATTTIME
105 		{
106 			$$ = $1;
107 		}
108 	;
109 
110 apm_battery_direction
111 	: BATTCHARGE
112 		{
113 			$$ = 1;
114 		}
115 	| BATTDISCHARGE
116 		{
117 			$$ = -1;
118 		}
119 	;
120 apm_battery_statement
121 	: APMBATT apm_battery_level apm_battery_direction
122 		BEGINBLOCK cmd_list ENDBLOCK
123 		{
124 			if (register_battery_handlers($2, $3, $5) < 0)
125 				abort(); /* XXX */
126 			free_event_cmd_list($5);
127 		}
128 	;
129 
130 event_list
131 	: EVENT
132 		{
133 			bit_nclear($$, 0, EVENT_MAX - 1);
134 			bit_set($$, $1);
135 		}
136 	| event_list COMMA EVENT
137 		{
138 			memcpy(&($$), &($1), bitstr_size(EVENT_MAX));
139 			bit_set($$, $3);
140 		}
141 	;
142 
143 cmd_list
144 	: /* empty */
145 		{
146 			$$  = NULL;
147 		}
148 	| cmd_list cmd
149 		{
150 			struct event_cmd * p = $1;
151 			if (p) {
152 				while (p->next != NULL)
153 					p = p->next;
154 				p->next = $2;
155 				$$ = $1;
156 			} else {
157 				$$ = $2;
158 			}
159 		}
160 	;
161 
162 cmd
163 	: exec_cmd SEMICOLON	{ $$ = $1; }
164 	| reject_cmd SEMICOLON	{ $$ = $1; }
165 	;
166 
167 exec_cmd
168 	: EXECCMD string
169 		{
170 			size_t len = sizeof (struct event_cmd_exec);
171 			struct event_cmd_exec *cmd = malloc(len);
172 			cmd->evcmd.next = NULL;
173 			cmd->evcmd.len = len;
174 			cmd->evcmd.name = "exec";
175 			cmd->evcmd.op = &event_cmd_exec_ops;
176 			cmd->line = $2;
177 			$$ = (struct event_cmd *) cmd;
178 		}
179 	;
180 
181 reject_cmd
182 	: REJECTCMD
183 		{
184 			size_t len = sizeof (struct event_cmd_reject);
185 			struct event_cmd_reject *cmd = malloc(len);
186 			cmd->evcmd.next = NULL;
187 			cmd->evcmd.len = len;
188 			cmd->evcmd.name = "reject";
189 			cmd->evcmd.op = &event_cmd_reject_ops;
190 			$$ = (struct event_cmd *) cmd;
191 		}
192 	;
193 
194 string
195 	: STRING	{ $$ = $1; }
196 	;
197 
198 unknown
199 	: UNKNOWN
200 		{
201 			$$ = $1;
202 		}
203 	;
204 %%
205 
206