2
0

sim.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: sim.c,v 2.12 2007/08/27 09:57:34 dick Exp $
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "settings.par"
  8. #include "sim.h"
  9. #include "options.h"
  10. #include "language.h"
  11. #include "error.h"
  12. #include "hash.h"
  13. #include "compare.h"
  14. #include "pass1.h"
  15. #include "pass2.h"
  16. #include "pass3.h"
  17. #include "stream.h"
  18. #include "lex.h"
  19. unsigned int MinRunSize = DFLT_MIN_RUN_SIZE;
  20. int PageWidth = DFLT_PAGE_WIDTH;
  21. FILE *OutputFile;
  22. FILE *DebugFile;
  23. struct text *Text; /* to be filled in by malloc */
  24. int NumberOfTexts; /* number of text records */
  25. int NumberOfNewTexts; /* number of new text records */
  26. char *progname; /* for error reporting */
  27. static const char *outputname; /* for reporting */
  28. static const char *minrunstring;
  29. static const char *pagewidthstring;
  30. static const struct option optlist[] = {
  31. {'r', "minimum run size", 'N', &minrunstring},
  32. {'w', "page width", 'N', &pagewidthstring},
  33. {'f', "function-like forms only", ' ', 0},
  34. {'d', "use diff format for output", ' ', 0},
  35. {'p', "use percentage format for output", ' ', 0},
  36. {'e', "compare each file to each file separately", ' ', 0},
  37. {'s', "do not compare a file to itself", ' ', 0},
  38. {'S', "compare new files to old files only", ' ', 0},
  39. {'F', "keep function identifiers in tact", ' ', 0},
  40. {'n', "display headings only", ' ', 0},
  41. {'x', "no pass2 nl_buff allocation", ' ', 0},
  42. {'o', "write output to file F", 'F', &outputname},
  43. {'-', "lexical scan output only", ' ', 0},
  44. {0, 0, 0, 0}
  45. };
  46. static void print_stream(const char *fname);
  47. int
  48. main(int argc, char *argv[]) {
  49. progname = argv[0]; /* save program name */
  50. argv++, argc--; /* and skip it */
  51. /* Set the default output and debug streams */
  52. OutputFile = stdout;
  53. DebugFile = stdout;
  54. /* Get command line options */
  55. { int nop = do_options(progname, optlist, argc, argv);
  56. argc -= nop, argv += nop; /* skip them */
  57. }
  58. /* Treat the value options */
  59. if (minrunstring) {
  60. MinRunSize = strtoul(minrunstring, NULL, 10);
  61. if (MinRunSize == 0) fatal("bad or zero run size; form is: -r N");
  62. }
  63. if (pagewidthstring) {
  64. PageWidth = atoi(pagewidthstring);
  65. if (PageWidth == 0) fatal("bad or zero page width; form is: -w N");
  66. }
  67. if (outputname) {
  68. OutputFile = fopen(outputname, "w");
  69. if (OutputFile == 0) {
  70. char msg[500];
  71. sprintf(msg, "cannot open output file %s", outputname);
  72. fatal(msg);
  73. /*NOTREACHED*/
  74. }
  75. }
  76. if (option_set('-')) {
  77. /* it is the lexical scan only */
  78. while (argv[0]) {
  79. print_stream(argv[0]);
  80. argv++;
  81. }
  82. return 0;
  83. }
  84. /* Start processing */
  85. InitLanguage();
  86. /* Read the input files */
  87. Pass1(argc, argv);
  88. /* Set up the forward reference table */
  89. MakeForwardReferences();
  90. /* Compare the input files to find runs */
  91. Compare();
  92. /* Delete forward reference table */
  93. FreeForwardReferences();
  94. /* Find positions of the runs found */
  95. Pass2();
  96. /* Print the similarities */
  97. Pass3();
  98. return 0;
  99. }
  100. static void
  101. print_stream(const char *fname) {
  102. fprintf(OutputFile, "File %s:", fname);
  103. if (!OpenStream(fname)) {
  104. fprintf(OutputFile, " cannot open\n");
  105. return;
  106. }
  107. fprintf(OutputFile, " showing token stream:\nnl_cnt, tk_cnt: tokens");
  108. lex_token = EOL;
  109. do {
  110. if (TOKEN_EQ(lex_token, EOL)) {
  111. fprintf(OutputFile, "\n%u,%u:",
  112. lex_nl_cnt, lex_tk_cnt
  113. );
  114. }
  115. else {
  116. print_token(OutputFile, lex_token);
  117. }
  118. } while (NextStreamTokenObtained());
  119. fprintf(OutputFile, "\n");
  120. CloseStream();
  121. }