History

ASN.1: Add an ASN.1 compiler option to dump the element tree

Add an ASN.1 compiler option to dump the element tree to stdout.

David Howells committed 11 years ago

Commit ae44a2f1 parent 459c15e1 file · +76 −12Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/asn1_compiler.c+76−12
@@ -13,6 +13,7 @@ · 12 unmodified lines
13#include <stdio.h>13#include <stdio.h>
14#include <stdlib.h>14#include <stdlib.h>
15#include <stdint.h>15#include <stdint.h>
16#include <stdbool.h>
16#include <string.h>17#include <string.h>
17#include <ctype.h>18#include <ctype.h>
18#include <unistd.h>19#include <unistd.h>
@@ -311,9 +312,11 @@ struct token { · 292 unmodified lines
311 312
312static struct token *token_list;313static struct token *token_list;
313static unsigned nr_tokens;314static unsigned nr_tokens;
314static _Bool verbose;315static bool verbose_opt;
316static 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
318static int directive_compare(const void *_key, const void *_pdir)321static 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 nr_tokens = tix;523 nr_tokens = tix;
521 debug("Extracted %u tokens\n", nr_tokens);524 verbose("Extracted %u tokens\n", nr_tokens);
522 525
523#if 0526#if 0
524 {527 {
@@ -534,6 +537,7 @@ static void tokenise(char *buffer, char *end) · 9 unmodified lines
534 537
535static void build_type_list(void);538static void build_type_list(void);
536static void parse(void);539static void parse(void);
540static void dump_elements(void);
537static void render(FILE *out, FILE *hdr);541static void render(FILE *out, FILE *hdr);
538 542
539/*543/*
@@ -548,16 +552,27 @@ int main(int argc, char **argv) · 8 unmodified lines
548 char *kbuild_verbose;552 char *kbuild_verbose;
549 int fd;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 if (argc != 4) {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 argv[0]);572 argv[0]);
554 exit(2);573 exit(2);
555 }574 }
556 575
557 kbuild_verbose = getenv("KBUILD_VERBOSE");
558 if (kbuild_verbose)
559 verbose = atoi(kbuild_verbose);
560
561 filename = argv[1];576 filename = argv[1];
562 outputname = argv[2];577 outputname = argv[2];
563 headername = argv[3];578 headername = argv[3];
@@ -608,6 +623,7 @@ int main(int argc, char **argv) · 44 unmodified lines
608 tokenise(buffer, buffer + readlen);623 tokenise(buffer, buffer + readlen);
609 build_type_list();624 build_type_list();
610 parse();625 parse();
626 dump_elements();
611 627
612 out = fopen(outputname, "w");628 out = fopen(outputname, "w");
613 if (!out) {629 if (!out) {
@@ -756,7 +772,7 @@ static void build_type_list(void) · 142 unmodified lines
756 772
757 qsort(type_index, nr, sizeof(type_index[0]), type_index_compare);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#if 0776#if 0
761 for (n = 0; n < nr_types; n++) {777 for (n = 0; n < nr_types; n++) {
762 struct type *type = type_index[n];778 struct type *type = type_index[n];
@@ -801,7 +817,7 @@ static void parse(void) · 38 unmodified lines
801 817
802 } while (type++, !(type->flags & TYPE_STOP_MARKER));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
807static struct element *element_list;823static struct element *element_list;
@@ -1192,6 +1208,54 @@ overrun_error: · 384 unmodified lines
1192 exit(1);1208 exit(1);
1193}1209}
1194 1210
1211static 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
1253static void dump_elements(void)
1254{
1255 if (debug_opt)
1256 dump_element(type_list[0].element, 0);
1257}
1258
1195static void render_element(FILE *out, struct element *e, struct element *tag);1259static void render_element(FILE *out, struct element *e, struct element *tag);
1196static void render_out_of_line_list(FILE *out);1260static 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 /* We do two passes - the first one calculates all the offsets */1359 /* We do two passes - the first one calculates all the offsets */
1296 debug("Pass 1\n");1360 verbose("Pass 1\n");
1297 nr_entries = 0;1361 nr_entries = 0;
1298 root = &type_list[0];1362 root = &type_list[0];
1299 render_element(NULL, root->element, NULL);1363 render_element(NULL, root->element, NULL);
@@ -1304,7 +1368,7 @@ static void render(FILE *out, FILE *hdr) · 4 unmodified lines
1304 e->flags &= ~ELEMENT_RENDERED;1368 e->flags &= ~ELEMENT_RENDERED;
1305 1369
1306 /* And then we actually render */1370 /* And then we actually render */
1307 debug("Pass 2\n");1371 verbose("Pass 2\n");
1308 fprintf(out, "\n");1372 fprintf(out, "\n");
1309 fprintf(out, "static const unsigned char %s_machine[] = {\n",1373 fprintf(out, "static const unsigned char %s_machine[] = {\n",
1310 grammar_name);1374 grammar_name);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t