File: fltpipe.c

package info (click to toggle)
mailfromd 9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,268 kB
  • sloc: ansic: 56,643; sh: 22,791; yacc: 4,130; lex: 1,428; makefile: 914; lisp: 488; awk: 393; perl: 319; sed: 25
file content (508 lines) | stat: -rw-r--r-- 10,595 bytes parent folder | download
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* A proposed replacement for mailutils filter chains.
   Copyright (C) 2021-2024 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <mailutils/mailutils.h>

enum {
	TOK_EOF = 0,
	TOK_OPAREN = '(',
	TOK_CPAREN = ')',
	TOK_COMMA = ',',
	TOK_PIPE = '|',
	TOK_FILTER = 256,
	TOK_STRING,
	TOK_IDENT,
	TOK_BOGUS
};

struct context {
	/*
	 * Syntactic context
	 */
	char const *buffer;   /* Input buffer */
	char const *curptr;   /* Current position in buffer */

	/*
	 * Current token.
	 */
	int token;            /* Token code */
	int point;            /* Index in buffer where the token starts */
				 
	char *tokbuf;         /* Token buffer */
	size_t toksize;       /* Size of tokbuf */
	size_t toklen;        /* Length of the used portion of tokbuf
				 (not counting terminating '\0') */


        /*
	 * User-provided context.
	 */
	int (*user_filter)(mu_stream_t *, mu_stream_t, int, int,
			   size_t, char **, void *);
	void *closure;

	void (*user_error)(int ec, /* Error code */
			   char const *msg, /* Error message */
			   char const *input, /* Input string */
			   int begin,  /* Error position: start */
			   int end,    /* Error position: end */
			   void *closure);
	
	/*
	 * Filter context.
	 */
	int mode;             /* Default filter mode: MU_FILTER_ENCODE or
				 MU_FILTER_DECODE */
	int flags;            /* Filter flags (a bitmask of MU_STREAM_*
				 flags) */
	
	mu_stream_t stream;   /* On input: input stream.  On return:
				 output stream. The caller must cleanup
				 it on errors. */
};

static int
context_point(struct context *ctx)
{
	return ctx->curptr - ctx->buffer;
}

static void
context_error(struct context *ctx, int ec, char const *fmt, ...)
{
	if (ctx->user_error) {
		char *msg = NULL;
		size_t size = 0;
		va_list ap;
		int rc;
		
		va_start(ap, fmt);
		rc = mu_vasnprintf(&msg, &size, fmt, ap);
		va_end(ap);

		if (rc) {
			msg = "out of memory trying to format error message";
			ec = ENOMEM;
		}

		ctx->user_error(ec, 
				msg,         /* Error message */
				ctx->buffer, /* Input string */
				ctx->point,  /* Error position: start */
				context_point(ctx)-1, /* Error position: end */
				ctx->closure);

		free(msg);
	}
}	

static void
context_init(struct context *ctx, char const *text, int mode, int flags,
	     mu_stream_t input)
{
	memset(ctx, 0, sizeof(ctx[0]));
	ctx->buffer = ctx->curptr = text;
	ctx->mode = mode;
	ctx->flags = flags;
	ctx->stream = input;
}

static void
context_free(struct context *ctx)
{
	free(ctx->tokbuf);
}

static void
token_assert(struct context *ctx, size_t n)
{
	n += ctx->toklen;
	while (n > ctx->toksize)
		ctx->tokbuf = mu_2nrealloc(ctx->tokbuf, &ctx->toksize, 1);
}

static void
skipws(struct context *ctx)
{
	ctx->curptr = mu_str_skip_class(ctx->curptr, MU_CTYPE_SPACE);
}

static int
is_delim(int c)
{
	return strchr("(),|", c) != 0;
}

