92d82357 |
1 | /****************************************************************************** |
2 | * Genevent |
3 | * |
4 | * Event generator. XML to logging C code converter. |
5 | * |
6 | * Program parameters : |
7 | * ./genevent name.xml |
8 | * |
9 | * Will generate ltt-facility-name.h, ltt-facility-id-name.h |
10 | * ltt-facility-loader-name.c, ltt-facility-loader-name.h |
11 | * in the current directory. |
12 | * |
13 | * Supports : |
14 | * - C Alignment |
15 | * - C types : struct, union, enum, basic types. |
16 | * - Architectures : LP32, ILP32, ILP64, LLP64, LP64. |
17 | * |
18 | * Additionnal structures supported : |
19 | * - embedded variable size strings |
20 | * - embedded variable size arrays |
21 | * - embedded variable size sequences |
22 | * |
23 | * Notes : |
24 | * (1) |
25 | * enums are limited to integer type, as this is what is used in C. Note, |
26 | * however, that ISO/IEC 9899:TC2 specify that the type of enum can be char, |
27 | * unsigned int or int. This is implementation defined (compiler). That's why we |
28 | * add a check for sizeof enum. |
29 | * |
30 | * (2) |
31 | * Because of archtecture defined type sizes, we need to ask for ltt_align |
32 | * (which gives the alignment) by passing basic types, not their actual sizes. |
33 | * It's up to ltt_align to determine sizes of types. |
34 | * |
35 | * Note that, from |
36 | * http://www.usenix.org/publications/login/standards/10.data.html |
37 | * (Andrew Josey <a.josey@opengroup.org>) : |
38 | * |
39 | * Data Type LP32 ILP32 ILP64 LLP64 LP64 |
40 | * char 8 8 8 8 8 |
41 | * short 16 16 16 16 16 |
42 | * int32 32 |
43 | * int 16 32 64 32 32 |
44 | * long 32 32 64 32 64 |
45 | * long long (int64) 64 |
46 | * pointer 32 32 64 64 64 |
47 | * |
48 | * With these constraints : |
49 | * sizeof(char) <= sizeof(short) <= sizeof(int) |
50 | * <= sizeof(long) = sizeof(size_t) |
51 | * |
52 | * and therefore sizeof(long) <= sizeof(pointer) <= sizeof(size_t) |
53 | * |
54 | * Which means we only have to remember which is the biggest type in a structure |
55 | * to know the structure's alignment. |
56 | */ |
57 | |
2d2d14a7 |
58 | #define _GNU_SOURCE |
59 | #include <limits.h> |
60 | #include <stdlib.h> |
92d82357 |
61 | #include <errno.h> |
62 | #include <sys/types.h> |
63 | #include <sys/stat.h> |
64 | #include <fcntl.h> |
65 | #include <stdio.h> |
66 | #include <string.h> |
67 | #include <unistd.h> |
2d2d14a7 |
68 | #include <assert.h> |
92d82357 |
69 | |
70 | #include "genevent.h" |
71 | #include "parser.h" |
72 | |
73 | |
74 | #define TRUE 1 |
75 | #define FALSE (!TRUE) |
76 | |
2d2d14a7 |
77 | /* Debugging printf */ |
78 | #ifdef DEBUG |
79 | #define dprintf(...) \ |
80 | do {\ |
81 | printf(__FILE__ ",%u,%s: ",\ |
82 | __LINE__, __func__);\ |
83 | printf(__VA_ARGS__);\ |
84 | } while(0) |
85 | #else |
86 | #define dprintf(...) |
87 | #endif |
88 | |
89 | |
92d82357 |
90 | /* Code printing */ |
91 | |
2d2d14a7 |
92 | void print_tabs(unsigned int tabs, FILE *fd) |
93 | { |
94 | for(unsigned int i = 0; i<tabs;i++) |
95 | fprintf(fd, "\t"); |
96 | } |
97 | |
92d82357 |
98 | /* Type size checking */ |
2d2d14a7 |
99 | /* Uses #error in the generated code to signal error and stop the compiler */ |
92d82357 |
100 | int print_check(int fd); |
101 | |
102 | |
103 | /* Print types */ |
104 | int print_types(int fd); |
105 | |
106 | |
107 | /* Print events */ |
108 | int print_events(int fd); |
109 | |
110 | |
2d2d14a7 |
111 | /* print type. |
112 | * |
113 | * Copied from construct_types_and_fields in LTTV facility.c */ |
114 | |
115 | int print_type(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
116 | char *nest_name, char *field_name) |
117 | { |
118 | char basename[PATH_MAX]; |
119 | unsigned int basename_len = 0; |
120 | |
121 | strcpy(basename, nest_name); |
122 | basename_len = strlen(basename); |
123 | |
124 | /* For a named type, we use the type_name directly */ |
125 | if(td->type_name != NULL) { |
126 | strncpy(basename, td->type_name, PATH_MAX); |
127 | basename_len = strlen(basename); |
128 | } else { |
129 | /* For a unnamed type, there must be a field name */ |
130 | if(basename_len != 0) { |
131 | strncat(basename, "_", PATH_MAX - basename_len); |
132 | basename_len = strlen(basename); |
133 | } |
134 | strncat(basename, field_name, PATH_MAX - basename_len); |
135 | } |
136 | |
137 | switch(td->type) { |
138 | case INT_FIXED: |
139 | fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]); |
140 | break; |
141 | case UINT_FIXED: |
142 | fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]); |
143 | break; |
144 | case CHAR: |
145 | fprintf(fd, "signed char"); |
146 | break; |
147 | case UCHAR: |
148 | fprintf(fd, "unsigned char"); |
149 | break; |
150 | case SHORT: |
151 | fprintf(fd, "short"); |
152 | break; |
153 | case USHORT: |
154 | fprintf(fd, "unsigned short"); |
155 | break; |
156 | case INT: |
157 | fprintf(fd, "int"); |
158 | break; |
159 | case UINT: |
160 | fprintf(fd, "unsigned int"); |
161 | break; |
162 | case FLOAT: |
163 | fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]); |
164 | break; |
165 | case POINTER: |
166 | fprintf(fd, "void *"); |
167 | break; |
168 | case LONG: |
169 | fprintf(fd, "long"); |
170 | break; |
171 | case ULONG: |
172 | fprintf(fd, "unsigned long"); |
173 | break; |
174 | case SIZE_T: |
175 | fprintf(fd, "size_t"); |
176 | break; |
177 | case SSIZE_T: |
178 | fprintf(fd, "ssize_t"); |
179 | break; |
180 | case OFF_T: |
181 | fprintf(fd, "off_t"); |
182 | break; |
183 | case STRING: |
184 | fprintf(fd, "char *"); |
185 | break; |
186 | case ENUM: |
187 | fprintf(fd, "enum lttng_%s", basename); |
188 | break; |
189 | case ARRAY: |
190 | fprintf(fd, "lttng_array_%s", basename); |
191 | break; |
192 | case SEQUENCE: |
193 | fprintf(fd, "lttng_sequence_%s", basename); |
194 | break; |
195 | case STRUCT: |
196 | fprintf(fd, "struct lttng_%s", basename); |
197 | break; |
198 | case UNION: |
199 | fprintf(fd, "union lttng_%s", basename); |
200 | break; |
201 | default: |
202 | printf("print_type : unknown type\n"); |
203 | return 1; |
204 | } |
205 | |
206 | return 0; |
207 | } |
208 | |
209 | /* print type declaration. |
210 | * |
211 | * Copied from construct_types_and_fields in LTTV facility.c */ |
212 | |
213 | int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
214 | char *nest_name, char *field_name) |
215 | { |
216 | char basename[PATH_MAX]; |
217 | unsigned int basename_len = 0; |
218 | |
219 | strcpy(basename, nest_name); |
220 | basename_len = strlen(basename); |
221 | |
222 | /* For a named type, we use the type_name directly */ |
223 | if(td->type_name != NULL) { |
224 | strncpy(basename, td->type_name, PATH_MAX); |
225 | basename_len = strlen(basename); |
226 | } else { |
227 | /* For a unnamed type, there must be a field name */ |
228 | if(basename_len != 0) { |
229 | strncat(basename, "_", PATH_MAX - basename_len); |
230 | basename_len = strlen(basename); |
231 | } |
232 | strncat(basename, field_name, PATH_MAX - basename_len); |
233 | dprintf("%s\n", field_name); |
234 | } |
235 | |
236 | switch(td->type) { |
237 | case ENUM: |
238 | fprintf(fd, "enum lttng_%s", basename); |
239 | fprintf(fd, " {\n"); |
240 | for(unsigned int i=0;i<td->labels.position;i++){ |
241 | print_tabs(1, fd); |
242 | fprintf(fd, "LTTNG_%s", ((char*)(td->labels.array[i]))); |
243 | fprintf(fd, ",\n"); |
244 | } |
245 | fprintf(fd, "};\n"); |
246 | fprintf(fd, "\n"); |
247 | break; |
248 | |
249 | case ARRAY: |
250 | assert(td->size >= 0); |
251 | if(td->nested_type->type_name == NULL) { |
252 | /* Not a named nested type : we must print its declaration first */ |
253 | if(print_type_declaration(td->nested_type, |
254 | fd, 0, basename, "")) return 1; |
255 | } |
256 | fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %llu\n", basename, |
257 | td->size); |
47299663 |
258 | if(print_type(td->nested_type, fd, tabs, basename, "")) return 1; |
2d2d14a7 |
259 | fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename, |
260 | basename); |
261 | fprintf(fd, "\n"); |
262 | break; |
263 | case SEQUENCE: |
264 | if(td->nested_type->type_name == NULL) { |
265 | /* Not a named nested type : we must print its declaration first */ |
266 | if(print_type_declaration(td->nested_type, |
267 | fd, 0, basename, "")) return 1; |
268 | } |
269 | fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n", |
270 | basename, |
271 | basename); |
272 | fprintf(fd, "struct lttng_sequence_%s", basename); |
273 | fprintf(fd, " {\n"); |
274 | print_tabs(1, fd); |
275 | fprintf(fd, "unsigned int len;\n"); |
276 | print_tabs(1, fd); |
47299663 |
277 | if(print_type(td->nested_type, fd, tabs, basename, "")) return 1; |
2d2d14a7 |
278 | fprintf(fd, " *array;\n"); |
279 | fprintf(fd, "};\n"); |
280 | fprintf(fd, "\n"); |
281 | break; |
282 | |
283 | case STRUCT: |
284 | for(unsigned int i=0;i<td->fields.position;i++){ |
285 | field_t *field = (field_t*)(td->fields.array[i]); |
286 | type_descriptor_t *type = field->type; |
287 | if(type->type_name == NULL) { |
288 | /* Not a named nested type : we must print its declaration first */ |
289 | if(print_type_declaration(type, |
290 | fd, 0, basename, field->name)) return 1; |
291 | } |
292 | } |
293 | fprintf(fd, "struct lttng_%s", basename); |
294 | fprintf(fd, " {\n"); |
295 | for(unsigned int i=0;i<td->fields.position;i++){ |
296 | field_t *field = (field_t*)(td->fields.array[i]); |
297 | type_descriptor_t *type = field->type; |
298 | print_tabs(1, fd); |
47299663 |
299 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
2d2d14a7 |
300 | fprintf(fd, " "); |
301 | fprintf(fd, "%s", field->name); |
302 | fprintf(fd, ";\n"); |
303 | } |
304 | fprintf(fd, "};\n"); |
305 | fprintf(fd, "\n"); |
306 | break; |
307 | case UNION: |
308 | /* TODO : Do not allow variable length fields in a union */ |
309 | for(unsigned int i=0;i<td->fields.position;i++){ |
310 | field_t *field = (field_t*)(td->fields.array[i]); |
311 | type_descriptor_t *type = field->type; |
312 | if(type->type_name == NULL) { |
313 | /* Not a named nested type : we must print its declaration first */ |
47299663 |
314 | if(print_type_declaration(type, |
315 | fd, 0, basename, field->name)) return 1; |
2d2d14a7 |
316 | } |
317 | } |
318 | fprintf(fd, "union lttng_%s", basename); |
319 | fprintf(fd, " {\n"); |
320 | for(unsigned i=0;i<td->fields.position;i++){ |
321 | field_t *field = (field_t*)(td->fields.array[i]); |
322 | type_descriptor_t *type = field->type; |
323 | print_tabs(1, fd); |
47299663 |
324 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
2d2d14a7 |
325 | fprintf(fd, " "); |
326 | fprintf(fd, "%s", field->name); |
327 | fprintf(fd, ";\n"); |
328 | } |
329 | fprintf(fd, "};\n"); |
330 | fprintf(fd, "\n"); |
331 | break; |
332 | default: |
333 | dprintf("print_type_declaration : unknown type or nothing to declare.\n"); |
334 | break; |
335 | } |
336 | |
337 | return 0; |
338 | } |
339 | |
47299663 |
340 | /* Print the logging function of an event. This is the core of genevent */ |
341 | int print_event_logging_function(char *basename, event_t *event, FILE *fd) |
342 | { |
343 | fprintf(fd, "static inline void trace_%s(\n", basename); |
344 | for(unsigned int j = 0; j < event->fields.position; j++) { |
345 | /* For each field, print the function argument */ |
346 | field_t *f = (field_t*)event->fields.array[j]; |
347 | type_descriptor_t *t = f->type; |
348 | print_tabs(2, fd); |
349 | if(print_type(t, fd, 0, basename, f->name)) return 1; |
350 | fprintf(fd, " %s", f->name); |
351 | if(j < event->fields.position-1) { |
352 | fprintf(fd, ","); |
353 | fprintf(fd, "\n"); |
354 | } |
355 | } |
356 | if(event->fields.position == 0) { |
357 | print_tabs(2, fd); |
358 | fprintf(fd, "void"); |
359 | } |
360 | fprintf(fd,")\n"); |
361 | fprintf(fd, "#ifndef CONFIG_LTT\n"); |
362 | fprintf(fd, "{\n"); |
363 | fprintf(fd, "}\n"); |
364 | fprintf(fd,"#else\n"); |
365 | fprintf(fd, "{\n"); |
366 | |
2d2d14a7 |
367 | |
47299663 |
368 | fprintf(fd, "}\n"); |
369 | fprintf(fd, "#endif //CONFIG_LTT\n\n"); |
370 | return 0; |
371 | } |
2d2d14a7 |
372 | |
373 | |
374 | /* ltt-facility-name.h : main logging header. |
375 | * log_header */ |
376 | |
377 | void print_log_header_head(facility_t *fac, FILE *fd) |
378 | { |
379 | fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname); |
380 | fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname); |
47299663 |
381 | fprintf(fd, "\n"); |
382 | fprintf(fd, "/* Facility activation at compile time. */\n"); |
383 | fprintf(fd, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac->capname); |
2d2d14a7 |
384 | } |
385 | |
386 | |
387 | |
2d2d14a7 |
388 | int print_log_header_types(facility_t *fac, FILE *fd) |
389 | { |
390 | sequence_t *types = &fac->named_types.values; |
391 | fprintf(fd, "/* Named types */\n"); |
392 | fprintf(fd, "\n"); |
393 | |
394 | for(unsigned int i = 0; i < types->position; i++) { |
395 | /* For each named type, print the definition */ |
396 | if((print_type_declaration(types->array[i], fd, |
397 | 0, "", ""))) return 1; |
398 | } |
399 | return 0; |
400 | } |
401 | |
402 | int print_log_header_events(facility_t *fac, FILE *fd) |
403 | { |
47299663 |
404 | sequence_t *events = &fac->events; |
405 | char basename[PATH_MAX]; |
406 | unsigned int facname_len; |
407 | |
408 | strncpy(basename, fac->name, PATH_MAX); |
409 | facname_len = strlen(basename); |
410 | strncat(basename, "_", PATH_MAX-facname_len); |
411 | facname_len = strlen(basename); |
412 | |
413 | for(unsigned int i = 0; i < events->position; i++) { |
414 | event_t *event = (event_t*)events->array[i]; |
415 | strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len); |
416 | |
417 | /* For each event, print structure, and then logging function */ |
418 | fprintf(fd, "/* Event %s structures */\n", |
419 | event->name); |
420 | for(unsigned int j = 0; j < event->fields.position; j++) { |
421 | /* For each unnamed type, print the definition */ |
422 | field_t *f = (field_t*)event->fields.array[j]; |
423 | type_descriptor_t *t = f->type; |
424 | if(t->type_name == NULL) |
425 | if((print_type_declaration(t, fd, 0, basename, f->name))) return 1; |
426 | } |
427 | fprintf(fd, "\n"); |
2d2d14a7 |
428 | |
47299663 |
429 | fprintf(fd, "/* Event %s logging function */\n", |
430 | event->name); |
431 | |
432 | if(print_event_logging_function(basename, event, fd)) return 1; |
433 | |
434 | fprintf(fd, "\n"); |
435 | } |
436 | |
2d2d14a7 |
437 | return 0; |
438 | } |
439 | |
440 | |
441 | void print_log_header_tail(facility_t *fac, FILE *fd) |
442 | { |
47299663 |
443 | fprintf(fd, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac->capname); |
2d2d14a7 |
444 | fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname); |
445 | } |
446 | |
447 | int print_log_header(facility_t *fac) |
448 | { |
449 | char filename[PATH_MAX]; |
450 | unsigned int filename_size = 0; |
451 | FILE *fd; |
452 | dprintf("%s\n", fac->name); |
453 | |
454 | strcpy(filename, "ltt-facility-"); |
455 | filename_size = strlen(filename); |
456 | |
457 | strncat(filename, fac->name, PATH_MAX - filename_size); |
458 | filename_size = strlen(filename); |
459 | |
460 | strncat(filename, ".h", PATH_MAX - filename_size); |
461 | filename_size = strlen(filename); |
462 | |
463 | |
464 | fd = fopen(filename, "w"); |
465 | if(fd == NULL) { |
466 | printf("Error opening file %s for writing : %s\n", |
467 | filename, strerror(errno)); |
468 | return errno; |
469 | } |
470 | |
471 | /* Print file head */ |
472 | print_log_header_head(fac, fd); |
473 | |
474 | /* print named types in declaration order */ |
475 | if(print_log_header_types(fac, fd)) return 1; |
476 | |
477 | /* Print events */ |
478 | if(print_log_header_events(fac, fd)) return 1; |
479 | |
480 | /* Print file tail */ |
481 | print_log_header_tail(fac, fd); |
482 | |
483 | |
484 | fclose(fd); |
485 | |
486 | return 0; |
487 | } |
488 | |
489 | |
490 | /* ltt-facility-id-name.h : facility id. |
491 | * log_id_header */ |
492 | int print_id_header(facility_t *fac) |
493 | { |
494 | char filename[PATH_MAX]; |
495 | unsigned int filename_size = 0; |
496 | FILE *fd; |
497 | dprintf("%s\n", fac->name); |
498 | |
499 | strcpy(filename, "ltt-facility-id-"); |
500 | filename_size = strlen(filename); |
501 | |
502 | strncat(filename, fac->name, PATH_MAX - filename_size); |
503 | filename_size = strlen(filename); |
504 | |
505 | strncat(filename, ".h", PATH_MAX - filename_size); |
506 | filename_size = strlen(filename); |
507 | |
508 | |
509 | fd = fopen(filename, "w"); |
510 | if(fd == NULL) { |
511 | printf("Error opening file %s for writing : %s\n", |
512 | filename, strerror(errno)); |
513 | return errno; |
514 | } |
515 | |
516 | fclose(fd); |
517 | |
518 | return 0; |
519 | } |
520 | |
521 | |
522 | /* ltt-facility-loader-name.h : facility specific loader info. |
523 | * loader_header */ |
524 | int print_loader_header(facility_t *fac) |
525 | { |
526 | char filename[PATH_MAX]; |
527 | unsigned int filename_size = 0; |
528 | FILE *fd; |
529 | dprintf("%s\n", fac->name); |
530 | |
531 | strcpy(filename, "ltt-facility-loader-"); |
532 | filename_size = strlen(filename); |
533 | |
534 | strncat(filename, fac->name, PATH_MAX - filename_size); |
535 | filename_size = strlen(filename); |
536 | |
537 | strncat(filename, ".h", PATH_MAX - filename_size); |
538 | filename_size = strlen(filename); |
539 | |
540 | |
541 | fd = fopen(filename, "w"); |
542 | if(fd == NULL) { |
543 | printf("Error opening file %s for writing : %s\n", |
544 | filename, strerror(errno)); |
545 | return errno; |
546 | } |
547 | |
548 | fclose(fd); |
549 | |
550 | return 0; |
551 | } |
552 | |
553 | /* ltt-facility-loader-name.c : generic faciilty loader |
554 | * loader_c */ |
555 | int print_loader_c(facility_t *fac) |
556 | { |
557 | char filename[PATH_MAX]; |
558 | unsigned int filename_size = 0; |
559 | FILE *fd; |
560 | dprintf("%s\n", fac->name); |
561 | |
562 | strcpy(filename, "ltt-facility-loader-"); |
563 | filename_size = strlen(filename); |
564 | |
565 | strncat(filename, fac->name, PATH_MAX - filename_size); |
566 | filename_size = strlen(filename); |
567 | |
568 | strncat(filename, ".c", PATH_MAX - filename_size); |
569 | filename_size = strlen(filename); |
570 | |
571 | |
572 | fd = fopen(filename, "w"); |
573 | if(fd == NULL) { |
574 | printf("Error opening file %s for writing : %s\n", |
575 | filename, strerror(errno)); |
576 | return errno; |
577 | } |
578 | |
579 | |
580 | fclose(fd); |
581 | |
582 | return 0; |
583 | } |
584 | |
585 | |
586 | |
92d82357 |
587 | /* open facility */ |
588 | /* code taken from ltt_facility_open in ltt/facility.c in lttv */ |
589 | |
590 | /***************************************************************************** |
591 | *Function name |
592 | * ltt_facility_open : open facilities |
593 | *Input params |
594 | * pathname : the path name of the facility |
595 | * |
596 | * Open the facility corresponding to the right checksum. |
597 | * |
598 | *returns the facility on success, NULL on error. |
599 | ****************************************************************************/ |
600 | facility_t *ltt_facility_open(char * pathname) |
601 | { |
602 | int ret = 0; |
603 | char *token; |
604 | parse_file_t in; |
605 | facility_t * fac = NULL; |
606 | unsigned long checksum; |
607 | char buffer[BUFFER_SIZE]; |
608 | int generated = FALSE; |
609 | |
610 | in.buffer = &(buffer[0]); |
611 | in.lineno = 0; |
612 | in.error = error_callback; |
613 | in.name = pathname; |
614 | in.unget = 0; |
615 | |
616 | in.fp = fopen(in.name, "r"); |
617 | if(in.fp == NULL) { |
618 | ret = 1; |
619 | goto open_error; |
620 | } |
621 | |
622 | while(1){ |
623 | token = getToken(&in); |
624 | if(in.type == ENDFILE) break; |
625 | |
626 | if(generated) { |
627 | printf("More than one facility in the file. Only using the first one.\n"); |
628 | break; |
629 | } |
630 | |
631 | if(strcmp(token, "<")) in.error(&in,"not a facility file"); |
632 | token = getName(&in); |
633 | |
634 | if(strcmp("facility",token) == 0) { |
635 | fac = malloc(sizeof(facility_t)); |
636 | fac->name = NULL; |
637 | fac->description = NULL; |
638 | sequence_init(&(fac->events)); |
639 | table_init(&(fac->named_types)); |
640 | sequence_init(&(fac->unnamed_types)); |
641 | |
642 | parseFacility(&in, fac); |
643 | |
644 | //check if any namedType is not defined |
645 | checkNamedTypesImplemented(&fac->named_types); |
646 | |
647 | generateChecksum(fac->name, &checksum, &fac->events); |
648 | |
649 | generated = TRUE; |
650 | } |
651 | else { |
652 | printf("facility token was expected in file %s\n", in.name); |
653 | ret = 1; |
654 | goto parse_error; |
655 | } |
656 | } |
657 | |
658 | parse_error: |
659 | fclose(in.fp); |
660 | open_error: |
661 | |
662 | if(!generated) { |
663 | printf("Cannot find facility %s\n", pathname); |
664 | fac = NULL; |
665 | } |
666 | |
667 | return fac; |
668 | } |
669 | |
670 | /* Close the facility */ |
671 | void ltt_facility_close(facility_t *fac) |
672 | { |
673 | free(fac->name); |
674 | free(fac->capname); |
675 | free(fac->description); |
676 | freeEvents(&fac->events); |
677 | sequence_dispose(&fac->events); |
678 | freeNamedType(&fac->named_types); |
679 | table_dispose(&fac->named_types); |
680 | freeTypes(&fac->unnamed_types); |
681 | sequence_dispose(&fac->unnamed_types); |
682 | free(fac); |
683 | } |
684 | |
685 | |
686 | /* Show help */ |
687 | void show_help(int argc, char ** argv) |
688 | { |
689 | printf("Genevent help : \n"); |
690 | printf("\n"); |
691 | printf("Use %s name.xml\n", argv[0]); |
692 | printf("to create :\n"); |
693 | printf("ltt-facility-name.h\n"); |
694 | printf("ltt-facility-id-name.h\n"); |
695 | printf("ltt-facility-loader-name.h\n"); |
696 | printf("ltt-facility-loader-name.c\n"); |
697 | printf("In the current directory.\n"); |
698 | printf("\n"); |
699 | } |
700 | |
701 | /* Parse program arguments */ |
702 | /* Return values : |
703 | * 0 : continue program |
704 | * -1 : stop program, return 0 |
705 | * > 0 : stop program, return value as exit. |
706 | */ |
707 | int check_args(int argc, char **argv) |
708 | { |
709 | if(argc < 2) { |
710 | printf("Not enough arguments\n"); |
711 | show_help(argc, argv); |
712 | return EINVAL; |
713 | } |
714 | |
715 | if(strcmp(argv[1], "-h") == 0) { |
716 | show_help(argc, argv); |
717 | return -1; |
718 | } |
719 | |
720 | return 0; |
721 | } |
722 | |
723 | int main(int argc, char **argv) |
724 | { |
725 | int err = 0; |
726 | facility_t *fac; |
727 | |
728 | err = check_args(argc, argv); |
729 | if(err > 0) return err; |
730 | else if(err < 0) return 0; |
731 | |
732 | /* open the facility */ |
733 | fac = ltt_facility_open(argv[1]); |
734 | if(fac == NULL) { |
735 | printf("Error opening file %s for reading : %s\n", |
736 | argv[1], strerror(errno)); |
737 | return errno; |
738 | } |
739 | |
740 | /* generate the output C files */ |
741 | |
742 | |
2d2d14a7 |
743 | /* ltt-facility-name.h : main logging header. |
744 | * log_header */ |
745 | err = print_log_header(fac); |
746 | if(err) return err; |
92d82357 |
747 | |
2d2d14a7 |
748 | /* ltt-facility-id-name.h : facility id. |
749 | * log_id_header */ |
750 | err = print_id_header(fac); |
751 | if(err) return err; |
92d82357 |
752 | |
2d2d14a7 |
753 | /* ltt-facility-loader-name.h : facility specific loader info. |
754 | * loader_header */ |
755 | err = print_loader_header(fac); |
756 | if(err) return err; |
757 | |
758 | /* ltt-facility-loader-name.c : generic faciilty loader |
759 | * loader_c */ |
760 | err = print_loader_c(fac); |
761 | if(err) return err; |
92d82357 |
762 | |
763 | /* close the facility */ |
764 | ltt_facility_close(fac); |
765 | |
766 | return 0; |
767 | } |
768 | |
769 | |