xref: /freebsd/sbin/devd/token.l (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1e530e044SWarner Losh %{
2e530e044SWarner Losh /*-
3e530e044SWarner Losh  * DEVD (Device action daemon)
4e530e044SWarner Losh  *
5*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
61de7b4b8SPedro F. Giffuni  *
7f86e6000SWarner Losh  * Copyright (c) 2002 M. Warner Losh <imp@FreeBSD.org>
8e530e044SWarner Losh  *
9e530e044SWarner Losh  * Redistribution and use in source and binary forms, with or without
10e530e044SWarner Losh  * modification, are permitted provided that the following conditions
11e530e044SWarner Losh  * are met:
12e530e044SWarner Losh  * 1. Redistributions of source code must retain the above copyright
13e530e044SWarner Losh  *    notice, this list of conditions and the following disclaimer.
14e530e044SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
15e530e044SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
16e530e044SWarner Losh  *    documentation and/or other materials provided with the distribution.
17e530e044SWarner Losh  *
18e530e044SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19e530e044SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20e530e044SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21e530e044SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22e530e044SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23e530e044SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24e530e044SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25e530e044SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26e530e044SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27e530e044SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28e530e044SWarner Losh  * SUCH DAMAGE.
29e530e044SWarner Losh  */
30e530e044SWarner Losh 
313054f218SWarner Losh #include <ctype.h>
32e530e044SWarner Losh #include <stdlib.h>
33e530e044SWarner Losh #include <string.h>
34e530e044SWarner Losh #include <syslog.h>
35e530e044SWarner Losh #include "devd.h"
36e530e044SWarner Losh #include "y.tab.h"
37e530e044SWarner Losh 
38e530e044SWarner Losh int lineno = 1;
39e530e044SWarner Losh 
403054f218SWarner Losh static void
update_lineno(const char * cp)413054f218SWarner Losh update_lineno(const char *cp)
423054f218SWarner Losh {
433054f218SWarner Losh 	while (*cp)
443054f218SWarner Losh 		if (*cp++ == '\n')
453054f218SWarner Losh 			lineno++;
463054f218SWarner Losh }
473054f218SWarner Losh 
48e530e044SWarner Losh %}
49e530e044SWarner Losh 
50e6f059a9SBaptiste Daroussin %option noyywrap
51a7880d59SJung-uk Kim %option nounput
52a7880d59SJung-uk Kim %option noinput
53a7880d59SJung-uk Kim 
54e530e044SWarner Losh %%
55e530e044SWarner Losh 
56e530e044SWarner Losh [ \t]+			;
57e530e044SWarner Losh \n			lineno++;
58e530e044SWarner Losh ;			{ return SEMICOLON; }
593054f218SWarner Losh #.*$			;
60e530e044SWarner Losh \/\/.*$			;
61fc3a3ee7SWarner Losh \/\*([^*]|(\*+([^*\/])))*\*+\/ { update_lineno(yytext); }
62e530e044SWarner Losh \{			{ return BEGINBLOCK; }
63e530e044SWarner Losh \}			{ return ENDBLOCK; }
64e530e044SWarner Losh [0-9]+			{ yylval.i = atoi(yytext); return NUMBER; }
65e530e044SWarner Losh \"[^"]+\"		{
66e530e044SWarner Losh 				int len = strlen(yytext) - 2;
673054f218SWarner Losh 				char *walker;
683054f218SWarner Losh 				int i;
69d6aed19dSWarner Losh 				update_lineno(yytext);
70e530e044SWarner Losh 				if ((yylval.str = (char *) malloc(len + 1)) == NULL)
71e530e044SWarner Losh 					goto out;
723054f218SWarner Losh 				walker = yylval.str;
733054f218SWarner Losh 				for (i = 1; i <= len; i++) {
743054f218SWarner Losh 					if (yytext[i] == '\\' &&
753054f218SWarner Losh 					    yytext[i + 1] == '\n') {
763054f218SWarner Losh 						i += 2;
773054f218SWarner Losh 						while(isspace(yytext[i]))
783054f218SWarner Losh 							i++;
793054f218SWarner Losh 					}
803054f218SWarner Losh 					*walker++ = yytext[i];
813054f218SWarner Losh 				}
823054f218SWarner Losh 				*walker++ = '\0';
83e530e044SWarner Losh 			out:;
84e530e044SWarner Losh 				return STRING;
85e530e044SWarner Losh 			}
86e530e044SWarner Losh 
87e530e044SWarner Losh 
88e530e044SWarner Losh options			{ return OPTIONS; }
89e530e044SWarner Losh set			{ return SET; }
90e530e044SWarner Losh directory		{ return DIRECTORY; }
91e530e044SWarner Losh pid-file		{ return PID_FILE; }
92e530e044SWarner Losh attach			{ return ATTACH; }
93e530e044SWarner Losh detach			{ return DETACH; }
94e530e044SWarner Losh device-name		{ return DEVICE_NAME; }
95cd70782bSWarner Losh media-type		{ return MEDIA_TYPE; }
96cd70782bSWarner Losh class			{ return CLASS; }
97cd70782bSWarner Losh subdevice		{ return SUBDEVICE; }
98e530e044SWarner Losh action			{ return ACTION; }
99e530e044SWarner Losh match			{ return MATCH; }
100e530e044SWarner Losh nomatch			{ return NOMATCH; }
101842ccec5SWarner Losh notify			{ return NOTIFY; }
1025102ef84SWarner Losh [A-Za-z][A-Za-z0-9_-]*	{
1033054f218SWarner Losh 				yylval.str = strdup(yytext);
104e530e044SWarner Losh 				return ID;
105e530e044SWarner Losh 			}
106e530e044SWarner Losh %%
107e530e044SWarner Losh 
108e530e044SWarner Losh void
109e530e044SWarner Losh yyerror(const char *s)
110e530e044SWarner Losh {
111e530e044SWarner Losh 	syslog(LOG_ERR, "line %d: %s%s %s.\n", lineno, yytext, yytext?":":"", s);
112e530e044SWarner Losh }
113