aboutsummaryrefslogtreecommitdiffstats
path: root/extension
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2010-07-16 13:14:38 +0300
committerArnold D. Robbins <arnold@skeeve.com>2010-07-16 13:14:38 +0300
commitfae4762eba9ff7bb466a600130e9c90eaac6b0bc (patch)
tree62711fe7cd511824b5f8a90ba1ba7b523d42e127 /extension
parentbc70de7b3302d5a81515b901cae376b8b51d2004 (diff)
downloadegawk-fae4762eba9ff7bb466a600130e9c90eaac6b0bc.tar.gz
egawk-fae4762eba9ff7bb466a600130e9c90eaac6b0bc.tar.bz2
egawk-fae4762eba9ff7bb466a600130e9c90eaac6b0bc.zip
Move to gawk-3.1.1.
Diffstat (limited to 'extension')
-rw-r--r--extension/arrayparm.c93
-rw-r--r--extension/ordchr.c106
-rw-r--r--extension/readfile.c109
-rwxr-xr-xextension/steps8
-rw-r--r--extension/testarrayparm.awk10
-rw-r--r--extension/testordchr.awk6
6 files changed, 332 insertions, 0 deletions
diff --git a/extension/arrayparm.c b/extension/arrayparm.c
new file mode 100644
index 00000000..9a158676
--- /dev/null
+++ b/extension/arrayparm.c
@@ -0,0 +1,93 @@
+/*
+ * arrayparm.c --- figure out how to make a parameter be an array
+ *
+ * Arnold Robbins
+ * arnold@skeeve.com
+ * 10/2001
+ */
+
+/*
+ * Copyright (C) 2001 the Free Software Foundation, Inc.
+ *
+ * This file is part of GAWK, the GNU implementation of the
+ * AWK Programming Language.
+ *
+ * GAWK is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GAWK is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include "awk.h"
+
+/* do_mkarray --- turn a variable into an array */
+
+/*
+ * From awk, call
+ *
+ * mkarray(var, sub, val)
+ */
+
+static NODE *
+do_mkarray(tree)
+NODE *tree;
+{
+ int ret = -1;
+ NODE *var, *sub, *val;
+ NODE **elemval;
+
+ if (do_lint && tree->param_cnt > 3)
+ lintwarn("mkarray: called with too many arguments");
+
+ var = get_argument(tree, 0);
+ if (var == NULL)
+ var = stack_ptr[0];
+ sub = get_argument(tree, 1);
+ val = get_argument(tree, 2);
+
+ printf("var->type = %s\n", nodetype2str(var->type));
+ printf("sub->type = %s\n", nodetype2str(sub->type));
+ printf("val->type = %s\n", nodetype2str(val->type));
+
+ if (var->var_array == NULL) {
+ if (var->type != Node_var_array) {
+ unref(var->var_value);
+ var->type = Node_var_array;
+ }
+ var->array_size = var->table_size = 0; /* sanity */
+ var->flags &= ~ARRAYMAXED;
+ }
+ assoc_clear(var);
+
+ elemval = assoc_lookup(var, sub, 0);
+ *elemval = dupnode(val);
+ ret = 0;
+
+
+ /* Set the return value */
+ set_value(tmp_number((AWKNUM) ret));
+
+ /* Just to make the interpreter happy */
+ return tmp_number((AWKNUM) 0);
+}
+
+/* dlload --- load new builtins in this library */
+
+NODE *
+dlload(tree, dl)
+NODE *tree;
+void *dl;
+{
+ make_builtin("mkarray", do_mkarray, 3);
+
+ return tmp_number((AWKNUM) 0);
+}
diff --git a/extension/ordchr.c b/extension/ordchr.c
new file mode 100644
index 00000000..037bfa37
--- /dev/null
+++ b/extension/ordchr.c
@@ -0,0 +1,106 @@
+/*
+ * ordchr.c - Builtin functions that provide ord() and chr() functions.
+ *
+ * Arnold Robbins
+ * arnold@skeeve.com
+ * 8/2001
+ */
+
+/*
+ * Copyright (C) 2001 the Free Software Foundation, Inc.
+ *
+ * This file is part of GAWK, the GNU implementation of the
+ * AWK Programming Language.
+ *
+ * GAWK is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GAWK is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include "awk.h"
+
+/* do_ord --- return numeric value of first char of string */
+
+static NODE *
+do_ord(tree)
+NODE *tree;
+{
+ NODE *str;
+ int ret = -1;
+
+ if (do_lint && tree->param_cnt > 1)
+ lintwarn("ord: called with too many arguments");
+
+ str = get_argument(tree, 0);
+ if (str != NULL) {
+ (void) force_string(str);
+ ret = str->stptr[0];
+ free_temp(str);
+ } else if (do_lint)
+ lintwarn("ord: called with no arguments");
+
+
+ /* Set the return value */
+ set_value(tmp_number((AWKNUM) ret));
+
+ /* Just to make the interpreter happy */
+ return tmp_number((AWKNUM) 0);
+}
+
+/* do_chr --- turn numeric value into a string */
+
+static NODE *
+do_chr(tree)
+NODE *tree;
+{
+ NODE *num;
+ unsigned int ret = 0;
+ AWKNUM val = 0.0;
+ char str[2];
+
+ str[0] = str[1] = '\0';
+
+ if (do_lint && tree->param_cnt > 1)
+ lintwarn("chr: called with too many arguments");
+
+ num = get_argument(tree, 0);
+ if (num != NULL) {
+ val = force_number(num);
+ ret = val; /* convert to int */
+ free_temp(num);
+ ret &= 0xff;
+ str[0] = ret;
+ str[1] = '\0';
+ } else if (do_lint)
+ lintwarn("chr: called with no arguments");
+
+
+ /* Set the return value */
+ set_value(tmp_string(str, 1));
+
+ /* Just to make the interpreter happy */
+ return tmp_number((AWKNUM) 0);
+}
+
+/* dlload --- load new builtins in this library */
+
+NODE *
+dlload(tree, dl)
+NODE *tree;
+void *dl;
+{
+ make_builtin("ord", do_ord, 1);
+ make_builtin("chr", do_chr, 1);
+
+ return tmp_number((AWKNUM) 0);
+}
diff --git a/extension/readfile.c b/extension/readfile.c
new file mode 100644
index 00000000..8713bc8d
--- /dev/null
+++ b/extension/readfile.c
@@ -0,0 +1,109 @@
+/*
+ * readfile.c - Read an entire file into a string.
+ *
+ * Arnold Robbins
+ * Tue Apr 23 17:43:30 IDT 2002
+ */
+
+/*
+ * Copyright (C) 2002 the Free Software Foundation, Inc.
+ *
+ * This file is part of GAWK, the GNU implementation of the
+ * AWK Programming Language.
+ *
+ * GAWK is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GAWK is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include "awk.h"
+#include <fcntl.h>
+
+/* do_readfile --- read a file into memory */
+
+NODE *
+do_readfile(tree)
+NODE *tree;
+{
+ NODE *filename;
+ int ret = -1;
+ struct stat sbuf;
+ char *text;
+ int fd;
+
+ if (do_lint && tree->param_cnt > 1)
+ lintwarn("readfile: called with too many arguments");
+
+ filename = get_argument(tree, 0);
+ if (filename != NULL) {
+ (void) force_string(filename);
+
+ ret = stat(filename->stptr, & sbuf);
+ if (ret < 0) {
+ update_ERRNO();
+ free_temp(filename);
+ goto done;
+ } else if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
+ errno = EINVAL;
+ ret = -1;
+ update_ERRNO();
+ free_temp(filename);
+ goto done;
+ }
+
+ if ((fd = open(filename->stptr, O_RDONLY)) < 0) {
+ ret = -1;
+ update_ERRNO();
+ free_temp(filename);
+ goto done;
+ }
+
+ emalloc(text, char *, sbuf.st_size + 2, "do_readfile");
+ memset(text, '\0', sbuf.st_size + 2);
+
+ if ((ret = read(fd, text, sbuf.st_size)) != sbuf.st_size) {
+ (void) close(fd);
+ ret = -1;
+ update_ERRNO();
+ free_temp(filename);
+ goto done;
+ }
+
+ close(fd);
+ free_temp(filename);
+ set_value(tmp_string(text, sbuf.st_size));
+ return tmp_number((AWKNUM) 0);
+ } else if (do_lint)
+ lintwarn("filename: called with no arguments");
+
+
+done:
+ /* Set the return value */
+ set_value(tmp_number((AWKNUM) ret));
+
+ /* Just to make the interpreter happy */
+ return tmp_number((AWKNUM) 0);
+}
+
+
+/* dlload --- load new builtins in this library */
+
+NODE *
+dlload(tree, dl)
+NODE *tree;
+void *dl;
+{
+ make_builtin("readfile", do_readfile, 1);
+
+ return tmp_number((AWKNUM) 0);
+}
diff --git a/extension/steps b/extension/steps
index 61a9e6eb..8bac5d85 100755
--- a/extension/steps
+++ b/extension/steps
@@ -1,9 +1,17 @@
# what to do under linux to make dl.so
# Tue Nov 24 15:04:14 EST 1998
+# Sun Aug 26 16:03:58 IDT 2001
+# Sun Apr 28 15:59:57 IDT 2002
gcc -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. dl.c
gcc -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. filefuncs.c
gcc -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. fork.c
+gcc -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. ordchr.c
+gcc -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. arrayparm.c
+gcc -shared -Wall -DHAVE_CONFIG_H -c -O -g -I.. readfile.c
ld -o dl.so -shared dl.o
ld -o filefuncs.so -shared filefuncs.o
ld -o fork.so -shared fork.o
+ld -o ordchr.so -shared ordchr.o
+ld -o arrayparm.so -shared arrayparm.o
+ld -o readfile.so -shared readfile.o
diff --git a/extension/testarrayparm.awk b/extension/testarrayparm.awk
new file mode 100644
index 00000000..08178f3e
--- /dev/null
+++ b/extension/testarrayparm.awk
@@ -0,0 +1,10 @@
+#! /bin/awk -f
+
+BEGIN {
+ extension("./arrayparm.so", "dlload")
+
+ mkarray(newvar, "hi", "hello")
+
+ for (i in newvar)
+ printf ("newvar[\"%s\"] = \"%s\"\n", i, newvar[i])
+}
diff --git a/extension/testordchr.awk b/extension/testordchr.awk
new file mode 100644
index 00000000..64e53d16
--- /dev/null
+++ b/extension/testordchr.awk
@@ -0,0 +1,6 @@
+BEGIN {
+ extension("./ordchr.so", "dlload")
+
+ print "ord(\"a\") is", ord("a")
+ print "chr(65) is", chr(65)
+}