2003-07-11 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / tools / regtests.c
1 /*
2 * Generate a file with test registrations from a list
3 * of files in a directory.
4 * Casper S. Hornstrup <chorns@users.sourceforge.net>
5 */
6
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #ifdef WIN32
14 #include <io.h>
15 #include <dos.h>
16 #else
17 #include <sys/io.h>
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <dirent.h>
21 #endif
22 #include <ctype.h>
23 #ifndef WIN32
24 #ifndef MAX_PATH
25 #define MAX_PATH 260
26 #endif
27 #define DIR_SEPARATOR_CHAR '/'
28 #define DIR_SEPARATOR_STRING "/"
29 #else
30 #define DIR_SEPARATOR_CHAR '\\'
31 #define DIR_SEPARATOR_STRING "\\"
32 #endif
33
34 static FILE *out;
35 static char *path;
36 static char *file;
37
38 char* convert_path(char* origpath)
39 {
40 char* newpath;
41 int i;
42
43 newpath = strdup(origpath);
44
45 i = 0;
46 while (newpath[i] != 0)
47 {
48 #ifndef WIN32
49 if (newpath[i] == '\\')
50 {
51 newpath[i] = '/';
52 }
53 #else
54 #ifdef WIN32
55 if (newpath[i] == '/')
56 {
57 newpath[i] = '\\';
58 }
59 #endif
60 #endif
61 i++;
62 }
63 return(newpath);
64 }
65
66 static void write_line(char *line)
67 {
68 int n_out;
69 char buf[200];
70
71 memset(buf, 0, sizeof(buf));
72 strcpy(buf, line);
73 /* Terminate the line */
74 buf[strlen(buf)] = '\r';
75 buf[strlen(buf)] = '\n';
76
77 n_out = fwrite(&buf[0], 1, strlen(buf), out);
78 }
79
80 void register_test(char *filename, int prototype)
81 {
82 char ext[100];
83 char testname[100];
84 char call[100];
85 char regtest[100];
86 int i, j;
87
88 strcpy(testname, filename);
89
90 i = strlen(testname);
91 while (i > 0 && testname[i] != '.')
92 {
93 i--;
94 }
95 if (i > 0)
96 {
97 memset(ext, 0, sizeof(ext));
98 strncpy(&ext[0], &testname[i], strlen(&testname[i]));
99
100 if ((strncmp(ext, ".c", 2) != 0) && (strncmp(ext, ".C", 2) != 0))
101 {
102 return;
103 }
104
105 testname[i] = 0;
106 }
107 else
108 {
109 return;
110 }
111
112 // Make a capital first letter and make all other letters lower case
113 testname[0] = toupper(testname[0]);
114 if (!((testname[0] >= 'A' && testname[0] <= 'Z') ||
115 (testname[0] >= '0' && testname[0] <= '9')))
116 {
117 testname[0] = '_';
118 }
119 j = 1;
120 while (j < strlen(testname))
121 {
122 testname[j] = tolower(testname[j]);
123 if (!((testname[j] >= 'a' && testname[j] <= 'z') ||
124 (testname[j] >= '0' && testname[j] <= '9')))
125 {
126 testname[j] = '_';
127 }
128 j++;
129 }
130
131 if (prototype)
132 {
133 sprintf(regtest, "extern int %sTest(int Command, char *Buffer);", testname);
134 write_line(regtest);
135 }
136 else
137 {
138 sprintf(call, "%sTest", testname);
139 sprintf(regtest, " AddTest((TestRoutine)%s);", call);
140 write_line(regtest);
141 }
142 }
143
144 #ifdef WIN32
145
146 /* Win32 version */
147
148 static void
149 make_file_list (int prototype)
150 {
151 struct _finddata_t f;
152 int findhandle;
153 char searchbuf[MAX_PATH];
154
155 strcpy(searchbuf, path);
156 strcpy(searchbuf, "*.*");
157 findhandle =_findfirst(searchbuf, &f);
158 if (findhandle != -1)
159 {
160 do
161 {
162 if (f.attrib & _A_SUBDIR)
163 {
164 /* Skip subdirectories */
165 continue;
166 }
167
168 register_test(f.name, prototype);
169 }
170 while (_findnext(findhandle, &f) == 0);
171 _findclose(findhandle);
172 }
173 }
174
175 #else
176
177 /* Linux version */
178 static void
179 make_file_list (int prototype)
180 {
181 DIR *dirp;
182 struct dirent *entry;
183 struct stat stbuf;
184 char buf[MAX_PATH];
185
186 #ifdef HAVE_D_TYPE
187 dirp = opendir(path);
188 if (dirp != NULL)
189 {
190 while ((entry = readdir(dirp)) != NULL)
191 {
192 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
193 continue; // skip self and parent
194
195 if (entry->d_type == DT_REG) // normal file
196 {
197 // Check for an absolute path
198 if (path[0] == DIR_SEPARATOR_CHAR)
199 {
200 strcpy(buf, path);
201 strcat(buf, DIR_SEPARATOR_STRING);
202 strcat(buf, entry->d_name);
203 }
204 else
205 {
206 getcwd(buf, sizeof(buf));
207 strcat(buf, DIR_SEPARATOR_STRING);
208 strcat(buf, path);
209 strcat(buf, entry->d_name);
210 }
211
212 if (stat(buf, &stbuf) == -1)
213 {
214 printf("Can't access '%s' (%s)\n", buf, strerror(errno));
215 return;
216 }
217
218 if (S_ISDIR(stbuf.st_mode))
219 {
220 /* Skip subdirectories */
221 continue;
222 }
223
224 register_test(entry->d_name, prototype);
225 }
226 }
227 closedir(dirp);
228 }
229 else
230 {
231 printf("Can't open %s\n", path);
232 return;
233 }
234
235 #else
236
237 dirp = opendir(path);
238 if (dirp != NULL)
239 {
240 while ((entry = readdir(dirp)) != NULL)
241 {
242 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
243 continue; // skip self and parent
244
245 // Check for an absolute path
246 if (path[0] == DIR_SEPARATOR_CHAR)
247 {
248 strcpy(buf, path);
249 strcat(buf, DIR_SEPARATOR_STRING);
250 strcat(buf, entry->d_name);
251 }
252 else
253 {
254 getcwd(buf, sizeof(buf));
255 strcat(buf, DIR_SEPARATOR_STRING);
256 strcat(buf, path);
257 strcat(buf, entry->d_name);
258 }
259
260 if (stat(buf, &stbuf) == -1)
261 {
262 printf("Can't access '%s' (%s)\n", buf, strerror(errno));
263 return;
264 }
265
266 if (S_ISDIR(stbuf.st_mode))
267 {
268 /* Skip subdirectories */
269 continue;
270 }
271
272 register_test(entry->d_name, prototype);
273 }
274 closedir(dirp);
275 }
276 else
277 {
278 printf("Can't open %s\n", path);
279 return;
280 }
281
282 #endif
283 }
284
285 #endif
286
287 static char HELP[] =
288 "REGTESTS path file\n"
289 "\n"
290 " path Path to files\n"
291 " file File to create\n";
292
293 int main(int argc, char **argv)
294 {
295 char buf[MAX_PATH];
296 int i;
297
298 if (argc < 2)
299 {
300 puts(HELP);
301 return 1;
302 }
303
304
305 strcpy(buf, convert_path(argv[1]));
306 if (buf[strlen(buf)] != DIR_SEPARATOR_CHAR)
307 {
308 int i = strlen(buf);
309 buf[strlen(buf)] = DIR_SEPARATOR_CHAR;
310 buf[i + 1] = 0;
311 }
312 path = buf;
313 if (path[0] == 0)
314 {
315 printf("Missing path\n");
316 return 1;
317 }
318
319 file = convert_path(argv[2]);
320 if (file[0] == 0)
321 {
322 printf("Missing file\n");
323 return 1;
324 }
325
326 out = fopen(file, "wb");
327 if (out == NULL)
328 {
329 perror("Cannot open output file");
330 return 1;
331 }
332
333 write_line("/* This file is autogenerated. */");
334 write_line("");
335 write_line("typedef int (*TestRoutine)(int Command, char *Buffer);");
336 write_line("");
337
338 make_file_list(1);
339
340 write_line("");
341 write_line("extern void AddTest(TestRoutine Routine);");
342 write_line("");
343 write_line("void RegisterTests()");
344 write_line("{");
345
346 make_file_list(0);
347
348 write_line("}");
349
350 fclose(out);
351
352 return 0;
353 }
354
355 /* EOF */