#include <stdint.h>
#include <stdio.h>
enum eventType {
Event_keyboard,
};
static bool compareKeyboardChar(
void *ch,
struct event *
event );
static void printRecognisedChar(
void *stateData,
struct event *
event );
static void printUnrecognisedChar(
void *oldStateData,
struct event *
event,
void *newStateData );
static void printReset(
void *oldStateData,
struct event *
event,
void *newStateData );
static void printHiMsg(
void *oldStateData,
struct event *
event,
void *newStateData );
static void printHaMsg(
void *oldStateData,
struct event *
event,
void *newStateData );
static void printErrMsg(
void *stateData,
struct event *
event );
static void printEnterMsg(
void *stateData,
struct event *
event );
static void printExitMsg(
void *stateData,
struct event *
event );
static struct state checkCharsGroupState, idleState, hState, iState, aState;
static struct state checkCharsGroupState = {
{ Event_keyboard, (void *)(intptr_t)'!', &compareKeyboardChar,
&printReset, &idleState, },
{ Event_keyboard, NULL, NULL, &printUnrecognisedChar, &idleState, },
},
.numTransitions = 2,
.data = "group",
.entryAction = &printEnterMsg,
.exitAction = &printExitMsg,
};
static struct state idleState = {
{ Event_keyboard, (void *)(intptr_t)'h', &compareKeyboardChar, NULL,
&hState },
},
.numTransitions = 1,
.data = "idle",
.entryAction = &printEnterMsg,
.exitAction = &printExitMsg,
};
static struct state hState = {
{ Event_keyboard, (void *)(intptr_t)'a', &compareKeyboardChar, NULL,
&aState },
{ Event_keyboard, (void *)(intptr_t)'i', &compareKeyboardChar, NULL,
&iState },
},
.numTransitions = 2,
.data = "H",
.entryAction = &printRecognisedChar,
.exitAction = &printExitMsg,
};
static struct state iState = {
{ Event_keyboard, (void *)(intptr_t)'\n', &compareKeyboardChar,
&printHiMsg, &idleState }
},
.data = "I",
.entryAction = &printRecognisedChar,
.exitAction = &printExitMsg,
};
static struct state aState = {
{ Event_keyboard, (void *)(intptr_t)'\n', &compareKeyboardChar,
&printHaMsg, &idleState }
},
.data = "A",
.entryAction = &printRecognisedChar,
.exitAction = &printExitMsg
};
static struct state errorState = {
};
int main()
{
int ch;
while ( ( ch = getc( stdin ) ) != EOF )
(void *)(intptr_t)ch } );
return 0;
}
static
bool compareKeyboardChar(
void *ch, struct
event *
event )
{
if ( event->type != Event_keyboard )
return false;
return (intptr_t)ch == (intptr_t)event->data;
}
static void printRecognisedChar( void *stateData, struct event *event )
{
printEnterMsg( stateData, event );
printf(
"parsed: %c\n", (
char)(intptr_t)event->
data );
}
static void printUnrecognisedChar( void *oldStateData, struct event *event,
void *newStateData )
{
printf( "unrecognised character: %c\n",
(
char)(intptr_t)event->
data );
}
static void printReset( void *oldStateData, struct event *event,
void *newStateData )
{
puts( "Resetting" );
}
static void printHiMsg( void *oldStateData, struct event *event,
void *newStateData )
{
puts( "Hi!" );
}
static void printHaMsg( void *oldStateData, struct event *event,
void *newStateData )
{
puts( "Ha-ha" );
}
static void printErrMsg( void *stateData, struct event *event )
{
puts( "ENTERED ERROR STATE!" );
}
static void printEnterMsg( void *stateData, struct event *event )
{
printf( "Entering %s state\n", (char *)stateData );
}
static void printExitMsg( void *stateData, struct event *event )
{
printf( "Exiting %s state\n", (char *)stateData );
}