3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2011 James Gritton
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
43 static ssize_t text2lval(size_t triml, size_t trimr, int tovar);
46 static int lineno = 1;
48 #define YY_DECL int yylex(void)
59 /* Whitespace or equivalent */
63 <_>\/\*([^*]|(\*+([^*\/])))*\*+\/ {
66 for (s = yytext; s < yytext + yyleng; s++)
86 /* Atomic (unquoted) strings */
87 <_,DQ>[A-Za-z0-9_!%&()\-.:<>?@\[\]^`|~]+ |
88 <_,DQ>\\(.|\n|[0-7]{1,3}|x[0-9A-Fa-f]{1,2}) |
90 (void)text2lval(0, 0, 0);
91 return instr ? STR1 : (instr = 1, STR);
94 /* Single and double quoted strings */
95 <_>'([^\'\\]|\\(.|\n))*' {
96 (void)text2lval(1, 1, 0);
97 return instr ? STR1 : (instr = 1, STR);
99 <_>\"([^"\\]|\\(.|\n))*\" |
100 <DQ>[^\"$\\]([^"\\]|\\(.|\n))*\" {
104 skip = yytext[0] == '"' ? 1 : 0;
105 atvar = text2lval(skip, 1, 1);
110 * The string has a variable inside it.
111 * Go into DQ mode to get the variable
112 * and then the rest of the string.
117 return instr ? STR1 : (instr = 1, STR);
121 /* Variables, single-word or bracketed */
122 <_,DQ>$[A-Za-z_][A-Za-z_0-9]* {
123 (void)text2lval(1, 0, 0);
124 return instr ? VAR1 : (instr = 1, VAR);
126 <_>$\{([^\n{}]|\\(.|\n))*\} |
127 <DQ>$\{([^\n\"{}]|\\(.|\n))*\} {
128 (void)text2lval(2, 1, 0);
129 return instr ? VAR1 : (instr = 1, VAR);
132 /* Partially formed bits worth complaining about */
133 <_>\/\*([^*]|(\*+([^*\/])))*\** {
134 warnx("%s line %d: unterminated comment",
138 <_>'([^\n'\\]|\\.)* |
139 <_>\"([^\n\"\\]|\\.)* {
140 warnx("%s line %d: unterminated string",
144 <_>$\{([^\n{}]|\\.)* |
145 <DQ>$\{([^\n\"{}]|\\.)* {
146 warnx("%s line %d: unterminated variable",
151 /* A hack because "<0>" rules aren't allowed */
152 <_>. return yytext[0];
161 yyerror(const char *s)
164 warnx("%s line %d: %s", cfname, lineno, s);
166 warnx("%s: unexpected EOF", cfname);
168 warnx("%s line %d: %s: %s", cfname, lineno, yytext, s);
172 * Copy string from yytext to yylval, handling backslash escapes,
173 * and optionally stopping at the beginning of a variable.
176 text2lval(size_t triml, size_t trimr, int tovar)
181 yylval.cs = d = emalloc(yyleng - trimr - triml + 1);
182 se = yytext + (yyleng - trimr);
183 for (s = yytext + triml; s < se; s++, d++) {
185 if (tovar && *s == '$') {
195 if (*s >= '0' && *s <= '7') {
197 if (s + 1 < se && s[1] >= '0' && s[1] <= '7') {
198 *d = 010 * *d + (*++s - '0');
199 if (s + 1 < se && s[1] >= '0' && s[1] <= '7')
200 *d = 010 * *d + (*++s - '0');
205 case 'a': *d = '\a'; break;
206 case 'b': *d = '\b'; break;
207 case 'f': *d = '\f'; break;
208 case 'n': *d = '\n'; break;
209 case 'r': *d = '\r'; break;
210 case 't': *d = '\t'; break;
211 case 'v': *d = '\v'; break;
212 case '\n': d--; lineno++; break;
213 default: *d = *s; break;
218 if (s[1] >= '0' && s[1] <= '9')
220 else if (s[1] >= 'A' && s[1] <= 'F')
221 *d = *++s + (0xA - 'A');
222 else if (s[1] >= 'a' && s[1] <= 'f')
223 *d = *++s + (0xa - 'a');
228 if (s[1] >= '0' && s[1] <= '9')
229 *d = *d * 0x10 + (*++s - '0');
230 else if (s[1] >= 'A' && s[1] <= 'F')
231 *d = *d * 0x10 + (*++s + (0xA - 'A'));
232 else if (s[1] >= 'a' && s[1] <= 'f')
233 *d = *d * 0x10 + (*++s + (0xa - 'a'));