Index: usr.sbin/syslogd/syslogd.c =================================================================== --- usr.sbin/syslogd/syslogd.c +++ usr.sbin/syslogd/syslogd.c @@ -133,8 +133,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -142,9 +144,11 @@ #include #include #include +#include #include #include -#include +#include +#include #include "pathnames.h" #include "ttymsg.h" @@ -784,6 +788,9 @@ /* tuck my process id away */ pidfile_write(pfh); + /* set the character set locale */ + setlocale(LC_CTYPE, getenv("LC_CTYPE")); + dprintf("off & running....\n"); tvp = &tv; @@ -961,28 +968,44 @@ static void parsemsg_remove_unsafe_characters(const char *in, char *out, size_t outlen) { - char *q; - int c; + const char *p; + char *q, *end_q; + int c, max_len; + size_t n; + p = in; q = out; - while ((c = (unsigned char)*in++) != '\0' && q < out + outlen - 4) { - if (mask_C1 && (c & 0x80) && c < 0xA0) { - c &= 0x7F; - *q++ = 'M'; - *q++ = '-'; - } - if (isascii(c) && iscntrl(c)) { - if (c == '\n') { - *q++ = ' '; - } else if (c == '\t') { - *q++ = '\t'; - } else { - *q++ = '^'; - *q++ = c ^ 0100; + end_q = out + outlen - 1; + while (*p != '\0' && q < end_q) { + max_len = MB_LEN_MAX > (end_q - q) ? (end_q - q) : MB_LEN_MAX; + n = mbrtoc32(&c, p, max_len, NULL); + if (n == 0) { + break; + } else if (n == (size_t)-1 || n == (size_t)-2) { + /* Invalid char sequence for this charset. */ + c = *p; + n = (size_t)1; + } + + if (c == '\n') { + *q++ = ' '; + } else if (c == '\t') { + *q++ = '\t'; + } else if (!mask_C1 && (c & 0x80) && c < 0xA0) { + *q++ = c; + } else if (iswprint(c)) { + if (q > end_q - n) { + break; } + memcpy(q, p, n); + q += n; } else { - *q++ = c; + if (q > end_q - 4) { + break; + } + q = vis(q, c, 0, 0); } + p += n; } *q = '\0'; }