-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-qprint.c
373 lines (346 loc) · 7.55 KB
/
lib-qprint.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright Neil Brown ©2016-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Filter a view on a document to make quoted-printable look like the
* decoded bytes. A UTF-8 filter would be needed if the text
* is actually utf-8.
*
* Chars are passed through except for '=' and following.
* =HH decodes the hex
* =\r\n disappears
* \r\n -> \n
* space/tab at eol ignored
*
* So when stepping backward if we see a \n or hex char we need
* to look further to see what is really there.
* When stepping forwards, we need only check for '=' or white space.
*/
#include <unistd.h>
#include <stdlib.h>
#define DOC_NEXT qp_next
#define DOC_PREV qp_prev
#define PANE_DATA_VOID
#include "core.h"
static struct map *qp_map safe;
DEF_LOOKUP_CMD(qp_handle, qp_map);
static int hex(wint_t c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return -1;
}
static inline wint_t qp_next(struct pane *home safe, struct mark *mark safe,
struct doc_ref *r safe, bool byte)
{
int move = r == &mark->ref;
struct pane *p = home->parent;
wint_t ch, c2, c3;
struct mark *m = mark;
retry:
if (move)
ch = doc_next(p, m);
else
ch = doc_following(p, m);
if (ch != '=' && ch != ' ' && ch != '\t' && ch != '\r') {
if (m != mark) {
if (move)
mark_to_mark(mark, m);
mark_free(m);
}
goto normalize;
}
if (ch == '\r') {
/* assume CR-LF - skip an extra char */
if (move)
doc_next(p, m);
if (m != mark) {
if (move)
mark_to_mark(mark, m);
mark_free(m);
}
ch = '\n';
goto normalize;
}
if (m == mark)
m = mark_dup(m);
if (!move)
doc_next(p, m);
if (ch == '=') {
/* CRLF or HexHex expected. */
c2 = doc_next(p, m);
if (c2 == '\n')
goto retry;
c3 = doc_next(p, m);
if (c2 == '\r' && c3 == '\n')
goto retry;
if (hex(c2) >= 0 && hex(c3) >= 0) {
ch = hex(c2)*16 + hex(c3);
if (move)
mark_to_mark(mark, m);
}
mark_free(m);
goto normalize;
}
/* Whitespace, ignore if at eol */
if (move)
mark_to_mark(mark, m);
while ((c2 = doc_next(p, m)) == ' ' || c2 == '\t')
;
if (c2 == '\r')
/* Found the white-space, retry from here and see the '\n' */
goto retry;
if (c2 == '\n') {
/* No \r, just \n. Step back to see it */
doc_prev(p, m);
goto retry;
}
/* Just normal white space */
mark_free(m);
normalize:
if (!move)
return ch;
normalize_more:
m = mark;
/* If next is "=\n" we need to skip over it. */
if (doc_following(p, m) != '=')
return ch;
m = mark_dup(mark);
doc_next(p, m);
while ((c2 = doc_next(p, m)) == ' ' ||
c2 == '\t' || c2 == '\r')
;
if (c2 != '\n') {
/* Don't need to skip this */
mark_free(m);
return ch;
}
mark_to_mark(mark, m);
mark_free(m);
goto normalize_more;
}
static inline wint_t qp_prev(struct pane *home safe, struct mark *mark safe,
struct doc_ref *r safe, bool byte)
{
int move = r == &mark->ref;
struct pane *p = home->parent;
wint_t ch, c2, c3;
struct mark *m = mark;
retry:
if (move)
ch = doc_prev(p, m);
else
ch = doc_prior(p, m);
if (ch == '\n') {
if (m == mark)
m = mark_dup(m);
if (!move)
doc_prev(p, m);
/* '\n', skip '\r' and white space */
while ((ch = doc_prior(p, m)) == '\r' ||
ch == ' ' || ch == '\t')
doc_prev(p, m);
if (ch == '=') {
doc_prev(p, m);
goto retry;
}
if (move)
mark_to_mark(mark, m);
mark_free(m);
return '\n';
}
if (hex(ch) < 0) {
if (m != mark) {
if (move)
mark_to_mark(mark, m);
mark_free(m);
}
return ch;
}
if (m == mark)
m = mark_dup(m);
else if (move)
mark_to_mark(mark, m);
if (!move)
doc_prev(p, m);
/* Maybe =HH */
c3 = ch;
c2 = doc_prev(p, m);
if (hex(c2) >= 0) {
wint_t ceq = doc_prev(p, m);
if (ceq == '=') {
/* =HH */
ch = hex(c2)*16 + hex(c3);
if (move)
mark_to_mark(mark, m);
mark_free(m);
return ch;
}
}
mark_free(m);
return ch;
}
DEF_CMD(qp_char)
{
return do_char_byte(ci);
}
struct qpcb {
struct command c;
struct command *cb safe;
struct pane *p safe;
char state; /* \0 or '=' or hexit */
int size;
struct buf lws;
struct mark *lws_start; /* after first lws char */
};
static int qpflush(struct qpcb *c safe, const struct cmd_info *ci, wint_t ch,
const char *remainder, int rlen)
{
char *lws = buf_final(&c->lws);
int lws_len = c->lws.len;
int ret = 1;
int i;
while (ret > 0 && lws_len > 0 && c->lws_start) {
ret = comm_call(c->cb, ci->key, c->p, *lws, c->lws_start, lws+1,
lws_len - 1, NULL, NULL, c->size, 0);
doc_next(ci->home, c->lws_start);
c->size = 0;
if (ret > 0) {
lws += ret;
lws_len -= ret;
ret = 1;
}
}
buf_reinit(&c->lws);
mark_free(c->lws_start);
c->lws_start = NULL;
if (!ch)
return ret;
for (i = 0; remainder && i < rlen; i++)
if (strchr("=\r\n", remainder[i])) {
rlen = i;
if (remainder[i] == '=')
break;
/* ignore trailing white space */
while (i > 0 && remainder[i] <= ' ')
i -= 1;
rlen = i;
}
if (ret > 0)
ret = comm_call(c->cb, ci->key, c->p, ch, ci->mark, remainder,
rlen, NULL, NULL, c->size, 0);
c->size = 0;
return ret;
}
DEF_CMD(qp_content_cb)
{
struct qpcb *c = container_of(ci->comm, struct qpcb, c);
wint_t wc = ci->num;
int ret = 1;
if (!ci->mark)
return Enoarg;
if (ci->x)
c->size = ci->x;
if (c->state && c->state != '=') {
/* Must see a hexit */
int h = hex(wc);
if (h >= 0) {
ret = qpflush(c, ci, (hex(c->state) << 4) | h, NULL, 0);
c->state = 0;
return ret;
}
/* Pass first 2 literally */
ret = qpflush(c, ci, '=', NULL, 0);
if (ret > 0)
ret = qpflush(c, ci, c->state, NULL, 0);
c->state = 0;
}
if (wc == '\r')
/* Always skip \r */
return ret;
if (!c->state) {
if (wc == '=') {
/* flush lws even if this turns out to be "=\n \n" */
if (ret)
ret = qpflush(c, ci, 0, NULL, 0);
c->state = wc;
return ret;
}
if (wc == ' ' || wc == '\t') {
if (!c->lws_start)
c->lws_start = mark_dup(ci->mark);
buf_append(&c->lws, wc);
return ret;
}
if (wc == '\n') {
/* drop any trailing space */
buf_reinit(&c->lws);
mark_free(c->lws_start);
c->lws_start = NULL;
}
if (ret > 0)
ret = qpflush(c, ci, wc, ci->str, ci->num2);
return ret;
}
/* Previous was '='. */
if (hex(wc) >= 0) {
c->state = wc;
return ret;
}
if (wc == ' ' || wc == '\t')
/* Ignore space after =, incase at eol */
return ret;
c->state = 0;
if (wc == '\n')
/* The '=' was hiding the \n */
return ret;
if (ret > 0)
ret = qpflush(c, ci, '=', NULL, 0);
if (ret > 0)
ret = qpflush(c, ci, wc, NULL, 0);
return ret;
}
DEF_CMD(qp_content)
{
struct qpcb c;
int ret;
if (!ci->comm2 || !ci->mark)
return Enoarg;
/* No need to check ->key as providing bytes as chars
* is close enough.
*/
c.c = qp_content_cb;
c.cb = ci->comm2;
c.p = ci->focus;
c.size = 0;
c.state = 0;
buf_init(&c.lws);
c.lws_start = NULL;
ret = home_call_comm(ci->home->parent, ci->key, ci->home,
&c.c, 0, ci->mark, NULL, 0, ci->mark2);
free(c.lws.b);
mark_free(c.lws_start);
return ret;
}
DEF_CMD(qp_attach)
{
struct pane *p;
p = pane_register(ci->focus, 0, &qp_handle.c);
if (!p)
return Efail;
return comm_call(ci->comm2, "callback:attach", p);
}
void edlib_init(struct pane *ed safe)
{
qp_map = key_alloc();
key_add(qp_map, "doc:char", &qp_char);
key_add(qp_map, "doc:byte", &qp_char);
key_add(qp_map, "doc:content", &qp_content);
key_add(qp_map, "doc:content-bytes", &qp_content);
call_comm("global-set-command", ed, &qp_attach, 0, NULL, "attach-quoted_printable");
call_comm("global-set-command", ed, &qp_attach, 0, NULL, "attach-qprint");
}