Skip to content

Commit

Permalink
safe_u8putstr: add oneline mode that prints CR/LF as C0 sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
leahneukirchen committed Mar 15, 2021
1 parent 709f8f1 commit e8981b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion blaze822.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int slurp(char *filename, char **bufo, off_t *leno);
// safe_u8putstr.c

#include <stdio.h>
void safe_u8putstr(char *s0, size_t l, FILE *stream);
void safe_u8putstr(char *s0, size_t l, int oneline, FILE *stream);

// pipeto.c

Expand Down
28 changes: 14 additions & 14 deletions mshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ printable(int c)
}

size_t
print_ascii(char *body, size_t bodylen)
print_ascii(char *body, size_t bodylen, int oneline)
{
if (safe_output) {
safe_u8putstr(body, bodylen, stdout);
safe_u8putstr(body, bodylen, oneline, stdout);
return bodylen;
} else {
return fwrite(body, 1, bodylen, stdout);
Expand All @@ -73,7 +73,7 @@ printhdr(char *hdr)
}

if (*hdr) {
print_ascii(hdr, strlen(hdr));
print_ascii(hdr, strlen(hdr), 1);
fputc('\n', stdout);
}
}
Expand All @@ -98,7 +98,7 @@ print_u8recode(char *body, size_t bodylen, char *srcenc)
size_t r = iconv(ic, &body, &bodylen, &bufptr, &buflen);

if (bufptr != buf) {
print_ascii(buf, bufptr-buf);
print_ascii(buf, bufptr-buf, 0);
final_char = bufptr[-1];
}

Expand All @@ -107,7 +107,7 @@ print_u8recode(char *body, size_t bodylen, char *srcenc)
buflen = sizeof buf;
r = iconv(ic, 0, 0, &bufptr, &buflen);
if (bufptr != buf) {
print_ascii(buf, bufptr-buf);
print_ascii(buf, bufptr-buf, 0);
final_char = bufptr[-1];
}
if (r != (size_t)-1)
Expand Down Expand Up @@ -185,7 +185,7 @@ print_filename(char *filename)
{
if (filename) {
printf(" name=\"");
safe_u8putstr(filename, strlen(filename), stdout);
safe_u8putstr(filename, strlen(filename), 1, stdout);
printf("\"");
}
}
Expand Down Expand Up @@ -252,7 +252,7 @@ render_mime(int depth, struct message *msg, char *body, size_t bodylen)
printf(" render=\"%s\" ---\n", cmd);
if (outlen) {
if (e == 0)
print_ascii(output, outlen);
print_ascii(output, outlen, 0);
else
fwrite(output, 1, outlen, stdout);
if (output[outlen-1] != '\n')
Expand Down Expand Up @@ -299,7 +299,7 @@ render_mime(int depth, struct message *msg, char *body, size_t bodylen)
if (blaze822_mime_parameter(ct, "charset", &cs, &cse))
charset = strndup(cs, cse-cs);
if (probably_utf8(charset)) {
print_ascii(body, bodylen);
print_ascii(body, bodylen, 0);
if (bodylen > 0 && body[bodylen-1] != '\n')
putchar('\n');
} else {
Expand Down Expand Up @@ -391,7 +391,7 @@ reply_mime(int depth, struct message *msg, char *body, size_t bodylen)
if (blaze822_mime_parameter(ct, "charset", &cs, &cse))
charset = strndup(cs, cse-cs);
if (probably_utf8(charset))
print_ascii(body, bodylen);
print_ascii(body, bodylen, 0);
else
print_u8recode(body, bodylen, charset);
reply_found++;
Expand Down Expand Up @@ -502,7 +502,7 @@ extract_mime(int depth, struct message *msg, char *body, size_t bodylen)
fwrite(body, 1, bodylen, stdout);
} else { // extract all named attachments
if (filename) {
safe_u8putstr(filename, strlen(filename), stdout);
safe_u8putstr(filename, strlen(filename), 1, stdout);
printf("\n");
writefile(filename, body, bodylen);
}
Expand Down Expand Up @@ -563,7 +563,7 @@ extract_mime(int depth, struct message *msg, char *body, size_t bodylen)
fwrite(body, 1, bodylen, stdout);
}
} else {
safe_u8putstr(filename, strlen(filename), stdout);
safe_u8putstr(filename, strlen(filename), 1, stdout);
printf("\n");
writefile(filename, body, bodylen);
}
Expand Down Expand Up @@ -609,7 +609,7 @@ print_date_header(char *v)
}

printf("Date: ");
print_ascii(v, strlen(v));
print_ascii(v, strlen(v), 1);

time_t t = blaze822_date(v);
if (t == -1) {
Expand Down Expand Up @@ -695,7 +695,7 @@ print_decode_header(char *h, char *v)
printhdr(h);
fputc(':', stdout);
fputc(' ', stdout);
print_ascii(d, strlen(d));
print_ascii(d, strlen(d), 1);
fputc('\n', stdout);
}

Expand Down Expand Up @@ -762,7 +762,7 @@ show(char *file)
printf("\n");

if (rflag) { // raw body
print_ascii(blaze822_body(msg), blaze822_bodylen(msg));
print_ascii(blaze822_body(msg), blaze822_bodylen(msg), 0);
goto done;
}

Expand Down
5 changes: 3 additions & 2 deletions safe_u8putstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "u8decode.h"

void
safe_u8putstr(char *s0, size_t l, FILE *stream)
safe_u8putstr(char *s0, size_t l, int oneline, FILE *stream)
{
// tty-safe output of s, with relaxed utf-8 semantics:
// - C0 and C1 are displayed as escape sequences
Expand Down Expand Up @@ -35,7 +35,8 @@ safe_u8putstr(char *s0, size_t l, FILE *stream)
fputc(0x80 | (*s & 0x3f), stream);
}
} else if (c < 32 &&
*s != ' ' && *s != '\t' && *s != '\n' && *s != '\r') {
*s != ' ' && *s != '\t' &&
(oneline || (*s != '\n' && *s != '\r'))) {
// NUL
if (l == 0)
l = 1;
Expand Down

0 comments on commit e8981b7

Please sign in to comment.