| 1 | /* |
| 2 | |
| 3 | parser.c: Generate helper declarations and functions to trace events |
| 4 | from an event description file. |
| 5 | |
| 6 | Copyright (C) 2002, Xianxiu Yang |
| 7 | Copyright (C) 2002, Michel Dagenais |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; version 2 of the License. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef PARSER_H |
| 23 | #define PARSER_H |
| 24 | |
| 25 | /* Extensible array container */ |
| 26 | |
| 27 | typedef struct _sequence { |
| 28 | int size; |
| 29 | int position; |
| 30 | void **array; |
| 31 | } sequence; |
| 32 | |
| 33 | void sequence_init(sequence *t); |
| 34 | void sequence_dispose(sequence *t); |
| 35 | void sequence_push(sequence *t, void *elem); |
| 36 | void *sequence_pop(sequence *t); |
| 37 | |
| 38 | |
| 39 | /* Hash table */ |
| 40 | |
| 41 | typedef struct _table { |
| 42 | sequence keys; |
| 43 | sequence values; |
| 44 | } table; |
| 45 | |
| 46 | void table_init(table *t); |
| 47 | void table_dispose(table *t); |
| 48 | void table_insert(table *t, char *key, void *value); |
| 49 | void *table_find(table *t, char *key); |
| 50 | void table_insert_int(table *t, int *key, void *value); |
| 51 | void *table_find_int(table *t, int *key); |
| 52 | |
| 53 | |
| 54 | /* Token types */ |
| 55 | |
| 56 | typedef enum _token_type { |
| 57 | ENDFILE, |
| 58 | FORWARDSLASH, |
| 59 | LANGLEBRACKET, |
| 60 | RANGLEBRACKET, |
| 61 | EQUAL, |
| 62 | QUOTEDSTRING, |
| 63 | NUMBER, |
| 64 | NAME |
| 65 | } token_type; |
| 66 | |
| 67 | |
| 68 | /* State associated with a file being parsed */ |
| 69 | typedef struct _parse_file { |
| 70 | char *name; |
| 71 | FILE * fp; |
| 72 | int lineno; |
| 73 | char *buffer; |
| 74 | token_type type; |
| 75 | int unget; |
| 76 | void (*error) (struct _parse_file *, char *); |
| 77 | } parse_file; |
| 78 | |
| 79 | void ungetToken(parse_file * in); |
| 80 | char *getToken(parse_file *in); |
| 81 | char *getForwardslash(parse_file *in); |
| 82 | char *getLAnglebracket(parse_file *in); |
| 83 | char *getRAnglebracket(parse_file *in); |
| 84 | char *getQuotedString(parse_file *in); |
| 85 | char *getName(parse_file *in); |
| 86 | int getNumber(parse_file *in); |
| 87 | char *getEqual(parse_file *in); |
| 88 | char seekNextChar(parse_file *in); |
| 89 | |
| 90 | void skipComment(parse_file * in); |
| 91 | void skipEOL(parse_file * in); |
| 92 | int isalpha(char car); |
| 93 | int isalnum(char car); |
| 94 | |
| 95 | /* Some constants */ |
| 96 | |
| 97 | static const int BUFFER_SIZE = 1024; |
| 98 | |
| 99 | |
| 100 | /* Events data types */ |
| 101 | |
| 102 | typedef enum _data_type { |
| 103 | INT, |
| 104 | UINT, |
| 105 | FLOAT, |
| 106 | STRING, |
| 107 | ENUM, |
| 108 | ARRAY, |
| 109 | SEQUENCE, |
| 110 | STRUCT, |
| 111 | UNION, |
| 112 | NONE |
| 113 | } data_type; |
| 114 | |
| 115 | |
| 116 | /* Event type descriptors */ |
| 117 | |
| 118 | typedef struct _type_descriptor { |
| 119 | char * type_name; //used for named type |
| 120 | data_type type; |
| 121 | char *fmt; |
| 122 | int size; |
| 123 | sequence labels; // for enumeration |
| 124 | sequence fields; // for structure |
| 125 | struct _type_descriptor *nested_type; // for array and sequence |
| 126 | } type_descriptor; |
| 127 | |
| 128 | |
| 129 | /* Fields within types */ |
| 130 | |
| 131 | typedef struct _field{ |
| 132 | char *name; |
| 133 | char *description; |
| 134 | type_descriptor *type; |
| 135 | } field; |
| 136 | |
| 137 | |
| 138 | /* Events definitions */ |
| 139 | |
| 140 | typedef struct _event { |
| 141 | char *name; |
| 142 | char *description; |
| 143 | type_descriptor *type; |
| 144 | } event; |
| 145 | |
| 146 | typedef struct _facility { |
| 147 | char * name; |
| 148 | char * description; |
| 149 | sequence events; |
| 150 | sequence unnamed_types; |
| 151 | table named_types; |
| 152 | } facility; |
| 153 | |
| 154 | int getSize(parse_file *in); |
| 155 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type); |
| 156 | |
| 157 | void parseFacility(parse_file *in, facility * fac); |
| 158 | void parseEvent(parse_file *in, event *ev, sequence * unnamed_types, table * named_types); |
| 159 | void parseTypeDefinition(parse_file *in, sequence * unnamed_types, table * named_types); |
| 160 | type_descriptor *parseType(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types); |
| 161 | void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types); |
| 162 | void checkNamedTypesImplemented(table * namedTypes); |
| 163 | type_descriptor * find_named_type(char *name, table * named_types); |
| 164 | void generateChecksum(char * facName, unsigned long * checksum, sequence * events); |
| 165 | |
| 166 | |
| 167 | /* get attributes */ |
| 168 | char * getNameAttribute(parse_file *in); |
| 169 | char * getFormatAttribute(parse_file *in); |
| 170 | int getSizeAttribute(parse_file *in); |
| 171 | int getValueAttribute(parse_file *in); |
| 172 | char * getValueStrAttribute(parse_file *in); |
| 173 | |
| 174 | char * getDescription(parse_file *in); |
| 175 | |
| 176 | |
| 177 | static char *intOutputTypes[] = { |
| 178 | "int8_t", "int16_t", "int32_t", "int64_t", "short int", "int", "long int" }; |
| 179 | |
| 180 | static char *uintOutputTypes[] = { |
| 181 | "uint8_t", "uint16_t", "uint32_t", "uint64_t", "unsigned short int", |
| 182 | "unsigned int", "unsigned long int" }; |
| 183 | |
| 184 | static char *floatOutputTypes[] = { |
| 185 | "undef", "undef", "float", "double", "undef", "float", "double" }; |
| 186 | |
| 187 | |
| 188 | /* Dynamic memory allocation and freeing */ |
| 189 | |
| 190 | void * memAlloc(int size); |
| 191 | char *allocAndCopy(char * str); |
| 192 | char *appendString(char *s, char *suffix); |
| 193 | void freeTypes(sequence *t); |
| 194 | void freeType(type_descriptor * td); |
| 195 | void freeEvents(sequence *t); |
| 196 | void freeNamedType(table * t); |
| 197 | void error_callback(parse_file *in, char *msg); |
| 198 | |
| 199 | |
| 200 | //checksum part |
| 201 | static const unsigned int crctab32[] = |
| 202 | { |
| 203 | #include "crc32.tab" |
| 204 | }; |
| 205 | |
| 206 | static inline unsigned long |
| 207 | partial_crc32_one(unsigned char c, unsigned long crc) |
| 208 | { |
| 209 | return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); |
| 210 | } |
| 211 | |
| 212 | static inline unsigned long |
| 213 | partial_crc32(const char *s, unsigned long crc) |
| 214 | { |
| 215 | while (*s) |
| 216 | crc = partial_crc32_one(*s++, crc); |
| 217 | return crc; |
| 218 | } |
| 219 | |
| 220 | static inline unsigned long |
| 221 | crc32(const char *s) |
| 222 | { |
| 223 | return partial_crc32(s, 0xffffffff) ^ 0xffffffff; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | #endif // PARSER_H |