/usr.bin/colldef/scan.l

https://bitbucket.org/freebsd/freebsd-head/ · LEX · 287 lines · 250 code · 11 blank · 26 comment · 0 complexity · 2a7d1884f5cf36d0b416b74572b81065 MD5 · raw file

  1. %x string name charmap defn nchar subs subs2
  2. %{
  3. /*-
  4. * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
  5. * at Electronni Visti IA, Kiev, Ukraine.
  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 ``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 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. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #include <sys/types.h>
  32. #include <ctype.h>
  33. #include <err.h>
  34. #include <limits.h>
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <sysexits.h>
  38. #include "common.h"
  39. #include "y.tab.h"
  40. int line_no = 1, save_no, fromsubs;
  41. u_char buf[BUFSIZE], *ptr;
  42. FILE *map_fp;
  43. YY_BUFFER_STATE main_buf, map_buf;
  44. #ifdef FLEX_DEBUG
  45. YYSTYPE yylval;
  46. #endif /* FLEX_DEBUG */
  47. int yylex(void);
  48. %}
  49. %%
  50. <INITIAL,charmap,nchar,subs,subs2>[ \t]+ ;
  51. <subs2>\" { ptr = buf; BEGIN(string); }
  52. <subs>\< { ptr = buf; fromsubs = 1; BEGIN(name); }
  53. <INITIAL>\< { ptr = buf; fromsubs = 0; BEGIN(name); }
  54. ^#.*\n line_no++;
  55. ^\n line_no++;
  56. <INITIAL>\\\n line_no++;
  57. <INITIAL,nchar,subs>\\t { yylval.ch = '\t'; return CHAR; }
  58. <INITIAL,nchar,subs>\\n { yylval.ch = '\n'; return CHAR; }
  59. <INITIAL,nchar,subs>\\b { yylval.ch = '\b'; return CHAR; }
  60. <INITIAL,nchar,subs>\\f { yylval.ch = '\f'; return CHAR; }
  61. <INITIAL,nchar,subs>\\v { yylval.ch = '\v'; return CHAR; }
  62. <INITIAL,nchar,subs>\\r { yylval.ch = '\r'; return CHAR; }
  63. <INITIAL,nchar,subs>\\a { yylval.ch = '\a'; return CHAR; }
  64. <subs2>\n {
  65. line_no++;
  66. BEGIN(INITIAL);
  67. return '\n';
  68. }
  69. <INITIAL,nchar>\n {
  70. line_no++;
  71. if (map_fp != NULL) {
  72. ptr = buf;
  73. BEGIN(defn);
  74. }
  75. return '\n';
  76. }
  77. <INITIAL>[;,{}()] return *yytext;
  78. <INITIAL>substitute { BEGIN(subs); return SUBSTITUTE; }
  79. <subs>with { BEGIN(subs2); return WITH; }
  80. <INITIAL>order return ORDER;
  81. <INITIAL>charmap BEGIN(charmap);
  82. <INITIAL>;[ \t]*\.\.\.[ \t]*; return RANGE;
  83. <INITIAL,nchar,subs>\\[0-7]{3} {
  84. u_int v;
  85. sscanf(&yytext[1], "%o", &v);
  86. yylval.ch = (u_char)v;
  87. return CHAR;
  88. }
  89. <INITIAL,nchar,subs>\\x[0-9a-fA-F]{2} {
  90. u_int v;
  91. sscanf(&yytext[2], "%x", &v);
  92. yylval.ch = (u_char)v;
  93. return CHAR;
  94. }
  95. <INITIAL,nchar,subs>\\. { yylval.ch = yytext[1]; return CHAR; }
  96. <INITIAL,nchar,subs>. { yylval.ch = *yytext; return CHAR; }
  97. <defn>^#.*\n line_no++;
  98. <defn>[ \t]+ {
  99. if (ptr == buf)
  100. errx(EX_UNAVAILABLE, "map expected near line %u of %s",
  101. line_no, map_name);
  102. *ptr = '\0';
  103. strcpy(yylval.str, buf);
  104. BEGIN(nchar);
  105. return DEFN;
  106. }
  107. <name>\/\/ {
  108. if(ptr >= buf + sizeof(buf) - 1)
  109. errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '/'",
  110. line_no);
  111. *ptr++ = '/';
  112. }
  113. <name>\/\> {
  114. if(ptr >= buf + sizeof(buf) - 1)
  115. errx(EX_UNAVAILABLE, "name buffer overflow near line %u, character '>'",
  116. line_no);
  117. *ptr++ = '>';
  118. }
  119. <string>\\\" {
  120. if(ptr >= buf + sizeof(buf) - 1)
  121. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\"'",
  122. line_no);
  123. *ptr++ = '"';
  124. }
  125. <name>\> {
  126. u_int i;
  127. if (ptr == buf)
  128. errx(EX_UNAVAILABLE, "non-empty name expected near line %u",
  129. line_no);
  130. *ptr = '\0';
  131. for (i = 0; i <= UCHAR_MAX; i++) {
  132. if (strcmp(charmap_table[i], buf) == 0)
  133. goto findit;
  134. }
  135. errx(EX_UNAVAILABLE, "name <%s> not 'charmap'-defined near line %u",
  136. buf, line_no);
  137. findit:
  138. yylval.ch = i;
  139. if (fromsubs)
  140. BEGIN(subs);
  141. else
  142. BEGIN(INITIAL);
  143. return CHAR;
  144. }
  145. <string>\" {
  146. *ptr = '\0';
  147. strcpy(yylval.str, buf);
  148. BEGIN(subs2);
  149. return STRING;
  150. }
  151. <name,defn>. {
  152. const char *s = (map_fp != NULL) ? map_name : "input";
  153. if (!isascii(*yytext) || !isprint(*yytext))
  154. errx(EX_UNAVAILABLE, "non-ASCII or non-printable character 0x%02x not allowed in the map/name near line %u of %s",
  155. *yytext, line_no, s);
  156. if(ptr >= buf + sizeof(buf) - 1)
  157. errx(EX_UNAVAILABLE, "map/name buffer overflow near line %u of %s, character '%c'",
  158. line_no, s, *yytext);
  159. *ptr++ = *yytext;
  160. }
  161. <string>\\t {
  162. if(ptr >= buf + sizeof(buf) - 1)
  163. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\t'",
  164. line_no);
  165. *ptr++ = '\t';
  166. }
  167. <string>\\b {
  168. if(ptr >= buf + sizeof(buf) - 1)
  169. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\b'",
  170. line_no);
  171. *ptr++ = '\b';
  172. }
  173. <string>\\f {
  174. if(ptr >= buf + sizeof(buf) - 1)
  175. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\f'",
  176. line_no);
  177. *ptr++ = '\f';
  178. }
  179. <string>\\v {
  180. if(ptr >= buf + sizeof(buf) - 1)
  181. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\v'",
  182. line_no);
  183. *ptr++ = '\v';
  184. }
  185. <string>\\n {
  186. if(ptr >= buf + sizeof(buf) - 1)
  187. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\n'",
  188. line_no);
  189. *ptr++ = '\n';
  190. }
  191. <string>\\r {
  192. if(ptr >= buf + sizeof(buf) - 1)
  193. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\r'",
  194. line_no);
  195. *ptr++ = '\r';
  196. }
  197. <string>\\a {
  198. if(ptr >= buf + sizeof(buf) - 1)
  199. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '\\a'",
  200. line_no);
  201. *ptr++ = '\a';
  202. }
  203. <name,string,defn>\n {
  204. const char *s = (map_fp != NULL) ? map_name : "input";
  205. errx(EX_UNAVAILABLE, "unterminated map/name/string near line %u of %s", line_no, s);
  206. }
  207. <name,string,nchar><<EOF>> {
  208. const char *s = (map_fp != NULL) ? map_name : "input";
  209. errx(EX_UNAVAILABLE, "premature EOF in the name/string/char near line %u of %s", line_no, s);
  210. }
  211. <string>\\x[0-9a-f]{2} {
  212. u_int v;
  213. sscanf(&yytext[2], "%x", &v);
  214. *ptr++ = (u_char)v;
  215. }
  216. <string>\\[0-7]{3} {
  217. u_int v;
  218. sscanf(&yytext[1], "%o", &v);
  219. *ptr++ = (u_char)v;
  220. }
  221. <string>\\. {
  222. if(ptr >= buf + sizeof(buf) - 1)
  223. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
  224. line_no, yytext[1]);
  225. *ptr++ = yytext[1];
  226. }
  227. <string>. {
  228. if(ptr >= buf + sizeof(buf) - 1)
  229. errx(EX_UNAVAILABLE, "string buffer overflow near line %u, character '%c'",
  230. line_no, *yytext);
  231. *ptr++ = *yytext;
  232. }
  233. <charmap>[^ \t\n]+ {
  234. strcat(map_name, "/");
  235. strcat(map_name, yytext);
  236. if((map_fp = fopen(map_name, "r")) == NULL)
  237. err(EX_UNAVAILABLE, "can't open 'charmap' file %s",
  238. map_name);
  239. save_no = line_no;
  240. line_no = 1;
  241. map_buf = yy_new_buffer(map_fp, YY_BUF_SIZE);
  242. main_buf = YY_CURRENT_BUFFER;
  243. yy_switch_to_buffer(map_buf);
  244. ptr = buf;
  245. BEGIN(defn);
  246. }
  247. <charmap>\n {
  248. errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
  249. line_no);
  250. }
  251. <charmap><<EOF>> {
  252. errx(EX_UNAVAILABLE, "'charmap' file name expected near line %u",
  253. line_no);
  254. }
  255. <INITIAL,defn><<EOF>> {
  256. if(map_fp != NULL) {
  257. if (ptr != buf)
  258. errx(EX_UNAVAILABLE, "premature EOF in the map near line %u of %s", line_no, map_name);
  259. yy_switch_to_buffer(main_buf);
  260. yy_delete_buffer(map_buf);
  261. fclose(map_fp);
  262. map_fp = NULL;
  263. line_no = save_no;
  264. BEGIN(INITIAL);
  265. } else
  266. yyterminate();
  267. }
  268. %%
  269. #ifdef FLEX_DEBUG
  270. main()
  271. {
  272. while(yylex())
  273. ;
  274. return 0;
  275. }
  276. #endif /* FLEX_DEBUG */