| 1 | #ifndef PARSER_H |
| 2 | #define PARSER_H |
| 3 | |
| 4 | /* Extensible array container */ |
| 5 | |
| 6 | typedef struct _sequence { |
| 7 | int size; |
| 8 | int position; |
| 9 | void **array; |
| 10 | } sequence; |
| 11 | |
| 12 | void sequence_init(sequence *t); |
| 13 | void sequence_dispose(sequence *t); |
| 14 | void sequence_push(sequence *t, void *elem); |
| 15 | void *sequence_pop(sequence *t); |
| 16 | |
| 17 | |
| 18 | /* Hash table */ |
| 19 | |
| 20 | typedef struct _table { |
| 21 | sequence keys; |
| 22 | sequence values; |
| 23 | } table; |
| 24 | |
| 25 | void table_init(table *t); |
| 26 | void table_dispose(table *t); |
| 27 | void table_insert(table *t, char *key, void *value); |
| 28 | void *table_find(table *t, char *key); |
| 29 | void table_insert_int(table *t, int *key, void *value); |
| 30 | void *table_find_int(table *t, int *key); |
| 31 | |
| 32 | |
| 33 | /* Token types */ |
| 34 | |
| 35 | typedef enum _token_type { |
| 36 | ENDFILE, |
| 37 | FORWARDSLASH, |
| 38 | LANGLEBRACKET, |
| 39 | RANGLEBRACKET, |
| 40 | EQUAL, |
| 41 | QUOTEDSTRING, |
| 42 | NUMBER, |
| 43 | NAME |
| 44 | } token_type; |
| 45 | |
| 46 | |
| 47 | /* State associated with a file being parsed */ |
| 48 | typedef struct _parse_file { |
| 49 | char *name; |
| 50 | FILE * fp; |
| 51 | int lineno; |
| 52 | char *buffer; |
| 53 | token_type type; |
| 54 | int unget; |
| 55 | void (*error) (struct _parse_file *, char *); |
| 56 | } parse_file; |
| 57 | |
| 58 | void ungetToken(parse_file * in); |
| 59 | char *getToken(parse_file *in); |
| 60 | char *getForwardslash(parse_file *in); |
| 61 | char *getLAnglebracket(parse_file *in); |
| 62 | char *getRAnglebracket(parse_file *in); |
| 63 | char *getQuotedString(parse_file *in); |
| 64 | char *getName(parse_file *in); |
| 65 | int getNumber(parse_file *in); |
| 66 | char *getEqual(parse_file *in); |
| 67 | char seekNextChar(parse_file *in); |
| 68 | |
| 69 | void skipComment(parse_file * in); |
| 70 | void skipEOL(parse_file * in); |
| 71 | int isalpha(char car); |
| 72 | int isalnum(char car); |
| 73 | |
| 74 | /* Some constants */ |
| 75 | |
| 76 | static const int BUFFER_SIZE = 1024; |
| 77 | |
| 78 | |
| 79 | /* Events data types */ |
| 80 | |
| 81 | typedef enum _data_type { |
| 82 | INT, |
| 83 | UINT, |
| 84 | FLOAT, |
| 85 | STRING, |
| 86 | ENUM, |
| 87 | ARRAY, |
| 88 | SEQUENCE, |
| 89 | STRUCT, |
| 90 | UNION, |
| 91 | NONE |
| 92 | } data_type; |
| 93 | |
| 94 | |
| 95 | /* Event type descriptors */ |
| 96 | |
| 97 | typedef struct _type_descriptor { |
| 98 | char * type_name; //used for named type |
| 99 | data_type type; |
| 100 | char *fmt; |
| 101 | int size; |
| 102 | sequence labels; // for enumeration |
| 103 | sequence fields; // for structure |
| 104 | struct _type_descriptor *nested_type; // for array and sequence |
| 105 | } type_descriptor; |
| 106 | |
| 107 | |
| 108 | /* Fields within types */ |
| 109 | |
| 110 | typedef struct _field{ |
| 111 | char *name; |
| 112 | char *description; |
| 113 | type_descriptor *type; |
| 114 | } field; |
| 115 | |
| 116 | |
| 117 | /* Events definitions */ |
| 118 | |
| 119 | typedef struct _event { |
| 120 | char *name; |
| 121 | char *description; |
| 122 | type_descriptor *type; |
| 123 | } event; |
| 124 | |
| 125 | typedef struct _facility { |
| 126 | char * name; |
| 127 | char * description; |
| 128 | sequence events; |
| 129 | sequence unnamed_types; |
| 130 | table named_types; |
| 131 | } facility; |
| 132 | |
| 133 | int getSize(parse_file *in); |
| 134 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type); |
| 135 | |
| 136 | void parseFacility(parse_file *in, facility * fac); |
| 137 | void parseEvent(parse_file *in, event *ev, sequence * unnamed_types, table * named_types); |
| 138 | void parseTypeDefinition(parse_file *in, sequence * unnamed_types, table * named_types); |
| 139 | type_descriptor *parseType(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types); |
| 140 | void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types); |
| 141 | void checkNamedTypesImplemented(table * namedTypes); |
| 142 | type_descriptor * find_named_type(char *name, table * named_types); |
| 143 | void generateChecksum(char * facName, unsigned long * checksum, sequence * events); |
| 144 | |
| 145 | |
| 146 | /* get attributes */ |
| 147 | char * getNameAttribute(parse_file *in); |
| 148 | char * getFormatAttribute(parse_file *in); |
| 149 | int getSizeAttribute(parse_file *in); |
| 150 | int getValueAttribute(parse_file *in); |
| 151 | char * getValueStrAttribute(parse_file *in); |
| 152 | |
| 153 | char * getDescription(parse_file *in); |
| 154 | |
| 155 | |
| 156 | static char *intOutputTypes[] = { |
| 157 | "int8_t", "int16_t", "int32_t", "int64_t", "short int", "int", "long int" }; |
| 158 | |
| 159 | static char *uintOutputTypes[] = { |
| 160 | "uint8_t", "uint16_t", "uint32_t", "uint64_t", "unsigned short int", |
| 161 | "unsigned int", "unsigned long int" }; |
| 162 | |
| 163 | static char *floatOutputTypes[] = { |
| 164 | "undef", "undef", "float", "double", "undef", "float", "double" }; |
| 165 | |
| 166 | |
| 167 | /* Dynamic memory allocation and freeing */ |
| 168 | |
| 169 | void * memAlloc(int size); |
| 170 | char *allocAndCopy(char * str); |
| 171 | char *appendString(char *s, char *suffix); |
| 172 | void freeTypes(sequence *t); |
| 173 | void freeType(type_descriptor * td); |
| 174 | void freeEvents(sequence *t); |
| 175 | void freeNamedType(table * t); |
| 176 | void error_callback(parse_file *in, char *msg); |
| 177 | |
| 178 | |
| 179 | //checksum part |
| 180 | static const unsigned int crctab32[] = |
| 181 | { |
| 182 | #include "crc32.tab" |
| 183 | }; |
| 184 | |
| 185 | static inline unsigned long |
| 186 | partial_crc32_one(unsigned char c, unsigned long crc) |
| 187 | { |
| 188 | return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); |
| 189 | } |
| 190 | |
| 191 | static inline unsigned long |
| 192 | partial_crc32(const char *s, unsigned long crc) |
| 193 | { |
| 194 | while (*s) |
| 195 | crc = partial_crc32_one(*s++, crc); |
| 196 | return crc; |
| 197 | } |
| 198 | |
| 199 | static inline unsigned long |
| 200 | crc32(const char *s) |
| 201 | { |
| 202 | return partial_crc32(s, 0xffffffff) ^ 0xffffffff; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | #endif // PARSER_H |