/* * header.c - read header line */ /* * Copyright (C) 2012 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 #endif #include #include #include #include #include #include #include #include "gawkapi.h" #include "gettext.h" #define _(msgid) gettext(msgid) #define N_(msgid) msgid static awk_bool_t init_header(); static const gawk_api_t *api; /* for convenience macros to work */ static awk_ext_id_t *ext_id; static const char *ext_version = "create_variable 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 awk_value_t * do_create_variable(int nargs, awk_value_t *result) { int i; awk_value_t var_name; awk_value_t var_value; assert(result != NULL); make_number(0.0, result); if (nargs != 1 && nargs != 2) { fatal(ext_id,"create_variable: wrong number of arguments (%d)"\ " - should be 1 or 2",nargs); } /* look up variable whose name is passed in, should pass */ if (!get_argument(0, AWK_STRING, &var_name)) { fatal(ext_id,"create_variable: failed to get variable name " \ "(1st parameter)"); } if (nargs==2) { get_argument(1, AWK_UNDEFINED, &var_value); if (var_name.val_type==AWK_UNDEFINED) { fatal(ext_id,"create_variable: failed to get variable value " \ "(2nd parameter)"); } } else { make_number(0.0, &var_value); } /* update the variable */ if (!sym_update(var_name.str_value.str, &var_value)) { fatal(ext_id,"create_variable: failed to create/updated"\ " variable (%s)", var_name.str_value); } make_number(1.0, result); return result; } static awk_value_t * do_print_variable(int nargs, awk_value_t *result) { int i; awk_value_t var_name; awk_value_t var_value; assert(result != NULL); make_number(0.0, result); if (nargs != 1) fatal(ext_id,"print_variable: wrong number of arguments (%d)"\ " - should be 1",nargs); /* look up variable whose name is passed in, should pass */ if (!get_argument(0, AWK_STRING, &var_name)) { fatal(ext_id,"print_variable: failed to get variable name " \ "(1st parameter)"); } if (!sym_lookup(var_name.str_value.str,AWK_STRING, &var_value)) { fatal(ext_id,"print_variable: failed to get variable (%s) " \ " as a string", var_name.str_value.str); } fprintf(stderr,"variable(%s) = '%s'\n", var_name.str_value.str, var_value.str_value.str); make_number(1.0, result); return result; } static awk_ext_func_t func_table[] = { { "create_variable", do_create_variable, 2 }, { "print_variable", do_print_variable, 1 } }; /* define the dl_load function using the boilerplate macro */ dl_load_func(func_table, header, "");