1/*	$NetBSD: putdriver.c,v 1.8 2005/12/11 12:25:16 christos Exp $	*/
2
3/*
4 * This code is such a kludge that I don't want to put my name on it.
5 * It was a ridiculously fast hack and needs rewriting.
6 * However it does work...
7 */
8
9/* The original idea was to put all the driver code
10 * in one place so it would be easy to modify
11 * but as hacks got thrown in it got worse and worse...
12 * It's to the point where a user would be better off
13 * writing his own driver and xebec should JUST produce
14 * the tables.
15 */
16
17#include <sys/cdefs.h>
18__KERNEL_RCSID(0, "$NetBSD: putdriver.c,v 1.8 2005/12/11 12:25:16 christos Exp $");
19
20#include <stdio.h>
21#include "main.h"
22#include "debug.h"
23
24extern char protocol[];
25char Eventshiftstring[10];
26static char statename[] = {'_', 's', 't', 'a', 't', 'e', 0 };
27
28static char *strings[] = {
29
30#define PART1 { 0,3 }
31
32	"\n#include \"",
33	kerneldirname,
34	protocol,
35	"_states.h\"",
36	(char *)0,
37	(char *)0,
38	(char *)0,
39	(char *)0,
40	(char *)0,
41	(char *)0,
42
43#define PART12 { 10,12 }
44	"\n\nstatic struct act_ent {\n",
45	"\tint a_newstate;\n\tint a_action;\n",
46	"} statetable[] = { {0,0},\n",
47	(char *)0,
48	(char *)0,
49	(char *)0,
50	(char *)0,
51	(char *)0,
52	(char *)0,
53	(char *)0,
54
55#define PART2 { 20,20 }
56	"};\n",
57	(char *)0,
58	(char *)0,
59	(char *)0,
60	(char *)0,
61	(char *)0,
62	(char *)0,
63	(char *)0,
64	(char *)0,
65	(char *)0,
66
67#define PART3 { 30,41 }
68	"\n",
69	protocol,
70	"_driver(p, e)\nregister ",
71	protocol,
72	PCBNAME,
73	" *p;\nregister struct ",
74	protocol,
75	"_event *e;\n",
76	"{\n",
77		"\tregister int index, error=0;\n",
78		"\tstruct act_ent *a;\n",
79		"\tstatic struct act_ent erroraction = {0,-1};\n",
80	(char *)0,
81	(char *)0,
82	(char *)0,
83	(char *)0,
84	(char *)0,
85	(char *)0,
86	(char *)0,
87	(char *)0,
88
89#define PART4 { 50,54 }
90
91	"\textern int ",
92	protocol,
93	"_debug;\n\textern FILE *",
94	protocol,
95	"_astringfile;\n",
96	(char *)0,
97	(char *)0,
98	(char *)0,
99	(char *)0,
100	(char *)0,
101
102#define PART6 { 60, 65 }
103	"\n\tindex = inx[1 + e->ev_number][p->",
104		protocol,
105		statename,
106		"];\n\tif(index<0) index=_Xebec_index(e, p);\n",
107		"\tif (index==0) {\n\t\ta = &erroraction;\n",
108		"\t} else\n\t\ta = &statetable[index];\n\n",
109	(char *)0,
110	(char *)0,
111	(char *)0,
112	(char *)0,
113
114#define PART7 {70, 77 }
115	"\tif(",
116	protocol,
117	"_debug) fprintf(",
118	protocol,
119	"_astringfile, \"%15s <-- %15s [%15s] \\n\\t%s\\n\",\n",
120	"\t\tsstring[a->a_newstate], sstring[p->",
121	protocol,
122	"_state], estring[e->ev_number], astring[a->a_action]);\n\n",
123	(char *)0,
124	(char *)0,
125
126#define PART8 { 80, 84 }
127		"\tif(a->a_action)\n",
128		"\t\terror = _Xebec_action( a->a_action, e, p );\n",
129		"\tif(error==0)\n\tp->",
130		protocol,
131		"_state = a->a_newstate;\n\treturn error;\n}\n",
132	(char *)0,
133	(char *)0,
134	(char *)0,
135	(char *)0,
136	(char *)0,
137
138#define PART9 { 90, 99 }
139	"\n_XEBEC_PG int _Xebec_action(a,e,p)\nint a;\nstruct ",
140	protocol,
141	"_event *e;\n",
142	protocol,
143	PCBNAME,
144	" *p;\n{\n",
145	"switch(a) {\n",
146	"case -1:  return ",
147	protocol,
148	"_protocol_error(e,p);\n",
149	(char *)0,
150
151#define PART10 { 101, 105 }
152	"\tif(",
153	protocol,
154	"_debug) fprintf(",
155	protocol,
156	"_astringfile, \"index 0x%5x\\n\", index);\n",
157	(char *)0,
158	(char *)0,
159	(char *)0,
160	(char *)0,
161
162#define PART5 { 110, 121 }
163	"\n_XEBEC_PG int\n_Xebec_index( e,p )\n",
164	"\tstruct ",
165	protocol,
166	"_event *e;\n\t",
167	protocol,
168	PCBNAME,
169	" *p;\n{\nswitch( (e->ev_number<<",
170	Eventshiftstring,
171	")+(p->",
172	protocol,
173	statename,
174	") ) {\n",
175	(char *)0,
176	(char *)0,
177	(char *)0,
178	(char *)0,
179	(char *)0,
180	(char *)0,
181	(char *)0,
182	(char *)0,
183
184#define PART11 {130, 137 }
185	"\tIFTRACE(D_DRIVER)\n",
186	"\t",
187	protocol,
188	"trace(DRIVERTRACE,",
189	"\t\ta->a_newstate, p->",
190	protocol,
191	"_state, e->ev_number, a->a_action, 0);\n\n",
192	"\tENDTRACE\n",
193	(char *)0,
194	(char *)0,
195
196#define PART13 {140, 147 }
197	"\tif(",
198	protocol,
199	"_debug) fprintf(",
200	protocol,
201	"_astringfile, \"%15s <-- %15s [%15s] \\n\",\n",
202	"\t\tsstring[a->a_newstate], sstring[p->",
203	protocol,
204	"_state], estring[e->ev_number]);\n\n",
205	(char *)0,
206	(char *)0,
207
208#define PART14 { 150,150 }
209	"#define _XEBEC_PG static\n",
210
211#define PART15 { 151,151 }
212	"#define _XEBEC_PG  \n",
213
214};
215
216static struct { int start; int finish; } parts[] = {
217	{ 0,0 },
218	PART1,
219	PART2,
220	PART3,
221	PART4,
222	PART5,
223	PART6,
224	PART7,
225	PART8,
226	PART9,
227	PART10,
228	PART11,
229	PART12,
230	PART13,
231	PART14,
232	PART15,
233};
234
235void
236putdriver(FILE *f, int x)
237{
238	register int i;
239
240	for( i = parts[x].start; i<= parts[x].finish; i++)
241		fprintf(f, "%s", strings[i]);
242	IFDEBUG(d)
243		fflush(f);
244	ENDDEBUG
245}
246