- int prevCtrl = 1; /* This is 1 so we strip out leading spaces */
- int len;
-#ifdef MULTIBYTE_SUPPORT
- int char_len, w;
- wchar_t wide_char;
- char *altstr = NULL;
-#endif /* MULTIBYTE_SUPPORT */
-
- if (!str)
- return;
-
- len = strlen(str);
-
-#ifdef MULTIBYTE_SUPPORT
- mbtowc(NULL, NULL, 0); /* Reset shift state */
-#endif /* MULTIBYTE_SUPPORT */
-
- /*
- * Process each character at a time; if we have multibyte support
- * then deal with that here.
- */
-
- while (*str != '\0' && len > 0 && *dest < *end) {
-#ifdef MULTIBYTE_SUPPORT
- char_len = mbtowc(&wide_char, str, len);
- w = wcwidth(wide_char);
-
- /*
- * Account for multibyte characters, and increment the end pointer
- * by the number of "extra" bytes in this character. That's the
- * character length (char_len) minus the column width (w).
- */
- if (w >= 0 && char_len > 1 && max - *end >= char_len - w) {
- *end += char_len - w;
- }
-
- /*
- * If mbrtowc() failed, then we have a character that isn't valid
- * in the current encoding. Replace it with a '?'. We do that by
- * setting the alstr variable to the value of the replacement string;
- * altstr is used below when the bytes are copied into the output
- * buffer.
- */
-
- if (char_len < 0) {
- altstr = "?";
- char_len = mbtowc(&wide_char, altstr, 1);
- }