diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2012-08-17 12:38:04 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2012-08-17 12:38:04 +0300 |
commit | e6b05afd9971b457c0b46907a91185b66be8ff4e (patch) | |
tree | e7d3b6fce47a2ac7b32cdf923ceb6e50db9fe441 /extension/fork.c | |
parent | 3b23e177cd166e96c700379491b4a99bddf9aa4d (diff) | |
parent | 76cc4d241f328876b18e48639d631823c3d304d6 (diff) | |
download | egawk-e6b05afd9971b457c0b46907a91185b66be8ff4e.tar.gz egawk-e6b05afd9971b457c0b46907a91185b66be8ff4e.tar.bz2 egawk-e6b05afd9971b457c0b46907a91185b66be8ff4e.zip |
Merge branch 'extgawk'
Diffstat (limited to 'extension/fork.c')
-rw-r--r-- | extension/fork.c | 134 |
1 files changed, 82 insertions, 52 deletions
diff --git a/extension/fork.c b/extension/fork.c index 3c288c04..7bee8ba1 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. @@ -25,102 +26,131 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#include "awk.h" +#include <stdio.h> +#include <assert.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/wait.h> + +#include <sys/types.h> +#include <sys/stat.h> +#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; 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_value_t index, value; + + set_array_element(array, + make_const_string(sub, strlen(sub), & index), + make_number(num, & value)); + +} + /* 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"); + assert(result != NULL); + + if (do_lint && nargs > 0) + 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", AWK_ARRAY, & procinfo)) { + 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"); + assert(result != NULL); + + if (do_lint && nargs > 1) + 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_argument(0, AWK_NUMBER, &pid)) { 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"); + assert(result != NULL); + + if (do_lint && nargs > 0) + 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, "") |