/* busmaster_scanner.l * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez * * Support for Busmaster log file format * Copyright (c) 2019 by Maksim Salau * * SPDX-License-Identifier: GPL-2.0-or-later */ %top { /* Include this before everything else, for various large-file definitions */ #include "config.h" #include } %option noyywrap %option noinput %option nounput %option batch %option never-interactive %option nodefault %option prefix="busmaster_" %option reentrant %option extra-type="busmaster_state_t *" %option noyy_scan_buffer %option noyy_scan_bytes %option noyy_scan_string /* * We have to override the memory allocators so that we don't get * "unused argument" warnings from the yyscanner argument (which * we don't use, as we have a global memory allocator). * * We provide, as macros, our own versions of the routines generated by Flex, * which just call malloc()/realloc()/free() (as the Flex versions do), * discarding the extra argument. */ %option noyyalloc %option noyyrealloc %option noyyfree %{ #include #include #include "busmaster_parser.h" #include "busmaster_priv.h" #ifndef HAVE_UNISTD_H #define YY_NO_UNISTD_H #endif static int busmaster_yyinput(void *buf, unsigned int length, busmaster_state_t *state) { int ret = file_read(buf, length, state->fh); if (ret < 0) { state->err = file_error(state->fh, &state->err_info); return YY_NULL; } return ret; } #define YY_INPUT(buf, result, max_size) \ do { (result) = busmaster_yyinput((buf), (max_size), yyextra); } while (0) /* Count bytes read. This is required in order to rewind the file * to the beginning of the next packet, since flex reads more bytes * before executing the action that does yyterminate(). */ #define YY_USER_ACTION do { yyextra->file_bytes_read += yyleng; } while (0); /* * Sleazy hack to suppress compiler warnings in yy_fatal_error(). */ #define YY_EXIT_FAILURE ((void)yyscanner, 2) /* * Macros for the allocators, to discard the extra argument. */ #define busmaster_alloc(size, yyscanner) (void *)malloc(size) #define busmaster_realloc(ptr, size, yyscanner) (void *)realloc((char *)(ptr), (size)) #define busmaster_free(ptr, yyscanner) free((char *)(ptr)) DIAG_OFF_FLEX() %} SPC [ \t]+ ENDL [\r\n][ \t\r\n]* INT [0-9]+ NUM (0x)?[0-9A-Fa-f]+ %x HEADER TIME %x HEADER_CHANNELS HEADER_DB_FILES %% <*>{SPC} ; {ENDL} { yyterminate(); }; {ENDL} { YY_FATAL_ERROR("Unterminated header statement"); } "***" { BEGIN(HEADER); } "***"{ENDL}"***" { BEGIN(HEADER); return TOKEN_ENDL; }
"***"{ENDL} { BEGIN(INITIAL); yyterminate(); }
"BUSMASTER" { return TOKEN_HEADER_VER; }
"PROTOCOL CAN" { yyextra->token.v0 = PROTOCOL_CAN; return TOKEN_PROTOCOL_TYPE; }
"PROTOCOL J1939" { yyextra->token.v0 = PROTOCOL_J1939; return TOKEN_PROTOCOL_TYPE; }
"PROTOCOL LIN" { yyextra->token.v0 = PROTOCOL_LIN; return TOKEN_PROTOCOL_TYPE; }
"START DATE AND TIME" { BEGIN(TIME); return TOKEN_START_TIME; }
"END DATE AND TIME" { BEGIN(TIME); return TOKEN_END_TIME; }
"DEC" { yyextra->token.v0 = DATA_MODE_DEC; return TOKEN_DATA_MODE; }
"HEX" { yyextra->token.v0 = DATA_MODE_HEX; return TOKEN_DATA_MODE; }
"ABSOLUTE MODE" { yyextra->token.v0 = TIME_MODE_ABSOLUTE; return TOKEN_TIME_MODE; }
"SYSTEM MODE" { yyextra->token.v0 = TIME_MODE_SYSTEM; return TOKEN_TIME_MODE; }
"RELATIVE MODE" { yyextra->token.v0 = TIME_MODE_RELATIVE; return TOKEN_TIME_MODE; }
"[START LOGGING SESSION]" { return TOKEN_START_SESSION; }
"[STOP LOGGING SESSION]" { return TOKEN_STOP_SESSION; }
"START CHANNEL BAUD RATE***" { BEGIN(HEADER_CHANNELS); }
"START DATABASE FILES (DBF/DBC)***" { BEGIN(HEADER_DB_FILES); }
. { return TOKEN_HEADER_CHAR; } "***END CHANNEL BAUD RATE***"{ENDL}"***" { BEGIN(HEADER); } .+ ; {ENDL} ; "***END DATABASE FILES (DBF/DBC)***"{ENDL}"***" { BEGIN(HEADER); } "***END OF DATABASE FILES (DBF/DBC)***"{ENDL}"***" { BEGIN(HEADER); } .+ ; {ENDL} ;