3. DONE Block

CLOSED: [2019-11-14 Thu 17:56] Creates a graph from a block.

<<available_commands>>=
fprintf(stderr, "block: Creates graph from named block.\n");
<<function_declarations>>=
static int dot_block(int argc, char *argv[]);
<<functions>>=
static void print_tree(wmp_core *c, const char *name, int level)
{
    wmp_segment s;
    wmp_block b;
    int next;
    int rc;
    int prog;

    prog = 0; /* TODO: make parameter */
    wmp_block_init(&b);
    rc = wmp_lookup_block(c, name, &b, prog);
    if (!rc) {
        fprintf(stderr,
                "Could not find block '%s'",
                name);
        return;
    }
    wmp_segment_init(&s);
    wmp_find_segment(c, b.head_segment, &s, prog);

    while (1) {
        next = s.nxtseg;
        if (s.id == next || s.id <= 0) {
            wmp_segment_free(&s);
            break;
        }

        if (s.type == 1) {
            printf("\"%s\"->\"%s\"\n", name, s.str);
            print_tree(c, s.str, level + 1);
        }

        wmp_segment_free(&s);
        wmp_segment_init(&s);
        wmp_find_segment(c, next, &s, prog);
    }
    wmp_block_free(&b);
}

static int dot_block(int argc, char *argv[])
{
    wmp_core core;
    int rc;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s NAME\n", argv[0]);
        return 1;
    }

    rc = wmp_core_open(&core, wmp_filename_get());
    if (!rc) return 0;
    printf("digraph {\n");
    print_tree(&core, argv[1], 0);
    printf("}\n");
    wmp_core_close(&core);
    return 0;
}
<<command_parsing>>=
else if (match(argv[1], len, "block", 5)) {
    argv++;
    argc--;
    dot_block(argc, argv);
}



prev | home | next