More unnecessary <stdio.h> includes.
[metze/wireshark/wip.git] / wiretap / ascend_scanner.l
1 /*
2  * We don't read interactively from the terminal.
3  */
4 %option never-interactive
5
6 /*
7  * Prefix scanner routines with "ascend" rather than "yy", so this scanner
8  * can coexist with other scanners.
9  */
10 %option prefix="ascend"
11
12 %{
13 /* ascend_scanner.l
14  *
15  * Wiretap Library
16  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
31  */
32
33 #include "config.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "wtap-int.h"
39 #include "ascendtext.h"
40 #include "ascend.h"
41 #include "ascend-int.h"
42 #include "file_wrappers.h"
43 #include "ascend_scanner_lex.h"
44
45 FILE_T yy_fh;
46 extern char *ascend_ra_ptr;
47 extern char *ascend_ra_last;
48 #define YY_INPUT(buf,result,max_size) { int c = file_getc(yy_fh);  \
49 result = (c==EOF) ? YY_NULL : (buf[0] = c, 1); }
50
51 int at_eof;
52 int mul, scratch;
53
54 #define NO_USER "<none>"
55
56 #ifndef HAVE_UNISTD_H
57 #define YY_NO_UNISTD_H
58 #endif
59
60
61 %}
62
63 D [0-9]
64 H [A-Fa-f0-9]
65
66 PPP_XPFX PPP-OUT
67 PPP_RPFX PPP-IN
68 ISDN_XPFX PRI-XMIT-
69 ISDN_RPFX PRI-RCV-
70 WAN_XPFX XMIT[\-:]*
71 WAN_RPFX RECV[\-:]*
72 ETHER_PFX ETHER
73
74 WDD_DATE    "Date:"
75 WDD_TIME    "Time:"
76 WDD_CAUSE   "Cause an attempt to place call to "
77 WDD_CALLNUM [^\n\r\t ]+
78 WDD_CHUNK   "WD_DIALOUT_DISP: chunk"
79 WDD_TYPE    "type "[^\n\r\t ]+
80
81 %s sc_gen_task
82 %s sc_gen_time_s
83 %s sc_gen_time_u
84 %s sc_gen_octets
85 %s sc_gen_counter
86 %s sc_gen_byte
87
88 %s sc_wds_user
89 %s sc_wds_sess
90
91 %s sc_wdd_date_d
92 %s sc_wdd_date_m
93 %s sc_wdd_date_y
94 %s sc_wdd_time
95 %s sc_wdd_time_h
96 %s sc_wdd_time_m
97 %s sc_wdd_time_s
98 %s sc_wdd_cause
99 %s sc_wdd_callnum
100 %s sc_wdd_chunk
101 %s sc_wdd_chunknum
102 %s sc_wdd_type
103
104 %s sc_chardisp
105
106 %s sc_isdn_call
107 %s sc_ether_direction
108
109 %%
110
111 <INITIAL,sc_gen_byte>{ETHER_PFX} {
112   BEGIN(sc_ether_direction);
113   ascendlval.d = ASCEND_PFX_ETHER;
114   return ETHER_PREFIX;
115 }
116
117 <INITIAL,sc_gen_byte>{ISDN_XPFX} {
118   BEGIN(sc_isdn_call);
119   ascendlval.d = ASCEND_PFX_ISDN_X;
120   return ISDN_PREFIX;
121 }
122
123 <INITIAL,sc_gen_byte>{ISDN_RPFX} {
124   BEGIN(sc_isdn_call);
125   ascendlval.d = ASCEND_PFX_ISDN_R;
126   return ISDN_PREFIX;
127 }
128
129 <INITIAL,sc_gen_byte>{WAN_XPFX} {
130   BEGIN(sc_wds_user);
131   ascendlval.d = ASCEND_PFX_WDS_X;
132   return WDS_PREFIX;
133 }
134
135 <INITIAL,sc_gen_byte>{WAN_RPFX} {
136   BEGIN(sc_wds_user);
137   ascendlval.d = ASCEND_PFX_WDS_R;
138   return WDS_PREFIX;
139 }
140
141 <INITIAL,sc_gen_byte>{PPP_XPFX} {
142   BEGIN(sc_wds_user);
143   ascendlval.d = ASCEND_PFX_WDS_X;
144   return WDS_PREFIX;
145 }
146
147 <INITIAL,sc_gen_byte>{PPP_RPFX} {
148   BEGIN(sc_wds_user);
149   ascendlval.d = ASCEND_PFX_WDS_R;
150   return WDS_PREFIX;
151 }
152
153 <sc_ether_direction>[^\(]{2,20} {
154   BEGIN(sc_gen_task);
155   return STRING;
156 }
157
158 <sc_isdn_call>[^\/\(:]{2,20} {
159   BEGIN(sc_gen_task);
160   return DECNUM;
161 }
162
163 <sc_wds_user>[^:]{2,20} {
164   char *atcopy = g_strdup(ascendtext);
165   char colon = input();
166   char after = input();
167   int retval = STRING;
168
169   unput(after); unput(colon);
170
171   if (after != '(' && after != ' ') {
172     BEGIN(sc_wds_sess);
173     if (pseudo_header != NULL) {
174       g_strlcpy(pseudo_header->user, atcopy, ASCEND_MAX_STR_LEN);
175     }
176   } else {      /* We have a version 7 file */
177     BEGIN(sc_gen_task);
178     if (pseudo_header != NULL) {
179       g_strlcpy(pseudo_header->user, NO_USER, ASCEND_MAX_STR_LEN);
180     }
181     /* Are valid values ever > 2^32? If so we need to adjust YYSTYPE and a lot of */
182     /* upstream code accordingly. */
183     ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
184     retval = DECNUM;
185   }
186   g_free (atcopy);
187   return retval;
188 }
189
190 <sc_wds_sess>{D}* {
191   BEGIN(sc_gen_task);
192   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
193   return DECNUM;
194 }
195
196 <sc_gen_task>(0x|0X)?{H}{2,8} {
197   BEGIN(sc_gen_time_s);
198   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 16);
199   return HEXNUM;
200 }
201
202 <sc_gen_task>\"[A-Za-z0-9_ ]+\" {
203   return STRING;
204 }
205
206 <sc_gen_time_s>{D}{1,10} {
207   BEGIN(sc_gen_time_u);
208   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
209   return DECNUM;
210 }
211
212 <sc_gen_time_u>{D}{1,6} {
213   char *atcopy = g_strdup(ascendtext);
214   BEGIN(sc_gen_octets);
215   /* only want the most significant 2 digits. convert to usecs */
216   if (strlen(atcopy) > 2)
217     atcopy[2] = '\0';
218   ascendlval.d = (guint32) strtoul(atcopy, NULL, 10) * 10000;
219   g_free(atcopy);
220   return DECNUM;
221 }
222
223 <sc_gen_octets>{D}{1,10} {
224   BEGIN(sc_gen_counter);
225   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
226   return DECNUM;
227 }
228
229 <sc_gen_counter,sc_gen_byte>"["{H}{4}"]:" {
230   BEGIN(sc_gen_byte);
231   return COUNTER;
232 }
233
234 <sc_gen_byte>{H}{2} {
235   ascendlval.b = (guint8)(guint32) strtoul(ascendtext, NULL, 16);
236   return HEXBYTE;
237 }
238
239 <sc_gen_byte>" "{4} {
240   BEGIN(sc_chardisp);
241 }
242
243 <sc_chardisp>.* {
244   BEGIN(sc_gen_byte);
245 }
246
247 <INITIAL,sc_gen_byte>{WDD_DATE} {
248   BEGIN(sc_wdd_date_d);
249   return WDD_DATE;
250 }
251
252 <sc_wdd_date_d>{D}{2} {
253   BEGIN(sc_wdd_date_m);
254   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
255   return DECNUM;
256 }
257
258 <sc_wdd_date_m>{D}{2} {
259   BEGIN(sc_wdd_date_y);
260   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
261   return DECNUM;
262 }
263
264 <sc_wdd_date_y>{D}{4} {
265   BEGIN(sc_wdd_time);
266   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
267   return DECNUM;
268 }
269
270 <sc_wdd_time>{WDD_TIME} {
271   BEGIN(sc_wdd_time_h);
272   return KEYWORD;
273 }
274
275 <sc_wdd_time_h>{D}{2} {
276   BEGIN(sc_wdd_time_m);
277   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
278   return DECNUM;
279 }
280
281 <sc_wdd_time_m>{D}{2} {
282   BEGIN(sc_wdd_time_s);
283   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
284   return DECNUM;
285 }
286
287 <sc_wdd_time_s>{D}{2} {
288   BEGIN(sc_wdd_cause);
289   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 10);
290   return DECNUM;
291 }
292
293 <sc_wdd_cause>{WDD_CAUSE} {
294   BEGIN(sc_wdd_callnum);
295   return KEYWORD;
296 }
297
298 <sc_wdd_callnum>{WDD_CALLNUM} {
299   BEGIN(sc_wdd_chunk);
300   if (pseudo_header != NULL) {
301     g_strlcpy(pseudo_header->call_num, ascendtext, ASCEND_MAX_STR_LEN);
302   }
303   return STRING;
304 }
305
306 <INITIAL,sc_wdd_chunk,sc_gen_byte>{WDD_CHUNK} {
307   BEGIN(sc_wdd_chunknum);
308   return WDD_CHUNK;
309 }
310
311 <sc_wdd_chunknum>{H}{1,8} {
312   BEGIN(sc_wdd_type);
313   ascendlval.d = (guint32) strtoul(ascendtext, NULL, 16);
314   return HEXNUM;
315 }
316
317 <sc_wdd_type>{WDD_TYPE} {
318   BEGIN(sc_gen_task);
319   return KEYWORD;
320 }
321
322 <sc_gen_task>\/{D}+ {
323   return SLASH_SUFFIX;
324 }
325
326 (0x|0X)?{H}+ { return HEXNUM; }
327
328 task:|task|at|time:|octets { return KEYWORD; }
329
330 <<EOF>> { at_eof++; yyterminate(); }
331
332 (.|\n) ;
333
334 %%
335
336 void ascend_init_lexer(FILE_T fh)
337 {
338   yyrestart(0);
339   yy_fh = fh;
340   BEGIN(INITIAL);
341 }
342
343 /*
344  * We want to stop processing when we get to the end of the input.
345  * (%option noyywrap is not used because if used then
346  * some flex versions (eg: 2.5.35) generate code which causes
347  * warnings by the Windows VC compiler).
348  */
349
350 int yywrap(void) {
351     return 1;
352 }