aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2008-09-16 12:10:02 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2008-09-16 12:10:02 +0000
commit99c453be5795d76e10b0a9cf1a2558367e538a1c (patch)
tree7810d5bed75fe0e6e24a647d754c187a714e9819
parentd6f4adec375f9f50186248462f2774d9dc6ae59e (diff)
downloadmailfromd-99c453be5795d76e10b0a9cf1a2558367e538a1c.tar.gz
mailfromd-99c453be5795d76e10b0a9cf1a2558367e538a1c.tar.bz2
Implement functions with variable number of arguments and 'run' mode.
* mfd/symtab.c (function_install): Take additional argument. * mfd/lex.l: New tokens `vaptr', `...' and ARGX. * mfd/snarf.m4 (MF_VASTRING): New define. * mfd/gram.y: Handle functions with variable number of arguments, $(expr), and vaptr() expressions. This raises shift/reduce expectation to 29 conflicts. (parmtype_function): Return dtype_string for arguments from vararg list. (FUNC_HIDDEN_ARGS): Return 1 if f->varargs is set. (type_to_string): Handle pointer argument. (function_call): Handle function->varargs. (node_type): Handle node_type_argx, and node_type_vaptr. (cast_arg_list): Handle variable number of arguments. (cast_to): Handle dtype_pointer as dtype_number. * mfd/drivers.c (argx, vaptr): New node types. (code_type_call): Handle variable number of arguments. (code_cast): Handle dtype_pointer as dtype_number. * mfd/mailfromd.h (MAILFROMD_RUN): New define. (struct argx_node): New type. (struct node): Add argx_node. (struct function): New member `varargs'. (function_install): Add varargs. (mailfromd_run): New prototype. * mfd/prog.c (env_vaptr, env_get_reg): New functions. (env_make_frame0): New function. (instr_xmemstk): New instruction handler. * mfd/opcodes (XMEMSTK): New opcode. * mfd/prog.h (env_get_reg, env_vaptr): New protos. * mfd/main.c (options): New option `--run'. (main): Handle `run' mode. * mfd/Makefile.am (M4_FILES): Add bi_getopt.m4. * NEWS: Update. git-svn-id: file:///svnroot/mailfromd/trunk@1669 7a8a7f39-df28-0410-adc6-e0d955640f24
-rw-r--r--ChangeLog37
-rw-r--r--NEWS44
-rw-r--r--mfd/Makefile.am1
-rw-r--r--mfd/bi_getopt.m4129
-rw-r--r--mfd/drivers.c99
-rw-r--r--mfd/gram.y175
-rw-r--r--mfd/lex.l8
-rw-r--r--mfd/mailfromd.h12
-rw-r--r--mfd/main.c69
-rw-r--r--mfd/opcodes1
-rw-r--r--mfd/prog.c40
-rw-r--r--mfd/prog.h3
-rw-r--r--mfd/snarf.m44
-rw-r--r--mfd/symtab.c6
14 files changed, 573 insertions, 55 deletions
diff --git a/ChangeLog b/ChangeLog
index db7d7df9..3116aef7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,40 @@
12008-09-16 Sergey Poznyakoff <gray@gnu.org.ua>
2
3 Implement functions with variable number of arguments and 'run' mode.
4
5 * mfd/symtab.c (function_install): Take additional argument.
6 * mfd/lex.l: New tokens `vaptr', `...' and ARGX.
7 * mfd/snarf.m4 (MF_VASTRING): New define.
8 * mfd/gram.y: Handle functions with variable number of arguments,
9 $(expr), and vaptr() expressions. This raises shift/reduce
10 expectation to 29 conflicts.
11 (parmtype_function): Return dtype_string for
12 arguments from vararg list.
13 (FUNC_HIDDEN_ARGS): Return 1 if f->varargs is set.
14 (type_to_string): Handle pointer argument.
15 (function_call): Handle function->varargs.
16 (node_type): Handle node_type_argx, and node_type_vaptr.
17 (cast_arg_list): Handle variable number of arguments.
18 (cast_to): Handle dtype_pointer as dtype_number.
19 * mfd/drivers.c (argx, vaptr): New node types.
20 (code_type_call): Handle variable number of arguments.
21 (code_cast): Handle dtype_pointer as dtype_number.
22 * mfd/mailfromd.h (MAILFROMD_RUN): New define.
23 (struct argx_node): New type.
24 (struct node): Add argx_node.
25 (struct function): New member `varargs'.
26 (function_install): Add varargs.
27 (mailfromd_run): New prototype.
28 * mfd/prog.c (env_vaptr, env_get_reg): New functions.
29 (env_make_frame0): New function.
30 (instr_xmemstk): New instruction handler.
31 * mfd/opcodes (XMEMSTK): New opcode.
32 * mfd/prog.h (env_get_reg, env_vaptr): New protos.
33 * mfd/main.c (options): New option `--run'.
34 (main): Handle `run' mode.
35 * mfd/Makefile.am (M4_FILES): Add bi_getopt.m4.
36 * NEWS: Update.
37
12008-09-15 Sergey Poznyakoff <gray@gnu.org.ua> 382008-09-15 Sergey Poznyakoff <gray@gnu.org.ua>
2 39
3 * tests/alias.at: New file. 40 * tests/alias.at: New file.
diff --git a/NEWS b/NEWS
index 90975a06..d44443ac 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
1Mailfromd NEWS -- history of user-visible changes. 2008-09-15 1Mailfromd NEWS -- history of user-visible changes. 2008-09-16
2Copyright (C) 2005, 2006, 2007, 2008 Sergey Poznyakoff 2Copyright (C) 2005, 2006, 2007, 2008 Sergey Poznyakoff
3See the end of file for copying conditions. 3See the end of file for copying conditions.
4 4
@@ -19,6 +19,8 @@ It can be converted to a usual string using the `body_string' function
19 19
20* Changes to MFL 20* Changes to MFL
21 21
22** Function aliases
23
22Functions can have several names. Alternative function names, or 24Functions can have several names. Alternative function names, or
23aliases, are introduced by `alias' statement, placed between the 25aliases, are introduced by `alias' statement, placed between the
24function declaration and return type declaration, e.g.: 26function declaration and return type declaration, e.g.:
@@ -32,6 +34,30 @@ done
32 34
33Any number of aliases is allowed. 35Any number of aliases is allowed.
34 36
37** Functions with variable number of arguments
38
39Ellipsis as the last argument in a list of formal arguments to a
40function indicates that this function takes a variable number of
41arguments. For example:
42
43 func foo (string a ; string b, ...)
44
45Actual arguments passed in a list of variable arguments have string
46data type. A special construct is provided to access these arguments:
47
48 $(expr)
49
50where expr is any valid MFL expression, evaluating to a number. This
51construct returns exprth argument from the variable argument list.
52
53FIXME: Document it.
54
55** getopt and varptr
56
57New function `getopt' is provided.
58
59FIXME: Document it.
60
35* New MFL functions 61* New MFL functions
36 62
37** body_string 63** body_string
@@ -93,6 +119,20 @@ the end of the string. Thus:
93 substring("mailfrom",4,-1) => "from" 119 substring("mailfrom",4,-1) => "from"
94 substring("mailfrom",4,-2) => "fro" 120 substring("mailfrom",4,-2) => "fro"
95 121
122* New operation mode.
123
124When given `--run' command line option, mailfromd looks for a function
125named `main' and invokes it, passing the rest of command line as its
126arguments. The function `main' must be declared as:
127
128 func main(...) returns number
129
130The return value from this function is used as the exit code.
131
132Command line arguments may be processed using `getopt' builtin function.
133
134FIXME: Document it.
135
96* New programs: 136* New programs:
97 137
98** smap. 138** smap.
@@ -172,7 +212,7 @@ Quarantines the message using the given reason.
172 212
173* mtasim 213* mtasim
174 214
175New command line options `--usage' and `--group' allow to specify user 215New command line options `--user' and `--group' allow to specify user
176name and a list of additional groups when the program is run with 216name and a list of additional groups when the program is run with
177`root' privileges. 217`root' privileges.
178 218
diff --git a/mfd/Makefile.am b/mfd/Makefile.am
index 136ab520..829454be 100644
--- a/mfd/Makefile.am
+++ b/mfd/Makefile.am
@@ -23,6 +23,7 @@ M4_FILES=\
23 bi_body.m4\ 23 bi_body.m4\
24 bi_db.m4\ 24 bi_db.m4\
25 bi_dns.m4\ 25 bi_dns.m4\
26 bi_getopt.m4\
26 bi_gettext.m4\ 27 bi_gettext.m4\
27 bi_header.m4\ 28 bi_header.m4\
28 bi_io.m4\ 29 bi_io.m4\
diff --git a/mfd/bi_getopt.m4 b/mfd/bi_getopt.m4
new file mode 100644
index 00000000..70a7e555
--- /dev/null
+++ b/mfd/bi_getopt.m4
@@ -0,0 +1,129 @@
1/* This file is part of Mailfromd. -*- c -*-
2 Copyright (C) 2008 Sergey Poznyakoff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include <unistd.h>
18#include <stdlib.h>
19#include <getopt.h>
20
21MF_VAR(optarg, STRING);
22MF_VAR(optind, NUMBER);
23
24MF_DEFUN_VARARGS(getopt, STRING, NUMBER argc, NUMBER argoff)
25{
26 int rc;
27 int long_idx;
28 static char xargc;
29 static char **xargv;
30 static struct option *option;
31 static char *optstr;
32 static char *loptstr;
33
34 if (argc) {
35 size_t i, n;
36 size_t serial = 256;
37
38 xargc = argc + 1;
39 xargv = xcalloc(xargc+1, sizeof(xargv[0]));
40 xargv[0] = script_file;
41 for (i = 0; i < argc; i++)
42 xargv[i+1] = MF_VASTRING(argoff + i);
43 xargv[i+1] = NULL;
44
45 n = MF_VA_COUNT();
46 if (n) {
47 size_t i, j;
48 size_t len;
49 char *str;
50 size_t size, lsize, lcnt;
51
52 MF_VA_START();
53
54 size = lsize = lcnt = 0;
55 for (i = 0; i < n; i++) {
56 MF_VA_ARG(i, STRING, str);
57 len = strcspn(str, ",");
58 size += len;
59 if (str[len]) {
60 lcnt++;
61 lsize += strlen(str + len);
62 }
63 }
64
65 optstr = xrealloc(optstr, size + 1);
66 loptstr = xrealloc(loptstr, lsize + 1);
67 option = xreall