-#define PUTS(cp, str) {\
- if ((sp = (str))) {\
- while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
- sp++;\
- while((c = (unsigned char) *sp++) && cp < ep)\
- if (isgraph(c)) \
- *cp++ = c;\
- else {\
- while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
- sp++;\
- *cp++ = ' ';\
- }\
- }\
+static void
+cpstripped (char **dest, char **end, char *max, char *str)
+{
+ 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;