gawkapi.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * gawkapi.h -- Definitions for use by extension functions calling into gawk.
  3. */
  4. /*
  5. * Copyright (C) 2012-2015 the Free Software Foundation, Inc.
  6. *
  7. * This file is part of GAWK, the GNU implementation of the
  8. * AWK Programming Language.
  9. *
  10. * GAWK is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GAWK is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /*
  25. * The following types and/or macros and/or functions are referenced
  26. * in this file. For correct use, you must therefore include the
  27. * corresponding standard header file BEFORE including this file.
  28. *
  29. * FILE - <stdio.h>
  30. * NULL - <stddef.h>
  31. * memset(), memcpy() - <string.h>
  32. * size_t - <sys/types.h>
  33. * struct stat - <sys/stat.h>
  34. *
  35. * Due to portability concerns, especially to systems that are not
  36. * fully standards-compliant, it is your responsibility to include
  37. * the correct files in the correct way. This requirement is necessary
  38. * in order to keep this file clean, instead of becoming a portability
  39. * hodge-podge as can be seen in the gawk source code.
  40. *
  41. * To pass reasonable integer values for ERRNO, you will also need to
  42. * include <errno.h>.
  43. */
  44. #ifndef _GAWK_API_H
  45. #define _GAWK_API_H
  46. /*
  47. * General introduction:
  48. *
  49. * This API purposely restricts itself to ISO C 90 features. In particular, no
  50. * bool, no // comments, no use of the restrict keyword, or anything else,
  51. * in order to provide maximal portability.
  52. *
  53. * Exception: the "inline" keyword is used below in the "constructor"
  54. * functions. If your compiler doesn't support it, you should either
  55. * -Dinline='' on your command line, or use the autotools and include a
  56. * config.h in your extensions.
  57. *
  58. * Additional important information:
  59. *
  60. * 1. ALL string values in awk_value_t objects need to come from api_malloc().
  61. * Gawk will handle releasing the storage if necessary. This is slightly
  62. * awkward, in that you can't take an awk_value_t that you got from gawk
  63. * and reuse it directly, even for something that is conceptually pass
  64. * by value.
  65. *
  66. * 2. Due to gawk internals, after using sym_update() to install an array
  67. * into gawk, you have to retrieve the array cookie from the value
  68. * passed in to sym_update(). Like so:
  69. *
  70. * new_array = create_array();
  71. * val.val_type = AWK_ARRAY;
  72. * val.array_cookie = new_array;
  73. * sym_update("array", & val); // install array in the symbol table
  74. *
  75. * new_array = val.array_cookie; // MUST DO THIS
  76. *
  77. * // fill in new array with lots of subscripts and values
  78. *
  79. * Similarly, if installing a new array as a subarray of an existing
  80. * array, you must add the new array to its parent before adding any
  81. * elements to it.
  82. *
  83. * You must also retrieve the value of the array_cookie after the call
  84. * to set_element().
  85. *
  86. * Thus, the correct way to build an array is to work "top down".
  87. * Create the array, and immediately install it in gawk's symbol table
  88. * using sym_update(), or install it as an element in a previously
  89. * existing array using set_element().
  90. *
  91. * Thus the new array must ultimately be rooted in a global symbol. This is
  92. * necessary before installing any subarrays in it, due to gawk's
  93. * internal implementation. Strictly speaking, this is required only
  94. * for arrays that will have subarrays as elements; however it is
  95. * a good idea to always do this. This restriction may be relaxed
  96. * in a subsequent revision of the API.
  97. */
  98. /* Allow use in C++ code. */
  99. #ifdef __cplusplus
  100. extern "C" {
  101. #endif
  102. /* This is used to keep extensions from modifying certain fields in some structs. */
  103. #ifdef GAWK
  104. #define awk_const
  105. #else
  106. #define awk_const const
  107. #endif
  108. typedef enum awk_bool {
  109. awk_false = 0,
  110. awk_true
  111. } awk_bool_t; /* we don't use <stdbool.h> on purpose */
  112. /* The information about input files that input parsers need to know: */
  113. typedef struct awk_input {
  114. const char *name; /* filename */
  115. int fd; /* file descriptor */
  116. #define INVALID_HANDLE (-1)
  117. void *opaque; /* private data for input parsers */
  118. /*
  119. * The get_record function is called to read the next record of data.
  120. *
  121. * It should return the length of the input record or EOF, and it
  122. * should set *out to point to the contents of $0. The rt_start
  123. * and rt_len arguments should be used to return RT to gawk.
  124. * If EOF is not returned, the parser must set *rt_len (and
  125. * *rt_start if *rt_len is non-zero).
  126. *
  127. * Note that gawk will make a copy of the record in *out, so the
  128. * parser is responsible for managing its own memory buffer.
  129. * Similarly, gawk will make its own copy of RT, so the parser
  130. * is also responsible for managing this memory.
  131. *
  132. * It is guaranteed that errcode is a valid pointer, so there is
  133. * no need to test for a NULL value. Gawk sets *errcode to 0,
  134. * so there is no need to set it unless an error occurs.
  135. *
  136. * If an error does occur, the function should return EOF and set
  137. * *errcode to a positive value. In that case, if *errcode is greater
  138. * than zero, gawk will automatically update the ERRNO variable based
  139. * on the value of *errcode (e.g., setting *errcode = errno should do
  140. * the right thing).
  141. */
  142. int (*get_record)(char **out, struct awk_input *iobuf, int *errcode,
  143. char **rt_start, size_t *rt_len);
  144. /*
  145. * No argument prototype on read_func to allow for older systems
  146. * whose headers are not up to date.
  147. */
  148. ssize_t (*read_func)();
  149. /*
  150. * The close_func is called to allow the parser to free private data.
  151. * Gawk itself will close the fd unless close_func first sets it to
  152. * INVALID_HANDLE.
  153. */
  154. void (*close_func)(struct awk_input *iobuf);
  155. /* put last, for alignment. bleah */
  156. struct stat sbuf; /* stat buf */
  157. } awk_input_buf_t;
  158. typedef struct awk_input_parser {
  159. const char *name; /* name of parser */
  160. /*
  161. * The can_take_file function should return non-zero if the parser
  162. * would like to parse this file. It should not change any gawk
  163. * state!
  164. */
  165. awk_bool_t (*can_take_file)(const awk_input_buf_t *iobuf);
  166. /*
  167. * If this parser is selected, then take_control_of will be called.
  168. * It can assume that a previous call to can_take_file was successful,
  169. * and no gawk state has changed since that call. It should populate
  170. * the awk_input_buf_t's get_record, close_func, and opaque values as needed.
  171. * It should return non-zero if successful.
  172. */
  173. awk_bool_t (*take_control_of)(awk_input_buf_t *iobuf);
  174. awk_const struct awk_input_parser *awk_const next; /* for use by gawk */
  175. } awk_input_parser_t;
  176. /*
  177. * Similar for output wrapper.
  178. */
  179. /* First the data structure */
  180. typedef struct awk_output_buf {
  181. const char *name; /* name of output file */
  182. const char *mode; /* mode argument to fopen */
  183. FILE *fp; /* stdio file pointer */
  184. awk_bool_t redirected; /* true if a wrapper is active */
  185. void *opaque; /* for use by output wrapper */
  186. /*
  187. * Replacement functions for I/O. Just like the regular
  188. * versions but also take the opaque pointer argument.
  189. */
  190. size_t (*gawk_fwrite)(const void *buf, size_t size, size_t count,
  191. FILE *fp, void *opaque);
  192. int (*gawk_fflush)(FILE *fp, void *opaque);
  193. int (*gawk_ferror)(FILE *fp, void *opaque);
  194. int (*gawk_fclose)(FILE *fp, void *opaque);
  195. } awk_output_buf_t;
  196. /* Next the output wrapper registered with gawk */
  197. typedef struct awk_output_wrapper {
  198. const char *name; /* name of the wrapper */
  199. /*
  200. * The can_take_file function should return non-zero if the wrapper
  201. * would like to process this file. It should not change any gawk
  202. * state!
  203. */
  204. awk_bool_t (*can_take_file)(const awk_output_buf_t *outbuf);
  205. /*
  206. * If this wrapper is selected, then take_control_of will be called.
  207. * It can assume that a previous call to can_take_file was successful,
  208. * and no gawk state has changed since that call. It should populate
  209. * the awk_output_buf_t function pointers and opaque pointer as needed.
  210. * It should return non-zero if successful.
  211. */
  212. awk_bool_t (*take_control_of)(awk_output_buf_t *outbuf);
  213. awk_const struct awk_output_wrapper *awk_const next; /* for use by gawk */
  214. } awk_output_wrapper_t;
  215. /* A two-way processor combines an input parser and an output wrapper. */
  216. typedef struct awk_two_way_processor {
  217. const char *name; /* name of the two-way processor */
  218. /*
  219. * The can_take_file function should return non-zero if the two-way
  220. * processor would like to parse this file. It should not change
  221. * any gawk state!
  222. */
  223. awk_bool_t (*can_take_two_way)(const char *name);
  224. /*
  225. * If this processor is selected, then take_control_of will be called.
  226. * It can assume that a previous call to can_take_file was successful,
  227. * and no gawk state has changed since that call. It should populate
  228. * the awk_input_buf_t and awk_otuput_buf_t structures as needed.
  229. * It should return non-zero if successful.
  230. */
  231. awk_bool_t (*take_control_of)(const char *name, awk_input_buf_t *inbuf,
  232. awk_output_buf_t *outbuf);
  233. awk_const struct awk_two_way_processor *awk_const next; /* for use by gawk */
  234. } awk_two_way_processor_t;
  235. /* Current version of the API. */
  236. enum {
  237. GAWK_API_MAJOR_VERSION = 1,
  238. GAWK_API_MINOR_VERSION = 1
  239. };
  240. /* A number of typedefs related to different types of values. */
  241. /*
  242. * A mutable string. Gawk owns the memory pointed to if it supplied
  243. * the value. Otherwise, it takes ownership of the memory pointed to.
  244. *
  245. * The API deals exclusively with regular chars; these strings may
  246. * be multibyte encoded in the current locale's encoding and character
  247. * set. Gawk will convert internally to wide characters if necessary.
  248. */
  249. typedef struct awk_string {
  250. char *str; /* data */
  251. size_t len; /* length thereof, in chars */
  252. } awk_string_t;
  253. /* Arrays are represented as an opaque type. */
  254. typedef void *awk_array_t;
  255. /* Scalars can be represented as an opaque type. */
  256. typedef void *awk_scalar_t;
  257. /* Any value can be stored as a cookie. */
  258. typedef void *awk_value_cookie_t;
  259. /*
  260. * This tag defines the type of a value.
  261. *
  262. * Values are associated with regular variables and with array elements.
  263. * Since arrays can be multidimensional (as can regular variables)
  264. * it's valid to have a "value" that is actually an array.
  265. */
  266. typedef enum {
  267. AWK_UNDEFINED,
  268. AWK_NUMBER,
  269. AWK_STRING,
  270. AWK_ARRAY,
  271. AWK_SCALAR, /* opaque access to a variable */
  272. AWK_VALUE_COOKIE /* for updating a previously created value */
  273. } awk_valtype_t;
  274. /*
  275. * An awk value. The val_type tag indicates what
  276. * is in the union.
  277. */
  278. typedef struct awk_value {
  279. awk_valtype_t val_type;
  280. union {
  281. awk_string_t s;
  282. double d;
  283. awk_array_t a;
  284. awk_scalar_t scl;
  285. awk_value_cookie_t vc;
  286. } u;
  287. #define str_value u.s
  288. #define num_value u.d
  289. #define array_cookie u.a
  290. #define scalar_cookie u.scl
  291. #define value_cookie u.vc
  292. } awk_value_t;
  293. /*
  294. * A "flattened" array element. Gawk produces an array of these
  295. * inside the awk_flat_array_t.
  296. * ALL memory pointed to belongs to gawk. Individual elements may
  297. * be marked for deletion. New elements must be added individually,
  298. * one at a time, using the separate API for that purpose.
  299. */
  300. typedef struct awk_element {
  301. /* convenience linked list pointer, not used by gawk */
  302. struct awk_element *next;
  303. enum {
  304. AWK_ELEMENT_DEFAULT = 0, /* set by gawk */
  305. AWK_ELEMENT_DELETE = 1 /* set by extension if
  306. should be deleted */
  307. } flags;
  308. awk_value_t index; /* guaranteed to be a string! */
  309. awk_value_t value;
  310. } awk_element_t;
  311. /*
  312. * A "flattened" array. See the description above for how
  313. * to use the elements contained herein.
  314. */
  315. typedef struct awk_flat_array {
  316. awk_const void *awk_const opaque1; /* private data for use by gawk */
  317. awk_const void *awk_const opaque2; /* private data for use by gawk */
  318. awk_const size_t count; /* how many elements */
  319. awk_element_t elements[1]; /* will be extended */
  320. } awk_flat_array_t;
  321. /*
  322. * A record describing an extension function. Upon being
  323. * loaded, the extension should pass in one of these to gawk for
  324. * each C function.
  325. *
  326. * Each called function must fill in the result with either a number
  327. * or string. Gawk takes ownership of any string memory.
  328. *
  329. * The called function must return the value of `result'.
  330. * This is for the convenience of the calling code inside gawk.
  331. *
  332. * Each extension function may decide what to do if the number of
  333. * arguments isn't what it expected. Following awk functions, it
  334. * is likely OK to ignore extra arguments.
  335. */
  336. typedef struct awk_ext_func {
  337. const char *name;
  338. awk_value_t *(*function)(int num_actual_args, awk_value_t *result);
  339. size_t num_expected_args;
  340. } awk_ext_func_t;
  341. typedef void *awk_ext_id_t; /* opaque type for extension id */
  342. /*
  343. * The API into gawk. Lots of functions here. We hope that they are
  344. * logically organized.
  345. */
  346. typedef struct gawk_api {
  347. /* First, data fields. */
  348. /* These are what gawk thinks the API version is. */
  349. awk_const int major_version;
  350. awk_const int minor_version;
  351. /*
  352. * These can change on the fly as things happen within gawk.
  353. * Currently only do_lint is prone to change, but we reserve
  354. * the right to allow the others to do so also.
  355. */
  356. #define DO_FLAGS_SIZE 6
  357. awk_const int do_flags[DO_FLAGS_SIZE];
  358. /* Use these as indices into do_flags[] array to check the values */
  359. #define gawk_do_lint 0
  360. #define gawk_do_traditional 1
  361. #define gawk_do_profile 2
  362. #define gawk_do_sandbox 3
  363. #define gawk_do_debug 4
  364. #define gawk_do_mpfr 5
  365. /* Next, registration functions: */
  366. /* Add a function to the interpreter, returns true upon success */
  367. awk_bool_t (*api_add_ext_func)(awk_ext_id_t id, const char *namespace,
  368. const awk_ext_func_t *func);
  369. /* Register an input parser; for opening files read-only */
  370. void (*api_register_input_parser)(awk_ext_id_t id,
  371. awk_input_parser_t *input_parser);
  372. /* Register an output wrapper, for writing files */
  373. void (*api_register_output_wrapper)(awk_ext_id_t id,
  374. awk_output_wrapper_t *output_wrapper);
  375. /* Register a processor for two way I/O */
  376. void (*api_register_two_way_processor)(awk_ext_id_t id,
  377. awk_two_way_processor_t *two_way_processor);
  378. /*
  379. * Add an exit call back.
  380. *
  381. * arg0 is a private data pointer for use by the extension;
  382. * gawk saves it and passes it into the function pointed
  383. * to by funcp at exit.
  384. *
  385. * Exit callback functions are called in LIFO order.
  386. */
  387. void (*api_awk_atexit)(awk_ext_id_t id,
  388. void (*funcp)(void *data, int exit_status),
  389. void *arg0);
  390. /* Register a version string for this extension with gawk. */
  391. void (*api_register_ext_version)(awk_ext_id_t id, const char *version);
  392. /* Functions to print messages */
  393. void (*api_fatal)(awk_ext_id_t id, const char *format, ...);
  394. void (*api_warning)(awk_ext_id_t id, const char *format, ...);
  395. void (*api_lintwarn)(awk_ext_id_t id, const char *format, ...);
  396. /* Functions to update ERRNO */
  397. void (*api_update_ERRNO_int)(awk_ext_id_t id, int errno_val);
  398. void (*api_update_ERRNO_string)(awk_ext_id_t id, const char *string);
  399. void (*api_unset_ERRNO)(awk_ext_id_t id);
  400. /*
  401. * All of the functions that return a value from inside gawk
  402. * (get a parameter, get a global variable, get an array element)
  403. * behave in the same way.
  404. *
  405. * For a function parameter, the return is false if the argument
  406. * count is out of range, or if actual paramater does not match
  407. * what is specified in wanted. In that case, result->val_type
  408. * will hold the actual type of what was passed.
  409. *
  410. * Similarly for symbol table access to variables and array elements,
  411. * the return is false if the actual variable or array element does
  412. * not match what was requested, and the result->val_type will hold
  413. * the actual type.
  414. Table entry is type returned:
  415. +-------------------------------------------------+
  416. | Type of Actual Value: |
  417. +------------+------------+-----------+-----------+
  418. | String | Number | Array | Undefined |
  419. +-----------+-----------+------------+------------+-----------+-----------+
  420. | | String | String | String | false | false |
  421. | |-----------+------------+------------+-----------+-----------+
  422. | | Number | Number if | Number | false | false |
  423. | | | can be | | | |
  424. | | | converted, | | | |
  425. | | | else false | | | |
  426. | |-----------+------------+------------+-----------+-----------+
  427. | Type | Array | false | false | Array | false |
  428. | Requested |-----------+------------+------------+-----------+-----------+
  429. | | Scalar | Scalar | Scalar | false | false |
  430. | |-----------+------------+------------+-----------+-----------+
  431. | | Undefined | String | Number | Array | Undefined |
  432. | |-----------+------------+------------+-----------+-----------+
  433. | | Value | false | false | false | false |
  434. | | Cookie | | | | |
  435. +-----------+-----------+------------+------------+-----------+-----------+
  436. */
  437. /* Functions to handle parameters passed to the extension. */
  438. /*
  439. * Get the count'th paramater, zero-based.
  440. * Returns false if count is out of range, or if actual paramater
  441. * does not match what is specified in wanted. In that case,
  442. * result->val_type is as described above.
  443. */
  444. awk_bool_t (*api_get_argument)(awk_ext_id_t id, size_t count,
  445. awk_valtype_t wanted,
  446. awk_value_t *result);
  447. /*
  448. * Convert a paramter that was undefined into an array
  449. * (provide call-by-reference for arrays). Returns false
  450. * if count is too big, or if the argument's type is
  451. * not undefined.
  452. */
  453. awk_bool_t (*api_set_argument)(awk_ext_id_t id,
  454. size_t count,
  455. awk_array_t array);
  456. /*
  457. * Symbol table access:
  458. * - Read-only access to special variables (NF, etc.)
  459. * - One special exception: PROCINFO.
  460. * - Use sym_update() to change a value, including from UNDEFINED
  461. * to scalar or array.
  462. */
  463. /*
  464. * Lookup a variable, fill in value. No messing with the value
  465. * returned.
  466. * Returns false if the variable doesn't exist* or if the wrong type
  467. * was requested. In the latter case, vaule->val_type will have
  468. * the real type, as described above.
  469. *
  470. * awk_value_t val;
  471. * if (! api->sym_lookup(id, name, wanted, & val))
  472. * error_code_here();
  473. * else {
  474. * // safe to use val
  475. * }
  476. */
  477. awk_bool_t (*api_sym_lookup)(awk_ext_id_t id,
  478. const char *name,
  479. awk_valtype_t wanted,
  480. awk_value_t *result);
  481. /*
  482. * Update a value. Adds it to the symbol table if not there.
  483. * Changing types (scalar <--> array) is not allowed.
  484. * In fact, using this to update an array is not allowed, either.
  485. * Such an attempt returns false.
  486. */
  487. awk_bool_t (*api_sym_update)(awk_ext_id_t id,
  488. const char *name,
  489. awk_value_t *value);
  490. /*
  491. * A ``scalar cookie'' is an opaque handle that provide access
  492. * to a global variable or array. It is an optimization that
  493. * avoids looking up variables in gawk's symbol table every time
  494. * access is needed.
  495. *
  496. * This function retrieves the current value of a scalar cookie.
  497. * Once you have obtained a scalar_cookie using sym_lookup, you can
  498. * use this function to get its value more efficiently.
  499. *
  500. * Return will be false if the value cannot be retrieved.
  501. *
  502. * Flow is thus
  503. * awk_value_t val;
  504. * awk_scalar_t cookie;
  505. * api->sym_lookup(id, "variable", AWK_SCALAR, & val); // get the cookie
  506. * cookie = val.scalar_cookie;
  507. * ...
  508. * api->sym_lookup_scalar(id, cookie, wanted, & val); // get the value
  509. */
  510. awk_bool_t (*api_sym_lookup_scalar)(awk_ext_id_t id,
  511. awk_scalar_t cookie,
  512. awk_valtype_t wanted,
  513. awk_value_t *result);
  514. /*
  515. * Update the value associated with a scalar cookie.
  516. * Flow is
  517. * sym_lookup with wanted == AWK_SCALAR
  518. * if returns false
  519. * sym_update with real initial value to install it
  520. * sym_lookup again with AWK_SCALAR
  521. * else
  522. * use the scalar cookie
  523. *
  524. * Return will be false if the new value is not one of
  525. * AWK_STRING or AWK_NUMBER.
  526. *
  527. * Here too, the built-in variables may not be updated.
  528. */
  529. awk_bool_t (*api_sym_update_scalar)(awk_ext_id_t id,
  530. awk_scalar_t cookie, awk_value_t *value);
  531. /* Cached values */
  532. /*
  533. * Create a cached string or numeric value for efficient later
  534. * assignment. This improves performance when you want to assign
  535. * the same value to one or more variables repeatedly. Only
  536. * AWK_NUMBER and AWK_STRING values are allowed. Any other type
  537. * is rejected. We disallow AWK_UNDEFINED since that case would
  538. * result in inferior performance.
  539. */
  540. awk_bool_t (*api_create_value)(awk_ext_id_t id, awk_value_t *value,
  541. awk_value_cookie_t *result);
  542. /*
  543. * Release the memory associated with a cookie from api_create_value.
  544. * Please call this to free memory when the value is no longer needed.
  545. */
  546. awk_bool_t (*api_release_value)(awk_ext_id_t id, awk_value_cookie_t vc);
  547. /* Array management */
  548. /*
  549. * Retrieve total number of elements in array.
  550. * Returns false if some kind of error.
  551. */
  552. awk_bool_t (*api_get_element_count)(awk_ext_id_t id,
  553. awk_array_t a_cookie, size_t *count);
  554. /*
  555. * Return the value of an element - read only!
  556. * Use set_array_element() to change it.
  557. * Behavior for value and return is same as for api_get_argument
  558. * and sym_lookup.
  559. */
  560. awk_bool_t (*api_get_array_element)(awk_ext_id_t id,
  561. awk_array_t a_cookie,
  562. const awk_value_t *const index,
  563. awk_valtype_t wanted,
  564. awk_value_t *result);
  565. /*
  566. * Change (or create) element in existing array with
  567. * index and value.
  568. *
  569. * ARGV and ENVIRON may not be updated.
  570. */
  571. awk_bool_t (*api_set_array_element)(awk_ext_id_t id, awk_array_t a_cookie,
  572. const awk_value_t *const index,
  573. const awk_value_t *const value);
  574. /*
  575. * Remove the element with the given index.
  576. * Returns success if removed or false if element did not exist.
  577. */
  578. awk_bool_t (*api_del_array_element)(awk_ext_id_t id,
  579. awk_array_t a_cookie, const awk_value_t* const index);
  580. /* Create a new array cookie to which elements may be added */
  581. awk_array_t (*api_create_array)(awk_ext_id_t id);
  582. /* Clear out an array */
  583. awk_bool_t (*api_clear_array)(awk_ext_id_t id, awk_array_t a_cookie);
  584. /* Flatten out an array so that it can be looped over easily. */
  585. awk_bool_t (*api_flatten_array)(awk_ext_id_t id,
  586. awk_array_t a_cookie,
  587. awk_flat_array_t **data);
  588. /* When done, delete any marked elements, release the memory. */
  589. awk_bool_t (*api_release_flattened_array)(awk_ext_id_t id,
  590. awk_array_t a_cookie,
  591. awk_flat_array_t *data);
  592. /*
  593. * Hooks to provide access to gawk's memory allocation functions.
  594. * This ensures that memory passed between gawk and the extension
  595. * is allocated and released by the same library.
  596. */
  597. void *(*api_malloc)(size_t size);
  598. void *(*api_calloc)(size_t nmemb, size_t size);
  599. void *(*api_realloc)(void *ptr, size_t size);
  600. void (*api_free)(void *ptr);
  601. } gawk_api_t;
  602. #ifndef GAWK /* these are not for the gawk code itself! */
  603. /*
  604. * Use these if you want to define "global" variables named api
  605. * and ext_id to make the code a little easier to read.
  606. * See the sample boilerplate code, below.
  607. */
  608. #define do_lint (api->do_flags[gawk_do_lint])
  609. #define do_traditional (api->do_flags[gawk_do_traditional])
  610. #define do_profile (api->do_flags[gawk_do_profile])
  611. #define do_sandbox (api->do_flags[gawk_do_sandbox])
  612. #define do_debug (api->do_flags[gawk_do_debug])
  613. #define do_mpfr (api->do_flags[gawk_do_mpfr])
  614. #define get_argument(count, wanted, result) \
  615. (api->api_get_argument(ext_id, count, wanted, result))
  616. #define set_argument(count, new_array) \
  617. (api->api_set_argument(ext_id, count, new_array))
  618. #define fatal api->api_fatal
  619. #define warning api->api_warning
  620. #define lintwarn api->api_lintwarn
  621. #define register_input_parser(parser) (api->api_register_input_parser(ext_id, parser))
  622. #define register_output_wrapper(wrapper) (api->api_register_output_wrapper(ext_id, wrapper))
  623. #define register_two_way_processor(processor) \
  624. (api->api_register_two_way_processor(ext_id, processor))
  625. #define update_ERRNO_int(e) (api->api_update_ERRNO_int(ext_id, e))
  626. #define update_ERRNO_string(str) \
  627. (api->api_update_ERRNO_string(ext_id, str))
  628. #define unset_ERRNO() (api->api_unset_ERRNO(ext_id))
  629. #define add_ext_func(ns, func) (api->api_add_ext_func(ext_id, ns, func))
  630. #define awk_atexit(funcp, arg0) (api->api_awk_atexit(ext_id, funcp, arg0))
  631. #define sym_lookup(name, wanted, result) \
  632. (api->api_sym_lookup(ext_id, name, wanted, result))
  633. #define sym_lookup_scalar(scalar_cookie, wanted, result) \
  634. (api->api_sym_lookup_scalar(ext_id, scalar_cookie, wanted, result))
  635. #define sym_update(name, value) \
  636. (api->api_sym_update(ext_id, name, value))
  637. #define sym_update_scalar(scalar_cookie, value) \
  638. (api->api_sym_update_scalar)(ext_id, scalar_cookie, value)
  639. #define get_array_element(array, index, wanted, result) \
  640. (api->api_get_array_element(ext_id, array, index, wanted, result))
  641. #define set_array_element(array, index, value) \
  642. (api->api_set_array_element(ext_id, array, index, value))
  643. #define set_array_element_by_elem(array, elem) \
  644. (api->api_set_array_element(ext_id, array, & (elem)->index, & (elem)->value))
  645. #define del_array_element(array, index) \
  646. (api->api_del_array_element(ext_id, array, index))
  647. #define get_element_count(array, count_p) \
  648. (api->api_get_element_count(ext_id, array, count_p))
  649. #define create_array() (api->api_create_array(ext_id))
  650. #define clear_array(array) (api->api_clear_array(ext_id, array))
  651. #define flatten_array(array, data) \
  652. (api->api_flatten_array(ext_id, array, data))
  653. #define release_flattened_array(array, data) \
  654. (api->api_release_flattened_array(ext_id, array, data))
  655. #define gawk_malloc(size) (api->api_malloc(size))
  656. #define gawk_calloc(nmemb, size) (api->api_calloc(nmemb, size))
  657. #define gawk_realloc(ptr, size) (api->api_realloc(ptr, size))
  658. #define gawk_free(ptr) (api->api_free(ptr))
  659. #define create_value(value, result) \
  660. (api->api_create_value(ext_id, value,result))
  661. #define release_value(value) \
  662. (api->api_release_value(ext_id, value))
  663. #define register_ext_version(version) \
  664. (api->api_register_ext_version(ext_id, version))
  665. #define emalloc(pointer, type, size, message) \
  666. do { \
  667. if ((pointer = (type) gawk_malloc(size)) == 0) \
  668. fatal(ext_id, "%s: malloc of %d bytes failed\n", message, size); \
  669. } while(0)
  670. #define erealloc(pointer, type, size, message) \
  671. do { \
  672. if ((pointer = (type) gawk_realloc(pointer, size)) == 0) \
  673. fatal(ext_id, "%s: realloc of %d bytes failed\n", message, size); \
  674. } while(0)
  675. /* Constructor functions */
  676. /* r_make_string --- make a string value in result from the passed-in string */
  677. static inline awk_value_t *
  678. r_make_string(const gawk_api_t *api, /* needed for emalloc */
  679. awk_ext_id_t *ext_id, /* ditto */
  680. const char *string,
  681. size_t length,
  682. awk_bool_t duplicate,
  683. awk_value_t *result)
  684. {
  685. char *cp = NULL;
  686. memset(result, 0, sizeof(*result));
  687. result->val_type = AWK_STRING;
  688. result->str_value.len = length;
  689. if (duplicate) {
  690. emalloc(cp, char *, length + 2, "r_make_string");
  691. memcpy(cp, string, length);
  692. cp[length] = '\0';
  693. result->str_value.str = cp;
  694. } else {
  695. result->str_value.str = (char *) string;
  696. }
  697. return result;
  698. }
  699. #define make_const_string(str, len, result) r_make_string(api, ext_id, str, len, 1, result)
  700. #define make_malloced_string(str, len, result) r_make_string(api, ext_id, str, len, 0, result)
  701. /* make_null_string --- make a null string value */
  702. static inline awk_value_t *
  703. make_null_string(awk_value_t *result)
  704. {
  705. memset(result, 0, sizeof(*result));
  706. result->val_type = AWK_UNDEFINED;
  707. return result;
  708. }
  709. /* make_number --- make a number value in result */
  710. static inline awk_value_t *
  711. make_number(double num, awk_value_t *result)
  712. {
  713. memset(result, 0, sizeof(*result));
  714. result->val_type = AWK_NUMBER;
  715. result->num_value = num;
  716. return result;
  717. }
  718. /*
  719. * Each extension must define a function with this prototype:
  720. *
  721. * int dl_load(gawk_api_t *api_p, awk_ext_id_t id)
  722. *
  723. * The return value should be zero on failure and non-zero on success.
  724. *
  725. * For the macros to work, the function should save api_p in a global
  726. * variable named 'api' and save id in a global variable named 'ext_id'.
  727. * In addition, a global function pointer named 'init_func' should be
  728. * defined and set to either NULL or an initialization function that
  729. * returns non-zero on success and zero upon failure.
  730. */
  731. extern int dl_load(const gawk_api_t *const api_p, awk_ext_id_t id);
  732. #if 0
  733. /* Boilerplate code: */
  734. int plugin_is_GPL_compatible;
  735. static gawk_api_t *const api;
  736. static awk_ext_id_t ext_id;
  737. static const char *ext_version = NULL; /* or ... = "some string" */
  738. static awk_ext_func_t func_table[] = {
  739. { "name", do_name, 1 },
  740. /* ... */
  741. };
  742. /* EITHER: */
  743. static awk_bool_t (*init_func)(void) = NULL;
  744. /* OR: */
  745. static awk_bool_t
  746. init_my_extension(void)
  747. {
  748. ...
  749. }
  750. static awk_bool_t (*init_func)(void) = init_my_extension;
  751. dl_load_func(func_table, some_name, "name_space_in_quotes")
  752. #endif
  753. #define dl_load_func(func_table, extension, name_space) \
  754. int dl_load(const gawk_api_t *const api_p, awk_ext_id_t id) \
  755. { \
  756. size_t i, j; \
  757. int errors = 0; \
  758. \
  759. api = api_p; \
  760. ext_id = id; \
  761. \
  762. if (api->major_version != GAWK_API_MAJOR_VERSION \
  763. || api->minor_version < GAWK_API_MINOR_VERSION) { \
  764. fprintf(stderr, #extension ": version mismatch with gawk!\n"); \
  765. fprintf(stderr, "\tmy version (%d, %d), gawk version (%d, %d)\n", \
  766. GAWK_API_MAJOR_VERSION, GAWK_API_MINOR_VERSION, \
  767. api->major_version, api->minor_version); \
  768. exit(1); \
  769. } \
  770. \
  771. /* load functions */ \
  772. for (i = 0, j = sizeof(func_table) / sizeof(func_table[0]); i < j; i++) { \
  773. if (func_table[i].name == NULL) \
  774. break; \
  775. if (! add_ext_func(name_space, & func_table[i])) { \
  776. warning(ext_id, #extension ": could not add %s\n", \
  777. func_table[i].name); \
  778. errors++; \
  779. } \
  780. } \
  781. \
  782. if (init_func != NULL) { \
  783. if (! init_func()) { \
  784. warning(ext_id, #extension ": initialization function failed\n"); \
  785. errors++; \
  786. } \
  787. } \
  788. \
  789. if (ext_version != NULL) \
  790. register_ext_version(ext_version); \
  791. \
  792. return (errors == 0); \
  793. }
  794. #endif /* GAWK */
  795. #ifdef __cplusplus
  796. }
  797. #endif /* C++ */
  798. #endif /* _GAWK_API_H */