forked from leahneukirchen/mblaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mflow.c
212 lines (182 loc) · 3.81 KB
/
mflow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifdef __sun
#define __EXTENSIONS__ /* to get TIOCGWINSZ */
#endif
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <termios.h>
#include "blaze822.h"
#include "xpledge.h"
int column = 0;
int maxcolumn = 80;
void
chgquote(int quotes)
{
static int oquotes;
if (quotes != oquotes) {
if (column)
putchar('\n');
column = 0;
oquotes = quotes;
}
}
void
flowed(int quotes, char *line, ssize_t linelen)
{
chgquote(quotes);
int done = 0;
while (!done) {
if (column == 0) {
for (; column < quotes; column++)
putchar('>');
column++;
if (quotes && *line != ' ')
putchar(' ');
}
char *eow;
if (*line == ' ')
eow = memchr(line + 1, ' ', linelen - 1);
else
eow = memchr(line, ' ', linelen);
if (!eow) {
eow = line + linelen;
done = 1;
}
if (column + (eow - line) > maxcolumn &&
eow - line < maxcolumn &&
column - quotes > 1) {
putchar('\n');
column = 0;
done = 0;
if (*line == ' ') {
line++;
linelen--;
}
} else {
fwrite(line, 1, eow - line, stdout);
column += eow - line;
linelen -= eow - line;
line = eow;
}
}
}
void
fixed(int quotes, char *line, size_t linelen)
{
flowed(quotes, line, linelen);
putchar('\n');
column = 0;
}
int
main(int argc, char *argv[])
{
char *linebuf = 0;
char *line;
size_t linelen = 0;
int outer_quotes = 0;
int quotes;
int reflow = 1; // re-evaluated on $PIPE_CONTENTTYPE
int force = 0;
int delsp = 0;
xpledge("stdio rpath tty", "");
char *ct = getenv("PIPE_CONTENTTYPE");
if (ct) {
char *s, *se;
reflow = 0;
if (blaze822_mime_parameter(ct, "format", &s, &se) && s)
reflow = (strncasecmp(s, "flowed", 6) == 0);
if (blaze822_mime_parameter(ct, "delsp", &s, &se) && s)
delsp = (strncasecmp(s, "yes", 3) == 0);
}
char *cols = getenv("COLUMNS");
if (cols && isdigit(*cols)) {
maxcolumn = atoi(cols);
} else {
struct winsize w;
int fd = open("/dev/tty", O_RDONLY | O_NOCTTY);
if (fd >= 0) {
if (ioctl(fd, TIOCGWINSZ, &w) == 0)
maxcolumn = w.ws_col;
close(fd);
}
}
xpledge("stdio", "");
char *maxcols = getenv("MAXCOLUMNS");
if (maxcols && isdigit(*maxcols)) {
int m = atoi(maxcols);
if (maxcolumn > m)
maxcolumn = m;
}
int c;
while ((c = getopt(argc, argv, "fqw:")) != -1)
switch (c) {
case 'f': force = 1; break;
case 'q': outer_quotes++; break;
case 'w': maxcolumn = atoi(optarg); 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);
if (rd == -1) {
if (errno == 0)
break;
fprintf(stderr, "mflow: error reading: %s\n",
strerror(errno));
exit(1);
}
line = linebuf;
if (!reflow && !force) {
fwrite(line, 1, rd, stdout);
continue;
}
if (rd > 0 && line[rd-1] == '\n')
line[--rd] = 0;
if (rd > 0 && line[rd-1] == '\r')
line[--rd] = 0;
quotes = outer_quotes;
while (*line == '>') { // measure quote depth
line++;
quotes++;
rd--;
}
if (reflow && *line == ' ') { // space stuffing
line++;
rd--;
}
if (strcmp(line, "-- ") == 0) { // usenet signature convention
if (column)
fixed(quotes, "", 0); // flush paragraph
fixed(quotes, line, rd);
continue;
}
if (reflow && rd > 0 && line[rd-1] == ' ') { // flowed line
if (delsp)
line[--rd] = 0;
flowed(quotes, line, rd);
} else if (rd == 0) { // empty line is fixed
if (column > 0)
putchar('\n');
putchar('\n');
column = 0;
} else {
if (force && rd > maxcolumn) {
flowed(quotes, line, rd);
fixed(quotes, "", 0);
} else {
fixed(quotes, line, rd);
}
}
}
if (reflow && column != 0)
putchar('\n');
}