diff options
Diffstat (limited to 'extension')
-rw-r--r-- | extension/ordchr2.c | 107 | ||||
-rw-r--r-- | extension/readfile2.c | 116 |
2 files changed, 223 insertions, 0 deletions
diff --git a/extension/ordchr2.c b/extension/ordchr2.c new file mode 100644 index 00000000..1f4a1fcb --- /dev/null +++ b/extension/ordchr2.c @@ -0,0 +1,107 @@ +/* + * ordchr.c - Builtin functions that provide ord() and chr() functions. + * + * Arnold Robbins + * arnold@skeeve.com + * 8/2001 + * Revised 6/2004 + */ + +/* + * Copyright (C) 2001, 2004, 2011 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 + */ + +#include <stdio.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sys/types.h> +#include <sys/stat.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; + +/* do_ord --- return numeric value of first char of string */ + +static int +do_ord(int nargs) +{ + const awk_value_t *str; + awk_value_t result; + double ret = -1; + + if (do_lint && nargs > 1) + lintwarn(ext_id, "ord: called with too many arguments"); + + str = get_curfunc_param(0, AWK_PARAM_STRING); + if (str != NULL) { + ret = str->str_value.str[0]; + } else if (do_lint) + lintwarn(ext_id, "ord: called with no arguments"); + + /* Set the return value */ + set_return_value(make_number(ret)); + return 1; +} + +/* do_chr --- turn numeric value into a string */ + +static int +do_chr(int nargs) +{ + const awk_value_t *num; + awk_value_t result; + unsigned int ret = 0; + double val = 0.0; + char str[2]; + + str[0] = str[1] = '\0'; + + if (do_lint && nargs > 1) + lintwarn(ext_id, "chr: called with too many arguments"); + + num = get_curfunc_param(0, AWK_PARAM_NUMBER); + if (num != NULL) { + val = num->num_value; + ret = val; /* convert to int */ + ret &= 0xff; + str[0] = ret; + str[1] = '\0'; + } else if (do_lint) + lintwarn(ext_id, "chr: called with no arguments"); + + /* Set the return value */ + set_return_value(dup_string(str, 1)); + return 1; +} + +static awk_ext_func_t func_table[] = { + { "ord", do_ord, 1 }, + { "chr", do_chr, 1 }, +}; + +/* define the dl_load function using the boilerplate macro */ + +dl_load_func(api, ext_id, func_table, ord_chr, "") diff --git a/extension/readfile2.c b/extension/readfile2.c new file mode 100644 index 00000000..4d8cd245 --- /dev/null +++ b/extension/readfile2.c @@ -0,0 +1,116 @@ +/* + * readfile.c - Read an entire file into a string. + * + * Arnold Robbins + * Tue Apr 23 17:43:30 IDT 2002 + * Revised per Peter Tillier + * Mon Jun 9 17:05:11 IDT 2003 + * Revised for new dynamic function facilities + * Mon Jun 14 14:53:07 IDT 2004 + */ + +/* + * Copyright (C) 2002, 2003, 2004, 2011 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 + */ + +#include <stdio.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include "gawkapi.h" + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +static const gawk_api_t *api; /* for convenience macros to work */ +static awk_ext_id_t *ext_id; + +int plugin_is_GPL_compatible; + +/* do_readfile --- read a file into memory */ + +static int +do_readfile(int nargs) +{ + const awk_value_t *filename; + awk_value_t result; + double ret = -1; + struct stat sbuf; + char *text; + int fd; + + if (do_lint && nargs > 1) + lintwarn(ext_id, "readfile: called with too many arguments"); + + filename = get_curfunc_param(0, AWK_PARAM_STRING); + if (filename != NULL) { + ret = stat(filename->str_value.str, & sbuf); + if (ret < 0) { + update_ERRNO_int(errno); + goto done; + } else if ((sbuf.st_mode & S_IFMT) != S_IFREG) { + errno = EINVAL; + ret = -1; + update_ERRNO_int(errno); + goto done; + } + + if ((fd = open(filename->str_value.str, O_RDONLY|O_BINARY)) < 0) { + ret = -1; + update_ERRNO_int(errno); + 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_int(errno); + goto done; + } + + close(fd); + set_return_value(make_string(text, sbuf.st_size)); + return 1; + } else if (do_lint) + lintwarn(ext_id, "readfile: called with no arguments"); + + +done: + /* Set the return value */ + set_return_value(make_number(ret)); + return 0; +} + +static awk_ext_func_t func_table[] = { + { "readfile", do_readfile, 1 }, +}; + +/* define the dl_load function using the boilerplate macro */ + +dl_load_func(api, ext_id, func_table, readfile, "") |