1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - tools - init2env
3 File: init.y
4
5 Copyright 2005-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Date:: 2008-09-18#$
14 $Rev: 8573 $
15 $Author: okubata_ryoma $
16 *---------------------------------------------------------------------------*/
17 %{
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <getopt.h>
23
24 int init_yylex(void);
25 void yyerror(char* s);
26
27 void SetResourceName( char* name);
28
29
30 FILE *yyout;
31 FILE *yybinary;
32 char *class_name;
33
34 int line_count = 1;
35
36 //resource list
37 typedef struct ResourceList ResourceList;
38 struct ResourceList
39 {
40 char* resname;
41 };
42 ResourceList reslist[8]; // 8 = ENV_RESOURCE_SET_MAX
43 int rescount = 0;
44 %}
45 %union {
46 int number;
47 char* letter;
48 }
49 %token L_BRACKET R_BRACKET D_QUOTATION L_INEQUALITY R_INEQUALITY
50 %token <letter> LETTER TYPENUM TYPESTR TYPEBIN
51
52 %%
53
54 envs: /* empty */
55 | envs env
56 ;
57
58 env:
59 envcore classes envend
60 ;
61
62 envend:
63 {
64 fprintf(yyout, " ENV_RESOURCE_END\n");
65 fprintf(yyout, "};\n\n");
66 }
67 ;
68
69 envcore:
70 L_INEQUALITY LETTER R_INEQUALITY
71 {
72 fprintf(yyout, "static ENVResource %s[] = {\n", $2);
73
74 SetResourceName($2);
75 }
76 ;
77
78 classes: /* empty */
79 | classes class
80 ;
81
82 class:
83 classheader resources
84 ;
85
86 classheader:
87 L_BRACKET LETTER R_BRACKET
88 {
89 class_name = strdup($2);
90 }
91 ;
92
93 resources: /* empty */
94 | resources resource
95 ;
96
97 resource:
98 LETTER TYPENUM LETTER
99 {
100 fprintf(yyout, " \"%s.%s\",\t", class_name, $1);
101 fprintf(yyout, "ENV_%s", $2);
102 fprintf(yyout, "(%s),\n", $3);
103 }
104 | LETTER TYPESTR LETTER
105 {
106 fprintf(yyout, " \"%s.%s\",\t", class_name, $1);
107 fprintf(yyout, "ENV_%s", $2);
108 fprintf(yyout, "(\"%s\"),\n", $3);
109 }
110 | LETTER TYPEBIN LETTER
111 {
112 fprintf(yyout, " \"%s.%s\",\t", class_name, $1);
113 fprintf(yyout, "ENV_%s", $2);
114 fprintf(yyout, "(\"%s\"),\n", $3);
115 }
116 | LETTER TYPEBIN D_QUOTATION LETTER D_QUOTATION
117 {
118 int c;
119
120 fprintf(yyout, " \"%s.%s\",\t", class_name, $1);
121 fprintf(yyout, "ENV_%s", $2);
122 fprintf(yyout, "(\"");
123 yybinary = fopen($4, "r");
124 if(yybinary == NULL)
125 {
126 fprintf(stderr, "file \"%s\" open error!\n", $4);
127 exit(1);
128 }
129 while( (c = getc(yybinary)) != EOF)
130 {
131 fprintf(yyout, "%c", c);
132 }
133 if( fclose(yybinary) == EOF )
134 {
135 fprintf(stderr, "fclose ERROR!\n");
136 exit(1);
137 }
138 fprintf(yyout, "\"),\n");
139 }
140 ;
141
142 %%
143 void SetResourceName(char* name)
144 {
145 reslist[rescount].resname = (char *)strdup(name);
146 rescount++;
147 }
148
init_yywrap()149 int init_yywrap()
150 {
151 return 1;
152 }
153
154
displayMessage(char * message[])155 void displayMessage( char* message[] )
156 {
157 int n;
158 //---- show help messages
159 for( n=0; message[n]; n++ )
160 {
161 printf( message[n] );
162 }
163 }
164
displayUsage(void)165 void displayUsage( void )
166 {
167 char* usageString[] = {
168 "Usage: parser [OPTION]... RESOURCE_FILE\n",
169 "\n",
170 "Options:\n",
171 " -o : Output C filename.\n",
172 0
173 };
174
175 displayMessage( usageString );
176 }
177
178
main(int argc,char * argv[])179 int main(int argc, char *argv[])
180 {
181 extern int yyparse(void);
182 extern FILE *init_yyin;
183
184 int c,n;
185
186 char *input_filename = NULL;
187 char *output_filename = NULL;
188
189 while(1)
190 {
191 c = getopt(argc, argv, "s:o:n:");
192
193 if(c == -1)
194 {
195 break;
196 }
197
198 switch(c)
199 {
200 case 'o':
201 printf("output = %s\n", optarg);
202 output_filename = strdup(optarg);
203 break;
204 default:
205 displayUsage();
206 exit(1);
207 }
208 }
209 //---- input filename
210 if(argv[optind] == NULL)
211 {
212 displayUsage();
213 exit(1);
214 }
215 input_filename = strdup(argv[optind]);
216 optind++;
217
218 if(output_filename == NULL)
219 {
220 //output_filename = strdup(input_filename);
221 //strcat(output_filename, ".c");
222 output_filename = strdup("./result.c");
223 printf("output = %s\n", output_filename);
224 }
225
226 yyout = fopen(output_filename, "w");
227 if(yyout == NULL)
228 {
229 fprintf(stderr, "fopen ERROR!\n");
230 exit(1);
231 }
232
233 fprintf(yyout, "#include <nitro.h>\n");
234
235 while(1)
236 {
237 //yyin = stdin;
238 init_yyin = fopen(input_filename, "r");
239
240 if (yyparse())
241 {
242 fprintf(stderr, "\n");
243 exit(1);
244 }
245
246 fprintf(yyout, "ENVResource* resourceArray[]=\n{\n ");
247 for(n = 0; n < rescount; n++)
248 {
249 fprintf(yyout, " %s,",reslist[n].resname);
250 }
251 fprintf(yyout, " NULL\n");
252 fprintf(yyout, "};\n");
253
254 //---- input filename
255 if(argv[optind] == NULL)
256 {
257 break;
258 }
259 input_filename = strdup(argv[optind]);
260 optind++;
261 }
262
263 if( fclose(yyout) == EOF )
264 {
265 fprintf(stderr, "fclose ERROR!\n");
266 exit(1);
267 }
268
269 printf("finish!!\n");
270
271 return 1;
272 }
273
yyerror(char * s)274 void yyerror(char* s)
275 {
276 printf("%s line %d\n", s, line_count);
277 printf("near %s\n", init_yylval.letter);
278 }