From 929bc9ee9e14fab839f0194d31cfc88c03b37f6a Mon Sep 17 00:00:00 2001 From: tg Date: Mon, 6 Feb 2012 17:42:24 +0000 Subject: [PATCH] make jobs reporting deal with UTF-8 (in utf8-mode) reported by Andrew Kudryashov, 10x --- check.t | 4 ++-- jobs.c | 6 +++--- sh.h | 4 ++-- tree.c | 51 +++++++++++++++++++++++++++++++++++---------------- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/check.t b/check.t index 89bd42f..999c001 100644 --- a/check.t +++ b/check.t @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/check.t,v 1.514 2012/01/29 01:41:09 tg Exp $ +# $MirOS: src/bin/mksh/check.t,v 1.515 2012/02/06 17:42:20 tg Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ @@ -29,7 +29,7 @@ # http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD expected-stdout: - @(#)MIRBSD KSH R40 2012/01/28 + @(#)MIRBSD KSH R40 2012/02/06 description: Check version of shell. stdin: diff --git a/jobs.c b/jobs.c index b43ef27..4f4e9ee 100644 --- a/jobs.c +++ b/jobs.c @@ -1,7 +1,7 @@ /* $OpenBSD: jobs.c,v 1.38 2009/12/12 04:28:44 deraadt Exp $ */ /*- - * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011 + * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012 * Thorsten Glaser * * Provided that these terms and disclaimer and all copyright notices @@ -22,7 +22,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.82 2011/12/31 00:52:22 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.83 2012/02/06 17:42:23 tg Exp $"); #if HAVE_KILLPG #define mksh_killpg killpg @@ -43,7 +43,7 @@ struct proc { pid_t pid; /* process id */ int state; int status; /* wait status */ - char command[48]; /* process command string */ + char command[44]; /* process command string */ }; /* Notify/print flag - j_print() argument */ diff --git a/sh.h b/sh.h index f5285ec..bd8e181 100644 --- a/sh.h +++ b/sh.h @@ -152,9 +152,9 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.520 2012/01/29 01:41:15 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.521 2012/02/06 17:42:23 tg Exp $"); #endif -#define MKSH_VERSION "R40 2012/01/28" +#define MKSH_VERSION "R40 2012/02/06" /* arithmetic types: C implementation */ #if !HAVE_CAN_INTTYPES diff --git a/tree.c b/tree.c index 2fdc59b..739bbcd 100644 --- a/tree.c +++ b/tree.c @@ -1,7 +1,8 @@ /* $OpenBSD: tree.c,v 1.19 2008/08/11 21:50:35 jaredy Exp $ */ /*- - * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 + * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, + * 2011, 2012 * Thorsten Glaser * * Provided that these terms and disclaimer and all copyright notices @@ -22,7 +23,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.55 2011/12/31 00:27:27 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.56 2012/02/06 17:42:24 tg Exp $"); #define INDENT 8 @@ -712,24 +713,42 @@ fpFUNCTf(struct shf *shf, int i, bool isksh, const char *k, struct op *v) void vistree(char *dst, size_t sz, struct op *t) { - int c; + unsigned int c; char *cp, *buf; + size_t n; - buf = alloc(sz, ATEMP); - snptreef(buf, sz, "%T", t); + buf = alloc(sz + 8, ATEMP); + snptreef(buf, sz + 8, "%T", t); cp = buf; - while ((c = *cp++)) { - if (((c & 0x60) == 0) || ((c & 0x7F) == 0x7F)) { - /* C0 or C1 control character or DEL */ - if (!--sz) - break; - *dst++ = (c & 0x80) ? '$' : '^'; - c = (c & 0x7F) ^ 0x40; - } - if (!--sz) - break; - *dst++ = c; + vist_loop: + if (UTFMODE && (n = utf_mbtowc(&c, cp)) != (size_t)-1) { + if (c == 0 || n >= sz) + /* NUL or not enough free space */ + goto vist_out; + /* copy multibyte char */ + sz -= n; + while (n--) + *dst++ = *cp++; + goto vist_loop; } + if (--sz == 0 || (c = (unsigned char)(*cp++)) == 0) + /* NUL or not enough free space */ + goto vist_out; + if ((c & 0x60) == 0 || (c & 0x7F) == 0x7F) { + /* C0 or C1 control character or DEL */ + if (--sz == 0) + /* not enough free space for two chars */ + goto vist_out; + *dst++ = (c & 0x80) ? '$' : '^'; + c = (c & 0x7F) ^ 0x40; + } else if (UTFMODE && c > 0x7F) { + /* better not try to display broken multibyte chars */ + c = '?'; + } + *dst++ = c; + goto vist_loop; + + vist_out: *dst = '\0'; afree(buf, ATEMP); }