static void __attribute__((__noreturn__)) usage(FILE *out)
{
fprintf(out, " %s [options] [<dir> ...]\n\n", program_invocation_short_name);
- fputs(" -c, --csv display a csv-like output\n", out);
- fputs(" -i, --ascii use ascii characters only\n", out);
- fputs(" -l, --list use list format output\n", out);
- fputs(" -n, --noheadings don't print headings\n", out);
- fputs(" -p, --pairs use key=\"value\" output format\n", out);
- fputs(" -J, --json use JSON output format\n", out);
- fputs(" -r, --raw use raw output format\n", out);
+ fputs(" -c, --csv display a csv-like output\n", out);
+ fputs(" -i, --ascii use ascii characters only\n", out);
+ fputs(" -l, --list use list format output\n", out);
+ fputs(" -n, --noheadings don't print headings\n", out);
+ fputs(" -p, --pairs use key=\"value\" output format\n", out);
+ fputs(" -J, --json use JSON output format\n", out);
+ fputs(" -r, --raw use raw output format\n", out);
+ fputs(" -S, --range-start <n> first line to print\n", out);
+ fputs(" -E, --range-end <n> last line to print\n", out);
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
struct libscols_table *tb;
- int c, notree = 0;
+ int c, notree = 0, nstart = -1, nend = -1;
+
static const struct option longopts[] = {
{ "ascii", 0, 0, 'i' },
{ "pairs", 0, 0, 'p' },
{ "json", 0, 0, 'J' },
{ "raw", 0, 0, 'r' },
-
+ { "range-start",1, 0, 'S' },
+ { "range-end", 1, 0, 'E' },
{ NULL, 0, 0, 0 },
};
if (!tb)
err(EXIT_FAILURE, "faild to create output table");
- while((c = getopt_long(argc, argv, "ciJlnpr", longopts, NULL)) != -1) {
+ while((c = getopt_long(argc, argv, "ciJlnprS:E:", longopts, NULL)) != -1) {
switch(c) {
case 'c':
scols_table_set_column_separator(tb, ",");
scols_table_enable_raw(tb, 1);
notree = 1;
break;
+ case 'S':
+ nstart = strtos32_or_err(optarg, "failed to parse range start") - 1;
+ break;
+ case 'E':
+ nend = strtos32_or_err(optarg, "failed to parse range end") - 1;
+ break;
default:
usage(stderr);
}
else while (optind < argc)
add_lines(tb, argv[optind++]);
- scols_print_table(tb);
- scols_unref_table(tb);
+ if (nstart >= 0 || nend >= 0) {
+ /* print subset */
+ struct libscols_line *start = NULL, *end = NULL;
+
+ if (nstart >= 0)
+ start = scols_table_get_line(tb, nstart);
+ if (nend >= 0)
+ end = scols_table_get_line(tb, nend);
+ if (start || end)
+ scols_table_print_range(tb, start, end);
+ } else
+ /* print all table */
+ scols_print_table(tb);
+
+ scols_unref_table(tb);
return EXIT_SUCCESS;
}
return rc;
}
-static int print_table(struct libscols_table *tb, struct libscols_buffer *buf)
+static int print_range( struct libscols_table *tb,
+ struct libscols_buffer *buf,
+ struct libscols_iter *itr,
+ struct libscols_line *end)
{
int rc = 0;
struct libscols_line *ln;
- struct libscols_iter itr;
assert(tb);
- scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
- while (rc == 0 && scols_table_next_line(tb, &itr, &ln) == 0) {
+ while (rc == 0 && scols_table_next_line(tb, itr, &ln) == 0) {
+
fput_line_open(tb);
rc = print_line(tb, ln, buf);
- fput_line_close(tb, scols_iter_is_last(&itr));
+ fput_line_close(tb, scols_iter_is_last(itr));
+
+ if (end && ln == end)
+ break;
}
return rc;
+
}
+static int print_table(struct libscols_table *tb, struct libscols_buffer *buf)
+{
+ struct libscols_iter itr;
+
+ scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+ return print_range(tb, buf, &itr, NULL);
+}
+
+
static int print_tree_line(struct libscols_table *tb,
struct libscols_line *ln,
struct libscols_buffer *buf,
return sz;
}
-
-
-/**
- * scols_print_table:
- * @tb: table
- *
- * Prints the table to the output stream.
- *
- * Returns: 0, a negative value in case of an error.
- */
-int scols_print_table(struct libscols_table *tb)
+static int initialize_printting(struct libscols_table *tb, struct libscols_buffer **buf)
{
- int rc = 0;
size_t bufsz, extra_bufsz = 0;
struct libscols_line *ln;
struct libscols_iter itr;
- struct libscols_buffer *buf;
-
- if (!tb)
- return -EINVAL;
-
- DBG(TAB, ul_debugobj(tb, "printing"));
+ int rc;
- if (list_empty(&tb->tb_lines)) {
- DBG(TAB, ul_debugobj(tb, "ignore -- epmty table"));
- return 0;
- }
+ DBG(TAB, ul_debugobj(tb, "initialize printting"));
if (!tb->symbols)
scols_table_set_symbols(tb, NULL); /* use default */
case SCOLS_FMT_EXPORT:
{
struct libscols_column *cl;
+
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+
while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
if (scols_column_is_hidden(cl))
continue;
break;
}
-
/*
* Enlarge buffer if necessary, the buffer should be large enough to
* store line data and tree ascii art (or another decoration).
bufsz = sz;
}
- buf = new_buffer(bufsz + 1); /* data + space for \0 */
- if (!buf)
+ *buf = new_buffer(bufsz + 1); /* data + space for \0 */
+ if (!*buf)
return -ENOMEM;
if (tb->format == SCOLS_FMT_HUMAN) {
- rc = recount_widths(tb, buf);
+ rc = recount_widths(tb, *buf);
if (rc != 0)
- goto done;
+ goto err;
}
+ return 0;
+err:
+ free_buffer(*buf);
+ return rc;
+}
+
+/**
+ * scola_table_print_range:
+ * @tb: table
+ * @start: first printed line or NULL to print from the beggin of the table
+ * @end: last printed line or NULL to print all from start.
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_table_print_range( struct libscols_table *tb,
+ struct libscols_line *start,
+ struct libscols_line *end)
+{
+ struct libscols_buffer *buf;
+ struct libscols_iter itr;
+ int rc;
+
+ if (scols_table_is_tree(tb))
+ return -EINVAL;
+
+ DBG(TAB, ul_debugobj(tb, "printing range"));
+
+ rc = initialize_printting(tb, &buf);
+ if (rc)
+ return rc;
+
+ if (start) {
+ itr.direction = SCOLS_ITER_FORWARD;
+ itr.head = &tb->tb_lines;
+ itr.p = &start->ln_lines;
+ } else
+ scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+
+ rc = print_range(tb, buf, &itr, end);
+
+ free_buffer(buf);
+ return 0;
+}
+
+/**
+ * scols_print_table:
+ * @tb: table
+ *
+ * Prints the table to the output stream.
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_print_table(struct libscols_table *tb)
+{
+ int rc = 0;
+ struct libscols_buffer *buf;
+
+ if (!tb)
+ return -EINVAL;
+
+ DBG(TAB, ul_debugobj(tb, "printing"));
+
+ if (list_empty(&tb->tb_lines)) {
+ DBG(TAB, ul_debugobj(tb, "ignore -- epmty table"));
+ return 0;
+ }
+
+ rc = initialize_printting(tb, &buf);
+ if (rc)
+ return rc;
+
fput_table_open(tb);
if (tb->format == SCOLS_FMT_HUMAN)