ソースを参照

safe_u8putstr: add oneline mode that prints CR/LF as C0 sequences

pull/209/head
Leah Neukirchen 1年前
コミット
e8981b723d
  1. 2
      blaze822.h
  2. 28
      mshow.c
  3. 5
      safe_u8putstr.c

2
blaze822.h

@ -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

28
mshow.c

@ -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);
@ -73,7 +73,7 @@ printhdr(char *hdr)
}
if (*hdr) {
print_ascii(hdr, strlen(hdr));
print_ascii(hdr, strlen(hdr), 1);
fputc('\n', stdout);
}
}
@ -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];
}
@ -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)
@ -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("\"");
}
}
@ -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')
@ -299,7 +299,7 @@ nofilter:
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 {
@ -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++;
@ -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);
}
@ -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);
}
@ -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) {
@ -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);
}
@ -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;
}

5
safe_u8putstr.c

@ -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
@ -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;

読み込み中…
キャンセル
保存