diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2017-03-06 09:20:33 -0500 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2017-03-06 09:20:33 -0500 |
commit | d6406b66add5652130385942a7e05ebc9ea799ce (patch) | |
tree | 4b7e1eedc5185b67d9eb78f5ecbe6e715037376f | |
parent | 62fe07b69e522c909aad303b31443cc3c9bdf6c0 (diff) | |
download | egawk-d6406b66add5652130385942a7e05ebc9ea799ce.tar.gz egawk-d6406b66add5652130385942a7e05ebc9ea799ce.tar.bz2 egawk-d6406b66add5652130385942a7e05ebc9ea799ce.zip |
Add a 6th argument to the API get_record function instead of having a separate field_width array pointer in the input buf.
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | gawkapi.h | 23 | ||||
-rw-r--r-- | io.c | 23 |
3 files changed, 41 insertions, 19 deletions
@@ -1,3 +1,17 @@ +2017-03-06 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * gawkapi.h (awk_input_buf_t): Remove field_width array and instead + add it as a 6th argument to the get_record function. This should + not break existing code, since it's fine to ignore the additional + argument. Document the behavior of the field_width argument. + * io.c (inrec): Pass pointer to field_width array to get_a_record, + and then hand it off to set_record. + (do_getline_redir): If not reading into a variable, pass pointer to + field_width array to get_a_record and then hand it off to set_record. + (do_getline): Ditto. + (get_a_record): Add a 4th field_width argument to pass through to + the API get_record method. + 2017-03-05 Andrew J. Schorr <aschorr@telemetry-investments.com> * awk.h (set_record): Add a new argument containing a field-width @@ -146,21 +146,24 @@ typedef struct awk_input { * than zero, gawk will automatically update the ERRNO variable based * on the value of *errcode (e.g., setting *errcode = errno should do * the right thing). - */ - int (*get_record)(char **out, struct awk_input *iobuf, int *errcode, - char **rt_start, size_t *rt_len); - - /* - * If this pointer is non-NULL, then this record should be parsed - * using the supplied field widths instead of the default gawk - * field parsing mechanism. The field_width array should have + * + * If field_width is non-NULL, then its value will be initialized + * to NULL, and the function may set it to point to an array of + * integers supplying field width information to override the default + * gawk field parsing mechanism. The field_width array should have * at least 2*NF+1 elements, and the value of field_width[2*NF] * must be negative. The first entry field_width[0] should contain * the number of bytes to skip before $1; field_width[1] contains * the number of bytes in $1. Note that these values are specified - * in bytes, not (potentially multi-byte) characters! + * in bytes, not (potentially multi-byte) characters! And note that this + * array will not be copied by gawk; it must persist at least until the + * next call to get_record or close_func. Note that field_width will + * be NULL when getline is assigning the results to a variable, thus + * field parsing is not needed. */ - const int *field_width; + int (*get_record)(char **out, struct awk_input *iobuf, int *errcode, + char **rt_start, size_t *rt_len, + const int **field_width); /* * No argument prototype on read_func to allow for older systems @@ -287,7 +287,7 @@ static RECVALUE rsrescan(IOBUF *iop, struct recmatch *recm, SCANSTATE *state); static RECVALUE (*matchrec)(IOBUF *iop, struct recmatch *recm, SCANSTATE *state) = rs1scan; -static int get_a_record(char **out, IOBUF *iop, int *errcode); +static int get_a_record(char **out, IOBUF *iop, int *errcode, const int **field_width); static void free_rp(struct redirect *rp); @@ -590,13 +590,14 @@ inrec(IOBUF *iop, int *errcode) char *begin; int cnt; bool retval = true; + const int *field_width = NULL; if (at_eof(iop) && no_data_left(iop)) cnt = EOF; else if ((iop->flag & IOP_CLOSED) != 0) cnt = EOF; else - cnt = get_a_record(& begin, iop, errcode); + cnt = get_a_record(& begin, iop, errcode, & field_width); /* Note that get_a_record may return -2 when I/O would block */ if (cnt < 0) { @@ -604,7 +605,7 @@ inrec(IOBUF *iop, int *errcode) } else { INCREMENT_REC(NR); INCREMENT_REC(FNR); - set_record(begin, cnt, iop->public.field_width); + set_record(begin, cnt, field_width); if (*errcode > 0) retval = false; } @@ -2618,6 +2619,7 @@ do_getline_redir(int into_variable, enum redirval redirtype) NODE *redir_exp = NULL; NODE **lhs = NULL; int redir_error = 0; + const int *field_width = NULL; if (into_variable) lhs = POP_ADDRESS(); @@ -2646,7 +2648,7 @@ do_getline_redir(int into_variable, enum redirval redirtype) return make_number((AWKNUM) 0.0); errcode = 0; - cnt = get_a_record(& s, iop, & errcode); + cnt = get_a_record(& s, iop, & errcode, (lhs ? NULL : & field_width)); if (errcode != 0) { if (! do_traditional && (errcode != -1)) update_ERRNO_int(errcode); @@ -2668,7 +2670,7 @@ do_getline_redir(int into_variable, enum redirval redirtype) } if (lhs == NULL) /* no optional var. */ - set_record(s, cnt, iop->public.field_width); + set_record(s, cnt, field_width); else { /* assignment to variable */ unref(*lhs); *lhs = make_string(s, cnt); @@ -2686,6 +2688,7 @@ do_getline(int into_variable, IOBUF *iop) int cnt = EOF; char *s = NULL; int errcode; + const int *field_width = NULL; if (iop == NULL) { /* end of input */ if (into_variable) @@ -2694,7 +2697,7 @@ do_getline(int into_variable, IOBUF *iop) } errcode = 0; - cnt = get_a_record(& s, iop, & errcode); + cnt = get_a_record(& s, iop, & errcode, (into_variable ? NULL : & field_width)); if (errcode != 0) { if (! do_traditional && (errcode != -1)) update_ERRNO_int(errcode); @@ -2709,7 +2712,7 @@ do_getline(int into_variable, IOBUF *iop) INCREMENT_REC(FNR); if (! into_variable) /* no optional var. */ - set_record(s, cnt, iop->public.field_width); + set_record(s, cnt, field_width); else { /* assignment to variable */ NODE **lhs; lhs = POP_ADDRESS(); @@ -3653,7 +3656,8 @@ errno_io_retry(void) static int get_a_record(char **out, /* pointer to pointer to data */ IOBUF *iop, /* input IOP */ - int *errcode) /* pointer to error variable */ + int *errcode, /* pointer to error variable */ + const int **field_width)/* pointer to pointer to field_width array */ { struct recmatch recm; SCANSTATE state; @@ -3672,7 +3676,8 @@ get_a_record(char **out, /* pointer to pointer to data */ char *rt_start; size_t rt_len; int rc = iop->public.get_record(out, &iop->public, errcode, - &rt_start, &rt_len); + &rt_start, &rt_len, + field_width); if (rc == EOF) iop->flag |= IOP_AT_EOF; else { |