aboutsummaryrefslogtreecommitdiff
path: root/mfd/pragma.c
blob: 93afbd75ee73486c509e587d701a3ee8e4b2285e (plain)
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
/* This file is part of Mailfromd.
   Copyright (C) 2009 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/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "c-ctype.h"
#include "mailfromd.h"
#include "prog.h"

int
check_pragma_args(const struct pragma *pdef, int argc)
{
	if (pdef->minargs > 0 && argc < pdef->minargs) {
		parse_error(_("too few arguments in pragma"));
		return 1;
	}
	if (pdef->maxargs > 0 && argc > pdef->maxargs) {
		parse_error(_("too many arguments in pragma"));
		return 1;
	}
	return 0;
}

void
parse_pragma(const char *text)
{
	int rc;
	int argc;
	char **argv;
	const struct pragma *pdef;
	
	while (*text != '#')
		text++;
	++text;
	while (*text && c_isspace(*text)) text++;
	text += 6; /* "pragma" */

	rc = mu_argcv_get(text, "", NULL, &argc, &argv);
	if (rc) {
		parse_error(_("error parsing pragma: %s"), mu_strerror (rc));
		return;
	}

	if (argc == 0) {
		parse_error(_("Empty pragma"));
		return;
	}

	pdef = lookup_pragma(argv[0]);
	if (!pdef) 
		parse_error(_("Unknown pragma"));
	else if (check_pragma_args(pdef, argc) == 0)
		pdef->handler(argc, argv, text);
	mu_argcv_free(argc, argv);
}

Return to:

Send suggestions and report system problems to the System administrator.