diff options
Diffstat (limited to 'extension/ordchr.c')
-rw-r--r-- | extension/ordchr.c | 101 |
1 files changed, 64 insertions, 37 deletions
diff --git a/extension/ordchr.c b/extension/ordchr.c index efbc6d56..7e3eda5e 100644 --- a/extension/ordchr.c +++ b/extension/ordchr.c @@ -5,10 +5,11 @@ * arnold@skeeve.com * 8/2001 * Revised 6/2004 + * Revised 5/2012 */ /* - * 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. @@ -28,71 +29,97 @@ * 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 <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; /* do_ord --- return numeric value of first char of string */ -static NODE * -do_ord(int nargs) +static awk_value_t * +do_ord(int nargs, awk_value_t *result) { - NODE *str; - int ret = -1; + awk_value_t str; + double ret = -1; - if (do_lint && get_curfunc_arg_count() > 1) - lintwarn("ord: called with too many arguments"); + assert(result != NULL); - str = get_scalar_argument(0, FALSE); - if (str != NULL) { - (void) force_string(str); - ret = str->stptr[0]; - } else if (do_lint) - lintwarn("ord: called with no arguments"); + if (do_lint && nargs > 1) + lintwarn(ext_id, _("ord: called with too many arguments")); + if (get_argument(0, AWK_STRING, & str)) { + ret = str.str_value.str[0]; + } else if (do_lint) { + if (nargs == 0) + lintwarn(ext_id, _("ord: called with no arguments")); + else + lintwarn(ext_id, _("ord: called with inappropriate argument(s)")); + } /* Set the return value */ - return make_number((AWKNUM) ret); + return make_number(ret, result); } /* do_chr --- turn numeric value into a string */ -static NODE * -do_chr(int nargs) +static awk_value_t * +do_chr(int nargs, awk_value_t *result) { - NODE *num; + awk_value_t num; unsigned int ret = 0; - AWKNUM val = 0.0; + double val = 0.0; char str[2]; str[0] = str[1] = '\0'; - if (do_lint && get_curfunc_arg_count() > 1) - lintwarn("chr: called with too many arguments"); + assert(result != NULL); + + if (do_lint && nargs > 1) + lintwarn(ext_id, _("chr: called with too many arguments")); - num = get_scalar_argument(0, FALSE); - if (num != NULL) { - val = force_number(num); + if (get_argument(0, AWK_NUMBER, & num)) { + val = num.num_value; ret = val; /* convert to int */ ret &= 0xff; str[0] = ret; str[1] = '\0'; - } else if (do_lint) - lintwarn("chr: called with no arguments"); + } else if (do_lint) { + if (nargs == 0) + lintwarn(ext_id, _("chr: called with no arguments")); + else + lintwarn(ext_id, _("chr: called with inappropriate argument(s)")); + } /* Set the return value */ - return make_string(str, 1); + return make_const_string(str, 1, result); } -/* dlload --- load new builtins in this library */ +static awk_ext_func_t func_table[] = { + { "ord", do_ord, 1 }, + { "chr", do_chr, 1 }, +}; -NODE * -dlload(tree, dl) -NODE *tree; -void *dl; -{ - make_builtin("ord", do_ord, 1); - make_builtin("chr", do_chr, 1); +/* define the dl_load function using the boilerplate macro */ - return make_number((AWKNUM) 0); -} +dl_load_func(func_table, ord_chr, "") |