Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
automaton.c File Reference
#include "automaton.h"
#include "internal.h"
Include dependency graph for automaton.c:

Go to the source code of this file.

Data Structures

struct  esctrie
 

Typedefs

typedef struct esctrie esctrie
 

Functions

uint32_t esctrie_id (const esctrie *e)
 
void input_free_esctrie (automaton *a)
 
int inputctx_add_cflow (automaton *a, const char *seq, triefunc fxn)
 
int inputctx_add_input_escape (automaton *a, const char *esc, uint32_t special, unsigned modifiers)
 
int walk_automaton (automaton *a, struct inputctx *ictx, unsigned candidate, ncinput *ni)
 

Typedef Documentation

◆ esctrie

typedef struct esctrie esctrie

Function Documentation

◆ esctrie_id()

uint32_t esctrie_id ( const esctrie e)

Definition at line 50 of file automaton.c.

50 {
51 return e->ni.id;
52}
static escape_e e
Definition termdesc.h:224

◆ input_free_esctrie()

void input_free_esctrie ( automaton a)

Definition at line 81 of file automaton.c.

81 {
82 a->escapes = 0;
83 a->poolsize = 0;
84 for(unsigned i = 0 ; i < a->poolused ; ++i){
85 free(a->nodepool[i].trie);
86 }
87 free(a->nodepool);
88 a->poolused = 0;
89 a->nodepool = NULL;
90}
free(duplicated)
struct esctrie * nodepool
Definition automaton.h:30
unsigned escapes
Definition automaton.h:20
unsigned poolsize
Definition automaton.h:28
unsigned poolused
Definition automaton.h:29
unsigned * trie
Definition automaton.c:23
return NULL
Definition termdesc.h:229
Here is the call graph for this function:

◆ inputctx_add_cflow()

int inputctx_add_cflow ( automaton a,
const char *  seq,
triefunc  fxn 
)

Definition at line 502 of file automaton.c.

502 {
503 esctrie* eptr = insert_path(a, seq);
504 if(eptr == NULL){
505 return -1;
506 }
507 free(eptr->trie);
508 eptr->trie = NULL;
509 return esctrie_make_function(eptr, fxn);
510}
Here is the call graph for this function:

◆ inputctx_add_input_escape()

int inputctx_add_input_escape ( automaton a,
const char *  esc,
uint32_t  special,
unsigned  modifiers 
)

Definition at line 513 of file automaton.c.

514 {
515 if(esc[0] != NCKEY_ESC || strlen(esc) < 2){ // assume ESC prefix + content
516 logerror("not an escape (0x%x)", special);
517 return -1;
518 }
519 esctrie* eptr = insert_path(a, esc + 1);
520 if(eptr == NULL){
521 return -1;
522 }
523 // it appears that multiple keys can be mapped to the same escape string. as
524 // an example, see "kend" and "kc1" in st ("simple term" from suckless) :/.
525 if(eptr->ni.id){ // already had one here!
526 if(eptr->ni.id != special){
527 logwarn("already added escape (got 0x%x, wanted 0x%x)", eptr->ni.id, special);
528 }
529 }else{
530 eptr->ni.id = special;
531 eptr->ni.shift = modifiers & NCKEY_MOD_SHIFT;
532 eptr->ni.ctrl = modifiers & NCKEY_MOD_CTRL;
533 eptr->ni.alt = modifiers & NCKEY_MOD_ALT;
534 eptr->ni.y = 0;
535 eptr->ni.x = 0;
536 eptr->ni.modifiers = modifiers;
537 logdebug("added 0x%08x to %u", special, esctrie_idx(a, eptr));
538 }
539 return 0;
540}
#define logerror(fmt,...)
Definition logging.h:32
#define logdebug(fmt,...)
Definition logging.h:52
#define logwarn(fmt,...)
Definition logging.h:37
#define NCKEY_MOD_SHIFT
Definition nckeys.h:219
#define NCKEY_MOD_CTRL
Definition nckeys.h:221
#define NCKEY_ESC
Definition nckeys.h:196
#define NCKEY_MOD_ALT
Definition nckeys.h:220
ncinput ni
Definition automaton.c:30
bool alt
Definition notcurses.h:1214
bool ctrl
Definition notcurses.h:1216
uint32_t id
Definition notcurses.h:1210
bool shift
Definition notcurses.h:1215
unsigned modifiers
Definition notcurses.h:1219

◆ walk_automaton()

int walk_automaton ( automaton a,
struct inputctx ictx,
unsigned  candidate,
ncinput ni 
)

Definition at line 546 of file automaton.c.

547 {
548 if(candidate >= 0x80){
549 logerror("eight-bit char %u in control sequence", candidate);
550 return -1;
551 }
552 esctrie* e = esctrie_from_idx(a, a->state);
553 // we ought not have been called for an escape with any state!
554 if(candidate == 0x1b && !a->instring){
555 assert(NULL == e);
556 a->state = a->escapes;
557 return 0;
558 }
559 if(e->ntype == NODE_STRING){
560 if(candidate == 0x1b || candidate == 0x07){
561 a->state = e->trie[candidate];
562 a->instring = 0;
563 }
564 e = esctrie_from_idx(a, a->state);
565 if(e->ntype == NODE_FUNCTION){ // for the 0x07s of the world
566 if(e->fxn == NULL){
567 return 2;
568 }
569 return e->fxn(ictx);
570 }
571 return 0;
572 }
573 if((a->state = e->trie[candidate]) == 0){
574 if(esctrie_idx(a, e) == a->escapes){
575 memset(ni, 0, sizeof(*ni));
576 ni->id = candidate;
577 ni->alt = true;
578 return 1;
579 }
580 loginfo("unexpected transition on %u[%u]",
581 esctrie_idx(a, e), candidate);
582 return -1;
583 }
584 e = esctrie_from_idx(a, a->state);
585 // initialize any node we've just stepped into
586 switch(e->ntype){
587 case NODE_NUMERIC:
588 break;
589 case NODE_STRING:
590 a->instring = 1;
591 break;
592 case NODE_SPECIAL:
593 if(e->ni.id){
594 memcpy(ni, &e->ni, sizeof(*ni));
595 return 1;
596 }
597 break;
598 case NODE_FUNCTION:
599 if(e->fxn == NULL){
600 return 2;
601 }
602 return e->fxn(ictx);
603 break;
604 }
605 return 0;
606}
assert(false)
#define loginfo(fmt,...)
Definition logging.h:42
unsigned state
Definition automaton.h:23
int instring
Definition automaton.h:22
Here is the call graph for this function: