about summary refs log tree commit diff
path: root/mflow.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-09-06 18:39:31 +0200
committerLeah Neukirchen <leah@vuxu.org>2017-09-06 18:39:31 +0200
commitebeaf43a2723846fbdc7555b5d4e6f6d626e57b4 (patch)
tree061765be714ece4af763ca751bc79d14d3295814 /mflow.c
parent285671f0e2cf186cd6a1401df015fab03e7b7450 (diff)
downloadmblaze-ebeaf43a2723846fbdc7555b5d4e6f6d626e57b4.tar.gz
mblaze-ebeaf43a2723846fbdc7555b5d4e6f6d626e57b4.tar.xz
mblaze-ebeaf43a2723846fbdc7555b5d4e6f6d626e57b4.zip
mflow: add wrapping of long lines, add argument parsing, add quoting
Diffstat (limited to 'mflow.c')
-rw-r--r--mflow.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/mflow.c b/mflow.c
index e608907..68d2629 100644
--- 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);
+			}
 		}
 	}