Convert to use libxo.
Obtained from: Phil Shafer <phil@juniper.net> Sponsored by: Juniper Networks, Inc.
This commit is contained in:
parent
39341fd090
commit
6711c4827a
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=274125
@ -2,4 +2,7 @@
|
|||||||
# $FreeBSD$
|
# $FreeBSD$
|
||||||
|
|
||||||
PROG= wc
|
PROG= wc
|
||||||
|
DPADD= ${LIBXO}
|
||||||
|
LDADD= -lxo
|
||||||
|
|
||||||
.include <bsd.prog.mk>
|
.include <bsd.prog.mk>
|
||||||
|
@ -57,10 +57,12 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#include <wctype.h>
|
#include <wctype.h>
|
||||||
|
#include <libxo/xo.h>
|
||||||
|
|
||||||
static uintmax_t tlinect, twordct, tcharct, tlongline;
|
static uintmax_t tlinect, twordct, tcharct, tlongline;
|
||||||
static int doline, doword, dochar, domulti, dolongline;
|
static int doline, doword, dochar, domulti, dolongline;
|
||||||
static volatile sig_atomic_t siginfo;
|
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,
|
static void show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
|
||||||
uintmax_t charct, uintmax_t llct);
|
uintmax_t charct, uintmax_t llct);
|
||||||
@ -81,6 +83,10 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
(void) setlocale(LC_CTYPE, "");
|
(void) setlocale(LC_CTYPE, "");
|
||||||
|
|
||||||
|
argc = xo_parse_args(argc, argv);
|
||||||
|
if (argc < 0)
|
||||||
|
return (argc);
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "clmwL")) != -1)
|
while ((ch = getopt(argc, argv, "clmwL")) != -1)
|
||||||
switch((char)ch) {
|
switch((char)ch) {
|
||||||
case 'l':
|
case 'l':
|
||||||
@ -113,21 +119,35 @@ main(int argc, char *argv[])
|
|||||||
if (doline + doword + dochar + domulti + dolongline == 0)
|
if (doline + doword + dochar + domulti + dolongline == 0)
|
||||||
doline = doword = dochar = 1;
|
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;
|
errors = 0;
|
||||||
total = 0;
|
total = 0;
|
||||||
if (!*argv) {
|
if (!*argv) {
|
||||||
|
xo_open_instance("file");
|
||||||
if (cnt((char *)NULL) != 0)
|
if (cnt((char *)NULL) != 0)
|
||||||
++errors;
|
++errors;
|
||||||
|
xo_close_instance("file");
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
|
xo_open_instance("file");
|
||||||
if (cnt(*argv) != 0)
|
if (cnt(*argv) != 0)
|
||||||
++errors;
|
++errors;
|
||||||
|
xo_close_instance("file");
|
||||||
++total;
|
++total;
|
||||||
} while(*++argv);
|
} while(*++argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (total > 1)
|
if (total > 1) {
|
||||||
|
xo_open_container("total");
|
||||||
show_cnt("total", tlinect, twordct, tcharct, tlongline);
|
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);
|
exit(errors == 0 ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,27 +155,29 @@ static void
|
|||||||
show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
|
show_cnt(const char *file, uintmax_t linect, uintmax_t wordct,
|
||||||
uintmax_t charct, uintmax_t llct)
|
uintmax_t charct, uintmax_t llct)
|
||||||
{
|
{
|
||||||
FILE *out;
|
xo_handle_t *xop;
|
||||||
|
|
||||||
if (!siginfo)
|
if (!siginfo)
|
||||||
out = stdout;
|
xop = NULL;
|
||||||
else {
|
else {
|
||||||
out = stderr;
|
xop = stderr_handle;
|
||||||
siginfo = 0;
|
siginfo = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
xo_emit("{ek:filename/%s}", file);
|
||||||
|
|
||||||
if (doline)
|
if (doline)
|
||||||
(void)fprintf(out, " %7ju", linect);
|
xo_emit_h(xop, " {:lines/%7ju/%ju}", linect);
|
||||||
if (doword)
|
if (doword)
|
||||||
(void)fprintf(out, " %7ju", wordct);
|
xo_emit_h(xop, " {:words/%7ju/%ju}", wordct);
|
||||||
if (dochar || domulti)
|
if (dochar || domulti)
|
||||||
(void)fprintf(out, " %7ju", charct);
|
xo_emit_h(xop, " {:characters/%7ju/%ju}", charct);
|
||||||
if (dolongline)
|
if (dolongline)
|
||||||
(void)fprintf(out, " %7ju", llct);
|
xo_emit_h(xop, " {:long-lines/%7ju/%ju}", llct);
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
(void)fprintf(out, " %s\n", file);
|
xo_emit_h(xop, " {d:filename/%s}\n", file);
|
||||||
else
|
else
|
||||||
(void)fprintf(out, "\n");
|
xo_emit_h(xop, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -176,7 +198,7 @@ cnt(const char *file)
|
|||||||
fd = STDIN_FILENO;
|
fd = STDIN_FILENO;
|
||||||
else {
|
else {
|
||||||
if ((fd = open(file, O_RDONLY, 0)) < 0) {
|
if ((fd = open(file, O_RDONLY, 0)) < 0) {
|
||||||
warn("%s: open", file);
|
xo_warn("%s: open", file);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
if (doword || (domulti && MB_CUR_MAX != 1))
|
if (doword || (domulti && MB_CUR_MAX != 1))
|
||||||
@ -189,7 +211,7 @@ cnt(const char *file)
|
|||||||
if (doline) {
|
if (doline) {
|
||||||
while ((len = read(fd, buf, MAXBSIZE))) {
|
while ((len = read(fd, buf, MAXBSIZE))) {
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
warn("%s: read", file);
|
xo_warn("%s: read", file);
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
@ -224,7 +246,7 @@ cnt(const char *file)
|
|||||||
*/
|
*/
|
||||||
if (dochar || domulti) {
|
if (dochar || domulti) {
|
||||||
if (fstat(fd, &sb)) {
|
if (fstat(fd, &sb)) {
|
||||||
warn("%s: fstat", file);
|
xo_warn("%s: fstat", file);
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
@ -244,7 +266,7 @@ word: gotsp = 1;
|
|||||||
memset(&mbs, 0, sizeof(mbs));
|
memset(&mbs, 0, sizeof(mbs));
|
||||||
while ((len = read(fd, buf, MAXBSIZE)) != 0) {
|
while ((len = read(fd, buf, MAXBSIZE)) != 0) {
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
warn("%s: read", file != NULL ? file : "stdin");
|
xo_warn("%s: read", file != NULL ? file : "stdin");
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
@ -259,7 +281,7 @@ word: gotsp = 1;
|
|||||||
(size_t)-1) {
|
(size_t)-1) {
|
||||||
if (!warned) {
|
if (!warned) {
|
||||||
errno = EILSEQ;
|
errno = EILSEQ;
|
||||||
warn("%s",
|
xo_warn("%s",
|
||||||
file != NULL ? file : "stdin");
|
file != NULL ? file : "stdin");
|
||||||
warned = 1;
|
warned = 1;
|
||||||
}
|
}
|
||||||
@ -291,7 +313,7 @@ word: gotsp = 1;
|
|||||||
}
|
}
|
||||||
if (domulti && MB_CUR_MAX > 1)
|
if (domulti && MB_CUR_MAX > 1)
|
||||||
if (mbrtowc(NULL, NULL, 0, &mbs) == (size_t)-1 && !warned)
|
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)
|
if (doline)
|
||||||
tlinect += linect;
|
tlinect += linect;
|
||||||
if (doword)
|
if (doword)
|
||||||
@ -310,6 +332,6 @@ word: gotsp = 1;
|
|||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
(void)fprintf(stderr, "usage: wc [-Lclmw] [file ...]\n");
|
xo_error("usage: wc [-Lclmw] [file ...]\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user