Skip to content

Commit ae44a2f

Browse files
committed
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. Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-By: David Woodhouse <David.Woodhouse@intel.com>
1 parent 459c15e commit ae44a2f

1 file changed

Lines changed: 76 additions & 12 deletions

File tree

‎scripts/asn1_compiler.c‎

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <stdio.h>
1414
#include <stdlib.h>
1515
#include <stdint.h>
16+
#include <stdbool.h>
1617
#include <string.h>
1718
#include <ctype.h>
1819
#include <unistd.h>
@@ -311,9 +312,11 @@ struct token {
311312

312313
static struct token *token_list;
313314
static unsigned nr_tokens;
314-
static _Bool verbose;
315+
static bool verbose_opt;
316+
static bool debug_opt;
315317

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)
317320

318321
static int directive_compare(const void *_key, const void *_pdir)
319322
{
@@ -518,7 +521,7 @@ static void tokenise(char *buffer, char *end)
518521
}
519522

520523
nr_tokens = tix;
521-
debug("Extracted %u tokens\n", nr_tokens);
524+
verbose("Extracted %u tokens\n", nr_tokens);
522525

523526
#if 0
524527
{
@@ -534,6 +537,7 @@ static void tokenise(char *buffer, char *end)
534537

535538
static void build_type_list(void);
536539
static void parse(void);
540+
static void dump_elements(void);
537541
static void render(FILE *out, FILE *hdr);
538542

539543
/*
@@ -548,16 +552,27 @@ int main(int argc, char **argv)
548552
char *kbuild_verbose;
549553
int fd;
550554

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+
551570
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",
553572
argv[0]);
554573
exit(2);
555574
}
556575

557-
kbuild_verbose = getenv("KBUILD_VERBOSE");
558-
if (kbuild_verbose)
559-
verbose = atoi(kbuild_verbose);
560-
561576
filename = argv[1];
562577
outputname = argv[2];
563578
headername = argv[3];
@@ -608,6 +623,7 @@ int main(int argc, char **argv)
608623
tokenise(buffer, buffer + readlen);
609624
build_type_list();
610625
parse();
626+
dump_elements();
611627

612628
out = fopen(outputname, "w");
613629
if (!out) {
@@ -756,7 +772,7 @@ static void build_type_list(void)
756772

757773
qsort(type_index, nr, sizeof(type_index[0]), type_index_compare);
758774

759-
debug("Extracted %u types\n", nr_types);
775+
verbose("Extracted %u types\n", nr_types);
760776
#if 0
761777
for (n = 0; n < nr_types; n++) {
762778
struct type *type = type_index[n];
@@ -801,7 +817,7 @@ static void parse(void)
801817

802818
} while (type++, !(type->flags & TYPE_STOP_MARKER));
803819

804-
debug("Extracted %u actions\n", nr_actions);
820+
verbose("Extracted %u actions\n", nr_actions);
805821
}
806822

807823
static struct element *element_list;
@@ -1192,6 +1208,54 @@ static struct element *parse_compound(struct token **_cursor, struct token *end,
11921208
exit(1);
11931209
}
11941210

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+
11951259
static void render_element(FILE *out, struct element *e, struct element *tag);
11961260
static void render_out_of_line_list(FILE *out);
11971261

@@ -1293,7 +1357,7 @@ static void render(FILE *out, FILE *hdr)
12931357
}
12941358

12951359
/* We do two passes - the first one calculates all the offsets */
1296-
debug("Pass 1\n");
1360+
verbose("Pass 1\n");
12971361
nr_entries = 0;
12981362
root = &type_list[0];
12991363
render_element(NULL, root->element, NULL);
@@ -1304,7 +1368,7 @@ static void render(FILE *out, FILE *hdr)
13041368
e->flags &= ~ELEMENT_RENDERED;
13051369

13061370
/* And then we actually render */
1307-
debug("Pass 2\n");
1371+
verbose("Pass 2\n");
13081372
fprintf(out, "\n");
13091373
fprintf(out, "static const unsigned char %s_machine[] = {\n",
13101374
grammar_name);

0 commit comments

Comments
 (0)