瀏覽代碼

dpkg-query: Fix output when using multibyte character strings

When cropping strings, possibly multibyte ones, take into account that
there's three different counts to be considered, the number of
characters to display, their screen width and the number of bytes they
take on the multibyte string.

Closes: #257505, #718541

Based-on-patch-by: Changwoo Ryu <cwryu@debian.org>
Guillem Jover 13 年之前
父節點
當前提交
ab5bdc1fc7
共有 7 個文件被更改,包括 158 次插入7 次删除
  1. 3 0
      debian/changelog
  2. 1 0
      lib/dpkg/Makefile.am
  3. 2 0
      lib/dpkg/libdpkg.map
  4. 9 1
      lib/dpkg/string.h
  5. 127 0
      lib/dpkg/strwide.c
  6. 1 0
      po/POTFILES.in
  7. 15 6
      src/querycmd.c

+ 3 - 0
debian/changelog

@@ -37,6 +37,9 @@ dpkg (1.17.2) UNRELEASED; urgency=low
     Regression introduced in 1.17.0. Closes: #725437
   * Activate file triggers on disappearance more accurately, only when we know
     we are inevitably removing things.
+  * Fix «dpkg-query --list» output when using multibyte character strings,
+    to avoid unaligned columns and mojibake. Closes: #257505, #718541
+    Based on a patch by Changwoo Ryu <cwryu@debian.org>.
 
   [ Updated programs translations ]
   * German (Sven Joachim).

+ 1 - 0
lib/dpkg/Makefile.am

@@ -65,6 +65,7 @@ libdpkg_a_SOURCES = \
 	progress.c \
 	report.c \
 	string.c \
+	strwide.c \
 	subproc.c \
 	tarfn.c \
 	test.h \

+ 2 - 0
lib/dpkg/libdpkg.map

