-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm_code_gen.c
196 lines (165 loc) · 4.72 KB
/
fsm_code_gen.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<stdio.h>
#include<stdlib.h>
/* This is a simple FSM Generator */
/* Note :
This program can be executed as part of Make file or separately
when you compile and run the required .c and .h are generated automatically
fsm.txt is the input file to this script
Just fill the action routines , you are good to go.
This script generates two files as output
1) auto_gen_fsm.h
2) auto_gen_fsm.c"
*/
/*constant definitions*/
#define NL "\n"
#define OPEN_BRCKT "{"
#define OPEN_BRCKT_NL "{\n"
#define CLOSE_BRCKT "}"
#define CLOSE_BRCKT_NL "}\n"
#define SEMI_COLON ";"
#define TYPEDEF "typedef enum"
#define ENUM_EVENT "gen_event_t"
#define ENUM_STATE "gen_state_t"
#define EVENT_PFX "FSM_EVENT"
#define STATE_PFX "FSM__STATE"
FILE *IN = NULL ;
FILE *OUTFILE_SRC = NULL;
FILE *OUTFILE_HDR = NULL;
void open_files()
{
IN= fopen("fsm.txt", "w");
OUTFILE_SRC = fopen("auto_gen_fsm.h", "w");
OUTFILE_HDR = fopen("auto_gen_fsm.c", "w");
return;
}
void print_events_enum()
{
fprintf( OUTFILE_HDR, NL);
fprintf( OUTFILE_HDR, NL);
fprintf( OUTFILE_HDR, "/* These are the enums for events of the FSM */\n");
fprintf( OUTFILE_HDR, TYPEDEF);
fprintf( OUTFILE_HDR, NL);
fprintf( OUTFILE_HDR, OPEN_BRCKT_NL);
fprintf( OUTFILE_HDR, "\t$tmp\n");
fprintf( OUTFILE_HDR, "\t$tmp\n");
fprintf( OUTFILE_HDR, CLOSE_BRCKT);
fprintf( OUTFILE_HDR, ENUM_EVENT);
fprintf( OUTFILE_HDR, SEMI_COLON);
fprintf( OUTFILE_HDR, NL);
}
void print_events()
{
fprintf( OUTFILE_SRC, NL);
fprintf( OUTFILE_SRC, NL);
fprintf( OUTFILE_SRC, "/* These are the events of the FSM */\n");
fprintf( OUTFILE_SRC, "/* This enum should match the event enums */\n");
fprintf( OUTFILE_SRC, " char* gen_fsm_event_string[]= \n");
fprintf( OUTFILE_SRC, OPEN_BRCKT_NL);
fprintf( OUTFILE_SRC, "\t\"MAX_EVENT\"\n");
fprintf( OUTFILE_SRC, CLOSE_BRCKT);
fprintf( OUTFILE_SRC, SEMI_COLON);
fprintf( OUTFILE_SRC, NL);
}
void print_states_enum()
{
fprintf( OUTFILE_HDR, NL);
fprintf( OUTFILE_HDR, NL);
fprintf( OUTFILE_HDR, "/* These are the enums for states of the FSM */\n");
fprintf( OUTFILE_HDR, TYPEDEF);
fprintf( OUTFILE_HDR, NL);
fprintf( OUTFILE_HDR, OPEN_BRCKT_NL);
fprintf( OUTFILE_HDR, "\t$tmp\n");
fprintf( OUTFILE_HDR, CLOSE_BRCKT);
fprintf( OUTFILE_HDR, ENUM_STATE);
fprintf( OUTFILE_HDR, SEMI_COLON);
fprintf( OUTFILE_HDR, NL);
}
void print_states()
{
fprintf( OUTFILE_SRC, NL);
fprintf( OUTFILE_SRC, NL);
fprintf( OUTFILE_SRC, "/* These are the states of the FSM */\n");
fprintf( OUTFILE_SRC, "/* This enum should match the state enums */\n");
fprintf( OUTFILE_SRC, "char* gen_fsm_state_string[] = \n" );
fprintf( OUTFILE_SRC, OPEN_BRCKT_NL);
{
fprintf( OUTFILE_SRC, "\t\"$k\",\n");
}
fprintf( OUTFILE_SRC, "\t\"MAX_STATE\"\n");
fprintf( OUTFILE_SRC, CLOSE_BRCKT);
fprintf( OUTFILE_SRC, SEMI_COLON);
fprintf( OUTFILE_SRC, NL);
}
void print_state_table()
{
fprintf( OUTFILE_SRC, NL);
fprintf( OUTFILE_SRC, NL);
fprintf( OUTFILE_SRC, "/* This is the init function of the fsm*/\n");
fprintf( OUTFILE_SRC, "void gen_fsm_register_handles()\n");
fprintf( OUTFILE_SRC, OPEN_BRCKT_NL);
{
fprintf( OUTFILE_SRC, "\t$k\n");
fprintf( OUTFILE_SRC, NL);
}
fprintf( OUTFILE_SRC, CLOSE_BRCKT_NL);
}
void print_comments()
{
}
void print_header_file()
{
fprintf( OUTFILE_HDR, "#ifndef _AUTO_GEN_FSM_\n");
fprintf( OUTFILE_HDR, "#define _AUTO_GEN_FSM_\n");
fprintf( OUTFILE_HDR, "\n");
fprintf( OUTFILE_HDR, "\n");
fprintf( OUTFILE_HDR, "/***** Auto generated C file ********************/\n");
/*fprintf( OUTFILE_HDR, "***** Generated at $time ****\n");*/
fprintf( OUTFILE_HDR, "/***** DO NOT EDIT THIS FILE EVER MANUALLY *******/\n");
}
void close_header_file()
{
fprintf( OUTFILE_HDR, "#endif");
close (OUTFILE_HDR);
}
void close_src_file()
{
close (OUTFILE_SRC);
}
/*generate files*/
void generate_header_file()
{
print_header_file();
fprintf( OUTFILE_HDR, "void gen_fsm_register_handles(void);\n");
/*print_states_enum();*/
print_events_enum();
close_header_file();
}
void generate_source_file()
{
fprintf(OUTFILE_SRC, "#include \"auto_gen_fsm.h\"\n");
fprintf(OUTFILE_SRC, "#include \"gen_fsm.h\"\n");
fprintf(OUTFILE_SRC, "#include \"gen_proto.h\"\n");
fprintf(OUTFILE_SRC, "\n");
fprintf(OUTFILE_SRC, "\n");
print_comments();
/*print_states(); */
print_events();
print_state_table();
close_src_file();
}
void parse_file()
{
/* TODO */
}
/*main function*/
int main()
{
open_files();
parse_file();
/*print file headers*/
generate_header_file();
generate_source_file();
print_state_table();
close_src_file();
printf( "FSM generation completed ! \n\n");
}