Rename field.

This commit is contained in:
CismonX 2020-12-24 18:21:03 +08:00
parent 5bf70d7b77
commit f264a8f183
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
5 changed files with 54 additions and 56 deletions

View File

@ -49,7 +49,7 @@
struct sixdraw_ctx { struct sixdraw_ctx {
struct termios termios; struct termios termios;
union ctlseqs_value buffer[64]; union ctlseqs_value result[64];
char const *prog_name; char const *prog_name;
struct ctlseqs_matcher *matcher; struct ctlseqs_matcher *matcher;
struct ctlseqs_reader *reader; struct ctlseqs_reader *reader;
@ -141,17 +141,17 @@ sixdraw_get_winsize(struct sixdraw_ctx *ctx)
static bool static bool
sixdraw_decrqm(struct sixdraw_ctx *ctx, unsigned mode_id, char const *name) sixdraw_decrqm(struct sixdraw_ctx *ctx, unsigned mode_id, char const *name)
{ {
ssize_t result; ssize_t retval;
union ctlseqs_value *buffer = ctx->buffer; union ctlseqs_value *result = ctx->result;
dprintf(ctx->out_fd, CTLSEQS_DECRQM("%u"), mode_id); dprintf(ctx->out_fd, CTLSEQS_DECRQM("%u"), mode_id);
do { do {
result = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout); retval = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout);
} while (result == CTLSEQS_PARTIAL); } while (retval == CTLSEQS_PARTIAL);
if (result != 1) { if (retval != 1) {
sixdraw_print_error(ctx, "failed to get %s status", name); sixdraw_print_error(ctx, "failed to get %s status", name);
return false; 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); sixdraw_print_error(ctx, "%s status not recognizable", name);
return false; return false;
} }
@ -207,7 +207,7 @@ sixdraw_init(struct sixdraw_ctx *ctx, int argc, char **argv)
return false; return false;
} }
struct ctlseqs_reader_options reader_options = { struct ctlseqs_reader_options reader_options = {
.buffer = ctx->buffer, .result = ctx->result,
.fd = ctx->in_fd, .fd = ctx->in_fd,
.maxlen = 4096, .maxlen = 4096,
}; };
@ -268,21 +268,21 @@ sixdraw_prepare(struct sixdraw_ctx *ctx)
// Check terminal support for sixel graphics and DEC locator. // Check terminal support for sixel graphics and DEC locator.
dprintf(ctx->out_fd, CTLSEQS_PRIMARY_DA()); dprintf(ctx->out_fd, CTLSEQS_PRIMARY_DA());
ssize_t result; ssize_t retval;
do { do {
result = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout); retval = ctlseqs_read(ctx->reader, ctx->matcher, ctx->timeout);
} while (result == CTLSEQS_PARTIAL); } while (retval == CTLSEQS_PARTIAL);
if (result != 0) { if (retval != 0) {
sixdraw_print_error(ctx, "failed to get terminal features"); sixdraw_print_error(ctx, "failed to get terminal features");
return false; return false;
} }
bool has_sixel = false; bool has_sixel = false;
bool has_dec_locator = false; bool has_dec_locator = false;
union ctlseqs_value *buffer = ctx->buffer; union ctlseqs_value *result = ctx->result;
for (size_t i = 1; i <= buffer[0].num; ++i) { for (size_t i = 1; i <= result[0].num; ++i) {
if (buffer[i].num == 4) { if (result[i].num == 4) {
has_sixel = true; has_sixel = true;
} else if (buffer[i].num == 29) { } else if (result[i].num == 29) {
has_dec_locator = true; has_dec_locator = true;
} }
} }
@ -299,7 +299,7 @@ sixdraw_prepare(struct sixdraw_ctx *ctx)
if (!sixdraw_decrqm(ctx, SIXDRAW_DECTCEM, "cursor")) { if (!sixdraw_decrqm(ctx, SIXDRAW_DECTCEM, "cursor")) {
return false; return false;
} }
ctx->show_cursor = buffer[1].num == SIXDRAW_DECRQM_SET; ctx->show_cursor = result[1].num == SIXDRAW_DECRQM_SET;
// Hide cursor. // Hide cursor.
if (ctx->show_cursor) { if (ctx->show_cursor) {
@ -310,7 +310,7 @@ sixdraw_prepare(struct sixdraw_ctx *ctx)
if (!sixdraw_decrqm(ctx, SIXDRAW_ALT_SCRBUF, "screen buffer")) { if (!sixdraw_decrqm(ctx, SIXDRAW_ALT_SCRBUF, "screen buffer")) {
return false; 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. // Switch to alternate screen buffer.
if (ctx->normal_scrbuf) { if (ctx->normal_scrbuf) {

View File

@ -25,7 +25,7 @@ as shown below:
.in +4n .in +4n
.EX .EX
struct ctlseqs_reader_options { struct ctlseqs_reader_options {
union ctlseqs_value *buffer; union ctlseqs_value *result;
size_t maxlen; size_t maxlen;
int fd; int fd;
unsigned flags; unsigned flags;
@ -35,7 +35,7 @@ struct ctlseqs_reader_options {
.fi .fi
.PP .PP
Field Field
.I buffer .I result
is the pointer to the buffer where the values extracted from the matching sequence of is the pointer to the buffer where the values extracted from the matching sequence of
.BR ctlseqs_read () .BR ctlseqs_read ()
will be stored. will be stored.
@ -51,12 +51,9 @@ is the file descriptor to read from.
Field Field
.I flags .I flags
is the bit mask of multiple boolean options. is the bit mask of multiple boolean options.
.SS Buffer values .SS Extracted values
A An extracted value can be either a string (not guaranteed to be NUL-terminated), or an unsigned integer, as in
.I buffer .BR "union ctlseqs_value" :
is an array of
.BR "union ctlseqs_value" ,
as shown below:
.PP .PP
.nf .nf
.in +4n .in +4n
@ -69,9 +66,9 @@ union ctlseqs_value {
.in .in
.fi .fi
.PP .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
Once a sequence is successfully matched, a group of values is extracted from the sequence for each placeholder, and stored into the buffer sequentially. .I result
.PP sequentially.
A group can contain one or multiple values, depending on different placeholders: A group can contain one or multiple values, depending on different placeholders:
.TP .TP
.B CTLSEQS_PH_NUM .B CTLSEQS_PH_NUM
@ -106,7 +103,8 @@ call returns
.B CTLSEQS_PARTIAL .B CTLSEQS_PARTIAL
or or
.BR CTLSEQS_NOMEM , .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 The same is true for a successful match when option
.B CTLSEQS_READER_SAVE_MATCHED_SEQS .B CTLSEQS_READER_SAVE_MATCHED_SEQS
is enabled, inserting these two values before the extracted ones. is enabled, inserting these two values before the extracted ones.

View File

@ -147,7 +147,7 @@ struct ctlseqs_matcher {
}; };
struct ctlseqs_reader { struct ctlseqs_reader {
union ctlseqs_value *buffer; union ctlseqs_value *result;
size_t readlen; size_t readlen;
struct pollfd pollfd; struct pollfd pollfd;
char *rbuf; char *rbuf;
@ -380,7 +380,7 @@ ctlseqs_reader_match(struct ctlseqs_reader *reader, struct ctlseqs_matcher const
.seq = reader->rbuf + reader->buf_start, .seq = reader->rbuf + reader->buf_start,
.seq_len = reader->buf_end - reader->buf_start, .seq_len = reader->buf_end - reader->buf_start,
.offset = reader->last_idx, .offset = reader->last_idx,
.result = reader->buffer, .result = reader->result,
.state = reader->state, .state = reader->state,
.save_seq = reader->save_matched, .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->buf_end = reader->readlen;
} }
} }
reader->buffer = options->buffer; reader->result = options->result;
reader->pollfd.fd = options->fd; reader->pollfd.fd = options->fd;
reader->no_poll = options->flags & CTLSEQS_READER_NO_POLL; reader->no_poll = options->flags & CTLSEQS_READER_NO_POLL;
reader->save_matched = options->flags & CTLSEQS_READER_SAVE_MATCHED_SEQS; reader->save_matched = options->flags & CTLSEQS_READER_SAVE_MATCHED_SEQS;

View File

@ -380,7 +380,7 @@ struct ctlseqs_matcher_options {
}; };
struct ctlseqs_reader_options { struct ctlseqs_reader_options {
union ctlseqs_value *buffer; union ctlseqs_value *result;
size_t maxlen; size_t maxlen;
int fd; int fd;
unsigned flags; unsigned flags;

View File

@ -85,10 +85,10 @@ parse_int(char const *str, int *dest)
} }
static void 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; size_t length = result[0].num;
char const *seq = buffer[1].str; char const *seq = result[1].str;
printf("%s %zu", header, length); printf("%s %zu", header, length);
for (size_t idx = 0; idx < length; ++idx) { for (size_t idx = 0; idx < length; ++idx) {
char ch = seq[idx]; char ch = seq[idx];
@ -108,11 +108,11 @@ print_generic_seq(char const *header, union ctlseqs_value *buffer, bool newline)
} }
static void 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) { if (verbose) {
print_generic_seq("OK", buffer, false); print_generic_seq("OK", result, false);
buffer += 2; result += 2;
} else { } else {
printf("OK"); printf("OK");
} }
@ -121,14 +121,14 @@ print_matching_seq(struct tcsgrep_sequence *seq, union ctlseqs_value *buffer, bo
char placeholder = seq->args[idx]; char placeholder = seq->args[idx];
switch (placeholder) { switch (placeholder) {
case 0x0e: // CTLSEQS_PH_NUM case 0x0e: // CTLSEQS_PH_NUM
printf(" %lu", buffer[idx].num); printf(" %lu", result[idx].num);
break; break;
case 0x10: // CTLSEQS_PH_STR 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; break;
case 0x0f: // CTLSEQS_PH_NUMS case 0x0f: // CTLSEQS_PH_NUMS
for (size_t i = 1; i <= buffer[idx].num; ++i) { for (size_t i = 1; i <= result[idx].num; ++i) {
printf(" %lu", buffer[idx + i].num); printf(" %lu", result[idx + i].num);
} }
break; break;
} }
@ -362,11 +362,11 @@ main(int argc, char **argv)
if (reader == NULL) { if (reader == NULL) {
print_error(&ctx, "failed to initialize reader"); 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 = { struct ctlseqs_reader_options reader_options = {
.fd = STDIN_FILENO, .fd = STDIN_FILENO,
.maxlen = ctx.limit, .maxlen = ctx.limit,
.buffer = buffer, .result = result,
.flags = ctx.verbose ? CTLSEQS_READER_SAVE_MATCHED_SEQS : 0, .flags = ctx.verbose ? CTLSEQS_READER_SAVE_MATCHED_SEQS : 0,
}; };
if (ctlseqs_reader_config(reader, &reader_options) != CTLSEQS_OK) { if (ctlseqs_reader_config(reader, &reader_options) != CTLSEQS_OK) {
@ -394,8 +394,8 @@ main(int argc, char **argv)
int status = 0; int status = 0;
while (true) { while (true) {
ssize_t result = ctlseqs_read(reader, matcher, ctx.timeout); ssize_t retval = ctlseqs_read(reader, matcher, ctx.timeout);
switch (result) { switch (retval) {
case CTLSEQS_ERROR: case CTLSEQS_ERROR:
print_error(&ctx, "unexpected error"); print_error(&ctx, "unexpected error");
status = 1; status = 1;
@ -412,29 +412,29 @@ main(int argc, char **argv)
break; break;
case CTLSEQS_PARTIAL: case CTLSEQS_PARTIAL:
if (ctx.verbose) { if (ctx.verbose) {
print_generic_seq("PARTIAL", buffer, true); print_generic_seq("PARTIAL", result, true);
} }
break; break;
case CTLSEQS_NOMATCH: case CTLSEQS_NOMATCH:
print_generic_seq("NOMATCH", buffer, true); print_generic_seq("NOMATCH", result, true);
break; break;
case CTLSEQS_NOMEM: case CTLSEQS_NOMEM:
print_generic_seq("NOMEM", buffer, true); print_generic_seq("NOMEM", result, true);
if (ctx.purge_long_seqs) { if (ctx.purge_long_seqs) {
ctlseqs_purge(reader, buffer[0].num); ctlseqs_purge(reader, result[0].num);
break; break;
} else { } else {
status = 1; status = 1;
goto terminate; goto terminate;
} }
case CTLSEQS_NOSEQ: case CTLSEQS_NOSEQ:
print_generic_seq("NOSEQ", buffer, true); print_generic_seq("NOSEQ", result, true);
if (buffer[1].str[0] == 0x03) { if (result[1].str[0] == 0x03) {
goto terminate; goto terminate;
} }
break; break;
default: default:
print_matching_seq(&seqs[result], buffer, ctx.verbose); print_matching_seq(&seqs[retval], result, ctx.verbose);
break; break;
} }
} }