2003-07-10 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / tools / rgenstat / rgenstat.c
1 /*
2 * Generate a file with API status information 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 #define TAG_UNKNOWN -1
35 #define TAG_IMPLEMENTED 0
36 #define TAG_UNIMPLEMENTED 1
37
38 typedef struct _API_INFO
39 {
40 struct _API_INFO *next;
41 int tag_id;
42 char name[100];
43 } API_INFO, *PAPI_INFO;
44
45
46 PAPI_INFO sort_linked_list(PAPI_INFO,
47 unsigned, int (*)(PAPI_INFO, PAPI_INFO));
48
49
50 static FILE *in;
51 static FILE *out;
52 static char *file;
53 static FILE *file_handle = NULL;
54 static char *file_buffer = NULL;
55 static unsigned int file_size = 0;
56 static int file_pointer = 0;
57 static char tagname[200];
58 static PAPI_INFO api_info_list = NULL;
59
60
61 static char*
62 convert_path(char* origpath)
63 {
64 char* newpath;
65 int i;
66
67 newpath = strdup(origpath);
68
69 i = 0;
70 while (newpath[i] != 0)
71 {
72 #ifndef WIN32
73 if (newpath[i] == '\\')
74 {
75 newpath[i] = '/';
76 }
77 #else
78 #ifdef WIN32
79 if (newpath[i] == '/')
80 {
81 newpath[i] = '\\';
82 }
83 #endif
84 #endif
85 i++;
86 }
87 return(newpath);
88 }
89
90 static void
91 write_line(char *line)
92 {
93 int n_out;
94 char buf[200];
95
96 memset(buf, 0, sizeof(buf));
97 strcpy(buf, line);
98 /* Terminate the line */
99 buf[strlen(buf)] = '\r';
100 buf[strlen(buf)] = '\n';
101
102 n_out = fwrite(&buf[0], 1, strlen(buf), out);
103 }
104
105
106 static void
107 read_file(char *filename)
108 {
109 file_handle = fopen(filename, "rb");
110 if (file_handle == NULL)
111 {
112 printf("Can't open %s\n", filename);
113 exit(1);
114 }
115
116 // Get the size of the file
117 fseek(file_handle, 0, SEEK_END);
118 file_size = ftell(file_handle);
119
120 // Load it all into memory
121 file_buffer = malloc(file_size);
122 if (file_buffer == NULL)
123 {
124 fclose(file_handle);
125 printf("Out of memory\n");
126 exit(1);
127 }
128 fseek(file_handle, 0, SEEK_SET);
129 if (file_size > 0)
130 {
131 if (fread (file_buffer, 1, file_size, file_handle) < 1)
132 {
133 fclose(file_handle);
134 printf("Read error in file %s\n", filename);
135 exit(1);
136 }
137 }
138
139 file_pointer = 0;
140 }
141
142 static void
143 close_file()
144 {
145 free(file_buffer);
146 file_buffer = NULL;
147 fclose(file_handle);
148 file_handle = NULL;
149 file_pointer = 0;
150 }
151
152 static int
153 is_whitespace(char ch)
154 {
155 if (ch == ' ')
156 {
157 return 1;
158 }
159 if (ch == '\t')
160 {
161 return 1;
162 }
163 return 0;
164 }
165
166 static int
167 is_eol_char(char ch)
168 {
169 if (ch == '\r')
170 {
171 return 1;
172 }
173 if (ch == '\n')
174 {
175 return 1;
176 }
177 return 0;
178 }
179
180 static int
181 is_end_of_tag(char ch)
182 {
183 if ((ch >= 'a') && (ch <= 'z'))
184 {
185 return 0;
186 }
187 if ((ch >= 'A') && (ch <= 'Z'))
188 {
189 return 0;
190 }
191 if ((ch >= '0') && (ch <= '9'))
192 {
193 return 0;
194 }
195 if (ch == '_')
196 {
197 return 0;
198 }
199 return 1;
200 }
201
202 static int
203 is_end_of_name(char ch)
204 {
205 /* Currently the same as is_end_of_tag() */
206 return is_end_of_tag(ch);
207 }
208
209 static int
210 is_valid_file(char *filename)
211 {
212 char ext[MAX_PATH];
213 int i;
214
215 i = strlen(filename);
216 while (i > 0 && filename[i] != '.')
217 {
218 i--;
219 }
220 if (i > 0)
221 {
222 memset(ext, 0, sizeof(ext));
223 strncpy(&ext[0], &filename[i], strlen(&filename[i]));
224
225 if ((strncmp(ext, ".c", 2) == 0) || (strncmp(ext, ".C", 2) == 0))
226 {
227 return 1;
228 }
229 }
230 return 0;
231 }
232
233 static int
234 get_tag_id(char *tag)
235 {
236 if (strcasecmp(tag, "implemented") == 0)
237 {
238 return TAG_IMPLEMENTED;
239 }
240 if (strcasecmp(tag, "unimplemented") == 0)
241 {
242 return TAG_UNIMPLEMENTED;
243 }
244 return TAG_UNKNOWN;
245 }
246
247 static int
248 skip_to_next_tag()
249 {
250 unsigned int start;
251 int end_of_tag;
252 int found_tag = 0;
253 int tag_id;
254 int len;
255
256 tagname[0] = 0;
257 while ((file_pointer < file_size) && (!found_tag))
258 {
259 if (file_buffer[file_pointer] == '@')
260 {
261 file_pointer++;
262 start = file_pointer;
263 end_of_tag = 0;
264 while ((file_pointer < file_size) && (!end_of_tag))
265 {
266 end_of_tag = is_end_of_tag(file_buffer[file_pointer]);
267 file_pointer++;
268 }
269 len = file_pointer > start ? file_pointer - start - 1 : 0;
270 strncpy(tagname, &file_buffer[start], len);
271 tagname[len] = 0;
272
273 tag_id = get_tag_id(tagname);
274 if (tag_id != TAG_UNKNOWN)
275 {
276 return tag_id;
277 }
278 }
279 file_pointer++;
280 }
281
282 return TAG_UNKNOWN;
283 }
284
285 static void
286 skip_line()
287 {
288 while ((file_pointer < file_size) && (!is_eol_char(file_buffer[file_pointer])))
289 {
290 file_pointer++;
291 }
292 if ((file_pointer < file_size) && (file_buffer[file_pointer] == '\n'))
293 {
294 file_pointer++;
295 }
296 }
297
298 static void
299 skip_comments()
300 {
301 while ((file_pointer < file_size))
302 {
303 if (file_buffer[file_pointer] == '*')
304 {
305 if ((file_pointer + 1 < file_size))
306 {
307 if (file_buffer[file_pointer + 1] == '/')
308 {
309 skip_line();
310 return;
311 }
312 }
313 }
314 file_pointer++;
315 }
316 }
317
318 static int
319 get_previous_identifier(unsigned int end, char *name)
320 {
321 unsigned int my_file_pointer = end;
322 int len;
323
324 name[0] = 0;
325
326 while ((my_file_pointer > 0) && (is_whitespace(file_buffer[my_file_pointer])))
327 {
328 my_file_pointer--;
329 }
330
331 /* Skip any comments between function name and it's parameters */
332 if ((my_file_pointer > 0) && (file_buffer[my_file_pointer] == '/'))
333 {
334 if ((my_file_pointer > 0) && (file_buffer[my_file_pointer - 1] == '*'))
335 {
336 my_file_pointer--;
337 while ((my_file_pointer > 0) && !((file_buffer[my_file_pointer] == '*')
338 && (file_buffer[my_file_pointer - 1] == '/')))
339 {
340 my_file_pointer--;
341 }
342 my_file_pointer -= 2;
343 }
344 }
345
346 /* Skip any remaining whitespace */
347 while ((my_file_pointer > 0) && (is_whitespace(file_buffer[my_file_pointer])))
348 {
349 my_file_pointer--;
350 }
351
352 end = my_file_pointer;
353 while ((my_file_pointer > 0))
354 {
355 if (is_end_of_name(file_buffer[my_file_pointer]))
356 {
357 len = end - my_file_pointer;
358 strncpy(name, &file_buffer[my_file_pointer + 1], len);
359 name[len] = 0;
360 return 1;
361 }
362 my_file_pointer--;
363 }
364
365 return 0;
366 }
367
368 static int
369 skip_to_next_name(char *name)
370 {
371 while ((file_pointer < file_size))
372 {
373 if (file_buffer[file_pointer] == '(')
374 {
375 return get_previous_identifier(file_pointer - 1, name);
376 }
377 file_pointer++;
378 }
379 return 0;
380 }
381
382 static void
383 parse_file(char *filename)
384 {
385 PAPI_INFO api_info;
386 char name[200];
387 int tag_id;
388
389 read_file(filename);
390
391 do
392 {
393 tag_id = skip_to_next_tag();
394 if (tag_id == TAG_UNKNOWN)
395 {
396 break;
397 }
398
399 /* Skip rest of the comments between the tag and the function name */
400 skip_comments();
401
402 if (skip_to_next_name(name))
403 {
404 api_info = malloc(sizeof(API_INFO));
405 if (api_info == NULL)
406 {
407 printf("Out of memory\n");
408 exit(1);
409 }
410
411 api_info->tag_id = tag_id;
412 strcpy(api_info->name, name);
413
414 api_info->next = api_info_list;
415 api_info_list = api_info;
416
417 }
418 } while (1);
419
420 close_file();
421 }
422
423 #ifdef WIN32
424
425 /* Win32 version */
426 static void
427 process_directory (char *path)
428 {
429 struct _finddata_t f;
430 int findhandle;
431 char searchbuf[MAX_PATH];
432 char buf[MAX_PATH];
433
434 printf("Processing '%s'\n", path);
435
436 strcpy(searchbuf, path);
437 strcat(searchbuf, "*.*");
438
439 findhandle =_findfirst(searchbuf, &f);
440 if (findhandle != -1)
441 {
442 do
443 {
444 if (f.attrib & _A_SUBDIR)
445 {
446 if (f.name[0] != '.')
447 {
448 strcpy(buf, path);
449 strcat(buf, f.name);
450 strcat(buf, DIR_SEPARATOR_STRING);
451 process_directory(buf);
452 }
453 continue;
454 }
455
456 strcpy(buf, path);
457 strcat(buf, f.name);
458
459 /* Must be a .c file */
460 if (!is_valid_file(buf))
461 {
462 continue;
463 }
464
465 parse_file(buf);
466 }
467 while (_findnext(findhandle, &f) == 0);
468 _findclose(findhandle);
469 }
470 else
471 {
472 printf("Cannot open directory '%s'", path);
473 exit(1);
474 }
475 }
476
477 #else
478
479 /* Linux version */
480 static void
481 process_directory (char *path)
482 {
483 DIR *dirp;
484 struct dirent *entry;
485 struct stat stbuf;
486 char buf[MAX_PATH];
487
488 #ifdef HAVE_D_TYPE
489 dirp = opendir(path);
490 if (dirp != NULL)
491 {
492 while ((entry = readdir(dirp)) != NULL)
493 {
494 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
495 continue; // skip self and parent
496
497 if (entry->d_type == DT_REG) // normal file
498 {
499 // Check for an absolute path
500 if (path[0] == DIR_SEPARATOR_CHAR)
501 {
502 strcpy(buf, path);
503 strcat(buf, DIR_SEPARATOR_STRING);
504 strcat(buf, entry->d_name);
505 }
506 else
507 {
508 getcwd(buf, sizeof(buf));
509 strcat(buf, DIR_SEPARATOR_STRING);
510 strcat(buf, path);
511 strcat(buf, entry->d_name);
512 }
513
514 if (stat(buf, &stbuf) == -1)
515 {
516 printf("Can't access '%s' (%s)\n", buf, strerror(errno));
517 return;
518 }
519
520 if (S_ISDIR(stbuf.st_mode))
521 {
522 process_directory(buf);
523 continue;
524 }
525
526 /* Must be a .c file */
527 if (!is_valid_file(buf))
528 {
529 continue;
530 }
531
532 parse_file(buf);
533 }
534 }
535 closedir(dirp);
536 }
537 else
538 {
539 printf("Can't open %s\n", path);
540 exit(1);
541 }
542
543 #else
544
545 dirp = opendir(path);
546 if (dirp != NULL)
547 {
548 while ((entry = readdir(dirp)) != NULL)
549 {
550 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
551 continue; // skip self and parent
552
553 // Check for an absolute path
554 if (path[0] == DIR_SEPARATOR_CHAR)
555 {
556 strcpy(buf, path);
557 strcat(buf, DIR_SEPARATOR_STRING);
558 strcat(buf, entry->d_name);
559 }
560 else
561 {
562 getcwd(buf, sizeof(buf));
563 strcat(buf, DIR_SEPARATOR_STRING);
564 strcat(buf, path);
565 strcat(buf, entry->d_name);
566 }
567
568 if (stat(buf, &stbuf) == -1)
569 {
570 printf("Can't access '%s' (%s)\n", buf, strerror(errno));
571 return;
572 }
573
574 if (S_ISDIR(stbuf.st_mode))
575 {
576 process_directory(buf);
577 continue;
578 }
579
580 /* Must be a .c file */
581 if (!is_valid_file(buf))
582 {
583 continue;
584 }
585
586 parse_file(buf);
587 }
588 closedir(dirp);
589 }
590 else
591 {
592 printf("Can't open %s\n", path);
593 exit(1);
594 }
595
596 #endif
597 }
598
599 #endif
600
601 /*
602 * This function compares two API entries. It returns a negative value if p is
603 * before q, or a positive value if p is after q.
604 */
605 static int
606 compare_api_order(PAPI_INFO p, PAPI_INFO q)
607 {
608 return strcmp(p->name, q->name);
609 }
610
611 static void
612 generate_xml_for_component(char *component_name)
613 {
614 PAPI_INFO api_info;
615 char buf[200];
616 int complete;
617 int total;
618 int implemented_total;
619 int unimplemented_total;
620
621 // Sort list
622 api_info_list = sort_linked_list(api_info_list, 0, compare_api_order);
623
624 implemented_total = 0;
625 unimplemented_total = 0;
626
627 api_info = api_info_list;
628 while (api_info != NULL)
629 {
630 if (api_info->tag_id == TAG_IMPLEMENTED)
631 {
632 implemented_total ++;
633 }
634 else if (api_info->tag_id == TAG_UNIMPLEMENTED)
635 {
636 unimplemented_total ++;
637 }
638
639 api_info = api_info->next;
640 }
641
642 if (implemented_total + unimplemented_total > 0)
643 {
644 complete = ((implemented_total) * 100) / (implemented_total + unimplemented_total);
645 }
646 else
647 {
648 complete = 100;
649 }
650
651 sprintf(buf, "<component name=\"%s\" complete=\"%d\" implemented_total=\"%d\" unimplemented_total=\"%d\">",
652 component_name, complete, implemented_total, unimplemented_total);
653 write_line(buf);
654
655 if (api_info_list != NULL)
656 {
657 write_line("<functions>");
658
659 api_info = api_info_list;
660 while (api_info != NULL)
661 {
662 sprintf(buf, "<function name=\"%s\" implemented=\"%s\">",
663 api_info->name, api_info->tag_id == TAG_IMPLEMENTED ? "true" : "false");
664 write_line(buf);
665 write_line("</function>");
666 api_info = api_info->next;
667 }
668
669 write_line("</functions>");
670 }
671
672 write_line("</component>");
673 }
674
675 static void
676 read_input_file(char *input_file)
677 {
678 char component_name[MAX_PATH];
679 char component_path[MAX_PATH];
680 char *canonical_path;
681 unsigned int index;
682 unsigned int start;
683 PAPI_INFO api_info;
684 PAPI_INFO next_api_info;
685 char *buffer;
686 int size;
687 int len;
688
689 in = fopen(input_file, "rb");
690 if (in == NULL)
691 {
692 printf("Cannot open input file");
693 exit(1);
694 }
695
696 // Get the size of the file
697 fseek(in, 0, SEEK_END);
698 size = ftell(in);
699
700 // Load it all into memory
701 buffer = malloc(size);
702 if (buffer == NULL)
703 {
704 fclose(in);
705 printf("Out of memory\n");
706 exit(1);
707 }
708 fseek(in, 0, SEEK_SET);
709 if (fread (buffer, 1, size, in) < 1)
710 {
711 fclose(in);
712 printf("Read error in file %s\n", input_file);
713 exit(1);
714 }
715
716 index = 0;
717
718 write_line("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>");
719 write_line("");
720 write_line("<components>");
721
722 while (1)
723 {
724 /* Free previous list */
725 for (api_info = api_info_list; api_info != NULL; api_info = next_api_info)
726 {
727 next_api_info = api_info->next;
728 free(api_info);
729 }
730 api_info_list = NULL;
731
732 /* Skip whitespace and eol characters */
733 while ((index < size) && (is_whitespace(buffer[index]) || (is_eol_char(buffer[index]))))
734 {
735 index++;
736 }
737 if ((file_pointer < size) && (buffer[index] == '\n'))
738 {
739 index++;
740 }
741
742 if (buffer[index] == ';')
743 {
744 /* Skip comments */
745 while ((index < size) && (!is_eol_char(buffer[index])))
746 {
747 index++;
748 }
749 if ((index < size) && (buffer[index] == '\n'))
750 {
751 index++;
752 }
753 continue;
754 }
755
756 /* Get component name */
757 start = index;
758 while ((index < size) && (!is_whitespace(buffer[index])))
759 {
760 index++;
761 }
762 if (index >= size)
763 {
764 break;
765 }
766
767 len = index - start;
768 strncpy(component_name, &buffer[start], len);
769 component_name[len] = 0;
770
771 /* Skip whitespace */
772 while ((index < size) && (is_whitespace(buffer[index])))
773 {
774 index++;
775 }
776 if (index >= size)
777 {
778 break;
779 }
780
781 /* Get component path */
782 start = index;
783 while ((index < size) && (!is_whitespace(buffer[index]) && !is_eol_char(buffer[index])))
784 {
785 index++;
786 }
787
788 len = index - start;
789 strncpy(component_path, &buffer[start], len);
790 component_path[len] = 0;
791
792 /* Append directory separator if needed */
793 if (component_path[strlen(component_path)] != DIR_SEPARATOR_CHAR)
794 {
795 int i = strlen(component_path);
796 component_path[strlen(component_path)] = DIR_SEPARATOR_CHAR;
797 component_path[i + 1] = 0;
798 }
799
800 /* Skip to end of line */
801 while ((index < size) && (!is_eol_char(buffer[index])))
802 {
803 index++;
804 }
805 if ((index < size) && (buffer[index] == '\n'))
806 {
807 index++;
808 }
809
810 canonical_path = convert_path(component_path);
811 if (canonical_path != NULL)
812 {
813 process_directory(canonical_path);
814 free(canonical_path);
815 generate_xml_for_component(component_name);
816 }
817 }
818
819 write_line("</components>");
820 }
821
822 static char HELP[] =
823 "RGENSTAT input-filename output-filename\n"
824 "\n"
825 " input-filename File containing list of components to process\n"
826 " output-filename File to create\n";
827
828 int main(int argc, char **argv)
829 {
830 char *input_file;
831 char *output_file;
832 int i;
833
834 if (argc < 2)
835 {
836 puts(HELP);
837 return 1;
838 }
839
840 input_file = convert_path(argv[1]);
841 if (input_file[0] == 0)
842 {
843 printf("Missing input-filename\n");
844 return 1;
845 }
846
847 output_file = convert_path(argv[2]);
848 if (output_file[0] == 0)
849 {
850 printf("Missing output-filename\n");
851 return 1;
852 }
853
854 out = fopen(output_file, "wb");
855 if (out == NULL)
856 {
857 printf("Cannot open output file");
858 return 1;
859 }
860
861 read_input_file(input_file);
862
863 fclose(out);
864
865 return 0;
866 }
867
868 /* EOF */