xref: /freebsd/usr.sbin/apmd/apmdparse.y (revision 4f52dfbb)
1 %{
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
33  */
34 
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <bitstring.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "apmd.h"
41 
42 #ifdef DEBUG
43 #define YYDEBUG 1
44 #endif
45 
46 extern int first_time;
47 
48 %}
49 
50 %union {
51 	char *str;
52 	bitstr_t bit_decl(evlist, EVENT_MAX);
53 	int ev;
54 	struct event_cmd * evcmd;
55 	int i;
56 }
57 
58 %token BEGINBLOCK ENDBLOCK
59 %token COMMA SEMICOLON
60 %token APMEVENT
61 %token APMBATT
62 %token BATTCHARGE BATTDISCHARGE
63 %token <i> BATTTIME BATTPERCENT
64 %token EXECCMD REJECTCMD
65 %token <ev> EVENT
66 %token <str> STRING UNKNOWN
67 
68 %type <i> apm_battery_level
69 %type <i> apm_battery_direction
70 %type <str> string
71 %type <str> unknown
72 %type <evlist> event_list
73 %type <evcmd> cmd_list
74 %type <evcmd> cmd
75 %type <evcmd> exec_cmd reject_cmd
76 
77 %%
78 
79 config_file
80 	: { first_time = 1; } config_list
81 	;
82 
83 config_list
84 	: config
85 	| config_list config
86 	;
87 
88 config
89 	: apm_event_statement
90 	| apm_battery_statement
91 	;
92 
93 apm_event_statement
94 	: APMEVENT event_list BEGINBLOCK cmd_list ENDBLOCK
95 		{
96 			if (register_apm_event_handlers($2, $4) < 0)
97 				abort(); /* XXX */
98 			free_event_cmd_list($4);
99 		}
100 	;
101 
102 apm_battery_level
103 	: BATTPERCENT
104 		{
105 			$$ = $1;
106 		}
107 	| BATTTIME
108 		{
109 			$$ = $1;
110 		}
111 	;
112 
113 apm_battery_direction
114 	: BATTCHARGE
115 		{
116 			$$ = 1;
117 		}
118 	| BATTDISCHARGE
119 		{
120 			$$ = -1;
121 		}
122 	;
123 apm_battery_statement
124 	: APMBATT apm_battery_level apm_battery_direction
125 		BEGINBLOCK cmd_list ENDBLOCK
126 		{
127 			if (register_battery_handlers($2, $3, $5) < 0)
128 				abort(); /* XXX */
129 			free_event_cmd_list($5);
130 		}
131 	;
132 
133 event_list
134 	: EVENT
135 		{
136 			bit_nclear($$, 0, EVENT_MAX - 1);
137 			bit_set($$, $1);
138 		}
139 	| event_list COMMA EVENT
140 		{
141 			memcpy(&($$), &($1), bitstr_size(EVENT_MAX));
142 			bit_set($$, $3);
143 		}
144 	;
145 
146 cmd_list
147 	: /* empty */
148 		{
149 			$$  = NULL;
150 		}
151 	| cmd_list cmd
152 		{
153 			struct event_cmd * p = $1;
154 			if (p) {
155 				while (p->next != NULL)
156 					p = p->next;
157 				p->next = $2;
158 				$$ = $1;
159 			} else {
160 				$$ = $2;
161 			}
162 		}
163 	;
164 
165 cmd
166 	: exec_cmd SEMICOLON	{ $$ = $1; }
167 	| reject_cmd SEMICOLON	{ $$ = $1; }
168 	;
169 
170 exec_cmd
171 	: EXECCMD string
172 		{
173 			size_t len = sizeof (struct event_cmd_exec);
174 			struct event_cmd_exec *cmd = malloc(len);
175 			cmd->evcmd.next = NULL;
176 			cmd->evcmd.len = len;
177 			cmd->evcmd.name = "exec";
178 			cmd->evcmd.op = &event_cmd_exec_ops;
179 			cmd->line = $2;
180 			$$ = (struct event_cmd *) cmd;
181 		}
182 	;
183 
184 reject_cmd
185 	: REJECTCMD
186 		{
187 			size_t len = sizeof (struct event_cmd_reject);
188 			struct event_cmd_reject *cmd = malloc(len);
189 			cmd->evcmd.next = NULL;
190 			cmd->evcmd.len = len;
191 			cmd->evcmd.name = "reject";
192 			cmd->evcmd.op = &event_cmd_reject_ops;
193 			$$ = (struct event_cmd *) cmd;
194 		}
195 	;
196 
197 string
198 	: STRING	{ $$ = $1; }
199 	;
200 
201 unknown
202 	: UNKNOWN
203 		{
204 			$$ = $1;
205 		}
206 	;
207 %%
208 
209