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