1 #ifndef NOTCURSES_AUTOMATON
2 #define NOTCURSES_AUTOMATON
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include <stdint.h>
9 
10 struct ncinput;
11 struct esctrie;
12 struct inputctx;
13 
14 typedef int (*triefunc)(struct inputctx*);
15 
16 // the state necessary for matching input against our automaton of control
17 // sequences. we *do not* match the bulk UTF-8 input. we match online (i.e.
18 // we can be passed a byte at a time). initialize with all zeroes.
19 typedef struct automaton {
20   unsigned escapes;         // head Esc node of trie
21   int used;                 // bytes consumed thus far
22   int instring;             // are we in an ST-terminated string?
23   unsigned state;
24   const unsigned char* matchstart;   // beginning of active match
25   // we keep a node pool not to save time when allocating, but because
26   // trying to free the automaton without reference counting otherwise
27   // sucks worse than three bitches in a bitchboat.
28   unsigned poolsize;
29   unsigned poolused;
30   struct esctrie* nodepool;
31 } automaton;
32 
33 // wipe out all storage internal to |a| (but not |a| itself).
34 void input_free_esctrie(automaton *a);
35 
36 int inputctx_add_input_escape(automaton* a, const char* esc,
37                               uint32_t special, unsigned shift,
38                               unsigned ctrl, unsigned alt);
39 
40 int inputctx_add_cflow(automaton* a, const char* csi, triefunc fxn)
41   __attribute__ ((nonnull (1, 2)));
42 
43 int walk_automaton(automaton* a, struct inputctx* ictx, unsigned candidate,
44                    struct ncinput* ni)
45   __attribute__ ((nonnull (1, 2, 4)));
46 
47 uint32_t esctrie_id(const struct esctrie* e);
48 
49 #ifdef __cplusplus
50 }
51 #endif
52 
53 #endif
54