static int
nextkn(struct context *ctx)
{
	skipws(ctx);
	ctx->point = context_point(ctx);
	switch (*ctx->curptr) {
	case 0:
		token_assert(ctx, 1);
		ctx->tokbuf[0] = 0;
		ctx->toklen = 0;
		ctx->token = 0;
		break;
		
	case '(':
	case ')':
	case ',':
	case '|':
		token_assert(ctx, 2);
		ctx->tokbuf[0] = *ctx->curptr++;
		ctx->tokbuf[1] = 0;
		ctx->toklen = 1;
		ctx->token = ctx->tokbuf[0];
		break;

	case '"':
		ctx->curptr++;
		if (!*ctx->curptr) {
			token_assert(ctx, 2);
			ctx->tokbuf[0] = '"';
			ctx->tokbuf[1] = 0;
			ctx->toklen = 1;
			ctx->token = TOK_STRING;
		} else {
			ctx->toklen = 0;
			while (*ctx->curptr != '"') {
				if (*ctx->curptr == '\\') 
					++ctx->curptr;
				
				if (*ctx->curptr == 0) {
					context_error(ctx,
						      MU_ERR_PARSE,
						      "unexpected end of input in quoted string");
					ctx->token = TOK_BOGUS;
					return ctx->token;
				}

				token_assert(ctx, 1);
				ctx->tokbuf[ctx->toklen++] = *ctx->curptr++;
			}

			token_assert(ctx, 1);
			ctx->tokbuf[ctx->toklen] = 0;
			++ctx->curptr;
			ctx->token = TOK_STRING;
		}
		break;

	case '\'':
		ctx->curptr++;
		if (!*ctx->curptr) {
			token_assert(ctx, 2);
			ctx->tokbuf[0] = '\'';
			ctx->tokbuf[1] = 0;
			ctx->toklen = 1;
			ctx->token = TOK_STRING;
		} else {
			ctx->toklen = 0;
			while (*ctx->curptr != '\'') {
				if (*ctx->curptr == 0) {
					context_error(ctx,
						      MU_ERR_PARSE,
						      "unexpected end of input in quoted string");
					ctx->token = TOK_BOGUS;
					return ctx->token;
				}
				
				token_assert(ctx, 1);
				ctx->tokbuf[ctx->toklen++] = *ctx->curptr++;
			}
			
			token_assert(ctx, 1);
			ctx->tokbuf[ctx->toklen] = 0;
			++ctx->curptr;
			ctx->token = TOK_STRING;
		}
		break;

	case '\\':
		ctx->curptr++;
		token_assert(ctx, 2);
		if (!*ctx->curptr) {
			ctx->tokbuf[0] = '\\';
		} else {
			ctx->tokbuf[0] = *++ctx->curptr;
			ctx->toklen = 1;
		}
		ctx->toklen = 1;
		ctx->tokbuf[1] = 0;
		ctx->token = TOK_STRING;
		break;
		
	default:
		ctx->toklen = 0;
		while (*ctx->curptr != 0) {
			if (*ctx->curptr == '\\') {
				if (ctx->curptr[1])
					++ctx->curptr;
			} else if (mu_isspace(*ctx->curptr) ||
				   is_delim(*ctx->curptr))
				break;
			
			token_assert(ctx, 1);
			ctx->tokbuf[ctx->toklen++] = *ctx->curptr++;
		}
		ctx->tokbuf[ctx->toklen] = 0;
		ctx->token = TOK_IDENT;
		break;
	}

	return ctx->token;
}

struct argcvbuf {
	size_t argc;
	char **argv;
	size_t argn;
};

#define ARGCVBUF_INITIALIZER {0,NULL,0}

static void
argcvbuf_append(struct argcvbuf *ap, char const *p)
{
	if (ap->argc == ap->argn)
		ap->argv = mu_2nrealloc(ap->argv, &ap->argn, sizeof(ap->argv[0]));
	if (p) 
		ap->argv[ap->argc++] = mu_strdup(p);
	else
		ap->argv[ap->argc] = NULL;
}

static void
argcvbuf_free(struct argcvbuf *ap)
{
	size_t i;

	for (i = 0; i < ap->argc; i++)
		free(ap->argv[i]);
	free(ap->argv);
}

static int
add_filter(struct context *ctx, int mode, struct argcvbuf *ap)
{
	int rc = ENOSYS;
	mu_stream_t str;

	if (ctx->user_filter) {
		rc = ctx->user_filter(&str, ctx->stream,
				      mode, ctx->flags,
				      ap->argc, ap->argv,
				      ctx->closure);
	}

	if (rc == ENOSYS)
		rc = mu_filter_create_args(&str, ctx->stream, ap->argv[0],
					   ap->argc, (char const **)ap->argv,
					   mode, ctx->flags);

	if (rc == 0) {
		mu_stream_unref (ctx->stream);
		ctx->stream = str;
	}

	return rc;
}

/*
 * ARG [ ',' ARG ... ]
 */
static int
parse_args(struct context *ctx, struct argcvbuf *ap)
{
	do {
		nextkn(ctx);
		if (ctx->token != TOK_STRING && ctx->token != TOK_IDENT)
			break;
		argcvbuf_append(ap, ctx->tokbuf);
	} while (nextkn(ctx) == ',');
	return 0;
}

static int parse_transcoder(struct context *ctx, int mode);

