From 577c3fc31a2718461fba2e599d162de96fe838fa Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Thu, 24 May 2012 15:34:17 -0400 Subject: First working version of new API mechanism (probably has memory leaks). --- ext.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 4dacad30..0da61746 100644 --- a/ext.c +++ b/ext.c @@ -42,12 +42,12 @@ do_ext(int nargs) SRCFILE *s; extern SRCFILE *srcfiles; - fun = POP_STRING(); - obj = POP_STRING(); + fun = POP_STRING(); /* name of initialization function */ + obj = POP_STRING(); /* name of shared object */ s = add_srcfile(SRC_EXTLIB, obj->stptr, srcfiles, NULL, NULL); if (s != NULL) - ret = load_ext(s->fullpath, fun->stptr, obj); + ret = load_ext(s->fullpath, fun->stptr); DEREF(obj); DEREF(fun); if (ret == NULL) @@ -58,10 +58,9 @@ do_ext(int nargs) /* load_ext --- load an external library */ NODE * -load_ext(const char *lib_name, const char *init_func, NODE *obj) +load_ext(const char *lib_name, const char *init_func) { - NODE *tmp = NULL; - NODE *(*func)(NODE *, void *); + int (*func)(const gawk_api_t *const, awk_ext_id_t); void *dl; int flags = RTLD_LAZY; int *gpl_compat; @@ -85,33 +84,31 @@ load_ext(const char *lib_name, const char *init_func, NODE *obj) if (gpl_compat == NULL) fatal(_("extension: library `%s': does not define `plugin_is_GPL_compatible' (%s)\n"), lib_name, dlerror()); - func = (NODE *(*)(NODE *, void *)) dlsym(dl, init_func); + func = (int (*)(const gawk_api_t *const, awk_ext_id_t)) dlsym(dl, init_func); if (func == NULL) fatal(_("extension: library `%s': cannot call function `%s' (%s)\n"), lib_name, init_func, dlerror()); - if (obj == NULL) { - obj = make_string(lib_name, strlen(lib_name)); - tmp = (*func)(obj, dl); - unref(tmp); - unref(obj); - return NULL; + if ((*func)(& api_impl, NULL /* ext_id */) == 0) { + warning(_("extension: library `%s' initialization routine `%s' failed\n"), + lib_name, init_func); + return make_number(-1); } - - tmp = (*func)(obj, dl); - return tmp; + return make_number(0); } /* make_builtin --- register name to be called as func with a builtin body */ -void -make_builtin(const char *name, NODE *(*func)(int), int count) +awk_bool_t +make_builtin(const awk_ext_func_t *funcinfo) { NODE *symbol, *f; INSTRUCTION *b; const char *sp; char c; + const char *name = funcinfo->name; + int count = funcinfo->num_args_expected; sp = name; if (sp == NULL || *sp == '\0') @@ -133,7 +130,7 @@ make_builtin(const char *name, NODE *(*func)(int), int count) /* multiple extension() calls etc. */ if (do_lint) lintwarn(_("extension: function `%s' already defined"), name); - return; + return false; } else /* variable name etc. */ fatal(_("extension: function name `%s' previously defined"), name); @@ -145,13 +142,14 @@ make_builtin(const char *name, NODE *(*func)(int), int count) name); b = bcalloc(Op_symbol, 1, 0); - b->builtin = func; + b->extfunc = funcinfo->function; b->expr_count = count; /* NB: extension sub must return something */ symbol = install_symbol(estrdup(name, strlen(name)), Node_ext_func); symbol->code_ptr = b; + return true; } -- cgit v1.2.3 From a9c75046c071c9a67455ef27be44cac0b64be3c4 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 5 Jun 2012 23:17:26 +0300 Subject: Minor edits in load_ext. --- ext.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 0da61746..66ea7fbe 100644 --- a/ext.c +++ b/ext.c @@ -69,28 +69,24 @@ load_ext(const char *lib_name, const char *init_func) fatal(_("extensions are not allowed in sandbox mode")); if (do_traditional || do_posix) - fatal(_("`extension' is a gawk extension")); - -#ifdef RTLD_GLOBAL - flags |= RTLD_GLOBAL; -#endif + fatal(_("-l / @load / `extension' are gawk extensions")); if ((dl = dlopen(lib_name, flags)) == NULL) - fatal(_("extension: cannot open library `%s' (%s)\n"), lib_name, + fatal(_("load_ext: cannot open library `%s' (%s)\n"), lib_name, dlerror()); /* Per the GNU Coding standards */ gpl_compat = (int *) dlsym(dl, "plugin_is_GPL_compatible"); if (gpl_compat == NULL) - fatal(_("extension: library `%s': does not define `plugin_is_GPL_compatible' (%s)\n"), + fatal(_("load_ext: library `%s': does not define `plugin_is_GPL_compatible' (%s)\n"), lib_name, dlerror()); func = (int (*)(const gawk_api_t *const, awk_ext_id_t)) dlsym(dl, init_func); if (func == NULL) - fatal(_("extension: library `%s': cannot call function `%s' (%s)\n"), + fatal(_("load_ext: library `%s': cannot call function `%s' (%s)\n"), lib_name, init_func, dlerror()); if ((*func)(& api_impl, NULL /* ext_id */) == 0) { - warning(_("extension: library `%s' initialization routine `%s' failed\n"), + warning(_("load_ext: library `%s' initialization routine `%s' failed\n"), lib_name, init_func); return make_number(-1); } -- cgit v1.2.3 From 8ce87087172ee5be4ee72a1513daad3821185bf7 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 12 Jun 2012 23:11:37 +0300 Subject: More API implementations and testext improvements. --- ext.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 66ea7fbe..d0755ccd 100644 --- a/ext.c +++ b/ext.c @@ -60,7 +60,7 @@ do_ext(int nargs) NODE * load_ext(const char *lib_name, const char *init_func) { - int (*func)(const gawk_api_t *const, awk_ext_id_t); + int (*install_func)(const gawk_api_t *const, awk_ext_id_t); void *dl; int flags = RTLD_LAZY; int *gpl_compat; @@ -80,12 +80,13 @@ load_ext(const char *lib_name, const char *init_func) if (gpl_compat == NULL) fatal(_("load_ext: library `%s': does not define `plugin_is_GPL_compatible' (%s)\n"), lib_name, dlerror()); - func = (int (*)(const gawk_api_t *const, awk_ext_id_t)) dlsym(dl, init_func); - if (func == NULL) + install_func = (int (*)(const gawk_api_t *const, awk_ext_id_t)) + dlsym(dl, init_func); + if (install_func == NULL) fatal(_("load_ext: library `%s': cannot call function `%s' (%s)\n"), lib_name, init_func, dlerror()); - if ((*func)(& api_impl, NULL /* ext_id */) == 0) { + if (install_func(& api_impl, NULL /* ext_id */) == 0) { warning(_("load_ext: library `%s' initialization routine `%s' failed\n"), lib_name, init_func); return make_number(-1); -- cgit v1.2.3 From fd3c1195711270a001d860770098b8c0d9118c10 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 19 Jun 2012 20:54:19 +0300 Subject: Delete marked elements from flattened array. --- ext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index d0755ccd..4945c7cb 100644 --- a/ext.c +++ b/ext.c @@ -152,7 +152,7 @@ make_builtin(const awk_ext_func_t *funcinfo) /* get_argument --- get the i'th argument of a dynamically linked function */ -NODE * +static NODE * get_argument(int i) { NODE *t; -- cgit v1.2.3 From d66f3c9922e36bb2e760e0ac36364c1a5aa11442 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 20 Jun 2012 21:41:15 +0300 Subject: API: Add set_parameter function and test. --- ext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 4945c7cb..d0755ccd 100644 --- a/ext.c +++ b/ext.c @@ -152,7 +152,7 @@ make_builtin(const awk_ext_func_t *funcinfo) /* get_argument --- get the i'th argument of a dynamically linked function */ -static NODE * +NODE * get_argument(int i) { NODE *t; -- cgit v1.2.3 From d0d954cce2ca5a2e0ed41116502b636446ac528f Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 21 Jun 2012 22:32:46 +0300 Subject: Remove extension() builtin. --- ext.c | 50 +++++++++----------------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index d0755ccd..911754bf 100644 --- a/ext.c +++ b/ext.c @@ -33,32 +33,12 @@ #include -/* do_ext --- load an extension at run-time: interface to load_ext */ - -NODE * -do_ext(int nargs) -{ - NODE *obj, *fun, *ret = NULL; - SRCFILE *s; - extern SRCFILE *srcfiles; - - fun = POP_STRING(); /* name of initialization function */ - obj = POP_STRING(); /* name of shared object */ - - s = add_srcfile(SRC_EXTLIB, obj->stptr, srcfiles, NULL, NULL); - if (s != NULL) - ret = load_ext(s->fullpath, fun->stptr); - DEREF(obj); - DEREF(fun); - if (ret == NULL) - ret = dupnode(Nnull_string); - return ret; -} +#define INIT_FUNC "dl_load" /* load_ext --- load an external library */ -NODE * -load_ext(const char *lib_name, const char *init_func) +void +load_ext(const char *lib_name) { int (*install_func)(const gawk_api_t *const, awk_ext_id_t); void *dl; @@ -69,7 +49,7 @@ load_ext(const char *lib_name, const char *init_func) fatal(_("extensions are not allowed in sandbox mode")); if (do_traditional || do_posix) - fatal(_("-l / @load / `extension' are gawk extensions")); + fatal(_("-l / @load are gawk extensions")); if ((dl = dlopen(lib_name, flags)) == NULL) fatal(_("load_ext: cannot open library `%s' (%s)\n"), lib_name, @@ -81,14 +61,14 @@ load_ext(const char *lib_name, const char *init_func) fatal(_("load_ext: library `%s': does not define `plugin_is_GPL_compatible' (%s)\n"), lib_name, dlerror()); install_func = (int (*)(const gawk_api_t *const, awk_ext_id_t)) - dlsym(dl, init_func); + dlsym(dl, INIT_FUNC); if (install_func == NULL) fatal(_("load_ext: library `%s': cannot call function `%s' (%s)\n"), - lib_name, init_func, dlerror()); + lib_name, INIT_FUNC, dlerror()); if (install_func(& api_impl, NULL /* ext_id */) == 0) { warning(_("load_ext: library `%s' initialization routine `%s' failed\n"), - lib_name, init_func); + lib_name, INIT_FUNC); return make_number(-1); } return make_number(0); @@ -231,23 +211,11 @@ get_actual_argument(int i, bool optional, bool want_array) #else -/* do_ext --- dummy version if extensions not available */ - -NODE * -do_ext(int nargs) -{ - const char *emsg = _("Operation Not Supported"); - - update_ERRNO_string(emsg, DONT_TRANSLATE); - return make_number((AWKNUM) -1); -} - /* load_ext --- dummy version if extensions not available */ -NODE * -load_ext(const char *lib_name, const char *init_func, NODE *obj) +void +load_ext(const char *lib_name) { fatal(_("dynamic loading of library not supported")); - return NULL; } #endif -- cgit v1.2.3 From 37cd3566b9b74c43d5f11f1cba8dec147a25e474 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 24 Jun 2012 21:07:22 +0300 Subject: Get rwarray extension working with new API. --- ext.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 911754bf..0b87def9 100644 --- a/ext.c +++ b/ext.c @@ -69,9 +69,9 @@ load_ext(const char *lib_name) if (install_func(& api_impl, NULL /* ext_id */) == 0) { warning(_("load_ext: library `%s' initialization routine `%s' failed\n"), lib_name, INIT_FUNC); - return make_number(-1); + return; } - return make_number(0); + return; } -- cgit v1.2.3 From 6139211362667682c3022a72321e0cd8945b6592 Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Sun, 24 Jun 2012 18:43:49 -0400 Subject: Hide private parts of IOBUF from extensions. --- ext.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 0b87def9..14d55c5f 100644 --- a/ext.c +++ b/ext.c @@ -66,12 +66,9 @@ load_ext(const char *lib_name) fatal(_("load_ext: library `%s': cannot call function `%s' (%s)\n"), lib_name, INIT_FUNC, dlerror()); - if (install_func(& api_impl, NULL /* ext_id */) == 0) { + if (install_func(& api_impl, NULL /* ext_id */) == 0) warning(_("load_ext: library `%s' initialization routine `%s' failed\n"), lib_name, INIT_FUNC); - return; - } - return; } -- cgit v1.2.3 From 93e689fa83ef9a78f2bdfa093af31fcecb429d58 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Mon, 25 Jun 2012 21:28:29 +0300 Subject: Fix lint checking for extension functions. --- ext.c | 1 + 1 file changed, 1 insertion(+) (limited to 'ext.c') diff --git a/ext.c b/ext.c index 14d55c5f..af6542d4 100644 --- a/ext.c +++ b/ext.c @@ -123,6 +123,7 @@ make_builtin(const awk_ext_func_t *funcinfo) symbol = install_symbol(estrdup(name, strlen(name)), Node_ext_func); symbol->code_ptr = b; + track_ext_func(name); return true; } -- cgit v1.2.3 From 3d2d6b46bf3325c0273b35a202184ab09d38e0cd Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 25 Jul 2012 22:34:19 +0300 Subject: Start refactoring iop handling. Add readdir extension. --- ext.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ext.c') diff --git a/ext.c b/ext.c index af6542d4..17ade95a 100644 --- a/ext.c +++ b/ext.c @@ -51,6 +51,9 @@ load_ext(const char *lib_name) if (do_traditional || do_posix) fatal(_("-l / @load are gawk extensions")); + if (lib_name == NULL) + fatal(_("load_ext: received NULL lib_name")); + if ((dl = dlopen(lib_name, flags)) == NULL) fatal(_("load_ext: cannot open library `%s' (%s)\n"), lib_name, dlerror()); @@ -60,6 +63,7 @@ load_ext(const char *lib_name) if (gpl_compat == NULL) fatal(_("load_ext: library `%s': does not define `plugin_is_GPL_compatible' (%s)\n"), lib_name, dlerror()); + install_func = (int (*)(const gawk_api_t *const, awk_ext_id_t)) dlsym(dl, INIT_FUNC); if (install_func == NULL) -- cgit v1.2.3