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). --- extension/fork.c | 114 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 47 deletions(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 3c288c04..a7f96017 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -25,102 +25,122 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#include "awk.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include "config.h" +#include "gawkapi.h" + +static const gawk_api_t *api; /* for convenience macros to work */ +static awk_ext_id_t *ext_id; int plugin_is_GPL_compatible; + +/* array_set --- set an array element */ + +static void +array_set_numeric(awk_array_t array, const char *sub, double num) +{ + awk_element_t element; + awk_value_t tmp; + + memset(& element, 0, sizeof(element)); + + element.index = dup_string(sub, strlen(sub), & tmp)->str_value; + make_number(num, &element.value); + + set_array_element(array, & element); +} + /* do_fork --- provide dynamically loaded fork() builtin for gawk */ -static NODE * -do_fork(int nargs) +static awk_value_t * +do_fork(int nargs, awk_value_t *result) { int ret = -1; - NODE **aptr; - NODE *tmp; if (do_lint && nargs > 0) - lintwarn("fork: called with too many arguments"); + lintwarn(ext_id, "fork: called with too many arguments"); ret = fork(); if (ret < 0) update_ERRNO_int(errno); - else if (ret == 0 && PROCINFO_node != NULL) { - /* update PROCINFO in the child */ - - aptr = assoc_lookup(PROCINFO_node, tmp = make_string("pid", 3)); - unref(*aptr); - *aptr = make_number((AWKNUM) getpid()); - unref(tmp); - - aptr = assoc_lookup(PROCINFO_node, tmp = make_string("ppid", 4)); - unref(*aptr); - *aptr = make_number((AWKNUM) getppid()); - unref(tmp); + else if (ret == 0) { + /* update PROCINFO in the child, if the array exists */ + awk_value_t procinfo; + if (sym_lookup("PROCINFO", &procinfo) != NULL) { + if (procinfo.val_type != AWK_ARRAY) { + if (do_lint) + lintwarn(ext_id, "fork: PROCINFO is not an array!"); + } else { + array_set_numeric(procinfo.array_cookie, "pid", getpid()); + array_set_numeric(procinfo.array_cookie, "ppid", getppid()); + } + } } /* Set the return value */ - return make_number((AWKNUM) ret); + return make_number(ret, result); } /* do_waitpid --- provide dynamically loaded waitpid() builtin for gawk */ -static NODE * -do_waitpid(int nargs) +static awk_value_t * +do_waitpid(int nargs, awk_value_t *result) { - NODE *pidnode; + awk_value_t pid; int ret = -1; - double pidval; - pid_t pid; int options = 0; if (do_lint && nargs > 1) - lintwarn("waitpid: called with too many arguments"); + lintwarn(ext_id, "waitpid: called with too many arguments"); - pidnode = get_scalar_argument(0, false); - if (pidnode != NULL) { - pidval = get_number_d(pidnode); - pid = (int) pidval; + if (get_curfunc_param(0, AWK_NUMBER, &pid) != NULL) { options = WNOHANG|WUNTRACED; - ret = waitpid(pid, NULL, options); + ret = waitpid(pid.num_value, NULL, options); if (ret < 0) update_ERRNO_int(errno); } else if (do_lint) - lintwarn("wait: called with no arguments"); + lintwarn(ext_id, "wait: called with no arguments"); /* Set the return value */ - return make_number((AWKNUM) ret); + return make_number(ret, result); } /* do_wait --- provide dynamically loaded wait() builtin for gawk */ -static NODE * -do_wait(int nargs) +static awk_value_t * +do_wait(int nargs, awk_value_t *result) { int ret; if (do_lint && nargs > 0) - lintwarn("wait: called with too many arguments"); + lintwarn(ext_id, "wait: called with too many arguments"); ret = wait(NULL); if (ret < 0) update_ERRNO_int(errno); /* Set the return value */ - return make_number((AWKNUM) ret); + return make_number(ret, result); } -/* dlload --- load new builtins in this library */ +static awk_ext_func_t func_table[] = { + { "fork", do_fork, 0 }, + { "waitpid", do_waitpid, 1 }, + { "wait", do_wait, 0 }, +}; -NODE * -dlload(tree, dl) -NODE *tree; -void *dl; -{ - make_builtin("fork", do_fork, 0); - make_builtin("waitpid", do_waitpid, 1); - make_builtin("wait", do_wait, 0); - return make_number((AWKNUM) 0); -} +/* define the dl_load function using the boilerplate macro */ + +dl_load_func(func_table, fork, "") -- cgit v1.2.3 From 04dc190623f0d99d80387b33ca747b8cbad37724 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 29 May 2012 23:33:27 +0300 Subject: Further API work. --- extension/fork.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index a7f96017..1d4ad82c 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -2,10 +2,11 @@ * fork.c - Provide fork and waitpid functions for gawk. * * Revised 6/2004 + * Revised 5/2012 for new extension API. */ /* - * Copyright (C) 2001, 2004, 2011 the Free Software Foundation, Inc. + * Copyright (C) 2001, 2004, 2011, 2012 the Free Software Foundation, Inc. * * This file is part of GAWK, the GNU implementation of the * AWK Programming Language. @@ -76,7 +77,8 @@ do_fork(int nargs, awk_value_t *result) else if (ret == 0) { /* update PROCINFO in the child, if the array exists */ awk_value_t procinfo; - if (sym_lookup("PROCINFO", &procinfo) != NULL) { + + if (sym_lookup("PROCINFO", & procinfo, AWK_ARRAY) != NULL) { if (procinfo.val_type != AWK_ARRAY) { if (do_lint) lintwarn(ext_id, "fork: PROCINFO is not an array!"); @@ -91,7 +93,6 @@ do_fork(int nargs, awk_value_t *result) return make_number(ret, result); } - /* do_waitpid --- provide dynamically loaded waitpid() builtin for gawk */ static awk_value_t * -- cgit v1.2.3 From 820b6a2ccb7859e15ade36af6ac1d0d08c1da4b1 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 12 Jun 2012 22:10:31 +0300 Subject: Further cleanups and improvements in API. --- extension/fork.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 1d4ad82c..0c2e31d0 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -78,7 +78,7 @@ do_fork(int nargs, awk_value_t *result) /* update PROCINFO in the child, if the array exists */ awk_value_t procinfo; - if (sym_lookup("PROCINFO", & procinfo, AWK_ARRAY) != NULL) { + if (sym_lookup("PROCINFO", AWK_ARRAY, & procinfo)) { if (procinfo.val_type != AWK_ARRAY) { if (do_lint) lintwarn(ext_id, "fork: PROCINFO is not an array!"); @@ -105,7 +105,7 @@ do_waitpid(int nargs, awk_value_t *result) if (do_lint && nargs > 1) lintwarn(ext_id, "waitpid: called with too many arguments"); - if (get_curfunc_param(0, AWK_NUMBER, &pid) != NULL) { + if (get_argument(0, AWK_NUMBER, &pid)) { options = WNOHANG|WUNTRACED; ret = waitpid(pid.num_value, NULL, options); if (ret < 0) -- cgit v1.2.3 From 5e79fa8735ec2984fee9054cccd51d86fa939621 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sun, 17 Jun 2012 20:47:50 +0300 Subject: Still more API and testext.c work. --- extension/fork.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 0c2e31d0..21a4145f 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -26,9 +26,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#include #include +#include #include +#include #include #include #include @@ -67,7 +68,9 @@ do_fork(int nargs, awk_value_t *result) { int ret = -1; - if (do_lint && nargs > 0) + assert(result != NULL); + + if (do_lint && nargs > 0) lintwarn(ext_id, "fork: called with too many arguments"); ret = fork(); @@ -102,7 +105,9 @@ do_waitpid(int nargs, awk_value_t *result) int ret = -1; int options = 0; - if (do_lint && nargs > 1) + assert(result != NULL); + + if (do_lint && nargs > 1) lintwarn(ext_id, "waitpid: called with too many arguments"); if (get_argument(0, AWK_NUMBER, &pid)) { @@ -125,7 +130,9 @@ do_wait(int nargs, awk_value_t *result) { int ret; - if (do_lint && nargs > 0) + assert(result != NULL); + + if (do_lint && nargs > 0) lintwarn(ext_id, "wait: called with too many arguments"); ret = wait(NULL); -- cgit v1.2.3 From 1e3ac8a49caeeb991d8163042a576a66db51c74b Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Mon, 18 Jun 2012 23:00:58 +0300 Subject: Get most of array flattening done. --- extension/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 21a4145f..58089d55 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -55,7 +55,7 @@ array_set_numeric(awk_array_t array, const char *sub, double num) memset(& element, 0, sizeof(element)); - element.index = dup_string(sub, strlen(sub), & tmp)->str_value; + element.index = *make_string(sub, strlen(sub), & tmp); make_number(num, &element.value); set_array_element(array, & element); -- cgit v1.2.3 From 7d37bcd5a8066718b15de8c03725708819389931 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Mon, 9 Jul 2012 21:17:10 +0300 Subject: API: Update set_array_element(). Adjust extensions. --- extension/fork.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 58089d55..efad17eb 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -50,15 +50,12 @@ int plugin_is_GPL_compatible; static void array_set_numeric(awk_array_t array, const char *sub, double num) { - awk_element_t element; - awk_value_t tmp; + awk_value_t index, value; - memset(& element, 0, sizeof(element)); + set_array_element(array, + make_string(sub, strlen(sub), & index), + make_number(num, & value)); - element.index = *make_string(sub, strlen(sub), & tmp); - make_number(num, &element.value); - - set_array_element(array, & element); } /* do_fork --- provide dynamically loaded fork() builtin for gawk */ -- cgit v1.2.3 From 6d1724214a95330b63a6a557f89fb9b40b4a521f Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 11 Jul 2012 21:26:37 +0300 Subject: API clean up and require strings to be malloced. --- extension/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index efad17eb..84232663 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -53,7 +53,7 @@ array_set_numeric(awk_array_t array, const char *sub, double num) awk_value_t index, value; set_array_element(array, - make_string(sub, strlen(sub), & index), + make_const_string(sub, strlen(sub), & index), make_number(num, & value)); } -- cgit v1.2.3 From 73533707616e119778993fe18540098239ecbb2e Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 11 Jul 2012 21:41:54 +0300 Subject: Add ability to call an initialization routine. --- extension/fork.c | 1 + 1 file changed, 1 insertion(+) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 84232663..02b6b6f2 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -41,6 +41,7 @@ static const gawk_api_t *api; /* for convenience macros to work */ static awk_ext_id_t *ext_id; +static awk_bool_t (*init_func)(void) = NULL; int plugin_is_GPL_compatible; -- cgit v1.2.3 From 7e5b2a94ce3c089c50c5862168d1d917e5febcf4 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 25 Jul 2012 23:10:35 +0300 Subject: Add translation to the extensions. --- extension/fork.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'extension/fork.c') diff --git a/extension/fork.c b/extension/fork.c index 02b6b6f2..7bee8ba1 100644 --- a/extension/fork.c +++ b/extension/fork.c @@ -39,6 +39,10 @@ #include "config.h" #include "gawkapi.h" +#include "gettext.h" +#define _(msgid) gettext(msgid) +#define N_(msgid) msgid + static const gawk_api_t *api; /* for convenience macros to work */ static awk_ext_id_t *ext_id; static awk_bool_t (*init_func)(void) = NULL; @@ -69,7 +73,7 @@ do_fork(int nargs, awk_value_t *result) assert(result != NULL); if (do_lint && nargs > 0) - lintwarn(ext_id, "fork: called with too many arguments"); + lintwarn(ext_id, _("fork: called with too many arguments")); ret = fork(); @@ -82,7 +86,7 @@ do_fork(int nargs, awk_value_t *result) if (sym_lookup("PROCINFO", AWK_ARRAY, & procinfo)) { if (procinfo.val_type != AWK_ARRAY) { if (do_lint) - lintwarn(ext_id, "fork: PROCINFO is not an array!"); + lintwarn(ext_id, _("fork: PROCINFO is not an array!")); } else { array_set_numeric(procinfo.array_cookie, "pid", getpid()); array_set_numeric(procinfo.array_cookie, "ppid", getppid()); @@ -106,7 +110,7 @@ do_waitpid(int nargs, awk_value_t *result) assert(result != NULL); if (do_lint && nargs > 1) - lintwarn(ext_id, "waitpid: called with too many arguments"); + lintwarn(ext_id, _("waitpid: called with too many arguments")); if (get_argument(0, AWK_NUMBER, &pid)) { options = WNOHANG|WUNTRACED; @@ -114,7 +118,7 @@ do_waitpid(int nargs, awk_value_t *result) if (ret < 0) update_ERRNO_int(errno); } else if (do_lint) - lintwarn(ext_id, "wait: called with no arguments"); + lintwarn(ext_id, _("wait: called with no arguments")); /* Set the return value */ return make_number(ret, result); @@ -131,7 +135,7 @@ do_wait(int nargs, awk_value_t *result) assert(result != NULL); if (do_lint && nargs > 0) - lintwarn(ext_id, "wait: called with too many arguments"); + lintwarn(ext_id, _("wait: called with too many arguments")); ret = wait(NULL); if (ret < 0) -- cgit v1.2.3