diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2016-12-22 18:32:21 +0200 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2016-12-22 18:32:21 +0200 |
commit | 6402c9480ffcd8610b1322a381f0c14a42d95083 (patch) | |
tree | bbf1f2c00923c562b1ff346b5a6b759d58920902 /gawkapi.h | |
parent | 2af62b50e5b9574c0e169fb4d99c4912e56eef1f (diff) | |
parent | af31a6de1bd15d4d5f5520ebfcb1d24239b9a683 (diff) | |
download | egawk-6402c9480ffcd8610b1322a381f0c14a42d95083.tar.gz egawk-6402c9480ffcd8610b1322a381f0c14a42d95083.tar.bz2 egawk-6402c9480ffcd8610b1322a381f0c14a42d95083.zip |
Merge branch 'master' into feature/fix-comments
Diffstat (limited to 'gawkapi.h')
-rw-r--r-- | gawkapi.h | 111 |
1 files changed, 74 insertions, 37 deletions
@@ -306,6 +306,8 @@ typedef enum { AWK_UNDEFINED, AWK_NUMBER, AWK_STRING, + AWK_REGEX, + AWK_STRNUM, AWK_ARRAY, AWK_SCALAR, /* opaque access to a variable */ AWK_VALUE_COOKIE /* for updating a previously created value */ @@ -325,6 +327,8 @@ typedef struct awk_value { awk_value_cookie_t vc; } u; #define str_value u.s +#define strnum_value str_value +#define regex_value str_value #define num_value u.d #define array_cookie u.a #define scalar_cookie u.scl @@ -347,7 +351,7 @@ typedef struct awk_element { AWK_ELEMENT_DELETE = 1 /* set by extension if should be deleted */ } flags; - awk_value_t index; /* guaranteed to be a string! */ + awk_value_t index; awk_value_t value; } awk_element_t; @@ -469,6 +473,7 @@ typedef struct gawk_api { void (*api_fatal)(awk_ext_id_t id, const char *format, ...); void (*api_warning)(awk_ext_id_t id, const char *format, ...); void (*api_lintwarn)(awk_ext_id_t id, const char *format, ...); + void (*api_nonfatal)(awk_ext_id_t id, const char *format, ...); /* Functions to update ERRNO */ void (*api_update_ERRNO_int)(awk_ext_id_t id, int errno_val); @@ -493,27 +498,28 @@ typedef struct gawk_api { Table entry is type returned: - +-------------------------------------------------+ - | Type of Actual Value: | - +------------+------------+-----------+-----------+ - | String | Number | Array | Undefined | - +-----------+-----------+------------+------------+-----------+-----------+ - | | String | String | String | false | false | - | |-----------+------------+------------+-----------+-----------+ - | | Number | Number if | Number | false | false | - | | | can be | | | | - | | | converted, | | | | - | | | else false | | | | - | |-----------+------------+------------+-----------+-----------+ - | Type | Array | false | false | Array | false | - | Requested |-----------+------------+------------+-----------+-----------+ - | | Scalar | Scalar | Scalar | false | false | - | |-----------+------------+------------+-----------+-----------+ - | | Undefined | String | Number | Array | Undefined | - | |-----------+------------+------------+-----------+-----------+ - | | Value | false | false | false | false | - | | Cookie | | | | | - +-----------+-----------+------------+------------+-----------+-----------+ + +-------------------------------------------------------+ + | Type of Actual Value: | + +--------+--------+--------+--------+-------+-----------+ + | String | Strnum | Number | Regex | Array | Undefined | + +-----------+-----------+--------+--------+--------+--------+-------+-----------+ + | | String | String | String | String | String | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Strnum | false | Strnum | Strnum | false | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Number | Number | Number | Number | false | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Regex | false | false | false | Regex | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | Type | Array | false | false | false | false | Array | false | + | Requested +-----------+--------+--------+--------+--------+-------+-----------+ + | | Scalar | Scalar | Scalar | Scalar | Scalar | false | false | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Undefined | String | Strnum | Number | Regex | Array | Undefined | + | +-----------+--------+--------+--------+--------+-------+-----------+ + | | Value | false | false | false | false | false | false | + | | Cookie | | | | | | | + +-----------+-----------+--------+--------+--------+--------+-------+-----------+ */ /* Functions to handle parameters passed to the extension. */ @@ -680,10 +686,15 @@ typedef struct gawk_api { /* Clear out an array */ awk_bool_t (*api_clear_array)(awk_ext_id_t id, awk_array_t a_cookie); - /* Flatten out an array so that it can be looped over easily. */ - awk_bool_t (*api_flatten_array)(awk_ext_id_t id, + /* + * Flatten out an array with type conversions as requested. + * This supersedes the api_flatten_array function that did not allow + * the caller to specify the requested types. + */ + awk_bool_t (*api_flatten_array_typed)(awk_ext_id_t id, awk_array_t a_cookie, - awk_flat_array_t **data); + awk_flat_array_t **data, + awk_valtype_t index_type, awk_valtype_t value_type); /* When done, delete any marked elements, release the memory. */ awk_bool_t (*api_release_flattened_array)(awk_ext_id_t id, @@ -733,9 +744,6 @@ typedef struct gawk_api { const awk_input_buf_t **ibufp, const awk_output_buf_t **obufp); - /* Print nonfatal error message */ - void (*api_nonfatal)(awk_ext_id_t id, const char *format, ...); - } gawk_api_t; #ifndef GAWK /* these are not for the gawk code itself! */ @@ -802,8 +810,11 @@ typedef struct gawk_api { #define clear_array(array) (api->api_clear_array(ext_id, array)) +#define flatten_array_typed(array, data, index_type, value_type) \ + (api->api_flatten_array_typed(ext_id, array, data, index_type, value_type)) + #define flatten_array(array, data) \ - (api->api_flatten_array(ext_id, array, data)) + flatten_array_typed(array, data, AWK_STRING, AWK_UNDEFINED) #define release_flattened_array(array, data) \ (api->api_release_flattened_array(ext_id, array, data)) @@ -839,21 +850,22 @@ typedef struct gawk_api { /* Constructor functions */ -/* r_make_string --- make a string value in result from the passed-in string */ +/* r_make_string_type --- make a string or strnum or regexp value in result from the passed-in string */ static inline awk_value_t * -r_make_string(const gawk_api_t *api, /* needed for emalloc */ - awk_ext_id_t *ext_id, /* ditto */ - const char *string, - size_t length, - awk_bool_t duplicate, - awk_value_t *result) +r_make_string_type(const gawk_api_t *api, /* needed for emalloc */ + awk_ext_id_t *ext_id, /* ditto */ + const char *string, + size_t length, + awk_bool_t duplicate, + awk_value_t *result, + awk_valtype_t val_type) { char *cp = NULL; memset(result, 0, sizeof(*result)); - result->val_type = AWK_STRING; + result->val_type = val_type; result->str_value.len = length; if (duplicate) { @@ -868,9 +880,33 @@ r_make_string(const gawk_api_t *api, /* needed for emalloc */ return result; } +/* r_make_string --- make a string value in result from the passed-in string */ + +static inline awk_value_t * +r_make_string(const gawk_api_t *api, /* needed for emalloc */ + awk_ext_id_t *ext_id, /* ditto */ + const char *string, + size_t length, + awk_bool_t duplicate, + awk_value_t *result) +{ + return r_make_string_type(api, ext_id, string, length, duplicate, result, AWK_STRING); +} + #define make_const_string(str, len, result) r_make_string(api, ext_id, str, len, 1, result) #define make_malloced_string(str, len, result) r_make_string(api, ext_id, str, len, 0, result) +#define make_const_regex(str, len, result) r_make_string_type(api, ext_id, str, len, 1, result, AWK_REGEX) +#define make_malloced_regex(str, len, result) r_make_string_type(api, ext_id, str, len, 0, result, AWK_REGEX) + +/* + * Note: The caller may not create a Strnum, but it can create a string that is + * flagged as user input that MAY be a Strnum. Gawk will decide whether it's a + * Strnum or a String by checking whether the string is numeric. + */ +#define make_const_user_input(str, len, result) r_make_string_type(api, ext_id, str, len, 1, result, AWK_STRNUM) +#define make_malloced_user_input(str, len, result) r_make_string_type(api, ext_id, str, len, 0, result, AWK_STRNUM) + /* make_null_string --- make a null string value */ static inline awk_value_t * @@ -895,6 +931,7 @@ make_number(double num, awk_value_t *result) return result; } + /* * Each extension must define a function with this prototype: * |