From 0b3301069c7bff1e6de387451ce93de4de8f0469 Mon Sep 17 00:00:00 2001 From: tjr Date: Thu, 29 Jul 2004 09:09:22 +0000 Subject: Add support for multibyte characters. The output is questionable when a character straddles the "start" or "stop" columns, but this should be quite uncommon. --- usr.bin/colrm/colrm.1 | 6 +----- usr.bin/colrm/colrm.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/usr.bin/colrm/colrm.1 b/usr.bin/colrm/colrm.1 index 77782c2..76a86be 100644 --- a/usr.bin/colrm/colrm.1 +++ b/usr.bin/colrm/colrm.1 @@ -32,7 +32,7 @@ .\" @(#)colrm.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" -.Dd July 15, 2004 +.Dd July 29, 2004 .Dt COLRM 1 .Os .Sh NAME @@ -78,7 +78,3 @@ The .Nm command appeared in .Bx 3.0 . -.Sh BUGS -The -.Nm -utility does not recognize multibyte characters. diff --git a/usr.bin/colrm/colrm.c b/usr.bin/colrm/colrm.c index d39df09..52dda90 100644 --- a/usr.bin/colrm/colrm.c +++ b/usr.bin/colrm/colrm.c @@ -50,10 +50,12 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include #include +#include #define TAB 8 @@ -64,9 +66,11 @@ int main(int argc, char *argv[]) { u_long column, start, stop; - int ch; + int ch, width; char *p; + setlocale(LC_ALL, ""); + while ((ch = getopt(argc, argv, "")) != -1) switch(ch) { case '?': @@ -98,8 +102,8 @@ main(int argc, char *argv[]) errx(1, "illegal start and stop columns"); for (column = 0;;) { - switch (ch = getchar()) { - case EOF: + switch (ch = getwchar()) { + case WEOF: check(stdin); break; case '\b': @@ -113,12 +117,13 @@ main(int argc, char *argv[]) column = (column + TAB) & ~(TAB - 1); break; default: - ++column; + if ((width = wcwidth(ch)) > 0) + column += width; break; } if ((!start || column < start || (stop && column > stop)) && - putchar(ch) == EOF) + putwchar(ch) == WEOF) check(stdout); } } -- cgit v1.1