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