>From 20f651f456cc3e0843bafd03196612152db626d5 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Mon, 18 Feb 2013 14:14:38 +0100 Subject: [PATCH] expand,unexpand: add multibyte support * NEWS: Mention the changes. * bootstrap.conf: Include libunistring in the list of modules. * configure.ac: Use libunistring if available. * po/POTFILES.in: Add new source file. * src/expand-core.c: Move functions common to both expand and unexpand to this file. * src/expand-core.h: Add function prototypes from expand-core.c. * src/expand.c (expand): Iterate over multibyte characters properly. * src/local.mk: Link expand and unexpand with libunistring and add expand-core.c to their lists of source codes. * src/unexpand.c (unexpand): Iterate over multibyte characters properly. * tests/local.mk: Add new tests. * tests/{expand,unexpand}/mb.sh: New tests. --- NEWS | 4 + bootstrap.conf | 1 + configure.ac | 2 + po/POTFILES.in | 1 + src/expand-core.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++ src/expand-core.h | 35 +++++++++ src/expand.c | 215 +++++++++++++++----------------------------------- src/local.mk | 5 ++ src/unexpand.c | 217 +++++++++++++++------------------------------------ tests/expand/mb.sh | 47 +++++++++++ tests/local.mk | 2 + tests/unexpand/mb.sh | 47 +++++++++++ 12 files changed, 469 insertions(+), 305 deletions(-) create mode 100644 src/expand-core.c create mode 100644 src/expand-core.h create mode 100755 tests/expand/mb.sh create mode 100755 tests/unexpand/mb.sh diff --git a/NEWS b/NEWS index 37bcdf7..c997007 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ GNU coreutils NEWS -*- outline -*- * Noteworthy changes in release ?.? (????-??-??) [?] +** New features + + expand and unexpand now handle multibyte characters properly. + * Noteworthy changes in release 8.21 (2013-02-14) [stable] diff --git a/bootstrap.conf b/bootstrap.conf index bb6c145..455f0e5 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -140,6 +140,7 @@ gnulib_modules=" lchown ldtoastr lib-ignore + libunistring linebuffer link link-follow diff --git a/configure.ac b/configure.ac index 3f0c58b..79bc920 100644 --- a/configure.ac +++ b/configure.ac @@ -373,6 +373,8 @@ fi # I'm leaving it here for now. This whole thing needs to be modernized... gl_WINSIZE_IN_PTEM +gl_LIBUNISTRING + gl_HEADER_TIOCGWINSZ_IN_TERMIOS_H if test $gl_cv_sys_tiocgwinsz_needs_termios_h = no && \ diff --git a/po/POTFILES.in b/po/POTFILES.in index 21617cc..27b19e5 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -59,6 +59,7 @@ src/dirname.c src/du.c src/echo.c src/env.c +src/expand-core.c src/expand.c src/expr.c src/factor.c diff --git a/src/expand-core.c b/src/expand-core.c new file mode 100644 index 0000000..4ffc46a --- /dev/null +++ b/src/expand-core.c @@ -0,0 +1,198 @@ +/* exp-common.c - elementary functions for the expand and unexpand utilities + Copyright (C) 1989-2013 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include +#include + +#if HAVE_LIBUNISTRING +# include +# include +# include +# include +# include +#endif + +#include "expand-core.h" + +#include "system.h" +#include "error.h" +#include "fadvise.h" +#include "quote.h" +#include "xstrndup.h" + +extern size_t first_free_tab; + +extern size_t n_tabs_allocated; + +extern uintmax_t *tab_list; + +extern int exit_status; + +extern char **file_list; + +extern bool have_read_stdin; + +extern bool u8_locale; + +/* Add the comma or blank separated list of tab stops STOPS + to the list of tab stops. */ + +extern void +parse_tab_stops (char const *stops) +{ + bool have_tabval = false; + uintmax_t tabval IF_LINT ( = 0); + char const *num_start IF_LINT ( = NULL); + bool ok = true; + + for (; *stops; stops++) + { + if (*stops == ',' || isblank (to_uchar (*stops))) + { + if (have_tabval) + add_tab_stop (tabval); + have_tabval = false; + } + else if (ISDIGIT (*stops)) + { + if (!have_tabval) + { + tabval = 0; + have_tabval = true; + num_start = stops; + } + + /* Detect overflow. */ + if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0', uintmax_t)) + { + size_t len = strspn (num_start, "0123456789"); + char *bad_num = xstrndup (num_start, len); + error (0, 0, _("tab stop is too large %s"), quote (bad_num)); + free (bad_num); + ok = false; + stops = num_start + len - 1; + } + } + else + { + error (0, 0, _("tab size contains invalid character(s): %s"), + quote (stops)); + ok = false; + break; + } + } + + if (!ok) + exit (EXIT_FAILURE); + + if (have_tabval) + add_tab_stop (tabval); +} + +/* Add tab stop TABVAL to the end of 'tab_list'. */ + +extern void +add_tab_stop (uintmax_t tabval) +{ + if (first_free_tab == n_tabs_allocated) + tab_list = X2NREALLOC (tab_list, &n_tabs_allocated); + tab_list[first_free_tab++] = tabval; +} + +/* Check that the list of tab stops TABS, with ENTRIES entries, + contains only nonzero, ascending values. */ + +extern void +validate_tab_stops (uintmax_t const *tabs, size_t entries) +{ + uintmax_t prev_tab = 0; + size_t i; + + for (i = 0; i < entries; i++) + { + if (tabs[i] == 0) + error (EXIT_FAILURE, 0, _("tab size cannot be 0")); + if (tabs[i] <= prev_tab) + error (EXIT_FAILURE, 0, _("tab sizes must be ascending")); + prev_tab = tabs[i]; + } +} + +/* Close the old stream pointer FP if it is non-NULL, + and return a new one opened to read the next input file. + Open a filename of '-' as the standard input. + Return NULL if there are no more input files. */ + +extern FILE * +next_file (FILE *fp) +{ + static char *prev_file; + char *file; + + if (fp) + { + if (ferror (fp)) + { + error (0, errno, "%s", prev_file); + exit_status = EXIT_FAILURE; + } + if (STREQ (prev_file, "-")) + clearerr (fp); /* Also clear EOF. */ + else if (fclose (fp) != 0) + { + error (0, errno, "%s", prev_file); + exit_status = EXIT_FAILURE; + } + } + + while ((file = *file_list++) != NULL) + { + if (STREQ (file, "-")) + { + have_read_stdin = true; + fp = stdin; + } + else + fp = fopen (file, "r"); + if (fp) + { + prev_file = file; + fadvise (fp, FADVISE_SEQUENTIAL); + return fp; + } + error (0, errno, "%s", file); + exit_status = EXIT_FAILURE; + } + return NULL; +} + +extern uint32_t +mb_index (uint8_t *line, size_t *clen, size_t offt) +{ +#if HAVE_LIBUNISTRING + if (u8_locale) + { + uint32_t c; + *clen = u8_mblen (line + offt, MB_CUR_MAX); + u8_next (&c, line + offt); + return c; + } + else +#endif + return line[offt]; +} diff --git a/src/expand-core.h b/src/expand-core.h new file mode 100644 index 0000000..a582fd9 --- /dev/null +++ b/src/expand-core.h @@ -0,0 +1,35 @@ +/* exp-common.h - function prototypes for the expand and unexpand utilities + Copyright (C) 1989-2013 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef EXP_COMMON_H_ +#define EXP_COMMON_H_ + +void +parse_tab_stops (char const *stops); + +void +add_tab_stop (uintmax_t tabval); + +void +validate_tab_stops (uintmax_t const *tabs, size_t entries); + +FILE * +next_file (FILE *fp); + +uint32_t +mb_index (uint8_t *line, size_t *clen, size_t offt); + +#endif /* EXP_COMMON_H_ */ diff --git a/src/expand.c b/src/expand.c index 0b12b02..edaac3c 100644 --- a/src/expand.c +++ b/src/expand.c @@ -37,10 +37,20 @@ #include #include #include + +#if HAVE_LIBUNISTRING +# include +# include +# include +# include +# include +#endif + +#include "expand-core.h" + #include "system.h" #include "error.h" #include "fadvise.h" -#include "quote.h" #include "xstrndup.h" /* The official name of this program (e.g., no 'g' prefix). */ @@ -58,17 +68,17 @@ static uintmax_t tab_size; /* Array of the explicit column numbers of the tab stops; after 'tab_list' is exhausted, each additional tab is replaced by a space. The first column is column 0. */ -static uintmax_t *tab_list; +uintmax_t *tab_list; /* The number of allocated entries in 'tab_list'. */ -static size_t n_tabs_allocated; +size_t n_tabs_allocated; /* The index of the first invalid element of 'tab_list', where the next element can be added. */ -static size_t first_free_tab; +size_t first_free_tab; /* Null-terminated array of input filenames. */ -static char **file_list; +char **file_list; /* Default for 'file_list' if no files are given on the command line. */ static char *stdin_argv[] = @@ -77,10 +87,13 @@ static char *stdin_argv[] = }; /* True if we have ever read standard input. */ -static bool have_read_stdin; +bool have_read_stdin; /* The desired exit status. */ -static int exit_status; +int exit_status; + +/* True if we are operating under UTF-8 locale. */ +bool u8_locale; static char const shortopts[] = "it:0::1::2::3::4::5::6::7::8::9::"; @@ -125,138 +138,6 @@ With no FILE, or when FILE is -, read standard input.\n\ exit (status); } -/* Add tab stop TABVAL to the end of 'tab_list'. */ - -static void -add_tab_stop (uintmax_t tabval) -{ - if (first_free_tab == n_tabs_allocated) - tab_list = X2NREALLOC (tab_list, &n_tabs_allocated); - tab_list[first_free_tab++] = tabval; -} - -/* Add the comma or blank separated list of tab stops STOPS - to the list of tab stops. */ - -static void -parse_tab_stops (char const *stops) -{ - bool have_tabval = false; - uintmax_t tabval IF_LINT ( = 0); - char const *num_start IF_LINT ( = NULL); - bool ok = true; - - for (; *stops; stops++) - { - if (*stops == ',' || isblank (to_uchar (*stops))) - { - if (have_tabval) - add_tab_stop (tabval); - have_tabval = false; - } - else if (ISDIGIT (*stops)) - { - if (!have_tabval) - { - tabval = 0; - have_tabval = true; - num_start = stops; - } - - /* Detect overflow. */ - if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0', uintmax_t)) - { - size_t len = strspn (num_start, "0123456789"); - char *bad_num = xstrndup (num_start, len); - error (0, 0, _("tab stop is too large %s"), quote (bad_num)); - free (bad_num); - ok = false; - stops = num_start + len - 1; - } - } - else - { - error (0, 0, _("tab size contains invalid character(s): %s"), - quote (stops)); - ok = false; - break; - } - } - - if (!ok) - exit (EXIT_FAILURE); - - if (have_tabval) - add_tab_stop (tabval); -} - -/* Check that the list of tab stops TABS, with ENTRIES entries, - contains only nonzero, ascending values. */ - -static void -validate_tab_stops (uintmax_t const *tabs, size_t entries) -{ - uintmax_t prev_tab = 0; - size_t i; - - for (i = 0; i < entries; i++) - { - if (tabs[i] == 0) - error (EXIT_FAILURE, 0, _("tab size cannot be 0")); - if (tabs[i] <= prev_tab) - error (EXIT_FAILURE, 0, _("tab sizes must be ascending")); - prev_tab = tabs[i]; - } -} - -/* Close the old stream pointer FP if it is non-NULL, - and return a new one opened to read the next input file. - Open a filename of '-' as the standard input. - Return NULL if there are no more input files. */ - -static FILE * -next_file (FILE *fp) -{ - static char *prev_file; - char *file; - - if (fp) - { - if (ferror (fp)) - { - error (0, errno, "%s", prev_file); - exit_status = EXIT_FAILURE; - } - if (STREQ (prev_file, "-")) - clearerr (fp); /* Also clear EOF. */ - else if (fclose (fp) != 0) - { - error (0, errno, "%s", prev_file); - exit_status = EXIT_FAILURE; - } - } - - while ((file = *file_list++) != NULL) - { - if (STREQ (file, "-")) - { - have_read_stdin = true; - fp = stdin; - } - else - fp = fopen (file, "r"); - if (fp) - { - prev_file = file; - fadvise (fp, FADVISE_SEQUENTIAL); - return fp; - } - error (0, errno, "%s", file); - exit_status = EXIT_FAILURE; - } - return NULL; -} - /* Change tabs to spaces, writing to stdout. Read each file in 'file_list', in order. */ @@ -265,19 +146,21 @@ expand (void) { /* Input stream. */ FILE *fp = next_file (NULL); + char **rawline = malloc (sizeof (char *)); + *rawline = NULL; + uint8_t *line; + uint32_t c; + size_t clen = 0, offt; + ssize_t rawlen; if (!fp) return; while (true) { - /* Input character, or EOF. */ - int c; - /* If true, perform translations. */ bool convert = true; - /* The following variables have valid values only when CONVERT is true: */ @@ -287,14 +170,34 @@ expand (void) /* Index in TAB_LIST of next tab stop to examine. */ size_t tab_index = 0; - /* Convert a line of text. */ + while ((rawlen = getline (rawline, &offt, fp)) == -1 + && (fp = next_file (fp))) + continue; + + if (!fp) + { + free (*rawline); + free (rawline); + return; + } +#if HAVE_LIBUNISTRING + if (u8_locale) + line = u8_strconv_from_locale (*rawline); + else +#endif + { + line = (uint8_t *) strdup (*rawline); + clen = 1; + } - do - { - while ((c = getc (fp)) < 0 && (fp = next_file (fp))) - continue; + free (*rawline); + *rawline = NULL; + offt = 0; + while (offt != rawlen) + { + c = mb_index (line, &clen, offt); if (convert) { if (c == '\t') @@ -347,13 +250,15 @@ expand (void) convert &= convert_entire_line || !! isblank (c); } - if (c < 0) - return; + if (c == ' ') + putchar (c); + else + fwrite (line + offt, 1, clen, stdout); + + offt += clen; - if (putchar (c) < 0) - error (EXIT_FAILURE, errno, _("write error")); } - while (c != '\n'); + free (line); } } @@ -376,6 +281,10 @@ main (int argc, char **argv) tab_list = NULL; first_free_tab = 0; +#if HAVE_LIBUNISTRING + u8_locale = STREQ ("UTF-8", locale_charset ()); +#endif + while ((c = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1) { switch (c) diff --git a/src/local.mk b/src/local.mk index 982cd4d..35616e4 100644 --- a/src/local.mk +++ b/src/local.mk @@ -118,6 +118,7 @@ src_du_LDADD = $(LDADD) src_echo_LDADD = $(LDADD) src_env_LDADD = $(LDADD) src_expand_LDADD = $(LDADD) +src_expand_LDADD += $(LIBUNISTRING) src_expr_LDADD = $(LDADD) src_factor_LDADD = $(LDADD) src_false_LDADD = $(LDADD) @@ -195,6 +196,7 @@ src_tsort_LDADD = $(LDADD) src_tty_LDADD = $(LDADD) src_uname_LDADD = $(LDADD) src_unexpand_LDADD = $(LDADD) +src_unexpand_LDADD += $(LIBUNISTRING) src_uniq_LDADD = $(LDADD) src_unlink_LDADD = $(LDADD) src_uptime_LDADD = $(LDADD) @@ -288,6 +290,7 @@ src_stdbuf_LDADD += $(LIBICONV) src_timeout_LDADD += $(LIBICONV) src_truncate_LDADD += $(LIBICONV) + # for canon_host src_pinky_LDADD += $(GETADDRINFO_LIB) src_who_LDADD += $(GETADDRINFO_LIB) @@ -325,6 +328,8 @@ src___SOURCES = src/lbracket.c src_cp_SOURCES = src/cp.c $(copy_sources) src_dir_SOURCES = src/ls.c src/ls-dir.c +src_expand_SOURCES = src/expand.c src/expand-core.c +src_unexpand_SOURCES = src/unexpand.c src/expand-core.c src_vdir_SOURCES = src/ls.c src/ls-vdir.c src_id_SOURCES = src/id.c src/group-list.c src_groups_SOURCES = src/groups.c src/group-list.c diff --git a/src/unexpand.c b/src/unexpand.c index 1803cd5..31e6b8e 100644 --- a/src/unexpand.c +++ b/src/unexpand.c @@ -38,10 +38,20 @@ #include #include #include + +#if HAVE_LIBUNISTRING +# include +# include +# include +# include +# include +#endif + +#include "expand-core.h" + #include "system.h" #include "error.h" #include "fadvise.h" -#include "quote.h" #include "xstrndup.h" /* The official name of this program (e.g., no 'g' prefix). */ @@ -62,17 +72,17 @@ static size_t max_column_width; /* Array of the explicit column numbers of the tab stops; after 'tab_list' is exhausted, the rest of the line is printed unchanged. The first column is column 0. */ -static uintmax_t *tab_list; +uintmax_t *tab_list; /* The number of allocated entries in 'tab_list'. */ -static size_t n_tabs_allocated; +size_t n_tabs_allocated; /* The index of the first invalid element of 'tab_list', where the next element can be added. */ -static size_t first_free_tab; +size_t first_free_tab; /* Null-terminated array of input filenames. */ -static char **file_list; +char **file_list; /* Default for 'file_list' if no files are given on the command line. */ static char *stdin_argv[] = @@ -81,10 +91,13 @@ static char *stdin_argv[] = }; /* True if we have ever read standard input. */ -static bool have_read_stdin; +bool have_read_stdin; /* The desired exit status. */ -static int exit_status; +int exit_status; + +/* True if we are operating under UTF-8 locale. */ +bool u8_locale; /* For long options that have no equivalent short option, use a non-character as a pseudo short option, starting with CHAR_MAX + 1. */ @@ -134,17 +147,13 @@ With no FILE, or when FILE is -, read standard input.\n\ exit (status); } -/* Add tab stop TABVAL to the end of 'tab_list'. */ - static void -add_tab_stop (uintmax_t tabval) +set_max_width (uintmax_t tabval) { uintmax_t prev_column = first_free_tab ? tab_list[first_free_tab - 1] : 0; uintmax_t column_width = prev_column <= tabval ? tabval - prev_column : 0; - if (first_free_tab == n_tabs_allocated) - tab_list = X2NREALLOC (tab_list, &n_tabs_allocated); - tab_list[first_free_tab++] = tabval; + add_tab_stop (tabval); if (max_column_width < column_width) { @@ -154,128 +163,6 @@ add_tab_stop (uintmax_t tabval) } } -/* Add the comma or blank separated list of tab stops STOPS - to the list of tab stops. */ - -static void -parse_tab_stops (char const *stops) -{ - bool have_tabval = false; - uintmax_t tabval IF_LINT ( = 0); - char const *num_start IF_LINT ( = NULL); - bool ok = true; - - for (; *stops; stops++) - { - if (*stops == ',' || isblank (to_uchar (*stops))) - { - if (have_tabval) - add_tab_stop (tabval); - have_tabval = false; - } - else if (ISDIGIT (*stops)) - { - if (!have_tabval) - { - tabval = 0; - have_tabval = true; - num_start = stops; - } - - /* Detect overflow. */ - if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0', uintmax_t)) - { - size_t len = strspn (num_start, "0123456789"); - char *bad_num = xstrndup (num_start, len); - error (0, 0, _("tab stop is too large %s"), quote (bad_num)); - free (bad_num); - ok = false; - stops = num_start + len - 1; - } - } - else - { - error (0, 0, _("tab size contains invalid character(s): %s"), - quote (stops)); - ok = false; - break; - } - } - - if (!ok) - exit (EXIT_FAILURE); - - if (have_tabval) - add_tab_stop (tabval); -} - -/* Check that the list of tab stops TABS, with ENTRIES entries, - contains only nonzero, ascending values. */ - -static void -validate_tab_stops (uintmax_t const *tabs, size_t entries) -{ - uintmax_t prev_tab = 0; - size_t i; - - for (i = 0; i < entries; i++) - { - if (tabs[i] == 0) - error (EXIT_FAILURE, 0, _("tab size cannot be 0")); - if (tabs[i] <= prev_tab) - error (EXIT_FAILURE, 0, _("tab sizes must be ascending")); - prev_tab = tabs[i]; - } -} - -/* Close the old stream pointer FP if it is non-NULL, - and return a new one opened to read the next input file. - Open a filename of '-' as the standard input. - Return NULL if there are no more input files. */ - -static FILE * -next_file (FILE *fp) -{ - static char *prev_file; - char *file; - - if (fp) - { - if (ferror (fp)) - { - error (0, errno, "%s", prev_file); - exit_status = EXIT_FAILURE; - } - if (STREQ (prev_file, "-")) - clearerr (fp); /* Also clear EOF. */ - else if (fclose (fp) != 0) - { - error (0, errno, "%s", prev_file); - exit_status = EXIT_FAILURE; - } - } - - while ((file = *file_list++) != NULL) - { - if (STREQ (file, "-")) - { - have_read_stdin = true; - fp = stdin; - } - else - fp = fopen (file, "r"); - if (fp) - { - prev_file = file; - fadvise (fp, FADVISE_SEQUENTIAL); - return fp; - } - error (0, errno, "%s", file); - exit_status = EXIT_FAILURE; - } - return NULL; -} - /* Change blanks to tabs, writing to stdout. Read each file in 'file_list', in order. */ @@ -284,6 +171,12 @@ unexpand (void) { /* Input stream. */ FILE *fp = next_file (NULL); + char **rawline = malloc (sizeof (char *)); + *rawline = NULL; + uint8_t *line; + uint32_t c; + size_t clen, offt; + ssize_t rawlen; /* The array of pending blanks. In non-POSIX locales, blanks can include characters other than spaces, so the blanks must be @@ -300,9 +193,6 @@ unexpand (void) while (true) { - /* Input character, or EOF. */ - int c; - /* If true, perform translations. */ bool convert = true; @@ -333,11 +223,32 @@ unexpand (void) /* Convert a line of text. */ - do - { - while ((c = getc (fp)) < 0 && (fp = next_file (fp))) - continue; + while ((rawlen = getline (rawline, &offt, fp)) == -1 + && (fp = next_file (fp))) + continue; + + if (!fp) + { + free (pending_blank); + return; + } +#if HAVE_LIBUNISTRING + if (u8_locale) + line = u8_strconv_from_locale (*rawline); + else +#endif + { + line = (uint8_t *) strdup (*rawline); + clen = 1; + } + offt = 0; + free (*rawline); + *rawline = NULL; + + while (offt != rawlen) + { + c = mb_index (line, &clen, offt); if (convert) { bool blank = !! isblank (c); @@ -391,6 +302,7 @@ unexpand (void) one_blank_before_tab_stop = true; pending_blank[pending++] = c; prev_blank = true; + offt += clen; continue; } @@ -432,16 +344,14 @@ unexpand (void) convert &= convert_entire_line || blank; } - if (c < 0) - { - free (pending_blank); - return; - } + if (c == '\t') + putchar (c); + else + fwrite (line + offt, 1, clen, stdout); - if (putchar (c) < 0) - error (EXIT_FAILURE, errno, _("write error")); + offt += clen; } - while (c != '\n'); + free (line); } } @@ -469,6 +379,9 @@ main (int argc, char **argv) convert_entire_line = false; tab_list = NULL; first_free_tab = 0; +#if HAVE_LIBUNISTRING + u8_locale = STREQ ("UTF-8", locale_charset ()); +#endif while ((c = getopt_long (argc, argv, ",0123456789at:", longopts, NULL)) != -1) @@ -489,7 +402,7 @@ main (int argc, char **argv) break; case ',': if (have_tabval) - add_tab_stop (tabval); + set_max_width (tabval); have_tabval = false; break; case_GETOPT_HELP_CHAR; @@ -510,7 +423,7 @@ main (int argc, char **argv) convert_entire_line = false; if (have_tabval) - add_tab_stop (tabval); + set_max_width (tabval); validate_tab_stops (tab_list, first_free_tab); diff --git a/tests/expand/mb.sh b/tests/expand/mb.sh new file mode 100755 index 0000000..0911564 --- /dev/null +++ b/tests/expand/mb.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# Copyright (C) 2012-2013 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ expand + +export LC_ALL=en_US.UTF-8 + +#input containing multibyte characters +cat > in <<\EOF +1234567812345678123456781 +. . . . +a b c d +. . . . +ä ö ü ß +. . . . + äöü . öüä. ä xx +EOF + +cat > exp <<\EOF +1234567812345678123456781 +. . . . +a b c d +. . . . +ä ö ü ß +. . . . + äöü . öüä. ä xx +EOF + +expand < in > out +cmp out exp > /dev/null 2>&1 || fail=1 + +exit $fail diff --git a/tests/local.mk b/tests/local.mk index 9be1970..9dcba02 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -502,6 +502,7 @@ all_tests = \ tests/du/threshold.sh \ tests/du/trailing-slash.sh \ tests/du/two-args.sh \ + tests/expand/mb.sh \ tests/id/gnu-zero-uids.sh \ tests/id/no-context.sh \ tests/install/basic-1.sh \ @@ -631,6 +632,7 @@ all_tests = \ tests/touch/read-only.sh \ tests/touch/relative.sh \ tests/touch/trailing-slash.sh \ + tests/unexpand/mb.sh \ $(all_root_tests) # See tests/factor/create-test.sh. diff --git a/tests/unexpand/mb.sh b/tests/unexpand/mb.sh new file mode 100755 index 0000000..4ffc365 --- /dev/null +++ b/tests/unexpand/mb.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# Copyright (C) 2012-2013 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ unexpand + +export LC_ALL=en_US.UTF-8 + +#input containing multibyte characters +cat > in <<\EOF +1234567812345678123456781 +. . . . +a b c d +. . . . +ä ö ü ß +. . . . + äöü . öüä. ä xx +EOF + +cat > exp <<\EOF +1234567812345678123456781 +. . . . +a b c d +. . . . +ä ö ü ß +. . . . + äöü . öüä. ä xx +EOF + +unexpand -a < in > out +cmp out exp > /dev/null 2>&1 || fail=1 + +exit $fail -- 1.7.11.7