11. Tree

Like blklist, only recursively prints blocks

<<available_commands>>=
fprintf(stderr, "tree: recursively print sub blocks\n");
<<function_declarations>>=
static int get_tree(int argc, char *argv[]);
<<functions>>=
static void print_tree(wmp_core *c,
                       const char *name,
                       int level,
                       int prog)
{
    wmp_segment s;
    wmp_block b;
    int next;
    int n;
    int rc;

    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) {
            for(n = 0; n < level; n++) printf(" ");
            printf("%s\n", s.str);
            print_tree(c, s.str, level + 1, prog);
        }

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

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

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

    rc = wmp_core_open(&core, wmp_filename_get());
    if (!rc) return 0;
    prog = atoi(argv[2]);
    print_tree(&core, argv[1], 0, prog);
    wmp_core_close(&core);
    return 0;
}
<<command_parsing>>=
else if (match(argv[1], len, "tree", 4)) {
    argv++;
    argc--;
    get_tree(argc, argv);
}



prev | home | next