From 9ee8aeb59ad3b3873d52f3c9a2ab80b28c4c2c20 Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Tue, 2 Jul 2013 20:45:46 -0400 Subject: After the gawkapi get_file function opens a file, call the BEGINFILE block. --- gawkapi.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'gawkapi.c') diff --git a/gawkapi.c b/gawkapi.c index 3fc23388..e12e55d5 100644 --- a/gawkapi.c +++ b/gawkapi.c @@ -1054,6 +1054,9 @@ api_lookup_file(awk_ext_id_t id, const char *name, size_t namelen) /* api_get_file --- return a handle to an existing or newly opened file */ +extern INSTRUCTION *main_beginfile; +extern int currule; + static const awk_input_buf_t * api_get_file(awk_ext_id_t id, const char *name, size_t namelen, const char *filetype, size_t typelen) { @@ -1065,8 +1068,27 @@ api_get_file(awk_ext_id_t id, const char *name, size_t namelen, const char *file if (curfile == NULL) { if (nextfile(& curfile, false) <= 0) return NULL; - /* XXX Fix me! */ - fputs("Bug: need to call BEGINFILE!\n", stderr); + { + INSTRUCTION *pc = main_beginfile; + /* save execution state */ + int save_rule = currule; + char *save_source = source; + + while (1) { + if (!pc) + fatal(_("cannot find end of BEGINFILE rule")); + if (pc->opcode == Op_after_beginfile) + break; + pc = pc->nexti; + } + pc->opcode = Op_stop; + (void) (*interpret)(main_beginfile); + pc->opcode = Op_after_beginfile; + after_beginfile(& curfile); + /* restore execution state */ + currule = save_rule; + source = save_source; + } } return &curfile->public; } -- cgit v1.2.3