Skip to content
Snippets Groups Projects
Commit e8c4a677 authored by deuce's avatar deuce
Browse files

Make the ugly-usage HARD_CR macro do all the stuff a good hard CR does.

Lines made up entirely of whitespace always end with a hard CR.
parent 9d1dfae2
No related branches found
No related tags found
No related merge requests found
...@@ -198,6 +198,19 @@ int get_word_len(char *buf, int starting_pos) ...@@ -198,6 +198,19 @@ int get_word_len(char *buf, int starting_pos)
return next_len; 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) char* wordwrap(char* inbuf, int len, int oldlen, BOOL handle_quotes)
{ {
int l; int l;
...@@ -310,19 +323,12 @@ char* wordwrap(char* inbuf, int len, int oldlen, BOOL handle_quotes) ...@@ -310,19 +323,12 @@ char* wordwrap(char* inbuf, int len, int oldlen, BOOL handle_quotes)
break; break;
} }
case '\n': case '\n':
fprintf(stderr, "CR\n");
if(handle_quotes && (quote_count=get_prefix(inbuf+i+1, &prefix_bytes, &prefix_len, len*2+2))!=0) { 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 */ /* Move the input pointer offset to the last char of the prefix */
i+=prefix_bytes; i+=prefix_bytes;
} }
if(!inbuf[i+1]) { /* EOF */ if(!inbuf[i+1]) /* EOF */
linebuf[l++]='\r'; HARD_CR
linebuf[l++]='\n';
outbuf_append(&outbuf, &outp, linebuf, l, &outbuf_size);
l=0;
ocol=1;
continue;
}
/* If there's a new prefix, it is a hardcr */ /* 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) { else if(compare_prefix(prefix, old_prefix_bytes, inbuf+i+1-prefix_bytes, prefix_bytes)!=0) {
if(prefix_len>len/3*2) { if(prefix_len>len/3*2) {
...@@ -338,43 +344,18 @@ fprintf(stderr, "CR\n"); ...@@ -338,43 +344,18 @@ fprintf(stderr, "CR\n");
/* Terminate prefix */ /* Terminate prefix */
prefix[prefix_bytes]=0; prefix[prefix_bytes]=0;
} }
linebuf[l++]='\r'; HARD_CR
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;
} }
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 { else {
if(icol < oldlen) { /* If this line is overly long, It's impossible for the next word to fit */ 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 will equal the length of the first word on the next line */
k = get_word_len(inbuf, i+1); 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 */ if(icol+k < oldlen) /* The next word would have fit but isn't here. Must be a hard CR */
linebuf[l++]='\r'; HARD_CR
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;
}
else { /* Not a hard CR... add space if needed */ else { /* Not a hard CR... add space if needed */
if(ocol > prefix_len+1 && (l<1 || !isspace((unsigned char)linebuf[l-1]))) { if(ocol > prefix_len+1 && (l<1 || !isspace((unsigned char)linebuf[l-1]))) {
linebuf[l++]=' '; linebuf[l++]=' ';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment