diff --git a/src/sbbs3/wordwrap.c b/src/sbbs3/wordwrap.c index 59dc417f193073e41a3c07c16d70d83e9df9db9c..d320e6af2139a6fe1a4b1e398ee3e4f6282654a1 100644 --- a/src/sbbs3/wordwrap.c +++ b/src/sbbs3/wordwrap.c @@ -198,6 +198,19 @@ int get_word_len(char *buf, int starting_pos) return next_len; } +#define HARD_CR { \ + linebuf[l++]='\r'; \ + linebuf[l++]='\n'; \ + outbuf_append(&outbuf, &outp, linebuf, l, &outbuf_size); \ + if(prefix) \ + memcpy(linebuf,prefix,prefix_bytes); \ + l=prefix_bytes; \ + ocol=prefix_len+1; \ + icol=prefix_len+1; \ + chopped = FALSE; \ + continue; \ +} + char* wordwrap(char* inbuf, int len, int oldlen, BOOL handle_quotes) { int l; @@ -310,19 +323,12 @@ char* wordwrap(char* inbuf, int len, int oldlen, BOOL handle_quotes) break; } case '\n': -fprintf(stderr, "CR\n"); if(handle_quotes && (quote_count=get_prefix(inbuf+i+1, &prefix_bytes, &prefix_len, len*2+2))!=0) { /* Move the input pointer offset to the last char of the prefix */ i+=prefix_bytes; } - if(!inbuf[i+1]) { /* EOF */ - linebuf[l++]='\r'; - linebuf[l++]='\n'; - outbuf_append(&outbuf, &outp, linebuf, l, &outbuf_size); - l=0; - ocol=1; - continue; - } + if(!inbuf[i+1]) /* EOF */ + HARD_CR /* If there's a new prefix, it is a hardcr */ else if(compare_prefix(prefix, old_prefix_bytes, inbuf+i+1-prefix_bytes, prefix_bytes)!=0) { if(prefix_len>len/3*2) { @@ -338,43 +344,18 @@ fprintf(stderr, "CR\n"); /* Terminate prefix */ prefix[prefix_bytes]=0; } - linebuf[l++]='\r'; - linebuf[l++]='\n'; - outbuf_append(&outbuf, &outp, linebuf, l, &outbuf_size); - memcpy(linebuf,prefix,prefix_bytes); - l=prefix_bytes; - ocol=prefix_len+1; - old_prefix_bytes=prefix_bytes; - icol=prefix_len+1; - continue; - } - else if(chopped || isspace((unsigned char)inbuf[i+1])) { /* Next line starts with whitespace. This is a "hard" CR. */ - linebuf[l++]='\r'; - linebuf[l++]='\n'; - outbuf_append(&outbuf, &outp, linebuf, l, &outbuf_size); - if(prefix) - memcpy(linebuf,prefix,prefix_bytes); - l=prefix_bytes; - ocol=prefix_len+1; - icol=prefix_len+1; - chopped = FALSE; - continue; + HARD_CR } + else if(chopped || isspace((unsigned char)inbuf[i+1])) /* Next line starts with whitespace. This is a "hard" CR. */ + HARD_CR + else if(strspn(linebuf+prefix_bytes, " \t\r") == l-prefix_bytes) /* Lines made entirely of whitespace always end with a "hard" CR */ + HARD_CR else { if(icol < oldlen) { /* If this line is overly long, It's impossible for the next word to fit */ /* k will equal the length of the first word on the next line */ k = get_word_len(inbuf, i+1); - if(icol+k < oldlen) { /* The next word would have fit but isn't here. Must be a hard CR */ - linebuf[l++]='\r'; - linebuf[l++]='\n'; - outbuf_append(&outbuf, &outp, linebuf, l, &outbuf_size); - if(prefix) - memcpy(linebuf,prefix,prefix_bytes); - l=prefix_bytes; - ocol=prefix_len+1; - icol=prefix_len+1; - continue; - } + if(icol+k < oldlen) /* The next word would have fit but isn't here. Must be a hard CR */ + HARD_CR else { /* Not a hard CR... add space if needed */ if(ocol > prefix_len+1 && (l<1 || !isspace((unsigned char)linebuf[l-1]))) { linebuf[l++]=' ';