summaryrefslogtreecommitdiff
path: root/extension/fork.c
diff options
context:
space:
mode:
Diffstat (limited to 'extension/fork.c')
-rw-r--r--extension/fork.c143
1 files changed, 100 insertions, 43 deletions
diff --git a/extension/fork.c b/extension/fork.c
index 88353879..0ca0a0e0 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, 2013 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
@@ -25,80 +26,136 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
-#include "awk.h"
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#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 "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 const char *ext_version = "fork extension: version 1.0";
+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();
+ update_ERRNO_int(errno);
else if (ret == 0) {
- /* update PROCINFO in the child */
-
- aptr = assoc_lookup(PROCINFO_node, tmp = make_string("pid", 3));
- (*aptr)->numbr = (AWKNUM) getpid();
- unref(tmp);
-
- aptr = assoc_lookup(PROCINFO_node, tmp = make_string("ppid", 4));
- (*aptr)->numbr = (AWKNUM) getppid();
- unref(tmp);
+ /* 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);
- pidnode = get_scalar_argument(0, FALSE);
- if (pidnode != NULL) {
- pidval = force_number(pidnode);
- pid = (int) pidval;
+ if (do_lint && nargs > 1)
+ lintwarn(ext_id, _("waitpid: called with too many arguments"));
+
+ 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();
+ 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);
}
-/* dlload --- load new builtins in this library */
-NODE *
-dlload(tree, dl)
-NODE *tree;
-void *dl;
+/* do_wait --- provide dynamically loaded wait() builtin for gawk */
+
+static awk_value_t *
+do_wait(int nargs, awk_value_t *result)
{
- make_builtin("fork", do_fork, 0);
- make_builtin("waitpid", do_waitpid, 1);
- return make_number((AWKNUM) 0);
+ int ret;
+
+ 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(ret, result);
}
+
+static awk_ext_func_t func_table[] = {
+ { "fork", do_fork, 0 },
+ { "waitpid", do_waitpid, 1 },
+ { "wait", do_wait, 0 },
+};
+
+/* define the dl_load function using the boilerplate macro */
+
+dl_load_func(func_table, fork, "")