xref: /freebsd/contrib/com_err/lex.l (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1847a7462SMark Murray %{
2847a7462SMark Murray /*
3*ae771770SStanislav Sedov  * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
4847a7462SMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
5847a7462SMark Murray  * All rights reserved.
6847a7462SMark Murray  *
7847a7462SMark Murray  * Redistribution and use in source and binary forms, with or without
8847a7462SMark Murray  * modification, are permitted provided that the following conditions
9847a7462SMark Murray  * are met:
10847a7462SMark Murray  *
11847a7462SMark Murray  * 1. Redistributions of source code must retain the above copyright
12847a7462SMark Murray  *    notice, this list of conditions and the following disclaimer.
13847a7462SMark Murray  *
14847a7462SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
15847a7462SMark Murray  *    notice, this list of conditions and the following disclaimer in the
16847a7462SMark Murray  *    documentation and/or other materials provided with the distribution.
17847a7462SMark Murray  *
182b2b3720SJacques Vidrine  * 3. Neither the name of the Institute nor the names of its contributors
19847a7462SMark Murray  *    may be used to endorse or promote products derived from this software
20847a7462SMark Murray  *    without specific prior written permission.
21847a7462SMark Murray  *
22847a7462SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23847a7462SMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24847a7462SMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25847a7462SMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26847a7462SMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27847a7462SMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28847a7462SMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29847a7462SMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30847a7462SMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31847a7462SMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32847a7462SMark Murray  * SUCH DAMAGE.
33847a7462SMark Murray  */
34847a7462SMark Murray 
35847a7462SMark Murray /*
36847a7462SMark Murray  * This is to handle the definition of this symbol in some AIX
37847a7462SMark Murray  * headers, which will conflict with the definition that lex will
38847a7462SMark Murray  * generate for it.  It's only a problem for AIX lex.
39847a7462SMark Murray  */
40847a7462SMark Murray 
41847a7462SMark Murray #undef ECHO
42847a7462SMark Murray 
43847a7462SMark Murray #include "compile_et.h"
44847a7462SMark Murray #include "parse.h"
452b2b3720SJacques Vidrine #include "lex.h"
46847a7462SMark Murray 
47847a7462SMark Murray static unsigned lineno = 1;
482b2b3720SJacques Vidrine static int getstring(void);
492b2b3720SJacques Vidrine 
502b2b3720SJacques Vidrine #define YY_NO_UNPUT
512b2b3720SJacques Vidrine 
522b2b3720SJacques Vidrine #undef ECHO
53847a7462SMark Murray 
54847a7462SMark Murray %}
55847a7462SMark Murray 
56*ae771770SStanislav Sedov %option nounput
57847a7462SMark Murray 
58847a7462SMark Murray %%
59847a7462SMark Murray et			{ return ET; }
60847a7462SMark Murray error_table		{ return ET; }
61847a7462SMark Murray ec			{ return EC; }
62847a7462SMark Murray error_code		{ return EC; }
63847a7462SMark Murray prefix			{ return PREFIX; }
64847a7462SMark Murray index			{ return INDEX; }
65847a7462SMark Murray id			{ return ID; }
66847a7462SMark Murray end			{ return END; }
67847a7462SMark Murray [0-9]+			{ yylval.number = atoi(yytext); return NUMBER; }
68847a7462SMark Murray #[^\n]*			;
69847a7462SMark Murray [ \t]			;
70847a7462SMark Murray \n			{ lineno++; }
71847a7462SMark Murray \"			{ return getstring(); }
72847a7462SMark Murray [a-zA-Z0-9_]+		{ yylval.string = strdup(yytext); return STRING; }
73847a7462SMark Murray .			{ return *yytext; }
74847a7462SMark Murray %%
75847a7462SMark Murray 
76847a7462SMark Murray #ifndef yywrap /* XXX */
77847a7462SMark Murray int
78847a7462SMark Murray yywrap ()
79847a7462SMark Murray {
80847a7462SMark Murray      return 1;
81847a7462SMark Murray }
82847a7462SMark Murray #endif
83847a7462SMark Murray 
842b2b3720SJacques Vidrine static int
85847a7462SMark Murray getstring(void)
86847a7462SMark Murray {
87847a7462SMark Murray     char x[128];
88847a7462SMark Murray     int i = 0;
89847a7462SMark Murray     int c;
90847a7462SMark Murray     int quote = 0;
9157680329SDoug Rabson     while(i < sizeof(x) - 1 && (c = input()) != EOF){
92847a7462SMark Murray 	if(quote) {
93847a7462SMark Murray 	    x[i++] = c;
94847a7462SMark Murray 	    quote = 0;
95847a7462SMark Murray 	    continue;
96847a7462SMark Murray 	}
97847a7462SMark Murray 	if(c == '\n'){
98*ae771770SStanislav Sedov 	    _lex_error_message("unterminated string");
99847a7462SMark Murray 	    lineno++;
100847a7462SMark Murray 	    break;
101847a7462SMark Murray 	}
102847a7462SMark Murray 	if(c == '\\'){
103847a7462SMark Murray 	    quote++;
104847a7462SMark Murray 	    continue;
105847a7462SMark Murray 	}
106847a7462SMark Murray 	if(c == '\"')
107847a7462SMark Murray 	    break;
108847a7462SMark Murray 	x[i++] = c;
109847a7462SMark Murray     }
110847a7462SMark Murray     x[i] = '\0';
111847a7462SMark Murray     yylval.string = strdup(x);
11257680329SDoug Rabson     if (yylval.string == NULL)
11357680329SDoug Rabson         err(1, "malloc");
114847a7462SMark Murray     return STRING;
115847a7462SMark Murray }
116847a7462SMark Murray 
117847a7462SMark Murray void
118*ae771770SStanislav Sedov _lex_error_message (const char *format, ...)
119847a7462SMark Murray {
120847a7462SMark Murray      va_list args;
121847a7462SMark Murray 
122847a7462SMark Murray      va_start (args, format);
123847a7462SMark Murray      fprintf (stderr, "%s:%d:", filename, lineno);
124847a7462SMark Murray      vfprintf (stderr, format, args);
125847a7462SMark Murray      va_end (args);
126847a7462SMark Murray      numerror++;
127847a7462SMark Murray }
128