summaryrefslogtreecommitdiff
path: root/gawkapi.c
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2013-07-02 20:45:46 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2013-07-02 20:45:46 -0400
commit9ee8aeb59ad3b3873d52f3c9a2ab80b28c4c2c20 (patch)
treeb51848f2af88f781a920b75ff6aca4efe815bdda /gawkapi.c
parentf0391b8a4db649853ecc47a10b09d7c4b04330cf (diff)
downloadgawk-9ee8aeb59ad3b3873d52f3c9a2ab80b28c4c2c20.tar.gz
After the gawkapi get_file function opens a file, call the BEGINFILE block.
Diffstat (limited to 'gawkapi.c')
-rw-r--r--gawkapi.c26
1 files changed, 24 insertions, 2 deletions
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;
}