]>
Commit | Line | Data |
---|---|---|
6054c1eb | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
311b4145 | 2 | %{ |
5f78bc3e | 3 | #include "emp_ematch.tab.h" |
311b4145 SH |
4 | #include "m_ematch.h" |
5 | ||
6 | extern int ematch_argc; | |
7 | extern char **ematch_argv; | |
8 | ||
737f15f6 SH |
9 | #define yylval ematch_lval |
10 | ||
311b4145 SH |
11 | #define NEXT_EM_ARG() do { ematch_argc--; ematch_argv++; } while(0); |
12 | ||
13 | #define YY_INPUT(buf, result, max_size) \ | |
14 | { \ | |
15 | next: \ | |
16 | if (ematch_argc <= 0) \ | |
17 | result = YY_NULL; \ | |
18 | else if (**ematch_argv == '\0') { \ | |
19 | NEXT_EM_ARG(); \ | |
20 | goto next; \ | |
21 | } else { \ | |
22 | if (max_size <= strlen(*ematch_argv) + 1) { \ | |
23 | fprintf(stderr, "match argument too long.\n"); \ | |
24 | result = YY_NULL; \ | |
25 | } else { \ | |
26 | strcpy(buf, *ematch_argv); \ | |
27 | result = strlen(*ematch_argv) + 1; \ | |
28 | buf[result-1] = ' '; \ | |
29 | buf[result] = '\0'; \ | |
30 | NEXT_EM_ARG(); \ | |
31 | } \ | |
32 | } \ | |
33 | } | |
34 | ||
35 | static void __attribute__ ((unused)) yyunput (int c,char *buf_ptr ); | |
36 | static void __attribute__ ((unused)) yy_push_state (int new_state ); | |
37 | static void __attribute__ ((unused)) yy_pop_state (void); | |
38 | static int __attribute__ ((unused)) yy_top_state (void ); | |
311b4145 | 39 | |
3fcdebb6 | 40 | static char *strbuf; |
311b4145 SH |
41 | static unsigned int strbuf_size; |
42 | static unsigned int strbuf_index; | |
43 | ||
737f15f6 | 44 | static void strbuf_enlarge(void) |
311b4145 SH |
45 | { |
46 | strbuf_size += 512; | |
47 | strbuf = realloc(strbuf, strbuf_size); | |
48 | } | |
49 | ||
3fcdebb6 | 50 | static void strbuf_append_char(char c) |
311b4145 SH |
51 | { |
52 | while (strbuf_index >= strbuf_size) | |
53 | strbuf_enlarge(); | |
54 | strbuf[strbuf_index++] = c; | |
55 | } | |
56 | ||
3fcdebb6 | 57 | static void strbuf_append_charp(char *s) |
311b4145 SH |
58 | { |
59 | while (strbuf_index >= strbuf_size) | |
60 | strbuf_enlarge(); | |
61 | memcpy(strbuf + strbuf_index, s, strlen(s)); | |
62 | strbuf_index += strlen(s); | |
63 | } | |
64 | ||
3fcdebb6 SH |
65 | %} |
66 | ||
608a96c7 | 67 | %x lexstr |
3fcdebb6 SH |
68 | |
69 | %option 8bit stack warn noyywrap prefix="ematch_" | |
70 | %% | |
311b4145 SH |
71 | [ \t\r\n]+ |
72 | ||
73 | \" { | |
74 | if (strbuf == NULL) { | |
75 | strbuf_size = 512; | |
76 | strbuf = calloc(1, strbuf_size); | |
77 | if (strbuf == NULL) | |
78 | return ERROR; | |
79 | } | |
80 | strbuf_index = 0; | |
ae665a52 | 81 | |
608a96c7 | 82 | BEGIN(lexstr); |
311b4145 SH |
83 | } |
84 | ||
608a96c7 | 85 | <lexstr>\" { |
311b4145 | 86 | BEGIN(INITIAL); |
737f15f6 SH |
87 | yylval.b = bstr_new(strbuf, strbuf_index); |
88 | yylval.b->quoted = 1; | |
311b4145 SH |
89 | return ATTRIBUTE; |
90 | } | |
91 | ||
608a96c7 | 92 | <lexstr>\\[0-7]{1,3} { /* octal escape sequence */ |
311b4145 | 93 | int res; |
ae665a52 | 94 | |
311b4145 SH |
95 | sscanf(yytext + 1, "%o", &res); |
96 | if (res > 0xFF) { | |
97 | fprintf(stderr, "error: octal escape sequence" \ | |
98 | " out of range\n"); | |
99 | return ERROR; | |
100 | } | |
101 | strbuf_append_char((unsigned char) res); | |
102 | } | |
103 | ||
608a96c7 | 104 | <lexstr>\\[0-9]+ { /* catch wrong octal escape seq. */ |
311b4145 SH |
105 | fprintf(stderr, "error: invalid octale escape sequence\n"); |
106 | return ERROR; | |
107 | } | |
108 | ||
608a96c7 | 109 | <lexstr>\\x[0-9a-fA-F]{1,2} { |
311b4145 | 110 | int res; |
ae665a52 | 111 | |
311b4145 | 112 | sscanf(yytext + 2, "%x", &res); |
ae665a52 | 113 | |
311b4145 SH |
114 | if (res > 0xFF) { |
115 | fprintf(stderr, "error: hexadecimal escape " \ | |
116 | "sequence out of range\n"); | |
117 | return ERROR; | |
118 | } | |
119 | strbuf_append_char((unsigned char) res); | |
120 | } | |
121 | ||
608a96c7 BT |
122 | <lexstr>\\n strbuf_append_char('\n'); |
123 | <lexstr>\\r strbuf_append_char('\r'); | |
124 | <lexstr>\\t strbuf_append_char('\t'); | |
125 | <lexstr>\\v strbuf_append_char('\v'); | |
126 | <lexstr>\\b strbuf_append_char('\b'); | |
127 | <lexstr>\\f strbuf_append_char('\f'); | |
128 | <lexstr>\\a strbuf_append_char('\a'); | |
311b4145 | 129 | |
608a96c7 BT |
130 | <lexstr>\\(.|\n) strbuf_append_char(yytext[1]); |
131 | <lexstr>[^\\\n\"]+ strbuf_append_charp(yytext); | |
311b4145 SH |
132 | |
133 | [aA][nN][dD] return AND; | |
134 | [oO][rR] return OR; | |
135 | [nN][oO][tT] return NOT; | |
136 | "(" | | |
137 | ")" { | |
737f15f6 | 138 | return yylval.i = *yytext; |
311b4145 | 139 | } |
7ac29190 | 140 | [^" \t\r\n()][^ \t\r\n()]* { |
737f15f6 SH |
141 | yylval.b = bstr_alloc(yytext); |
142 | if (yylval.b == NULL) | |
311b4145 SH |
143 | return ERROR; |
144 | return ATTRIBUTE; | |
145 | } | |
146 | %% |