3 @@ -53,9 +53,14 @@ typedef struct chain_s {
7 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
8 typedef struct func_s {
10 + enum { AWKFUNC, CFUNC } type;
18 @@ -1424,7 +1429,8 @@ static void parse_program(char *p)
19 next_token(TC_FUNCTION);
21 f = newfunc(t_string);
22 - f->body.first = NULL;
24 + f->x.body.first = NULL;
26 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
27 v = findvar(ahash, t_string);
28 @@ -1433,7 +1439,7 @@ static void parse_program(char *p)
29 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
37 @@ -2446,7 +2452,8 @@ static var *evaluate(node *op, var *res)
41 - if (!op->r.f->body.first)
42 + if ((op->r.f->type == AWKFUNC) &&
43 + !op->r.f->x.body.first)
44 syntax_error(EMSG_UNDEF_FUNC);
46 X.v = R.v = nvalloc(op->r.f->nargs + 1);
47 @@ -2463,7 +2470,10 @@ static var *evaluate(node *op, var *res)
51 - res = evaluate(op->r.f->body.first, res);
52 + if (op->r.f->type == AWKFUNC)
53 + res = evaluate(op->r.f->x.body.first, res);
54 + else if (op->r.f->type == CFUNC)
55 + res = op->r.f->x.cfunc(res, fnargs, op->r.f->nargs);
59 @@ -2829,6 +2839,143 @@ static rstream *next_input_file(void)
63 +/* read the contents of an entire file */
64 +static char *get_file(const char *fname)
70 + F = fopen(fname, "r");
75 + if (fseek(F, 0, SEEK_END) == 0) {
77 + s = (char *)xmalloc(flen+4);
78 + fseek(F, 0, SEEK_SET);
79 + i = 1 + fread(s+1, 1, flen, F);
81 + for (i=j=1; j>0; i+=j) {
82 + s = (char *)xrealloc(s, i+4096);
83 + j = fread(s+i, 1, 4094, F);
95 + * taken from parse_program from awk.c
96 + * END{} is not parsed here, and BEGIN{} is executed immediately
98 +static void parse_include(char *p)
101 + chain *initseq = NULL;
107 + memset(&tmp, 0, sizeof(tmp));
110 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
111 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
112 + if (tclass & TC_OPTERM)
116 + if (tclass & TC_BEGIN) {
117 + initseq = xzalloc(sizeof(chain));
120 + } else if (tclass & TC_FUNCDECL) {
121 + next_token(TC_FUNCTION);
123 + f = newfunc(t_string);
125 + f->x.body.first = NULL;
127 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
128 + v = findvar(ahash, t_string);
129 + v->x.aidx = (f->nargs)++;
131 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
134 + seq = &(f->x.body);
136 + clear_array(ahash);
139 + if (initseq && initseq->first)
140 + tv = evaluate(initseq->first, tv);
145 +/* include an awk file and run its BEGIN{} section */
146 +static xhash *includes = NULL;
147 +static void include_file(const char *filename)
151 + int oldlnr = g_lineno;
152 + const char *oldprg = g_progname;
155 + includes = hash_init();
157 + /* find out if the file has been included already */
158 + v = findvar(includes, filename);
163 + /* read include file */
164 + s = get_file(filename);
166 + fprintf(stderr, "Could not open file.\n");
170 + g_progname = xstrdup(filename);
171 + parse_include(s+1);
174 + g_progname = oldprg;
177 +static var *include(var *res, var *args, int nargs)
181 + nargs = nargs; /* shut up, gcc */
182 + s = getvar_s(args);
183 + if (s && (strlen(s) > 0))
189 +/* registers a global c function for the awk interpreter */
190 +static void register_cfunc(const char *name, awk_cfunc cfunc, int nargs)
196 + f->x.cfunc = cfunc;
200 int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
201 int awk_main(int argc, char **argv)
203 @@ -2894,6 +3041,9 @@ int awk_main(int argc, char **argv)
208 + register_cfunc("include", include, 1);
210 opt_complementary = "v::f::"; /* -v and -f can occur multiple times */
211 opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W);