3 * Copyright (c) 2011 James Gritton
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
43 static ssize_t text2lval(size_t triml, size_t trimr, int tovar);
46 static int lineno = 1;
53 /* Whitespace or equivalent */
57 <_>\/\*([^*]|(\*+([^*\/])))*\*+\/ {
60 for (s = yytext; s < yytext + yyleng; s++)
80 /* Atomic (unquoted) strings */
81 <_,DQ>[A-Za-z0-9_!%&()\-.:<>?@\[\]^`|~]+ |
82 <_,DQ>\\(.|\n|[0-7]{1,3}|x[0-9A-Fa-f]{1,2}) |
84 (void)text2lval(0, 0, 0);
85 return instr ? STR1 : (instr = 1, STR);
88 /* Single and double quoted strings */
89 <_>'([^\'\\]|\\(.|\n))*' {
90 (void)text2lval(1, 1, 0);
91 return instr ? STR1 : (instr = 1, STR);
93 <_>\"([^"\\]|\\(.|\n))*\" |
94 <DQ>[^\"$\\]([^"\\]|\\(.|\n))*\" {
98 skip = yytext[0] == '"' ? 1 : 0;
99 atvar = text2lval(skip, 1, 1);
104 * The string has a variable inside it.
105 * Go into DQ mode to get the variable
106 * and then the rest of the string.
111 return instr ? STR1 : (instr = 1, STR);
115 /* Variables, single-word or bracketed */
116 <_,DQ>$[A-Za-z_][A-Za-z_0-9]* {
117 (void)text2lval(1, 0, 0);
118 return instr ? VAR1 : (instr = 1, VAR);
120 <_>$\{([^\n{}]|\\(.|\n))*\} |
121 <DQ>$\{([^\n\"{}]|\\(.|\n))*\} {
122 (void)text2lval(2, 1, 0);
123 return instr ? VAR1 : (instr = 1, VAR);
126 /* Partially formed bits worth complaining about */
127 <_>\/\*([^*]|(\*+([^*\/])))*\** {
128 warnx("%s line %d: unterminated comment",
132 <_>'([^\n'\\]|\\.)* |
133 <_>\"([^\n\"\\]|\\.)* {
134 warnx("%s line %d: unterminated string",
138 <_>$\{([^\n{}]|\\.)* |
139 <DQ>$\{([^\n\"{}]|\\.)* {
140 warnx("%s line %d: unterminated variable",
145 /* A hack because "<0>" rules aren't allowed */
146 <_>. return yytext[0];
155 yyerror(const char *s)
158 warnx("%s line %d: %s", cfname, lineno, s);
160 warnx("%s: unexpected EOF", cfname);
162 warnx("%s line %d: %s: %s", cfname, lineno, yytext, s);
166 * Copy string from yytext to yylval, handling backslash escapes,
167 * and optionally stopping at the beginning of a variable.
170 text2lval(size_t triml, size_t trimr, int tovar)
175 yylval.cs = d = emalloc(yyleng - trimr - triml + 1);
176 se = yytext + (yyleng - trimr);
177 for (s = yytext + triml; s < se; s++, d++) {
179 if (tovar && *s == '$') {
189 if (*s >= '0' && *s <= '7') {
191 if (s + 1 < se && s[1] >= '0' && s[1] <= '7') {
192 *d = 010 * *d + (*++s - '0');
193 if (s + 1 < se && s[1] >= '0' && s[1] <= '7')
194 *d = 010 * *d + (*++s - '0');
199 case 'a': *d = '\a'; break;
200 case 'b': *d = '\b'; break;
201 case 'f': *d = '\f'; break;
202 case 'n': *d = '\n'; break;
203 case 'r': *d = '\r'; break;
204 case 't': *d = '\t'; break;
205 case 'v': *d = '\v'; break;
206 case '\n': d--; lineno++; break;
207 default: *d = *s; break;
212 if (s[1] >= '0' && s[1] <= '9')
214 else if (s[1] >= 'A' && s[1] <= 'F')
215 *d = *++s + (0xA - 'A');
216 else if (s[1] >= 'a' && s[1] <= 'a')
217 *d = *++s + (0xa - 'a');
222 if (s[1] >= '0' && s[1] <= '9')
223 *d = *d * 0x10 + (*++s - '0');
224 else if (s[1] >= 'A' && s[1] <= 'F')
225 *d = *d * 0x10 + (*++s + (0xA - 'A'));
226 else if (s[1] >= 'a' && s[1] <= 'a')
227 *d = *d * 0x10 + (*++s + (0xa - 'a'));