From 2cdb088697618f5e3bdc04b1fd134dae63ddd7d2 Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 18 Dec 2020 18:16:38 +0800 Subject: [PATCH] Add option to save matched sequences. --- man/ctlseqs_reader_config.3 | 8 ++++++++ src/ctlseqs.c | 6 ++++-- src/ctlseqs.h | 5 +++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/man/ctlseqs_reader_config.3 b/man/ctlseqs_reader_config.3 index c371ff2..fdab338 100644 --- a/man/ctlseqs_reader_config.3 +++ b/man/ctlseqs_reader_config.3 @@ -107,6 +107,9 @@ call returns or .BR CTLSEQS_NOMEM , the length of the entire string followed by the string itself will be stored into the buffer. +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. .SS Flags Flags are boolean switches which can be combined as a bitmask for the .I flags @@ -124,6 +127,11 @@ before You may want this option enabled if .I fd is maintained in an event loop. +.TP +.B CTLSEQS_READER_SAVE_MATCHED_SEQS +When a sequence is successfully matched against a pattern during +.BR ctlseqs_read (), +save the original sequence to buffer alongside the extracted values. . .SH RETURN VALUE .TP diff --git a/src/ctlseqs.c b/src/ctlseqs.c index c825909..1d3a09c 100644 --- a/src/ctlseqs.c +++ b/src/ctlseqs.c @@ -142,6 +142,7 @@ struct ctlseqs_reader { size_t last_idx; enum ctlseqs_state state; bool no_poll; + bool save_matched; }; CTLSEQS_HOT static inline int @@ -282,7 +283,7 @@ ctlseqs_match_pattern(struct ctlseqs_reader *reader, struct ctlseqs_matcher cons struct ctlseqs_match_ctx match_ctx = { .node = matcher == NULL ? &empty_node : &matcher->root, .rbuf = reader->rbuf + reader->buf_start + 1, - .result = reader->buffer, + .result = reader->buffer + (reader->save_matched ? 2 : 0), }; ssize_t match_stack_top = -1; while (true) { @@ -348,7 +349,7 @@ ctlseqs_match(struct ctlseqs_reader *reader, struct ctlseqs_matcher const *match break; } } - if (retval < 0) { + if (retval < 0 || reader->save_matched) { reader->buffer[0].num = idx; reader->buffer[1].str = buf; } @@ -496,6 +497,7 @@ ctlseqs_reader_config(struct ctlseqs_reader *reader, struct ctlseqs_reader_optio reader->buffer = options->buffer; reader->pollfd.fd = options->fd; reader->no_poll = options->flags & CTLSEQS_READER_NO_POLL; + reader->save_matched = options->flags & CTLSEQS_READER_SAVE_MATCHED_SEQS; return CTLSEQS_OK; } diff --git a/src/ctlseqs.h b/src/ctlseqs.h index eed0060..2507a17 100644 --- a/src/ctlseqs.h +++ b/src/ctlseqs.h @@ -345,7 +345,7 @@ /* Placeholders */ #define CTLSEQS_PH_NUM "\x0e" // CSI Parameter Bytes, numbers only -#define CTLSEQS_PH_NUMS "\x0f" // CSI Parameter Bytes, multiple numbers separated by semicolon +#define CTLSEQS_PH_NUMS "\x0f" // CSI Parameter Bytes, multiple numbers separated by semicolon #define CTLSEQS_PH_STR "\x10" // String, printable characters only #define CTLSEQS_PH_CMDSTR "\x11" // Command String #define CTLSEQS_PH_CSI_PARAM "\x12" // CSI Parameter Bytes @@ -355,7 +355,8 @@ /* Reader option flags */ -#define CTLSEQS_READER_NO_POLL (1 << 0) // Do not poll() before read() +#define CTLSEQS_READER_NO_POLL (1 << 0) // Do not poll() before read() +#define CTLSEQS_READER_SAVE_MATCHED_SEQS (1 << 1) // Save successfully matched sequence to buffer /* Function return status codes */