-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching.h
122 lines (103 loc) · 3.11 KB
/
matching.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef MATCHING_H__
#define MATCHING_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "zringbuf.h"
#include "zabuffer.h"
#ifdef __cplusplus
#define MATCHING_ITEM_STR(_s) reinterpret_cast<const char *>(_s), sizeof(_s) - 1
#else
#define MATCHING_ITEM_STR(_s) .s = (const char *)_s, .size = sizeof(_s) - 1
#endif
#ifndef MATCHING_RINGBUF_SIZE
#define MATCHING_RINGBUF_SIZE 128
#endif
/**
* Matching item state
*/
enum matching_item_states {
MATCHING_STATE_NO_MATCH = 0, /**< Item not matched */
MATCHING_STATE_UNMATCHABLE = 1, /**< Item is unmatchable */
MATCHING_STATE_CAN_MATCH = 2, /**< Item can match */
MATCHING_STATE_MATCHED = 3 /**< Item is matched */
};
/**
* Matching engine item flags
*/
enum matching_item_flags {
MATCHING_ITEM_FLAGS_DEFAULTS = 0, /**< Default flags are used from matching_ctx.cfg.flags */
MATCHING_ITEM_FLAG_CB_ON_MATCH = (1 << 0), /**< Execute callback on match */
MATCHING_ITEM_FLAG_CB_ON_RESET = (1 << 1) /**< Execute callback on reset */
};
/**
* Matching engine context
*/
struct matching_ctx {
char ringbuf_data[MATCHING_RINGBUF_SIZE];
struct zringbuf ringbuf;
struct za_buffer linebuffer;
struct za_buffer *cc;
uint32_t skip;
/** Configuration */
struct cfg {
uint8_t flags; /**< Default item flags */
const char *reset_chars; /**< Statemachine reset characters */
void *private_data; /**< Private data field for callback context */
} cfg;
struct match {
const struct matching_item *item; /**< Current matched item */
size_t pos; /**< Current position of match */
char *str; /**< Matched item string in linebuffer */
} match;
struct items {
const struct matching_item *list; /**< Items to match */
enum matching_item_states *state; /**< Items state */
size_t n; /**< Items count */
} items;
};
/**
* Matching item
*/
struct matching_item {
const char *s; /**< Item string */
const size_t size; /**< String size */
uint8_t flags; /**< Item flags, when set to MATCHING_ITEM_FLAGS_DEFAULTS.
* matching_ctx.cfg.flags is used
*/
void (*cb)(struct matching_ctx *ctx); /**< Item match callback */
};
/**
* Decode a single character
* - Reset statemachine on ctx->cfg.reset_char
* - Chomp all non printables (less than ' ' || bigger than '~')
*/
void matching_decode(struct matching_ctx *ctx);
void matching_skip(struct matching_ctx *ctx, uint32_t skip);
/**
* Initializes the matching engine
* - Clear ring-buffer
* - Resets the matching engine via matching_reset
*/
void matching_init(struct matching_ctx *ctx);
/**
* Reset matching engine
* - Clear ctx->cfg.linebuffer.data
* - Set all ctx->item->flags to default when set to MATCHING_ITEM_FLAGS_DEFAULTS
* - Set all ctx->items state to NOMATCH
* - Set ctx->pos = 0
* - Set ctx->match_prev
* - Set ctx->match = NULL
*/
void matching_reset(struct matching_ctx *ctx);
/**
* Feed data into the engine
*/
void matching_feed(struct matching_ctx *ctx, char c, bool decode_now);
#ifdef __cplusplus
}
#endif
#endif /* MATCHING_H__ */