Extension API Functions Introduction (The GNU Awk User’s Guide)
Access to facilities within
gawk is achieved by calling through function pointers passed into your extension.
API function pointers are provided for the following kinds of operations:
- Allocating, reallocating, and releasing memory.
- Registration functions. You may register:
- Printing fatal, warning, and “lint” warning messages.
ERRNO, or unsetting it.
- Accessing parameters, including converting an undefined parameter into an array.
- Symbol table access: retrieving a global variable, creating one, or changing one.
- Creating and releasing cached values; this provides an efficient way to use values for multiple variables and can be a big performance win.
- Manipulating arrays:
- Accessing and manipulating redirections.
Some points about using the API:
- The following types, macros, and/or functions are referenced in
gawkapi.h. For correct use, you must therefore include the corresponding standard header file before including
gawkapi.h. The list of macros and related header files is shown in Table 17.1.
C entityHeader file EOF Values for errno FILE NULL memcpy() memset() size_t struct stat Table 17.1: Standard header files needed by API
- If your extension uses MPFR facilities, and you wish to receive such values from
gawkand/or pass such values to it, you must include the
<mpfr.h>header before including
gawkapi.hfile may be included more than once without ill effect. Doing so, however, is poor coding practice.
- Although the API only uses ISO C 90 features, there is an exception; the “constructor” functions use the
inlinekeyword. If your compiler does not support this keyword, you should either place ‘
-Dinline=’ on your command line or use the GNU Autotools and include a
config.hfile in your extensions.
- All pointers filled in by
gawkpoint to memory managed by
gawkand should be treated by the extension as read-only. Memory for all strings passed into
gawkfrom the extension must come from calling one of
gawk_realloc(), and is managed by
gawkfrom then on.
- The API defines several simple
structs that map values as seen from
awk. A value can be a
double, a string, or an array (as in multidimensional arrays, or when creating a new array). String values maintain both pointer and length, because embedded NUL characters are allowed. NOTE: By intent, gawk maintains strings using the current multibyte encoding (as defined by LC_xxx environment variables) and not using wide characters. This matches how gawk stores strings internally and also how characters are likely to be input into and output from files. NOTE: String values passed to an extension by gawk are always NUL-terminated. Thus it is safe to pass such string values to standard library and system routines. However, because gawk allows embedded NUL characters in string data, before using the data as a regular C string, you should check that the length for that string passed to the extension matches the return value of strlen() for it.
- When retrieving a value (such as a parameter or that of a global variable or array element), the extension requests a specific type (number, string, scalar, value cookie, array, or “undefined”). When the request is “undefined,” the returned value will have the real underlying type. However, if the request and actual type don’t match, the access function returns “false” and fills in the type of the actual value that is there, so that the extension can, e.g., print an error message (such as “scalar passed where array expected”).
You may call the API functions by using the function pointers directly, but the interface is not so pretty. To make extension code look more like regular code, the
gawkapi.h header file defines several macros that you should use in your code. This section presents the macros as if they were functions.