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