@@ -66,6 +66,8 @@ LIBDPKG_PRIVATE {
 	str_escape_fmt;
 	str_strip_quotes;
 	str_quote_meta;
+	str_width;
+	str_gen_crop;
 
 	# Variable buffer support
 	varbuf_init;

+ 9 - 1
lib/dpkg/string.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * string.h - string handling routines
  *
- * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2013 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -55,6 +55,14 @@ char *str_escape_fmt(char *dest, const char *src, size_t n);
 char *str_quote_meta(const char *src);
 char *str_strip_quotes(char *str);
 
+struct str_crop_info {
+	int str_bytes;
+	int max_bytes;
+};
+
+int str_width(const char *str);
+void str_gen_crop(const char *str, int max_width, struct str_crop_info *crop);
+
 /** @} */
 
 DPKG_END_DECLS

+ 127 - 0
lib/dpkg/strwide.c

@@ -0,0 +1,127 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * strwide.c - wide character string handling routines
+ *
+ * Copyright © 2004 Changwoo Ryu <cwryu@debian.org>
+ * Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
+ *
+ * This 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <string.h>
+#include <stdlib.h>
+#ifdef ENABLE_NLS
+#include <wchar.h>
+#endif
+
+#include <dpkg/i18n.h>
+#include <dpkg/dpkg.h>
+#include <dpkg/string.h>
+
+/**
+ * Compute the screen width of a string.
+ *
+ * @param str The multibyte string.
+ *
+ * @return The width of the string.
+ */
+int
+str_width(const char *str)
+{
+#ifdef ENABLE_NLS
+	mbstate_t state;
+	wchar_t *wcs;
+	const char *mbs = str;
+	size_t len, res;
+	int width;
+
+	len = strlen(str) + 1;
+	wcs = m_malloc(sizeof(wcs[0]) * len);
+
+	memset(&state, 0, sizeof(state));
+
+	res = mbsrtowcs(wcs, &mbs, len, &state);
+	if (res == (size_t)-1)
+		ohshit(_("cannot convert multibyte string '%s' "
+		         "to a wide-character string"), str);
+
+	width = wcswidth(wcs, res);
+
+	free(wcs);
+
+	return width;
+#else
+	return strlen(str);
+#endif
+}
+
+/**
+ * Generate the crop values for a string given a maximum screen width.
+ *
+ * This function analyzes the string passed and computes the correct point
+ * where to crop the string, returning the amount of string and maximum
+ * bytes to use for padding for example.
+ *
+ * On NLS enabled builds, in addition the string will be cropped on any
+ * newline.
+ *
+ * @param str        The string to crop.
+ * @param max_width  The max screen width to use.
+ * @param crop[out]  The generated crop values for the string.
+ */
+void
+str_gen_crop(const char *str, int max_width, struct str_crop_info *crop)
+{
+#ifdef ENABLE_NLS
+	mbstate_t state;
+	size_t str_bytes;
+	int mbs_bytes = 0;
+	int mbs_width = 0;
+
+	str_bytes = strlen(str) + 1;
+	memset(&state, 0, sizeof(state));
+
+	for (;;) {
+		wchar_t wc;
+		int wc_width;
+		size_t mb_bytes;
+
+		mb_bytes = mbrtowc(&wc, str, str_bytes, &state);
+		if (mb_bytes == (size_t)-1 || mb_bytes == (size_t)-2)
+			ohshit(_("cannot convert multibyte sequence '%s' "
+			         "to a wide character"), str);
+		if (mb_bytes == 0)
+			break;
+
+		wc_width = wcwidth(wc);
+		if (wc_width < 0)
+			break;
+		if (mbs_width + wc_width > max_width)
+			break;
+
+		mbs_width += wc_width;
+		mbs_bytes += mb_bytes;
+		str_bytes -= mb_bytes;
+		str += mb_bytes;
+	}
+
+	crop->str_bytes = mbs_bytes;
+	crop->max_bytes = mbs_bytes + max_width - mbs_width;
+#else
+	crop->str_bytes = crop->max_bytes = max_width;
+#endif
+}

+ 1 - 0
po/POTFILES.in

@@ -33,6 +33,7 @@ lib/dpkg/pkg-spec.c
 lib/dpkg/progress.c
 lib/dpkg/report.c
 lib/dpkg/string.c
+lib/dpkg/strwide.c
 lib/dpkg/subproc.c
 lib/dpkg/tarfn.c
 lib/dpkg/trigdeferred.l

+ 15 - 6
src/querycmd.c

@@ -118,10 +118,10 @@ list_format_init(struct list_format *fmt, struct pkg_array *array)
     for (i = 0; i < array->n_pkgs; i++) {
       int plen, vlen, alen, dlen;
 
-      plen = strlen(pkg_name(array->pkgs[i], pnaw_nonambig));
-      vlen = strlen(versiondescribe(&array->pkgs[i]->installed.version,
-                                    vdew_nonambig));
-      alen = strlen(dpkg_arch_describe(array->pkgs[i]->installed.arch));
+      plen = str_width(pkg_name(array->pkgs[i], pnaw_nonambig));
+      vlen = str_width(versiondescribe(&array->pkgs[i]->installed.version,
+                                       vdew_nonambig));
+      alen = str_width(dpkg_arch_describe(array->pkgs[i]->installed.arch));
       pkg_summary(array->pkgs[i], &array->pkgs[i]->installed, &dlen);
 
       if (plen > fmt->nw)
@@ -157,9 +157,18 @@ list_format_print(struct list_format *fmt,
                   const char *name, const char *version, const char *arch,
                   const char *desc, int desc_len)
 {
+  struct str_crop_info ns, vs, as, ds;
+
+  str_gen_crop(name, fmt->nw, &ns);
+  str_gen_crop(version, fmt->vw, &vs);
+  str_gen_crop(arch, fmt->aw, &as);
+  str_gen_crop(desc, desc_len, &ds);
+
   printf("%c%c%c %-*.*s %-*.*s %-*.*s %.*s\n", c_want, c_status, c_eflag,
-         fmt->nw, fmt->nw, name, fmt->vw, fmt->vw, version,
-         fmt->aw, fmt->aw, arch, desc_len, desc);
+         ns.max_bytes, ns.str_bytes, name,
+         vs.max_bytes, vs.str_bytes, version,
+         as.max_bytes, as.str_bytes, arch,
+         ds.str_bytes, desc);
 }
 
 static void