mflow: add wrapping of long lines, add argument parsing, add quoting

pull/75/head
Leah Neukirchen 7 years ago
parent 285671f0e2
commit ebeaf43a27

@ -1,4 +1,4 @@
.Dd July 26, 2017
.Dd September 6, 2017
.Dt MFLOW 1
.Os
.Sh NAME
@ -6,6 +6,9 @@
.Nd reflow format=flowed plain text mails
.Sh SYNOPSIS
.Nm
.Op Fl f
.Op Fl q
.Op Fl w Ar width
\&<
.Ar file
.Sh DESCRIPTION
@ -31,6 +34,19 @@ If defined,
the environment variable
.Ev MAXCOLUMNS
specifies the maximum line length.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl f
Force line wrapping of long lines.
.It Fl q
Prefix lines with
.Sq Li \&< .
Can be used multiple times.
.It Fl w Ar width
Set maximum line length to
.Ar width .
.El
.Sh EXIT STATUS
.Ex -std
.Sh SEE ALSO

@ -32,7 +32,7 @@ fixed(int quotes, char *line, size_t linelen)
{
chgquote(quotes);
if (linelen > (size_t)(maxcolumn - column)) {
if (column && linelen > (size_t)(maxcolumn - column)) {
putchar('\n');
column = 0;
}
@ -93,14 +93,16 @@ flowed(int quotes, char *line, ssize_t linelen)
}
int
main()
main(int argc, char *argv[])
{
char *linebuf = 0;
char *line;
size_t linelen = 0;
int quotes = 0;
int outer_quotes = 0;
int quotes;
int reflow = 1; // re-evaluated on $PIPE_CONTENTTYPE
int force = 0;
int delsp = 0;
char *ct = getenv("PIPE_CONTENTTYPE");
@ -133,6 +135,17 @@ main()
maxcolumn = m;
}
int c;
while ((c = getopt(argc, argv, "f:w:q")) != -1)
switch (c) {
case 'f': force = 1; break;
case 'w': maxcolumn = atoi(optarg); break;
case 'q': outer_quotes++; break;
default:
fprintf(stderr, "Usage: mflow [-f] [-q] [-w MAXCOLUMNS]\n");
exit(2);
}
while (1) {
errno = 0;
ssize_t rd = getdelim(&linebuf, &linelen, '\n', stdin);
@ -146,7 +159,7 @@ main()
line = linebuf;
if (!reflow) {
if (!reflow && !force) {
fwrite(line, 1, rd, stdout);
continue;
}
@ -156,7 +169,7 @@ main()
if (rd > 0 && line[rd-1] == '\r')
line[--rd] = 0;
quotes = 0;
quotes = outer_quotes;
while (*line == '>') { // measure quote depth
line++;
quotes++;
@ -180,7 +193,12 @@ main()
line[--rd] = 0;
flowed(quotes, line, rd);
} else {
fixed(quotes, line, rd);
if (force && rd > maxcolumn) {
flowed(quotes, line, rd);
fixed(quotes, "", 0);
} else {
fixed(quotes, line, rd);
}
}
}

Loading…
Cancel
Save