From f264a8f1838e2178f3507101e93f151a21a832bc Mon Sep 17 00:00:00 2001 From: CismonX Date: Thu, 24 Dec 2020 18:21:03 +0800 Subject: [PATCH] Rename field. --- examples/sixdraw.c | 36 +++++++++++++++--------------- man/ctlseqs_reader_config.3 | 22 +++++++++---------- src/ctlseqs.c | 6 ++--- src/ctlseqs.h | 2 +- tests/tcsgrep.c | 44 ++++++++++++++++++------------------- 5 files changed, 54 insertions(+), 56 deletions(-) diff --git a/examples/sixdraw.c b/examples/sixdraw.c index 45b4430..12a9891 100644 --- a/examples/sixdraw.c +++ b/examples/sixdraw.c @@ -49,7 +49,7 @@ struct sixdraw_ctx { struct termios termios; - union ctlseqs_value buffer[64]; + union ctlseqs_value result[64]; char const *prog_name; struct ctlseqs_matcher *matcher; struct ctlseqs_reader *reader; @@ -141,17 +141,17 @@ sixdraw_get_winsize(struct sixdraw_ctx *ctx) static bool sixdraw_decrqm(struct sixdraw_ctx *ctx, unsigned mode_id, char const *name) { - ssize_t result; - union ctlseqs_value *buffer = ctx->buffer; + ssize_t retval; + union ctlseqs_value *result = ctx->result; dprintf(ctx->out_fd, CTLSEQS_DECRQM("%u"), mode_id); do { - result = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout); - } while (result == CTLSEQS_PARTIAL); - if (result != 1) { + retval = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout); + } while (retval == CTLSEQS_PARTIAL); + if (retval != 1) { sixdraw_print_error(ctx, "failed to get %s status", name); return false; } - if (buffer[0].num != mode_id || (buffer[1].num != SIXDRAW_DECRQM_SET && buffer[1].num != SIXDRAW_DECRQM_RST)) { + if (result[0].num != mode_id || (result[1].num != SIXDRAW_DECRQM_SET && result[1].num != SIXDRAW_DECRQM_RST)) { sixdraw_print_error(ctx, "%s status not recognizable", name); return false; } @@ -207,7 +207,7 @@ sixdraw_init(struct sixdraw_ctx *ctx, int argc, char **argv) return false; } struct ctlseqs_reader_options reader_options = { - .buffer = ctx->buffer, + .result = ctx->result, .fd = ctx->in_fd, .maxlen = 4096, }; @@ -268,21 +268,21 @@ sixdraw_prepare(struct sixdraw_ctx *ctx) // Check terminal support for sixel graphics and DEC locator. dprintf(ctx->out_fd, CTLSEQS_PRIMARY_DA()); - ssize_t result; + ssize_t retval; do { - result = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout); - } while (result == CTLSEQS_PARTIAL); - if (result != 0) { + retval = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout); + } while (retval == CTLSEQS_PARTIAL); + if (retval != 0) { sixdraw_print_error(ctx, "failed to get terminal features"); return false; } bool has_sixel = false; bool has_dec_locator = false; - union ctlseqs_value *buffer = ctx->buffer; - for (size_t i = 1; i <= buffer[0].num; ++i) { - if (buffer[i].num == 4) { + union ctlseqs_value *result = ctx->result; + for (size_t i = 1; i <= result[0].num; ++i) { + if (result[i].num == 4) { has_sixel = true; - } else if (buffer[i].num == 29) { + } else if (result[i].num == 29) { has_dec_locator = true; } } @@ -299,7 +299,7 @@ sixdraw_prepare(struct sixdraw_ctx *ctx) if (!sixdraw_decrqm(ctx, SIXDRAW_DECTCEM, "cursor")) { return false; } - ctx->show_cursor = buffer[1].num == SIXDRAW_DECRQM_SET; + ctx->show_cursor = result[1].num == SIXDRAW_DECRQM_SET; // Hide cursor. if (ctx->show_cursor) { @@ -310,7 +310,7 @@ sixdraw_prepare(struct sixdraw_ctx *ctx) if (!sixdraw_decrqm(ctx, SIXDRAW_ALT_SCRBUF, "screen buffer")) { return false; } - ctx->normal_scrbuf = buffer[1].num == SIXDRAW_DECRQM_RST; + ctx->normal_scrbuf = result[1].num == SIXDRAW_DECRQM_RST; // Switch to alternate screen buffer. if (ctx->normal_scrbuf) { diff --git a/man/ctlseqs_reader_config.3 b/man/ctlseqs_reader_config.3 index 395c167..2a7eb1e 100644 --- a/man/ctlseqs_reader_config.3 +++ b/man/ctlseqs_reader_config.3 @@ -25,7 +25,7 @@ as shown below: .in +4n .EX struct ctlseqs_reader_options { - union ctlseqs_value *buffer; + union ctlseqs_value *result; size_t maxlen; int fd; unsigned flags; @@ -35,7 +35,7 @@ struct ctlseqs_reader_options { .fi .PP Field -.I buffer +.I result is the pointer to the buffer where the values extracted from the matching sequence of .BR ctlseqs_read () will be stored. @@ -51,12 +51,9 @@ is the file descriptor to read from. Field .I flags is the bit mask of multiple boolean options. -.SS Buffer values -A -.I buffer -is an array of -.BR "union ctlseqs_value" , -as shown below: +.SS Extracted values +An extracted value can be either a string (not guaranteed to be NUL-terminated), or an unsigned integer, as in +.BR "union ctlseqs_value" : .PP .nf .in +4n @@ -69,9 +66,9 @@ union ctlseqs_value { .in .fi .PP -The value can be either a string (not guaranteed to be NUL-terminated), or an unsigned integer. -Once a sequence is successfully matched, a group of values is extracted from the sequence for each placeholder, and stored into the buffer sequentially. -.PP +Once a sequence is successfully matched, a group of values is extracted from the sequence for each placeholder, and stored into +.I result +sequentially. A group can contain one or multiple values, depending on different placeholders: .TP .B CTLSEQS_PH_NUM @@ -106,7 +103,8 @@ call returns .B CTLSEQS_PARTIAL or .BR CTLSEQS_NOMEM , -the length of the entire string followed by the string itself will be stored into the buffer. +the length of the entire string followed by the string itself will be stored into +.IR result . The same is true for a successful match when option .B CTLSEQS_READER_SAVE_MATCHED_SEQS is enabled, inserting these two values before the extracted ones. diff --git a/src/ctlseqs.c b/src/ctlseqs.c index 069ec6d..00627fc 100644 --- a/src/ctlseqs.c +++ b/src/ctlseqs.c @@ -147,7 +147,7 @@ struct ctlseqs_matcher { }; struct ctlseqs_reader { - union ctlseqs_value *buffer; + union ctlseqs_value *result; size_t readlen; struct pollfd pollfd; char *rbuf; @@ -380,7 +380,7 @@ ctlseqs_reader_match(struct ctlseqs_reader *reader, struct ctlseqs_matcher const .seq = reader->rbuf + reader->buf_start, .seq_len = reader->buf_end - reader->buf_start, .offset = reader->last_idx, - .result = reader->buffer, + .result = reader->result, .state = reader->state, .save_seq = reader->save_matched, }; @@ -544,7 +544,7 @@ ctlseqs_reader_config(struct ctlseqs_reader *reader, struct ctlseqs_reader_optio reader->buf_end = reader->readlen; } } - reader->buffer = options->buffer; + reader->result = options->result; reader->pollfd.fd = options->fd; reader->no_poll = options->flags & CTLSEQS_READER_NO_POLL; reader->save_matched = options->flags & CTLSEQS_READER_SAVE_MATCHED_SEQS; diff --git a/src/ctlseqs.h b/src/ctlseqs.h index 92ffc98..8cf2913 100644 --- a/src/ctlseqs.h +++ b/src/ctlseqs.h @@ -380,7 +380,7 @@ struct ctlseqs_matcher_options { }; struct ctlseqs_reader_options { - union ctlseqs_value *buffer; + union ctlseqs_value *result; size_t maxlen; int fd; unsigned flags; diff --git a/tests/tcsgrep.c b/tests/tcsgrep.c index 87c538e..ed8b1a9 100644 --- a/tests/tcsgrep.c +++ b/tests/tcsgrep.c @@ -85,10 +85,10 @@ parse_int(char const *str, int *dest) } static void -print_generic_seq(char const *header, union ctlseqs_value *buffer, bool newline) +print_generic_seq(char const *header, union ctlseqs_value *result, bool newline) { - size_t length = buffer[0].num; - char const *seq = buffer[1].str; + size_t length = result[0].num; + char const *seq = result[1].str; printf("%s %zu", header, length); for (size_t idx = 0; idx < length; ++idx) { char ch = seq[idx]; @@ -108,11 +108,11 @@ print_generic_seq(char const *header, union ctlseqs_value *buffer, bool newline) } static void -print_matching_seq(struct tcsgrep_sequence *seq, union ctlseqs_value *buffer, bool verbose) +print_matching_seq(struct tcsgrep_sequence *seq, union ctlseqs_value *result, bool verbose) { if (verbose) { - print_generic_seq("OK", buffer, false); - buffer += 2; + print_generic_seq("OK", result, false); + result += 2; } else { printf("OK"); } @@ -121,14 +121,14 @@ print_matching_seq(struct tcsgrep_sequence *seq, union ctlseqs_value *buffer, bo char placeholder = seq->args[idx]; switch (placeholder) { case 0x0e: // CTLSEQS_PH_NUM - printf(" %lu", buffer[idx].num); + printf(" %lu", result[idx].num); break; case 0x10: // CTLSEQS_PH_STR - printf(" %.*s", (int)buffer[idx].num, buffer[idx + 1].str); + printf(" %.*s", (int)result[idx].num, result[idx + 1].str); break; case 0x0f: // CTLSEQS_PH_NUMS - for (size_t i = 1; i <= buffer[idx].num; ++i) { - printf(" %lu", buffer[idx + i].num); + for (size_t i = 1; i <= result[idx].num; ++i) { + printf(" %lu", result[idx + i].num); } break; } @@ -362,11 +362,11 @@ main(int argc, char **argv) if (reader == NULL) { print_error(&ctx, "failed to initialize reader"); } - static union ctlseqs_value buffer[4096]; + static union ctlseqs_value result[4096]; struct ctlseqs_reader_options reader_options = { - .fd = STDIN_FILENO, + .fd = STDIN_FILENO, .maxlen = ctx.limit, - .buffer = buffer, + .result = result, .flags = ctx.verbose ? CTLSEQS_READER_SAVE_MATCHED_SEQS : 0, }; if (ctlseqs_reader_config(reader, &reader_options) != CTLSEQS_OK) { @@ -394,8 +394,8 @@ main(int argc, char **argv) int status = 0; while (true) { - ssize_t result = ctlseqs_read(reader, matcher, ctx.timeout); - switch (result) { + ssize_t retval = ctlseqs_read(reader, matcher, ctx.timeout); + switch (retval) { case CTLSEQS_ERROR: print_error(&ctx, "unexpected error"); status = 1; @@ -412,29 +412,29 @@ main(int argc, char **argv) break; case CTLSEQS_PARTIAL: if (ctx.verbose) { - print_generic_seq("PARTIAL", buffer, true); + print_generic_seq("PARTIAL", result, true); } break; case CTLSEQS_NOMATCH: - print_generic_seq("NOMATCH", buffer, true); + print_generic_seq("NOMATCH", result, true); break; case CTLSEQS_NOMEM: - print_generic_seq("NOMEM", buffer, true); + print_generic_seq("NOMEM", result, true); if (ctx.purge_long_seqs) { - ctlseqs_purge(reader, buffer[0].num); + ctlseqs_purge(reader, result[0].num); break; } else { status = 1; goto terminate; } case CTLSEQS_NOSEQ: - print_generic_seq("NOSEQ", buffer, true); - if (buffer[1].str[0] == 0x03) { + print_generic_seq("NOSEQ", result, true); + if (result[1].str[0] == 0x03) { goto terminate; } break; default: - print_matching_seq(&seqs[result], buffer, ctx.verbose); + print_matching_seq(&seqs[retval], result, ctx.verbose); break; } }