Ejemplo n.º 1
0
static void nodeset_print (nodeset_t *ns, const char *label, fmt_t fmt)
{
    const char *s;
    switch (fmt) {
        case FMT_RANGED:
            nodeset_config_ranges (ns, true);
            nodeset_config_separator (ns, ',');
            break;
        case FMT_COMMA:
            nodeset_config_ranges (ns, false);
            nodeset_config_separator (ns, ',');
            break;
        case FMT_NEWLINE:
            nodeset_config_ranges (ns, false);
            nodeset_config_separator (ns, '\n');
            break;
    }
    s = nodeset_string (ns);
    if (label) {
        if (fmt == FMT_NEWLINE)
            printf ("%-8s\n%s%s", label, s, strlen (s) > 0 ? "\n" : "");
        else
            printf ("%-8s%s\n", label, s);
    } else {
        if (fmt == FMT_NEWLINE)
            printf ("%s%s", s, strlen (s) > 0 ? "\n" : "");
        else
            printf ("%s\n", s);
    }
}
Ejemplo n.º 2
0
static int cmd_nodeset (optparse_t *p, int ac, char *av[])
{
    int ix = optparse_option_index (p);
    int nsc = ac - ix;
    nodeset_t *nsp, **nsv = nsc > 0 ? xzmalloc (sizeof (nsv[0]) * nsc) : NULL;
    int i;

    for (i = 0; i < nsc; i++)
        if (!(nsv[i] = nodeset_create_string (av[ix + i])))
            log_errn_exit (EINVAL, "%s", av[ix + i]);

    if (optparse_hasopt (p, "intersection"))
        nsp = nsv_intersection (nsc, nsv);
    else
        nsp = nsv_union (nsc, nsv);

    if (optparse_hasopt (p, "subtract")) {
        const char *s = optparse_get_str (p, "subtract", "");
        nodeset_t *ns = nodeset_create_string (s);
        if (!ns)
            log_errn_exit (EINVAL, "%s", s);
        ns_subtract (nsp, ns);
    }

    if (optparse_hasopt (p, "cardinality")) {
        printf ("%" PRIu32 "\n", nsp ? nodeset_count (nsp) : 0);
    } else if (nsp) {
        const char *delim = optparse_get_str (p, "delimiter", "," );
        if (optparse_hasopt (p, "expand")) {
            nodeset_config_ranges (nsp, false);
            nodeset_config_brackets (nsp, false);
        }
        nodeset_config_separator (nsp, delim[0]);
        if (nodeset_count (nsp) > 0)
            printf ("%s\n", nodeset_string (nsp));
    }

    if (nsv) {
        for (i = 0; i < nsc; i++)
            nodeset_destroy (nsv[i]);
        free (nsv);
    }
    return (0);
}