#include #include #include #include #include #include #include #include #include #include "gawkapi.h" #define __namespace__ "valtype" #define DEBUG 0 int plugin_is_GPL_compatible; static const gawk_api_t *api; static awk_ext_id_t ext_id; static const char *ext_version = "0.1"; static awk_value_t * do_test(int nargs, awk_value_t *result, struct awk_ext_func *finfo); static awk_ext_func_t func_table[] = { { "test", do_test, 2, 1, awk_false, NULL }, }; __attribute__((unused)) static awk_bool_t (*init_func)(void) = NULL; int dl_load(const gawk_api_t *api_p, void *id) { api = api_p; ext_id = (awk_ext_id_t) &id; int errors = 0; long unsigned int i; if (api->major_version < 3) { fprintf(stderr, "incompatible api version: %d.%d != %d.%d (extension/gawk version)\n", GAWK_API_MAJOR_VERSION, GAWK_API_MINOR_VERSION, api->major_version, api->minor_version); exit(1); } for (i=0; i < sizeof(func_table) / sizeof(awk_ext_func_t); i++) { if (! add_ext_func(__namespace__, & func_table[i])) { fprintf(stderr, "can't add extension function <%s>\n", func_table[0].name); errors++; } } if (ext_version != NULL) { register_ext_version(ext_version); } return (errors == 0); } const char * VT[] = { "AWK_UNDEFINED", "AWK_NUMBER", "AWK_STRING", "AWK_REGEX", "AWK_STRNUM", "AWK_ARRAY", "AWK_SCALAR", "AWK_VALUE_COOKIE", "AWK_BOOL" }; int get_valtype(char *s) { int i; for (i=0; i < 9; i++) if (0 == strcmp(s, VT[i])) return i; return -1; } static awk_value_t* do_test(int nargs, awk_value_t *result, __attribute__((unused)) struct awk_ext_func *finfo) { /* * Test the $nargs[0] valtype. */ assert(result != NULL); make_number(1.0, result); awk_value_t var_name, type_name; awk_valtype_t wanted; int ret; if (nargs == 2) { if (! get_argument(1, AWK_STRING, & type_name)) fatal(ext_id, "2nd arg: wrong type"); if (0 == type_name.str_value.len) fatal(ext_id, "2nd arg: empty"); if (-1 == (ret = get_valtype(type_name.str_value.str))) fatal(ext_id, "2nd arg: unknown valtype <%s>", type_name.str_value.str); wanted = (awk_valtype_t) ret; } else { wanted = AWK_UNDEFINED; } if (DEBUG) fprintf(stderr, "set wanted to %s (%d)\n", VT[wanted], wanted); ret = get_argument(0, wanted, & var_name); printf("get_argument returns <%d> (requested: %s | got: %s)\n", ret, VT[wanted], VT[var_name.val_type]); return result; }