Add option to save matched sequences.

This commit is contained in:
CismonX 2020-12-18 18:16:38 +08:00
parent 08cd45653e
commit 2cdb088697
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
3 changed files with 15 additions and 4 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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 */