Add an ASN.1 compiler option to dump the element tree to stdout.
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -13,6 +13,7 @@ · 12 unmodified lines | ||
| 13 | 13 | #include <stdio.h> |
| 14 | 14 | #include <stdlib.h> |
| 15 | 15 | #include <stdint.h> |
| 16 | +#include <stdbool.h> | |
| 16 | 17 | #include <string.h> |
| 17 | 18 | #include <ctype.h> |
| 18 | 19 | #include <unistd.h> |
| @@ -311,9 +312,11 @@ struct token { · 292 unmodified lines | ||
| 311 | 312 | |
| 312 | 313 | static struct token *token_list; |
| 313 | 314 | static unsigned nr_tokens; |
| 314 | −static _Bool verbose; | |
| 315 | +static bool verbose_opt; | |
| 316 | +static bool debug_opt; | |
| 315 | 317 | |
| 316 | −#define debug(fmt, ...) do { if (verbose) printf(fmt, ## __VA_ARGS__); } while (0) | |
| 318 | +#define verbose(fmt, ...) do { if (verbose_opt) printf(fmt, ## __VA_ARGS__); } while (0) | |
| 319 | +#define debug(fmt, ...) do { if (debug_opt) printf(fmt, ## __VA_ARGS__); } while (0) | |
| 317 | 320 | |
| 318 | 321 | static int directive_compare(const void *_key, const void *_pdir) |
| 319 | 322 | { |
| @@ -518,7 +521,7 @@ static void tokenise(char *buffer, char *end) · 198 unmodified lines | ||
| 518 | 521 | } |
| 519 | 522 | |
| 520 | 523 | nr_tokens = tix; |
| 521 | − debug("Extracted %u tokens\n", nr_tokens); | |
| 524 | + verbose("Extracted %u tokens\n", nr_tokens); | |
| 522 | 525 | |
| 523 | 526 | #if 0 |
| 524 | 527 | { |
| @@ -534,6 +537,7 @@ static void tokenise(char *buffer, char *end) · 9 unmodified lines | ||
| 534 | 537 | |
| 535 | 538 | static void build_type_list(void); |
| 536 | 539 | static void parse(void); |
| 540 | +static void dump_elements(void); | |
| 537 | 541 | static void render(FILE *out, FILE *hdr); |
| 538 | 542 | |
| 539 | 543 | /* |
| @@ -548,16 +552,27 @@ int main(int argc, char **argv) · 8 unmodified lines | ||
| 548 | 552 | char *kbuild_verbose; |
| 549 | 553 | int fd; |
| 550 | 554 | |
| 555 | + kbuild_verbose = getenv("KBUILD_VERBOSE"); | |
| 556 | + if (kbuild_verbose) | |
| 557 | + verbose_opt = atoi(kbuild_verbose); | |
| 558 | + | |
| 559 | + while (argc > 4) { | |
| 560 | + if (strcmp(argv[1], "-v") == 0) | |
| 561 | + verbose_opt = true; | |
| 562 | + else if (strcmp(argv[1], "-d") == 0) | |
| 563 | + debug_opt = true; | |
| 564 | + else | |
| 565 | + break; | |
| 566 | + memmove(&argv[1], &argv[2], (argc - 2) * sizeof(char *)); | |
| 567 | + argc--; | |
| 568 | + } | |
| 569 | + | |
| 551 | 570 | if (argc != 4) { |
| 552 | − fprintf(stderr, "Format: %s <grammar-file> <c-file> <hdr-file>\n", | |
| 571 | + fprintf(stderr, "Format: %s [-v] [-d] <grammar-file> <c-file> <hdr-file>\n", | |
| 553 | 572 | argv[0]); |
| 554 | 573 | exit(2); |
| 555 | 574 | } |
| 556 | 575 | |
| 557 | − kbuild_verbose = getenv("KBUILD_VERBOSE"); | |
| 558 | − if (kbuild_verbose) | |
| 559 | − verbose = atoi(kbuild_verbose); | |
| 560 | − | |
| 561 | 576 | filename = argv[1]; |
| 562 | 577 | outputname = argv[2]; |
| 563 | 578 | headername = argv[3]; |
| @@ -608,6 +623,7 @@ int main(int argc, char **argv) · 44 unmodified lines | ||
| 608 | 623 | tokenise(buffer, buffer + readlen); |
| 609 | 624 | build_type_list(); |
| 610 | 625 | parse(); |
| 626 | + dump_elements(); | |
| 611 | 627 | |
| 612 | 628 | out = fopen(outputname, "w"); |
| 613 | 629 | if (!out) { |
| @@ -756,7 +772,7 @@ static void build_type_list(void) · 142 unmodified lines | ||
| 756 | 772 | |
| 757 | 773 | qsort(type_index, nr, sizeof(type_index[0]), type_index_compare); |
| 758 | 774 | |
| 759 | − debug("Extracted %u types\n", nr_types); | |
| 775 | + verbose("Extracted %u types\n", nr_types); | |
| 760 | 776 | #if 0 |
| 761 | 777 | for (n = 0; n < nr_types; n++) { |
| 762 | 778 | struct type *type = type_index[n]; |
| @@ -801,7 +817,7 @@ static void parse(void) · 38 unmodified lines | ||
| 801 | 817 | |
| 802 | 818 | } while (type++, !(type->flags & TYPE_STOP_MARKER)); |
| 803 | 819 | |
| 804 | − debug("Extracted %u actions\n", nr_actions); | |
| 820 | + verbose("Extracted %u actions\n", nr_actions); | |
| 805 | 821 | } |
| 806 | 822 | |
| 807 | 823 | static struct element *element_list; |
| @@ -1192,6 +1208,54 @@ overrun_error: · 384 unmodified lines | ||
| 1192 | 1208 | exit(1); |
| 1193 | 1209 | } |
| 1194 | 1210 | |
| 1211 | +static void dump_element(const struct element *e, int level) | |
| 1212 | +{ | |
| 1213 | + const struct element *c; | |
| 1214 | + const struct type *t = e->type_def; | |
| 1215 | + const char *name = e->name ? e->name->value : "."; | |
| 1216 | + int nsize = e->name ? e->name->size : 1; | |
| 1217 | + const char *tname = t && t->name ? t->name->value : "."; | |
| 1218 | + int tnsize = t && t->name ? t->name->size : 1; | |
| 1219 | + char tag[32]; | |
| 1220 | + | |
| 1221 | + if (e->class == 0 && e->method == 0 && e->tag == 0) | |
| 1222 | + strcpy(tag, "<...>"); | |
| 1223 | + else if (e->class == ASN1_UNIV) | |
| 1224 | + sprintf(tag, "%s %s %s", | |
| 1225 | + asn1_classes[e->class], | |
| 1226 | + asn1_methods[e->method], | |
| 1227 | + asn1_universal_tags[e->tag]); | |
| 1228 | + else | |
| 1229 | + sprintf(tag, "%s %s %u", | |
| 1230 | + asn1_classes[e->class], | |
| 1231 | + asn1_methods[e->method], | |
| 1232 | + e->tag); | |
| 1233 | + | |
| 1234 | + printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %*.*s %*.*s \e[35m%s\e[m\n", | |
| 1235 | + e->flags & ELEMENT_IMPLICIT ? 'I' : '-', | |
| 1236 | + e->flags & ELEMENT_EXPLICIT ? 'E' : '-', | |
| 1237 | + e->flags & ELEMENT_TAG_SPECIFIED ? 'T' : '-', | |
| 1238 | + e->flags & ELEMENT_SKIPPABLE ? 'S' : '-', | |
| 1239 | + e->flags & ELEMENT_CONDITIONAL ? 'C' : '-', | |
| 1240 | + "-tTqQcaro"[e->compound], | |
| 1241 | + level, "", | |
| 1242 | + tag, | |
| 1243 | + tnsize, tnsize, tname, | |
| 1244 | + nsize, nsize, name, | |
| 1245 | + e->action ? e->action->name : ""); | |
| 1246 | + if (e->compound == TYPE_REF) | |
| 1247 | + dump_element(e->type->type->element, level + 3); | |
| 1248 | + else | |
| 1249 | + for (c = e->children; c; c = c->next) | |
| 1250 | + dump_element(c, level + 3); | |
| 1251 | +} | |
| 1252 | + | |
| 1253 | +static void dump_elements(void) | |
| 1254 | +{ | |
| 1255 | + if (debug_opt) | |
| 1256 | + dump_element(type_list[0].element, 0); | |
| 1257 | +} | |
| 1258 | + | |
| 1195 | 1259 | static void render_element(FILE *out, struct element *e, struct element *tag); |
| 1196 | 1260 | static void render_out_of_line_list(FILE *out); |
| 1197 | 1261 | |
| @@ -1293,7 +1357,7 @@ static void render(FILE *out, FILE *hdr) · 95 unmodified lines | ||
| 1293 | 1357 | } |
| 1294 | 1358 | |
| 1295 | 1359 | /* We do two passes - the first one calculates all the offsets */ |
| 1296 | − debug("Pass 1\n"); | |
| 1360 | + verbose("Pass 1\n"); | |
| 1297 | 1361 | nr_entries = 0; |
| 1298 | 1362 | root = &type_list[0]; |
| 1299 | 1363 | render_element(NULL, root->element, NULL); |
| @@ -1304,7 +1368,7 @@ static void render(FILE *out, FILE *hdr) · 4 unmodified lines | ||
| 1304 | 1368 | e->flags &= ~ELEMENT_RENDERED; |
| 1305 | 1369 | |
| 1306 | 1370 | /* And then we actually render */ |
| 1307 | − debug("Pass 2\n"); | |
| 1371 | + verbose("Pass 2\n"); | |
| 1308 | 1372 | fprintf(out, "\n"); |
| 1309 | 1373 | fprintf(out, "static const unsigned char %s_machine[] = {\n", |
| 1310 | 1374 | grammar_name); |