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 printf("Name '%s' File '%s'\n", name, filename);
415
416 api_info->next = api_info_list;
417 api_info_list = api_info;
418
419 }
420 } while (1);
421
422 close_file();
423 }
424
425 #ifdef WIN32
426
427 /* Win32 version */
428 static void
429 process_directory (char *path)
430 {
431 struct _finddata_t f;
432 int findhandle;
433 char searchbuf[MAX_PATH];
434 char buf[MAX_PATH];
435
436 printf("Processing '%s'\n", path);
437
438 strcpy(searchbuf, path);
439 strcat(searchbuf, "*.*");
440
441 findhandle =_findfirst(searchbuf, &f);
442 if (findhandle != -1)
443 {
444 do
445 {
446 if (f.attrib & _A_SUBDIR)
447 {
448 if (f.name[0] != '.')
449 {
450 strcpy(buf, path);
451 strcat(buf, f.name);
452 strcat(buf, DIR_SEPARATOR_STRING);
453 process_directory(buf);
454 }
455 continue;
456 }
457
458 strcpy(buf, path);
459 strcat(buf, f.name);
460
461 /* Must be a .c file */
462 if (!is_valid_file(buf))
463 {
464 continue;
465 }
466
467 parse_file(buf);
468 }
469 while (_findnext(findhandle, &f) == 0);
470 _findclose(findhandle);
471 }
472 else
473 {
474 printf("Cannot open directory '%s'", path);
475 exit(1);
476 }
477 }
478
479 #else
480
481 /* Linux version */
482 static void
483 process_directory (char *path)
484 {
485 DIR *dirp;
486 struct dirent *entry;
487 struct stat stbuf;
488 char buf[MAX_PATH];
489
490 #ifdef HAVE_D_TYPE
491 dirp = opendir(path);
492 if (dirp != NULL)
493 {
494 while ((entry = readdir(dirp)) != NULL)
495 {
496 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
497 continue; // skip self and parent
498
499 if (entry->d_type == DT_REG) // normal file
500 {
501 // Check for an absolute path
502 if (path[0] == DIR_SEPARATOR_CHAR)
503 {
504 strcpy(buf, path);
505 strcat(buf, DIR_SEPARATOR_STRING);
506 strcat(buf, entry->d_name);
507 }
508 else
509 {
510 getcwd(buf, sizeof(buf));
511 strcat(buf, DIR_SEPARATOR_STRING);
512 strcat(buf, path);
513 strcat(buf, entry->d_name);
514 }
515
516 if (stat(buf, &stbuf) == -1)
517 {
518 printf("Can't access '%s' (%s)\n", buf, strerror(errno));
519 return;
520 }
521
522 if (S_ISDIR(stbuf.st_mode))
523 {
524 process_directory(buf);
525 continue;
526 }
527
528 /* Must be a .c file */
529 if (!is_valid_file(buf))
530 {
531 continue;
532 }
533
534 parse_file(buf);
535 }
536 }
537 closedir(dirp);
538 }
539 else
540 {
541 printf("Can't open %s\n", path);
542 exit(1);
543 }
544
545 #else
546
547 dirp = opendir(path);
548 if (dirp != NULL)
549 {
550 while ((entry = readdir(dirp)) != NULL)
551 {
552 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
553 continue; // skip self and parent
554
555 // Check for an absolute path
556 if (path[0] == DIR_SEPARATOR_CHAR)
557 {
558 strcpy(buf, path);
559 strcat(buf, DIR_SEPARATOR_STRING);
560 strcat(buf, entry->d_name);
561 }
562 else
563 {
564 getcwd(buf, sizeof(buf));
565 strcat(buf, DIR_SEPARATOR_STRING);
566 strcat(buf, path);
567 strcat(buf, entry->d_name);
568 }
569
570 if (stat(buf, &stbuf) == -1)
571 {
572 printf("Can't access '%s' (%s)\n", buf, strerror(errno));
573 return;
574 }
575
576 if (S_ISDIR(stbuf.st_mode))
577 {
578 process_directory(buf);
579 continue;
580 }
581
582 /* Must be a .c file */
583 if (!is_valid_file(buf))
584 {
585 continue;
586 }
587
588 parse_file(buf);
589 }
590 closedir(dirp);
591 }
592 else
593 {
594 printf("Can't open %s\n", path);
595 exit(1);
596 }
597
598 #endif
599 }
600
601 #endif
602
603 /*
604 * This function compares two API entries. It returns a negative value if p is
605 * before q, or a positive value if p is after q.
606 */
607 static int
608 compare_api_order(PAPI_INFO p, PAPI_INFO q)
609 {
610 return strcmp(p->name, q->name);
611 }
612
613 static void
614 generate_xml_for_component(char *component_name)
615 {
616 PAPI_INFO api_info;
617 char buf[200];
618 int complete;
619 int total;
620 int implemented_total;
621 int unimplemented_total;
622
623 // Sort list
624 api_info_list = sort_linked_list(api_info_list, 0, compare_api_order);
625
626 implemented_total = 0;
627 unimplemented_total = 0;
628
629 api_info = api_info_list;
630 while (api_info != NULL)
631 {
632 if (api_info->tag_id == TAG_IMPLEMENTED)
633 {
634 implemented_total ++;
635 }
636 else if (api_info->tag_id == TAG_UNIMPLEMENTED)
637 {
638 unimplemented_total ++;
639 }
640
641 api_info = api_info->next;
642 }
643
644 if (implemented_total + unimplemented_total > 0)
645 {
646 complete = ((implemented_total) * 100) / (implemented_total + unimplemented_total);
647 }
648 else
649 {
650 complete = 100;
651 }
652
653 sprintf(buf, "<component name=\"%s\" complete=\"%d\" implemented_total=\"%d\" unimplemented_total=\"%d\">",
654 component_name, complete, implemented_total, unimplemented_total);
655 write_line(buf);
656
657 if (api_info_list != NULL)
658 {
659 write_line("<functions>");
660
661 api_info = api_info_list;
662 while (api_info != NULL)
663 {
664 sprintf(buf, "<function name=\"%s\" implemented=\"%s\">",
665 api_info->name, api_info->tag_id == TAG_IMPLEMENTED ? "true" : "false");
666 write_line(buf);
667 write_line("</function>");
668 api_info = api_info->next;
669 }
670
671 write_line("</functions>");
672 }
673
674 write_line("</component>");
675 }
676
677 static void
678 read_input_file(char *input_file)
679 {
680 char component_name[MAX_PATH];
681 char component_path[MAX_PATH];
682 char *canonical_path;
683 unsigned int index;
684 unsigned int start;
685 PAPI_INFO api_info;
686 PAPI_INFO next_api_info;
687 char *buffer;
688 int size;
689 int len;
690
691 in = fopen(input_file, "rb");
692 if (in == NULL)
693 {
694 printf("Cannot open input file");
695 exit(1);
696 }
697
698 // Get the size of the file
699 fseek(in, 0, SEEK_END);
700 size = ftell(in);
701
702 // Load it all into memory
703 buffer = malloc(size);
704 if (buffer == NULL)
705 {
706 fclose(in);
707 printf("Out of memory\n");
708 exit(1);
709 }
710 fseek(in, 0, SEEK_SET);
711 if (fread (buffer, 1, size, in) < 1)
712 {
713 fclose(in);
714 printf("Read error in file %s\n", input_file);
715 exit(1);
716 }
717
718 index = 0;
719
720 write_line("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>");
721 write_line("");
722 write_line("<components>");
723
724 while (1)
725 {
726 /* Free previous list */
727 for (api_info = api_info_list; api_info != NULL; api_info = next_api_info)
728 {
729 next_api_info = api_info->next;
730 free(api_info);
731 }
732 api_info_list = NULL;
733
734 /* Skip whitespace and eol characters */
735 while ((index < size) && (is_whitespace(buffer[index]) || (is_eol_char(buffer[index]))))
736 {
737 index++;
738 }
739 if ((file_pointer < size) && (buffer[index] == '\n'))
740 {
741 index++;
742 }
743
744 if (buffer[index] == ';')
745 {
746 /* Skip comments */
747 while ((index < size) && (!is_eol_char(buffer[index])))
748 {
749 index++;
750 }
751 if ((index < size) && (buffer[index] == '\n'))
752 {
753 index++;
754 }
755 continue;
756 }
757
758 /* Get component name */
759 start = index;
760 while ((index < size) && (!is_whitespace(buffer[index])))
761 {
762 index++;
763 }
764 if (index >= size)
765 {
766 break;
767 }
768
769 len = index - start;
770 strncpy(component_name, &buffer[start], len);
771 component_name[len] = 0;
772
773 /* Skip whitespace */
774 while ((index < size) && (is_whitespace(buffer[index])))
775 {
776 index++;
777 }
778 if (index >= size)
779 {
780 break;
781 }
782
783 /* Get component path */
784 start = index;
785 while ((index < size) && (!is_whitespace(buffer[index]) && !is_eol_char(buffer[index])))
786 {
787 index++;
788 }
789
790 len = index - start;
791 strncpy(component_path, &buffer[start], len);
792 component_path[len] = 0;
793
794 /* Append directory separator if needed */
795 if (component_path[strlen(component_path)] != DIR_SEPARATOR_CHAR)
796 {
797 int i = strlen(component_path);
798 component_path[strlen(component_path)] = DIR_SEPARATOR_CHAR;
799 component_path[i + 1] = 0;
800 }
801
802 /* Skip to end of line */
803 while ((index < size) && (!is_eol_char(buffer[index])))
804 {
805 index++;
806 }
807 if ((index < size) && (buffer[index] == '\n'))
808 {
809 index++;
810 }
811
812 canonical_path = convert_path(component_path);
813 if (canonical_path != NULL)
814 {
815 process_directory(canonical_path);
816 free(canonical_path);
817 generate_xml_for_component(component_name);
818 }
819 }
820
821 write_line("</components>");
822 }
823
824 static char HELP[] =
825 "RGENSTAT input-filename output-filename\n"
826 "\n"
827 " input-filename File containing list of components to process\n"
828 " output-filename File to create\n";
829
830 int main(int argc, char **argv)
831 {
832 char *input_file;
833 char *output_file;
834 int i;
835
836 if (argc < 2)
837 {
838 puts(HELP);
839 return 1;
840 }
841
842 input_file = convert_path(argv[1]);
843 if (input_file[0] == 0)
844 {
845 printf("Missing input-filename\n");
846 return 1;
847 }
848
849 output_file = convert_path(argv[2]);
850 if (output_file[0] == 0)
851 {
852 printf("Missing output-filename\n");
853 return 1;
854 }
855
856 out = fopen(output_file, "wb");
857 if (out == NULL)
858 {
859 printf("Cannot open output file");
860 return 1;
861 }
862
863 read_input_file(input_file);
864
865 fclose(out);
866
867 return 0;
868 }
869
870 /* EOF */