}
scols_groups_reset_state(tb);
- DBG(TAB, ul_debugobj(tb, "<- done grpset calculate [top-level]"));
+ DBG(TAB, ul_debugobj(tb, "<- done grpset calculate [top-level, rc=%d, size=%zu]",
+ rc, tb->grpset_size));
return rc;
}
DBG(TAB, ul_debugobj(tb, " zeroize grpset"));
memset(tb->grpset, 0, tb->grpset_size * sizeof(struct libscols_group *));
}
+ tb->ngrpchlds_pending = 0;
}
static void add_member(struct libscols_group *gr, struct libscols_line *ln)
scols_ref_line(ln);
}
+/*
+ * Returns first group which is ready to print group children.
+ *
+ * This function scans grpset[] in backward order and returns first group
+ * with SCOLS_GSTATE_CONT_CHILDREN or SCOLS_GSTATE_LAST_MEMBER state.
+ */
+struct libscols_group *scols_grpset_get_printable_children(struct libscols_table *tb)
+{
+ size_t i;
+
+ for (i = tb->grpset_size; i > 0; i -= SCOLS_GRPSET_CHUNKSIZ) {
+ struct libscols_group *gr = tb->grpset[i-1];
+
+ if (gr == NULL)
+ continue;
+ if (gr->state == SCOLS_GSTATE_CONT_CHILDREN ||
+ gr->state == SCOLS_GSTATE_LAST_MEMBER)
+ return gr;
+ }
+
+ return NULL;
+}
+
+
/**
* scols_table_group_lines:
* @tb: a pointer to a struct libscols_table instance
goto done;
children = has_children(ln);
- gr_children = is_last_group_member(ln) && has_group_children(ln);
- if (children || gr_children)
- fput_children_open(tb);
+ /* we print group children in __scols_print_tree() after tree is printed */
+ gr_children = is_last_group_member(ln) && has_group_children(ln);
+ if (gr_children) {
+ last_in_table = 0;
+ tb->ngrpchlds_pending++;
+ }
- /* print children */
if (children) {
struct list_head *p;
DBG(LINE, ul_debugobj(ln, " printing children"));
+ fput_children_open(tb);
list_for_each(p, &ln->ln_branch) {
struct libscols_line *chld =
list_entry(p, struct libscols_line, ln_children);
- int last_child = !gr_children && p->next == &ln->ln_branch;
-
- rc = print_tree_line(tb, chld, buf, last_child, last_in_table && last_child);
- if (rc)
- goto done;
- }
- }
-
- /* print group's children */
- if (gr_children) {
- struct list_head *p;
-
- DBG(LINE, ul_debugobj(ln, " printing group children"));
-
- list_for_each(p, &ln->group->gr_children) {
- struct libscols_line *chld =
- list_entry(p, struct libscols_line, ln_children);
- int last_child = p->next == &ln->group->gr_children;
+ int last_child = p->next == &ln->ln_branch;
rc = print_tree_line(tb, chld, buf, last_child, last_in_table && last_child);
if (rc)
goto done;
}
- }
-
- if (children || gr_children)
fput_children_close(tb);
+ }
- if ((!children && !gr_children) || scols_table_is_json(tb))
+ if (!children || scols_table_is_json(tb))
fput_line_close(tb, last, last_in_table);
done:
DBG(LINE, ul_debugobj(ln, "<- print tree line [rc=%d]", rc));
if (ln->parent || ln->parent_group)
continue;
rc = print_tree_line(tb, ln, buf, ln == last, ln == last);
- }
+ if (rc)
+ break;
+
+ DBG(LINE, ul_debugobj(ln, " pending groups: %zu", tb->ngrpchlds_pending));
+
+ /* print group's children */
+ while (tb->ngrpchlds_pending) {
+ struct libscols_group *gr = scols_grpset_get_printable_children(tb);
+ struct list_head *p;
+
+ DBG(LINE, ul_debugobj(ln, " printing group children [pending=%zu]", tb->ngrpchlds_pending));
+ if (!gr) {
+ DBG(LINE, ul_debugobj(ln, " *** ngrpchlds_pending counter invalid"));
+ tb->ngrpchlds_pending = 0;
+ break;
+ }
+ DBG(LINE, ul_debugobj(ln, " printing group children"));
+ tb->ngrpchlds_pending--;
+ list_for_each(p, &gr->gr_children) {
+ struct libscols_line *chld =
+ list_entry(p, struct libscols_line, ln_children);
+ int last_child = p->next == &gr->gr_children;
+
+ rc = print_tree_line(tb, chld, buf, last_child,
+ last_child
+ && ln == last
+ && tb->ngrpchlds_pending == 0);
+ if (rc)
+ goto done;
+ }
+ }
+ }
+done:
return rc;
}