4 * Bill Paul <wpaul@windriver.com>. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.sbin/ndiscvt/inf-token.l,v 1.3 2004/01/11 21:10:35 mdodd Exp $");
39 __RCSID("$NetBSD: inf-token.l,v 1.7 2007/12/12 10:04:52 martin Exp $");
48 #include "inf-parse.h"
52 int input_is_unicode = 0;
54 #define ISNT_UNICODE 2
55 static size_t opt_unicode_input(char *buf, size_t max_size);
57 #define YY_INPUT(buf, result, max_size) \
58 result = opt_unicode_input(buf, max_size)
61 void ndiscvt_error(const char *);
62 int ndiscvt_wrap(void);
65 update_lineno(const char *cp)
79 \n { lineno++; return EOL; }
85 \"(\\\"|[^"]|\"\")*\" {
86 int len = strlen(yytext) - 2;
90 update_lineno(yytext);
91 ndiscvt_lval.str = (char *)malloc(blen);
92 if (ndiscvt_lval.str == NULL)
94 walker = ndiscvt_lval.str;
95 for (i = 1; i <= len; i++) {
96 if (yytext[i] == '\"') {
97 switch (yytext[i + 1]) {
105 if (yytext[i] == '\\') {
106 switch (yytext[i + 1]) {
110 (unsigned char)yytext[i]))
123 *walker++ = yytext[i];
129 \[[a-zA-Z0-9%&\{\}\-\.\/_\\\*\ ]+\] {
130 int len = strlen(yytext);
131 yytext[len-1] = '\0';
132 ndiscvt_lval.str = strdup(yytext+1);
135 [a-zA-Z0-9%&\{\}\-\.\/_\\\*]+ {
136 ndiscvt_lval.str = strdup(yytext);
142 ndiscvt_error(const char *s)
144 errx(1, "line %d: %s%s %s.", lineno, yytext, yytext?":":"", s);
154 opt_unicode_input(char *buf, size_t max_size)
159 if (feof(yyin)) return YY_NULL;
160 if (input_is_unicode == ISNT_UNICODE)
161 /* standard ASCII .inf file */
162 return fread(buf, 1, max_size, yyin);
164 if (input_is_unicode != IS_UNICODE) {
165 /* we don't know yet if this file is unicode */
167 fprintf(stderr, "internal error: can not determine "
168 "unicode with max_size %zd\n", max_size);
171 rb = fread(buf, 2, 1, yyin);
174 if ((unsigned char)buf[0] == 0xff
175 && (unsigned char)buf[1] == 0xfe) {
176 input_is_unicode = IS_UNICODE;
178 input_is_unicode = ISNT_UNICODE;
183 if (input_is_unicode != IS_UNICODE)
187 fprintf(stderr, "internal error: can not read small unicode "
192 * read unicode string and skip every second byte if it is zero,
193 * replace it by an innocent 'A' otherwise. Poor man's
194 * version of "iconv -f unicode -t ascii" - should be good enough
195 * for INF files (maybe besides comments, which we don't care
198 rb = fread(buf, 1, max_size&~1, yyin);
199 for (i = 0; i < rb; i += 2) {