=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/column/column.c,v retrieving revision 1.24 retrieving revision 1.25 diff -c -r1.24 -r1.25 *** src/usr.bin/column/column.c 2016/08/31 20:43:57 1.24 --- src/usr.bin/column/column.c 2016/09/04 20:33:36 1.25 *************** *** 1,4 **** ! /* $OpenBSD: column.c,v 1.24 2016/08/31 20:43:57 martijn Exp $ */ /* $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $ */ /* --- 1,4 ---- ! /* $OpenBSD: column.c,v 1.25 2016/09/04 20:33:36 martijn Exp $ */ /* $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $ */ /* *************** *** 36,45 **** --- 36,48 ---- #include #include #include + #include #include #include #include #include + #include + #include void c_columnate(void); void *ereallocarray(void *, size_t, size_t); *************** *** 60,66 **** int eval; /* exit value */ int *maxwidths; /* longest record per column */ struct field **table; /* one array of pointers per line */ ! char *separator = "\t "; /* field separator for table option */ int main(int argc, char *argv[]) --- 63,69 ---- int eval; /* exit value */ int *maxwidths; /* longest record per column */ struct field **table; /* one array of pointers per line */ ! wchar_t *separator = L"\t "; /* field separator for table option */ int main(int argc, char *argv[]) *************** *** 71,76 **** --- 74,81 ---- char *p; const char *errstr; + setlocale(LC_CTYPE, ""); + termwidth = 0; if ((p = getenv("COLUMNS")) != NULL) termwidth = strtonum(p, 1, INT_MAX, NULL); *************** *** 92,98 **** errx(1, "%s: %s", errstr, optarg); break; case 's': ! separator = optarg; break; case 't': tflag = 1; --- 97,108 ---- errx(1, "%s: %s", errstr, optarg); break; case 's': ! if ((separator = reallocarray(NULL, strlen(optarg) + 1, ! sizeof(*separator))) == NULL) ! err(1, NULL); ! if (mbstowcs(separator, optarg, strlen(optarg) + 1) == ! (size_t) -1) ! err(1, "sep"); break; case 't': tflag = 1; *************** *** 106,112 **** } if (!tflag) ! separator = ""; argv += optind; if (*argv == NULL) { --- 116,122 ---- } if (!tflag) ! separator = L""; argv += optind; if (*argv == NULL) { *************** *** 222,231 **** static int maxentry = 0; static int maxcols = 0; static struct field *cols = NULL; ! int col, width; size_t blen; ssize_t llen; char *p, *s, *buf = NULL; while ((llen = getline(&buf, &blen, fp)) > -1) { if (buf[llen - 1] == '\n') --- 232,243 ---- static int maxentry = 0; static int maxcols = 0; static struct field *cols = NULL; ! int col, width, twidth; size_t blen; ssize_t llen; char *p, *s, *buf = NULL; + wchar_t wc; + int wlen; while ((llen = getline(&buf, &blen, fp)) > -1) { if (buf[llen - 1] == '\n') *************** *** 236,251 **** /* Skip lines containing nothing but whitespace. */ ! for (s = p; s != '\0'; s++) ! if (!isspace((unsigned char)*s)) break; if (*s == '\0') break; /* Skip leading, multiple, and trailing separators. */ ! while (*p != '\0' && strchr(separator, *p) != NULL) ! p++; if (*p == '\0') break; --- 248,265 ---- /* Skip lines containing nothing but whitespace. */ ! for (s = p; (wlen = mbtowc(&wc, s, MB_CUR_MAX)) > 0; ! s += wlen) ! if (!iswspace(wc)) break; if (*s == '\0') break; /* Skip leading, multiple, and trailing separators. */ ! while ((wlen = mbtowc(&wc, p, MB_CUR_MAX)) > 0 && ! wcschr(separator, wc) != NULL) ! p += wlen; if (*p == '\0') break; *************** *** 256,266 **** s = p; width = 0; ! while (*p != '\0' && strchr(separator, *p) == NULL) { ! if (*p++ == '\t') ! INCR_NEXTTAB(width); ! else width++; } if (col + 1 >= maxcols) { --- 270,290 ---- s = p; width = 0; ! while (*p != '\0') { ! if ((wlen = mbtowc(&wc, p, MB_CUR_MAX)) == -1) { width++; + p++; + continue; + } + if (wcschr(separator, wc) != NULL) + break; + if (*p == '\t') + INCR_NEXTTAB(width); + else { + width += (twidth = wcwidth(wc)) == -1 ? + 1 : twidth; + } + p += wlen; } if (col + 1 >= maxcols) { *************** *** 284,291 **** cols[col].width = width; if (maxwidths[col] < width) maxwidths[col] = width; ! if (*p != '\0') ! *p++ = '\0'; if ((cols[col].content = strdup(s)) == NULL) err(1, NULL); } --- 308,317 ---- cols[col].width = width; if (maxwidths[col] < width) maxwidths[col] = width; ! if (*p != '\0') { ! *p = '\0'; ! p += wlen; ! } if ((cols[col].content = strdup(s)) == NULL) err(1, NULL); }