xref: /freebsd/usr.sbin/apmd/apmdlex.l (revision a7880d59c9588622a96ec154fd3fbb6c037bb83a)
1d50a71bdSMitsuru IWASAKI %{
2d50a71bdSMitsuru IWASAKI /*-
3d50a71bdSMitsuru IWASAKI  * APM (Advanced Power Management) Event Dispatcher
4d50a71bdSMitsuru IWASAKI  *
5d50a71bdSMitsuru IWASAKI  * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6d50a71bdSMitsuru IWASAKI  * Copyright (c) 1999 KOIE Hidetaka <koie@suri.co.jp>
7d50a71bdSMitsuru IWASAKI  * All rights reserved.
8d50a71bdSMitsuru IWASAKI  *
9d50a71bdSMitsuru IWASAKI  * Redistribution and use in source and binary forms, with or without
10d50a71bdSMitsuru IWASAKI  * modification, are permitted provided that the following conditions
11d50a71bdSMitsuru IWASAKI  * are met:
12d50a71bdSMitsuru IWASAKI  * 1. Redistributions of source code must retain the above copyright
13d50a71bdSMitsuru IWASAKI  *    notice, this list of conditions and the following disclaimer.
14d50a71bdSMitsuru IWASAKI  * 2. Redistributions in binary form must reproduce the above copyright
15d50a71bdSMitsuru IWASAKI  *    notice, this list of conditions and the following disclaimer in the
16d50a71bdSMitsuru IWASAKI  *    documentation and/or other materials provided with the distribution.
17d50a71bdSMitsuru IWASAKI  *
18d50a71bdSMitsuru IWASAKI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19d50a71bdSMitsuru IWASAKI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20d50a71bdSMitsuru IWASAKI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21d50a71bdSMitsuru IWASAKI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22d50a71bdSMitsuru IWASAKI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23d50a71bdSMitsuru IWASAKI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24d50a71bdSMitsuru IWASAKI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25d50a71bdSMitsuru IWASAKI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26d50a71bdSMitsuru IWASAKI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27d50a71bdSMitsuru IWASAKI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28d50a71bdSMitsuru IWASAKI  * SUCH DAMAGE.
29d50a71bdSMitsuru IWASAKI  *
3097d92980SPeter Wemm  * $FreeBSD$
31d50a71bdSMitsuru IWASAKI  */
32d50a71bdSMitsuru IWASAKI 
33d50a71bdSMitsuru IWASAKI #include <string.h>
34d50a71bdSMitsuru IWASAKI #include <syslog.h>
35d50a71bdSMitsuru IWASAKI #include <bitstring.h>
36d50a71bdSMitsuru IWASAKI #include "apmd.h"
37d50a71bdSMitsuru IWASAKI #include "y.tab.h"
38d50a71bdSMitsuru IWASAKI 
39d50a71bdSMitsuru IWASAKI int lineno;
40d50a71bdSMitsuru IWASAKI int first_time;
41d50a71bdSMitsuru IWASAKI %}
42d50a71bdSMitsuru IWASAKI 
43*a7880d59SJung-uk Kim /* We don't need it, avoid the warning. */
44*a7880d59SJung-uk Kim %option nounput
45*a7880d59SJung-uk Kim %option noinput
46*a7880d59SJung-uk Kim 
47d50a71bdSMitsuru IWASAKI %s TOP
48d50a71bdSMitsuru IWASAKI 
49d50a71bdSMitsuru IWASAKI %%
50d50a71bdSMitsuru IWASAKI 
51d50a71bdSMitsuru IWASAKI %{
52d50a71bdSMitsuru IWASAKI 	if (first_time) {
53d50a71bdSMitsuru IWASAKI 		BEGIN TOP;
54d50a71bdSMitsuru IWASAKI 		lineno = 1;
55d50a71bdSMitsuru IWASAKI 		first_time = 0;
56d50a71bdSMitsuru IWASAKI 	}
57d50a71bdSMitsuru IWASAKI %}
58d50a71bdSMitsuru IWASAKI 
59d50a71bdSMitsuru IWASAKI <TOP>[ \t]+		;
60d50a71bdSMitsuru IWASAKI <TOP>\n			lineno++;
61d50a71bdSMitsuru IWASAKI <TOP>,			{ return COMMA; }
62d50a71bdSMitsuru IWASAKI <TOP>;			{ return SEMICOLON; }
63d50a71bdSMitsuru IWASAKI <TOP>#.*$		;
64d50a71bdSMitsuru IWASAKI 
65d50a71bdSMitsuru IWASAKI <TOP>apm_event		{ return APMEVENT; }
66d50a71bdSMitsuru IWASAKI 
67d50a71bdSMitsuru IWASAKI <TOP>NOEVENT		{ yylval.ev = EVENT_NOEVENT; return EVENT; }
68d50a71bdSMitsuru IWASAKI <TOP>STANDBYREQ		{ yylval.ev = EVENT_STANDBYREQ; return EVENT; }
69d50a71bdSMitsuru IWASAKI <TOP>SUSPENDREQ		{ yylval.ev = EVENT_SUSPENDREQ; return EVENT; }
70d50a71bdSMitsuru IWASAKI <TOP>NORMRESUME		{ yylval.ev = EVENT_NORMRESUME; return EVENT; }
71d50a71bdSMitsuru IWASAKI <TOP>CRITRESUME		{ yylval.ev = EVENT_CRITRESUME; return EVENT; }
72d50a71bdSMitsuru IWASAKI <TOP>BATTERYLOW		{ yylval.ev = EVENT_BATTERYLOW; return EVENT; }
73d50a71bdSMitsuru IWASAKI <TOP>POWERSTATECHANGE	{ yylval.ev = EVENT_POWERSTATECHANGE; return EVENT; }
74d50a71bdSMitsuru IWASAKI <TOP>UPDATETIME		{ yylval.ev = EVENT_UPDATETIME; return EVENT; }
75d50a71bdSMitsuru IWASAKI <TOP>CRITSUSPEND	{ yylval.ev = EVENT_CRITSUSPEND; return EVENT; }
76d50a71bdSMitsuru IWASAKI <TOP>USERSTANDBYREQ	{ yylval.ev = EVENT_USERSTANDBYREQ; return EVENT; }
77d50a71bdSMitsuru IWASAKI <TOP>USERSUSPENDREQ	{ yylval.ev = EVENT_USERSUSPENDREQ; return EVENT; }
78d50a71bdSMitsuru IWASAKI <TOP>STANDBYRESUME	{ yylval.ev = EVENT_STANDBYRESUME; return EVENT; }
79d50a71bdSMitsuru IWASAKI <TOP>CAPABILITIESCHANGE	{ yylval.ev = EVENT_CAPABILITIESCHANGE; return EVENT; }
80d50a71bdSMitsuru IWASAKI 
81719b9dc1SNick Sayer <TOP>apm_battery	{ return APMBATT; }
82719b9dc1SNick Sayer 
83719b9dc1SNick Sayer <TOP>charging		{ return BATTCHARGE; }
84719b9dc1SNick Sayer <TOP>discharging	{ return BATTDISCHARGE; }
85719b9dc1SNick Sayer <TOP>[0-9]+%		{
86719b9dc1SNick Sayer 				yylval.i = atoi(yytext);
87719b9dc1SNick Sayer 				return BATTPERCENT;
88719b9dc1SNick Sayer 			}
89719b9dc1SNick Sayer <TOP>[0-9]+[Mm]		{
90719b9dc1SNick Sayer 				yylval.i = -atoi(yytext);
91719b9dc1SNick Sayer 				return BATTTIME;
92719b9dc1SNick Sayer 			}
93719b9dc1SNick Sayer 
94d50a71bdSMitsuru IWASAKI <TOP>exec		{ return EXECCMD; }
95d50a71bdSMitsuru IWASAKI <TOP>reject		{ return REJECTCMD; }
96d50a71bdSMitsuru IWASAKI 
97d50a71bdSMitsuru IWASAKI <TOP>\{			{ return BEGINBLOCK; }
98d50a71bdSMitsuru IWASAKI <TOP>\}			{ return ENDBLOCK; }
99d50a71bdSMitsuru IWASAKI <TOP>\"[^"]+\"	{
100d50a71bdSMitsuru IWASAKI 				int len = strlen(yytext) - 2;
101d50a71bdSMitsuru IWASAKI 				if ((yylval.str = (char *) malloc(len + 1)) == NULL)
102d50a71bdSMitsuru IWASAKI 					goto out;
103d50a71bdSMitsuru IWASAKI 				memcpy(yylval.str, yytext + 1, len);
104d50a71bdSMitsuru IWASAKI 				yylval.str[len] = '\0';
105d50a71bdSMitsuru IWASAKI 			out:
106d50a71bdSMitsuru IWASAKI 				return STRING;
107d50a71bdSMitsuru IWASAKI 			}
108d50a71bdSMitsuru IWASAKI 
109d50a71bdSMitsuru IWASAKI <TOP>[^"{},;#\n\t ]+	{ yylval.str = strdup(yytext); return UNKNOWN; }
110d50a71bdSMitsuru IWASAKI %%
111d50a71bdSMitsuru IWASAKI 
112d50a71bdSMitsuru IWASAKI void
113d50a71bdSMitsuru IWASAKI yyerror(const char *s)
114d50a71bdSMitsuru IWASAKI {
115d50a71bdSMitsuru IWASAKI 	syslog(LOG_ERR, "line %d: %s%s %s.\n", lineno, yytext, yytext?":":"", s);
116d50a71bdSMitsuru IWASAKI }
117