From 29f55fb7c9e2d7d3923d796216a9506d95b1fd60 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 22 Feb 2008 20:57:45 -0600 Subject: Fix wide character output for download progress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that we have a Chinese translation, all of the problems with new character sets crop up. Assumptions were made in the past that all characters occupied one column, which is not true with a Chinese character set. In addition, the download code even failed on such things as 'ö', which is two bytes wide but only 1 column. This code will need to also be ported to the add/remove/upgrade/conflicts progress printouts. Note that the tests below try to incorporate a number of things: 1. download filenames too long to fit 2. download filenames cut off in the middle of a multibyte sequence 3. download filenames incorporating multicolumn chars 4. download filenames incorporating multibyte, single-column chars 5. 'plain' download filenames that have always worked Before: :: 正在同步软件包数据库…… 正在解决倚赖��... 0.0K 199.8K/s 00:00:00 [-----------------] 100% 错误:无法升级正在解决倚赖关系junköëjunköëjunköëäää (未预计的系统错误) 正在解决倚赖��... 0.0K 308.4K/s 00:00:00 [-----------------] 100% 错误:无法升级正在解决倚赖关系 (未预计的系统错误) junköëä 0.0K 390.6K/s 00:00:00 [-----------------] 100% 错误:无法升级junköëä (未预计的系统错误) pacman-git 0.5K 4.3M/s 00:00:00 [-----------------] 100% 本地数据库已是最新的 After: :: 正在同步软件包数据库…… 正在解决倚赖关系jun... 0.0K 89.7K/s 00:00:00 [-----------------] 100% 错误:无法升级正在解决倚赖关系junköëjunköëjunköëäää (未预计的系统错误) 正在解决倚赖关系 0.0K 147.7K/s 00:00:00 [-----------------] 100% 错误:无法升级正在解决倚赖关系 (未预计的系统错误) junköëä 0.0K 156.9K/s 00:00:00 [-----------------] 100% 错误:无法升级junköëä (未预计的系统错误) pacman-git 0.5K 1515.9K/s 00:00:00 [-----------------] 100% 本地数据库已是最新的 Signed-off-by: Dan McGee --- src/pacman/callback.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/pacman/callback.c b/src/pacman/callback.c index cf07a09d..5865550e 100644 --- a/src/pacman/callback.c +++ b/src/pacman/callback.c @@ -36,7 +36,6 @@ /* TODO this should not have to be defined twice- trans.c & log.c */ #define LOG_STR_LEN 256 -#define FILENAME_TRIM_LEN 23 /* download progress bar */ static float rate_last; @@ -421,7 +420,11 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total, int list_xfered, int list_total) { const int infolen = 50; + const int filenamelen = infolen - 27; char *fname, *p; + /* used for wide character width determination and printing */ + int len, wclen, wcwid, padwid; + wchar_t *wcfname; float rate = 0.0, timediff = 0.0, f_xfered = 0.0; unsigned int eta_h = 0, eta_m = 0, eta_s = 0; @@ -501,8 +504,30 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total, if((p = strstr(fname, PKGEXT)) || (p = strstr(fname, DBEXT))) { *p = '\0'; } - if(strlen(fname) > FILENAME_TRIM_LEN) { - strcpy(fname + FILENAME_TRIM_LEN -3,"..."); + /* In order to deal with characters from all locales, we have to worry + * about wide characters and their column widths. A lot of stuff is + * done here to figure out the actual number of screen columns used + * by the output, and then pad it accordingly so we fill the terminal. + */ + /* len = filename len + null */ + len = strlen(filename) + 1; + wcfname = calloc(len, sizeof(wchar_t)); + wclen = mbstowcs(wcfname, fname, len); + wcwid = wcswidth(wcfname, wclen); + padwid = filenamelen - wcwid; + /* if padwid is < 0, we need to trim the string so padwid = 0 */ + if(padwid < 0) { + int i = filenamelen - 3; + wchar_t *p = wcfname; + /* grab the max number of char columns we can fill */ + while(i > 0 && wcwidth(*p) < i) { + i -= wcwidth(*p); + p++; + } + /* then add the ellipsis and fill out any extra padding */ + wcscpy(p, L"..."); + padwid = i; + } /* Awesome formatting for progress bar. We need a mess of Kb->Mb->Gb stuff @@ -532,10 +557,12 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total, } } - printf(" %-*s %6.1f%c %#6.1f%c/s %02u:%02u:%02u", FILENAME_TRIM_LEN, fname, - f_xfered, xfered_size, rate, rate_size, eta_h, eta_m, eta_s); + printf(" %ls%-*s %6.1f%c %#6.1f%c/s %02u:%02u:%02u", wcfname, + padwid, "", f_xfered, xfered_size, + rate, rate_size, eta_h, eta_m, eta_s); free(fname); + free(wcfname); /* The progress bar is based on the file percent regardless of the * TotalDownload option. */ @@ -548,7 +575,7 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total, /* Callback to handle notifications from the library */ void cb_log(pmloglevel_t level, char *fmt, va_list args) { - if(!strlen(fmt)) { + if(strlen(fmt) == 0) { return; } -- cgit v1.2.3 From a6470956bc2db5acb821485e590822953966586a Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 22 Feb 2008 22:00:15 -0600 Subject: Fix wide character output for add/remove/upgrade/conflict progress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Due to the addition of the Chinese translation, our column widths were all messed up as mentioned in the download progress commit fixing this same problem there. This is a port of the code and ideas from that fix to the installation progress bars. Once again, a handful of examples were tested to ensure we work in all locales and with varying byte and char widths. English (before & after): (1/1) checking for file conflicts [-----------------] 100% (1/1) upgrading man-pages [-----------------] 100% German (before & after): (1/1) Prüfe auf Dateikonflikte [-----------------] 100% (1/1) Aktualisiere man-pages [-----------------] 100% Chinese (before): (1/1) 正在检查文件冲突 [-----------------] 100% (1/1) 生在升级 man-pages [c o o o o o ] (1/1) 生在升级 man-pages [----------C o o ] (1/1) 生在升级 man-pages [-----------------] 100% Chinese (after): (1/1) 正在检查文件冲突 [-----------------] 100% (1/1) 生在升级 man-pages [-----------------] 100% Signed-off-by: Dan McGee --- src/pacman/callback.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/pacman/callback.c b/src/pacman/callback.c index 5865550e..629c2e1c 100644 --- a/src/pacman/callback.c +++ b/src/pacman/callback.c @@ -328,8 +328,11 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent, /* size of line to allocate for text printing (e.g. not progressbar) */ const int infolen = 50; - int tmp, digits, oprlen, textlen, remainlen; + int tmp, digits, textlen; char *opr = NULL; + /* used for wide character width determination and printing */ + int len, wclen, wcwid, padwid; + wchar_t *wcstr; if(config->noprogressbar) { return; @@ -378,27 +381,41 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent, while((tmp /= 10)) { ++digits; } - /* determine room left for non-digits text [not ( 1/12) part] */ textlen = infolen - 3 - (2 * digits); - oprlen = mbstowcs(NULL, opr, 0); - /* room left (eg for package name) */ - remainlen = textlen - oprlen - 1; + /* In order to deal with characters from all locales, we have to worry + * about wide characters and their column widths. A lot of stuff is + * done here to figure out the actual number of screen columns used + * by the output, and then pad it accordingly so we fill the terminal. + */ + /* len = opr len + pkgname len (if available) + space + null */ + len = strlen(opr) + ((pkgname) ? strlen(pkgname) : 0) + 2; + wcstr = calloc(len, sizeof(wchar_t)); + /* print our strings to the alloc'ed memory */ + wclen = swprintf(wcstr, len, L"%s %s", opr, pkgname); + wcwid = wcswidth(wcstr, wclen); + padwid = textlen - wcwid; + /* if padwid is < 0, we need to trim the string so padwid = 0 */ + if(padwid < 0) { + int i = textlen - 3; + wchar_t *p = wcstr; + /* grab the max number of char columns we can fill */ + while(i > 0 && wcwidth(*p) < i) { + i -= wcwidth(*p); + p++; + } + /* then add the ellipsis and fill out any extra padding */ + wcscpy(p, L"..."); + padwid = i; - switch (event) { - case PM_TRANS_PROGRESS_ADD_START: - case PM_TRANS_PROGRESS_UPGRADE_START: - case PM_TRANS_PROGRESS_REMOVE_START: - printf("(%*d/%*d) %s %-*.*s", digits, remain, digits, howmany, - opr, remainlen, remainlen, pkgname); - break; - case PM_TRANS_PROGRESS_CONFLICTS_START: - printf("(%*d/%*d) %s %-*s", digits, remain, digits, howmany, - opr, remainlen, ""); - break; } + printf("(%*d/%*d) %ls%-*s", digits, remain, digits, howmany, + wcstr, padwid, ""); + + free(wcstr); + /* call refactored fill progress function */ fill_progress(percent, percent, getcols() - infolen); -- cgit v1.2.3 From 4c14dcc5804aa6736616d35aac4db1ea3b29985e Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 22 Feb 2008 23:47:03 -0600 Subject: A few more wide character output fixes Fix up the indentprint and list printing functions so they work properly. This output can be seen in places such as -Ss, -Si, -Qs, and -Qi. Signed-off-by: Dan McGee --- src/pacman/package.c | 15 ++++++++++--- src/pacman/util.c | 62 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/pacman/package.c b/src/pacman/package.c index cc4be139..7019e7e6 100644 --- a/src/pacman/package.c +++ b/src/pacman/package.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,8 @@ void dump_pkg_full(pmpkg_t *pkg, int level) char bdatestr[50] = "", idatestr[50] = ""; const alpm_list_t *i; alpm_list_t *requiredby = NULL, *depstrings = NULL; + wchar_t *wcstr; + int len; if(pkg == NULL) { return; @@ -86,8 +89,6 @@ void dump_pkg_full(pmpkg_t *pkg, int level) requiredby = alpm_pkg_compute_requiredby(pkg); } - descheader = _("Description : "); - /* actual output */ if(level == 0) { string_display(_("Filename :"), alpm_pkg_get_filename(pkg)); @@ -136,8 +137,16 @@ void dump_pkg_full(pmpkg_t *pkg, int level) } /* printed using a variable to make i18n safe */ + descheader = _("Description : "); + /* len goes from # bytes -> # chars -> # cols */ + len = strlen(descheader) + 1; + wcstr = calloc(len, sizeof(wchar_t)); + len = mbstowcs(wcstr, descheader, len); + len = wcswidth(wcstr, len); + free(wcstr); + /* we can finally print the darn thing */ printf("%s", descheader); - indentprint(alpm_pkg_get_desc(pkg), mbstowcs(NULL, descheader, 0)); + indentprint(alpm_pkg_get_desc(pkg), len); printf("\n\n"); /* Print additional package info if info flag passed more than once */ diff --git a/src/pacman/util.c b/src/pacman/util.c index 20f4c072..42436e26 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -219,37 +220,46 @@ char *mdirname(const char *path) */ void indentprint(const char *str, int indent) { - const char *p = str; - int cidx = indent; + wchar_t *wcstr; + const wchar_t *p; + int len, cidx; + + len = strlen(str) + 1; + wcstr = calloc(len, sizeof(wchar_t)); + len = mbstowcs(wcstr, str, len); + p = wcstr; + cidx = indent; while(*p) { - if(*p == ' ') { - const char *next = NULL; - int len; + if(*p == L' ') { + const wchar_t *q, *next; p++; - if(p == NULL || *p == ' ') continue; - next = strchr(p, ' '); + if(p == NULL || *p == L' ') continue; + next = wcschr(p, L' '); if(next == NULL) { - next = p + mbstowcs(NULL, p, 0); + next = p + wcslen(p); + } + /* len captures # cols */ + len = 0; + q = p; + while(q < next) { + len += wcwidth(*q++); } - len = next - p; if(len > (getcols() - cidx - 1)) { - /* newline */ - int i; - fprintf(stdout, "\n"); - for(i = 0; i < indent; i++) { - fprintf(stdout, " "); - } + /* wrap to a newline and reindent */ + fprintf(stdout, "\n%-*s", indent, ""); cidx = indent; } else { printf(" "); cidx++; } + continue; } - fprintf(stdout, "%c", *p); + fprintf(stdout, "%lc", (wint_t)*p); + cidx += wcwidth(*p); p++; - cidx++; } + free(wcstr); } /* Convert a string to uppercase @@ -389,14 +399,28 @@ void list_display(const char *title, const alpm_list_t *list) { const alpm_list_t *i; int cols, len; + wchar_t *wcstr; + + /* len goes from # bytes -> # chars -> # cols */ + len = strlen(title) + 1; + wcstr = calloc(len, sizeof(wchar_t)); + len = mbstowcs(wcstr, title, len); + len = wcswidth(wcstr, len); + free(wcstr); - len = mbstowcs(NULL, title, 0); printf("%s ", title); if(list) { for(i = list, cols = len; i; i = alpm_list_next(i)) { char *str = alpm_list_getdata(i); - int s = mbstowcs(NULL, str, 0) + 2; + /* s goes from # bytes -> # chars -> # cols */ + int s = strlen(str) + 1; + wcstr = calloc(s, sizeof(wchar_t)); + s = mbstowcs(wcstr, str, s); + s = wcswidth(wcstr, s); + free(wcstr); + /* two additional spaces are added to the length */ + s += 2; int maxcols = getcols(); if(s + cols >= maxcols) { int i; -- cgit v1.2.3 From 96f7613d15e46131bf8a4b93828ad70b041524d1 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sun, 24 Feb 2008 01:17:17 -0600 Subject: Add some NULL checks into recently modified output functions After a merge with master where some strings we print (such as descriptions) could be NULL, a few segfaults popped up due to strlen() calls on null pointers. Fix this by doing some preemptive checks and returning from functions early if the string was null. Signed-off-by: Dan McGee --- src/pacman/callback.c | 2 +- src/pacman/util.c | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/pacman/callback.c b/src/pacman/callback.c index 629c2e1c..01e65a95 100644 --- a/src/pacman/callback.c +++ b/src/pacman/callback.c @@ -592,7 +592,7 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total, /* Callback to handle notifications from the library */ void cb_log(pmloglevel_t level, char *fmt, va_list args) { - if(strlen(fmt) == 0) { + if(!fmt || strlen(fmt) == 0) { return; } diff --git a/src/pacman/util.c b/src/pacman/util.c index 42436e26..aa08eb26 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -224,6 +224,10 @@ void indentprint(const char *str, int indent) const wchar_t *p; int len, cidx; + if(!str) { + return; + } + len = strlen(str) + 1; wcstr = calloc(len, sizeof(wchar_t)); len = mbstowcs(wcstr, str, len); @@ -401,14 +405,17 @@ void list_display(const char *title, const alpm_list_t *list) int cols, len; wchar_t *wcstr; - /* len goes from # bytes -> # chars -> # cols */ - len = strlen(title) + 1; - wcstr = calloc(len, sizeof(wchar_t)); - len = mbstowcs(wcstr, title, len); - len = wcswidth(wcstr, len); - free(wcstr); - - printf("%s ", title); + if(title) { + /* len goes from # bytes -> # chars -> # cols */ + len = strlen(title) + 1; + wcstr = calloc(len, sizeof(wchar_t)); + len = mbstowcs(wcstr, title, len); + len = wcswidth(wcstr, len); + free(wcstr); + printf("%s ", title); + } else { + len = 0; + } if(list) { for(i = list, cols = len; i; i = alpm_list_next(i)) { -- cgit v1.2.3