From: David Levine Date: Mon, 25 Aug 2014 02:19:10 +0000 (-0500) Subject: Dynamically allocate space for the output of fmt_scan(), using X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/92128dacf8d5db02379e8f872dc50d31c6aaa55f?ds=sidebyside;hp=-c Dynamically allocate space for the output of fmt_scan(), using charstring_t, so a fixed sized output buffer is no longer needed. Also got rid of scanl global. --- 92128dacf8d5db02379e8f872dc50d31c6aaa55f diff --git a/h/fmt_scan.h b/h/fmt_scan.h index e35bfa20..2bcc265f 100644 --- a/h/fmt_scan.h +++ b/h/fmt_scan.h @@ -138,11 +138,11 @@ int fmt_compile (char *fstring, struct format **fmt, int reset); * Interpret a sequence of compiled format instructions. Arguments are: * * format - Array of format instructions generated by fmt_compile() - * scanl - Passed-in character array that will contain the output - * of the format instructions. Is always terminated with - * a newline (\n). - * max - Maximum number of bytes to be written to "scanl" (in other - * words, the buffer size). Includes the trailing NUL. + * scanl - Passed-in charstring_t object (created with + * charstring_create() and later destroyed with + * charstring_free()) that will contain the output of the + * format instructions. Is always terminated with a + * newline (\n). * width - Maximum number of displayed characters. Does not include * characters marked as nonprinting or (depending on the * encoding) bytes in a multibyte encoding that exceed the @@ -169,8 +169,8 @@ int fmt_compile (char *fstring, struct format **fmt, int reset); * execute, which is currently always NULL. */ -struct format *fmt_scan (struct format *format, char *scanl, size_t max, - int width, int *dat, struct fmt_callbacks *callbacks); +struct format *fmt_scan (struct format *format, charstring_t scanl, int width, + int *dat, struct fmt_callbacks *callbacks); /* * Free a format structure and/or component hash table. Arguments are: diff --git a/h/scansbr.h b/h/scansbr.h index f6cd35ca..d3a3993b 100644 --- a/h/scansbr.h +++ b/h/scansbr.h @@ -3,8 +3,6 @@ * scansbr.h -- definitions for scan() */ -extern char *scanl; - #define SCNENC 2 /* message just fine, but encrypted(!!) */ #define SCNMSG 1 /* message just fine */ #define SCNEOF 0 /* empty message */ @@ -37,4 +35,5 @@ extern char *scanl; /* * prototypes */ -int scan (FILE *, int, int, char *, int, int, int, char *, long, int); +int scan (FILE *, int, int, char *, int, int, int, char *, long, int, + charstring_t *); diff --git a/man/fmttest.man b/man/fmttest.man index 31de59d3..908b3cbe 100644 --- a/man/fmttest.man +++ b/man/fmttest.man @@ -1,4 +1,4 @@ -.TH FMTTEST %manext1% "December 4, 2013" "%nmhversion%" +.TH FMTTEST %manext1% "August 24, 2014" "%nmhversion%" .\" .\" %nmhwarning% .\" @@ -22,8 +22,6 @@ language .RB [ \-ccme " | " \-noccme ] .RB [ \-outsize .IR size-in-characters ] -.RB [ \-bufsize -.IR size-in-bytes ] .RB [ \-width .IR column-width ] .RB [ \-msgnum @@ -215,19 +213,11 @@ characters with zero width, and extra bytes that are part of a multibyte character are not counted against this total. Two special values are supported: .RI \*(lq max \*(rq, -which will set the value -to the size of the output buffer, and +which will set the value to the maximum value of an integer, and .RI \*(lq width \*(rq, which will set the value to the width of the terminal. In message mode it defaults to the -terminal width, otherwise the default is the output buffer size. -.PP -The -.B \-bufsize -switch controls the size of the output buffer. By default it is set -to the size of the -.B BUFSIZ -C preprocessor symbol, which is system-dependent. +terminal width, otherwise the default is the maximum value of an integer. .PP The .B \-width diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 8f59e4b4..998d3938 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -15,6 +15,7 @@ #include #include #include +#include #ifdef HAVE_SYS_TIME_H # include @@ -76,45 +77,54 @@ match (char *str, char *sub) /* * copy a number to the destination subject to a maximum width */ -static void -cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) { - int i, c; - char *sp; - char *cp = *dest; - char *ep = cp + n; - - if (cp + wid < ep) { - if ((i = (num)) < 0) - i = -(num); - if ((c = (wid)) < 0) - c = -c; - sp = cp + c; +void +cpnumber(charstring_t dest, int num, unsigned int wid, char fill, size_t max) { + if (wid < (num >= 0 ? max : max-1)) { + /* Build up the string representation of num in reverse. */ + charstring_t rev = charstring_create (0); + int i = num >= 0 ? num : -num; + do { - *--sp = (i % 10) + '0'; + charstring_push_back (rev, 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; + } while (--wid > 0 && i > 0); + if (i > 0) { + /* Overflowed the field (wid). */ + charstring_push_back (rev, '?'); + } else if (num < 0 && wid > 0) { + /* Shouldn't need the wid > 0 check, that's why the condition + at the top checks wid < max-1 when num < 0. */ + charstring_push_back (rev, '-'); + --wid; + } + while (wid-- > 0 && fill != 0) { + charstring_push_back (rev, fill); + } + + { + /* Output the string in reverse. */ + size_t b = charstring_bytes (rev); + const char *cp = b ? &charstring_buffer (rev)[b] : NULL; + + for (; b > 0; --b) { + charstring_push_back (dest, *--cp); + } + } + + charstring_free (rev); } - *dest = cp; } /* - * copy string from str to dest padding with the fill character to a size - * of wid characters. if wid is negative, the string is right aligned - * no more than n bytes are copied + * copy string from str to dest padding with the fill character to a + * size of wid characters. if wid is negative, the string is right + * aligned no more than max characters are copied */ -static void -cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill, - char *epmax) { +void +cptrimmed(charstring_t dest, char *str, int wid, char fill, size_t max) { int remaining; /* remaining output width available */ - int c, ljust; - int end; /* number of input bytes remaining in str */ + int rjust; + size_t end; /* number of input bytes remaining in str */ #ifdef MULTIBYTE_SUPPORT int char_len; /* bytes in current character */ int w; @@ -122,16 +132,17 @@ cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill, 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) { + rjust = 0; + if ((remaining = wid) < 0) { remaining = -remaining; - ljust++; + rjust++; } - if ((sp = (str))) { + if (remaining > (int) max) { remaining = max; } + + if ((sp = str)) { #ifdef MULTIBYTE_SUPPORT mbtowc(NULL, NULL, 0); /* reset shift state */ #endif @@ -151,22 +162,16 @@ cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill, char_len = mbtowc(&wide_char, altstr, 1); } - if (char_len <= 0) + 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 (w >= 0 && char_len > 1 && epmax - *ep >= char_len - w) { - *ep += char_len - w; - } - - if (w >= 0 && cp + w > *ep) + /* If w > remaining, w must be positive. */ + if (w > remaining) { break; + } end -= char_len; @@ -182,7 +187,7 @@ cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill, sp++; #endif if (!prevCtrl) { - *cp++ = ' '; + charstring_push_back (dest, ' '); remaining--; } @@ -193,44 +198,44 @@ cptrimmed(char **dest, char **ep, char *str, unsigned int wid, char fill, #ifdef MULTIBYTE_SUPPORT if (w >= 0 && remaining >= w) { - strncpy(cp, altstr ? altstr : sp, char_len); - cp += char_len; + charstring_push_back_chars (dest, altstr ? altstr : sp, + char_len, w); remaining -= w; altstr = NULL; } sp += char_len; #else - *cp++ = *sp++; + charstring_push_back (dest, *sp++); remaining--; #endif } } - if (ljust) { - char *endfield; - if (cp + remaining > *ep) - remaining = *ep - cp; - endfield = cp + remaining; + if (rjust) { if (remaining > 0) { /* copy string to the right */ - while (--cp >= *dest) - *(cp + remaining) = *cp; + charstring_t copy = charstring_copy (dest); + /* add padding at the beginning */ - cp += remaining; - for (c=remaining; c>0; c--) - *cp-- = fill; + charstring_clear (dest); + for (; remaining > 0; --remaining) { + charstring_push_back (dest, fill); + } + + charstring_append (dest, copy); + + charstring_free (copy); } - *dest = endfield; } else { /* pad remaining space */ - while (remaining-- > 0 && cp < *ep) - *cp++ = fill; - *dest = cp; + while (remaining-- > 0) { + charstring_push_back (dest, fill); + } } } static void -cpstripped (char **dest, char **end, char *max, char *str) +cpstripped (charstring_t dest, size_t max, char *str) { int prevCtrl = 1; /* This is 1 so we strip out leading spaces */ int len; @@ -240,8 +245,9 @@ cpstripped (char **dest, char **end, char *max, char *str) char *altstr = NULL; #endif /* MULTIBYTE_SUPPORT */ - if (!str) + if (!str) { return; + } len = strlen(str); @@ -254,20 +260,11 @@ cpstripped (char **dest, char **end, char *max, char *str) * then deal with that here. */ - while (*str != '\0' && len > 0 && *dest < *end) { + while (*str != '\0' && len > 0 && max > 0) { #ifdef MULTIBYTE_SUPPORT - char_len = mbtowc(&wide_char, str, len); + 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 @@ -281,8 +278,9 @@ cpstripped (char **dest, char **end, char *max, char *str) char_len = mbtowc(&wide_char, altstr, 1); } - if (char_len <= 0 || *dest + char_len > *end) + if (char_len <= 0) { break; + } len -= char_len; @@ -295,7 +293,8 @@ cpstripped (char **dest, char **end, char *max, char *str) str++; #endif /* MULTIBYTE_SUPPORT */ if (! prevCtrl) { - *(*dest)++ = ' '; + charstring_push_back (dest, ' '); + --max; } prevCtrl = 1; @@ -305,12 +304,13 @@ cpstripped (char **dest, char **end, char *max, char *str) prevCtrl = 0; #ifdef MULTIBYTE_SUPPORT - memcpy(*dest, altstr ? altstr : str, char_len); + charstring_push_back_chars (dest, altstr ? altstr : str, char_len, w); + max -= w; str += char_len; - *dest += char_len; altstr = NULL; #else /* MULTIBYE_SUPPORT */ - *(*dest)++ = *str++ + charstring_push_back (dest, *str++); + --max; #endif /* MULTIBYTE_SUPPORT */ } } @@ -364,25 +364,35 @@ get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len) } struct format * -fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, +fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat, struct fmt_callbacks *callbacks) { - char *cp, *ep, *sp; - char *savestr = NULL, *str = NULL; + char *sp; + char *savestr, *str; char buffer[BUFSIZ], buffer2[BUFSIZ]; - int i, c, ljust, n; - int value = 0; + int i, c, rjust; + int value; time_t t; + size_t max; struct format *fmt; struct comp *comp; struct tws *tws; struct mailname *mn; - /* ep keeps track of displayed characters. They're limited by width. - The total number of characters, cp - scanl + 1 (for trailing NULL), - includes invisible control characters and is limited by max. */ - cp = scanl; - ep = scanl + (width <= (int) max ? width : (int) max) - 1; + /* + * The newline counts in the display width, for backward + * compatibility. To change that so that the newline doesn't + * count, remove the following statement. + */ + --width; + + /* + * max is the same as width, but unsigned. So comparisons + * with charstring_chars() won't raise compile warning. + */ + max = width; + savestr = str = NULL; + value = 0; for (fmt = format; fmt->f_type != FT_DONE; fmt++) switch (fmt->f_type) { @@ -415,81 +425,70 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, fmt = format; - while (cp < ep) { + for ( ; charstring_chars (scanlp) < max; ) { switch (fmt->f_type) { case FT_COMP: - cpstripped (&cp, &ep, scanl + max - 1, fmt->f_comp->c_text); + cpstripped (scanlp, max - charstring_chars (scanlp), + fmt->f_comp->c_text); break; case FT_COMPF: - cptrimmed (&cp, &ep, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, - scanl + max - 1); + cptrimmed (scanlp, fmt->f_comp->c_text, fmt->f_width, + fmt->f_fill, max - charstring_chars (scanlp)); break; case FT_LIT: sp = fmt->f_text; - while( (c = *sp++) && cp < ep) - *cp++ = c; + while ((c = *sp++) && charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, c); + } break; case FT_LITF: sp = fmt->f_text; - ljust = 0; + rjust = 0; i = fmt->f_width; if (i < 0) { i = -i; - ljust++; /* XXX should do something with this */ + rjust++; /* XXX should do something with this */ + } + while ((c = *sp++) && --i >= 0 && charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, c); + } + while (--i >= 0 && charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, fmt->f_fill); } - while( (c = *sp++) && --i >= 0 && cp < ep) - *cp++ = c; - while( --i >= 0 && cp < ep) - *cp++ = fmt->f_fill; break; case FT_STR: - cpstripped (&cp, &ep, scanl + max - 1, str); + cpstripped (scanlp, max - charstring_chars (scanlp), str); break; case FT_STRF: - cptrimmed (&cp, &ep, str, fmt->f_width, fmt->f_fill, - scanl + max - 1); + cptrimmed (scanlp, str, fmt->f_width, fmt->f_fill, + max - charstring_chars (scanlp)); break; case FT_STRLIT: if (str) { sp = str; - while ((c = *sp++) && cp < ep) - *cp++ = c; + while ((c = *sp++) && charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, c); + } } break; case FT_STRLITZ: - if (str) { - 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; - } - } - } + if (str) charstring_push_back_chars (scanlp, str, strlen (str), 0); break; case FT_STRFW: adios (NULL, "internal error (FT_STRFW)"); - case FT_NUM: - n = snprintf(cp, ep - cp + 1, "%d", value); - if (n >= 0) { - if (n >= ep - cp) { - cp = ep; - } else - cp += n; - } + case FT_NUM: { + int num = value; + unsigned int wid; + + for (wid = num <= 0 ? 1 : 0; num; ++wid, num /= 10) {} + cpnumber (scanlp, value, wid, ' ', + max - charstring_chars (scanlp)); break; + } case FT_LS_KILO: case FT_LS_KIBI: { @@ -540,24 +539,25 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, } break; case FT_NUMF: - cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp); + cpnumber (scanlp, value, fmt->f_width, fmt->f_fill, + max - charstring_chars (scanlp)); break; case FT_CHAR: - *cp++ = fmt->f_char; + charstring_push_back (scanlp, fmt->f_char); break; case FT_DONE: if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); goto finished; case FT_IF_S: if (!(value = (str && *str))) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -567,7 +567,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (!(value = (str == NULL || *str == 0))) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -577,7 +577,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (value != fmt->f_value) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -587,7 +587,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (value == fmt->f_value) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -597,7 +597,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (value <= fmt->f_value) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -607,7 +607,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (!(value = (str && match (str, fmt->f_text)))) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -624,7 +624,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (!(value = (str && uprf (str, fmt->f_text)))) { if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; } @@ -657,7 +657,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, case FT_GOTO: if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt += fmt->f_skip; continue; @@ -700,19 +700,19 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, str = buffer; while (isspace((unsigned char) *str)) str++; - ljust = 0; + rjust = 0; if ((i = fmt->f_width) < 0) { i = -i; - ljust++; + rjust++; } - if (!ljust && i > 0 && (int) strlen(str) > i) + if (!rjust && i > 0 && (int) strlen(str) > i) str[i] = '\0'; xp = str; xp += strlen(str) - 1; while (xp > str && isspace((unsigned char) *xp)) *xp-- = '\0'; - if (ljust && i > 0 && (int) strlen(str) > i) + if (rjust && i > 0 && (int) strlen(str) > i) str += strlen(str) - i; } break; @@ -736,7 +736,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, value = 0; break; case FT_LV_CHAR_LEFT: - value = width - (cp - scanl); + value = max - charstring_bytes (scanlp); break; case FT_LV_PLUS_L: value += fmt->f_value; @@ -900,7 +900,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, } } else if (!(str = get_x400_friendly (mn->m_mbox, buffer, sizeof(buffer)))) { - unfriendly: ; + unfriendly: switch (mn->m_type) { case LOCALHOST: str = mn->m_mbox; @@ -960,7 +960,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, *comp->c_tws = *tws; comp->c_flags &= ~CF_TRUE; } else if ((comp->c_flags & CF_DATEFAB) == 0) { - memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws); + memset (comp->c_tws, 0, sizeof *comp->c_tws); comp->c_flags = CF_TRUE; } comp->c_flags |= CF_PARSED; @@ -1003,8 +1003,9 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, adios(NULL, "putaddr -- num register (%d) must be greater " "than label width (%d)", value, indent); } - while( (c = (unsigned char) *sp++) && cp < ep) - *cp++ = (char) c; + while ((c = *sp++) && charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, c); + } while (len > wid) { /* try to break at a comma; failing that, break at a * space. @@ -1025,18 +1026,22 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, } } len -= sp - lp + 1; - while (cp < ep && lp <= sp) - *cp++ = *lp++; + while (lp <= sp && charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, *lp++); + } while (isspace((unsigned char) *lp)) lp++, len--; if (*lp) { - if (cp < ep) - *cp++ = '\n'; - for (i=indent; cp < ep && i > 0; i--) - *cp++ = ' '; + if (charstring_chars (scanlp) < max) { + charstring_push_back (scanlp, '\n'); + } + for (i=indent; + charstring_chars (scanlp) < max && i > 0; + i--) + charstring_push_back (scanlp, ' '); } } - cpstripped (&cp, &ep, scanl + max - 1, lp); + cpstripped (scanlp, max - charstring_chars (scanlp), lp); } break; @@ -1098,7 +1103,7 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); fmt++; } @@ -1108,30 +1113,45 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat, str = fmt->f_text; if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); } 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 (str && (cp - scanl + strlen (str) + 1 < max)) { - for (sp = str; *sp; *cp++ = *sp++) continue; + if (str) { + for (sp = str; *sp; ++sp) { + charstring_push_back (scanlp, *sp); + } } if (callbacks && callbacks->trace_func) callbacks->trace_func(callbacks->trace_context, fmt, value, - str, scanl); + str, charstring_buffer (scanlp)); } fmt++; } - finished:; - if (cp > scanl && cp[-1] != '\n') { - if (cp - scanl < (int) max - 1) { - *cp++ = '\n'; - } else { - cp[-1] = '\n'; + finished: + if (charstring_bytes (scanlp) > 0) { + /* + * Append a newline if the last character wasn't. + */ +#ifdef MULTIBYTE_SUPPORT + /* + * It's a little tricky because the last byte might be part of + * a multibyte character, in which case we assume that wasn't + * a newline. + */ + size_t last_char_len = charstring_last_char_len (scanlp); +#else /* ! MULTIBYTE_SUPPORT */ + size_t last_char_len = 1; +#endif /* ! MULTIBYTE_SUPPORT */ + + if (last_char_len > 1 || + charstring_buffer (scanlp)[charstring_bytes (scanlp) - 1] != '\n') { + charstring_push_back (scanlp, '\n'); } } - *cp = '\0'; + return ((struct format *)0); } diff --git a/uip/ap.c b/uip/ap.c index c8cf4b25..1148871d 100644 --- a/uip/ap.c +++ b/uip/ap.c @@ -144,7 +144,7 @@ process (char *arg, int length) { int status = 0; register char *cp; - char buffer[WBUFSIZ + 1], error[BUFSIZ]; + char error[BUFSIZ]; register struct comp *cptr; register struct pqpair *p, *q; struct pqpair pq; @@ -167,6 +167,8 @@ process (char *arg, int length) } for (p = pq.pq_next; p; p = q) { + charstring_t scanl = charstring_create (length); + cptr = fmt_findcomp ("text"); if (cptr) { if (cptr->c_text) @@ -182,8 +184,9 @@ process (char *arg, int length) p->pq_error = NULL; } - fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL); - fputs (buffer, stdout); + fmt_scan (fmt, scanl, length, dat, NULL); + fputs (charstring_buffer (scanl), stdout); + charstring_free (scanl); if (p->pq_text) free (p->pq_text); diff --git a/uip/comp.c b/uip/comp.c index 02feefaa..2f7da416 100644 --- a/uip/comp.c +++ b/uip/comp.c @@ -371,18 +371,18 @@ try_it_again: if ((out = creat (drft, m_gmprot ())) == NOTOK) adios (drft, "unable to create"); if (cp) { - char *scanl; + charstring_t scanl; i = format_len + 1024; - scanl = mh_xmalloc((size_t) i + 2); + scanl = charstring_create (i + 2); dat[0] = 0; dat[1] = 0; dat[2] = 0; dat[3] = outputlinelen; dat[4] = 0; - fmt_scan(fmt, scanl, i + 1, i, dat, NULL); - write(out, scanl, strlen(scanl)); - free(scanl); + fmt_scan(fmt, scanl, i, dat, NULL); + write(out, charstring_buffer (scanl), charstring_bytes (scanl)); + charstring_free(scanl); } else { cpydata (in, out, form, drft); close (in); diff --git a/uip/dp.c b/uip/dp.c index dabdd74c..82d9963d 100644 --- a/uip/dp.c +++ b/uip/dp.c @@ -134,7 +134,7 @@ static int process (char *date, int length) { int status = 0; - char buffer[WBUFSIZ + 1]; + charstring_t scanl = charstring_create (length); register struct comp *cptr; cptr = fmt_findcomp ("text"); @@ -143,8 +143,9 @@ process (char *date, int length) free(cptr->c_text); cptr->c_text = getcpy(date); } - fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL); - fputs (buffer, stdout); + fmt_scan (fmt, scanl, length, dat, NULL); + fputs (charstring_buffer (scanl), stdout); + charstring_free (scanl); return status; } diff --git a/uip/fmttest.c b/uip/fmttest.c index 387003f3..ad9dcef0 100644 --- a/uip/fmttest.c +++ b/uip/fmttest.c @@ -29,7 +29,6 @@ X("ccme", 0, CCMESW) \ X("noccme", 0, NCCMESW) \ X("outsize size-in-characters", 0, OUTSIZESW) \ - X("bufsize size-in-bytes", 0, BUFSZSW) \ X("width column-width", 0, WIDTHSW) \ X("msgnum number", 0, MSGNUMSW) \ X("msgcur flag", 0, MSGCURSW) \ @@ -85,15 +84,16 @@ static char *c_typestr(int); static char *c_flagsstr(int); static void litputs(const char *); static void litputc(char); -static void process_addresses(struct format *, struct msgs_array *, char *, - int, int, int *, struct fmt_callbacks *); -static void process_raw(struct format *, struct msgs_array *, char *, - int, int, int *, struct fmt_callbacks *); +static void process_addresses(struct format *, struct msgs_array *, + charstring_t, int, int *, + struct fmt_callbacks *); +static void process_raw(struct format *, struct msgs_array *, charstring_t, + int, int *, struct fmt_callbacks *); static void process_messages(struct format *, struct msgs_array *, - struct msgs_array *, char *, char *, int, - int, int, int *, struct fmt_callbacks *); + struct msgs_array *, charstring_t, char *, int, + int, int *, struct fmt_callbacks *); static void process_single_file(FILE *, struct msgs_array *, int *, int, - struct format *, char *, int, int, + struct format *, charstring_t, int, struct fmt_callbacks *); static void test_trace(void *, struct format *, int, char *, const char *); static char *test_formataddr(char *, char *); @@ -110,12 +110,13 @@ int main (int argc, char **argv) { char *cp, *form = NULL, *format = NULL, *defformat = FORMAT, *folder = NULL; - char buf[BUFSIZ], *nfs, **argp, **arguments, *buffer; + char buf[BUFSIZ], *nfs, **argp, **arguments; + charstring_t buffer; struct format *fmt; struct comp *cptr; struct msgs_array msgs = { 0, 0, NULL }, compargs = { 0, 0, NULL}; int dump = 0, i; - int outputsize = 0, bufsize = 0, dupaddrs = 1, trace = 0, files = 0; + int outputsize = 0, dupaddrs = 1, trace = 0, files = 0; int colwidth = -1, msgnum = -1, msgcur = -1, msgsize = -1, msgunseen = -1; enum mode_t mode = MESSAGE; int dat[5]; @@ -164,17 +165,12 @@ main (int argc, char **argv) if (!(cp = *argp++) || *cp == '-') adios(NULL, "missing argument to %s", argp[-2]); if (strcmp(cp, "max") == 0) - outputsize = -1; + outputsize = INT_MAX; else if (strcmp(cp, "width") == 0) outputsize = sc_width(); else outputsize = atoi(cp); continue; - case BUFSZSW: - if (!(cp = *argp++) || *cp == '-') - adios(NULL, "missing argument to %s", argp[-2]); - bufsize = atoi(cp); - continue; case FORMSW: if (!(form = *argp++) || *form == '-') @@ -321,22 +317,13 @@ main (int argc, char **argv) } } - /* - * If we don't specify a buffer size, allocate a default one. - */ - - if (bufsize == 0) - bufsize = BUFSIZ; - - buffer = mh_xmalloc(bufsize); + buffer = charstring_create(BUFSIZ); - if (outputsize < 0) - outputsize = bufsize - 1; /* For the trailing NUL */ - else if (outputsize == 0) { + if (outputsize == 0) { if (mode == MESSAGE) outputsize = sc_width(); else - outputsize = bufsize - 1; + outputsize = INT_MAX; } dat[0] = msgnum; @@ -375,8 +362,8 @@ main (int argc, char **argv) } if (mode == MESSAGE) { - process_messages(fmt, &compargs, &msgs, buffer, folder, bufsize, - outputsize, files, dat, cbp); + process_messages(fmt, &compargs, &msgs, buffer, folder, outputsize, + files, dat, cbp); } else { if (compargs.size) { for (i = 0; i < compargs.size; i += 2) { @@ -387,12 +374,12 @@ main (int argc, char **argv) } if (mode == ADDRESS) { - process_addresses(fmt, &msgs, buffer, bufsize, outputsize, - dat, cbp); + process_addresses(fmt, &msgs, buffer, outputsize, dat, cbp); } else /* Fall-through for RAW or DATE */ - process_raw(fmt, &msgs, buffer, bufsize, outputsize, dat, cbp); + process_raw(fmt, &msgs, buffer, outputsize, dat, cbp); } + charstring_free(buffer); fmt_free(fmt, 1); done(0); @@ -410,8 +397,9 @@ struct pqpair { }; static void -process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer, - int bufsize, int outwidth, int *dat, struct fmt_callbacks *cb) +process_addresses(struct format *fmt, struct msgs_array *addrs, + charstring_t buffer, int outwidth, int *dat, + struct fmt_callbacks *cb) { int i; char *cp, error[BUFSIZ]; @@ -460,8 +448,8 @@ process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer, p->pq_error = NULL; } - fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb); - fputs(buffer, stdout); + fmt_scan(fmt, buffer, outwidth, dat, cb); + fputs(charstring_buffer(buffer), stdout); mlistfree(); if (p->pq_text) @@ -481,8 +469,8 @@ process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer, static void process_messages(struct format *fmt, struct msgs_array *comps, - struct msgs_array *msgs, char *buffer, char *folder, - int bufsize, int outwidth, int files, int *dat, + struct msgs_array *msgs, charstring_t buffer, char *folder, + int outwidth, int files, int *dat, struct fmt_callbacks *cb) { int i, msgnum, msgsize = dat[2], num = dat[0], cur = dat[1]; @@ -504,7 +492,7 @@ process_messages(struct format *fmt, struct msgs_array *comps, continue; } process_single_file(in, comps, dat, msgsize, fmt, buffer, - bufsize, outwidth, cb); + outwidth, cb); } return; @@ -585,7 +573,7 @@ process_messages(struct format *fmt, struct msgs_array *comps, */ process_single_file(in, comps, dat, msgsize, fmt, buffer, - bufsize, outwidth, cb); + outwidth, cb); } } @@ -600,8 +588,8 @@ process_messages(struct format *fmt, struct msgs_array *comps, static void process_single_file(FILE *in, struct msgs_array *comps, int *dat, int msgsize, - struct format *fmt, char *buffer, int bufsize, - int outwidth, struct fmt_callbacks *cb) + struct format *fmt, charstring_t buffer, int outwidth, + struct fmt_callbacks *cb) { int i, state; char name[NAMESZ], rbuf[BUFSIZ]; @@ -691,8 +679,8 @@ finished: } } } - fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb); - fputs(buffer, stdout); + fmt_scan(fmt, buffer, outwidth, dat, cb); + fputs(charstring_buffer (buffer), stdout); mlistfree(); } @@ -701,8 +689,8 @@ finished: */ static void -process_raw(struct format *fmt, struct msgs_array *text, char *buffer, - int bufsize, int outwidth, int *dat, struct fmt_callbacks *cb) +process_raw(struct format *fmt, struct msgs_array *text, charstring_t buffer, + int outwidth, int *dat, struct fmt_callbacks *cb) { int i; struct comp *c; @@ -725,8 +713,8 @@ process_raw(struct format *fmt, struct msgs_array *text, char *buffer, c->c_text = getcpy(text->msgs[i]); } - fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb); - fputs(buffer, stdout); + fmt_scan(fmt, buffer, outwidth, dat, cb); + fputs(charstring_buffer (buffer), stdout); mlistfree(); } } diff --git a/uip/forwsbr.c b/uip/forwsbr.c index 295507d6..97d1f3d3 100644 --- a/uip/forwsbr.c +++ b/uip/forwsbr.c @@ -49,7 +49,8 @@ build_form (char *form, char *digest, int *dat, char *from, char *to, int fmtsize, state; int i; register char *nfs; - char *line, tmpfil[BUFSIZ], name[NAMESZ], **ap; + char tmpfil[BUFSIZ], name[NAMESZ], **ap; + charstring_t line; FILE *tmp; register struct comp *cptr; struct format *fmt; @@ -182,10 +183,10 @@ finished: if ((in = dup (fileno (tmp))) == NOTOK) adios ("dup", "unable to"); - line = mh_xmalloc ((unsigned) fmtsize); - fmt_scan (fmt, line, fmtsize - 1, fmtsize, dat, NULL); - fputs (line, tmp); - free (line); + line = charstring_create (fmtsize); + fmt_scan (fmt, line, fmtsize, dat, NULL); + fputs (charstring_buffer (line), tmp); + charstring_free (line); if (fclose (tmp)) adios (tmpfil, "error writing"); diff --git a/uip/inc.c b/uip/inc.c index 7673da32..c2213f13 100644 --- a/uip/inc.c +++ b/uip/inc.c @@ -587,6 +587,8 @@ go_to_it: } for (i = 1; i <= nmsgs; i++) { + charstring_t scanl = NULL; + msgnum++; if (packfile) { fseek (pf, 0L, SEEK_CUR); @@ -619,7 +621,7 @@ go_to_it: } switch (incerr = scan (pf, msgnum, 0, nfs, width, packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag, - 1, NULL, stop - start, noisy)) { + 1, NULL, stop - start, noisy, &scanl)) { case SCNEOF: printf ("%*d empty\n", DMAXFOLDER, msgnum); break; @@ -638,11 +640,13 @@ go_to_it: case SCNENC: default: if (aud) - fputs (scanl, aud); + fputs (charstring_buffer (scanl), aud); if (noisy) fflush (stdout); break; } + charstring_free (scanl); + if (packfile) { fseek (pf, stop, SEEK_SET); fwrite (mmdlm2, 1, strlen (mmdlm2), pf); @@ -685,9 +689,12 @@ go_to_it: scan_detect_mbox_style (in); /* the MAGIC invocation... */ hghnum = msgnum = mp->hghmsg; for (;;) { + charstring_t scanl = NULL; + /* create scanline for new message */ switch (incerr = scan (in, msgnum + 1, msgnum + 1, nfs, width, - msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) { + msgnum == hghnum && chgflag, 1, NULL, 0L, noisy, + &scanl)) { case SCNFAT: case SCNEOF: break; @@ -716,13 +723,15 @@ go_to_it: (void)ext_hook("add-hook", b, (char *)0); if (aud) - fputs (scanl, aud); + fputs (charstring_buffer (scanl), aud); if (noisy) fflush (stdout); msgnum++; continue; } + charstring_free (scanl); + /* If we get here there was some sort of error from scan(), * so stop processing anything more from the spool. */ @@ -735,6 +744,8 @@ go_to_it: hghnum = msgnum = mp->hghmsg; for (i = 0; i < num_maildir_entries; i++) { + charstring_t scanl = NULL; + msgnum++; sp = Maildir[i].filename; @@ -767,7 +778,7 @@ go_to_it: fseek (pf, 0L, SEEK_SET); switch (incerr = scan (pf, msgnum, 0, nfs, width, msgnum == mp->hghmsg + 1 && chgflag, - 1, NULL, stop - start, noisy)) { + 1, NULL, stop - start, noisy, &scanl)) { case SCNEOF: printf ("%*d empty\n", DMAXFOLDER, msgnum); break; @@ -793,11 +804,13 @@ go_to_it: (void)ext_hook("add-hook", b, (char *)0); if (aud) - fputs (scanl, aud); + fputs (charstring_buffer (scanl), aud); if (noisy) fflush (stdout); break; } + charstring_free (scanl); + if (ferror(pf) || fclose (pf)) { int e = errno; (void) m_unlink (cp); diff --git a/uip/mhlsbr.c b/uip/mhlsbr.c index 39f42801..3a53941d 100644 --- a/uip/mhlsbr.c +++ b/uip/mhlsbr.c @@ -542,7 +542,7 @@ mhl_format (char *file, int length, int width) { int i; char *bp, *cp, **ip; - char *ap, buffer[BUFSIZ], name[NAMESZ]; + char *ap, name[NAMESZ]; struct mcomp *c1; struct stat st; FILE *fp; @@ -663,6 +663,8 @@ mhl_format (char *file, int length, int width) if (mhldebug) { for (c1 = fmthd; c1; c1 = c1->c_next) { + char buffer[BUFSIZ]; + fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt); fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n", @@ -1143,7 +1145,7 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2) { int dat[5]; char *ap, *cp; - char buffer[BUFSIZ], error[BUFSIZ]; + char error[BUFSIZ]; struct pqpair *p, *q; struct pqpair pq; struct mailname *mp; @@ -1153,20 +1155,22 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2) dat[0] = 0; dat[1] = 0; dat[2] = filesize; - dat[3] = sizeof(buffer) - 1; + dat[3] = BUFSIZ - 1; dat[4] = 0; if (!(c1->c_flags & ADDRFMT)) { + charstring_t scanl = charstring_create (BUFSIZ); + if (c1->c_c_text) c1->c_c_text->c_text = ap; if ((cp = strrchr(ap, '\n'))) /* drop ending newline */ if (!cp[1]) *cp = 0; - fmt_scan (c1->c_fmt, buffer, sizeof buffer - 1, sizeof buffer - 1, - dat, NULL); + fmt_scan (c1->c_fmt, scanl, BUFSIZ - 1, dat, NULL); /* Don't need to append a newline, dctime() already did */ - c2->c_text = getcpy (buffer); + c2->c_text = charstring_buffer_copy (scanl); + charstring_free (scanl); /* ap is now owned by the component struct, so do NOT free it here */ return; @@ -1188,6 +1192,9 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2) } for (p = pq.pq_next; p; p = q) { + charstring_t scanl = charstring_create (BUFSIZ); + char *buffer; + if (c1->c_c_text) { c1->c_c_text->c_text = p->pq_text; p->pq_text = NULL; @@ -1197,15 +1204,16 @@ mcomp_format (struct mcomp *c1, struct mcomp *c2) p->pq_error = NULL; } - fmt_scan (c1->c_fmt, buffer, sizeof buffer - 1, sizeof buffer - 1, - dat, NULL); - if (*buffer) { + fmt_scan (c1->c_fmt, scanl, BUFSIZ - 1, dat, NULL); + buffer = charstring_buffer_copy (scanl); + if (strlen (buffer) > 0) { if (c2->c_text) c2->c_text = add (",\n", c2->c_text); if (*(cp = buffer + strlen (buffer) - 1) == '\n') *cp = 0; c2->c_text = add (buffer, c2->c_text); } + charstring_free (scanl); if (p->pq_text) free (p->pq_text); @@ -1758,22 +1766,25 @@ filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp, */ for (a = arglist_head, i = argp; a != NULL; a = a->a_next, i++) { - args[i] = mh_xmalloc(BUFSIZ); - fmt_scan(a->a_fmt, args[i], BUFSIZ - 1, BUFSIZ, dat, NULL); + charstring_t scanl = charstring_create (BUFSIZ); + + fmt_scan(a->a_fmt, scanl, BUFSIZ, dat, NULL); + args[i] = charstring_buffer_copy (scanl); + charstring_free (scanl); /* * fmt_scan likes to put a trailing newline at the end of the * format string. If we have one, get rid of it. */ s = strlen(args[i]); if (args[i][s - 1] == '\n') - args[i][s - 1] = '\0'; + args[i][s - 1] = '\0'; if (mhldebug) - fprintf(stderr, "filterarg: fmt=\"%s\", output=\"%s\"\n", + fprintf(stderr, "filterarg: fmt=\"%s\", output=\"%s\"\n", a->a_nfs, args[i]); } - if (dup2(fdinput[0], STDIN_FILENO) < 0) { + if (dup2(fdinput[0], STDIN_FILENO) < 0) { adios("formatproc", "Unable to dup2() standard input"); } if (dup2(fdoutput[1], STDOUT_FILENO) < 0) { diff --git a/uip/mhshowsbr.c b/uip/mhshowsbr.c index 4a51dd1c..d99f5827 100644 --- a/uip/mhshowsbr.c +++ b/uip/mhshowsbr.c @@ -1277,7 +1277,7 @@ compile_marker(char *markerform) static void output_marker(CT ct, struct format *fmt, int hidden) { - char outbuf[BUFSIZ]; + charstring_t outbuf = charstring_create (BUFSIZ); struct param_comp_list *pcentry; int partsize; int dat[5]; @@ -1326,9 +1326,10 @@ output_marker(CT ct, struct format *fmt, int hidden) dat[4] = hidden; dat[0] = dat[1] = dat[3] = 0; - fmt_scan(fmt, outbuf, sizeof(outbuf), sizeof(outbuf), dat, NULL); + fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL); - fputs(outbuf, stdout); + fputs(charstring_buffer (outbuf), stdout); + charstring_free (outbuf); fmt_freecomptext(); } diff --git a/uip/rcvdist.c b/uip/rcvdist.c index 85baf33d..6d144be1 100644 --- a/uip/rcvdist.c +++ b/uip/rcvdist.c @@ -175,7 +175,8 @@ rcvdistout (FILE *inb, char *form, char *addrs) { register int char_read = 0, format_len, i, state; register char **ap; - char *cp, *scanl, name[NAMESZ], tmpbuf[SBUFSIZ]; + char *cp, name[NAMESZ], tmpbuf[SBUFSIZ]; + charstring_t scanl; register struct comp *cptr; FILE *out; m_getfld_state_t gstate = 0; @@ -234,17 +235,17 @@ finished: ; m_getfld_state_destroy (&gstate); i = format_len + char_read + 256; - scanl = mh_xmalloc ((size_t) i + 2); + scanl = charstring_create (i + 2); dat[0] = dat[1] = dat[2] = dat[4] = 0; dat[3] = outputlinelen; - fmt_scan (fmt, scanl, i + 1, i, dat, NULL); - fputs (scanl, out); + fmt_scan (fmt, scanl, i, dat, NULL); + fputs (charstring_buffer (scanl), out); if (ferror (out)) adios (drft, "error writing"); fclose (out); - free (scanl); + charstring_free (scanl); fmt_free(fmt, 1); } diff --git a/uip/rcvtty.c b/uip/rcvtty.c index 3bee075e..d91b6a4e 100644 --- a/uip/rcvtty.c +++ b/uip/rcvtty.c @@ -91,14 +91,14 @@ main (int argc, char **argv) while ((cp = *argp++)) { if (*cp == '-') { switch (smatch (++cp, switches)) { - case AMBIGSW: + case AMBIGSW: ambigsw (cp, switches); done (1); - case UNKWNSW: + case UNKWNSW: vec[vecp++] = --cp; continue; - case HELPSW: + case HELPSW: snprintf (buf, sizeof(buf), "%s [command ...]", invo_name); print_help (buf, switches, 1); done (0); @@ -110,12 +110,12 @@ main (int argc, char **argv) biff = 1; continue; - case FORMSW: + case FORMSW: if (!(form = *argp++) || *form == '-') adios (NULL, "missing argument to %s", argp[-2]); format = NULL; continue; - case FMTSW: + case FMTSW: if (!(format = *argp++) || *format == '-') adios (NULL, "missing argument to %s", argp[-2]); form = NULL; @@ -251,6 +251,7 @@ header_fd (void) int fd; char *nfs; char *tfile = NULL; + charstring_t scanl = NULL; if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) { advise(NULL, "unable to create temporary file in %s", get_temp_dir()); @@ -262,11 +263,12 @@ header_fd (void) /* get new format string */ nfs = new_fs (form, format, SCANFMT); - scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0); + scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0, &scanl); scan_finished (); if (newline) write (fd, "\n\r", 2); - write (fd, scanl, strlen (scanl)); + write (fd, charstring_buffer (scanl), charstring_bytes (scanl)); + charstring_free (scanl); if (bell) write (fd, "\007", 1); diff --git a/uip/replsbr.c b/uip/replsbr.c index 6a906b44..aec090fe 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -71,7 +71,8 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen, struct format *fmt; register char **ap; int char_read = 0, format_len, mask; - char name[NAMESZ], *scanl, *cp; + char name[NAMESZ], *cp; + charstring_t scanl; static int dat[5]; /* aux. data for format routine */ m_getfld_state_t gstate = 0; struct fmt_callbacks cb; @@ -204,7 +205,7 @@ finished: } } i = format_len + char_read + 256; - scanl = mh_xmalloc ((size_t) i + 2); + scanl = charstring_create (i + 2); dat[0] = 0; dat[1] = 0; dat[2] = 0; @@ -213,8 +214,8 @@ finished: memset(&cb, 0, sizeof(cb)); cb.formataddr = replformataddr; cb.concataddr = replconcataddr; - fmt_scan (fmt, scanl, i + 1, i, dat, &cb); - fputs (scanl, out); + fmt_scan (fmt, scanl, i, dat, &cb); + fputs (charstring_buffer (scanl), out); if (badaddrs) { fputs ("\nrepl: bad addresses:\n", out); fputs ( badaddrs, out); @@ -241,7 +242,7 @@ finished: fclose (out); /* return dynamically allocated buffers */ - free (scanl); + charstring_free (scanl); fmt_free(fmt, 1); } diff --git a/uip/scan.c b/uip/scan.c index 20668691..8ba50367 100644 --- a/uip/scan.c +++ b/uip/scan.c @@ -165,8 +165,11 @@ main (int argc, char **argv) scan_detect_mbox_style (in); for (msgnum = 1; ; ++msgnum) { + charstring_t scanl = NULL; + state = scan (in, msgnum, -1, nfs, width, 0, 0, - hdrflag ? file : NULL, 0L, 1); + hdrflag ? file : NULL, 0L, 1, &scanl); + charstring_free (scanl); if (state != SCNMSG && state != SCNENC) break; } @@ -229,6 +232,8 @@ main (int argc, char **argv) (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel); msgnum += (revflag ? -1 : 1)) { if (is_selected(mp, msgnum)) { + charstring_t scanl = NULL; + if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) { admonish (cp, "unable to open message"); continue; @@ -252,7 +257,7 @@ main (int argc, char **argv) switch (state = scan (in, msgnum, 0, nfs, width, msgnum == mp->curmsg, unseen, - folder, 0L, 1)) { + folder, 0L, 1, &scanl)) { case SCNMSG: case SCNENC: case SCNERR: @@ -265,6 +270,7 @@ main (int argc, char **argv) advise (NULL, "message %d: empty", msgnum); break; } + charstring_free (scanl); scan_finished (); hdrflag = 0; fclose (in); diff --git a/uip/scansbr.c b/uip/scansbr.c index abcb4962..97df2c3a 100644 --- a/uip/scansbr.c +++ b/uip/scansbr.c @@ -35,7 +35,6 @@ static struct comp **used_buf = 0; /* stack for comp that use buffers */ static int dat[5]; /* aux. data for format routine */ -char *scanl = 0; /* text of most recent scanline */ m_getfld_state_t gstate; /* for access by msh */ #define DIEWRERR() adios (scnmsg, "write error on") @@ -50,15 +49,9 @@ m_getfld_state_t gstate; /* for access by msh */ */ static int mh_fputs(char *, FILE *); -#ifdef MULTIBYTE_SUPPORT -#define SCAN_CHARWIDTH MB_CUR_MAX -#else -#define SCAN_CHARWIDTH 1 -#endif - int scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, - int unseen, char *folder, long size, int noisy) + int unseen, char *folder, long size, int noisy, charstring_t *scanl) { int i, compnum, encrypted, state; char *cp, *tmpbuf, *startbody, **nxtbuf; @@ -70,10 +63,11 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, char name[NAMESZ]; int bufsz; static int rlwidth, slwidth; - static size_t scanl_size; - /* first-time only initialization */ - if (!scanl) { + /* first-time only initialization, which will always happen the + way the code is now, with callers initializing *scanl to NULL. + scanl used to be a global. */ + if (! *scanl) { if (width == 0) { if ((width = sc_width ()) < WIDTH/2) width = WIDTH/2; @@ -81,10 +75,7 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, width = MAXSCANL; } dat[3] = slwidth = width; - /* Arbitrarily allocate 20 * slwidth to provide room for lots - of escape sequences. */ - scanl_size = SCAN_CHARWIDTH * (20 * slwidth + 2); - scanl = (char *) mh_xmalloc (scanl_size); + *scanl = charstring_create (width); if (outnum) umask(~m_gmprot()); @@ -346,13 +337,13 @@ finished: } } - fmt_scan (fmt, scanl, scanl_size, slwidth, dat, NULL); + fmt_scan (fmt, *scanl, slwidth, dat, NULL); if (bodycomp) bodycomp->c_text = saved_c_text; if (noisy) - fputs (scanl, stdout); + fputs (charstring_buffer (*scanl), stdout); cptr = fmt_findcomp ("encrypted"); encrypted = cptr && cptr->c_text;