/*
 * FILTER
 * FILTER '(' arglist ')'
 */
static int
parse_filter(struct context *ctx, int mode)
{
	struct argcvbuf args = ARGCVBUF_INITIALIZER;
	int rc = 0;
	int pt;
	
	if (ctx->token != TOK_IDENT) {
		if (ctx->token == TOK_STRING)
			context_error(ctx, MU_ERR_PARSE,
				      "expected filter name, but found string");
		else
			context_error(ctx, MU_ERR_PARSE,
				      "expected filter name, but found \"%s\"",
				      ctx->tokbuf);
		return MU_ERR_PARSE;
	}

	if (strcmp(ctx->tokbuf, "encode") == 0 ||
	    strcmp(ctx->tokbuf, "decode") == 0)
		return parse_transcoder(ctx, mode);

	pt = ctx->point;
	
	argcvbuf_append(&args, ctx->tokbuf);

	if (nextkn(ctx) == '(') {
		int pt1 = ctx->point;

		if (parse_args(ctx, &args)) {
			rc = MU_ERR_PARSE;
		} else if (ctx->token != ')') {
			ctx->point = pt1; /* Indicate error starting point */
			context_error(ctx,
				      MU_ERR_PARSE,
				      "unexpected end of input in argument list");
			rc = MU_ERR_PARSE;
		} else
			nextkn(ctx);
	}

	if (rc == 0) {
		rc = add_filter(ctx, mode, &args);
		if (rc) {
			ctx->point = pt; /* Indicate error starting point */
			context_error(ctx,
				      rc,
				      "failed to create filter %s: %s",
				      args.argv[0], mu_strerror(rc));
		}
	}
		
	argcvbuf_free(&args);

	return rc;
}

/*
 * filter ['|' filter ...]
 */
static int
parse_pipe(struct context *ctx, int mode)
{
	int rc;
	while ((rc = parse_filter(ctx, mode)) == 0 && ctx->token == '|')
		nextkn(ctx);
	return rc;
}

/*
 * '(' pipe ')'
 */
static int
parse_group(struct context *ctx, int mode)
{
	if (ctx->token == '(') {
		int rc;
		int pt = ctx->point;
		nextkn(ctx);
		if ((rc = parse_pipe(ctx, mode)) != 0)
			return rc;
		if (ctx->token != ')') {
			ctx->point = pt;
			context_error(ctx,
				      MU_ERR_PARSE,
				      "unexpected end of input in pipe group");
			return MU_ERR_PARSE;
		}
		nextkn(ctx);
		return 0;
	} else
		return parse_pipe(ctx, mode);
}

/*
 * "encode" ( pipe )
 * "decode" ( pipe )
 */
static int
parse_transcoder(struct context *ctx, int mode)
{
	if (ctx->token == TOK_IDENT) {
		if (strcmp(ctx->tokbuf, "encode") == 0) {
			mode = MU_FILTER_ENCODE;
			nextkn(ctx);
		} else if (strcmp(ctx->tokbuf, "decode") == 0) {
			mode = MU_FILTER_DECODE;
			nextkn(ctx);
		}
	}
	return parse_group(ctx, mode);
}

static int
parse_filter_command(struct context *ctx)
{
	int rc;
	
	do {
		nextkn(ctx);
		rc = parse_transcoder(ctx, ctx->mode);
	} while (rc == 0 && ctx->token == '|');

	if (rc == 0 && ctx->token != TOK_EOF) {
		rc = MU_ERR_PARSE;
		if (ctx->token == TOK_STRING)
			context_error(ctx, rc,
				      "expected pipe or end of input, but found string");
		else
			context_error(ctx, rc,
				      "expected pipe or end of input, but found \"%s\"",
				      ctx->tokbuf);
	}
	return rc;
}

int
mfl_filter_pipe_create(mu_stream_t *pret, mu_stream_t transport,
		       int mode, int flags,
		       char const *cmdline,
		       int (*fltfunc)(mu_stream_t *, mu_stream_t, int, int, size_t, char **, void *),

		       void (*errfunc)(int, char const *, char const *, int, int, void *),
		       void *closure)
{
	struct context ctx;
	int rc;
	
	mu_stream_ref(transport);
	context_init(&ctx, cmdline, mode, flags, transport);
	ctx.user_filter = fltfunc;
	ctx.user_error = errfunc;
	ctx.closure = closure;
	
	rc = parse_filter_command(&ctx);
	context_free(&ctx);
	if (rc == 0)
		*pret = ctx.stream;
	else
		mu_stream_destroy(&ctx.stream);
	return rc;
}