From owner-svn-src-all@FreeBSD.ORG Wed Nov 5 04:02:26 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 68353B23; Wed, 5 Nov 2014 04:02:26 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 49E7F81B; Wed, 5 Nov 2014 04:02:26 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id sA542Qcf007681; Wed, 5 Nov 2014 04:02:26 GMT (envelope-from marcel@FreeBSD.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id sA542P9g007678; Wed, 5 Nov 2014 04:02:25 GMT (envelope-from marcel@FreeBSD.org) Message-Id: <201411050402.sA542P9g007678@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: marcel set sender to marcel@FreeBSD.org using -f From: Marcel Moolenaar Date: Wed, 5 Nov 2014 04:02:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r274125 - head/usr.bin/wc X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Nov 2014 04:02:26 -0000 Author: marcel Date: Wed Nov 5 04:02:25 2014 New Revision: 274125 URL: https://svnweb.freebsd.org/changeset/base/274125 Log: Convert to use libxo. Obtained from: Phil Shafer Sponsored by: Juniper Networks, Inc. Modified: head/usr.bin/wc/Makefile head/usr.bin/wc/wc.c Modified: head/usr.bin/wc/Makefile ============================================================================== --- head/usr.bin/wc/Makefile Wed Nov 5 02:58:02 2014 (r274124) +++ head/usr.bin/wc/Makefile Wed Nov 5 04:02:25 2014 (r274125) @@ -2,4 +2,7 @@ # $FreeBSD$ PROG= wc +DPADD= ${LIBXO} +LDADD= -lxo + .include Modified: head/usr.bin/wc/wc.c ============================================================================== --- head/usr.bin/wc/wc.c Wed Nov 5 02:58:02 2014 (r274124) +++ head/usr.bin/wc/wc.c Wed Nov 5 04:02:25 2014 (r274125) @@ -57,10 +57,12 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include static uintmax_t tlinect, twordct, tcharct, tlongline; static int doline, doword, dochar, domulti, dolongline; static volatile sig_atomic_t siginfo; +static xo_handle_t *stderr_handle; static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct, uintmax_t charct, uintmax_t llct); @@ -81,6 +83,10 @@ main(int argc, char *argv[]) (void) setlocale(LC_CTYPE, ""); + argc = xo_parse_args(argc, argv); + if (argc < 0) + return (argc); + while ((ch = getopt(argc, argv, "clmwL")) != -1) switch((char)ch) { case 'l': @@ -113,21 +119,35 @@ main(int argc, char *argv[]) if (doline + doword + dochar + domulti + dolongline == 0) doline = doword = dochar = 1; + stderr_handle = xo_create_to_file(stderr, XO_STYLE_TEXT, 0); + xo_open_container("wc"); + xo_open_list("file"); + errors = 0; total = 0; if (!*argv) { + xo_open_instance("file"); if (cnt((char *)NULL) != 0) ++errors; + xo_close_instance("file"); } else { do { + xo_open_instance("file"); if (cnt(*argv) != 0) ++errors; + xo_close_instance("file"); ++total; } while(*++argv); } - if (total > 1) + if (total > 1) { + xo_open_container("total"); show_cnt("total", tlinect, twordct, tcharct, tlongline); + xo_close_container("total"); + } + xo_close_list("file"); + xo_close_container("wc"); + xo_finish(); exit(errors == 0 ? 0 : 1); } @@ -135,27 +155,29 @@ static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct, uintmax_t charct, uintmax_t llct) { - FILE *out; + xo_handle_t *xop; if (!siginfo) - out = stdout; + xop = NULL; else { - out = stderr; + xop = stderr_handle; siginfo = 0; } + xo_emit("{ek:filename/%s}", file); + if (doline) - (void)fprintf(out, " %7ju", linect); + xo_emit_h(xop, " {:lines/%7ju/%ju}", linect); if (doword) - (void)fprintf(out, " %7ju", wordct); + xo_emit_h(xop, " {:words/%7ju/%ju}", wordct); if (dochar || domulti) - (void)fprintf(out, " %7ju", charct); + xo_emit_h(xop, " {:characters/%7ju/%ju}", charct); if (dolongline) - (void)fprintf(out, " %7ju", llct); + xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct); if (file != NULL) - (void)fprintf(out, " %s\n", file); + xo_emit_h(xop, " {d:filename/%s}\n", file); else - (void)fprintf(out, "\n"); + xo_emit_h(xop, "\n"); } static int @@ -176,7 +198,7 @@ cnt(const char *file) fd = STDIN_FILENO; else { if ((fd = open(file, O_RDONLY, 0)) < 0) { - warn("%s: open", file); + xo_warn("%s: open", file); return (1); } if (doword || (domulti && MB_CUR_MAX != 1)) @@ -189,7 +211,7 @@ cnt(const char *file) if (doline) { while ((len = read(fd, buf, MAXBSIZE))) { if (len == -1) { - warn("%s: read", file); + xo_warn("%s: read", file); (void)close(fd); return (1); } @@ -224,7 +246,7 @@ cnt(const char *file) */ if (dochar || domulti) { if (fstat(fd, &sb)) { - warn("%s: fstat", file); + xo_warn("%s: fstat", file); (void)close(fd); return (1); } @@ -244,7 +266,7 @@ word: gotsp = 1; memset(&mbs, 0, sizeof(mbs)); while ((len = read(fd, buf, MAXBSIZE)) != 0) { if (len == -1) { - warn("%s: read", file != NULL ? file : "stdin"); + xo_warn("%s: read", file != NULL ? file : "stdin"); (void)close(fd); return (1); } @@ -259,7 +281,7 @@ word: gotsp = 1; (size_t)-1) { if (!warned) { errno = EILSEQ; - warn("%s", + xo_warn("%s", file != NULL ? file : "stdin"); warned = 1; } @@ -291,7 +313,7 @@ word: gotsp = 1; } if (domulti && MB_CUR_MAX > 1) if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned) - warn("%s", file != NULL ? file : "stdin"); + xo_warn("%s", file != NULL ? file : "stdin"); if (doline) tlinect += linect; if (doword) @@ -310,6 +332,6 @@ word: gotsp = 1; static void usage(void) { - (void)fprintf(stderr, "usage: wc [-Lclmw] [file ...]\n"); + xo_error("usage: wc [-Lclmw] [file ...]\n"); exit(1); }