# include <wchar.h>
#endif
-#ifdef LBL
-struct msgs *fmt_current_folder; /* current folder (set by main program) */
-#endif
-
extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
struct mailname fmt_mnull = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
NULL, NULL };
* no more than n bytes are copied
*/
static void
-cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
+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 */
#endif
char *sp; /* current position in source string */
char *cp = *dest; /* current position in destination string */
- char *ep = cp + n; /* end of destination buffer */
int prevCtrl = 1;
/* get alignment */
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);
- if (char_len <= 0 || (cp + char_len > ep))
+
+ if (char_len <= 0)
+ break;
+
+ w = wcwidth(wide_char);
+
+ /*
+ * Multibyte characters can have a variable number of column
+ * widths, so use the column width to bump the end pointer when
+ * appropriate.
+ */
+ if (char_len > 1 && epmax - *ep >= char_len - w) {
+ *ep += char_len - w;
+ }
+
+ if (cp + w > *ep)
break;
end -= char_len;
prevCtrl = 0;
#ifdef MULTIBYTE_SUPPORT
- w = wcwidth(wide_char);
if (w >= 0 && remaining >= w) {
strncpy(cp, sp, char_len);
cp += char_len;
}
if (ljust) {
- if (cp + remaining > ep)
- remaining = ep - cp;
- ep = cp + remaining;
+ char *endfield;
+ if (cp + remaining > *ep)
+ remaining = *ep - cp;
+ endfield = cp + remaining;
if (remaining > 0) {
/* copy string to the right */
while (--cp >= *dest)
for (c=remaining; c>0; c--)
*cp-- = fill;
}
- *dest = ep;
+ *dest = endfield;
} else {
/* pad remaining space */
- while (remaining-- > 0 && cp < ep)
+ while (remaining-- > 0 && cp < *ep)
*cp++ = fill;
*dest = cp;
}
}
static void
-cpstripped (char **dest, char *end, char *str)
+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;
+ int char_len, w;
wchar_t wide_char;
#endif /* MULTIBYTE_SUPPORT */
* then deal with that here.
*/
- while (*str != '\0' && len > 0 && *dest < end) {
+ 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 (char_len > 1 && max - *end >= char_len - w) {
+ *end += char_len - w;
+ }
- if (char_len <= 0 || *dest + char_len > end)
+ if (char_len <= 0 || *dest + char_len > *end)
break;
len -= char_len;
comp = fmt->f_comp;
- if (! (comp->c_flags & CF_TRIMMED) && comp->c_text) {
- i = strlen(comp->c_text);
+ if (! (comp->c_flags & CF_TRIMMED) && comp->c_text &&
+ (i = strlen(comp->c_text)) > 0) {
if (comp->c_text[i - 1] == '\n' &&
- strcmp(comp->c_name, "body") != 0 &&
- strcmp(comp->c_name, "text") != 0)
+ strcmp(comp->c_name, "body") != 0 &&
+ strcmp(comp->c_name, "text") != 0)
comp->c_text[i - 1] = '\0';
comp->c_flags |= CF_TRIMMED;
}
switch (fmt->f_type) {
case FT_COMP:
- cpstripped (&cp, ep, fmt->f_comp->c_text);
+ cpstripped (&cp, &ep, scanl + max - 1, fmt->f_comp->c_text);
break;
case FT_COMPF:
- cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
+ cptrimmed (&cp, &ep, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill,
+ scanl + max - 1);
break;
case FT_LIT:
break;
case FT_STR:
- cpstripped (&cp, ep, str);
+ cpstripped (&cp, &ep, scanl + max - 1, str);
break;
case FT_STRF:
- cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
+ cptrimmed (&cp, &ep, str, fmt->f_width, fmt->f_fill,
+ scanl + max - 1);
break;
case FT_STRLIT:
sp = str;
while ((c = *sp++) && cp < ep)
*cp++ = c;
break;
+ case FT_STRLITZ: {
+ size_t len = strlen (str);
+
+ /* Don't want to emit part of an escape sequence. So if
+ there isn't enough room in the buffer for the entire
+ string, skip it completely. */
+ if (cp - scanl + len + 1 < max) {
+ for (sp = str; *sp; *cp++ = *sp++) continue;
+
+ /* This string doesn't count against the width. So
+ increase ep the same amount as cp, only if the
+ scan buffer will always be large enough. */
+ if (ep - scanl + len + 1 < max) {
+ ep += len;
+ }
+ }
+
+ break;
+ }
case FT_STRFW:
adios (NULL, "internal error (FT_STRFW)");
*cp++ = ' ';
}
}
- cpstripped (&cp, ep, lp);
+ cpstripped (&cp, &ep, scanl + max - 1, lp);
}
break;
comp->c_mn = &fmt_mnull;
}
break;
+ }
+ fmt++;
+ }
- case FT_ADDTOSEQ:
-#ifdef LBL
- /* If we're working on a folder (as opposed to a file), add the
- * current msg to sequence given in literal field. Don't
- * disturb string or value registers.
- */
- if (fmt_current_folder)
- seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
-#endif
- break;
+ /* Emit any trailing sequences of zero display length. */
+ while (fmt->f_type != FT_DONE) {
+ if (fmt->f_type == FT_LS_LIT) {
+ str = fmt->f_text;
+ } else if (fmt->f_type == FT_STRLITZ) {
+ /* Don't want to emit part of an escape sequence. So if
+ there isn't enough room in the buffer for the entire
+ string, skip it completely. Need room for null
+ terminator, and maybe trailing newline (added below). */
+ if (cp - scanl + strlen (str) + 1 < max) {
+ for (sp = str; *sp; *cp++ = *sp++) continue;
+ }
}
fmt++;
}
-#ifndef JLR
+
finished:;
if (cp > scanl && cp[-1] != '\n') {
if (cp - scanl < (int) max - 1) {
}
*cp = '\0';
return ((struct format *)0);
-#else /* JLR */
- if (cp[-1] != '\n')
- *cp++ = '\n';
- while (fmt->f_type != FT_DONE)
- fmt++;
-
- finished:;
- *cp = '\0';
- return (fmt->f_value ? ++fmt : (struct format *) 0);
-
-#endif /* JLR */
}