xref: /freebsd/usr.sbin/apmd/apmdlex.l (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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 <string.h>
37 #include <syslog.h>
38 #include <bitstring.h>
39 #include "apmd.h"
40 #include "y.tab.h"
41 
42 int lineno;
43 int first_time;
44 %}
45 
46 /* We don't need it, avoid the warning. */
47 %option noyywrap
48 %option nounput
49 %option noinput
50 
51 %s TOP
52 
53 %%
54 
55 %{
56 	if (first_time) {
57 		BEGIN TOP;
58 		lineno = 1;
59 		first_time = 0;
60 	}
61 %}
62 
63 <TOP>[ \t]+		;
64 <TOP>\n			lineno++;
65 <TOP>,			{ return COMMA; }
66 <TOP>;			{ return SEMICOLON; }
67 <TOP>#.*$		;
68 
69 <TOP>apm_event		{ return APMEVENT; }
70 
71 <TOP>NOEVENT		{ yylval.ev = EVENT_NOEVENT; return EVENT; }
72 <TOP>STANDBYREQ		{ yylval.ev = EVENT_STANDBYREQ; return EVENT; }
73 <TOP>SUSPENDREQ		{ yylval.ev = EVENT_SUSPENDREQ; return EVENT; }
74 <TOP>NORMRESUME		{ yylval.ev = EVENT_NORMRESUME; return EVENT; }
75 <TOP>CRITRESUME		{ yylval.ev = EVENT_CRITRESUME; return EVENT; }
76 <TOP>BATTERYLOW		{ yylval.ev = EVENT_BATTERYLOW; return EVENT; }
77 <TOP>POWERSTATECHANGE	{ yylval.ev = EVENT_POWERSTATECHANGE; return EVENT; }
78 <TOP>UPDATETIME		{ yylval.ev = EVENT_UPDATETIME; return EVENT; }
79 <TOP>CRITSUSPEND	{ yylval.ev = EVENT_CRITSUSPEND; return EVENT; }
80 <TOP>USERSTANDBYREQ	{ yylval.ev = EVENT_USERSTANDBYREQ; return EVENT; }
81 <TOP>USERSUSPENDREQ	{ yylval.ev = EVENT_USERSUSPENDREQ; return EVENT; }
82 <TOP>STANDBYRESUME	{ yylval.ev = EVENT_STANDBYRESUME; return EVENT; }
83 <TOP>CAPABILITIESCHANGE	{ yylval.ev = EVENT_CAPABILITIESCHANGE; return EVENT; }
84 
85 <TOP>apm_battery	{ return APMBATT; }
86 
87 <TOP>charging		{ return BATTCHARGE; }
88 <TOP>discharging	{ return BATTDISCHARGE; }
89 <TOP>[0-9]+%		{
90 				yylval.i = atoi(yytext);
91 				return BATTPERCENT;
92 			}
93 <TOP>[0-9]+[Mm]		{
94 				yylval.i = -atoi(yytext);
95 				return BATTTIME;
96 			}
97 
98 <TOP>exec		{ return EXECCMD; }
99 <TOP>reject		{ return REJECTCMD; }
100 
101 <TOP>\{			{ return BEGINBLOCK; }
102 <TOP>\}			{ return ENDBLOCK; }
103 <TOP>\"[^"]+\"	{
104 				int len = strlen(yytext) - 2;
105 				if ((yylval.str = (char *) malloc(len + 1)) == NULL)
106 					goto out;
107 				memcpy(yylval.str, yytext + 1, len);
108 				yylval.str[len] = '\0';
109 			out:
110 				return STRING;
111 			}
112 
113 <TOP>[^"{},;#\n\t ]+	{ yylval.str = strdup(yytext); return UNKNOWN; }
114 %%
115 
116 void
117 yyerror(const char *s)
118 {
119 	syslog(LOG_ERR, "line %d: %s%s %s.\n", lineno, yytext, yytext?":":"", s);
120 }
121