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