]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ndiscvt/inf-token.l
Revert r345171 pending review
[FreeBSD/FreeBSD.git] / usr.sbin / ndiscvt / inf-token.l
1 %{
2 /*-
3  * SPDX-License-Identifier: BSD-4-Clause
4  *
5  * Copyright (c) 2003
6  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Bill Paul.
19  * 4. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <regex.h>
40 #include <ctype.h>
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include "y.tab.h"
46
47 int lineno = 1;
48
49 int yylex(void);
50 void yyerror(const char *);
51
52 static void
53 update_lineno(const char *cp)
54 {
55         while (*cp)
56                 if (*cp++ == '\n')
57                         lineno++;
58 }
59
60 %}
61
62 %option nounput
63 %option noinput
64
65 %%
66
67 [ \t]+                  ;
68 \n                      { lineno++; return EOL; }
69 \r                      ;
70 ;.*$                    ;
71 \/\/.*$                 ;
72 =                       { return EQUALS; }
73 ,                       { return COMMA; }
74 \"(\\\"|[^"]|\"\")*\"   {
75                         int len = strlen(yytext) - 2;
76                         int blen = len + 1;
77                         char *walker;
78                         int i;
79                         update_lineno(yytext);
80                         yylval.str = (char *)malloc(blen);
81                         if (yylval.str == NULL)
82                                 goto out;
83                         walker = yylval.str;
84                         for (i = 1; i <= len; i++) {
85                                 if (yytext[i] == '\"') {
86                                         switch (yytext[i + 1]) {
87                                         case '\"':
88                                                 i++;
89                                                 break;
90                                         default:
91                                                 break;
92                                         }
93                                 }
94                                 if (yytext[i] == '\\') {
95                                         switch (yytext[i + 1]) {
96                                         case '\n':
97                                                 i += 2;
98                                                 while(isspace(yytext[i]))
99                                                         i++;
100                                                 break;
101                                         case '\"':
102                                                 i++;
103                                                 break;
104                                         case '(':
105                                                 i++;
106                                                 break;
107                                         default:
108                                                 break;
109                                         }
110                                 }
111                                 *walker++ = yytext[i];
112                         }
113                         *walker++ = '\0';
114                         out:;
115                         return STRING;
116                         }
117 \[[a-zA-Z0-9%&\{\}\-\.\/_\\\*\ ]+\]     {
118                                 int len = strlen(yytext);
119                                 yytext[len-1] = '\0';
120                                 yylval.str = strdup(yytext+1);
121                                 return SECTION;
122                         }
123 [a-zA-Z0-9%&\{\}\-\.\/_\\\*]+           {
124                                 yylval.str = strdup(yytext);
125                                 return WORD;
126                         }
127 %%
128
129 void
130 yyerror(const char *s)
131 {
132         errx(1, "line %d: %s%s %s.", lineno, yytext, yytext?":":"", s);
133 }