diff options
Diffstat (limited to 'extension')
-rw-r--r-- | extension/ChangeLog | 9 | ||||
-rw-r--r-- | extension/Makefile.am | 5 | ||||
-rw-r--r-- | extension/Makefile.in | 19 | ||||
-rw-r--r-- | extension/configh.in | 6 | ||||
-rwxr-xr-x | extension/configure | 6 | ||||
-rw-r--r-- | extension/configure.ac | 6 | ||||
-rw-r--r-- | extension/select.c | 183 |
7 files changed, 228 insertions, 6 deletions
diff --git a/extension/ChangeLog b/extension/ChangeLog index 04159df8..37cfccf2 100644 --- a/extension/ChangeLog +++ b/extension/ChangeLog @@ -1,3 +1,12 @@ +2013-06-30 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * Makefile.am (pkgextension_LTLIBRARIES): Add select.la. + (select_la_SOURCES, select_la_LDFLAGS, select_la_LIBADD): Build new + select extension. + * configure.ac (AC_CHECK_HEADERS): Add signal.h. + (AC_CHECK_FUNCS): Add sigaction. + * select.c: Implement the new select extension. + 2013-06-10 Arnold D. Robbins <arnold@skeeve.com> * configure.ac (AC_HEADER_MAJOR): New macro added. diff --git a/extension/Makefile.am b/extension/Makefile.am index 7b52b14b..58fc3df6 100644 --- a/extension/Makefile.am +++ b/extension/Makefile.am @@ -42,6 +42,7 @@ pkgextension_LTLIBRARIES = \ revoutput.la \ revtwoway.la \ rwarray.la \ + select.la \ testext.la \ time.la @@ -70,6 +71,10 @@ ordchr_la_SOURCES = ordchr.c ordchr_la_LDFLAGS = $(MY_MODULE_FLAGS) ordchr_la_LIBADD = $(MY_LIBS) +select_la_SOURCES = select.c +select_la_LDFLAGS = $(MY_MODULE_FLAGS) +select_la_LIBADD = $(MY_LIBS) + readdir_la_SOURCES = readdir.c gawkdirfd.h readdir_la_LDFLAGS = $(MY_MODULE_FLAGS) readdir_la_LIBADD = $(MY_LIBS) diff --git a/extension/Makefile.in b/extension/Makefile.in index 8d0a2869..97ee41fe 100644 --- a/extension/Makefile.in +++ b/extension/Makefile.in @@ -203,6 +203,12 @@ rwarray_la_OBJECTS = $(am_rwarray_la_OBJECTS) rwarray_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(rwarray_la_LDFLAGS) $(LDFLAGS) -o $@ +select_la_DEPENDENCIES = $(am__DEPENDENCIES_2) +am_select_la_OBJECTS = select.lo +select_la_OBJECTS = $(am_select_la_OBJECTS) +select_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(select_la_LDFLAGS) $(LDFLAGS) -o $@ testext_la_DEPENDENCIES = $(am__DEPENDENCIES_2) am_testext_la_OBJECTS = testext.lo testext_la_OBJECTS = $(am_testext_la_OBJECTS) @@ -253,12 +259,14 @@ SOURCES = $(filefuncs_la_SOURCES) $(fnmatch_la_SOURCES) \ $(fork_la_SOURCES) $(inplace_la_SOURCES) $(ordchr_la_SOURCES) \ $(readdir_la_SOURCES) $(readfile_la_SOURCES) \ $(revoutput_la_SOURCES) $(revtwoway_la_SOURCES) \ - $(rwarray_la_SOURCES) $(testext_la_SOURCES) $(time_la_SOURCES) + $(rwarray_la_SOURCES) $(select_la_SOURCES) \ + $(testext_la_SOURCES) $(time_la_SOURCES) DIST_SOURCES = $(filefuncs_la_SOURCES) $(fnmatch_la_SOURCES) \ $(fork_la_SOURCES) $(inplace_la_SOURCES) $(ordchr_la_SOURCES) \ $(readdir_la_SOURCES) $(readfile_la_SOURCES) \ $(revoutput_la_SOURCES) $(revtwoway_la_SOURCES) \ - $(rwarray_la_SOURCES) $(testext_la_SOURCES) $(time_la_SOURCES) + $(rwarray_la_SOURCES) $(select_la_SOURCES) \ + $(testext_la_SOURCES) $(time_la_SOURCES) RECURSIVE_TARGETS = all-recursive check-recursive cscopelist-recursive \ ctags-recursive dvi-recursive html-recursive info-recursive \ install-data-recursive install-dvi-recursive \ @@ -497,6 +505,7 @@ pkgextension_LTLIBRARIES = \ revoutput.la \ revtwoway.la \ rwarray.la \ + select.la \ testext.la \ time.la @@ -520,6 +529,9 @@ inplace_la_LIBADD = $(MY_LIBS) ordchr_la_SOURCES = ordchr.c ordchr_la_LDFLAGS = $(MY_MODULE_FLAGS) ordchr_la_LIBADD = $(MY_LIBS) +select_la_SOURCES = select.c +select_la_LDFLAGS = $(MY_MODULE_FLAGS) +select_la_LIBADD = $(MY_LIBS) readdir_la_SOURCES = readdir.c gawkdirfd.h readdir_la_LDFLAGS = $(MY_MODULE_FLAGS) readdir_la_LIBADD = $(MY_LIBS) @@ -664,6 +676,8 @@ revtwoway.la: $(revtwoway_la_OBJECTS) $(revtwoway_la_DEPENDENCIES) $(EXTRA_revtw $(AM_V_CCLD)$(revtwoway_la_LINK) -rpath $(pkgextensiondir) $(revtwoway_la_OBJECTS) $(revtwoway_la_LIBADD) $(LIBS) rwarray.la: $(rwarray_la_OBJECTS) $(rwarray_la_DEPENDENCIES) $(EXTRA_rwarray_la_DEPENDENCIES) $(AM_V_CCLD)$(rwarray_la_LINK) -rpath $(pkgextensiondir) $(rwarray_la_OBJECTS) $(rwarray_la_LIBADD) $(LIBS) +select.la: $(select_la_OBJECTS) $(select_la_DEPENDENCIES) $(EXTRA_select_la_DEPENDENCIES) + $(AM_V_CCLD)$(select_la_LINK) -rpath $(pkgextensiondir) $(select_la_OBJECTS) $(select_la_LIBADD) $(LIBS) testext.la: $(testext_la_OBJECTS) $(testext_la_DEPENDENCIES) $(EXTRA_testext_la_DEPENDENCIES) $(AM_V_CCLD)$(testext_la_LINK) -rpath $(pkgextensiondir) $(testext_la_OBJECTS) $(testext_la_LIBADD) $(LIBS) time.la: $(time_la_OBJECTS) $(time_la_DEPENDENCIES) $(EXTRA_time_la_DEPENDENCIES) @@ -686,6 +700,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/revoutput.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/revtwoway.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rwarray.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/select.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stack.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/testext.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Plo@am__quote@ diff --git a/extension/configh.in b/extension/configh.in index 8571844b..a7212dc1 100644 --- a/extension/configh.in +++ b/extension/configh.in @@ -78,6 +78,12 @@ /* Define to 1 if you have the `select' function. */ #undef HAVE_SELECT +/* Define to 1 if you have the `sigaction' function. */ +#undef HAVE_SIGACTION + +/* Define to 1 if you have the <signal.h> header file. */ +#undef HAVE_SIGNAL_H + /* Define to 1 if you have the <stdint.h> header file. */ #undef HAVE_STDINT_H diff --git a/extension/configure b/extension/configure index f59548c1..02ff3dc8 100755 --- a/extension/configure +++ b/extension/configure @@ -14002,7 +14002,8 @@ fi fi fi -for ac_header in dirent.h fnmatch.h limits.h time.h sys/time.h sys/select.h sys/param.h +for ac_header in dirent.h fnmatch.h limits.h time.h sys/time.h sys/select.h \ + sys/param.h signal.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" @@ -14017,7 +14018,8 @@ done for ac_func in fdopendir fnmatch gettimeofday \ - getdtablesize nanosleep select GetSystemTimeAsFileTime + getdtablesize nanosleep select sigaction \ + GetSystemTimeAsFileTime do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/extension/configure.ac b/extension/configure.ac index d819ebfa..5b95f966 100644 --- a/extension/configure.ac +++ b/extension/configure.ac @@ -67,10 +67,12 @@ else fi AC_HEADER_MAJOR -AC_CHECK_HEADERS(dirent.h fnmatch.h limits.h time.h sys/time.h sys/select.h sys/param.h) +AC_CHECK_HEADERS(dirent.h fnmatch.h limits.h time.h sys/time.h sys/select.h \ + sys/param.h signal.h) AC_CHECK_FUNCS(fdopendir fnmatch gettimeofday \ - getdtablesize nanosleep select GetSystemTimeAsFileTime) + getdtablesize nanosleep select sigaction \ + GetSystemTimeAsFileTime) GAWK_FUNC_DIRFD GAWK_PREREQ_DIRFD diff --git a/extension/select.c b/extension/select.c new file mode 100644 index 00000000..f74ae250 --- /dev/null +++ b/extension/select.c @@ -0,0 +1,183 @@ +/* + * select.c - Builtin functions to provide select I/O multiplexing. + */ + +/* + * Copyright (C) 2013 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#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/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 = "ordchr extension: version 1.0"; +static awk_bool_t (*init_func)(void) = NULL; + +int plugin_is_GPL_compatible; + +#if defined(HAVE_SELECT) && defined(HAVE_SYS_SELECT_H) +#include <sys/select.h> +#endif + +#ifdef HAVE_SIGNAL_H +#include <signal.h> +#endif + +/* do_ord --- return numeric value of first char of string */ + +static awk_value_t * +do_select(int nargs, awk_value_t *result) +{ + static const char *argname[] = { "read", "write", "except" }; + struct { + awk_value_t array; + awk_flat_array_t *flat; + fd_set bits; + int *array2fd; + } fds[3]; + awk_value_t timeout_arg; + int i; + struct timeval maxwait; + struct timeval *timeout; + int nfds = 0; + int rc; + + if (do_lint && nargs > 5) + lintwarn(ext_id, _("select: called with too many arguments")); + +#define EL fds[i].flat->elements[j] + for (i = 0; i < sizeof(fds)/sizeof(fds[0]); i++) { + size_t j; + + if (! get_argument(i, AWK_ARRAY, & fds[i].array)) { + warning(ext_id, _("select: bad array parameter `%s'"), argname[i]); + update_ERRNO_string(_("select: bad array parameter")); + return make_number(-1, result); + } + /* N.B. flatten_array fails for empty arrays, so that's OK */ + FD_ZERO(&fds[i].bits); + if (flatten_array(fds[i].array.array_cookie, &fds[i].flat)) { + emalloc(fds[i].array2fd, int *, fds[i].flat->count*sizeof(int), "select"); + for (j = 0; j < fds[i].flat->count; j++) { + fds[i].array2fd[j] = -1; + switch (EL.index.val_type) { + case AWK_NUMBER: + if (EL.index.num_value >= 0) + fds[i].array2fd[j] = EL.index.num_value; + if (fds[i].array2fd[j] != EL.index.num_value) + fds[i].array2fd[j] = -1; + break; + case AWK_STRING: + if (EL.value.val_type == AWK_STRING) { + const awk_input_buf_t *buf; + if ((buf = get_file(EL.index.str_value.str, EL.index.str_value.len, EL.value.str_value.str, EL.value.str_value.len)) != NULL) + fds[i].array2fd[j] = buf->fd; + } + break; + } + if (fds[i].array2fd[j] < 0) { + warning(ext_id, _("select: get_file failed")); + update_ERRNO_string(_("select: get_file failed")); + if (! release_flattened_array(fds[i].array.array_cookie, fds[i].flat)) + warning(ext_id, _("select: release_flattened_array failed")); + free(fds[i].array2fd); + return make_number(-1, result); + } + FD_SET(fds[i].array2fd[j], &fds[i].bits); + if (nfds <= fds[i].array2fd[j]) + nfds = fds[i].array2fd[j]+1; + } + } + else + fds[i].flat = NULL; + } + if (get_argument(3, AWK_NUMBER, &timeout_arg)) { + double secs = timeout_arg.num_value; + if (secs < 0) { + warning(ext_id, _("select: treating negative timeout as zero")); + secs = 0; + } + maxwait.tv_sec = secs; + maxwait.tv_usec = (secs-(double)maxwait.tv_sec)*1000000.0; + timeout = &maxwait; + } else + timeout = NULL; + rc = select(nfds, &fds[0].bits, &fds[1].bits, &fds[2].bits, timeout); + + if (rc < 0) { + update_ERRNO_int(errno); + /* bit masks are undefined, so delete all array entries */ + for (i = 0; i < sizeof(fds)/sizeof(fds[0]); i++) { + if (fds[i].flat) { + size_t j; + for (j = 0; j < fds[i].flat->count; j++) + EL.flags |= AWK_ELEMENT_DELETE; + if (! release_flattened_array(fds[i].array.array_cookie, fds[i].flat)) + warning(ext_id, _("select: release_flattened_array failed")); + free(fds[i].array2fd); + } + } + return make_number(rc, result); + } + + for (i = 0; i < sizeof(fds)/sizeof(fds[0]); i++) { + if (fds[i].flat) { + size_t j; + /* remove array elements not set in the bit mask */ + for (j = 0; j < fds[i].flat->count; j++) { + if (! FD_ISSET(fds[i].array2fd[j], &fds[i].bits)) + EL.flags |= AWK_ELEMENT_DELETE; + } + if (! release_flattened_array(fds[i].array.array_cookie, fds[i].flat)) + warning(ext_id, _("select: release_flattened_array failed")); + free(fds[i].array2fd); + } + } +#undef EL + + /* Set the return value */ + return make_number(rc, result); +} + +static awk_ext_func_t func_table[] = { + { "select", do_select, 5 }, +}; + +/* define the dl_load function using the boilerplate macro */ + +dl_load_func(func_table, select, "") |