1 /*
2    hsf2RC
3 
4    Copyright (C) 2002 Lo�c YHUEL (hwti)
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
19 */
20 
21 //#define WIN32_LEAN_AND_MEAN		// Exclure les en-t�tes Windows rarement utilis�s
22 #include <stdio.h>
23 #include <io.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <malloc.h>
27 
28 void WriteInfo(char *str1,char *str2);
29 
30 FILE *os,*doorsos,*alias;
31 bool MakeDB92INFO;
32 char *(*table)[2000];
33 int Highest=-1;
34 
35 
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38 	_finddata_t data;
39 	long file, folder;
40  	FILE *f1,*db92;
41 	char dummy[100],dummy2[100],path[50];
42 	char * s,* c;
43 	int b;
44 
45 	puts("Builds os.h, doorsos.h [and db92 rom_call info] from TIGCC sources\nhsf2RC [/db92]");
46 	if ((argc > 1) && (!(stricmp("/db92",argv[1])))) {
47 		MakeDB92INFO = true;
48 		if (!(table = (char*(*)[2000])calloc(2000,sizeof(char *)))) {
49 			puts("Out of memory");
50 			return 1;
51 		}
52 		db92 = fopen("Romcalls.bin","wt");
53 	}
54 	else MakeDB92INFO = false;
55 
56 	folder = _findfirst("*.h",&data);
57 	if (folder == -1) {
58 		puts("No directory found. Run hsf2rc in doc\\system\\include directory of TIGCC sources.");
59 		return 1;
60 	}
61 
62 	os = fopen("os.h","wt");
63 	doorsos = fopen("doorsos.h","wt");
64 	alias = fopen("db92_alias","a+t");
65 
66 	do {
67 		if (!(data.attrib && _A_SUBDIR)) continue;
68 		strcpy(path,data.name);
69 		strcat(path,"\\");
70 		c = strchr(path,0);
71 		strcpy(c,"*.hsf");
72 		file = _findfirst(path,&data);
73 		if (file == -1) continue;
74 		do {
75 			strcpy(c,data.name);
76 			f1 = fopen(path, "rt");
77 			do {
78 				if (!fgets(dummy,100,f1)) goto nextfile;
79 			} while (strncmp("Name=",dummy,5));
80 			do {
81 				if (!fgets(dummy2,100,f1)) goto nextfile;
82 			} while (strcmp("[ROM Call]\n",dummy2));
83 			fgets(dummy2,100,f1);
84 			*strchr(dummy+5,'\n') = 0;
85 			WriteInfo(dummy+5,dummy2);
86 	nextfile :
87 			fclose(f1);
88 		} while (!_findnext(file,&data));
89 		_findclose(file);
90 	} while (!_findnext(folder,&data));
91 	_findclose(folder);
92 
93 	if (f1 = fopen("notdefinedrom_calls","rt")) {
94 		while (fgets(dummy,100,f1)) {
95 			if (dummy[0] == ';') continue;
96 			*(s = strchr(dummy,' ')) = 0;
97 			WriteInfo(dummy,s+1);
98 		}
99 		fclose(f1);
100 	}
101 
102 	if (f1 = fopen("os.h.end","rt")) {
103 		while (fgets(dummy,100,f1)) fputs(dummy,os);
104 		fclose(f1);
105 	}
106 
107 	if (f1 = fopen("doorsos.h.end","rt")) {
108 		while (fgets(dummy,100,f1)) fputs(dummy,doorsos);
109 		fclose(f1);
110 	}
111 
112 	fclose(os);
113 	fclose(doorsos);
114 
115 	if (MakeDB92INFO) {
116 		for (b = 0;b <= Highest;b++) {
117 			if (!((*table)[b] == 0)) {
118 				fputs((*table)[b],db92);
119 				free((*table)[b]);
120 			}
121 			else printf("Warning : ROM_CALL %i not defined\n",b);
122 			fputc(0,db92);
123 		}
124 		f1 = fopen("romcalls.h","w");
125 		fprintf(f1,";Include file automatically generated by hsf2RC.\n"
126 		";Copyright (C) 2002 Lo�c YHUEL (hwti)\n"
127 		"RCNum equ %i\n"
128 		"ROM_CALLs_Names\n"
129 		" incbin romcalls.bin",b-1);
130 		fclose(f1);
131 		free(table);
132 		fclose(db92);
133 	}
134 	fclose(alias);
135 	return 0;
136 }
137 
WriteInfo(char * str1,char * str2)138 void WriteInfo(char *str1,char *str2)
139 {
140 	int a,b;
141 	char dummy[100];
142 
143 	str2 = strchr(str2,'$');
144 	fprintf(os,"%s\t\tequ\t%s",str1,str2);
145 	fprintf(doorsos,"doorsos::%s\tequ\t_ROM_CALL_%s",str1,str2 + 1);
146 	if (MakeDB92INFO) {
147 		str2 = strchr(str2,0) - 1;
148 		if (*str2 == '\n') str2--;
149 		a = 1;
150 		b = 0;
151 		while (1) {
152 			if ((*str2 >= '0') && (*str2 <= '9')) b += a * (*str2 - '0');
153 			else if ((*str2 >= 'A') && (*str2 <= 'F')) b += a * (*str2 - 'A' + 10);
154 			else break;
155 			a *= 16;
156 			str2--;
157 		}
158 
159 		if (!((*table)[b] == NULL)) {
160 			if (alias) {
161 				rewind(alias);
162 				while (fgets(dummy,100,alias)) {
163 					if ((atoi(dummy) == b)) {
164 						if (*(strchr(dummy,' ')+1) == 'n') return;
165 						else goto overw;
166 					}
167 				}
168 			}
169 			printf("ROM_CALL %i is already named %s. Name it %s ?",b,(*table)[b],str1);
170 			do {
171 				a = getchar();
172 				if (a == 'n') {
173 					if (alias) fprintf(alias,"%i n\n",b);
174 					return;
175 				}
176 			} while (!(a == 'y'));
177 			if (alias) fprintf(alias,"%i y\n",b);
178 			}
179 
180 overw :
181 		if (!((*table)[b] = (char *)realloc((*table)[b],strlen(str1)+1))) {
182 			puts("Out of memory");
183 			exit(1);
184 		}
185 
186 		strcpy((*table)[b],str1);
187 		if (b > Highest) Highest = b;
188 	}
189 }
190