aboutsummaryrefslogtreecommitdiffstats
path: root/doc/gawk.texi
diff options
context:
space:
mode:
Diffstat (limited to 'doc/gawk.texi')
-rw-r--r--doc/gawk.texi111
1 files changed, 68 insertions, 43 deletions
diff --git a/doc/gawk.texi b/doc/gawk.texi
index a419a668..4998f81e 100644
--- a/doc/gawk.texi
+++ b/doc/gawk.texi
@@ -933,6 +933,7 @@ particular records in a file and perform operations upon them.
* Extension API Informational Variables:: Variables providing information about
@command{gawk}'s invocation.
* Extension API Boilerplate:: Boilerplate code for using the API.
+* Changes from API V1:: Changes from V1 of the API.
* Finding Extensions:: How @command{gawk} finds compiled
extensions.
* Extension Example:: Example C code for an extension.
@@ -32323,6 +32324,7 @@ This (rather large) @value{SECTION} describes the API in detail.
redirections.
* Extension API Variables:: Variables provided by the API.
* Extension API Boilerplate:: Boilerplate code for using the API.
+* Changes from API V1:: Changes from V1 of the API.
@end menu
@node Extension API Functions Introduction
@@ -32823,11 +32825,15 @@ or an underscore, which may be followed by any number of
letters, digits, and underscores.
Letter case in function names is significant.
-@item awk_value_t *(*const function)(int num_actual_args, awk_value_t *result, struct awk_ext_func *finfo);
+@item awk_value_t *(*const function)(int num_actual_args,
+@itemx @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ awk_value_t *result,
+@itemx @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ struct awk_ext_func *finfo);
This is a pointer to the C function that provides the extension's
functionality.
The function must fill in @code{*result} with either a number
-or a string. @command{gawk} takes ownership of any string memory.
+or a string.
+@c FIXME: Change to a scalar - number, string or regex once regex api stuff is merged.
+@command{gawk} takes ownership of any string memory.
As mentioned earlier, string memory @emph{must} come from one of
@code{gawk_malloc()}, @code{gawk_calloc()}, or @code{gawk_realloc()}.
@@ -32862,9 +32868,9 @@ to @code{awk_true}.
@item void *data;
This is an opaque pointer to any data that an extension function may
-wish to have available when called. Passing the @code{awk_ext_funct_t}
+wish to have available when called. Passing the @code{awk_ext_func_t}
structure to the extension function, and having this pointer available
-in it enables writing a single C or C++ function that implements multiple
+in it enable writing a single C or C++ function that implements multiple
@command{awk}-level extension functions.
@end table
@@ -32872,11 +32878,42 @@ Once you have a record representing your extension function, you register
it with @command{gawk} using this API function:
@table @code
-@item awk_bool_t add_ext_func(const char *namespace, const awk_ext_func_t *func);
+@item awk_bool_t add_ext_func(const char *namespace, awk_ext_func_t *func);
This function returns true upon success, false otherwise.
The @code{namespace} parameter is currently not used; you should pass in an
empty string (@code{""}). The @code{func} pointer is the address of a
@code{struct} representing your function, as just described.
+
+@command{gawk} does not modify what @code{func} points to, but the
+extension function itself receives this pointer and can modify what it
+points to, thus it is purposely not declared to be @code{const}.
+@end table
+
+The combination of @code{min_required_args}, @code{max_expected_args},
+and @code{suppress_lint} may be confusing. Here is how you should
+set things up.
+
+@table @asis
+@item Any number of arguments is valid
+Set @code{min_required_args} and @code{max_expected_args} to zero and
+set @code{suppress_lint} to @code{awk_true}.
+
+@item A minimum number of arguments is required, no limit on maximum number of arguments
+Set @code{min_required_args} to the minimum required. Set
+@code{max_expected_args} to zero and
+set @code{suppress_lint} to @code{awk_true}.
+
+@item A minium number of arguments is required, a maximum number is expected
+Set @code{min_required_args} to the minimum required. Set
+@code{max_expected_args} to the maximum expected.
+Set @code{suppress_lint} to @code{awk_false}.
+
+@item A minum number of arguments is required, and no more than a maximum is allowed
+Set @code{min_required_args} to the minimum required. Set
+@code{max_expected_args} to the maximum expected.
+Set @code{suppress_lint} to @code{awk_false}.
+In your extension function, check that @code{num_actual_args} does not
+exceed @code{f->max_expected_args}. If it does, issue a fatal error message.
@end table
@node Exit Callback Functions
@@ -33617,13 +33654,6 @@ An extension can look up the value of @command{gawk}'s special variables.
However, with the exception of the @code{PROCINFO} array, an extension
cannot change any of those variables.
-@quotation CAUTION
-It is possible for the lookup of @code{PROCINFO} to fail. This happens if
-the @command{awk} program being run does not reference @code{PROCINFO};
-in this case, @command{gawk} doesn't bother to create the array and
-populate it.
-@end quotation
-
@node Symbol table by cookie
@subsubsection Variable Access and Update by Cookie
@@ -34509,10 +34539,10 @@ debugging:
@float Table,gawk-api-version
@caption{gawk API version constants}
-@multitable @columnfractions .33 .33 .33
+@multitable {@b{API Version}} {@code{gawk_api_major_version}} {@code{GAWK_API_MAJOR_VERSION}}
@headitem API Version @tab C preprocessor define @tab enum constant
-@item Major @tab gawk_api_major_version @tab GAWK_API_MAJOR_VERSION
-@item Minor @tab gawk_api_minor_version @tab GAWK_API_MINOR_VERSION
+@item Major @tab @code{gawk_api_major_version} @tab @code{GAWK_API_MAJOR_VERSION}
+@item Minor @tab @code{gawk_api_minor_version} @tab @code{GAWK_API_MINOR_VERSION}
@end multitable
@end float
@@ -34531,10 +34561,10 @@ constant integers:
@table @code
@item api->major_version
-The major version of the running @command{gawk}
+The major version of the running @command{gawk}.
@item api->minor_version
-The minor version of the running @command{gawk}
+The minor version of the running @command{gawk}.
@end table
It is up to the extension to decide if there are API incompatibilities.
@@ -34607,7 +34637,7 @@ static awk_ext_id_t ext_id;
static const char *ext_version = NULL; /* or @dots{} = "some string" */
static awk_ext_func_t func_table[] = @{
- @{ "name", do_name, 1 @},
+ @{ "name", do_name, 1, 0, awk_false, NULL @},
/* @dots{} */
@};
@@ -34708,6 +34738,19 @@ If @code{ext_version} is not @code{NULL}, register
the version string with @command{gawk}.
@end enumerate
+
+@node Changes from API V1
+@subsection Changes From Version 1 of the API
+
+The current API is @emph{not} binary compatible with version 1 of the API.
+You will have to recompile your extensions in order to use them with
+the current version of @command{gawk}.
+
+Fortunately, at the possible expense of some compile-time warnings, the API remains
+source-code--compatible with the previous API. The major differences are
+the additional members in the @code{awk_ext_func_t} structure, and the
+addition of the third argument to the C implementation function.
+
@node Finding Extensions
@section How @command{gawk} Finds Extensions
@cindex extension search path
@@ -34948,17 +34991,12 @@ The second is a pointer to an @code{awk_value_t} structure, usually named
/* do_chdir --- provide dynamically loaded chdir() function for gawk */
static awk_value_t *
-do_chdir(int nargs, awk_value_t *result)
+do_chdir(int nargs, awk_value_t *result, struct awk_ext_func *unused)
@{
awk_value_t newdir;
int ret = -1;
assert(result != NULL);
-
- if (do_lint && nargs != 1)
- lintwarn(ext_id,
- _("chdir: called with incorrect number of arguments, "
- "expecting 1"));
@end example
The @code{newdir}
@@ -34967,8 +35005,8 @@ with @code{get_argument()}. Note that the first argument is
numbered zero.
If the argument is retrieved successfully, the function calls the
-@code{chdir()} system call. If the @code{chdir()} fails, @code{ERRNO}
-is updated:
+@code{chdir()} system call. Otherwise, if the @code{chdir()} fails,
+it updates @code{ERRNO}:
@example
if (get_argument(0, AWK_STRING, & newdir)) @{
@@ -35172,15 +35210,11 @@ is set to point to @code{stat()}, instead.
Here is the @code{do_stat()} function, which starts with
variable declarations and argument checking:
-@ignore
-Changed message for page breaking. Used to be:
- "stat: called with incorrect number of arguments (%d), should be 2",
-@end ignore
@example
/* do_stat --- provide a stat() function for gawk */
static awk_value_t *
-do_stat(int nargs, awk_value_t *result)
+do_stat(int nargs, awk_value_t *result, struct awk_ext_func *unused)
@{
awk_value_t file_param, array_param;
char *name;
@@ -35191,13 +35225,6 @@ do_stat(int nargs, awk_value_t *result)
int (*statfunc)(const char *path, struct stat *sbuf) = lstat;
assert(result != NULL);
-
- if (nargs != 2 && nargs != 3) @{
- if (do_lint)
- lintwarn(ext_id,
- _("stat: called with wrong number of arguments"));
- return make_number(-1, result);
- @}
@end example
Then comes the actual work. First, the function gets the arguments.
@@ -35265,11 +35292,9 @@ structures for loading each function into @command{gawk}:
@example
static awk_ext_func_t func_table[] = @{
- @{ "chdir", do_chdir, 1 @},
- @{ "stat", do_stat, 2 @},
-#ifndef __MINGW32__
- @{ "fts", do_fts, 3 @},
-#endif
+ @{ "chdir", do_chdir, 1, 1, awk_false, NULL @},
+ @{ "stat", do_stat, 3, 2, awk_false, NULL @},
+ @dots{}
@};
@end example