+static void text_plain_flowed_handler (BODY *a, STATE *s)
+{
+ int bytes = a->length;
+ char buf[LONG_STRING];
+ char * curline = strdup("");
+ char * t;
+ unsigned int curline_len = 1;
+ unsigned int quotelevel = 0, newql = 0;
+ int append_next_line = 0;
+ int first_line = 1;
+
+ while (bytes > 0 && fgets(buf,sizeof(buf),s->fpin)) {
+
+ /* fprintf(stderr,"read `%s'",buf); */
+ bytes -= strlen(buf);
+
+ newql = get_quote_level(buf);
+
+ if ((t=strrchr(buf,'\n')) || (t=strrchr(buf,'\r'))) {
+ *t = '\0';
+ if (strlen(curline)>0 && curline[strlen(curline)-1]==' ' && newql==quotelevel && strcmp(curline+quotelevel,"-- ")!=0) {
+ if (buf[newql]==' ')
+ curline[strlen(curline)-1] = '\0';
+
+ curline = realloc(curline,curline_len+strlen(buf));
+ if (curline_len == 1) *curline = '\0';
+ curline_len+=strlen(buf);
+ safe_strncat(curline,curline_len,buf+newql,strlen(buf+newql));
+ } else {
+ if (first_line) {
+ first_line = 0;
+ } else {
+ print_flowed_line(curline,s,quotelevel);
+ }
+ FREE(&curline);
+ curline_len = 1;
+ curline = realloc(curline,curline_len+strlen(buf));
+ if (curline_len == 1) *curline = '\0';
+ curline_len+=strlen(buf);
+ safe_strncat(curline,curline_len,buf,strlen(buf));
+ quotelevel = newql;
+ }
+ } else {
+ append_next_line = 1;
+ /* @todo: add handling of very long lines */
+ }
+ }
+ if (curline) {
+ print_flowed_line(curline,s,quotelevel);
+ FREE(&curline);
+ }
+}