mblaze

Unix utilities to deal with Maildir - my mirror
git clone https://pi.duncano.de/git/mblaze.git
Log | Files | Refs | README | COPYING

commit ebeaf43a2723846fbdc7555b5d4e6f6d626e57b4
parent 285671f0e2cf186cd6a1401df015fab03e7b7450
Author: Leah Neukirchen <leah@vuxu.org>
Date:   Wed,  6 Sep 2017 18:39:31 +0200

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

Diffstat:
man/mflow.1 | 18+++++++++++++++++-
mflow.c | 30++++++++++++++++++++++++------
2 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/man/mflow.1 b/man/mflow.1 @@ -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 diff --git a/mflow.c b/mflow.c @@ -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); + } } }