21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix C: Implementation Notes 291And the function will also probably want to set the IOBUF close_func methodto be called when the file is closed to clean up any state associated with theinput.Finally, hook functions should be prepared to receive an IOBUF structure wherethe fd field is set to INVALID_HANDLE, meaning that gawk was not able to openthe file itself. In this case, the hook function must be able to successfully openthe file and place a valid file descriptor there.Currently, for example, the hook function facility is used to implement the XMLparser shared library extension. For more info, please look in ‘awk.h’ and in‘io.c’.Caution: This function is new as of gawk 3.1.5.An argument that is supposed to be an array needs to be handled with some extra code,in case the array being passed in is actually from a function parameter.In versions of gawk up to and including 3.1.2, the following boilerplate code shows howto do this:NODE *the_arg;the_arg = get_argument(tree, 2); /* assume need 3rd arg, 0-based *//* if a parameter, get it off the stack */if (the_arg->type == Node_param_list)the_arg = stack_ptr[the_arg->param_cnt];/* parameter referenced an array, get it */if (the_arg->type == Node_array_ref)the_arg = the_arg->orig_array;/* check type */if (the_arg->type != Node_var && the_arg->type != Node_var_array)fatal("newfunc: third argument is not an array");/* force it to be an array, if necessary, clear it */the_arg->type = Node_var_array;assoc_clear(the_arg);For versions 3.1.3 and later, the internals changed. In particular, the interface wasactually simplified drastically. The following boilerplate code now suffices:NODE *the_arg;the_arg = get_argument(tree, 2); /* assume need 3rd arg, 0-based *//* force it to be an array: */the_arg = get_array(the_arg);/* if necessary, clear it: */assoc_clear(the_arg);As of version 3.1.4, the internals improved again, and became even simpler:NODE *the_arg;the_arg = get_array_argument(tree, 2, FALSE); /* assume need 3rd arg, 0-based */Again, you should spend time studying the gawk internals; don’t just blindly copy thiscode.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!