aboutsummaryrefslogtreecommitdiffstats
path: root/gawkapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'gawkapi.c')
-rw-r--r--gawkapi.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/gawkapi.c b/gawkapi.c
index bcf8d90a..46aef7b6 100644
--- a/gawkapi.c
+++ b/gawkapi.c
@@ -25,6 +25,11 @@
#include "awk.h"
+/* Declare some globals used by api_get_file: */
+extern IOBUF *curfile;
+extern INSTRUCTION *main_beginfile;
+extern int currule;
+
static awk_bool_t node_to_awk_value(NODE *node, awk_value_t *result, awk_valtype_t wanted);
/*
@@ -1032,6 +1037,86 @@ api_release_value(awk_ext_id_t id, awk_value_cookie_t value)
return awk_true;
}
+/* api_get_file --- return a handle to an existing or newly opened file */
+
+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)
+{
+ const struct redirect *f;
+ int flag; /* not used, sigh */
+ enum redirval redirtype;
+
+ if ((name == NULL) || (namelen == 0)) {
+ if (curfile == NULL) {
+ if (nextfile(& curfile, false) <= 0)
+ return NULL;
+ {
+ 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;
+ }
+ redirtype = redirect_none;
+ switch (typelen) {
+ case 1:
+ switch (*filetype) {
+ case '<':
+ redirtype = redirect_input;
+ break;
+ case '>':
+ redirtype = redirect_output;
+ break;
+ }
+ break;
+ case 2:
+ switch (*filetype) {
+ case '>':
+ if (filetype[1] == '>')
+ redirtype = redirect_append;
+ break;
+ case '|':
+ switch (filetype[1]) {
+ case '>':
+ redirtype = redirect_pipe;
+ break;
+ case '<':
+ redirtype = redirect_pipein;
+ break;
+ case '&':
+ redirtype = redirect_twoway;
+ break;
+ }
+ break;
+ }
+ }
+ if (redirtype == redirect_none) {
+ warning(_("cannot open unrecognized file type `%s' for `%s'"),
+ filetype, name);
+ return NULL;
+ }
+ if ((f = redirect_string(name, namelen, 0, redirtype, &flag)) == NULL)
+ return NULL;
+ return &f->iop->public;
+}
+
/*
* Register a version string for this extension with gawk.
*/
@@ -1117,6 +1202,9 @@ gawk_api_t api_impl = {
calloc,
realloc,
free,
+
+ /* Find/open a file */
+ api_get_file,
};
/* init_ext_api --- init the extension API */