From f88886b084c936c71f33c3f082ef333c4b9777e8 Mon Sep 17 00:00:00 2001 From: Sascha Wildner Date: Tue, 12 Jan 2016 05:33:09 +0100 Subject: [PATCH] expand(1)/unexpand(1): Sync with FreeBSD. * Multibyte character support. * Some cleanup. --- usr.bin/expand/expand.1 | 15 +++++----- usr.bin/expand/expand.c | 46 ++++++++++++++++++----------- usr.bin/unexpand/Makefile | 3 +- usr.bin/unexpand/unexpand.c | 59 +++++++++++++++++++++---------------- 4 files changed, 69 insertions(+), 54 deletions(-) diff --git a/usr.bin/expand/expand.1 b/usr.bin/expand/expand.1 index 144e2597e5..60866d30b1 100644 --- a/usr.bin/expand/expand.1 +++ b/usr.bin/expand/expand.1 @@ -26,10 +26,9 @@ .\" SUCH DAMAGE. .\" .\" @(#)expand.1 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/usr.bin/expand/expand.1,v 1.3.2.7 2003/02/25 00:54:13 trhodes Exp $ -.\" $DragonFly: src/usr.bin/expand/expand.1,v 1.2 2003/06/17 04:29:26 dillon Exp $ +.\" $FreeBSD: head/usr.bin/expand/expand.1 284057 2015-06-06 11:58:19Z bapt $ .\" -.Dd April 21, 2002 +.Dd January 12, 2016 .Dt EXPAND 1 .Os .Sh NAME @@ -46,9 +45,8 @@ .Oc .Op Ar .Nm unexpand -.Op Fl a .Oo -.Fl t +.Fl a | t .Sm off .Ar tab1 , tab2 , ... , tabn .Sm on @@ -78,7 +76,8 @@ The following options are available: .Nm ( unexpand only.) By default, only leading blanks and tabs -are reconverted to maximal strings of tabs. If the +are reconverted to maximal strings of tabs. +If the .Fl a option is given, then tabs are inserted whenever they would compress the resultant file by replacing two or more characters. @@ -111,5 +110,5 @@ utilities conform to .Sh HISTORY The .Nm -command appeared in -.Bx 3.0 . +utility first appeared in +.Bx 1 . diff --git a/usr.bin/expand/expand.c b/usr.bin/expand/expand.c index c151be04d1..c62ec85705 100644 --- a/usr.bin/expand/expand.c +++ b/usr.bin/expand/expand.c @@ -28,8 +28,7 @@ * * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved. * @(#)expand.c 8.1 (Berkeley) 6/9/93 - * $FreeBSD: src/usr.bin/expand/expand.c,v 1.5.2.6 2002/07/09 10:47:59 tjr Exp $ - * $DragonFly: src/usr.bin/expand/expand.c,v 1.4 2005/04/10 20:55:38 drhodus Exp $ + * $FreeBSD: head/usr.bin/expand/expand.c 227238 2011-11-06 18:49:30Z ed $ */ #include @@ -38,22 +37,27 @@ #include #include #include +#include +#include /* * expand - expand tabs to equivalent spaces */ -int nstops; -int tabstops[100]; +static int nstops; +static int tabstops[100]; static void getstops(char *); static void usage(void); int -main(int argc, char **argv) +main(int argc, char *argv[]) { + const char *curfile; + wint_t wc; int c, column; int n; int rval; + int width; setlocale(LC_CTYPE, ""); @@ -87,22 +91,24 @@ main(int argc, char **argv) argc--, argv++; continue; } + curfile = argv[0]; argc--, argv++; - } + } else + curfile = "stdin"; column = 0; - while ((c = getchar()) != EOF) { - switch (c) { + while ((wc = getwchar()) != WEOF) { + switch (wc) { case '\t': if (nstops == 0) { do { - putchar(' '); + putwchar(' '); column++; } while (column & 07); continue; } if (nstops == 1) { do { - putchar(' '); + putwchar(' '); column++; } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1)); continue; @@ -111,12 +117,12 @@ main(int argc, char **argv) if (tabstops[n] > column) break; if (n == nstops) { - putchar(' '); + putwchar(' '); column++; continue; } while (column < tabstops[n]) { - putchar(' '); + putwchar(' '); column++; } continue; @@ -124,21 +130,25 @@ main(int argc, char **argv) case '\b': if (column) column--; - putchar('\b'); + putwchar('\b'); continue; default: - putchar(c); - if (isprint(c)) - column++; + putwchar(wc); + if ((width = wcwidth(wc)) > 0) + column += width; continue; case '\n': - putchar(c); + putwchar(wc); column = 0; continue; } } + if (ferror(stdin)) { + warn("%s", curfile); + rval = 1; + } } while (argc > 0); exit(rval); } @@ -171,6 +181,6 @@ getstops(char *cp) static void usage(void) { - (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n"); + fprintf(stderr, "usage: expand [-t tablist] [file ...]\n"); exit(1); } diff --git a/usr.bin/unexpand/Makefile b/usr.bin/unexpand/Makefile index 73dddae472..e276a4e40f 100644 --- a/usr.bin/unexpand/Makefile +++ b/usr.bin/unexpand/Makefile @@ -1,8 +1,7 @@ # From: @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD: src/usr.bin/unexpand/Makefile,v 1.1.1.1.14.1 2002/07/09 10:35:16 tjr Exp $ -# $DragonFly: src/usr.bin/unexpand/Makefile,v 1.4 2007/08/27 16:51:00 pavalos Exp $ PROG= unexpand -NOMAN= noman +NOMAN= .include diff --git a/usr.bin/unexpand/unexpand.c b/usr.bin/unexpand/unexpand.c index 3d3a38f4d0..3724672475 100644 --- a/usr.bin/unexpand/unexpand.c +++ b/usr.bin/unexpand/unexpand.c @@ -28,8 +28,7 @@ * * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved. * @(#)unexpand.c 8.1 (Berkeley) 6/6/93 - * $FreeBSD: src/usr.bin/unexpand/unexpand.c,v 1.5.2.3 2002/10/11 11:33:23 tjr Exp $ - * $DragonFly: src/usr.bin/unexpand/unexpand.c,v 1.3 2003/10/04 20:36:54 hmp Exp $ + * $FreeBSD: head/usr.bin/unexpand/unexpand.c 227192 2011-11-06 08:18:05Z ed $ */ /* @@ -43,17 +42,19 @@ #include #include #include +#include +#include -int all; -int nstops; -int tabstops[100]; +static int all; +static int nstops; +static int tabstops[100]; static void getstops(const char *); static void usage(void); -static void tabify(void); +static int tabify(const char *); int -main(int argc, char **argv) +main(int argc, char *argv[]) { int ch, failed; char *filename; @@ -81,14 +82,14 @@ main(int argc, char **argv) failed = 0; if (argc == 0) - tabify(); + failed |= tabify("stdin"); else { while ((filename = *argv++) != NULL) { if (freopen(filename, "r", stdin) == NULL) { warn("%s", filename); - failed++; + failed = 1; } else - tabify(); + failed |= tabify(filename); } } exit(failed != 0); @@ -97,19 +98,20 @@ main(int argc, char **argv) static void usage(void) { - fprintf(stderr, "usage: unexpand [-a] [-t tablist] [file ...]\n"); + fprintf(stderr, "usage: unexpand [-a | -t tablist] [file ...]\n"); exit(1); } -static void -tabify(void) +static int +tabify(const char *curfile) { - int ch, dcol, doneline, limit, n, ocol; + int dcol, doneline, limit, n, ocol, width; + wint_t ch; limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1; doneline = ocol = dcol = 0; - while ((ch = getchar()) != EOF) { + while ((ch = getwchar()) != WEOF) { if (ch == ' ' && !doneline) { if (++dcol >= limit) doneline = 1; @@ -137,7 +139,7 @@ tabify(void) <= (dcol / tabstops[0])) { if (dcol - ocol < 2) break; - putchar('\t'); + putwchar('\t'); ocol = (1 + ocol / tabstops[0]) * tabstops[0]; } @@ -145,29 +147,29 @@ tabify(void) for (n = 0; tabstops[n] - 1 < ocol && n < nstops; n++) ; while (ocol < dcol && n < nstops && ocol < limit) { - putchar('\t'); + putwchar('\t'); ocol = tabstops[n++]; } } /* Then spaces. */ while (ocol < dcol && ocol < limit) { - putchar(' '); + putwchar(' '); ocol++; } if (ch == '\b') { - putchar('\b'); + putwchar('\b'); if (ocol > 0) ocol--, dcol--; } else if (ch == '\n') { - putchar('\n'); + putwchar('\n'); doneline = ocol = dcol = 0; continue; } else if (ch != ' ' || dcol > limit) { - putchar(ch); - if (isprint(ch)) - ocol++, dcol++; + putwchar(ch); + if ((width = wcwidth(ch)) > 0) + ocol += width, dcol += width; } /* @@ -175,13 +177,18 @@ tabify(void) * last tab stop. Emit remainder of this line unchanged. */ if (!all || dcol >= limit) { - while ((ch = getchar()) != '\n' && ch != EOF) - putchar(ch); + while ((ch = getwchar()) != '\n' && ch != WEOF) + putwchar(ch); if (ch == '\n') - putchar('\n'); + putwchar('\n'); doneline = ocol = dcol = 0; } } + if (ferror(stdin)) { + warn("%s", curfile); + return (1); + } + return (0); } static void -- 2.41.0