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