From owner-svn-src-projects@freebsd.org Sun Aug 5 04:16:01 2018 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4BAE51062F83 for ; Sun, 5 Aug 2018 04:16:01 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 0265378736; Sun, 5 Aug 2018 04:16:01 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D78A9194A2; Sun, 5 Aug 2018 04:16:00 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w754G03M047148; Sun, 5 Aug 2018 04:16:00 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w754G0Ms047147; Sun, 5 Aug 2018 04:16:00 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201808050416.w754G0Ms047147@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Sun, 5 Aug 2018 04:16:00 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r337341 - projects/bectl/sbin/bectl X-SVN-Group: projects X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: projects/bectl/sbin/bectl X-SVN-Commit-Revision: 337341 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Aug 2018 04:16:01 -0000 Author: kevans Date: Sun Aug 5 04:16:00 2018 New Revision: 337341 URL: https://svnweb.freebsd.org/changeset/base/337341 Log: bectl(8): Take -H parameter to list into account -H is for a scripted format, where all fields are tab-delimited and the headers go away. We accomplish this by splitting out pad printing to a separate function that'll take into account whether we're supposed to be scripted or not. This has the nice side effect of maintaining positive column sizes again. Modified: projects/bectl/sbin/bectl/bectl.c Modified: projects/bectl/sbin/bectl/bectl.c ============================================================================== --- projects/bectl/sbin/bectl/bectl.c Sat Aug 4 22:41:17 2018 (r337340) +++ projects/bectl/sbin/bectl/bectl.c Sun Aug 5 04:16:00 2018 (r337341) @@ -59,7 +59,7 @@ struct printc { int current_indent; int mount_colsz; int space_colsz; - bool hide_headers; + bool script_fmt; bool show_all_datasets; bool show_snaps; bool show_space; @@ -73,6 +73,7 @@ static int bectl_cmd_import(int argc, char *argv[]); static int bectl_cmd_add(int argc, char *argv[]); static int bectl_cmd_jail(int argc, char *argv[]); static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops); +static void print_padding(const char *fval, int colsz, struct printc *pc); static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc); static void print_headers(nvlist_t *props, struct printc *pc); static int bectl_cmd_list(int argc, char *argv[]); @@ -456,7 +457,20 @@ get_origin_props(nvlist_t *dsprops, nvlist_t **originp return (NULL); } +static void +print_padding(const char *fval, int colsz, struct printc *pc) +{ + if (pc->script_fmt) { + printf("\t"); + return; + } + + if (fval != NULL) + colsz -= strlen(fval); + printf("%*s ", colsz, ""); +} + static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc) { @@ -470,8 +484,7 @@ print_info(const char *name, nvlist_t *dsprops, struct boolean_t active_now, active_reboot; originprops = NULL; - printf("%*s%*s ", pc->current_indent, "", - pc->be_colsz + pc->current_indent, name); + printf("%*s%s", pc->current_indent, "", name); /* Recurse at the base level if we're breaking info down */ if (pc->current_indent == 0 && (pc->show_all_datasets || @@ -489,7 +502,8 @@ print_info(const char *name, nvlist_t *dsprops, struct } pc->current_indent = 0; return; - } + } else + print_padding(name, pc->be_colsz - pc->current_indent, pc); active_colsz = pc->active_colsz_def; if (nvlist_lookup_boolean_value(dsprops, "active", @@ -506,11 +520,14 @@ print_info(const char *name, nvlist_t *dsprops, struct printf("-"); active_colsz--; } - printf("%*s ", -active_colsz, " "); - if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0) - printf("%*s ", pc->mount_colsz, propstr); - else - printf("%*s ", pc->mount_colsz, "-"); + print_padding(NULL, active_colsz, pc); + if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0){ + printf("%s", propstr); + print_padding(propstr, pc->mount_colsz, pc); + } else { + printf("%s", "-"); + print_padding("-", pc->mount_colsz, pc); + } get_origin_props(dsprops, &originprops); @@ -524,9 +541,12 @@ print_info(const char *name, nvlist_t *dsprops, struct /* Alas, there's more to it,. */ humanize_number(buf, 6, space, "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE | HN_B); - printf("%*s ", pc->space_colsz, buf); - } else - printf("%*s ", pc->space_colsz, "-"); + printf("%s", buf); + print_padding(buf, pc->space_colsz, pc); + } else { + printf("%s", "-"); + print_padding("-", pc->space_colsz, pc); + } if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) { ctimenum = strtoull(propstr, NULL, 10); @@ -573,13 +593,11 @@ print_headers(nvlist_t *props, struct printc *pc) strlen(propstr) + INDENT_INCREMENT * 2); } - pc->be_colsz = -be_maxcol; - /* To be made negative after calculating final col sz */ + pc->be_colsz = be_maxcol; pc->active_colsz_def = strlen(HEADER_ACTIVE); - pc->mount_colsz = -(int)strlen(HEADER_MOUNT); - pc->space_colsz = -(int)strlen(HEADER_SPACE); - /* XXX TODO: Take -H into account */ - printf("%*s %s %s %s %s\n", pc->be_colsz, chosen_be_header, + pc->mount_colsz = strlen(HEADER_MOUNT); + pc->space_colsz = strlen(HEADER_SPACE); + printf("%*s %s %s %s %s\n", -pc->be_colsz, chosen_be_header, HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED); /* @@ -612,7 +630,7 @@ bectl_cmd_list(int argc, char *argv[]) pc.show_space = true; break; case 'H': - pc.hide_headers = true; + pc.script_fmt = true; break; case 's': pc.show_snaps = true; @@ -641,7 +659,8 @@ bectl_cmd_list(int argc, char *argv[]) return (1); } - print_headers(props, &pc); + if (!pc.script_fmt) + print_headers(props, &pc); /* Do a first pass to print active and next active first */ for (cur = nvlist_next_nvpair(props, NULL); cur != NULL; cur = nvlist_next_nvpair(props, cur)) {