-#define PUTDF(cp, num, wid, fill)\
- if (cp + wid < ep) {\
- if ((i = (num)) < 0)\
- i = -(num);\
- if ((c = (wid)) < 0)\
- c = -c;\
- sp = cp + c;\
- do {\
- *--sp = (i % 10) + '0';\
- i /= 10;\
- } while (i > 0 && sp > cp);\
- if (i > 0)\
- *sp = '?';\
- else if ((num) < 0 && sp > cp)\
- *--sp = '-';\
- while (sp > cp)\
- *--sp = fill;\
- cp += c;\
+static void
+cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill,
+ char *epmax) {
+ int remaining; /* remaining output width available */
+ int c, ljust;
+ int end; /* number of input bytes remaining in str */
+#ifdef MULTIBYTE_SUPPORT
+ int char_len; /* bytes in current character */
+ int w;
+ wchar_t wide_char;
+ char *altstr = NULL;
+#endif
+ char *sp; /* current position in source string */
+ char *cp = *dest; /* current position in destination string */
+ int prevCtrl = 1;
+
+ /* get alignment */
+ ljust = 0;
+ if ((remaining = (wid)) < 0) {
+ remaining = -remaining;
+ ljust++;
+ }
+ if ((sp = (str))) {
+#ifdef MULTIBYTE_SUPPORT
+ mbtowc(NULL, NULL, 0); /* reset shift state */
+#endif
+ end = strlen(str);
+ while (*sp && remaining > 0 && end > 0) {
+#ifdef MULTIBYTE_SUPPORT
+ char_len = mbtowc(&wide_char, sp, end);
+
+ /*
+ * See the relevant comments in cpstripped() to explain what's
+ * going on here; we want to handle the case where we get
+ * characters that mbtowc() cannot handle
+ */
+
+ if (char_len < 0) {
+ altstr = "?";
+ char_len = mbtowc(&wide_char, altstr, 1);