1 /* $NetBSD: main.c,v 1.16 2009/03/14 21:04:25 dsl Exp $ */
5 * rewrite the command line stuff altogether - it's kludged beyond
6 * belief (as is the rest of the code...)
8 * DISCLAIMER DISCLAIMER DISCLAIMER
9 * This code is such a kludge that I don't want to put my name on it.
10 * It was a ridiculously fast hack and needs rewriting.
11 * However it does work...
14 #include <sys/cdefs.h>
15 __KERNEL_RCSID(0, "$NetBSD: main.c,v 1.16 2009/03/14 21:04:25 dsl Exp $");
30 FILE *statefile
, *actfile
, *eventfile_h
, *statevalfile
;
31 FILE *infile
, *astringfile
;
33 char *astringfile_name
= DEBUGFILE
;
34 char *actfile_name
= ACTFILE
;
35 char *statefile_name
= STATEFILE
;
36 char *statevalfile_name
= STATEVALFILE
;
37 char *eventfile_h_name
= EVENTFILE_H
;
39 int print_protoerrs
= 0;
41 char kerneldirname
[50] = "\0";
52 extern void llparse();
53 extern void initsets();
54 extern void init_alloc();
55 extern void dump_predtable();
56 extern void printprotoerrs();
62 "usage: %s <transition file> {-D<debug options>} <other options>\n",
64 fprintf(stderr
, "\t<other options> is any combination of:\n");
65 fprintf(stderr
, "\t\t-A<action file name>\n");
66 fprintf(stderr
, "\t\t-E<event file name>\n");
67 fprintf(stderr
, "\t\t-S<state file name>\n");
68 fprintf(stderr
, "\t\t-I<initial values file name>\n");
69 fprintf(stderr
, "\t\t-X<debugging file name>\n");
70 fprintf(stderr
, "\t\t-K<directory name>\n");
72 "\tThese names do NOT include the suffixes (.c, .h)\n");
74 "\t\t-D<options> to turn on debug options for xebec itself\n");
75 fprintf(stderr
, "\t-<nn> for levels of debugging output\n");
76 fprintf(stderr
, "\t\t<nn> ranges from 1 to 3, 1 is default(everything)\n");
77 fprintf(stderr
, "\t\t-T to print transitions\n");
78 fprintf(stderr
, "\t\t-e to print list of combinations of\n");
79 fprintf(stderr
, "\t\t\t [event,old_state] that produce protocol errors\n");
80 fprintf(stderr
, "\t\t-g include profiling code in driver\n");
85 openfiles(register char *proto
)
88 register int lenp
= strlen(proto
);
91 fprintf(OUT
, "openfiles %s\n",proto
);
96 junk = Malloc( 2 + lenp + strlen(X ## _name) );\
97 (void) sprintf(junk, "%s_", proto);\
98 X ## _name = strcat(junk, X ## _name);\
99 X = fopen(X ## _name, "w");\
101 { fprintf(stderr,"Open failed: %s\n", "X"); Exit(-1); }\
102 fprintf(X, "/* %cHeader%c */\n",'$', '$' );\
103 fprintf(X, "/* %cSource%c */\n",'$', '$' );
112 "#ifndef _NFILE\n#include <stdio.h>\n#endif /* _NFILE */\n" );
119 "#ifndef lint\nstatic char *rcsid = \"%cHeader%c\";\n#endif /* lint */\n",
123 putdriver(actfile
, 15);
125 putdriver(actfile
, 14);
127 FakeFilename(actfile
, Transfilename
, lineno
);
128 putdriver(actfile
, 1);
129 FakeFilename(actfile
, Transfilename
, lineno
);
130 putdriver(actfile
, 12);
131 fprintf(actfile
, "#include \"%s%s\"\n", kerneldirname
, statevalfile_name
);
132 FakeFilename(actfile
, Transfilename
, lineno
);
133 putdriver(actfile
, 2);
135 initsets(eventfile_h
, statefile
);
139 includecode(FILE *file
, register char *f
)
141 register int count
=1;
147 fprintf(stdout
, "including: %s, f=%p", f
,f
);
157 fprintf(stdout
, "derived: %s", f
);
159 fprintf(file
, "%s", f
);
160 FakeFilename(file
, Transfilename
, lineno
);
166 FakeFilename(actfile
, Transfilename
, lineno
);
167 fprintf(actfile
, "\n#include \"%s%s\"\n", kerneldirname
, eventfile_h_name
);
170 fprintf(actfile
, "\n#include \"%s\"\n", astringfile_name
);
171 /* not in kernel mode */
173 FakeFilename(actfile
, Transfilename
, lineno
);
177 main(int argc
, char *argv
[])
180 extern char *strcpy();
182 extern int FirstEventAttribute
;
183 extern int Nevents
, Nstates
;
190 fprintf(stdout
, "infile = %s\n",argv
[1]);
192 Transfilename
= argv
[1];
193 infile
= fopen(argv
[1], "r");
195 if(argc
> 2) while(i
< argc
) {
200 if(argv
[i
][j
] == '-') j
++;
201 switch(c
= argv
[i
][j
]) {
205 name
= &argv
[i
][++j
];
206 actfile_name
= Malloc( strlen(name
)+4);
207 actfile_name
= (char *)strcpy(actfile_name
,name
);
211 strcat(actfile_name
, ".c");
212 fprintf(stdout
, "debugging file is %s\n",actfile_name
);
215 debug
[(unsigned char) c
]=1;
216 fprintf(OUT
, "option %c file %s\n",c
, &argv
[i
][j
+1]);
217 (void) strcpy(kerneldirname
,&argv
[i
][++j
]);
220 debug
[(unsigned char) c
]=1;
221 name
= &argv
[i
][++j
];
222 astringfile_name
= Malloc( strlen(name
)+4);
223 astringfile_name
= (char *)strcpy(astringfile_name
,name
);
227 strcat(astringfile_name
, ".c");
228 fprintf(OUT
, "option %c, astringfile name %s\n",c
, name
);
231 name
= &argv
[i
][++j
];
232 eventfile_h_name
= Malloc( strlen(name
)+4);
233 eventfile_h_name
= (char *)strcpy(eventfile_h_name
,name
);
237 strcat(eventfile_h_name
, ".h");
238 fprintf(stdout
, "event files is %s\n",eventfile_h_name
);
241 name
= &argv
[i
][++j
];
242 statevalfile_name
= Malloc( strlen(name
)+4 );
243 statevalfile_name
= (char *)strcpy(statevalfile_name
,name
);
247 strcat(statevalfile_name
, ".init");
248 fprintf(stdout
, "state table initial values file is %s\n",statevalfile_name
);
251 name
= &argv
[i
][++j
];
252 statefile_name
= Malloc( strlen(name
)+4);
253 statefile_name
= (char *)strcpy(statefile_name
,name
);
257 strcat(statefile_name
, ".h");
258 fprintf(stdout
, "state file is %s\n",statefile_name
);
264 debug
['X']= (int)argv
[i
][j
] - (int) '0';
265 fprintf(OUT
, "value of debug['X'] is 0x%x,%d\n", debug
['X'],
269 while((c
= argv
[i
][++j
])) {
271 fprintf(OUT
, "debugging on");
272 if(debug
['X']) fprintf(OUT
,
273 " - overrides any -%d flags used\n", debug
['X']);
275 debug
[(unsigned char) c
]=1;
276 fprintf(OUT
, "debug %c\n",c
);
281 fprintf(stdout
, "Profiling\n");
285 fprintf(stdout
, "Protocol error table:\n");
290 fprintf(stdout
, "Transitions:\n");
298 if(kerneldirname
[0]) {
302 fprintf(OUT
, "Option K overrides option X\n");
306 if(strlen(kerneldirname
)<1) {
307 fprintf(OUT
, "K option: dir name too short!\n");
311 c
= (char *) Malloc(strlen(kerneldirname
)+6) ;
313 fprintf(OUT
, "Cannot allocate %d bytes for kerneldirname\n",
314 strlen(kerneldirname
+ 6) );
315 fprintf(OUT
, "kerneldirname is %s\n", kerneldirname
);
321 (void) strcat(c
, kerneldirname
);
322 (void) strcat(c
, "/\0");
323 strcpy(kerneldirname
, c
);
331 if( !FirstEventAttribute
)
332 fprintf(eventfile_h
, "\t}ev_union;\n");
333 fprintf(eventfile_h
, "};/* end struct event */\n");
334 fprintf(eventfile_h
, "\n#define %s_NEVENTS 0x%x\n", protocol
, Nevents
);
336 "\n#define ATTR(X)ev_union.%s ## X ## \n",EV_PREFIX
);
337 (void) fclose(eventfile_h
);
339 /* {{ */ fprintf(actfile
, "\t}\nreturn 0;\n}\n"); /* end switch; end action() */
340 dump_predtable(actfile
);
342 putdriver(actfile
, 3);
345 putdriver(actfile
, 4);
347 putdriver(actfile
, 6);
350 putdriver(actfile, 10);
353 putdriver(actfile
, 11);
358 putdriver(actfile
, 7);
361 putdriver(actfile
, 13);
368 putdriver(actfile
, 8);
369 (void) fclose(actfile
);
372 fprintf(astringfile
, "};\n");
373 (void) fclose(astringfile
);
376 (void) fclose(statevalfile
);
378 fprintf(statefile
, "\n#define %s_NSTATES 0x%x\n", protocol
, Nstates
);
379 (void) fclose(statefile
);
382 fprintf(stdout
, "%d seconds\n", finish
- start
);
383 if( print_protoerrs
)
394 fprintf(stderr
, "Error at line %d\n",lineno
);
395 if(transno
) fprintf(stderr
, "Transition number %d\n",transno
);
396 (void) fflush(stdout
);
397 (void) fflush(statefile
);
398 (void) fflush(eventfile_h
);
399 (void) fflush(actfile
);
406 static char *synt
[] = {
407 "*PROTOCOL <string>\n",
408 "*PCB <string> <optional: SYNONYM synonymstring>\n",
409 "<optional: *INCLUDE {\n<C source>\n} >\n",
410 "*STATES <string>\n",
411 "*EVENTS <string>\n",
412 "*TRANSITIONS <string>\n",
418 FakeFilename(FILE *outfile
, char *name
, int l
)
422 fprintf(outfile, "\n\n\n\n# line %d \"%s\"\n", l, name);