token.l revision 147874
1105573Simp%{
2105573Simp/*-
3105573Simp * DEVD (Device action daemon)
4105573Simp *
5105573Simp * Copyright (c) 2002 M. Warner Losh <imp@freebsd.org>.
6105573Simp * All rights reserved.
7105573Simp *
8105573Simp * Redistribution and use in source and binary forms, with or without
9105573Simp * modification, are permitted provided that the following conditions
10105573Simp * are met:
11105573Simp * 1. Redistributions of source code must retain the above copyright
12105573Simp *    notice, this list of conditions and the following disclaimer.
13105573Simp * 2. Redistributions in binary form must reproduce the above copyright
14105573Simp *    notice, this list of conditions and the following disclaimer in the
15105573Simp *    documentation and/or other materials provided with the distribution.
16105573Simp *
17105573Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18105573Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19105573Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20105573Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21105573Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22105573Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23105573Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24105573Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25105573Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26105573Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27105573Simp * SUCH DAMAGE.
28105573Simp *
29105573Simp * $FreeBSD: head/sbin/devd/token.l 147874 2005-07-10 03:37:15Z imp $
30105573Simp */
31105573Simp
32107665Simp#include <ctype.h>
33105573Simp#include <stdlib.h>
34105573Simp#include <string.h>
35105573Simp#include <syslog.h>
36105573Simp#include "devd.h"
37105573Simp#include "y.tab.h"
38105573Simp
39105573Simpint lineno = 1;
40105573Simp#define YY_NO_UNPUT
41105573Simp
42107665Simpstatic void
43107665Simpupdate_lineno(const char *cp)
44107665Simp{
45107665Simp	while (*cp)
46107665Simp		if (*cp++ == '\n')
47107665Simp			lineno++;
48107665Simp}
49107665Simp
50105573Simp%}
51105573Simp
52105573Simp%%
53105573Simp
54105573Simp[ \t]+			;
55105573Simp\n			lineno++;
56105573Simp;			{ return SEMICOLON; }
57107665Simp#.*$			;
58105573Simp\/\/.*$			;
59117945Simp\/\*([^*]|(\*+([^*\/])))*\*+\/ { update_lineno(yytext); }
60105573Simp\{			{ return BEGINBLOCK; }
61105573Simp\}			{ return ENDBLOCK; }
62105573Simp[0-9]+			{ yylval.i = atoi(yytext); return NUMBER; }
63105573Simp\"[^"]+\"		{
64107665Simp				update_lineno(yytext);
65105573Simp				int len = strlen(yytext) - 2;
66107665Simp				char *walker;
67107665Simp				int i;
68105573Simp				if ((yylval.str = (char *) malloc(len + 1)) == NULL)
69105573Simp					goto out;
70107665Simp				walker = yylval.str;
71107665Simp				for (i = 1; i <= len; i++) {
72107665Simp					if (yytext[i] == '\\' &&
73107665Simp					    yytext[i + 1] == '\n') {
74107665Simp						i += 2;
75107665Simp						while(isspace(yytext[i]))
76107665Simp							i++;
77107665Simp					}
78107665Simp					*walker++ = yytext[i];
79107665Simp				}
80107665Simp				*walker++ = '\0';
81105573Simp			out:;
82105573Simp				return STRING;
83105573Simp			}
84105573Simp
85105573Simp
86105573Simpoptions			{ return OPTIONS; }
87105573Simpset			{ return SET; }
88105573Simpdirectory		{ return DIRECTORY; }
89105573Simppid-file		{ return PID_FILE; }
90105573Simpattach			{ return ATTACH; }
91105573Simpdetach			{ return DETACH; }
92105573Simpdevice-name		{ return DEVICE_NAME; }
93147874Simpmedia-type		{ return MEDIA_TYPE; }
94147874Simpclass			{ return CLASS; }
95147874Simpsubdevice		{ return SUBDEVICE; }
96105573Simpaction			{ return ACTION; }
97105573Simpmatch			{ return MATCH; }
98105573Simpnomatch			{ return NOMATCH; }
99121487Simpnotify			{ return NOTIFY; }
100108014Simp[A-Za-z][A-Za-z0-9_-]*	{
101107665Simp				yylval.str = strdup(yytext);
102105573Simp				return ID;
103105573Simp			}
104105573Simp%%
105105573Simp
106105573Simpvoid
107105573Simpyyerror(const char *s)
108105573Simp{
109105573Simp	syslog(LOG_ERR, "line %d: %s%s %s.\n", lineno, yytext, yytext?":":"", s);
110105573Simp}
111