1 %{ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2011 James Gritton 6 * 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 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <err.h> 34 #include <stddef.h> 35 #include <stdlib.h> 36 #include <string.h> 37 38 #include "jailp.h" 39 #include "y.tab.h" 40 41 extern int yynerrs; 42 43 static ssize_t text2lval(size_t triml, size_t trimr, int tovar); 44 45 static int instr; 46 static int lineno = 1; 47 48 #define YY_DECL int yylex(void) 49 %} 50 51 %option noinput 52 %option nounput 53 54 %start _ DQ 55 56 %% 57 58 /* Whitespace or equivalent */ 59 <_>[ \t]+ instr = 0; 60 <_>#.* ; 61 <_>\/\/.* ; 62 <_>\/\*([^*]|(\*+([^*\/])))*\*+\/ { 63 const char *s; 64 65 for (s = yytext; s < yytext + yyleng; s++) 66 if (*s == '\n') 67 lineno++; 68 instr = 0; 69 } 70 <_>\n { 71 lineno++; 72 instr = 0; 73 } 74 75 /* Reserved tokens */ 76 <_>\+= { 77 instr = 0; 78 return PLEQ; 79 } 80 <_>[,;={}] { 81 instr = 0; 82 return yytext[0]; 83 } 84 85 /* Atomic (unquoted) strings */ 86 <_,DQ>[A-Za-z0-9_!%&()\-.:<>?@\[\]^`|~]+ | 87 <_,DQ>\\(.|\n|[0-7]{1,3}|x[0-9A-Fa-f]{1,2}) | 88 <_,DQ>[$*+/\\] { 89 (void)text2lval(0, 0, 0); 90 return instr ? STR1 : (instr = 1, STR); 91 } 92 93 /* Single and double quoted strings */ 94 <_>'([^\'\\]|\\(.|\n))*' { 95 (void)text2lval(1, 1, 0); 96 return instr ? STR1 : (instr = 1, STR); 97 } 98 <_>\"([^"\\]|\\(.|\n))*\" | 99 <DQ>[^\"$\\]([^"\\]|\\(.|\n))*\" { 100 size_t skip; 101 ssize_t atvar; 102 103 skip = yytext[0] == '"' ? 1 : 0; 104 atvar = text2lval(skip, 1, 1); 105 if (atvar < 0) 106 BEGIN _; 107 else { 108 /* 109 * The string has a variable inside it. 110 * Go into DQ mode to get the variable 111 * and then the rest of the string. 112 */ 113 BEGIN DQ; 114 yyless(atvar); 115 } 116 return instr ? STR1 : (instr = 1, STR); 117 } 118 <DQ>\" BEGIN _; 119 120 /* Variables, single-word or bracketed */ 121 <_,DQ>$[A-Za-z_][A-Za-z_0-9]* { 122 (void)text2lval(1, 0, 0); 123 return instr ? VAR1 : (instr = 1, VAR); 124 } 125 <_>$\{([^\n{}]|\\(.|\n))*\} | 126 <DQ>$\{([^\n\"{}]|\\(.|\n))*\} { 127 (void)text2lval(2, 1, 0); 128 return instr ? VAR1 : (instr = 1, VAR); 129 } 130 131 /* Partially formed bits worth complaining about */ 132 <_>\/\*([^*]|(\*+([^*\/])))*\** { 133 warnx("%s line %d: unterminated comment", 134 cfname, lineno); 135 yynerrs++; 136 } 137 <_>'([^\n'\\]|\\.)* | 138 <_>\"([^\n\"\\]|\\.)* { 139 warnx("%s line %d: unterminated string", 140 cfname, lineno); 141 yynerrs++; 142 } 143 <_>$\{([^\n{}]|\\.)* | 144 <DQ>$\{([^\n\"{}]|\\.)* { 145 warnx("%s line %d: unterminated variable", 146 cfname, lineno); 147 yynerrs++; 148 } 149 150 /* A hack because "<0>" rules aren't allowed */ 151 <_>. return yytext[0]; 152 .|\n { 153 BEGIN _; 154 yyless(0); 155 } 156 157 %% 158 159 void 160 yyerror(const char *s) 161 { 162 if (!yytext) 163 warnx("%s line %d: %s", cfname, lineno, s); 164 else if (!yytext[0]) 165 warnx("%s: unexpected EOF", cfname); 166 else 167 warnx("%s line %d: %s: %s", cfname, lineno, yytext, s); 168 } 169 170 /* 171 * Copy string from yytext to yylval, handling backslash escapes, 172 * and optionally stopping at the beginning of a variable. 173 */ 174 static ssize_t 175 text2lval(size_t triml, size_t trimr, int tovar) 176 { 177 char *d; 178 const char *s, *se; 179 180 yylval.cs = d = emalloc(yyleng - trimr - triml + 1); 181 se = yytext + (yyleng - trimr); 182 for (s = yytext + triml; s < se; s++, d++) { 183 if (*s != '\\') { 184 if (tovar && *s == '$') { 185 *d = '\0'; 186 return s - yytext; 187 } 188 if (*s == '\n') 189 lineno++; 190 *d = *s; 191 continue; 192 } 193 s++; 194 if (*s >= '0' && *s <= '7') { 195 *d = *s - '0'; 196 if (s + 1 < se && s[1] >= '0' && s[1] <= '7') { 197 *d = 010 * *d + (*++s - '0'); 198 if (s + 1 < se && s[1] >= '0' && s[1] <= '7') 199 *d = 010 * *d + (*++s - '0'); 200 } 201 continue; 202 } 203 switch (*s) { 204 case 'a': *d = '\a'; break; 205 case 'b': *d = '\b'; break; 206 case 'f': *d = '\f'; break; 207 case 'n': *d = '\n'; break; 208 case 'r': *d = '\r'; break; 209 case 't': *d = '\t'; break; 210 case 'v': *d = '\v'; break; 211 case '\n': d--; lineno++; break; 212 default: *d = *s; break; 213 case 'x': 214 *d = 0; 215 if (s + 1 >= se) 216 break; 217 if (s[1] >= '0' && s[1] <= '9') 218 *d = *++s - '0'; 219 else if (s[1] >= 'A' && s[1] <= 'F') 220 *d = *++s + (0xA - 'A'); 221 else if (s[1] >= 'a' && s[1] <= 'f') 222 *d = *++s + (0xa - 'a'); 223 else 224 break; 225 if (s + 1 >= se) 226 break; 227 if (s[1] >= '0' && s[1] <= '9') 228 *d = *d * 0x10 + (*++s - '0'); 229 else if (s[1] >= 'A' && s[1] <= 'F') 230 *d = *d * 0x10 + (*++s + (0xA - 'A')); 231 else if (s[1] >= 'a' && s[1] <= 'f') 232 *d = *d * 0x10 + (*++s + (0xa - 'a')); 233 } 234 } 235 *d = '\0'; 236 return -1; 237 } 238